Skip to content

Commit

Permalink
fix(affected): no longer error if main does not exist (#9061)
Browse files Browse the repository at this point in the history
### Description

#9045 changed the error type of
`changed_files` as we could now exit with `Error::UnableToResolveRef`
instead of just returning the raw `Error::Git`. This resulted in us
failing to catch the error type. We keep the previous catch as there are
still scenarios where we'll be able to resolve a ref, but the checkout
will not include the merge base.

Existing integration test did not capture this as it our mocked shallow
clone leaves the branch `main` intact, just missing the commit that
would be the merge base.

### Testing Instructions

Added unit test
  • Loading branch information
chris-olszewski authored Aug 26, 2024
1 parent 1b9f0ea commit 473fb53
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions crates/turborepo-scm/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use turbopath::{

use crate::{Error, Git, SCM};

#[derive(Debug)]
pub enum ChangedFiles {
All,
Some(HashSet<AnchoredSystemPathBuf>),
Expand Down Expand Up @@ -35,18 +36,24 @@ impl SCM {
include_uncommitted: bool,
allow_unknown_objects: bool,
) -> Result<ChangedFiles, Error> {
fn unable_to_detect_range(error: impl std::error::Error) -> Result<ChangedFiles, Error> {
warn!(
"unable to detect git range, assuming all files have changed: {}",
error
);
Ok(ChangedFiles::All)
}
match self {
Self::Git(git) => {
match git.changed_files(turbo_root, from_commit, to_commit, include_uncommitted) {
Ok(files) => Ok(ChangedFiles::Some(files)),
Err(ref error @ Error::Git(ref message, _))
if allow_unknown_objects && message.contains("no merge base") =>
{
warn!(
"unable to detect git range, assuming all files have changed: {}",
error
);
Ok(ChangedFiles::All)
unable_to_detect_range(error)
}
Err(Error::UnableToResolveRef) => {
unable_to_detect_range(Error::UnableToResolveRef)
}
Err(e) => Err(e),
}
Expand Down Expand Up @@ -825,4 +832,28 @@ mod tests {

Ok(())
}

#[test]
fn test_changed_files_no_base() -> Result<(), Error> {
let mut repo_opts = RepositoryInitOptions::new();

let repo_init = repo_opts.initial_head("my-main");
let (repo_root, repo) = setup_repository(Some(repo_init))?;
let root = AbsoluteSystemPathBuf::try_from(repo_root.path()).unwrap();

// WARNING:
// if you do not make a commit, git will show you that you have no branches.
let file = root.join_component("todo.txt");
file.create_with_contents("1. explain why async Rust is good")?;
let _first_commit = commit_file(&repo, Path::new("todo.txt"), None);

let scm = SCM::new(&root);
let actual = scm
.changed_files(&root, None, Some("HEAD"), true, true)
.unwrap();

assert_matches!(actual, ChangedFiles::All);

Ok(())
}
}

0 comments on commit 473fb53

Please sign in to comment.