Skip to content

Commit 53682ef

Browse files
committed
add fetch option to status and sort to put current branch at the top
1 parent 225d6ef commit 53682ef

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

src/main.rs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ enum Command {
4747
/// The name of the branch to restack.
4848
#[arg(long, short)]
4949
branch: Option<String>,
50+
/// Whether to fetch the latest changes from the remote before restacking.
51+
#[arg(long, short, default_value_t = false)]
52+
fetch: bool,
5053
/// Push any changes up to the remote after restacking.
5154
#[arg(long, short)]
5255
push: bool,
@@ -141,9 +144,19 @@ fn inner_main() -> Result<()> {
141144
Some(Command::Checkout { branch_name }) => {
142145
state.checkout(&repo, current_branch, current_upstream, branch_name)
143146
}
144-
Some(Command::Restack { branch, push }) => {
145-
restack(state, &repo, run_version, branch, current_branch, push)
146-
}
147+
Some(Command::Restack {
148+
branch,
149+
fetch,
150+
push,
151+
}) => restack(
152+
state,
153+
&repo,
154+
run_version,
155+
branch,
156+
current_branch,
157+
fetch,
158+
push,
159+
),
147160
Some(Command::Mount { parent_branch }) => {
148161
state.mount(&repo, &current_branch, parent_branch)
149162
}
@@ -307,12 +320,8 @@ fn recur_tree(
307320

308321
let first_line = note.lines().next().unwrap_or("");
309322
println!(
310-
"{} {}",
311-
if is_current_branch {
312-
"🗒 ".truecolor(155, 155, 150)
313-
} else {
314-
"›".truecolor(55, 55, 50)
315-
},
323+
" {} {}",
324+
"›".truecolor(55, 55, 50),
316325
if is_current_branch {
317326
first_line.bright_blue().bold()
318327
} else {
@@ -321,7 +330,18 @@ fn recur_tree(
321330
);
322331
}
323332

324-
for child in &branch.branches {
333+
let mut branches_sorted = branch.branches.iter().collect::<Vec<_>>();
334+
branches_sorted.sort_by(|&a, &b| {
335+
let a_is_ancestor = is_ancestor(&a.name, orig_branch).unwrap_or(false);
336+
let b_is_ancestor = is_ancestor(&b.name, orig_branch).unwrap_or(false);
337+
match (a_is_ancestor, b_is_ancestor) {
338+
(true, true) => a.name.cmp(&b.name),
339+
(true, false) => std::cmp::Ordering::Less,
340+
(false, true) => std::cmp::Ordering::Greater,
341+
(false, false) => a.name.cmp(&b.name),
342+
}
343+
});
344+
for child in branches_sorted {
325345
recur_tree(child, depth + 1, orig_branch, Some(branch.name.as_ref()))?;
326346
}
327347
Ok(())
@@ -358,10 +378,15 @@ fn restack(
358378
run_version: String,
359379
restack_branch: Option<String>,
360380
orig_branch: String,
381+
fetch: bool,
361382
push: bool,
362383
) -> Result<(), anyhow::Error> {
363384
let restack_branch = restack_branch.unwrap_or(orig_branch.clone());
364385

386+
if fetch {
387+
git_fetch()?;
388+
}
389+
365390
// Find starting_branch in the stacks of branches to determine which stack to use.
366391
let plan = state.plan_restack(repo, &restack_branch)?;
367392

0 commit comments

Comments
 (0)