Distinguish between "nothing to pull" and "pull error" in josh-sync

This commit is contained in:
Jakub Beránek 2025-01-30 16:24:39 +01:00
parent f61d56ba4d
commit 88197e4182
2 changed files with 32 additions and 7 deletions

View File

@ -1,5 +1,5 @@
use clap::Parser;
use crate::sync::GitSync;
use crate::sync::{GitSync, RustcPullError};
mod sync;
@ -22,7 +22,18 @@ fn main() -> anyhow::Result<()> {
let sync = GitSync::from_current_dir()?;
match args {
Args::RustcPull => {
sync.rustc_pull(None)?;
if let Err(error) = sync.rustc_pull(None) {
match error {
RustcPullError::NothingToPull => {
eprintln!("Nothing to pull");
std::process::exit(2);
}
RustcPullError::PullFailed(error) => {
eprintln!("Pull failure: {error:?}");
std::process::exit(1);
}
}
}
}
Args::RustcPush { github_username, branch } => {
sync.rustc_push(github_username, branch)?;

View File

@ -11,6 +11,19 @@ const JOSH_FILTER: &str = ":/src/doc/rustc-dev-guide";
const JOSH_PORT: u16 = 42042;
const UPSTREAM_REPO: &str = "rust-lang/rust";
pub enum RustcPullError {
/// No changes are available to be pulled.
NothingToPull,
/// A rustc-pull has failed, probably a git operation error has occurred.
PullFailed(anyhow::Error)
}
impl<E> From<E> for RustcPullError where E: Into<anyhow::Error> {
fn from(error: E) -> Self {
Self::PullFailed(error.into())
}
}
pub struct GitSync {
dir: PathBuf,
}
@ -24,7 +37,7 @@ impl GitSync {
})
}
pub fn rustc_pull(&self, commit: Option<String>) -> anyhow::Result<()> {
pub fn rustc_pull(&self, commit: Option<String>) -> Result<(), RustcPullError> {
let sh = Shell::new()?;
sh.change_dir(&self.dir);
let commit = commit.map(Ok).unwrap_or_else(|| {
@ -38,7 +51,7 @@ impl GitSync {
})?;
// Make sure the repo is clean.
if cmd!(sh, "git status --untracked-files=no --porcelain").read()?.is_empty().not() {
bail!("working directory must be clean before performing rustc pull");
return Err(anyhow::anyhow!("working directory must be clean before performing rustc pull").into());
}
// Make sure josh is running.
let josh = Self::start_josh()?;
@ -47,7 +60,7 @@ impl GitSync {
let previous_base_commit = sh.read_file("rust-version")?.trim().to_string();
if previous_base_commit == commit {
return Err(anyhow::anyhow!("No changes since last pull"));
return Err(RustcPullError::NothingToPull);
}
// Update rust-version file. As a separate commit, since making it part of
@ -94,12 +107,13 @@ impl GitSync {
cmd!(sh, "git reset --hard HEAD^")
.run()
.expect("FAILED to clean up after creating the preparation commit");
return Err(anyhow::anyhow!("No merge was performed, nothing to pull. Rolled back the preparation commit."));
eprintln!("No merge was performed, no changes to pull were found. Rolled back the preparation commit.");
return Err(RustcPullError::NothingToPull);
}
// Check that the number of roots did not increase.
if num_roots()? != num_roots_before {
bail!("Josh created a new root commit. This is probably not the history you want.");
return Err(anyhow::anyhow!("Josh created a new root commit. This is probably not the history you want.").into());
}
drop(josh);