Skip to content

Commit 03a423a

Browse files
committed
updates
1 parent dd1fa20 commit 03a423a

2 files changed

Lines changed: 28 additions & 44 deletions

File tree

src/main.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,8 @@ use anyhow::{Context, Result, anyhow, bail, ensure};
55
use clap::{Parser, Subcommand};
66
use colored::Colorize;
77
use git::{
8-
DEFAULT_REMOTE,
9-
GitBranchStatus,
10-
after_text,
11-
git_branch_status,
12-
git_checkout_main,
13-
git_fetch,
14-
git_get_upstream,
15-
git_remote_main,
16-
git_sha,
17-
is_ancestor,
18-
run_git_status,
19-
shas_match,
8+
DEFAULT_REMOTE, GitBranchStatus, after_text, git_branch_status, git_checkout_main, git_fetch,
9+
git_get_upstream, git_remote_main, git_sha, is_ancestor, run_git_status, shas_match,
2010
};
2111
use state::{Branch, RestackStep, StackMethod};
2212
use tracing::level_filters::LevelFilter;
@@ -51,7 +41,7 @@ enum Command {
5141
fetch: bool,
5242
},
5343
/// Open the git-stack state file in an editor for manual editing.
54-
EditState,
44+
Edit,
5545
/// Restack your active branch and all branches in its related stack.
5646
Restack {
5747
/// The name of the branch to restack.
@@ -153,7 +143,7 @@ fn inner_main() -> Result<()> {
153143
Some(Command::Checkout { branch_name }) => {
154144
state.checkout(&repo, current_branch, current_upstream, branch_name)
155145
}
156-
Some(Command::EditState) => state.edit_config(),
146+
Some(Command::Edit) => state.edit_config(),
157147
Some(Command::Restack {
158148
branch,
159149
fetch,

src/state.rs

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::{
22
cell::{Cell, Ref, RefCell},
33
collections::{BTreeMap, HashMap, VecDeque},
4-
default,
5-
fs,
4+
default, fs,
65
path::PathBuf,
76
process::Command,
87
rc::Rc,
@@ -14,14 +13,8 @@ use serde::{Deserialize, Serialize};
1413

1514
use crate::{
1615
git::{
17-
DEFAULT_REMOTE,
18-
GitTrunk,
19-
after_text,
20-
git_branch_exists,
21-
git_remote_main,
22-
git_sha,
23-
git_trunk,
24-
is_ancestor,
16+
DEFAULT_REMOTE, GitTrunk, after_text, git_branch_exists, git_remote_main, git_sha,
17+
git_trunk, is_ancestor,
2518
},
2619
run_git,
2720
};
@@ -180,11 +173,13 @@ impl State {
180173
};
181174
is_branch_mentioned_in_tree(branch_name, branch)
182175
}
176+
183177
pub fn get_tree_branch<'a>(&'a self, repo: &str, branch_name: &str) -> Option<&'a Branch> {
184178
self.trees
185179
.get(repo)
186180
.and_then(|tree| find_branch_by_name(tree, branch_name))
187181
}
182+
188183
fn get_tree_branch_mut<'a>(
189184
&'a mut self,
190185
repo: &str,
@@ -196,10 +191,10 @@ impl State {
196191
}
197192

198193
pub(crate) fn plan_restack(
199-
&self,
194+
&'_ self,
200195
repo: &str,
201196
starting_branch: &str,
202-
) -> Result<Vec<RestackStep>> {
197+
) -> Result<Vec<RestackStep<'_>>> {
203198
tracing::debug!("Planning restack for {starting_branch}");
204199
let trunk = git_trunk()?;
205200
// Find all the descendents of the starting branch.
@@ -278,11 +273,10 @@ impl State {
278273
if let (Some(Branch { name: name_a, .. }), Some(Branch { name: name_b, .. })) = (
279274
self.get_parent_branch_of(repo, branch_name),
280275
self.get_tree_branch(repo, &parent_branch),
281-
) {
282-
if name_a == name_b {
283-
tracing::warn!("Branch {branch_name} is already mounted on {name_a}");
284-
return Ok(());
285-
}
276+
) && name_a == name_b
277+
{
278+
tracing::warn!("Branch {branch_name} is already mounted on {name_a}");
279+
return Ok(());
286280
}
287281

288282
let current_parent_branch = self.get_parent_branch_of_mut(repo, branch_name);
@@ -349,17 +343,17 @@ impl State {
349343
parent_lkgs.insert(tree_branch.name.clone(), None);
350344
}
351345
}
352-
if is_ancestor(&parent, &branch).unwrap_or(false) {
353-
if let Ok(new_lkg_parent) = git_sha(&parent) {
354-
tracing::debug!(
355-
lkg_parent = ?new_lkg_parent,
356-
"Branch {} is a descendent of {}",
357-
branch.yellow(),
358-
parent.yellow(),
359-
);
360-
// Save the LKG parent for the branch.
361-
parent_lkgs.insert(branch.clone(), Some(new_lkg_parent));
362-
}
346+
if is_ancestor(&parent, &branch).unwrap_or(false)
347+
&& let Ok(new_lkg_parent) = git_sha(&parent)
348+
{
349+
tracing::debug!(
350+
lkg_parent = ?new_lkg_parent,
351+
"Branch {} is a descendent of {}",
352+
branch.yellow(),
353+
parent.yellow(),
354+
);
355+
// Save the LKG parent for the branch.
356+
parent_lkgs.insert(branch.clone(), Some(new_lkg_parent));
363357
}
364358
}
365359
if let Some(branch) = self.get_tree_branch(repo, &branch) {
@@ -551,7 +545,7 @@ mod tests {
551545
"/tmp/foo".to_string(),
552546
super::Branch {
553547
name: "main".to_string(),
554-
stack_method: Some(super::StackMethod::ApplyMerge),
548+
stack_method: super::StackMethod::ApplyMerge,
555549
note: None,
556550
lkg_parent: None,
557551
branches: vec![],
@@ -574,6 +568,6 @@ mod tests {
574568
assert!(state.trees.contains_key("/tmp/foo"));
575569
let tree = state.trees.get("/tmp/foo").unwrap();
576570
assert_eq!(tree.name, "main");
577-
assert_eq!(tree.stack_method, Some(super::StackMethod::ApplyMerge));
571+
assert_eq!(tree.stack_method, super::StackMethod::ApplyMerge);
578572
}
579573
}

0 commit comments

Comments
 (0)