1use crate::config::GitFlowConfig;
4use std::path::{Path, PathBuf};
5use std::process::Command;
6use tracing::{debug, info, warn};
7
8#[derive(Debug, thiserror::Error)]
10pub enum GitError {
11 #[error("failed to execute git: {0}")]
13 Io(#[from] std::io::Error),
14 #[error("git command failed: {0}")]
16 Command(String),
17}
18
19#[derive(Debug, Clone)]
21pub struct GitFlow {
22 root: PathBuf,
23 config: GitFlowConfig,
24}
25
26#[derive(Debug, Clone)]
28pub struct BranchInfo {
29 pub name: String,
31 pub ahead: usize,
33 pub behind: usize,
35 pub last_commit: String,
37}
38
39impl GitFlow {
40 pub fn new(root: impl AsRef<Path>) -> Self {
43 Self {
44 root: root.as_ref().to_path_buf(),
45 config: GitFlowConfig::default(),
46 }
47 }
48
49 pub fn feature_start(&self, phase: u32) -> Result<String, GitError> {
54 let branch = format!("{}phase-{:02}", self.config.feature_prefix, phase);
55 info!("creating feature branch: {branch}");
56 self.git(["checkout", &self.config.develop])?;
57 self.git(["checkout", "-b", &branch])?;
58 Ok(branch)
59 }
60
61 pub fn feature_start_force(&self, phase: u32) -> Result<String, GitError> {
63 let branch = format!("{}phase-{:02}", self.config.feature_prefix, phase);
64 warn!("force-creating feature branch: {branch}");
65 self.git(["checkout", &self.config.develop])?;
66 self.git(["checkout", "-B", &branch])?;
67 Ok(branch)
68 }
69
70 pub fn feature_finish(&self, phase: u32) -> Result<String, GitError> {
72 let branch = self.merge_feature_into_develop(phase)?;
73 self.git(["branch", "-d", &branch])?;
74 Ok(branch)
75 }
76
77 pub fn merge_feature_into_develop(&self, phase: u32) -> Result<String, GitError> {
82 let branch = format!("{}phase-{:02}", self.config.feature_prefix, phase);
83 info!("merging feature branch: {branch}");
84 self.git(["checkout", &self.config.develop])?;
85 self.git(["merge", "--no-ff", &branch])?;
86 Ok(branch)
87 }
88
89 pub fn is_merged_into_develop(&self, phase: u32) -> bool {
94 let branch = format!("{}phase-{:02}", self.config.feature_prefix, phase);
95 if !self.branch_exists(&branch) {
96 return false;
97 }
98
99 Command::new("git")
100 .args(["merge-base", "--is-ancestor", &branch, &self.config.develop])
101 .current_dir(&self.root)
102 .output()
103 .map(|output| output.status.success())
104 .unwrap_or(false)
105 }
106
107 pub fn release_start(&self, version: &str) -> Result<String, GitError> {
114 let branch = format!("release/{version}");
115 info!("creating release branch: {branch}");
116 self.git(["checkout", "-B", &branch])?;
117 Ok(branch)
118 }
119
120 pub fn release_finish(&self, version: &str) -> Result<String, GitError> {
122 let branch = format!("release/{version}");
123 info!("finishing release branch: {branch}");
124 self.git(["checkout", &self.config.main])?;
125 self.git(["merge", "--no-ff", &branch])?;
126 self.git(["-c", "tag.gpgSign=false", "tag", &format!("v{version}")])?;
133 self.git(["checkout", &self.config.develop])?;
134 self.git(["merge", "--no-ff", &branch])?;
135 self.git(["branch", "-d", &branch])?;
136 Ok(branch)
137 }
138
139 pub fn tag(&self, tag: &str) -> Result<(), GitError> {
148 info!("tagging {tag}");
149 self.git(["-c", "tag.gpgSign=false", "tag", tag])
150 }
151
152 pub fn delete_branch(&self, branch: &str, force: bool) -> Result<(), GitError> {
158 if branch == self.config.main || branch == self.config.develop {
159 return Err(GitError::Command(format!(
160 "refusing to delete protected branch `{branch}`"
161 )));
162 }
163 let flag = if force { "-D" } else { "-d" };
164 if force {
165 warn!("force-deleting branch: {branch}");
166 } else {
167 info!("deleting branch: {branch}");
168 }
169 self.git(["branch", flag, branch])
170 }
171
172 pub fn branch_exists(&self, branch: &str) -> bool {
174 Command::new("git")
175 .args([
176 "rev-parse",
177 "--verify",
178 "--quiet",
179 &format!("refs/heads/{branch}"),
180 ])
181 .current_dir(&self.root)
182 .output()
183 .map(|o| o.status.success())
184 .unwrap_or(false)
185 }
186
187 pub fn branch_tip(&self, branch: &str) -> Result<String, GitError> {
189 Ok(self.git_output(["rev-parse", branch])?.trim().to_string())
190 }
191
192 pub fn ensure_branch(&self, branch: &str, start_point: &str) -> Result<(), GitError> {
195 if self.branch_exists(branch) {
196 return Ok(());
197 }
198 self.git(["branch", branch, start_point])
199 }
200
201 pub fn fast_forward_branch(&self, target: &str, source: &str) -> Result<(), GitError> {
206 let is_ancestor = Command::new("git")
207 .args(["merge-base", "--is-ancestor", target, source])
208 .current_dir(&self.root)
209 .output()?
210 .status
211 .success();
212 if !is_ancestor {
213 return Err(GitError::Command(format!(
214 "{target} is not an ancestor of {source}; refusing non-fast-forward update"
215 )));
216 }
217 self.git(["branch", "-f", target, source])
218 }
219
220 pub fn rebase_in(&self, dir: &Path, onto: &str) -> Result<(), GitError> {
225 debug!("rebasing worktree at {} onto {onto}", dir.display());
226 match git_in(dir, &["rebase", onto]) {
227 Ok(()) => Ok(()),
228 Err(err) => {
229 warn!("rebase conflict in {}; aborting", dir.display());
231 let _ = git_in(dir, &["rebase", "--abort"]);
232 Err(err)
233 }
234 }
235 }
236
237 pub fn checkout(&self, branch: &str) -> Result<(), GitError> {
239 debug!("checking out branch: {branch}");
240 self.git(["checkout", branch])
241 }
242
243 pub fn delete_remote_branch(&self, branch: &str) -> Result<(), GitError> {
245 info!("deleting remote branch: {branch}");
246 self.git(["push", "origin", "--delete", branch])
247 }
248
249 pub fn has_remote(&self) -> bool {
251 self.git_output(["remote"])
252 .map(|s| !s.trim().is_empty())
253 .unwrap_or(false)
254 }
255
256 pub fn push(&self, branch: &str) -> Result<(), GitError> {
258 info!("pushing branch: {branch}");
259 self.git(["push", "-u", "origin", branch])
260 }
261
262 pub fn cleanup_merged(&self) -> Result<Vec<String>, GitError> {
278 let output = self.git_output(["branch", "--merged", &self.config.develop])?;
279 let protected = [self.config.main.as_str(), self.config.develop.as_str()];
280 let mut deleted = Vec::new();
281 for line in output.lines() {
282 let branch = line
288 .strip_prefix("* ")
289 .or_else(|| line.strip_prefix("+ "))
290 .unwrap_or(line)
291 .trim();
292 if branch.is_empty() || branch.starts_with('(') || protected.contains(&branch) {
295 continue;
296 }
297 info!("cleaning up merged branch: {branch}");
298 match self.git(["branch", "-D", branch]) {
299 Ok(()) => deleted.push(branch.to_string()),
300 Err(err) => warn!("could not delete merged branch {branch}: {err}"),
301 }
302 }
303 Ok(deleted)
304 }
305
306 pub fn commit_all(&self, message: &str) -> Result<(), GitError> {
309 debug!("committing all changes: {message}");
310 self.git(["add", "."])?;
311 match self.git_raw(&["commit", "--allow-empty", "-m", message]) {
313 Ok(()) => Ok(()),
314 Err(GitError::Command(ref msg)) if msg.contains("nothing to commit") => Ok(()),
317 Err(e) => Err(e),
318 }
319 }
320
321 pub fn commit_path(&self, relative_path: &str, message: &str) -> Result<(), GitError> {
326 debug!("committing {relative_path}: {message}");
327 self.git(["add", relative_path])?;
333 match self.git_raw(&[
335 "commit",
336 "--allow-empty",
337 "-m",
338 message,
339 "--",
340 relative_path,
341 ]) {
342 Ok(()) => Ok(()),
343 Err(GitError::Command(ref msg)) if msg.contains("nothing to commit") => Ok(()),
344 Err(e) => Err(e),
345 }
346 }
347
348 pub fn divergence_from_develop(&self) -> Result<(usize, usize), GitError> {
354 let current = self
355 .git_output(["rev-parse", "--abbrev-ref", "HEAD"])?
356 .trim()
357 .to_string();
358 if current == self.config.develop {
359 return Ok((0, 0));
360 }
361 let ahead = self
362 .rev_count(&format!("{}..{current}", self.config.develop))
363 .unwrap_or(0);
364 let behind = self
365 .rev_count(&format!("{current}..{}", self.config.develop))
366 .unwrap_or(0);
367 Ok((ahead, behind))
368 }
369
370 pub fn list_feature_branches(&self) -> Result<Vec<BranchInfo>, GitError> {
375 let prefix = &self.config.feature_prefix;
376 let branches = self.git_output(["branch", "--format=%(refname:short)"])?;
377 let mut result = Vec::new();
378 for name in branches.lines().map(|l| l.trim()) {
379 if name.is_empty()
380 || name == self.config.main
381 || name == self.config.develop
382 || !name.starts_with(prefix)
383 {
384 continue;
385 }
386 let ahead = self
387 .rev_count(&format!("{dev}..{name}", dev = self.config.develop))
388 .unwrap_or(0);
389 let behind = self
390 .rev_count(&format!("{name}..{dev}", dev = self.config.develop))
391 .unwrap_or(0);
392 let last_commit = self
393 .git_output(["log", "-1", "--format=%aI", name])
394 .map(|s| s.trim().to_string())
395 .unwrap_or_default();
396 result.push(BranchInfo {
397 name: name.to_string(),
398 ahead,
399 behind,
400 last_commit,
401 });
402 }
403 result.sort_by(|a, b| a.name.cmp(&b.name));
405 Ok(result)
406 }
407
408 fn rev_count(&self, range: &str) -> Option<usize> {
410 self.git_output(["rev-list", "--count", range])
411 .ok()
412 .and_then(|s| s.trim().parse().ok())
413 }
414
415 fn git_raw(&self, args: &[&str]) -> Result<(), GitError> {
416 debug!("git {}", args.join(" "));
417 let output = Command::new("git")
418 .args(args)
419 .current_dir(&self.root)
420 .output()?;
421 if output.status.success() {
422 Ok(())
423 } else {
424 Err(GitError::Command(stderr_or_status(&output)))
425 }
426 }
427
428 fn git<const N: usize>(&self, args: [&str; N]) -> Result<(), GitError> {
429 debug!("git {}", args.iter().copied().collect::<Vec<_>>().join(" "));
430 let output = Command::new("git")
431 .args(args)
432 .current_dir(&self.root)
433 .output()?;
434 if output.status.success() {
435 Ok(())
436 } else {
437 Err(GitError::Command(stderr_or_status(&output)))
438 }
439 }
440
441 fn git_output<const N: usize>(&self, args: [&str; N]) -> Result<String, GitError> {
442 let output = Command::new("git")
443 .args(args)
444 .current_dir(&self.root)
445 .output()?;
446 if output.status.success() {
447 Ok(String::from_utf8_lossy(&output.stdout).to_string())
448 } else {
449 Err(GitError::Command(stderr_or_status(&output)))
450 }
451 }
452}
453
454fn git_in(dir: &Path, args: &[&str]) -> Result<(), GitError> {
456 debug!("git (in {}) {}", dir.display(), args.join(" "));
457 let output = Command::new("git").args(args).current_dir(dir).output()?;
458 if output.status.success() {
459 Ok(())
460 } else {
461 Err(GitError::Command(stderr_or_status(&output)))
462 }
463}
464
465fn stderr_or_status(output: &std::process::Output) -> String {
466 let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
467 if stderr.is_empty() {
468 format!("exited with {}", output.status)
469 } else {
470 stderr
471 }
472}
473
474#[cfg(test)]
475mod tests {
476 use super::*;
477 use std::process::Command;
478 use tempfile::TempDir;
479
480 fn git(root: &Path, args: &[&str]) {
482 let output = Command::new("git")
483 .args(args)
484 .current_dir(root)
485 .output()
486 .expect("spawn git");
487 assert!(
488 output.status.success(),
489 "git {args:?} failed: {}",
490 String::from_utf8_lossy(&output.stderr)
491 );
492 }
493
494 fn current_branch(root: &Path) -> String {
495 let output = Command::new("git")
496 .args(["rev-parse", "--abbrev-ref", "HEAD"])
497 .current_dir(root)
498 .output()
499 .expect("rev-parse");
500 String::from_utf8_lossy(&output.stdout).trim().to_string()
501 }
502
503 fn commit_file(root: &Path, name: &str) {
504 std::fs::write(root.join(name), name).unwrap();
505 git(root, &["add", "."]);
506 git(root, &["commit", "-q", "-m", &format!("add {name}")]);
507 }
508
509 fn init_repo() -> TempDir {
511 let dir = tempfile::tempdir().unwrap();
512 let root = dir.path();
513 git(root, &["init", "-q"]);
514 git(root, &["config", "user.email", "test@example.com"]);
515 git(root, &["config", "user.name", "Test"]);
516 git(root, &["config", "commit.gpgsign", "false"]);
517 git(root, &["config", "tag.gpgsign", "false"]);
518 git(root, &["config", "core.hooksPath", "/dev/null"]);
520 commit_file(root, "README.md");
521 git(root, &["branch", "-M", "main"]);
522 git(root, &["checkout", "-q", "-b", "develop"]);
523 dir
524 }
525
526 fn flow(root: &Path) -> GitFlow {
527 GitFlow::new(root)
528 }
529
530 #[test]
531 fn feature_start_branches_from_develop() {
532 let repo = init_repo();
533 let root = repo.path();
534 let branch = flow(root).feature_start(3).expect("feature_start");
535 assert_eq!(branch, "feature/phase-03");
536 assert_eq!(current_branch(root), "feature/phase-03");
537 }
538
539 #[test]
540 fn list_feature_branches_reports_ahead_and_behind_semantics() {
541 let repo = init_repo();
542 let root = repo.path();
543 let gf = flow(root);
544
545 gf.feature_start(12).expect("feature_start");
546 commit_file(root, "feature-one.txt");
547 commit_file(root, "feature-two.txt");
548 git(root, &["checkout", "-q", "develop"]);
549 commit_file(root, "develop-only.txt");
550
551 let branches = gf.list_feature_branches().unwrap();
552 let branch = branches
553 .iter()
554 .find(|branch| branch.name == "feature/phase-12")
555 .unwrap();
556
557 assert_eq!(branch.ahead, 2);
558 assert_eq!(branch.behind, 1);
559 }
560
561 #[test]
562 fn feature_finish_merges_into_develop_and_deletes() {
563 let repo = init_repo();
564 let root = repo.path();
565 let gf = flow(root);
566
567 gf.feature_start(1).expect("start");
568 commit_file(root, "feature.txt");
569
570 let branch = gf.feature_finish(1).expect("finish");
571 assert_eq!(branch, "feature/phase-01");
572 assert_eq!(current_branch(root), "develop");
573
574 let branches = Command::new("git")
576 .args(["branch"])
577 .current_dir(root)
578 .output()
579 .unwrap();
580 let listing = String::from_utf8_lossy(&branches.stdout);
581 assert!(!listing.contains("feature/phase-01"));
582 assert!(root.join("feature.txt").exists());
583 }
584
585 #[test]
586 fn release_start_and_finish_tags_main_and_merges_both() {
587 let repo = init_repo();
588 let root = repo.path();
589 let gf = flow(root);
590
591 commit_file(root, "work.txt");
593 let branch = gf.release_start("1.2.0").expect("release_start");
594 assert_eq!(branch, "release/1.2.0");
595
596 gf.release_finish("1.2.0").expect("release_finish");
597 assert_eq!(current_branch(root), "develop");
598
599 let tags = Command::new("git")
601 .args(["tag"])
602 .current_dir(root)
603 .output()
604 .unwrap();
605 assert!(String::from_utf8_lossy(&tags.stdout).contains("v1.2.0"));
606
607 let branches = Command::new("git")
609 .args(["branch"])
610 .current_dir(root)
611 .output()
612 .unwrap();
613 assert!(!String::from_utf8_lossy(&branches.stdout).contains("release/1.2.0"));
614 }
615
616 #[test]
623 fn tag_stays_lightweight_when_gpgsign_is_forced_on() {
624 let repo = init_repo();
625 let root = repo.path();
626 git(root, &["config", "tag.gpgsign", "true"]);
630
631 flow(root)
632 .tag("v9.9.9")
633 .expect("tag must not block on $EDITOR");
634
635 let tags = Command::new("git")
636 .args(["tag", "-l"])
637 .current_dir(root)
638 .output()
639 .unwrap();
640 assert!(String::from_utf8_lossy(&tags.stdout).contains("v9.9.9"));
641
642 let obj_type = Command::new("git")
646 .args(["cat-file", "-t", "v9.9.9"])
647 .current_dir(root)
648 .output()
649 .unwrap();
650 assert_eq!(
651 String::from_utf8_lossy(&obj_type.stdout).trim(),
652 "commit",
653 "tag() must stay lightweight even when tag.gpgsign=true"
654 );
655 }
656
657 #[test]
658 fn commit_path_stages_only_the_given_path_leaving_other_dirt_uncommitted() {
659 let repo = init_repo();
663 let root = repo.path();
664 std::fs::write(root.join("CHANGELOG.md"), "# Changelog\n").unwrap();
665 std::fs::write(root.join("unrelated.txt"), "not part of this commit\n").unwrap();
666
667 Command::new("git")
672 .args(["add", "unrelated.txt"])
673 .current_dir(root)
674 .status()
675 .unwrap();
676
677 flow(root)
678 .commit_path("CHANGELOG.md", "docs: add changelog entry")
679 .expect("commit_path");
680
681 let committed = Command::new("git")
682 .args(["log", "-1", "--name-only", "--pretty=format:"])
683 .current_dir(root)
684 .output()
685 .unwrap();
686 let committed_files = String::from_utf8_lossy(&committed.stdout);
687 assert!(committed_files.contains("CHANGELOG.md"));
688 assert!(!committed_files.contains("unrelated.txt"));
689
690 let status = Command::new("git")
691 .args(["status", "--porcelain"])
692 .current_dir(root)
693 .output()
694 .unwrap();
695 let status = String::from_utf8_lossy(&status.stdout);
696 assert!(
697 status.contains("A unrelated.txt"),
698 "unrelated.txt must remain staged-but-uncommitted, got: {status}"
699 );
700 }
701
702 #[test]
703 fn release_start_branches_from_current_head_not_develop() {
704 let repo = init_repo();
705 let root = repo.path();
706 let gf = flow(root);
707
708 gf.feature_start(5).expect("feature_start");
710 commit_file(root, "feature-only.txt");
711 let feature_tip = gf.branch_tip("feature/phase-05").expect("feature tip");
712
713 let branch = gf.release_start("2.0.0").expect("release_start");
714 assert_eq!(branch, "release/2.0.0");
715 assert_eq!(current_branch(root), "release/2.0.0");
716
717 let release_tip = gf.branch_tip("release/2.0.0").expect("release tip");
720 let is_ancestor = Command::new("git")
721 .args(["merge-base", "--is-ancestor", &feature_tip, &release_tip])
722 .current_dir(root)
723 .output()
724 .unwrap()
725 .status
726 .success();
727 assert!(
728 is_ancestor,
729 "release branch must descend from the shipped feature commit"
730 );
731 assert!(root.join("feature-only.txt").exists());
732 }
733
734 #[test]
735 fn cleanup_merged_removes_merged_but_keeps_protected() {
736 let repo = init_repo();
737 let root = repo.path();
738 let gf = flow(root);
739
740 gf.feature_start(2).expect("start");
742 commit_file(root, "f.txt");
743 gf.feature_finish(2).expect("finish");
744
745 git(root, &["branch", "stale-merged"]);
747
748 let deleted = gf.cleanup_merged().expect("cleanup");
749 assert!(deleted.contains(&"stale-merged".to_string()));
750 assert!(!deleted.contains(&"develop".to_string()));
752 assert!(!deleted.contains(&"main".to_string()));
753 }
754
755 #[test]
762 fn cleanup_merged_is_relative_to_develop_not_current_head() {
763 let repo = init_repo();
764 let root = repo.path();
765 let gf = flow(root);
766
767 git(root, &["checkout", "-q", "-b", "topic", "develop"]);
771 commit_file(root, "topic-only.txt");
772 git(root, &["checkout", "-q", "-b", "premature", "topic"]);
773
774 git(root, &["checkout", "-q", "topic"]);
782
783 let _ = gf.cleanup_merged();
784 assert!(
785 gf.branch_exists("premature"),
786 "premature is merged into topic (current HEAD) but not into \
787 develop — it must survive cleanup_merged when the baseline is develop"
788 );
789 }
790
791 #[test]
799 fn cleanup_merged_skips_worktree_branch_and_continues_sweep() {
800 let repo = init_repo();
801 let root = repo.path();
802 let gf = flow(root);
803
804 git(
807 root,
808 &["checkout", "-q", "-b", "worktree-merged", "develop"],
809 );
810 commit_file(root, "g.txt");
811 git(root, &["checkout", "-q", "develop"]);
812 git(root, &["merge", "-q", "--no-ff", "worktree-merged"]);
813
814 let wt_dir = tempfile::tempdir().unwrap();
817 git(
818 root,
819 &[
820 "worktree",
821 "add",
822 wt_dir.path().to_str().unwrap(),
823 "worktree-merged",
824 ],
825 );
826
827 git(root, &["branch", "aa-stale"]);
831 git(root, &["branch", "zz-stale"]);
832
833 let deleted = gf
834 .cleanup_merged()
835 .expect("a skipped worktree branch must not abort the sweep");
836 assert!(deleted.contains(&"aa-stale".to_string()));
837 assert!(deleted.contains(&"zz-stale".to_string()));
838 assert!(
839 !deleted.contains(&"worktree-merged".to_string()),
840 "worktree checkout cannot be deleted"
841 );
842 assert!(gf.branch_exists("worktree-merged"));
843 }
844
845 #[test]
850 fn cleanup_merged_deletes_when_head_is_not_on_develop() {
851 let repo = init_repo();
852 let root = repo.path();
853 let gf = flow(root);
854
855 git(root, &["checkout", "-q", "-b", "old", "develop"]);
858 git(root, &["checkout", "-q", "develop"]);
859 git(root, &["checkout", "-q", "-b", "merged-feature", "develop"]);
860 commit_file(root, "h.txt");
861 git(root, &["checkout", "-q", "develop"]);
862 git(root, &["merge", "-q", "--no-ff", "merged-feature"]);
863 git(root, &["checkout", "-q", "old"]);
864
865 let deleted = gf.cleanup_merged().expect("cleanup");
866 assert!(
867 deleted.contains(&"merged-feature".to_string()),
868 "merged-into-develop branch must be deleted even when HEAD is elsewhere: {deleted:?}"
869 );
870 assert!(!gf.branch_exists("merged-feature"));
871 }
872
873 #[test]
874 fn delete_branch_removes_unmerged_with_force_and_protects_trunk() {
875 let repo = init_repo();
876 let root = repo.path();
877 let gf = flow(root);
878
879 gf.feature_start(8).expect("start");
881 commit_file(root, "unmerged.txt");
882 git(root, &["checkout", "-q", "develop"]);
884
885 assert!(gf.delete_branch("feature/phase-08", false).is_err());
887 gf.delete_branch("feature/phase-08", true)
888 .expect("force delete");
889 let branches = Command::new("git")
890 .args(["branch"])
891 .current_dir(root)
892 .output()
893 .unwrap();
894 assert!(!String::from_utf8_lossy(&branches.stdout).contains("feature/phase-08"));
895
896 assert!(gf.delete_branch("develop", true).is_err());
898 assert!(gf.delete_branch("main", true).is_err());
899 }
900
901 #[test]
902 fn sequentagent_helpers_integrate_and_rebase_cleanly() {
903 let repo = init_repo();
904 let root = repo.path();
905 let gf = flow(root);
906
907 gf.ensure_branch("feature/phase-07", "develop")
909 .expect("ensure base");
910 assert!(gf.branch_exists("feature/phase-07"));
911 assert!(!gf.branch_tip("feature/phase-07").unwrap().is_empty());
912 gf.ensure_branch("feature/phase-07", "develop")
914 .expect("ensure again");
915
916 let wt_a = root.join(".worktrees/a");
918 let wt_b = root.join(".worktrees/b");
919 crate::worktree::add(root, &wt_a, "feat-a", "feature/phase-07", true).expect("add A");
920 crate::worktree::add(root, &wt_b, "feat-b", "feature/phase-07", true).expect("add B");
921
922 std::fs::write(wt_a.join("a.txt"), "from-a\n").unwrap();
924 git(&wt_a, &["add", "."]);
925 git(&wt_a, &["commit", "-q", "-m", "a work"]);
926 gf.fast_forward_branch("feature/phase-07", "feat-a")
927 .expect("ff base to A");
928 assert_eq!(
929 gf.branch_tip("feature/phase-07").unwrap(),
930 gf.branch_tip("feat-a").unwrap()
931 );
932
933 gf.rebase_in(&wt_b, "feature/phase-07")
935 .expect("clean rebase");
936 assert!(wt_b.join("a.txt").exists());
938 }
939
940 #[test]
941 fn rebase_in_aborts_and_errors_on_conflict() {
942 let repo = init_repo();
943 let root = repo.path();
944 let gf = flow(root);
945
946 gf.ensure_branch("feature/phase-07", "develop")
947 .expect("ensure base");
948
949 let wt_b = root.join(".worktrees/b");
951 crate::worktree::add(root, &wt_b, "feat-b", "feature/phase-07", true).expect("add B");
952 std::fs::write(wt_b.join("a.txt"), "from-b\n").unwrap();
953 git(&wt_b, &["add", "."]);
954 git(&wt_b, &["commit", "-q", "-m", "b edits a"]);
955
956 let wt_a = root.join(".worktrees/a");
958 crate::worktree::add(root, &wt_a, "feat-a", "feature/phase-07", true).expect("add A");
959 std::fs::write(wt_a.join("a.txt"), "from-base\n").unwrap();
960 git(&wt_a, &["add", "."]);
961 git(&wt_a, &["commit", "-q", "-m", "base edits a"]);
962 gf.fast_forward_branch("feature/phase-07", "feat-a")
963 .expect("ff base to A");
964
965 let err = gf.rebase_in(&wt_b, "feature/phase-07").unwrap_err();
967 assert!(matches!(err, GitError::Command(_)));
968 assert!(!root.join(".git/worktrees/b/rebase-merge").exists());
970 assert_eq!(
972 std::fs::read_to_string(wt_b.join("a.txt")).unwrap(),
973 "from-b\n"
974 );
975 }
976
977 #[test]
978 fn merge_of_missing_branch_is_an_error() {
979 let repo = init_repo();
980 let root = repo.path();
981 let err = flow(root).feature_finish(99).unwrap_err();
984 assert!(matches!(err, GitError::Command(_)));
985 }
986}