From ae369146bec73045506171f0edd9e31cb27c62f4 Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Mon, 2 Dec 2024 19:42:30 +0530 Subject: [PATCH] fix(`forge`): run git cmd in correct dir --- crates/cli/src/utils/mod.rs | 10 ++++++++-- crates/forge/bin/cmd/install.rs | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/crates/cli/src/utils/mod.rs b/crates/cli/src/utils/mod.rs index 6a2afe657d87..6bf8c5b5d162 100644 --- a/crates/cli/src/utils/mod.rs +++ b/crates/cli/src/utils/mod.rs @@ -444,8 +444,8 @@ impl<'a> Git<'a> { self.cmd().args(["status", "--porcelain"]).exec().map(|out| out.stdout.is_empty()) } - pub fn has_branch(self, branch: impl AsRef) -> Result { - self.cmd() + pub fn has_branch(self, branch: impl AsRef, at: &Path) -> Result { + self.cmd_at(at) .args(["branch", "--list", "--no-color"]) .arg(branch) .get_stdout_lossy() @@ -567,6 +567,12 @@ ignore them in the `.gitignore` file, or run this command again with the `--no-c cmd } + pub fn cmd_at(self, path: &Path) -> Command { + let mut cmd = Self::cmd_no_root(); + cmd.current_dir(path); + cmd + } + pub fn cmd_no_root() -> Command { let mut cmd = Command::new("git"); cmd.stdout(Stdio::piped()).stderr(Stdio::piped()); diff --git a/crates/forge/bin/cmd/install.rs b/crates/forge/bin/cmd/install.rs index 2024b3d23fef..3543caeea51b 100644 --- a/crates/forge/bin/cmd/install.rs +++ b/crates/forge/bin/cmd/install.rs @@ -164,7 +164,7 @@ impl DependencyInstallOpts { // Pin branch to submodule if branch is used if let Some(branch) = &installed_tag { // First, check if this tag has a branch - if git.has_branch(branch)? { + if git.has_branch(branch, &path)? { // always work with relative paths when directly modifying submodules git.cmd() .args(["submodule", "set-branch", "-b", branch])