@@ -65,3 +65,44 @@ impl StackExt for gitbutler_stack::Stack {
65
65
Ok ( steps)
66
66
}
67
67
}
68
+
69
+ /// Extension trait for `but_workspace::ui::StackDetails`.
70
+ pub trait StackDetailsExt {
71
+ /// Return the stack as a series of rebase steps in the order the steps should be applied.
72
+ fn as_rebase_steps ( & self , repo : & gix:: Repository ) -> anyhow:: Result < Vec < RebaseStep > > ;
73
+ /// Return the stack as a series of rebase steps in reverse order, i.e. in the order they were generated.
74
+ ///
75
+ /// The generation order starts at the top of the stack (tip first) and goes down to the merge base (parent most commit).
76
+ /// This is useful for operations that need to process the stack in reverse order.
77
+ fn as_rebase_steps_rev ( & self , repo : & gix:: Repository ) -> anyhow:: Result < Vec < RebaseStep > > ;
78
+ }
79
+
80
+ impl StackDetailsExt for crate :: ui:: StackDetails {
81
+ fn as_rebase_steps ( & self , repo : & gix:: Repository ) -> anyhow:: Result < Vec < RebaseStep > > {
82
+ self . as_rebase_steps_rev ( repo) . map ( |mut steps| {
83
+ steps. reverse ( ) ;
84
+ steps
85
+ } )
86
+ }
87
+
88
+ fn as_rebase_steps_rev ( & self , repo : & gix:: Repository ) -> anyhow:: Result < Vec < RebaseStep > > {
89
+ let mut steps: Vec < RebaseStep > = Vec :: new ( ) ;
90
+ for branch in & self . branch_details {
91
+ let reference_step = if let Some ( reference) = repo. try_find_reference ( & branch. name ) ? {
92
+ RebaseStep :: Reference ( but_core:: Reference :: Git ( reference. name ( ) . to_owned ( ) ) )
93
+ } else {
94
+ RebaseStep :: Reference ( but_core:: Reference :: Virtual ( branch. name . to_string ( ) ) )
95
+ } ;
96
+ steps. push ( reference_step) ;
97
+ let commits = & branch. commits ;
98
+ for commit in commits {
99
+ let pick_step = RebaseStep :: Pick {
100
+ commit_id : commit. id ,
101
+ new_message : None ,
102
+ } ;
103
+ steps. push ( pick_step) ;
104
+ }
105
+ }
106
+ Ok ( steps)
107
+ }
108
+ }
0 commit comments