Skip to content

Commit 12fe3ec

Browse files
committed
Fix tests
1 parent ec34891 commit 12fe3ec

File tree

8 files changed

+250
-110
lines changed

8 files changed

+250
-110
lines changed

src/cli.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ pub enum StashBranchAction {
110110
},
111111
#[clap(about = "Clean old stashes")]
112112
Clean {
113-
#[clap(long = "older-than", help = "Remove stashes older than (e.g., '7d', '2w', '1m')")]
113+
#[clap(
114+
long = "older-than",
115+
help = "Remove stashes older than (e.g., '7d', '2w', '1m')"
116+
)]
114117
older_than: Option<String>,
115118
#[clap(long = "dry-run", help = "Show what would be cleaned without doing it", action = clap::ArgAction::SetTrue)]
116119
dry_run: bool,

src/fixup.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ pub fn run(commit_hash: String, rebase: bool) {
5757
// Helper function to validate commit hash exists
5858
fn validate_commit_hash(commit_hash: &str) -> Result<(), &'static str> {
5959
let output = Command::new("git")
60-
.args(["rev-parse", "--verify", &format!("{commit_hash}^{{commit}}")])
60+
.args([
61+
"rev-parse",
62+
"--verify",
63+
&format!("{commit_hash}^{{commit}}"),
64+
])
6165
.output()
6266
.map_err(|_| "Failed to validate commit hash")?;
6367

@@ -216,4 +220,4 @@ pub fn get_commit_hash_validation_rules() -> &'static [&'static str] {
216220
"Must contain only hex characters (0-9, a-f)",
217221
"Must reference an existing commit",
218222
]
219-
}
223+
}

src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ fn main() {
2525
Commands::Sync { merge } => sync::run(merge),
2626
Commands::New { branch_name, from } => new_branch::run(branch_name, from),
2727
Commands::LargeFiles { limit, threshold } => large_files::run(limit, threshold),
28-
Commands::Fixup { commit_hash, rebase } => fixup::run(commit_hash, rebase),
28+
Commands::Fixup {
29+
commit_hash,
30+
rebase,
31+
} => fixup::run(commit_hash, rebase),
2932
Commands::StashBranch { action } => stash_branch::run(action),
3033
Commands::Upstream { action } => upstream::run(action),
3134
}

src/stash_branch.rs

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ use std::process::Command;
33

44
pub fn run(action: StashBranchAction) {
55
match action {
6-
StashBranchAction::Create { branch_name, stash_ref } => {
7-
create_branch_from_stash(branch_name, stash_ref)
8-
}
9-
StashBranchAction::Clean { older_than, dry_run } => {
10-
clean_old_stashes(older_than, dry_run)
11-
}
12-
StashBranchAction::ApplyByBranch { branch_name, list_only } => {
13-
apply_stashes_by_branch(branch_name, list_only)
14-
}
6+
StashBranchAction::Create {
7+
branch_name,
8+
stash_ref,
9+
} => create_branch_from_stash(branch_name, stash_ref),
10+
StashBranchAction::Clean {
11+
older_than,
12+
dry_run,
13+
} => clean_old_stashes(older_than, dry_run),
14+
StashBranchAction::ApplyByBranch {
15+
branch_name,
16+
list_only,
17+
} => apply_stashes_by_branch(branch_name, list_only),
1518
}
1619
}
1720

@@ -81,7 +84,10 @@ fn clean_old_stashes(older_than: Option<String>, dry_run: bool) {
8184
return;
8285
}
8386

84-
println!("{}", format_stashes_to_clean_message(stashes_to_clean.len(), dry_run));
87+
println!(
88+
"{}",
89+
format_stashes_to_clean_message(stashes_to_clean.len(), dry_run)
90+
);
8591

