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> {
330 debug!("committing {relative_path}: {message}");
331 self.git(["add", relative_path])?;
337 match self.git_raw_combined(&["commit", "-m", message, "--", relative_path]) {
338 Ok(()) => Ok(()),
339 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")
423 .args(args)
424 .env("LC_ALL", "C")
425 .env("LANG", "C")
426 .current_dir(&self.root)
427 .output()?;
428 if output.status.success() {
429 Ok(())
430 } else {
431 Err(GitError::Command(stderr_or_status(&output)))
432 }
433 }
434
435 fn git_raw_combined(&self, args: &[&str]) -> Result<(), GitError> {
449 debug!("git {}", args.join(" "));
450 let output = Command::new("git")
451 .args(args)
452 .env("LC_ALL", "C")
453 .env("LANG", "C")
454 .current_dir(&self.root)
455 .output()?;
456 if output.status.success() {
457 Ok(())
458 } else {
459 let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
460 let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
461 let combined = match (stderr.is_empty(), stdout.is_empty()) {
462 (false, false) => format!("{stderr}\n{stdout}"),
463 (false, true) => stderr,
464 (true, false) => stdout,
465 (true, true) => format!("exited with {}", output.status),
466 };
467 Err(GitError::Command(combined))
468 }
469 }
470
471 fn git<const N: usize>(&self, args: [&str; N]) -> Result<(), GitError> {
472 debug!("git {}", args.iter().copied().collect::<Vec<_>>().join(" "));
473 let output = Command::new("git")
474 .args(args)
475 .current_dir(&self.root)
476 .output()?;
477 if output.status.success() {
478 Ok(())
479 } else {
480 Err(GitError::Command(stderr_or_status(&output)))
481 }
482 }
483
484 fn git_output<const N: usize>(&self, args: [&str; N]) -> Result<String, GitError> {
485 let output = Command::new("git")
486 .args(args)
487 .current_dir(&self.root)
488 .output()?;
489 if output.status.success() {
490 Ok(String::from_utf8_lossy(&output.stdout).to_string())
491 } else {
492 Err(GitError::Command(stderr_or_status(&output)))
493 }
494 }
495}
496
497#[derive(Debug, Clone, Copy, PartialEq, Eq)]
502pub enum AncestorStatus {
503 Ancestor,
505 Diverged,
509 RefAbsent,
514}
515
516pub fn origin_main_ancestor_status(project_root: &Path) -> AncestorStatus {
523 let ref_exists = Command::new("git")
524 .args(["rev-parse", "--verify", "--quiet", "origin/main"])
525 .current_dir(project_root)
526 .output()
527 .map(|out| out.status.success())
528 .unwrap_or(false);
529 if !ref_exists {
530 return AncestorStatus::RefAbsent;
531 }
532 let is_ancestor = Command::new("git")
533 .args(["merge-base", "--is-ancestor", "origin/main", "HEAD"])
534 .current_dir(project_root)
535 .output()
536 .map(|out| out.status.success())
537 .unwrap_or(false);
538 if is_ancestor {
539 AncestorStatus::Ancestor
540 } else {
541 AncestorStatus::Diverged
542 }
543}
544
545pub fn publish_order(project_root: &Path) -> Vec<String> {
552 let Ok(root_contents) = std::fs::read_to_string(project_root.join("Cargo.toml")) else {
553 return Vec::new();
554 };
555 let member_paths = workspace_member_paths(&root_contents);
556
557 let mut members: Vec<(String, String)> = Vec::new();
558 for path in &member_paths {
559 let manifest = project_root.join(path).join("Cargo.toml");
560 let Ok(contents) = std::fs::read_to_string(&manifest) else {
561 continue;
562 };
563 let name = package_name(&contents).unwrap_or_else(|| path.clone());
564 members.push((name, contents));
565 }
566
567 let names: Vec<String> = members.iter().map(|(name, _)| name.clone()).collect();
568 let mut edges: Vec<(String, String)> = Vec::new();
569 for (name, contents) in &members {
570 for other in &names {
571 if other != name && member_depends_on(contents, other) {
572 edges.push((name.clone(), other.clone()));
573 }
574 }
575 }
576 topo_sort(names, edges)
577}
578
579fn workspace_member_paths(contents: &str) -> Vec<String> {
584 let Some(start) = contents.find("members") else {
585 return Vec::new();
586 };
587 let rest = &contents[start..];
588 let Some(open) = rest.find('[') else {
589 return Vec::new();
590 };
591 let Some(close) = rest[open..].find(']') else {
592 return Vec::new();
593 };
594 let inner = &rest[open + 1..open + close];
595 inner
596 .split(',')
597 .filter_map(|fragment| {
598 let fragment = fragment.trim();
599 let fragment = fragment.strip_prefix('"')?.strip_suffix('"')?;
600 (!fragment.is_empty()).then(|| fragment.to_string())
601 })
602 .collect()
603}
604
605fn package_name(contents: &str) -> Option<String> {
607 let mut current = String::new();
608 for line in contents.lines() {
609 let trimmed = line.trim();
610 if let Some(inner) = trimmed.strip_prefix('[').and_then(|s| s.strip_suffix(']')) {
611 current = inner.trim().to_string();
612 continue;
613 }
614 if current == "package"
615 && let Some((key, value)) = trimmed.split_once('=')
616 && key.trim() == "name"
617 {
618 return Some(value.trim().trim_matches('"').to_string());
619 }
620 }
621 None
622}
623
624fn member_depends_on(contents: &str, dep_name: &str) -> bool {
635 let mut current = String::new();
636 for line in contents.lines() {
637 let trimmed = line.trim();
638 if let Some(inner) = trimmed.strip_prefix('[').and_then(|s| s.strip_suffix(']')) {
639 current = inner.trim().to_string();
640 if let Some(name) = current.strip_prefix("dependencies.")
641 && name == dep_name
642 {
643 return true;
644 }
645 continue;
646 }
647 if current != "dependencies" {
648 continue;
649 }
650 let key = trimmed.split(['.', '=']).next().unwrap_or("").trim();
651 if key == dep_name {
652 return true;
653 }
654 }
655 false
656}
657
658fn topo_sort(names: Vec<String>, edges: Vec<(String, String)>) -> Vec<String> {
664 let mut result = Vec::new();
665 let mut published: Vec<String> = Vec::new();
666 let mut remaining = names;
667 while !remaining.is_empty() {
668 let ready: Vec<String> = remaining
669 .iter()
670 .filter(|name| {
671 edges
672 .iter()
673 .filter(|(dependent, _)| dependent == *name)
674 .all(|(_, dep)| published.contains(dep))
675 })
676 .cloned()
677 .collect();
678 if ready.is_empty() {
679 result.extend(remaining);
680 break;
681 }
682 for name in &ready {
683 published.push(name.clone());
684 result.push(name.clone());
685 }
686 remaining.retain(|name| !ready.contains(name));
687 }
688 result
689}
690
691#[derive(Debug, Clone, Copy, PartialEq, Eq)]
699pub enum SigningStatus {
700 NoAgent,
702 AgentEmpty,
704 KeysListed,
707 Unknown(i32),
710}
711
712pub fn classify_ssh_add_status(exit_code: i32) -> SigningStatus {
715 match exit_code {
716 2 => SigningStatus::NoAgent,
717 1 => SigningStatus::AgentEmpty,
718 0 => SigningStatus::KeysListed,
719 other => SigningStatus::Unknown(other),
720 }
721}
722
723#[derive(Debug, Clone, PartialEq, Eq)]
729pub enum SigningViability {
730 Viable { fingerprint: Option<String> },
733 NotViable { reason: String },
735 Unknown { reason: String },
738}
739
740fn git_config(project_root: &Path, key: &str) -> Option<String> {
743 let output = Command::new("git")
744 .args(["config", "--get", key])
745 .current_dir(project_root)
746 .output()
747 .ok()?;
748 if !output.status.success() {
749 return None;
750 }
751 let value = String::from_utf8_lossy(&output.stdout).trim().to_string();
752 (!value.is_empty()).then_some(value)
753}
754
755fn public_key_fingerprint(pub_key_path: &Path) -> Option<String> {
759 let path_str = pub_key_path.to_str()?;
760 let output = Command::new("ssh-keygen")
761 .args(["-lf", path_str])
762 .output()
763 .ok()?;
764 if !output.status.success() {
765 return None;
766 }
767 String::from_utf8_lossy(&output.stdout)
769 .split_whitespace()
770 .nth(1)
771 .map(str::to_string)
772}
773
774fn check_ssh_signing_viability(project_root: &Path) -> SigningViability {
779 let Some(signingkey) = git_config(project_root, "user.signingkey") else {
780 return SigningViability::NotViable {
781 reason: "gpg.format=ssh but user.signingkey is not set".into(),
782 };
783 };
784 let key_path = Path::new(&signingkey);
785 if !key_path.exists() {
786 return SigningViability::NotViable {
787 reason: "user.signingkey is set but the key file does not exist".into(),
788 };
789 }
790
791 let output = match Command::new("ssh-add").arg("-l").output() {
792 Ok(out) => out,
793 Err(_) => {
794 return SigningViability::Unknown {
795 reason: "cannot verify signing viability — ssh-add not found".into(),
796 };
797 }
798 };
799 let exit_code = output.status.code().unwrap_or(-1);
800 match classify_ssh_add_status(exit_code) {
801 SigningStatus::NoAgent => SigningViability::NotViable {
802 reason: "no ssh-agent reachable (SSH_AUTH_SOCK unset or dead)".into(),
803 },
804 SigningStatus::AgentEmpty => SigningViability::NotViable {
805 reason: "ssh-agent reachable but has no identities loaded".into(),
806 },
807 SigningStatus::KeysListed => {
808 let stdout = String::from_utf8_lossy(&output.stdout);
809 match public_key_fingerprint(key_path) {
810 Some(fingerprint) if stdout.contains(&fingerprint) => SigningViability::Viable {
811 fingerprint: Some(fingerprint),
812 },
813 Some(_) => SigningViability::NotViable {
814 reason: "ssh-agent has keys loaded, but not the configured signing key".into(),
815 },
816 None => SigningViability::Unknown {
817 reason: "cannot verify signing viability — ssh-keygen not found or the key \
818 is unreadable"
819 .into(),
820 },
821 }
822 }
823 SigningStatus::Unknown(code) => SigningViability::Unknown {
824 reason: format!("ssh-add -l exited with an unexpected code {code}"),
825 },
826 }
827}
828
829fn check_gpg_signing_viability(project_root: &Path) -> SigningViability {
832 let Some(signingkey) = git_config(project_root, "user.signingkey") else {
833 return SigningViability::Unknown {
834 reason: "cannot verify signing viability — user.signingkey is not set".into(),
835 };
836 };
837 let output = match Command::new("gpg")
838 .args(["--list-secret-keys", &signingkey])
839 .output()
840 {
841 Ok(out) => out,
842 Err(_) => {
843 return SigningViability::Unknown {
844 reason: "cannot verify signing viability — gpg not found".into(),
845 };
846 }
847 };
848 if output.status.success() {
849 SigningViability::Viable {
850 fingerprint: Some(signingkey),
851 }
852 } else {
853 SigningViability::NotViable {
854 reason: "no secret key found for the configured user.signingkey".into(),
855 }
856 }
857}
858
859pub fn check_signing_viability(project_root: &Path) -> SigningViability {
866 match git_config(project_root, "gpg.format").as_deref() {
867 Some("ssh") => check_ssh_signing_viability(project_root),
868 _ => check_gpg_signing_viability(project_root),
869 }
870}
871
872fn git_in(dir: &Path, args: &[&str]) -> Result<(), GitError> {
874 debug!("git (in {}) {}", dir.display(), args.join(" "));
875 let output = Command::new("git").args(args).current_dir(dir).output()?;
876 if output.status.success() {
877 Ok(())
878 } else {
879 Err(GitError::Command(stderr_or_status(&output)))
880 }
881}
882
883fn stderr_or_status(output: &std::process::Output) -> String {
884 let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
885 if stderr.is_empty() {
886 format!("exited with {}", output.status)
887 } else {
888 stderr
889 }
890}
891
892#[cfg(test)]
893mod tests {
894 use super::*;
895 use tempfile::TempDir;
896
897 fn git(root: &Path, args: &[&str]) {
899 let output = crate::test_support::git_command(root)
900 .args(args)
901 .output()
902 .expect("spawn git");
903 assert!(
904 output.status.success(),
905 "git {args:?} failed: {}",
906 String::from_utf8_lossy(&output.stderr)
907 );
908 }
909
910 fn current_branch(root: &Path) -> String {
911 let output = crate::test_support::git_command(root)
912 .args(["rev-parse", "--abbrev-ref", "HEAD"])
913 .output()
914 .expect("rev-parse");
915 String::from_utf8_lossy(&output.stdout).trim().to_string()
916 }
917
918 fn commit_file(root: &Path, name: &str) {
919 std::fs::write(root.join(name), name).unwrap();
920 git(root, &["add", "."]);
921 git(root, &["commit", "-q", "-m", &format!("add {name}")]);
922 }
923
924 fn init_repo() -> TempDir {
926 let dir = tempfile::tempdir().unwrap();
927 let root = dir.path();
928 git(root, &["init", "-q"]);
929 git(root, &["config", "user.email", "test@example.com"]);
930 git(root, &["config", "user.name", "Test"]);
931 git(root, &["config", "commit.gpgsign", "false"]);
932 git(root, &["config", "tag.gpgsign", "false"]);
933 git(root, &["config", "core.hooksPath", "/dev/null"]);
935 commit_file(root, "README.md");
936 git(root, &["branch", "-M", "main"]);
937 git(root, &["checkout", "-q", "-b", "develop"]);
938 dir
939 }
940
941 fn flow(root: &Path) -> GitFlow {
942 GitFlow::new(root)
943 }
944
945 #[test]
946 fn feature_start_branches_from_develop() {
947 let repo = init_repo();
948 let root = repo.path();
949 let branch = flow(root).feature_start(3).expect("feature_start");
950 assert_eq!(branch, "feature/phase-03");
951 assert_eq!(current_branch(root), "feature/phase-03");
952 }
953
954 #[test]
955 fn list_feature_branches_reports_ahead_and_behind_semantics() {
956 let repo = init_repo();
957 let root = repo.path();
958 let gf = flow(root);
959
960 gf.feature_start(12).expect("feature_start");
961 commit_file(root, "feature-one.txt");
962 commit_file(root, "feature-two.txt");
963 git(root, &["checkout", "-q", "develop"]);
964 commit_file(root, "develop-only.txt");
965
966 let branches = gf.list_feature_branches().unwrap();
967 let branch = branches
968 .iter()
969 .find(|branch| branch.name == "feature/phase-12")
970 .unwrap();
971
972 assert_eq!(branch.ahead, 2);
973 assert_eq!(branch.behind, 1);
974 }
975
976 #[test]
977 fn feature_finish_merges_into_develop_and_deletes() {
978 let repo = init_repo();
979 let root = repo.path();
980 let gf = flow(root);
981
982 gf.feature_start(1).expect("start");
983 commit_file(root, "feature.txt");
984
985 let branch = gf.feature_finish(1).expect("finish");
986 assert_eq!(branch, "feature/phase-01");
987 assert_eq!(current_branch(root), "develop");
988
989 let branches = crate::test_support::git_command(root)
991 .args(["branch"])
992 .output()
993 .unwrap();
994 let listing = String::from_utf8_lossy(&branches.stdout);
995 assert!(!listing.contains("feature/phase-01"));
996 assert!(root.join("feature.txt").exists());
997 }
998
999 #[test]
1000 fn release_start_and_finish_tags_main_and_merges_both() {
1001 let repo = init_repo();
1002 let root = repo.path();
1003 let gf = flow(root);
1004
1005 commit_file(root, "work.txt");
1007 let branch = gf.release_start("1.2.0").expect("release_start");
1008 assert_eq!(branch, "release/1.2.0");
1009
1010 gf.release_finish("1.2.0").expect("release_finish");
1011 assert_eq!(current_branch(root), "develop");
1012
1013 let tags = crate::test_support::git_command(root)
1015 .args(["tag"])
1016 .output()
1017 .unwrap();
1018 assert!(String::from_utf8_lossy(&tags.stdout).contains("v1.2.0"));
1019
1020 let branches = crate::test_support::git_command(root)
1022 .args(["branch"])
1023 .output()
1024 .unwrap();
1025 assert!(!String::from_utf8_lossy(&branches.stdout).contains("release/1.2.0"));
1026 }
1027
1028 #[test]
1035 fn tag_stays_lightweight_when_gpgsign_is_forced_on() {
1036 let repo = init_repo();
1037 let root = repo.path();
1038 git(root, &["config", "tag.gpgsign", "true"]);
1042
1043 flow(root)
1044 .tag("v9.9.9")
1045 .expect("tag must not block on $EDITOR");
1046
1047 let tags = crate::test_support::git_command(root)
1048 .args(["tag", "-l"])
1049 .output()
1050 .unwrap();
1051 assert!(String::from_utf8_lossy(&tags.stdout).contains("v9.9.9"));
1052
1053 let obj_type = crate::test_support::git_command(root)
1057 .args(["cat-file", "-t", "v9.9.9"])
1058 .output()
1059 .unwrap();
1060 assert_eq!(
1061 String::from_utf8_lossy(&obj_type.stdout).trim(),
1062 "commit",
1063 "tag() must stay lightweight even when tag.gpgsign=true"
1064 );
1065 }
1066
1067 #[test]
1068 fn commit_path_stages_only_the_given_path_leaving_other_dirt_uncommitted() {
1069 let repo = init_repo();
1073 let root = repo.path();
1074 std::fs::write(root.join("CHANGELOG.md"), "# Changelog\n").unwrap();
1075 std::fs::write(root.join("unrelated.txt"), "not part of this commit\n").unwrap();
1076
1077 crate::test_support::git_command(root)
1082 .args(["add", "unrelated.txt"])
1083 .status()
1084 .unwrap();
1085
1086 flow(root)
1087 .commit_path("CHANGELOG.md", "docs: add changelog entry")
1088 .expect("commit_path");
1089
1090 let committed = crate::test_support::git_command(root)
1091 .args(["log", "-1", "--name-only", "--pretty=format:"])
1092 .output()
1093 .unwrap();
1094 let committed_files = String::from_utf8_lossy(&committed.stdout);
1095 assert!(committed_files.contains("CHANGELOG.md"));
1096 assert!(!committed_files.contains("unrelated.txt"));
1097
1098 let status = crate::test_support::git_command(root)
1099 .args(["status", "--porcelain"])
1100 .output()
1101 .unwrap();
1102 let status = String::from_utf8_lossy(&status.stdout);
1103 assert!(
1104 status.contains("A unrelated.txt"),
1105 "unrelated.txt must remain staged-but-uncommitted, got: {status}"
1106 );
1107 }
1108
1109 fn rev_list_count(root: &Path) -> u32 {
1112 let output = crate::test_support::git_command(root)
1113 .args(["rev-list", "--count", "HEAD"])
1114 .output()
1115 .unwrap();
1116 assert!(output.status.success(), "git rev-list --count HEAD failed");
1117 String::from_utf8_lossy(&output.stdout)
1118 .trim()
1119 .parse::<u32>()
1120 .expect("rev-list --count HEAD must print an integer")
1121 }
1122
1123 #[test]
1131 fn commit_path_twice_with_identical_content_creates_only_one_commit() {
1132 let repo = init_repo();
1133 let root = repo.path();
1134 std::fs::write(root.join("CHANGELOG.md"), "# Changelog\n").unwrap();
1135
1136 flow(root)
1137 .commit_path("CHANGELOG.md", "docs: add changelog entry")
1138 .expect("first commit_path call");
1139 let n1 = rev_list_count(root);
1140
1141 flow(root)
1144 .commit_path("CHANGELOG.md", "docs: add changelog entry")
1145 .expect("second commit_path call");
1146 let n2 = rev_list_count(root);
1147
1148 assert_eq!(
1149 n2, n1,
1150 "a repeat commit_path call on unchanged content must not add a \
1151 commit: n1={n1}, n2={n2}"
1152 );
1153 }
1154
1155 #[test]
1163 fn commit_path_with_no_changes_returns_ok_without_committing() {
1164 let repo = init_repo();
1165 let root = repo.path();
1166 std::fs::write(root.join("CHANGELOG.md"), "# Changelog\n").unwrap();
1167 flow(root)
1168 .commit_path("CHANGELOG.md", "docs: add changelog entry")
1169 .expect("initial commit_path");
1170 let n1 = rev_list_count(root);
1171
1172 let result = flow(root).commit_path("CHANGELOG.md", "docs: add changelog entry");
1175 let n2 = rev_list_count(root);
1176
1177 assert!(
1178 result.is_ok(),
1179 "no-op call must return Ok(()), got: {result:?}"
1180 );
1181 assert_eq!(
1182 n2, n1,
1183 "no-op call must not create a commit: n1={n1}, n2={n2}"
1184 );
1185 }
1186
1187 #[test]
1193 fn commit_path_on_nonexistent_path_still_errors() {
1194 let repo = init_repo();
1195 let root = repo.path();
1196
1197 let result = flow(root).commit_path("does-not-exist.md", "docs: add changelog entry");
1198
1199 assert!(
1200 result.is_err(),
1201 "commit_path on an unknown pathspec must still error, got: {result:?}"
1202 );
1203 }
1204
1205 #[test]
1206 fn release_start_branches_from_current_head_not_develop() {
1207 let repo = init_repo();
1208 let root = repo.path();
1209 let gf = flow(root);
1210
1211 gf.feature_start(5).expect("feature_start");
1213 commit_file(root, "feature-only.txt");
1214 let feature_tip = gf.branch_tip("feature/phase-05").expect("feature tip");
1215
1216 let branch = gf.release_start("2.0.0").expect("release_start");
1217 assert_eq!(branch, "release/2.0.0");
1218 assert_eq!(current_branch(root), "release/2.0.0");
1219
1220 let release_tip = gf.branch_tip("release/2.0.0").expect("release tip");
1223 let is_ancestor = crate::test_support::git_command(root)
1224 .args(["merge-base", "--is-ancestor", &feature_tip, &release_tip])
1225 .output()
1226 .unwrap()
1227 .status
1228 .success();
1229 assert!(
1230 is_ancestor,
1231 "release branch must descend from the shipped feature commit"
1232 );
1233 assert!(root.join("feature-only.txt").exists());
1234 }
1235
1236 #[test]
1237 fn cleanup_merged_removes_merged_but_keeps_protected() {
1238 let repo = init_repo();
1239 let root = repo.path();
1240 let gf = flow(root);
1241
1242 gf.feature_start(2).expect("start");
1244 commit_file(root, "f.txt");
1245 gf.feature_finish(2).expect("finish");
1246
1247 git(root, &["branch", "stale-merged"]);
1249
1250 let deleted = gf.cleanup_merged().expect("cleanup");
1251 assert!(deleted.contains(&"stale-merged".to_string()));
1252 assert!(!deleted.contains(&"develop".to_string()));
1254 assert!(!deleted.contains(&"main".to_string()));
1255 }
1256
1257 #[test]
1264 fn cleanup_merged_is_relative_to_develop_not_current_head() {
1265 let repo = init_repo();
1266 let root = repo.path();
1267 let gf = flow(root);
1268
1269 git(root, &["checkout", "-q", "-b", "topic", "develop"]);
1273 commit_file(root, "topic-only.txt");
1274 git(root, &["checkout", "-q", "-b", "premature", "topic"]);
1275
1276 git(root, &["checkout", "-q", "topic"]);
1284
1285 let _ = gf.cleanup_merged();
1286 assert!(
1287 gf.branch_exists("premature"),
1288 "premature is merged into topic (current HEAD) but not into \
1289 develop — it must survive cleanup_merged when the baseline is develop"
1290 );
1291 }
1292
1293 #[test]
1301 fn cleanup_merged_skips_worktree_branch_and_continues_sweep() {
1302 let repo = init_repo();
1303 let root = repo.path();
1304 let gf = flow(root);
1305
1306 git(
1309 root,
1310 &["checkout", "-q", "-b", "worktree-merged", "develop"],
1311 );
1312 commit_file(root, "g.txt");
1313 git(root, &["checkout", "-q", "develop"]);
1314 git(root, &["merge", "-q", "--no-ff", "worktree-merged"]);
1315
1316 let wt_dir = tempfile::tempdir().unwrap();
1319 git(
1320 root,
1321 &[
1322 "worktree",
1323 "add",
1324 wt_dir.path().to_str().unwrap(),
1325 "worktree-merged",
1326 ],
1327 );
1328
1329 git(root, &["branch", "aa-stale"]);
1333 git(root, &["branch", "zz-stale"]);
1334
1335 let deleted = gf
1336 .cleanup_merged()
1337 .expect("a skipped worktree branch must not abort the sweep");
1338 assert!(deleted.contains(&"aa-stale".to_string()));
1339 assert!(deleted.contains(&"zz-stale".to_string()));
1340 assert!(
1341 !deleted.contains(&"worktree-merged".to_string()),
1342 "worktree checkout cannot be deleted"
1343 );
1344 assert!(gf.branch_exists("worktree-merged"));
1345 }
1346
1347 #[test]
1352 fn cleanup_merged_deletes_when_head_is_not_on_develop() {
1353 let repo = init_repo();
1354 let root = repo.path();
1355 let gf = flow(root);
1356
1357 git(root, &["checkout", "-q", "-b", "old", "develop"]);
1360 git(root, &["checkout", "-q", "develop"]);
1361 git(root, &["checkout", "-q", "-b", "merged-feature", "develop"]);
1362 commit_file(root, "h.txt");
1363 git(root, &["checkout", "-q", "develop"]);
1364 git(root, &["merge", "-q", "--no-ff", "merged-feature"]);
1365 git(root, &["checkout", "-q", "old"]);
1366
1367 let deleted = gf.cleanup_merged().expect("cleanup");
1368 assert!(
1369 deleted.contains(&"merged-feature".to_string()),
1370 "merged-into-develop branch must be deleted even when HEAD is elsewhere: {deleted:?}"
1371 );
1372 assert!(!gf.branch_exists("merged-feature"));
1373 }
1374
1375 #[test]
1376 fn delete_branch_removes_unmerged_with_force_and_protects_trunk() {
1377 let repo = init_repo();
1378 let root = repo.path();
1379 let gf = flow(root);
1380
1381 gf.feature_start(8).expect("start");
1383 commit_file(root, "unmerged.txt");
1384 git(root, &["checkout", "-q", "develop"]);
1386
1387 assert!(gf.delete_branch("feature/phase-08", false).is_err());
1389 gf.delete_branch("feature/phase-08", true)
1390 .expect("force delete");
1391 let branches = crate::test_support::git_command(root)
1392 .args(["branch"])
1393 .output()
1394 .unwrap();
1395 assert!(!String::from_utf8_lossy(&branches.stdout).contains("feature/phase-08"));
1396
1397 assert!(gf.delete_branch("develop", true).is_err());
1399 assert!(gf.delete_branch("main", true).is_err());
1400 }
1401
1402 #[test]
1403 fn sequentagent_helpers_integrate_and_rebase_cleanly() {
1404 let repo = init_repo();
1405 let root = repo.path();
1406 let gf = flow(root);
1407
1408 gf.ensure_branch("feature/phase-07", "develop")
1410 .expect("ensure base");
1411 assert!(gf.branch_exists("feature/phase-07"));
1412 assert!(!gf.branch_tip("feature/phase-07").unwrap().is_empty());
1413 gf.ensure_branch("feature/phase-07", "develop")
1415 .expect("ensure again");
1416
1417 let wt_a = root.join(".worktrees/a");
1419 let wt_b = root.join(".worktrees/b");
1420 crate::worktree::add(root, &wt_a, "feat-a", "feature/phase-07", true).expect("add A");
1421 crate::worktree::add(root, &wt_b, "feat-b", "feature/phase-07", true).expect("add B");
1422
1423 std::fs::write(wt_a.join("a.txt"), "from-a\n").unwrap();
1425 git(&wt_a, &["add", "."]);
1426 git(&wt_a, &["commit", "-q", "-m", "a work"]);
1427 gf.fast_forward_branch("feature/phase-07", "feat-a")
1428 .expect("ff base to A");
1429 assert_eq!(
1430 gf.branch_tip("feature/phase-07").unwrap(),
1431 gf.branch_tip("feat-a").unwrap()
1432 );
1433
1434 gf.rebase_in(&wt_b, "feature/phase-07")
1436 .expect("clean rebase");
1437 assert!(wt_b.join("a.txt").exists());
1439 }
1440
1441 #[test]
1442 fn rebase_in_aborts_and_errors_on_conflict() {
1443 let repo = init_repo();
1444 let root = repo.path();
1445 let gf = flow(root);
1446
1447 gf.ensure_branch("feature/phase-07", "develop")
1448 .expect("ensure base");
1449
1450 let wt_b = root.join(".worktrees/b");
1452 crate::worktree::add(root, &wt_b, "feat-b", "feature/phase-07", true).expect("add B");
1453 std::fs::write(wt_b.join("a.txt"), "from-b\n").unwrap();
1454 git(&wt_b, &["add", "."]);
1455 git(&wt_b, &["commit", "-q", "-m", "b edits a"]);
1456
1457 let wt_a = root.join(".worktrees/a");
1459 crate::worktree::add(root, &wt_a, "feat-a", "feature/phase-07", true).expect("add A");
1460 std::fs::write(wt_a.join("a.txt"), "from-base\n").unwrap();
1461 git(&wt_a, &["add", "."]);
1462 git(&wt_a, &["commit", "-q", "-m", "base edits a"]);
1463 gf.fast_forward_branch("feature/phase-07", "feat-a")
1464 .expect("ff base to A");
1465
1466 let err = gf.rebase_in(&wt_b, "feature/phase-07").unwrap_err();
1468 assert!(matches!(err, GitError::Command(_)));
1469 assert!(!root.join(".git/worktrees/b/rebase-merge").exists());
1471 assert_eq!(
1473 std::fs::read_to_string(wt_b.join("a.txt")).unwrap(),
1474 "from-b\n"
1475 );
1476 }
1477
1478 #[test]
1479 fn merge_of_missing_branch_is_an_error() {
1480 let repo = init_repo();
1481 let root = repo.path();
1482 let err = flow(root).feature_finish(99).unwrap_err();
1485 assert!(matches!(err, GitError::Command(_)));
1486 }
1487
1488 #[test]
1493 fn workspace_member_paths_parses_multiline_array() {
1494 let contents = "[workspace]\nresolver = \"2\"\nmembers = [\n \"crates/devflow-core\",\n \"crates/devflow-cli\",\n]\n";
1495 assert_eq!(
1496 workspace_member_paths(contents),
1497 vec![
1498 "crates/devflow-core".to_string(),
1499 "crates/devflow-cli".to_string()
1500 ]
1501 );
1502 }
1503
1504 #[test]
1505 fn package_name_reads_the_package_section() {
1506 let contents = "[package]\nname = \"devflow-core\"\nversion.workspace = true\n";
1507 assert_eq!(package_name(contents), Some("devflow-core".to_string()));
1508 }
1509
1510 #[test]
1511 fn member_depends_on_matches_dotted_workspace_shorthand() {
1512 let contents = "[package]\nname = \"devflow\"\n\n[dependencies]\ndevflow-core.workspace = true\nclap.workspace = true\n";
1513 assert!(member_depends_on(contents, "devflow-core"));
1514 assert!(!member_depends_on(contents, "serde"));
1515 }
1516
1517 #[test]
1523 fn member_depends_on_matches_long_form_dependency_section() {
1524 let contents = "[package]\nname = \"devflow\"\n\n[dependencies.devflow-core]\nworkspace = true\n\n[dependencies.clap]\nversion = \"4\"\n";
1525 assert!(member_depends_on(contents, "devflow-core"));
1526 assert!(member_depends_on(contents, "clap"));
1527 assert!(!member_depends_on(contents, "serde"));
1528 }
1529
1530 #[test]
1531 fn topo_sort_orders_dependency_before_dependent() {
1532 let names = vec!["devflow".to_string(), "devflow-core".to_string()];
1533 let edges = vec![("devflow".to_string(), "devflow-core".to_string())];
1534 assert_eq!(
1535 topo_sort(names, edges),
1536 vec!["devflow-core".to_string(), "devflow".to_string()]
1537 );
1538 }
1539
1540 #[test]
1541 fn topo_sort_falls_back_to_input_order_on_a_cycle() {
1542 let names = vec!["a".to_string(), "b".to_string()];
1545 let edges = vec![
1546 ("a".to_string(), "b".to_string()),
1547 ("b".to_string(), "a".to_string()),
1548 ];
1549 let result = topo_sort(names, edges);
1550 assert_eq!(result.len(), 2);
1551 }
1552
1553 #[test]
1554 fn publish_order_derives_core_before_cli_from_a_fixture_workspace() {
1555 let dir = tempfile::tempdir().unwrap();
1556 let root = dir.path();
1557 std::fs::write(
1558 root.join("Cargo.toml"),
1559 "[workspace]\nmembers = [\n \"crates/devflow-core\",\n \"crates/devflow-cli\",\n]\n",
1560 )
1561 .unwrap();
1562 std::fs::create_dir_all(root.join("crates/devflow-core")).unwrap();
1563 std::fs::write(
1564 root.join("crates/devflow-core/Cargo.toml"),
1565 "[package]\nname = \"devflow-core\"\n\n[dependencies]\n",
1566 )
1567 .unwrap();
1568 std::fs::create_dir_all(root.join("crates/devflow-cli")).unwrap();
1569 std::fs::write(
1570 root.join("crates/devflow-cli/Cargo.toml"),
1571 "[package]\nname = \"devflow\"\n\n[dependencies]\ndevflow-core.workspace = true\n",
1572 )
1573 .unwrap();
1574
1575 assert_eq!(
1576 publish_order(root),
1577 vec!["devflow-core".to_string(), "devflow".to_string()]
1578 );
1579 }
1580
1581 #[test]
1588 fn publish_order_recognizes_long_form_dependency_section_self_dependency() {
1589 let dir = tempfile::tempdir().unwrap();
1590 let root = dir.path();
1591 std::fs::write(
1592 root.join("Cargo.toml"),
1593 "[workspace]\nmembers = [\n \"crates/devflow-core\",\n \"crates/devflow-cli\",\n]\n",
1594 )
1595 .unwrap();
1596 std::fs::create_dir_all(root.join("crates/devflow-core")).unwrap();
1597 std::fs::write(
1598 root.join("crates/devflow-core/Cargo.toml"),
1599 "[package]\nname = \"devflow-core\"\n\n[dependencies]\n",
1600 )
1601 .unwrap();
1602 std::fs::create_dir_all(root.join("crates/devflow-cli")).unwrap();
1603 std::fs::write(
1604 root.join("crates/devflow-cli/Cargo.toml"),
1605 "[package]\nname = \"devflow\"\n\n[dependencies.devflow-core]\nworkspace = true\n",
1606 )
1607 .unwrap();
1608
1609 assert_eq!(
1610 publish_order(root),
1611 vec!["devflow-core".to_string(), "devflow".to_string()],
1612 "the long-form dependency section must still order devflow-core before devflow"
1613 );
1614 }
1615
1616 #[test]
1621 fn origin_main_ancestor_status_is_ref_absent_without_a_remote() {
1622 let repo = init_repo();
1623 let root = repo.path();
1624 assert_eq!(origin_main_ancestor_status(root), AncestorStatus::RefAbsent);
1625 }
1626
1627 #[test]
1628 fn origin_main_ancestor_status_is_ancestor_when_head_is_up_to_date() {
1629 let repo = init_repo();
1630 let root = repo.path();
1631 let head = crate::test_support::git_command(root)
1632 .args(["rev-parse", "HEAD"])
1633 .output()
1634 .unwrap();
1635 let head_sha = String::from_utf8_lossy(&head.stdout).trim().to_string();
1636 git(root, &["update-ref", "refs/remotes/origin/main", &head_sha]);
1637 assert_eq!(origin_main_ancestor_status(root), AncestorStatus::Ancestor);
1638 }
1639
1640 #[test]
1645 fn classify_ssh_add_status_maps_all_three_documented_exit_codes() {
1646 assert_eq!(classify_ssh_add_status(2), SigningStatus::NoAgent);
1647 assert_eq!(classify_ssh_add_status(1), SigningStatus::AgentEmpty);
1648 assert_eq!(classify_ssh_add_status(0), SigningStatus::KeysListed);
1649 assert_eq!(classify_ssh_add_status(7), SigningStatus::Unknown(7));
1650 }
1651
1652 static HOME_ENV_MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(());
1659
1660 #[test]
1661 fn check_signing_viability_degrades_when_gpg_format_unset_and_no_signingkey() {
1662 let _lock = HOME_ENV_MUTEX.lock().unwrap();
1665 let repo = init_repo();
1666 let root = repo.path();
1667 let fake_home = tempfile::tempdir().unwrap();
1668 let original_home = std::env::var_os("HOME");
1669 unsafe { std::env::set_var("HOME", fake_home.path()) };
1672
1673 let result = check_signing_viability(root);
1674
1675 match original_home {
1677 Some(home) => unsafe { std::env::set_var("HOME", home) },
1678 None => unsafe { std::env::remove_var("HOME") },
1679 }
1680
1681 match result {
1682 SigningViability::Unknown { reason } => {
1683 assert!(
1684 reason.contains("user.signingkey"),
1685 "unexpected reason: {reason}"
1686 );
1687 }
1688 other => panic!("expected Unknown (fail-soft), got: {other:?}"),
1689 }
1690 }
1691}