@@ -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
143157fn 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