1mod branch;
2mod clone;
3mod local_branch;
4mod prune;
5mod remote;
6
7pub use branch::{branch_exists_remote, detect_default_branch};
8pub use clone::{bare_clone, git_fetch};
9pub use local_branch::{branch_exists_local, detect_local_default_branch};
10pub use prune::git_worktree_prune;
11pub use remote::get_remote_url;
12
13use anyhow::{bail, Context, Result};
14use std::path::Path;
15use std::process::Command;
16
17pub(super) fn git_cmd() -> Command {
24 let mut cmd = Command::new("git");
25 cmd.env_remove("GIT_DIR")
26 .env_remove("GIT_WORK_TREE")
27 .env_remove("GIT_INDEX_FILE");
28 cmd
29}
30
31pub fn create_local_worktree(
40 repo: &Path,
41 dest: &Path,
42 branch: &str,
43 branch_exists: bool,
44) -> Result<()> {
45 let mut cmd = git_cmd();
46 cmd.args(["-C"]).arg(repo).arg("worktree").arg("add");
47
48 if branch_exists {
49 cmd.arg(dest).arg(branch);
50 } else {
51 cmd.arg(dest).arg("-b").arg(branch);
53 }
54
55 let status = cmd.status().context("Failed to run `git worktree add`")?;
56
57 if !status.success() {
58 bail!("git worktree add failed for branch {branch}"); }
60 Ok(())
61}
62
63pub fn create_worktree(
72 bare: &Path,
73 dest: &Path,
74 branch: &str,
75 base_branch: &str,
76 branch_exists: bool,
77) -> Result<()> {
78 let mut cmd = git_cmd();
79 cmd.args(["-C"]).arg(bare).arg("worktree").arg("add");
80
81 if branch_exists {
82 cmd.arg(dest).arg(branch);
83 } else {
84 cmd.arg(dest)
85 .arg("-b")
86 .arg(branch)
87 .arg(format!("origin/{base_branch}"));
88 }
89
90 let status = cmd.status().context("Failed to run `git worktree add`")?;
91
92 if !status.success() {
93 bail!("git worktree add failed for branch {branch}"); }
95 Ok(())
96}