@@ -1,13 +1,17 @@
1-//! A code project's branches, as the widget library's trial does it: a ticket starts from main
2-//! plus every branch in review, and landing it merges into main what it was built on too.
3-use crate::{agent::git, library::merges};
1+//! A code project's tickets, as the widget library's trial does it: a ticket starts from the
2+//! user's checked-out branch plus every ticket in review, and landing it merges into the branch
3+//! checked out then what it was built on too. Each ticket's work is `store::branch`, never a
4+//! branch of the user's, so their branches and pushes stay their own.
5+use crate::{agent::git, library::merges, store::branch};
46 use std::path::Path;
57
6-/// What a code ticket starts from: main with `branches` (those in review, oldest first) merged
7-/// in, as one commit no branch points to. A branch that conflicts with those before it is left out.
8-pub fn stack(repo: &Path, branches: &[String]) -> Result<String, String> {
9- let mut head = git(repo, "", &["rev-parse", "main"])?.trim().to_owned();
10- for b in branches {
8+/// What a code ticket starts from: the checked-out commit with `tickets` (those in review,
9+/// oldest first, as id and title) merged in, as one commit no branch points to. Each merge is
10+/// titled as its ticket. A ticket that conflicts with those before it is left out.
11+pub fn stack(repo: &Path, tickets: &[(u32, &str)]) -> Result<String, String> {
12+ let mut head = git(repo, "", &["rev-parse", "HEAD"])?.trim().to_owned();
13+ for (id, title) in tickets {
14+ let b = &branch(*id);
1115 if git(repo, "", &["merge-base", "--is-ancestor", b, &head]).is_ok() { continue; }
1216 if git(repo, "", &["merge-base", "--is-ancestor", &head, b]).is_ok() {
1317 head = git(repo, "", &["rev-parse", b])?.trim().to_owned();
@@ -15,28 +19,28 @@ pub fn stack(repo: &Path, branches: &[String]) -> Result<String, String> {
1519 }
1620 let Ok(tree) = git(repo, "", &["merge-tree", "--write-tree", "--no-messages", &head, b]) else { continue };
1721 head = git(repo, "", &["-c", "user.name=conveyor", "-c", "user.email=conveyor@localhost", "commit-tree", tree.trim(),
18- "-p", &head, "-p", b, "-m", &format!("Merge branch '{b}'")])?.trim().to_owned();
22+ "-p", &head, "-p", b, "-m", title])?.trim().to_owned();
1923 }
2024 Ok(head)
2125 }
2226
23-/// Merges `branch` into `repo`'s main and deletes it. Leaves everything as it was, and says
24-/// why, when it conflicts with main or git won't touch the user's checkout.
25-pub fn land(repo: &Path, branch: &str) -> Result<(), String> {
26- merges(repo, "main", branch).map_err(|files| format!(
27- "{branch} conflicts with main in {files}. Move it to ready to have the agent merge main, or merge it yourself."))?;
28- let head = git(repo, "", &["symbolic-ref", "-q", "--short", "HEAD"]).unwrap_or_default();
29- if head.trim() == "main" {
30- // The user's checkout: git refuses to overwrite their uncommitted changes, and says so.
31- // It can't conflict (checked above), so there's never a merge of ours to abort.
32- git(repo, "", &["merge", "-q", "--no-edit", branch])
33- } else {
34- // main isn't checked out, so there's no worktree to merge in: fast-forward only.
35- git(repo, "", &["fetch", "-q", ".", &format!("{branch}:main")])
36- .map_err(|_| format!("{branch} doesn't fast-forward main, and main isn't checked out. Check out main, then move it to done again."))
37- }?;
38- // In main now, which may not be HEAD, so -d would refuse.
39- git(repo, "", &["branch", "-D", branch]).map(|_| ())
27+/// Merges ticket `id`'s work into the branch checked out in `repo`, a merge commit titled
28+/// `title` if it takes one, and deletes its ref. Says which branch it went into. Leaves
29+/// everything as it was, and says why, when no branch is checked out, it conflicts with it, or
30+/// git won't touch the user's checkout.
31+pub fn land(repo: &Path, id: u32, title: &str) -> Result<String, String> {
32+ let onto = git(repo, "", &["symbolic-ref", "-q", "--short", "HEAD"])
33+ .map_err(|_| "no branch is checked out to merge it into. Check one out, then move it to done again.".to_string())?;
34+ let (onto, b) = (onto.trim().to_owned(), branch(id));
35+ merges(repo, "HEAD", &b).map_err(|files| format!(
36+ "it conflicts with {onto} in {files}. Move it to ready to have the agent merge {onto}, or merge it yourself."))?;
37+ // git refuses to overwrite the user's uncommitted changes, and says so. It can't conflict
38+ // (checked above), and an empty title would leave it half done, so there's never a merge
39+ // of ours to abort.
40+ let title = if title.trim().is_empty() { format!("Ticket {id}") } else { title.into() };
41+ git(repo, "", &["merge", "-q", "-m", &title, &b])?;
42+ git(repo, "", &["update-ref", "-d", &b])?;
43+ Ok(onto)
4044 }
4145
4246 #[cfg(test)]
@@ -53,6 +57,20 @@ mod tests {
5357 run(repo, &["commit", "-qm", file]);
5458 }
5559
60+ /// Ticket `id`'s work, as its agent leaves it: `text` in `file`, on top of `from`. The
61+ /// checkout stays on its branch.
62+ fn work(repo: &Path, id: u32, from: &str, file: &str, text: &str) {
63+ let head = run(repo, &["symbolic-ref", "--short", "HEAD"]);
64+ run(repo, &["switch", "-q", "--detach", from]);
65+ commit(repo, file, text);
66+ run(repo, &["update-ref", &branch(id), "HEAD"]);
67+ run(repo, &["switch", "-q", head.trim()]);
68+ }
69+
70+ fn subject(repo: &Path, rev: &str) -> String {
71+ run(repo, &["log", "-1", "--format=%s", rev]).trim().to_owned()
72+ }
73+
5674 #[test]
5775 fn land_edges() {
5876 let repo = std::env::temp_dir().join(format!("conveyor-land-{}", std::process::id()));
@@ -63,74 +81,71 @@ mod tests {
6381 run(&repo, &["config", "user.email", "t@t"]);
6482 commit(&repo, "a", "1\n");
6583
66- // A fast-forward lands in the checkout and drops the branch; untracked files stay.
67- run(&repo, &["switch", "-qc", "t-1"]);
68- commit(&repo, "b", "b\n");
69- run(&repo, &["switch", "-q", "main"]);
84+ // A fast-forward lands in the checkout and drops the ref; untracked files stay. No
85+ // branch of the user's ever named the ticket.
86+ work(&repo, 1, "main", "b", "b\n");
7087 std::fs::write(repo.join("idea.md"), "mine").unwrap();
71- land(&repo, "t-1").unwrap();
88+ assert_eq!(run(&repo, &["branch", "--format=%(refname:short)"]).trim(), "main");
89+ assert_eq!(land(&repo, 1, "One").unwrap(), "main");
7290 assert!(repo.join("b").exists() && repo.join("idea.md").exists());
73- assert!(git(&repo, "", &["rev-parse", "-q", "--verify", "t-1"]).is_err());
91+ assert!(git(&repo, "", &["rev-parse", "-q", "--verify", &branch(1)]).is_err());
7492
75- // The stack: main, then t-a, then t-b built on t-a (no merge needed), and t-c, which
76- // conflicts with t-a and is left out. A ticket built on it lands all it was built on.
77- for (b, from, file, text) in [("t-a", "main", "x", "a\n"), ("t-b", "t-a", "y", "b\n"), ("t-c", "main", "x", "c\n")] {
78- run(&repo, &["switch", "-qc", b, from]);
79- commit(&repo, file, text);
93+ // The stack: the checkout, then 10, then 11 built on 10 (no merge needed), and 12, which
94+ // conflicts with 10 and is left out. A ticket built on it lands all it was built on.
95+ for (id, from, file, text) in [(10, "main".into(), "x", "a\n"), (11, branch(10), "y", "b\n"), (12, "main".into(), "x", "c\n")] {
96+ work(&repo, id, &from, file, text);
8097 }
81- run(&repo, &["switch", "-q", "main"]);
8298 commit(&repo, "z", "z\n");
83- let names = |v: &[&str]| v.iter().map(|s| s.to_string()).collect::<Vec<_>>();
84- let top = stack(&repo, &names(&["t-a", "t-b", "t-c"])).unwrap();
99+ let top = stack(&repo, &[(10, "Ten"), (11, "Eleven"), (12, "Twelve")]).unwrap();
85100 let files = run(&repo, &["ls-tree", "--name-only", &top]);
86101 assert!(files.contains('y') && files.contains('z') && run(&repo, &["show", &format!("{top}:x")]) == "a\n");
87- run(&repo, &["switch", "-qc", "t-d", &top]);
88- commit(&repo, "w", "w\n");
89- run(&repo, &["switch", "-q", "main"]);
90- land(&repo, "t-d").unwrap();
91- assert!(git(&repo, "", &["merge-base", "--is-ancestor", "t-b", "main"]).is_ok());
92- assert_eq!(stack(&repo, &names(&["t-a", "t-b"])).unwrap(), run(&repo, &["rev-parse", "main"]).trim());
93- run(&repo, &["branch", "-qD", "t-a", "t-b", "t-c"]);
102+ assert_eq!(subject(&repo, &format!("{top}^")), "Ten");
103+ work(&repo, 13, &top, "w", "w\n");
104+ land(&repo, 13, "Thirteen").unwrap();
105+ assert!(git(&repo, "", &["merge-base", "--is-ancestor", &branch(11), "main"]).is_ok());
106+ assert_eq!(stack(&repo, &[(10, "Ten"), (11, "Eleven")]).unwrap(), run(&repo, &["rev-parse", "main"]).trim());
107+ for id in [10, 11, 12] { run(&repo, &["update-ref", "-d", &branch(id)]); }
94108
95- // main moved meanwhile: a real merge.
96- run(&repo, &["switch", "-qc", "t-2"]);
97- commit(&repo, "c", "c\n");
98- run(&repo, &["switch", "-q", "main"]);
109+ // main moved meanwhile: a real merge, titled as the ticket.
110+ work(&repo, 2, "main", "c", "c\n");
99111 commit(&repo, "d", "d\n");
100- land(&repo, "t-2").unwrap();
112+ land(&repo, 2, "Two").unwrap();
101113 assert!(repo.join("c").exists() && repo.join("d").exists());
114+ assert_eq!(subject(&repo, "main"), "Two");
115+ work(&repo, 6, "main^", "g", "g\n");
116+ land(&repo, 6, " ").unwrap();
117+ assert_eq!(subject(&repo, "main"), "Ticket 6");
102118
103- // A conflict leaves main, the checkout and the branch alone.
104- run(&repo, &["switch", "-qc", "t-3"]);
105- commit(&repo, "a", "3\n");
106- run(&repo, &["switch", "-q", "main"]);
119+ // A conflict leaves the branch, the checkout and the ticket's ref alone.
120+ work(&repo, 3, "main", "a", "3\n");
107121 commit(&repo, "a", "main\n");
108122 let before = run(&repo, &["rev-parse", "main"]);
109- assert!(land(&repo, "t-3").unwrap_err().contains("conflicts with main in a"));
123+ assert!(land(&repo, 3, "Three").unwrap_err().contains("conflicts with main in a"));
110124 assert_eq!(run(&repo, &["rev-parse", "main"]), before);
125+ assert!(git(&repo, "", &["rev-parse", "-q", "--verify", &branch(3)]).is_ok());
111126 assert!(run(&repo, &["status", "--porcelain"]).lines().all(|l| l.starts_with("??")));
112127
113- // A file in the checkout the branch would overwrite: git refuses, nothing moves.
114- run(&repo, &["switch", "-qc", "t-4"]);
115- commit(&repo, "e", "e\n");
116- run(&repo, &["switch", "-q", "main"]);
128+ // A file in the checkout the work would overwrite: git refuses, nothing moves.
129+ work(&repo, 4, "main", "e", "e\n");
117130 std::fs::write(repo.join("e"), "dirty").unwrap();
118- let before = run(&repo, &["rev-parse", "main"]);
119- assert!(land(&repo, "t-4").is_err());
131+ assert!(land(&repo, 4, "Four").is_err());
120132 assert_eq!(run(&repo, &["rev-parse", "main"]), before);
121133 assert_eq!(std::fs::read_to_string(repo.join("e")).unwrap(), "dirty");
122134 std::fs::remove_file(repo.join("e")).unwrap();
123135
124- // main not checked out: a fast-forward lands, a real merge is refused.
125- run(&repo, &["switch", "-qc", "other"]);
126- land(&repo, "t-4").unwrap();
127- assert!(run(&repo, &["ls-tree", "--name-only", "main"]).contains('e'));
128- run(&repo, &["switch", "-qc", "t-5", "main"]);
129- commit(&repo, "f", "f\n");
130- run(&repo, &["switch", "-q", "main"]);
131- commit(&repo, "g", "g\n");
132- run(&repo, &["switch", "-q", "other"]);
133- assert!(land(&repo, "t-5").unwrap_err().contains("isn't checked out"));
136+ // Another branch checked out: tickets start from it, and land on it, not on main.
137+ run(&repo, &["switch", "-qc", "feature"]);
138+ commit(&repo, "h", "h\n");
139+ assert_eq!(stack(&repo, &[]).unwrap(), run(&repo, &["rev-parse", "feature"]).trim());
140+ assert_eq!(land(&repo, 4, "Four").unwrap(), "feature");
141+ let has_e = |rev| run(&repo, &["ls-tree", "--name-only", rev]).lines().any(|f| f == "e");
142+ assert!(has_e("feature") && !has_e("main"));
143+
144+ // No branch checked out: refused, the ref kept.
145+ work(&repo, 5, "main", "f", "f\n");
146+ run(&repo, &["switch", "-q", "--detach"]);
147+ assert!(land(&repo, 5, "Five").unwrap_err().contains("no branch is checked out"));
148+ assert!(git(&repo, "", &["rev-parse", "-q", "--verify", &branch(5)]).is_ok());
134149 let _ = std::fs::remove_dir_all(&repo);
135150 }
136151 }