Skip to content

Commit 8efa358

Browse files
committed
more bug fixes
1 parent 361f3a2 commit 8efa358

15 files changed

Lines changed: 857 additions & 150 deletions

File tree

‎Cargo.lock‎

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "gitst"
3-
version = "0.1.3"
3+
version = "0.1.4"
44
edition = "2024"
55
description = "Live, glanceable git status TUI for small terminal panes"
66
license = "MIT"
@@ -23,6 +23,7 @@ unicode-width = "0.2"
2323

2424
[target.'cfg(unix)'.dependencies]
2525
libc = "0.2"
26+
signal-hook = "0.3"
2627

2728
[target.'cfg(windows)'.dependencies]
2829
windows-sys = { version = "0.61", features = [

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ than 10 seconds, failed fetches and config errors.
7777
| `f` | fetch now | fetch now |
7878
| `?` | help | help |
7979
| `q` / `Ctrl-C` | quit | quit |
80+
| `Ctrl-Z` | suspend (`fg` resumes; not on Windows) | suspend |
8081

8182
### Options
8283

‎src/activity.rs‎

Lines changed: 156 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@ pub struct ActivityEvent {
3131
pub text: String,
3232
/// Commit the event refers to, when clicking it can open one.
3333
pub rev: Option<String>,
34+
/// The one changed file the event is about, which clicking can open.
35+
pub path: Option<String>,
3436
}
3537

38+
/// What `git stash` writes to HEAD's reflog, as a user's `git reset --hard
39+
/// HEAD` also does.
40+
const STASH_RESET: &str = "reset: moving to HEAD";
41+
3642
/// Turns a reflog entry into a timeline event, or `None` for noise such as
3743
/// individual rebase picks.
3844
pub fn classify(e: &ReflogEntry) -> Option<ActivityEvent> {
@@ -42,6 +48,7 @@ pub fn classify(e: &ReflogEntry) -> Option<ActivityEvent> {
4248
kind,
4349
text,
4450
rev: Some(e.short.clone()),
51+
path: None,
4552
})
4653
};
4754
if let Some(remote) = e.refname.strip_prefix("refs/remotes/") {
@@ -64,6 +71,8 @@ pub fn classify(e: &ReflogEntry) -> Option<ActivityEvent> {
6471
event(ActivityKind::Rebase, "rebase started".into())
6572
} else if action.contains("(finish)") {
6673
event(ActivityKind::Rebase, "rebase finished".into())
74+
} else if action.contains("(abort)") {
75+
event(ActivityKind::Rebase, "rebase aborted".into())
6776
} else {
6877
None
6978
};
@@ -100,22 +109,43 @@ fn plural(n: usize, word: &str) -> String {
100109
}
101110
}
102111

103-
/// `path +a −r` for one changed path (counts from `snap`), or `N files changed`.
104-
pub fn describe_files(paths: &[String], snap: &Snapshot) -> String {
105-
let [path] = paths else {
106-
return format!("{} changed", plural(paths.len(), "file"));
107-
};
108-
let mut text = path.clone();
109-
if let Some(c) = snap.changes.iter().find(|c| &c.path == path) {
110-
let (a, r) = c.counts.known().unwrap_or((0, 0));
111-
if a > 0 {
112-
text.push_str(&format!(" +{a}"));
113-
}
114-
if r > 0 {
115-
text.push_str(&format!(" −{r}"));
112+
/// A file event for `paths` as they stand in `snap`: `path +a −r` for one
113+
/// changed path, `path discarded` for one no longer changed, or
114+
/// `N files changed` / `N files discarded`.
115+
pub fn files_event(paths: &[String], snap: &Snapshot, time: i64) -> ActivityEvent {
116+
let find = |p: &String| snap.changes.iter().find(|c| &c.path == p);
117+
let (text, path) = match paths {
118+
[p] => match find(p) {
119+
Some(c) => {
120+
let mut text = p.clone();
121+
let (a, r) = c.counts.known().unwrap_or((0, 0));
122+
if a > 0 {
123+
text.push_str(&format!(" +{a}"));
124+
}
125+
if r > 0 {
126+
text.push_str(&format!(" −{r}"));
127+
}
128+
(text, Some(p.clone()))
129+
}
130+
None => (format!("{p} discarded"), None),
131+
},
132+
_ if paths.iter().all(|p| find(p).is_none()) => {
133+
(format!("{} discarded", plural(paths.len(), "file")), None)
116134
}
135+
_ => (format!("{} changed", plural(paths.len(), "file")), None),
136+
};
137+
ActivityEvent {
138+
time,
139+
kind: ActivityKind::Files,
140+
text,
141+
rev: None,
142+
path,
117143
}
118-
text
144+
}
145+
146+
/// HEAD's newest reflog entry.
147+
fn newest_head(snap: &Snapshot) -> Option<&ReflogEntry> {
148+
snap.reflog.iter().find(|r| r.refname == "HEAD")
119149
}
120150

121151
/// Events describing what changed between two snapshots, plus the paths
@@ -134,14 +164,39 @@ pub fn diff_snapshots(
134164
.filter(|(c, old)| old.is_none_or(|o| o != c))
135165
.collect();
136166

167+
// Paths that left the list, unless a commit, reset, checkout or stash
168+
// explains it, or the list is capped and a path may only have moved
169+
// past the cap.
170+
let explained = newest_head(prev) != newest_head(next)
171+
|| prev.oid != next.oid
172+
|| prev.stash_count != next.stash_count;
173+
let capped = prev.changes_omitted > 0 || next.changes_omitted > 0;
174+
let left: Vec<&Change> = if explained || capped {
175+
Vec::new()
176+
} else {
177+
let now_paths: HashSet<&str> = next.changes.iter().map(|c| c.path.as_str()).collect();
178+
prev.changes
179+
.iter()
180+
.filter(|c| !now_paths.contains(c.path.as_str()))
181+
.collect()
182+
};
183+
137184
let mut events = Vec::new();
138185
let event = |kind, text: String| ActivityEvent {
139186
time: now,
140187
kind,
141188
text,
142189
rev: None,
190+
path: None,
143191
};
144-
if !changed.is_empty() {
192+
let paths: Vec<String> = changed
193+
.iter()
194+
.map(|(c, _)| c.path.clone())
195+
.chain(left.iter().map(|c| c.path.clone()))
196+
.collect();
197+
if !left.is_empty() {
198+
events.push(files_event(&paths, next, now));
199+
} else if !changed.is_empty() {
145200
let moved_to_index = |to_index: bool| {
146201
changed.iter().all(|(c, old)| {
147202
old.is_some_and(|o| {
@@ -160,26 +215,40 @@ pub fn diff_snapshots(
160215
format!("unstaged {}", plural(changed.len(), "file")),
161216
));
162217
} else {
163-
let paths: Vec<String> = changed.iter().map(|(c, _)| c.path.clone()).collect();
164-
events.push(event(ActivityKind::Files, describe_files(&paths, next)));
218+
events.push(files_event(&paths, next, now));
165219
}
166220
}
167221
if next.stash_count > prev.stash_count {
168-
events.push(event(ActivityKind::Stash, "stash saved".into()));
222+
// The stash takes the time of the reset `git stash` wrote, which
223+
// `merged` then leaves out.
224+
let reset = next
225+
.reflog
226+
.iter()
227+
.filter(|r| r.refname == "HEAD" && !prev.reflog.contains(r))
228+
.find(|r| r.message == STASH_RESET);
229+
events.push(ActivityEvent {
230+
time: reset.map_or(now, |r| r.time),
231+
..event(ActivityKind::Stash, "stash saved".into())
232+
});
169233
} else if next.stash_count < prev.stash_count {
170234
events.push(event(
171235
ActivityKind::Stash,
172236
"stash applied or dropped".into(),
173237
));
174238
}
175-
let paths = changed.into_iter().map(|(c, _)| c.path.clone()).collect();
176239
(events, paths)
177240
}
178241

179242
/// The timeline shown in the Activity section, newest first.
180243
pub fn merged(reflog: &[ReflogEntry], live: &[ActivityEvent], limit: usize) -> Vec<ActivityEvent> {
244+
let stashes: HashSet<i64> = live
245+
.iter()
246+
.filter(|e| e.kind == ActivityKind::Stash)
247+
.map(|e| e.time)
248+
.collect();
181249
let mut all: Vec<ActivityEvent> = reflog
182250
.iter()
251+
.filter(|r| !(r.message == STASH_RESET && stashes.contains(&r.time)))
183252
.filter_map(classify)
184253
.chain(live.iter().cloned())
185254
.collect();
@@ -323,7 +392,8 @@ mod tests {
323392
time: 5,
324393
kind: ActivityKind::Files,
325394
text: "2 files changed".into(),
326-
rev: None
395+
rev: None,
396+
path: None,
327397
}]
328398
);
329399
assert_eq!(paths, vec!["a", "b"]);
@@ -378,6 +448,70 @@ mod tests {
378448
);
379449
}
380450

451+
fn at(time: i64, m: &str) -> ReflogEntry {
452+
ReflogEntry {
453+
time,
454+
..e("HEAD", m)
455+
}
456+
}
457+
458+
#[test]
459+
fn stash_is_not_also_shown_as_a_reset() {
460+
let prev = Snapshot::default();
461+
let next = Snapshot {
462+
stash_count: 1,
463+
reflog: vec![at(9, "reset: moving to HEAD")],
464+
..Snapshot::default()
465+
};
466+
let (events, _) = diff_snapshots(&prev, &next, 12);
467+
let texts: Vec<_> = merged(&next.reflog, &events, 10)
468+
.into_iter()
469+
.map(|e| (e.time, e.text))
470+
.collect();
471+
assert_eq!(texts, vec![(9, "stash saved".to_string())]);
472+
// A reset to HEAD of its own still shows.
473+
let texts: Vec<_> = merged(&next.reflog, &[], 10)
474+
.into_iter()
475+
.map(|e| e.text)
476+
.collect();
477+
assert_eq!(texts, vec!["reset to HEAD"]);
478+
}
479+
480+
#[test]
481+
fn aborted_rebase_is_shown() {
482+
let ev = classify(&e("HEAD", "rebase (abort): returning to refs/heads/side")).unwrap();
483+
assert_eq!(
484+
(ev.kind, ev.text.as_str()),
485+
(ActivityKind::Rebase, "rebase aborted")
486+
);
487+
}
488+
489+
#[test]
490+
fn files_leaving_the_list_are_reported() {
491+
let a = snap_with(vec![ch("a", ' ', 'M', 1, 0), ch("b", '?', '?', 2, 0)]);
492+
let b = snap_with(vec![ch("b", '?', '?', 2, 0)]);
493+
let (ev, paths) = diff_snapshots(&a, &b, 5);
494+
assert_eq!(ev[0].text, "a discarded");
495+
assert_eq!(paths, vec!["a"]);
496+
let (ev, _) = diff_snapshots(&a, &snap_with(vec![]), 5);
497+
assert_eq!(ev[0].text, "2 files discarded");
498+
// A new HEAD reflog entry (commit, reset, checkout) explains it.
499+
let reset = Snapshot {
500+
reflog: vec![at(4, "reset: moving to HEAD")],
501+
..snap_with(vec![])
502+
};
503+
assert!(diff_snapshots(&a, &reset, 5).0.is_empty());
504+
}
505+
506+
#[test]
507+
fn single_path_events_carry_the_path() {
508+
let a = snap_with(vec![]);
509+
let b = snap_with(vec![ch("a", ' ', 'M', 1, 0)]);
510+
assert_eq!(diff_snapshots(&a, &b, 5).0[0].path.as_deref(), Some("a"));
511+
let c = snap_with(vec![ch("a", ' ', 'M', 1, 0), ch("b", ' ', 'M', 1, 0)]);
512+
assert_eq!(diff_snapshots(&a, &c, 5).0[0].path, None);
513+
}
514+
381515
#[test]
382516
fn merged_sorted_deduped_limited() {
383517
let live = vec![
@@ -386,12 +520,14 @@ mod tests {
386520
kind: ActivityKind::Files,
387521
text: "x".into(),
388522
rev: None,
523+
path: None,
389524
},
390525
ActivityEvent {
391526
time: 20,
392527
kind: ActivityKind::Files,
393528
text: "x".into(),
394529
rev: None,
530+
path: None,
395531
},
396532
];
397533
let m = merged(

0 commit comments

Comments
 (0)