diff --git a/src/bin/rustc_josh_sync.rs b/src/bin/rustc_josh_sync.rs index 72b62dd..ff3794a 100644 --- a/src/bin/rustc_josh_sync.rs +++ b/src/bin/rustc_josh_sync.rs @@ -42,8 +42,10 @@ enum Command { #[clap(long)] upstream_commit: Option, - /// By default, the `pull` command will exit with status code 2 if there is nothing to pull. - /// If you instead want to exit successfully in that case, pass this flag. + /// By default, the `pull` command will exit with status code 2 if there is nothing to pull, + /// and reset git to the original state. + /// If you instead want to exit successfully and keep the intermediate changes + /// in that case, pass this flag. #[clap(long)] allow_noop: bool, @@ -110,7 +112,7 @@ fn main() -> anyhow::Result<()> { let ctx = load_context(&config_path, &rust_version_path)?; let josh = get_josh_proxy(verbose)?; let sync = GitSync::new(ctx.clone(), josh, verbose); - match sync.rustc_pull(upstream_repo, upstream_commit) { + match sync.rustc_pull(upstream_repo, upstream_commit, allow_noop) { Ok(result) => { if !maybe_create_gh_pr( &ctx.config.full_repo_name(), diff --git a/src/sync.rs b/src/sync.rs index 920c75d..bb37982 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -45,6 +45,7 @@ impl GitSync { &self, upstream_repo: String, upstream_commit: Option, + allow_noop: bool, ) -> Result { // The upstream commit that we want to pull let upstream_sha = if let Some(sha) = upstream_commit { @@ -142,8 +143,13 @@ This updates the rust-version file to {upstream_sha}."#, ) .context("cannot create preparation commit")?; - // Make sure that we reset the above commit if something fails - let mut git_reset = GitResetOnDrop::new(orig_head, self.verbose); + // Make sure that we reset either to the default HEAD, or the preparation commit, if + // something bad happens + let mut git_reset = if allow_noop { + GitResetOnDrop::new(get_current_head_sha(self.verbose)?, self.verbose) + } else { + GitResetOnDrop::new(orig_head, self.verbose) + }; // Fetch given rustc commit. run_command(&["git", "fetch", &josh_url], self.verbose) @@ -215,9 +221,7 @@ After you fix the conflicts, `git add` the changes and run `git merge --continue // This is the easy case, no merge was performed, so we bail if current_sha == sha_pre_merge { - eprintln!( - "No merge was performed, no changes to pull were found. Rolling back the preparation commit." - ); + eprintln!("No merge was performed, no changes to pull were found. Rolling back."); return Err(RustcPullError::NothingToPull); } @@ -225,7 +229,7 @@ After you fix the conflicts, `git add` the changes and run `git merge --continue // rustc, so a merge was created, but the in-tree diff can still be empty. // In that case we also bail. if self.has_empty_diff(&sha_pre_merge) { - eprintln!("Only empty changes were pulled. Rolling back the preparation commit."); + eprintln!("Only empty changes were pulled. Rolling back."); return Err(RustcPullError::NothingToPull); }