Skip to content

Commit 097e3af

Browse files
committed
allow deletes
1 parent fc4af4c commit 097e3af

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

src/main.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ enum Commands {
4141
Restack,
4242
/// Create a new branch and make it a descendent of the current branch.
4343
Checkout { branch_name: String },
44+
/// Delete a branch from the git-stack tree.
45+
Delete { branch_name: String },
4446
}
4547

4648
fn main() {
@@ -92,6 +94,7 @@ fn inner_main() -> Result<()> {
9294
}
9395
Commands::Restack => restack(state, &repo, run_version, current_branch),
9496
Commands::Status => status(state, &repo, &current_branch),
97+
Commands::Delete { branch_name } => state.delete_branch(&repo, &branch_name),
9598
}
9699
}
97100

@@ -151,14 +154,14 @@ fn recur_tree(
151154
)
152155
}
153156
} else {
154-
"does not exist".red().to_string()
157+
"does not exist!".bright_red().to_string()
155158
};
156159
details
157160
},
158161
{
159162
if let Some(upstream_status) = branch_status.upstream_status {
160163
format!(
161-
" ({} {})",
164+
" (upstream {} {})",
162165
upstream_status.symbolic_name.truecolor(88, 88, 88),
163166
if upstream_status.synced {
164167
"synced".truecolor(142, 192, 124)

src/state.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl State {
112112
) -> Option<&'a mut Branch> {
113113
self.trees
114114
.get_mut(repo)
115-
.and_then(|tree| get_branch_mut(tree, branch_name))
115+
.and_then(|tree| find_branch_by_name_mut(tree, branch_name))
116116
}
117117

118118
pub(crate) fn plan_restack(
@@ -138,6 +138,20 @@ impl State {
138138
})
139139
.collect::<Vec<_>>())
140140
}
141+
142+
pub(crate) fn delete_branch(&mut self, repo: &str, branch_name: &str) -> Result<()> {
143+
let Some(tree) = self
144+
.trees
145+
.get_mut(repo)
146+
.and_then(|tree| find_parent_of_branch_mut(tree, branch_name))
147+
else {
148+
bail!("Branch {branch_name} not found in the git-stack tree.");
149+
};
150+
tree.branches.retain(|branch| branch.name != branch_name);
151+
save_state(self)?;
152+
153+
Ok(())
154+
}
141155
}
142156

143157
fn get_path<'a>(branch: &'a Branch, target_branch: &str, path: &mut Vec<&'a str>) -> bool {
@@ -160,12 +174,25 @@ pub(crate) struct RebaseStep {
160174
pub(crate) branch: String,
161175
}
162176

163-
fn get_branch_mut<'a>(tree: &'a mut Branch, name: &str) -> Option<&'a mut Branch> {
164-
if tree.name == name {
177+
fn find_branch_by_name_mut<'a>(tree: &'a mut Branch, name: &str) -> Option<&'a mut Branch> {
178+
find_branch_mut(tree, &|branch| branch.name == name)
179+
}
180+
181+
fn find_parent_of_branch_mut<'a>(tree: &'a mut Branch, name: &str) -> Option<&'a mut Branch> {
182+
find_branch_mut(tree, &|branch| {
183+
branch.branches.iter().any(|branch| branch.name == name)
184+
})
185+
}
186+
187+
fn find_branch_mut<'a, F>(tree: &'a mut Branch, pred: &F) -> Option<&'a mut Branch>
188+
where
189+
F: Fn(&Branch) -> bool,
190+
{
191+
if pred(tree) {
165192
Some(tree)
166193
} else {
167194
for child_branch in tree.branches.iter_mut() {
168-
let result = get_branch_mut(child_branch, name);
195+
let result = find_branch_mut(child_branch, pred);
169196
if result.is_some() {
170197
return result;
171198
}
@@ -228,7 +255,7 @@ pub fn load_state() -> anyhow::Result<State> {
228255
Ok(state)
229256
}
230257

231-
pub fn save_state(state: &State) -> anyhow::Result<()> {
258+
pub fn save_state(state: &State) -> Result<()> {
232259
let config_path = get_xdg_path()?;
233260
tracing::info!(?state, ?config_path, "Saving state to config file");
234261
Ok(fs::write(config_path, serde_yaml::to_string(&state)?)?)

0 commit comments

Comments
 (0)