Skip to content

Commit 9cfdddf

Browse files
unhappychoiceclaude
andcommitted
feat: add watch mode to replay new commits as they are made
Add a --watch (-w) flag that polls HEAD every second and replays newly arrived commits back-to-back, turning gitlogue into a live commit visualizer. - Show the startup HEAD commit in its final state while idle - Queue commits reachable from the new HEAD but not the previous one - Skip merge commits to avoid replaying merged changes twice - Ignore commits brought in by switching branches - Keep at most 100 queued commits, dropping the oldest on overflow - Speed up typing linearly from 1x to 10x as the queue fills Closes #215 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 574cd23 commit 9cfdddf

7 files changed

Lines changed: 567 additions & 6 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ gitlogue --commit abc123 --loop
116116
# Loop through a commit range
117117
gitlogue --commit HEAD~10..HEAD --loop
118118

119+
# Watch the repository and replay new commits as they are made
120+
gitlogue --watch
121+
119122
# View staged changes (default)
120123
gitlogue diff
121124

‎docs/usage.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,25 @@ This is especially useful when viewing a specific commit or commit range and you
217217
- Desktop ricing and ambience
218218
- Educational replays of feature development
219219

220+
### `--watch` / `-w`
221+
222+
Watch the repository and replay new commits as they are made — a live commit visualizer.
223+
224+
```bash
225+
gitlogue --watch # Wait for new commits and replay them
226+
gitlogue --watch --speed 10 # Faster typing for live demos
227+
```
228+
229+
**Behavior:**
230+
- HEAD is polled every second; any commits reachable from the new HEAD but not from the previous one are queued (covers `git commit`, `git pull`, `git rebase`, etc.)
231+
- Merge commits are skipped (their changes are replayed via the merged commits)
232+
- Switching branches (`git checkout` / `git switch`) does not replay the commits of the new branch; only commits made afterwards are replayed
233+
- On startup, the current HEAD commit is shown in its final state (without animation) while waiting for new commits
234+
- Queued commits are replayed back-to-back in chronological order
235+
- Typing speeds up automatically while commits are queued, linearly from 1x (empty queue) to 10x (100 queued commits)
236+
- At most 100 commits are queued; when more arrive, the oldest queued commits are dropped
237+
- Cannot be combined with `--commit`, `--order`, `--loop`, `--author`, `--before`, or `--after`
238+
220239
### `--help`
221240

222241
Display help information:

