Skip to content

A visual git client, simple to use yet powerful. Light, fast and debloated.

Rust

feat: show stashes in the graph

Jonas Kamskerfb707582503a
diff --git a/src/git.rs b/src/git.rs
index dcfa80b..6015f2d 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -862,8 +862,13 @@ pub struct Stash {
/// `stash@{0}`.
pub name: String,
pub hash: String,
+ /// The commit the changes were stashed on (the stash commit's first parent).
+ pub base: String,
pub message: String,
+ pub author: String,
+ pub email: String,
pub time: i64,
+ pub date: String,
}
pub fn stashes(repo: &Path) -> Result<Vec<Stash>, String> {
@@ -872,14 +877,16 @@ pub fn stashes(repo: &Path) -> Result<Vec<Stash>, String> {
&[
"stash",
"list",
- &format!("--format=%gd{FS}%H{FS}%gs{FS}%at"),
+ "--date=format-local:%Y-%m-%d %H:%M",
+ &format!("--format=%H{FS}%P{FS}%gs{FS}%an{FS}%ae{FS}%at{FS}%ad"),
],
)?;
Ok(out
.lines()
- .filter_map(|line| {
+ .enumerate()
+ .filter_map(|(i, line)| {
let f: Vec<&str> = line.split(FS).collect();
- let [name, hash, message, time] = f[..] else {
+ let [hash, parents, message, author, email, time, date] = f[..] else {
return None;
};
// `On main: wip` / `WIP on main: 1234 msg` → the part after the colon reads better.
@@ -889,10 +896,20 @@ pub fn stashes(repo: &Path) -> Result<Vec<Stash>, String> {
.unwrap_or(message)
.to_owned();
Some(Stash {
- name: name.to_owned(),
+ // Not `%gd`: with a date format it names the stash by its date, which two
+ // stashes made in the same minute share.
+ name: format!("stash@{{{i}}}"),
hash: hash.to_owned(),
+ base: parents
+ .split_whitespace()
+ .next()
+ .unwrap_or_default()
+ .to_owned(),
message,
+ author: author.to_owned(),
+ email: email.to_owned(),
time: time.parse().unwrap_or(0),
+ date: date.to_owned(),
})
})
.collect())
@@ -1362,4 +1379,35 @@ second.
assert_eq!(stats[1], ("img.png".to_owned(), None, None));
assert_eq!(stats[2], ("new.rs".to_owned(), Some(3), Some(0)));
}
+
+ /// Two stashes made in the same minute keep names of their own, and each knows its commit.
+ #[test]
+ fn stashes_are_named_by_number() {
+ let dir = std::env::temp_dir().join(format!("gitcraft-stashes-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&dir);
+ let _ = std::fs::create_dir_all(&dir);
+ let git = |args: &[&str]| run(Some(&dir), args);
+ for args in [
+ &["init", "-q"][..],
+ &["config", "user.name", "t"],
+ &["config", "user.email", "t@t"],
+ ] {
+ assert!(git(args).is_ok());
+ }
+ let file = dir.join("f.txt");
+ let _ = std::fs::write(&file, "a\n");
+ assert!(git(&["add", "."]).is_ok());
+ assert!(git(&["commit", "-q", "-m", "base"]).is_ok());
+ let head = git(&["rev-parse", "HEAD"]).unwrap_or_default();
+ for text in ["b\n", "c\n"] {
+ let _ = std::fs::write(&file, text);
+ assert!(git(&["stash", "push", "-q", "-m", text.trim()]).is_ok());
+ }
+ let list = stashes(&dir).unwrap_or_default();
+ let _ = std::fs::remove_dir_all(&dir);
+ let names: Vec<&str> = list.iter().map(|s| s.name.as_str()).collect();
+ assert_eq!(names, ["stash@{0}", "stash@{1}"]);
+ assert_eq!(list[0].message, "c");
+ assert!(list.iter().all(|s| s.base == head.trim()));
+ }
}
diff --git a/src/repo/graph_view.rs b/src/repo/graph_view.rs
index 28d5507..9b58259 100644
--- a/src/repo/graph_view.rs
+++ b/src/repo/graph_view.rs
@@ -213,6 +213,12 @@ impl RepoTab {
});
continue;
}
+ if let Some(stash) = self.stash_at(i).and_then(|s| self.stashes.get(s)) {
+ egui::Popup::context_menu(&resp)
+ .style(crate::theme::menu_style)
+ .show(|ui| super::sidebar::stash_menu_items(ui, acts, &stash.name, idle));
+ continue;
+ }
self.commit_menu(&resp, i, idle, acts);
}
});
@@ -734,6 +740,7 @@ impl RepoTab {
};
let painter = ui.painter_at(rect);
let is_wip = commit.hash == WIP_HASH;
+ let stash = self.stash_at(i).and_then(|s| self.stashes.get(s));
let is_match = self.search.matches.contains(&commit.hash);
// The row's own band, without the gap.
let band = rect.shrink2(vec2(0.0, ROW_GAP / 2.0));
@@ -905,11 +912,36 @@ impl RepoTab {
edge(&gp, node, pos2(x(lane), top), t.lane(lane), width);
}
for &lane in &row.outgoing {
- let color = if is_wip { t.text_faint } else { t.lane(lane) };
+ let color = if is_wip || stash.is_some() {
+ t.text_faint
+ } else {
+ t.lane(lane)
+ };
edge(&gp, node, pos2(x(lane), bottom), color, width);
}
- if is_wip {
+ if stash.is_some() {
+ // A dashed box with the stash icon, as the toolbar's Stash button shows it.
+ let r = Rect::from_center_size(node, vec2(15.0, 15.0));
+ gp.rect_filled(r, CornerRadius::same(3), t.dock);
+ let (l, tp, rt, b) = (r.left(), r.top(), r.right(), r.bottom());
+ gp.add(egui::Shape::dashed_line(
+ &[
+ pos2(l, tp),
+ pos2(rt, tp),
+ pos2(rt, b),
+ pos2(l, b),
+ pos2(l, tp),
+ ],
+ Stroke::new(1.5, t.text_dim),
+ 2.5,
+ 2.0,
+ ));
+ // Images ignore the clip; a squeezed graph column leaves the icon out.
+ if r.right() <= gx + cols.graph {
+ icons::paint(ui, r, "archive", 10.0, t.text_dim);
+ }
+ } else if is_wip {
gp.circle_filled(node, 6.0, t.dock);
gp.add(egui::Shape::dashed_line(
&circle_points(node, 6.0, 16),
diff --git a/src/repo/mod.rs b/src/repo/mod.rs
index 53a18de..78583b5 100644
--- a/src/repo/mod.rs
+++ b/src/repo/mod.rs
@@ -629,6 +629,12 @@ impl RepoTab {
self.commits.get(row).is_some_and(|c| c.hash == WIP_HASH)
}
+ /// The stash a graph row stands for, if it's one.
+ fn stash_at(&self, row: usize) -> Option<usize> {
+ let hash = &self.commits.get(row)?.hash;
+ self.stashes.iter().position(|s| &s.hash == hash)
+ }
+
fn selected_hash(&self) -> Option<String> {
self.selected
.and_then(|r| self.commits.get(r))
@@ -651,7 +657,22 @@ impl RepoTab {
..Commit::default()
});
}
- self.commits.extend(self.history.iter().cloned());
+ // Each stash sits right above the commit it was made on, as a commit on no branch.
+ for commit in &self.history {
+ for stash in self.stashes.iter().filter(|s| s.base == commit.hash) {
+ self.commits.push(Commit {
+ hash: stash.hash.clone(),
+ parents: vec![stash.base.clone()],
+ author: stash.author.clone(),
+ email: stash.email.clone(),
+ time: stash.time,
+ date: stash.date.clone(),
+ summary: stash.message.clone(),
+ ..Commit::default()
+ });
+ }
+ self.commits.push(commit.clone());
+ }
self.graph = graph::layout(&self.commits);
self.index = self
.commits
@@ -682,6 +703,10 @@ impl RepoTab {
self.detail = None;
}
}
+ } else {
+ self.selected = self
+ .stash_view
+ .and_then(|i| self.index.get(&self.stashes.get(i)?.hash).copied());
}
// A merge or cherry-pick leaves a message to commit with.
@@ -729,12 +754,13 @@ impl RepoTab {
let Some(commit) = self.commits.get(row) else {
return;
};
- if self.selected == Some(row) && self.stash_view.is_none() {
+ let stash = self.stash_at(row);
+ if self.selected == Some(row) && self.stash_view == stash {
return;
}
let hash = commit.hash.clone();
self.selected = Some(row);
- self.stash_view = None;
+ self.stash_view = stash;
self.file_view = None;
self.compare = None;
if matches!(
@@ -774,7 +800,8 @@ impl RepoTab {
};
let hash = stash.hash.clone();
self.stash_view = Some(i);
- self.selected = None;
+ self.selected = self.index.get(&hash).copied();
+ self.scroll_to = self.selected;
self.file_view = None;
self.compare = None;
self.diff = None;
@@ -1450,10 +1477,14 @@ impl RepoTab {
self.history = loaded.history;
self.work = loaded.work;
self.worktrees = loaded.worktrees;
+ // Stash numbers shift when one is pushed or dropped; follow the commit.
+ let viewed = self
+ .stash_view
+ .and_then(|i| self.stashes.get(i))
+ .map(|s| s.hash.clone());
self.stashes = loaded.stashes;
- if self.stash_view.is_some_and(|i| i >= self.stashes.len()) {
- self.stash_view = None;
- }
+ self.stash_view =
+ viewed.and_then(|h| self.stashes.iter().position(|s| s.hash == h));
self.identity = loaded.identity;
let forge = forge::detect(&loaded.remotes);
self.remotes = loaded.remotes;