Skip to content

Commit

Permalink
x: check if scratch dir is git repo
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill authored and bors-libra committed Sep 9, 2021
1 parent ef131d5 commit 9e8117c
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions devtools/x-core/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,12 @@ impl GitCli {
let mut scratch_dir = self.root.join("target");
scratch_dir.extend(&["x-scratch", "tree"]);

if scratch_dir.is_dir() {
if scratch_dir.is_dir() && self.is_git_repo(&scratch_dir)? {
debug!(
"Using existing scratch worktree at {}",
scratch_dir.display()
);

// TODO: check if the directory is actually a Git worktree.

// Check out the given hash in the scratch worktree.
let output = self
.git_command()
Expand All @@ -213,6 +211,11 @@ impl GitCli {
});
}
} else {
if scratch_dir.is_dir() {
std::fs::remove_dir_all(&scratch_dir)
.map_err(|err| SystemError::io("cleaning old scratch_dir", err))?;
}

// Try creating a scratch worktree at that location.
info!("Setting up scratch worktree in {}", scratch_dir.display());
let output = self
Expand All @@ -234,6 +237,17 @@ impl GitCli {
// about it for now.
Ok(scratch_dir)
}

pub fn is_git_repo(&self, dir: &Path) -> Result<bool> {
let output = self
.git_command()
.current_dir(dir)
.args(&["rev-parse", "--git-dir"])
.output()
.map_err(|err| SystemError::io("checking if a directory is a git repo", err))?;

Ok(output.status.success())
}
}

/// A Git hash.
Expand Down

0 comments on commit 9e8117c

Please sign in to comment.