Skip to content

Commit c433e3d

Browse files
committed
formatted, linted, removed or clarified dead code
Signed-off-by: Adam Poulemanos <[email protected]>
1 parent ed4624c commit c433e3d

14 files changed

+1159
-1479
lines changed

hk.pkl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
amends "package://github.com/jdx/hk/releases/download/v1.2.0/[email protected]#/Config.pkl"
2+
import "package://github.com/jdx/hk/releases/download/v1.2.0/[email protected]#/Builtins.pkl"
3+
4+
local linters = new Mapping<String, Step> {
5+
// uses builtin prettier linter config
6+
["prettier"] = Builtins.prettier
7+
8+
// define a custom linter
9+
["pkl"] {
10+
glob = "*.pkl"
11+
check = "pkl eval {{files}} >/dev/null"
12+
}
13+
}
14+
15+
hooks {
16+
["pre-commit"] {
17+
fix = true // automatically modify files with available linter fixes
18+
stash = "git" // stashes unstaged changes while running fix steps
19+
steps {
20+
// "prelint" here is simply a name to define the step
21+
["prelint"] {
22+
// if a step has a "check" script it will execute that
23+
check = "mise run prelint"
24+
exclusive = true // ensures that the step runs in isolation
25+
}
26+
...linters // add all linters defined above
27+
["postlint"] {
28+
check = "mise run postlint"
29+
exclusive = true
30+
}
31+
}
32+
}
33+
// instead of pre-commit, you can instead define pre-push hooks
34+
["pre-push"] {
35+
steps = linters
36+
}
37+
// "fix" and "check" are special steps for `hk fix` and `hk check` commands
38+
["fix"] {
39+
fix = true
40+
steps = linters
41+
}
42+
["check"] {
43+
steps = linters
44+
}
45+
}

mise.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ cargo-binstall = "latest" # For installing binaries from crates.io
55
"cargo:cargo-deny" = "latest"
66
"cargo:cargo-nextest" = "latest"
77
"cargo:cargo-watch" = "latest"
8+
hk = "latest"
89
rust = "1.87" # The minimum Rust version we support; mise just makes sure it's there.
910
uv = "latest" # Another runner for MCP servers.
1011

src/commands.rs

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use clap::{Parser, Subcommand};
22
use std::path::PathBuf;
33

