Skip to content

Commit 3b7f372

Browse files
unhappychoiceclaude
andcommitted
fix: respect pause and detached checkouts in watch mode
- Do not advance to the next queued commit while playback is paused, and keep the paused state from falling through to the non-watch waiting branch - Do not replay commits when HEAD returns to an unchanged branch tip after a detached checkout, while still replaying commits rebased while detached Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 784f6e5 commit 3b7f372

2 files changed

Lines changed: 132 additions & 10 deletions

File tree

‎src/ui.rs‎

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,9 @@ impl<'a> UI<'a> {
514514

515515
match self.state {
516516
UIState::Playing if self.engine.is_finished() && self.watcher.is_some() => {
517-
self.advance_to_next_commit();
517+
if self.playback_state != PlaybackState::Paused {
518+
self.advance_to_next_commit();
519+
}
518520
}
519521
UIState::Watching if self.playback_state != PlaybackState::Paused => {
520522
self.play_next_watched_commit();
@@ -1839,6 +1841,84 @@ mod tests {
18391841
assert_eq!(ui.history.last().unwrap().hash, next);
18401842
}
18411843

1844+
#[test]
1845+
fn watch_mode_ignores_return_from_detached_checkout() {
1846+
let test_repo = TestRepo::new();
1847+
let ancestor =
1848+
test_repo.commit_file("src/lib.rs", "fn old() {}\n", "ancestor", 1_700_000_000);
1849+
test_repo.commit_file("src/lib.rs", "fn tip() {}\n", "tip", 1_700_000_060);
1850+
let branch = test_repo.repo.head().unwrap().name().unwrap().to_string();
1851+
let repo = GitRepository::open(&test_repo.path).unwrap();
1852+
let now = Instant::now();
1853+
let mut ui = watch_ui(&repo, now);
1854+
1855+
test_repo
1856+
.repo
1857+
.set_head_detached(git2::Oid::from_str(&ancestor).unwrap())
1858+
.unwrap();
1859+
ui.advance_state_after_tick(now + Duration::from_secs(2));
1860+
test_repo.repo.set_head(&branch).unwrap();
1861+
ui.advance_state_after_tick(now + Duration::from_secs(4));
1862+
1863+
assert_eq!(ui.state, UIState::Watching);
1864+
assert!(ui.history.is_empty());
1865+
}
1866+
1867+
#[test]
1868+
fn watch_mode_replays_commits_rebased_while_detached() {
1869+
let test_repo = TestRepo::new();
1870+
let tip = test_repo.commit_file("src/lib.rs", "fn old() {}\n", "tip", 1_700_000_000);
1871+
let branch = test_repo.repo.head().unwrap().name().unwrap().to_string();
1872+
let repo = GitRepository::open(&test_repo.path).unwrap();
1873+
let now = Instant::now();
1874+
let mut ui = watch_ui(&repo, now);
1875+
1876+
test_repo
1877+
.repo
1878+
.set_head_detached(git2::Oid::from_str(&tip).unwrap())
1879+
.unwrap();
1880+
ui.advance_state_after_tick(now + Duration::from_secs(2));
1881+
let rebased =
1882+
test_repo.commit_file("src/lib.rs", "fn rebased() {}\n", "rebased", 1_700_000_060);
1883+
test_repo
1884+
.repo
1885+
.reference(
1886+
&branch,
1887+
git2::Oid::from_str(&rebased).unwrap(),
1888+
true,
1889+
"rebase",
1890+
)
1891+
.unwrap();
1892+
test_repo.repo.set_head(&branch).unwrap();
1893+
ui.advance_state_after_tick(now + Duration::from_secs(4));
1894+
1895+
assert_eq!(ui.state, UIState::Playing);
1896+
assert_eq!(ui.history.last().unwrap().hash, rebased);
1897+
}
1898+
1899+
#[test]
1900+
fn watch_mode_does_not_advance_to_queued_commit_while_paused() {
1901+
let test_repo = TestRepo::new();
1902+
test_repo.commit_file("src/lib.rs", "fn old() {}\n", "existing", 1_700_000_000);
1903+
let repo = GitRepository::open(&test_repo.path).unwrap();
1904+
let now = Instant::now();
1905+
let mut ui = watch_ui(&repo, now);
1906+
test_repo.commit_file("src/lib.rs", "fn first() {}\n", "first", 1_700_000_060);
1907+
let second =
1908+
test_repo.commit_file("src/lib.rs", "fn second() {}\n", "second", 1_700_000_120);
1909+
ui.advance_state_after_tick(now + Duration::from_secs(2));
1910+
1911+
ui.toggle_pause();
1912+
finish_playback(&mut ui);
1913+
ui.advance_state_after_tick(now + Duration::from_secs(2));
1914+
assert_eq!(ui.state, UIState::Playing);
1915+
assert_eq!(ui.history.len(), 1);
1916+
1917+
ui.toggle_pause();
1918+
ui.advance_state_after_tick(now + Duration::from_secs(2));
1919+
assert_eq!(ui.history.last().unwrap().hash, second);
1920+
}
1921+
18421922
#[test]
18431923
fn watch_mode_does_not_start_new_commit_while_paused() {
18441924
let test_repo = TestRepo::new();

‎src/watch.rs‎

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const MAX_SPEED_MULTIPLIER: f64 = 10.0;
1313
pub 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

Comments
 (0)