‎src/animation.rs‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ pub struct AnimationEngine {
304304
pending_metadata: Option<CommitMetadata>,
305305
/// Speed rules for different file patterns
306306
speed_rules: Vec<SpeedRule>,
307+
speed_multiplier: f64,
307308
paused: bool,
308309
line_checkpoints: VecDeque<ManualCheckpoint>,
309310
change_checkpoints: VecDeque<ManualCheckpoint>,
@@ -343,6 +344,7 @@ impl AnimationEngine {
343344
current_metadata: None,
344345
pending_metadata: None,
345346
speed_rules: Vec::new(),
347+
speed_multiplier: 1.0,
346348
paused: false,
347349
line_checkpoints: VecDeque::new(),
348350
change_checkpoints: VecDeque::new(),
@@ -541,15 +543,20 @@ impl AnimationEngine {
541543
self.speed_rules = rules;
542544
}
543545

546+
/// Speed up typing by `multiplier` (applied from the next file onward)
547+
pub fn set_speed_multiplier(&mut self, multiplier: f64) {
548+
self.speed_multiplier = multiplier.max(1.0);
549+
}
550+
544551
/// Get the speed for a given file path based on speed rules
545552
/// Returns the first matching rule's speed, or the base speed if no match
546553
fn get_speed_for_file(&self, path: &str) -> u64 {
547-
for rule in &self.speed_rules {
548-
if rule.matches(path) {
549-
return rule.speed_ms;
550-
}
551-
}
552-
self.base_speed_ms
554+
let speed = self
555+
.speed_rules
556+
.iter()
557+
.find(|rule| rule.matches(path))
558+
.map_or(self.base_speed_ms, |rule| rule.speed_ms);
559+
(speed as f64 / self.speed_multiplier) as u64
553560
}
554561

555562
/// Sets the viewport height for scroll calculations.

‎src/git.rs‎

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,38 @@ impl GitRepository {
400400
Self::extract_metadata_with_changes(&self.repo, &commit)
401401
}
402402

403+
pub fn head_commit_id(&self) -> Option<String> {
404+
self.repo
405+
.head()
406+
.and_then(|head| head.peel_to_commit())
407+
.map(|commit| commit.id().to_string())
408+
.ok()
409+
}
410+
411+
/// Returns the checked-out branch name, or `None` when HEAD is detached (e.g. mid-rebase).
412+
pub fn head_branch_name(&self) -> Option<String> {
413+
self.repo
414+
.head()
415+
.ok()
416+
.filter(|head| head.is_branch())
417+
.and_then(|head| head.shorthand().map(String::from).ok())
418+
}
419+
420+
/// Returns non-merge commits reachable from `to` but not from `from`, oldest first.
421+
pub fn commit_ids_between(&self, from: Option<&str>, to: &str) -> Result<Vec<String>> {
422+
let mut revwalk = self.repo.revwalk()?;
423+
revwalk.set_sorting(git2::Sort::TOPOLOGICAL | git2::Sort::REVERSE)?;
424+
revwalk.push(Oid::from_str(to)?)?;
425+
if let Some(oid) = from.and_then(|hash| Oid::from_str(hash).ok()) {
426+
revwalk.hide(oid).ok();
427+
}
428+
revwalk
429+
.map(|oid| oid.and_then(|oid| self.repo.find_commit(oid)))
430+
.filter(|commit| !matches!(commit, Ok(commit) if commit.parent_count() > 1))
431+
.map(|commit| Ok(commit?.id().to_string()))
432+
.collect()
433+
}
434+
403435
pub fn reset_index(&self) {
404436
*self.commit_index.borrow_mut() = 0;
405437
}
@@ -1523,6 +1555,99 @@ mod tests {
15231555
assert!(error.contains("No commits found matching the filters in repository"));
15241556
}
15251557

1558+
#[test]
1559+
fn test_commit_ids_between_returns_new_commits_oldest_first() {
1560+
let test_repo = TestRepo::new();
1561+
let base = test_repo.commit_file(
1562+
"history.txt",
1563+
"one\n",
1564+
"Alice Example",
1565+
1566+
1_700_000_000,
1567+
"base",
1568+
);
1569+
let first = test_repo.commit_file(
1570+
"history.txt",
1571+
"two\n",
1572+
"Alice Example",
1573+
1574+
1_700_000_060,
1575+
"first",
1576+
);
1577+
let second = test_repo.commit_file(
1578+
"history.txt",
1579+
"three\n",
1580+
"Alice Example",
1581+
1582+
1_700_000_120,
1583+
"second",
1584+
);
1585+
let repo = GitRepository::open(&test_repo.path).unwrap();
1586+
1587+
assert_eq!(repo.head_commit_id().as_deref(), Some(second.as_str()));
1588+
assert_eq!(
1589+
repo.commit_ids_between(Some(&base), &second).unwrap(),
1590+
vec![first.clone(), second.clone()]
1591+
);
1592+
assert_eq!(
1593+
repo.commit_ids_between(None, &second).unwrap(),
1594+
vec![base, first, second]
1595+
);
1596+
}
1597+
1598+
#[test]
1599+
fn test_commit_ids_between_skips_merge_commits() {
1600+
let test_repo = TestRepo::new();
1601+
let base = test_repo.commit_file(
1602+
"history.txt",
1603+
"one\n",
1604+
"Alice Example",
1605+
1606+
1_700_000_000,
1607+
"base",
1608+
);
1609+
let feature = test_repo.commit_file(
1610+
"feature.txt",
1611+
"feature\n",
1612+
"Alice Example",
1613+
1614+
1_700_000_060,
1615+
"feature",
1616+
);
1617+
let base_commit = test_repo
1618+
.repo
1619+
.find_commit(Oid::from_str(&base).unwrap())
1620+
.unwrap();
1621+
let feature_commit = test_repo
1622+
.repo
1623+
.find_commit(Oid::from_str(&feature).unwrap())
1624+
.unwrap();
1625+
let signature = git2::Signature::new(
1626+
"Alice Example",
1627+
1628+
&git2::Time::new(1_700_000_120, 0),
1629+
)
1630+
.unwrap();
1631+
let merge = test_repo
1632+
.repo
1633+
.commit(
1634+
Some("HEAD"),
1635+
&signature,
1636+
&signature,
1637+
"merge",
1638+
&feature_commit.tree().unwrap(),
1639+
&[&feature_commit, &base_commit],
1640+
)
1641+
.unwrap()
1642+
.to_string();
1643+
let repo = GitRepository::open(&test_repo.path).unwrap();
1644+
1645+
assert_eq!(
1646+
repo.commit_ids_between(Some(&base), &merge).unwrap(),
1647+
vec![feature]
1648+
);
1649+
}
1650+
15261651
#[test]
15271652
fn test_commit_range_lookup_and_validation() {
15281653
let test_repo = TestRepo::new();

‎src/main.rs‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod panes;
55
mod syntax;
66
mod theme;
77
mod ui;
8+
mod watch;
89
mod widgets;
910

1011
use animation::SpeedRule;
@@ -15,6 +16,7 @@ use git::{DiffMode, GitRepository};
1516
use std::path::{Path, PathBuf};
1617
use theme::Theme;
1718
use ui::UI;
19+
use watch::CommitWatcher;
1820

1921
/// Defines the order in which commits are played back during animation.
2022
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, ValueEnum)]
@@ -91,6 +93,14 @@ pub struct Args {
9193
)]
9294
pub loop_playback: Option<bool>,
9395