4-
54
#[derive(Parser)]
65
#[command(name = "submod")]
76
#[command(about = "Manage git submodules with sparse checkout support")]
@@ -15,36 +14,36 @@ pub struct Cli {
1514

1615
#[derive(Subcommand)]
1716
pub enum Commands {
18-
/// Add a new submodule configuration
19-
Add {
20-
/// Submodule name
21-
name: String,
22-
/// Local path for the submodule
23-
path: String,
24-
/// Git repository URL
25-
url: String,
26-
/// Sparse checkout paths (comma-separated)
27-
#[arg(short, long)]
28-
sparse_paths: Option<String>,
29-
/// Git settings like "ignore=all"
30-
#[arg(short = 'S', long)]
31-
settings: Option<String>,
32-
},
33-
/// Check submodule status and configuration
34-
Check,
35-
/// Initialize missing submodules
36-
Init,
37-
/// Update all submodules
38-
Update,
39-
/// Hard reset submodules (stash, reset --hard, clean)
40-
Reset {
41-
/// Reset all submodules
42-
#[arg(short, long)]
43-
all: bool,
44-
/// Specific submodule names to reset
45-
#[arg(required_unless_present = "all")]
46-
names: Vec<String>,
47-
},
48-
/// Run full sync: check, init, update
49-
Sync,
17+
/// Add a new submodule configuration
18+
Add {
19+
/// Submodule name
20+
name: String,
21+
/// Local path for the submodule
22+
path: String,
23+
/// Git repository URL
24+
url: String,
25+
/// Sparse checkout paths (comma-separated)
26+
#[arg(short, long)]
27+
sparse_paths: Option<String>,
28+
/// Git settings like "ignore=all"
29+
#[arg(short = 'S', long)]
30+
settings: Option<String>,
31+
},
32+
/// Check submodule status and configuration
33+
Check,
34+
/// Initialize missing submodules
35+
Init,
36+
/// Update all submodules
37+
Update,
38+
/// Hard reset submodules (stash, reset --hard, clean)
39+
Reset {
40+
/// Reset all submodules
41+
#[arg(short, long)]
42+
all: bool,
43+
/// Specific submodule names to reset
44+
#[arg(required_unless_present = "all")]
45+
names: Vec<String>,
46+
},
47+
/// Run full sync: check, init, update
48+
Sync,
5049
}

src/config.rs

Lines changed: 62 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use anyhow::{Context, Result};
2-
use std::{collections::HashMap, path::Path};
3-
use std::fs;
42
use bstr::BStr;
53
use gix_submodule::config::{Branch, FetchRecurse, Ignore, Update};
6-
use toml_edit::{DocumentMut, Item, Table, Array, value};
74
use serde::{Deserialize, Serialize};
5+
use std::fs;
6+
use std::{collections::HashMap, path::Path};
7+
use toml_edit::{Array, DocumentMut, Item, Table, value};
88

99
/**========================================================================
1010
** Wrappers for Gix Submodule Config
@@ -54,10 +54,9 @@ impl<'de> Deserialize<'de> for SerializableIgnore {
5454
// Convert String to BStr for the TryFrom implementation
5555
let bstr = BStr::new(s.as_bytes());
5656
match Ignore::try_from(bstr) {
57-
Ok(ignore) => Ok(SerializableIgnore(ignore)),
58-
Err(_) => Err(serde::de::Error::custom(format!(
59-
"Invalid ignore value: {}",
60-
s
57+
Ok(ignore) => Ok(Self(ignore)),
58+
Err(()) => Err(serde::de::Error::custom(format!(
59+
"Invalid ignore value: {s}"
6160
))),
6261
}
6362
}
@@ -86,12 +85,11 @@ impl<'de> Deserialize<'de> for SerializableFetchRecurse {
8685
{
8786
let s = String::deserialize(deserializer)?;
8887
match s.as_str() {
89-
"on-demand" => Ok(SerializableFetchRecurse(FetchRecurse::OnDemand)),
90-
"always" => Ok(SerializableFetchRecurse(FetchRecurse::Always)),
91-
"never" => Ok(SerializableFetchRecurse(FetchRecurse::Never)),
88+
"on-demand" => Ok(Self(FetchRecurse::OnDemand)),
89+
"always" => Ok(Self(FetchRecurse::Always)),
90+
"never" => Ok(Self(FetchRecurse::Never)),
9291
_ => Err(serde::de::Error::custom(format!(
93-
"Invalid fetch recurse value: {}",
94-
s
92+
"Invalid fetch recurse value: {s}"
9593
))),
9694
}
9795
}
@@ -120,10 +118,9 @@ impl<'de> Deserialize<'de> for SerializableBranch {
120118
// Convert String to BStr for the TryFrom implementation
121119
let bstr = BStr::new(s.as_bytes());
122120
match Branch::try_from(bstr) {
123-
Ok(branch) => Ok(SerializableBranch(branch)),
121+
Ok(branch) => Ok(Self(branch)),
124122
Err(e) => Err(serde::de::Error::custom(format!(
125-
"Invalid branch value '{}': {}",
126-
s, e
123+
"Invalid branch value '{s}': {e}"
127124
))),
128125
}
129126
}
@@ -142,7 +139,7 @@ impl Serialize for SerializableUpdate {
142139
Update::None => serializer.serialize_str("none"),
143140
Update::Command(cmd) => {
144141
// Convert BString to String with ! prefix
145-
let cmd_str = format!("!{}", cmd.to_string());
142+
let cmd_str = format!("!{cmd}");
146143
serializer.serialize_str(&cmd_str)
147144
}
148145
}
@@ -159,10 +156,9 @@ impl<'de> Deserialize<'de> for SerializableUpdate {
159156
// Convert String to BStr for the TryFrom implementation
160157
let bstr = BStr::new(s.as_bytes());
161158
match Update::try_from(bstr) {
162-
Ok(update) => Ok(SerializableUpdate(update)),
163-
Err(_) => Err(serde::de::Error::custom(format!(
164-
"Invalid update value: {}",
165-
s
159+
Ok(update) => Ok(Self(update)),
160+
Err(()) => Err(serde::de::Error::custom(format!(
161+
"Invalid update value: {s}"
166162
))),
167163
}
168164
}
@@ -246,6 +242,7 @@ pub struct SubmoduleGitOptions {
246242
impl SubmoduleGitOptions {
247243
/// Create a new instance with default git options
248244
#[allow(dead_code)]
245+
#[must_use]
249246
pub fn new() -> Self {
250247
Self {
251248
ignore: Some(SerializableIgnore(Ignore::default())),
@@ -262,6 +259,7 @@ pub struct SubmoduleDefaults(pub SubmoduleGitOptions);
262259
impl SubmoduleDefaults {
263260
/// Create new default submodule configuration
264261
#[allow(dead_code)]
262+
#[must_use]
265263
pub fn new() -> Self {
266264
Self(SubmoduleGitOptions::new())
267265
}
@@ -286,6 +284,7 @@ pub struct SubmoduleConfig {
286284
impl SubmoduleConfig {
287285
/// Create a new submodule configuration with defaults
288286
#[allow(dead_code)]
287+
#[must_use]
289288
pub fn new() -> Self {
290289
Self {
291290
git_options: SubmoduleGitOptions::new(),
@@ -298,10 +297,10 @@ impl SubmoduleConfig {
298297
/// Check if our active setting matches what git would report
299298
/// `git_active_state` should be the result of calling git's active check
300299
#[allow(dead_code)]
301-
pub fn active_setting_matches_git(&self, git_active_state: bool) -> bool {
300+
#[must_use]
301+
pub const fn active_setting_matches_git(&self, git_active_state: bool) -> bool {
302302
self.active == git_active_state
303303
}
304-
305304
}
306305

307306
/// Main configuration structure for the submod tool
@@ -315,12 +314,11 @@ pub struct Config {
315314
pub submodules: HashMap<String, SubmoduleConfig>,
316315
}
317316

318-
319317
impl Config {
320318
/// Load configuration from a TOML file
321319
pub fn load(path: &Path) -> Result<Self> {
322320
if !path.exists() {
323-
return Ok(Config {
321+
return Ok(Self {
324322
defaults: SubmoduleDefaults::default(),
325323
submodules: HashMap::new(),
326324
});
@@ -329,8 +327,7 @@ impl Config {
329327
let content = fs::read_to_string(path)
330328
.with_context(|| format!("Failed to read config file: {}", path.display()))?;
331329

332-
toml::from_str(&content)
333-
.with_context(|| "Failed to parse TOML config")
330+
toml::from_str(&content).with_context(|| "Failed to parse TOML config")
334331
}
335332

336333
/// Save configuration to a TOML file
@@ -343,14 +340,15 @@ impl Config {
343340
let mut doc = if path.exists() {
344341
let content = fs::read_to_string(path)
345342
.with_context(|| format!("Failed to read existing config: {}", path.display()))?;
346-
content.parse::<DocumentMut>()
343+
content
344+
.parse::<DocumentMut>()
347345
.with_context(|| "Failed to parse existing TOML document")?
348346
} else {
349347
// Create a beautiful new document with header comment
350348
let mut doc = DocumentMut::new();
351349
doc.insert(
352350
"# Submodule configuration for gitoxide-based submodule manager",
353-
Item::None
351+
Item::None,
354352
);
355353
doc.insert("# Each section [name] defines a submodule", Item::None);
356354
doc.insert("", Item::None); // Empty line for spacing
@@ -386,7 +384,8 @@ impl Config {
386384
}
387385

388386
// Remove existing submodule sections but preserve defaults and comments
389-
let keys_to_remove: Vec<String> = doc.iter()
387+
let keys_to_remove: Vec<String> = doc
388+
.iter()
390389
.filter_map(|(key, _)| {
391390
if key != "defaults" && self.submodules.contains_key(key) {
392391
Some(key.to_string())
@@ -455,11 +454,11 @@ impl Config {
455454
Ok(())
456455
}
457456

458-
fn defaults_are_empty(&self) -> bool {
459-
self.defaults.0.ignore.is_none() &&
460-
self.defaults.0.update.is_none() &&
461-
self.defaults.0.branch.is_none() &&
462-
self.defaults.0.fetch_recurse.is_none()
457+
const fn defaults_are_empty(&self) -> bool {
458+
self.defaults.0.ignore.is_none()
459+
&& self.defaults.0.update.is_none()
460+
&& self.defaults.0.branch.is_none()
461+
&& self.defaults.0.fetch_recurse.is_none()
463462
}
464463

465464
/// Add a submodule configuration
@@ -473,29 +472,40 @@ impl Config {
473472
}
474473

475474
/// Get the effective setting for a submodule, falling back to defaults
476-
pub fn get_effective_setting(&self, submodule: &SubmoduleConfig, setting: &str) -> Option<String> {
475+
#[must_use]
476+
pub fn get_effective_setting(
477+
&self,
478+
submodule: &SubmoduleConfig,
479+
setting: &str,
480+
) -> Option<String> {
477481
// Check submodule-specific setting first, then fall back to defaults
478482
match setting {
479483
"ignore" => {
480-
submodule.git_options.ignore.as_ref()
484+
submodule
485+
.git_options
486+
.ignore
487+
.as_ref()
481488
.or(self.defaults.0.ignore.as_ref())
482-
.map(|s| format!("{:?}", s)) // Convert to string representation
483-
}
484-
"update" => {
485-
submodule.git_options.update.as_ref()
486-
.or(self.defaults.0.update.as_ref())
487-
.map(|s| format!("{:?}", s))
488-
}
489-
"branch" => {
490-
submodule.git_options.branch.as_ref()
491-
.or(self.defaults.0.branch.as_ref())
492-
.map(|s| format!("{:?}", s))
493-
}
494-
"fetchRecurse" => {
495-
submodule.git_options.fetch_recurse.as_ref()
496-
.or(self.defaults.0.fetch_recurse.as_ref())
497-
.map(|s| format!("{:?}", s))
489+
.map(|s| format!("{s:?}")) // Convert to string representation
498490
}
491+
"update" => submodule
492+
.git_options
493+
.update
494+
.as_ref()
495+
.or(self.defaults.0.update.as_ref())
496+
.map(|s| format!("{s:?}")),
497+
"branch" => submodule
498+
.git_options
499+
.branch
500+
.as_ref()
501+
.or(self.defaults.0.branch.as_ref())
502+
.map(|s| format!("{s:?}")),
503+
"fetchRecurse" => submodule
504+
.git_options
505+
.fetch_recurse
506+
.as_ref()
507+
.or(self.defaults.0.fetch_recurse.as_ref())
508+
.map(|s| format!("{s:?}")),
499509
_ => None,
500510
}
501511
}

0 commit comments

Comments
 (0)