@@ -13,6 +13,8 @@ const MAX_SPEED_MULTIPLIER: f64 = 10.0;
1313pub struct CommitWatcher {
1414 last_head : Option < String > ,
1515 last_branch : Option < String > ,
16+ /// Branch and its tip recorded when HEAD became detached.
17+ detached_from : Option < ( String , Option < String > ) > ,
1618 pending : VecDeque < String > ,
1719 next_poll : Instant ,
1820}
@@ -22,6 +24,7 @@ impl CommitWatcher {
2224 Self {
2325 last_head : repo. head_commit_id ( ) ,
2426 last_branch : repo. head_branch_name ( ) ,
27+ detached_from : None ,
2528 pending : VecDeque :: new ( ) ,
2629 next_poll : now + POLL_INTERVAL ,
2730 }
@@ -46,10 +49,10 @@ impl CommitWatcher {
4649 }
4750
4851 fn check_head ( & mut self , repo : & GitRepository ) {
49- let branch_switched = self . update_branch ( repo. head_branch_name ( ) ) ;
5052 let Some ( head) = repo. head_commit_id ( ) else {
5153 return ;
5254 } ;
55+ let branch_switched = self . update_branch ( repo. head_branch_name ( ) , & head) ;
5356 if branch_switched || self . last_head . as_deref ( ) == Some ( head. as_str ( ) ) {
5457 self . last_head = Some ( head) ;
5558 return ;
@@ -61,18 +64,33 @@ impl CommitWatcher {
6164 self . last_head = Some ( head) ;
6265 }
6366
64- /// Records the checked-out branch and reports whether it changed.
65- /// Detached HEAD (e.g. mid-rebase) is ignored so rebased commits are still replayed.
66- fn update_branch ( & mut self , branch : Option < String > ) -> bool {
67+ /// Records the checked-out branch and reports whether HEAD moved without new commits:
68+ /// a switch to another branch, or a return to an unchanged branch after a detached checkout.
69+ /// Detached HEAD itself (e.g. mid-rebase) is ignored so rebased commits are still replayed.
70+ fn update_branch ( & mut self , branch : Option < String > , head : & str ) -> bool {
6771 let Some ( branch) = branch else {
72+ self . remember_detached_tip ( ) ;
6873 return false ;
6974 } ;
75+ let returned_unchanged = self
76+ . detached_from
77+ . take ( )
78+ . is_some_and ( |( detached, tip) | detached == branch && tip. as_deref ( ) == Some ( head) ) ;
7079 let switched = self
7180 . last_branch
7281 . as_ref ( )
7382 . is_some_and ( |last| * last != branch) ;
7483 self . last_branch = Some ( branch) ;
75- switched
84+ switched || returned_unchanged
85+ }
86+
87+ fn remember_detached_tip ( & mut self ) {
88+ if self . detached_from . is_none ( ) {
89+ self . detached_from = self
90+ . last_branch
91+ . clone ( )
92+ . map ( |branch| ( branch, self . last_head . clone ( ) ) ) ;
93+ }
7694 }
7795
7896 fn enqueue ( & mut self , commits : Vec < String > ) {
@@ -96,6 +114,7 @@ mod tests {
96114 CommitWatcher {
97115 last_head : None ,
98116 last_branch : None ,
117+ detached_from : None ,
99118 pending : pending. iter ( ) . map ( |hash| hash. to_string ( ) ) . collect ( ) ,
100119 next_poll : Instant :: now ( ) ,
101120 }
@@ -129,10 +148,33 @@ mod tests {
129148 fn update_branch_reports_switch_only_between_named_branches ( ) {
130149 let mut watcher = watcher_with_pending ( & [ ] ) ;
131150
132- assert ! ( !watcher. update_branch( Some ( "main" . to_string( ) ) ) ) ;
133- assert ! ( !watcher. update_branch( None ) ) ;
134- assert ! ( !watcher. update_branch( Some ( "main" . to_string( ) ) ) ) ;
135- assert ! ( watcher. update_branch( Some ( "feature" . to_string( ) ) ) ) ;
151+ assert ! ( !watcher. update_branch( Some ( "main" . to_string( ) ) , "a" ) ) ;
152+ assert ! ( !watcher. update_branch( None , "b" ) ) ;
153+ assert ! ( !watcher. update_branch( Some ( "main" . to_string( ) ) , "c" ) ) ;
154+ assert ! ( watcher. update_branch( Some ( "feature" . to_string( ) ) , "c" ) ) ;
155+ }
156+
157+ #[ test]
158+ fn update_branch_reports_return_to_unchanged_tip_after_detaching ( ) {
159+ let mut watcher = watcher_with_pending ( & [ ] ) ;
160+ watcher. last_branch = Some ( "main" . to_string ( ) ) ;
161+ watcher. last_head = Some ( "tip" . to_string ( ) ) ;
162+
163+ assert ! ( !watcher. update_branch( None , "ancestor" ) ) ;
164+ watcher. last_head = Some ( "ancestor" . to_string ( ) ) ;
165+
166+ assert ! ( watcher. update_branch( Some ( "main" . to_string( ) ) , "tip" ) ) ;
167+ }
168+
169+ #[ test]
170+ fn update_branch_keeps_replaying_when_branch_tip_changed_while_detached ( ) {
171+ let mut watcher = watcher_with_pending ( & [ ] ) ;
172+ watcher. last_branch = Some ( "main" . to_string ( ) ) ;
173+ watcher. last_head = Some ( "tip" . to_string ( ) ) ;
174+
175+ assert ! ( !watcher. update_branch( None , "onto" ) ) ;
176+
177+ assert ! ( !watcher. update_branch( Some ( "main" . to_string( ) ) , "rebased" ) ) ;
136178 }
137179
138180 #[ test]
0 commit comments