96+
#[arg(
97+
short = 'w',
98+
long,
99+
conflicts_with_all = ["commit", "order", "loop_playback", "author", "before", "after"],
100+
help = "Watch the repository and replay new commits as they are made"
101+
)]
102+
pub watch: bool,
103+
94104
#[arg(long, help = "Display third-party license information")]
95105
pub license: bool,
96106

@@ -487,6 +497,41 @@ fn prepare_diff_playback(
487497
}))
488498
}
489499

500+
fn run_watch_mode(repo: &GitRepository, args: &Args, config: &Config) -> Result<()> {
501+
let patterns = collect_ignore_patterns(
502+
&config.ignore_patterns,
503+
args.ignore_file.as_deref(),
504+
&args.ignore,
505+
)?;
506+
git::init_ignore_patterns(&patterns).ok();
507+
let RuntimeOptions {
508+
speed,
509+
theme,
510+
speed_rules,
511+
..
512+
} = resolve_runtime_options(
513+
args.speed,
514+
args.theme.as_deref(),
515+
args.background,
516+
None,
517+
&args.speed_rule,
518+
config,
519+
false,
520+
)?;
521+
let mut ui = UI::new(
522+
speed,
523+
Some(repo),
524+
theme,
525+
PlaybackOrder::Asc,
526+
false,
527+
None,
528+
false,
529+
speed_rules,
530+
);
531+
ui.enable_watch(CommitWatcher::new(repo, std::time::Instant::now()));
532+
ui.run()
533+
}
534+
490535
fn format_theme_list() -> String {
491536
std::iter::once("Available themes:".to_string())
492537
.chain(
@@ -595,6 +640,9 @@ fn main() -> Result<()> {
595640
let repo_path = args.validate()?;
596641
let mut repo = GitRepository::open(&repo_path)?;
597642
let config = Config::load()?;
643+
if args.watch {
644+
return run_watch_mode(&repo, &args, &config);
645+
}
598646
let CommitPlaybackPlan {
599647
metadata,
600648
runtime,
@@ -784,6 +832,7 @@ mod tests {
784832
background: None,
785833
order: None,
786834
loop_playback: None,
835+
watch: false,
787836
license: false,
788837
author: None,
789838
before: None,
@@ -1201,4 +1250,16 @@ mod tests {
12011250

12021251
assert!(error.contains("Author pattern cannot be empty"));
12031252
}
1253+
1254+
#[test]
1255+
fn watch_flag_parses_and_rejects_commit_selection_options() {
1256+
let args = Args::try_parse_from(["gitlogue", "--watch", "--speed", "5"]).unwrap();
1257+
assert!(args.watch);
1258+
assert_eq!(args.speed, Some(5));
1259+
1260+
let error = Args::try_parse_from(["gitlogue", "--watch", "--commit", "HEAD"])
1261+
.unwrap_err()
1262+
.to_string();
1263+
assert!(error.contains("cannot be used with"));
1264+
}
12041265
}

0 commit comments

Comments
 (0)