@@ -0,0 +1,98 @@
1+//! Lands a code project's finished ticket: its branch merges into main, as Keep does for widgets.
2+use crate::{agent::git, library::merges};
3+use std::path::Path;
4+
5+/// Merges `branch` into `repo`'s main and deletes it. Leaves everything as it was, and says
6+/// why, when it conflicts with main or git won't touch the user's checkout.
7+pub fn land(repo: &Path, branch: &str) -> Result<(), String> {
8+ merges(repo, "main", branch).map_err(|files| format!(
9+ "{branch} conflicts with main in {files}. Move it to ready to have the agent merge main, or merge it yourself."))?;
10+ let head = git(repo, "", &["symbolic-ref", "-q", "--short", "HEAD"]).unwrap_or_default();
11+ if head.trim() == "main" {
12+ // The user's checkout: git refuses to overwrite their uncommitted changes, and says so.
13+ // It can't conflict (checked above), so there's never a merge of ours to abort.
14+ git(repo, "", &["merge", "-q", "--no-edit", branch])
15+ } else {
16+ // main isn't checked out, so there's no worktree to merge in: fast-forward only.
17+ git(repo, "", &["fetch", "-q", ".", &format!("{branch}:main")])
18+ .map_err(|_| format!("{branch} doesn't fast-forward main, and main isn't checked out. Check out main, then move it to done again."))
19+ }?;
20+ // In main now, which may not be HEAD, so -d would refuse.
21+ git(repo, "", &["branch", "-D", branch]).map(|_| ())
22+}
23+
24+#[cfg(test)]
25+mod tests {
26+ use super::*;
27+
28+ fn run(repo: &Path, args: &[&str]) -> String {
29+ git(repo, "", &[&["-c", "user.name=t", "-c", "user.email=t@t"][..], args].concat()).unwrap()
30+ }
31+
32+ fn commit(repo: &Path, file: &str, text: &str) {
33+ std::fs::write(repo.join(file), text).unwrap();
34+ run(repo, &["add", "-A"]);
35+ run(repo, &["commit", "-qm", file]);
36+ }
37+
38+ #[test]
39+ fn land_edges() {
40+ let repo = std::env::temp_dir().join(format!("conveyor-land-{}", std::process::id()));
41+ let _ = std::fs::remove_dir_all(&repo);
42+ std::fs::create_dir_all(&repo).unwrap();
43+ run(&repo, &["init", "-q", "-b", "main"]);
44+ run(&repo, &["config", "user.name", "t"]);
45+ run(&repo, &["config", "user.email", "t@t"]);
46+ commit(&repo, "a", "1\n");
47+
48+ // A fast-forward lands in the checkout and drops the branch; untracked files stay.
49+ run(&repo, &["switch", "-qc", "t-1"]);
50+ commit(&repo, "b", "b\n");
51+ run(&repo, &["switch", "-q", "main"]);
52+ std::fs::write(repo.join("idea.md"), "mine").unwrap();
53+ land(&repo, "t-1").unwrap();
54+ assert!(repo.join("b").exists() && repo.join("idea.md").exists());
55+ assert!(git(&repo, "", &["rev-parse", "-q", "--verify", "t-1"]).is_err());
56+
57+ // main moved meanwhile: a real merge.
58+ run(&repo, &["switch", "-qc", "t-2"]);
59+ commit(&repo, "c", "c\n");
60+ run(&repo, &["switch", "-q", "main"]);
61+ commit(&repo, "d", "d\n");
62+ land(&repo, "t-2").unwrap();
63+ assert!(repo.join("c").exists() && repo.join("d").exists());
64+
65+ // A conflict leaves main, the checkout and the branch alone.
66+ run(&repo, &["switch", "-qc", "t-3"]);
67+ commit(&repo, "a", "3\n");
68+ run(&repo, &["switch", "-q", "main"]);
69+ commit(&repo, "a", "main\n");
70+ let before = run(&repo, &["rev-parse", "main"]);
71+ assert!(land(&repo, "t-3").unwrap_err().contains("conflicts with main in a"));
72+ assert_eq!(run(&repo, &["rev-parse", "main"]), before);
73+ assert!(run(&repo, &["status", "--porcelain"]).lines().all(|l| l.starts_with("??")));
74+
75+ // A file in the checkout the branch would overwrite: git refuses, nothing moves.
76+ run(&repo, &["switch", "-qc", "t-4"]);
77+ commit(&repo, "e", "e\n");
78+ run(&repo, &["switch", "-q", "main"]);
79+ std::fs::write(repo.join("e"), "dirty").unwrap();
80+ let before = run(&repo, &["rev-parse", "main"]);
81+ assert!(land(&repo, "t-4").is_err());
82+ assert_eq!(run(&repo, &["rev-parse", "main"]), before);
83+ assert_eq!(std::fs::read_to_string(repo.join("e")).unwrap(), "dirty");
84+ std::fs::remove_file(repo.join("e")).unwrap();
85+
86+ // main not checked out: a fast-forward lands, a real merge is refused.
87+ run(&repo, &["switch", "-qc", "other"]);
88+ land(&repo, "t-4").unwrap();
89+ assert!(run(&repo, &["ls-tree", "--name-only", "main"]).contains('e'));
90+ run(&repo, &["switch", "-qc", "t-5", "main"]);
91+ commit(&repo, "f", "f\n");
92+ run(&repo, &["switch", "-q", "main"]);
93+ commit(&repo, "g", "g\n");
94+ run(&repo, &["switch", "-q", "other"]);
95+ assert!(land(&repo, "t-5").unwrap_err().contains("isn't checked out"));
96+ let _ = std::fs::remove_dir_all(&repo);
97+ }
98+}