8692
for stash in &stashes_to_clean {
8793
println!(" {}", format_stash_entry(&stash.name, &stash.message));
@@ -90,10 +96,16 @@ fn clean_old_stashes(older_than: Option<String>, dry_run: bool) {
9096
if !dry_run {
9197
for stash in &stashes_to_clean {
9298
if let Err(msg) = delete_stash(&stash.name) {
93-
eprintln!("{}", format_error_message(&format!("Failed to delete {}: {}", stash.name, msg)));
99+
eprintln!(
100+
"{}",
101+
format_error_message(&format!("Failed to delete {}: {}", stash.name, msg))
102+
);
94103
}
95104
}
96-
println!("{}", format_cleanup_complete_message(stashes_to_clean.len()));
105+
println!(
106+
"{}",
107+
format_cleanup_complete_message(stashes_to_clean.len())
108+
);
97109
}
98110
}
99111

@@ -119,13 +131,19 @@ fn apply_stashes_by_branch(branch_name: String, list_only: bool) {
119131
}
120132

121133
if list_only {
122-
println!("{}", format_stashes_for_branch_header(&branch_name, branch_stashes.len()));
134+
println!(
135+
"{}",
136+
format_stashes_for_branch_header(&branch_name, branch_stashes.len())
137+
);
123138
for stash in &branch_stashes {
124139
println!(" {}", format_stash_entry(&stash.name, &stash.message));
125140
}
126141
} else {
127-
println!("{}", format_applying_stashes_message(&branch_name, branch_stashes.len()));
128-
142+
println!(
143+
"{}",
144+
format_applying_stashes_message(&branch_name, branch_stashes.len())
145+
);
146+
129147
for stash in &branch_stashes {
130148
match apply_stash(&stash.name) {
131149
Ok(()) => println!(" ✅ Applied {}", stash.name),
@@ -168,7 +186,12 @@ fn validate_branch_name(name: &str) -> Result<(), &'static str> {
168186
// Helper function to check if branch exists
169187
fn branch_exists(branch_name: &str) -> bool {
170188
Command::new("git")
171-
.args(["show-ref", "--verify", "--quiet", &format!("refs/heads/{branch_name}")])
189+
.args([
190+
"show-ref",
191+
"--verify",
192+
"--quiet",
193+
&format!("refs/heads/{branch_name}"),
194+
])
172195
.status()
173196
.map(|status| status.success())
174197
.unwrap_or(false)
@@ -287,7 +310,7 @@ fn extract_branch_from_message(message: &str) -> String {
287310
return rest[..end].to_string();
288311
}
289312
}
290-
313+
291314
if let Some(start) = message.find("WIP on ") {
292315
let rest = &message[start + 7..];
293316
if let Some(end) = rest.find(':') {
@@ -399,4 +422,4 @@ pub fn format_applying_stashes_message(branch_name: &str, count: usize) -> Strin
399422

400423
pub fn format_stash_entry(name: &str, message: &str) -> String {
401424
format!("{name}: {message}")
402-
}
425+
}

src/upstream.rs

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,21 @@ fn set_upstream(upstream: String) {
3232
}
3333
};
3434

35-
println!("{}", format_setting_upstream_message(&current_branch, &upstream));
35+
println!(
36+
"{}",
37+
format_setting_upstream_message(&current_branch, &upstream)
38+
);
3639

3740
// Set upstream
3841
if let Err(msg) = set_branch_upstream(&current_branch, &upstream) {
3942
eprintln!("{}", format_error_message(msg));
4043
return;
4144
}
4245

43-
println!("{}", format_upstream_set_message(&current_branch, &upstream));
46+
println!(
47+
"{}",
48+
format_upstream_set_message(&current_branch, &upstream)
49+
);
4450
}
4551

4652
fn show_upstream_status() {
@@ -72,18 +78,21 @@ fn show_upstream_status() {
7278
let current_branch = get_current_branch().unwrap_or_default();
7379

7480
println!("{}", format_upstream_status_header());
75-
81+
7682
for branch in &branches {
7783
let is_current = branch == &current_branch;
7884
let upstream = branch_upstreams.get(branch).unwrap();
79-
85+
8086
match upstream {
8187
Some(upstream_ref) => {
8288
// Check sync status
83-
let sync_status = get_branch_sync_status(branch, upstream_ref)
84-
.unwrap_or_else(|_| SyncStatus::Unknown);
85-
86-
println!("{}", format_branch_with_upstream(branch, upstream_ref, &sync_status, is_current));
89+
let sync_status =
90+
get_branch_sync_status(branch, upstream_ref).unwrap_or(SyncStatus::Unknown);
91+
92+
println!(
93+
"{}",
94+
format_branch_with_upstream(branch, upstream_ref, &sync_status, is_current)
95+
);
8796
}
8897
None => {
8998
println!("{}", format_branch_without_upstream(branch, is_current));
@@ -107,15 +116,21 @@ fn sync_all_branches(dry_run: bool, merge: bool) {
107116
return;
108117
}
109118

110-
println!("{}", format_sync_all_start_message(branches.len(), dry_run, merge));
119+
println!(
120+
"{}",
121+
format_sync_all_start_message(branches.len(), dry_run, merge)
122+
);
111123

112124
let mut sync_results = Vec::new();
113125

114126
for (branch, upstream) in &branches {
115127
let sync_status = match get_branch_sync_status(branch, upstream) {
116128
Ok(status) => status,
117129
Err(_) => {
118-
sync_results.push((branch.clone(), SyncResult::Error("Failed to get sync status".to_string())));
130+
sync_results.push((
131+
branch.clone(),
132+
SyncResult::Error("Failed to get sync status".to_string()),
133+
));
119134
continue;
120135
}
121136
};
@@ -130,15 +145,20 @@ fn sync_all_branches(dry_run: bool, merge: bool) {
130145
} else {
131146
match sync_branch_with_upstream(branch, upstream, merge) {
132147
Ok(()) => sync_results.push((branch.clone(), SyncResult::Synced)),
133-
Err(msg) => sync_results.push((branch.clone(), SyncResult::Error(msg.to_string()))),
148+
Err(msg) => {
149+
sync_results.push((branch.clone(), SyncResult::Error(msg.to_string())))
150+
}
134151
}
135152
}
136153
}
137154
SyncStatus::Ahead(_) => {
138155
sync_results.push((branch.clone(), SyncResult::Ahead));
139156
}
140157
SyncStatus::Unknown => {
141-
sync_results.push((branch.clone(), SyncResult::Error("Unknown sync status".to_string())));
158+
sync_results.push((
159+
branch.clone(),
160+
SyncResult::Error("Unknown sync status".to_string()),
161+
));
142162
}
143163
}
144164
}
@@ -150,7 +170,10 @@ fn sync_all_branches(dry_run: bool, merge: bool) {
150170
}
151171

152172
// Print summary
153-
let synced_count = sync_results.iter().filter(|(_, r)| matches!(r, SyncResult::Synced | SyncResult::WouldSync)).count();
173+
let synced_count = sync_results
174+
.iter()
175+
.filter(|(_, r)| matches!(r, SyncResult::Synced | SyncResult::WouldSync))
176+
.count();
154177
println!("{}", format_sync_summary(synced_count, dry_run));
155178
}
156179

@@ -270,7 +293,12 @@ fn get_branch_upstream(branch: &str) -> Result<String, &'static str> {
270293
// Helper function to get branch sync status
271294
fn get_branch_sync_status(branch: &str, upstream: &str) -> Result<SyncStatus, &'static str> {
272295
let output = Command::new("git")
273-
.args(["rev-list", "--left-right", "--count", &format!("{upstream}...{branch}")])
296+
.args([
297+
"rev-list",
298+
"--left-right",
299+
"--count",
300+
&format!("{upstream}...{branch}"),
301+
])
274302
.output()
275303
.map_err(|_| "Failed to get sync status")?;
276304

@@ -279,13 +307,13 @@ fn get_branch_sync_status(branch: &str, upstream: &str) -> Result<SyncStatus, &'
279307
}
280308

281309
let counts = String::from_utf8_lossy(&output.stdout);
282-
let mut parts = counts.trim().split_whitespace();
283-
310+
let mut parts = counts.split_whitespace();
311+
284312
let behind: u32 = parts
285313
.next()
286314
.and_then(|s| s.parse().ok())
287315
.ok_or("Invalid sync count format")?;
288-
316+
289317
let ahead: u32 = parts
290318
.next()
291319
.and_then(|s| s.parse().ok())
@@ -315,7 +343,11 @@ fn get_branches_with_upstreams() -> Result<Vec<(String, String)>, &'static str>
315343
}
316344

317345
// Helper function to sync branch with upstream
318-
fn sync_branch_with_upstream(branch: &str, upstream: &str, merge: bool) -> Result<(), &'static str> {
346+
fn sync_branch_with_upstream(
347+
branch: &str,
348+
upstream: &str,
349+
merge: bool,
350+
) -> Result<(), &'static str> {
319351
// Switch to the branch first
320352
let status = Command::new("git")
321353
.args(["checkout", branch])
@@ -339,7 +371,11 @@ fn sync_branch_with_upstream(branch: &str, upstream: &str, merge: bool) -> Resul
339371
.map_err(|_| "Failed to sync with upstream")?;
340372

341373
if !status.success() {
342-
return Err(if merge { "Merge failed" } else { "Rebase failed" });
374+
return Err(if merge {
375+
"Merge failed"
376+
} else {
377+
"Rebase failed"
378+
});
343379
}
344380

345381
Ok(())
@@ -371,7 +407,12 @@ pub fn format_upstream_status_header() -> &'static str {
371407
"🔗 Upstream status for all branches:\n"
372408
}
373409

374-
pub fn format_branch_with_upstream(branch: &str, upstream: &str, sync_status: &SyncStatus, is_current: bool) -> String {
410+
pub fn format_branch_with_upstream(
411+
branch: &str,
412+
upstream: &str,
413+
sync_status: &SyncStatus,
414+
is_current: bool,
415+
) -> String {
375416
let current_indicator = if is_current { "* " } else { " " };
376417
let status_text = match sync_status {
377418
SyncStatus::UpToDate => "✅ up-to-date",
@@ -380,7 +421,7 @@ pub fn format_branch_with_upstream(branch: &str, upstream: &str, sync_status: &S
380421
SyncStatus::Diverged(b, a) => &format!("🔀 {b} behind, {a} ahead"),
381422
SyncStatus::Unknown => "❓ unknown",
382423
};
383-
424+
384425
format!("{current_indicator}{branch} -> {upstream} ({status_text})")
385426
}
386427

@@ -418,8 +459,10 @@ pub fn format_sync_result_line(branch: &str, result: &SyncResult) -> String {
418459

419460
pub fn format_sync_summary(synced_count: usize, dry_run: bool) -> String {
420461
if dry_run {
421-
format!("\n💡 Would sync {synced_count} branch(es). Run without --dry-run to apply changes.")
462+
format!(
463+
"\n💡 Would sync {synced_count} branch(es). Run without --dry-run to apply changes."
464+
)
422465
} else {
423466
format!("\n✅ Synced {synced_count} branch(es) successfully.")
424467
}
425-
}
468+
}

0 commit comments

Comments
 (0)