Skip to content

Commit

Permalink
feat(cli): Use a local git rev as a baseline
Browse files Browse the repository at this point in the history
Fixes #51
  • Loading branch information
epage committed Aug 18, 2022
1 parent 08bf535 commit 6703d44
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 0 deletions.
143 changes: 143 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ clap-cargo = { version = "0.9.1", features = ["cargo_metadata"] }
ignore = "0.4.18"
clap-verbosity-flag = "1.0.1"
log = "0.4.17"
git2 = { version = "0.15.0", default-features = false }
41 changes: 41 additions & 0 deletions src/baseline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,44 @@ impl BaselineLoader for PathBaseline {
Ok(rustdoc_path)
}
}

pub(crate) struct GitBaseline {
path: PathBaseline,
}

impl GitBaseline {
pub fn with_rev(
source: &std::path::Path,
target: &std::path::Path,
rev: &str,
config: &mut GlobalConfig,
) -> anyhow::Result<Self> {
config.shell_status("Cloning", rev)?;
let repo = git2::Repository::discover(source)?;

let rev = repo.revparse_single(rev)?;

std::fs::create_dir_all(target)?;
let mut co = git2::build::CheckoutBuilder::new();
co.target_dir(target)
.remove_untracked(true)
.remove_ignored(true)
.use_ours(true)
.force();
repo.checkout_tree(&rev, Some(&mut co))?;

let path = PathBaseline::new(target)?;
Ok(Self { path })
}
}

impl BaselineLoader for GitBaseline {
fn load_rustdoc(
&self,
config: &mut GlobalConfig,
rustdoc: &RustDocCommand,
name: &str,
) -> anyhow::Result<std::path::PathBuf> {
self.path.load_rustdoc(config, rustdoc, name)
}
}
26 changes: 26 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ fn main() -> anyhow::Result<()> {
Box::new(baseline::RustdocBaseline::new(path.to_owned()))
} else if let Some(root) = args.baseline_root.as_deref() {
Box::new(baseline::PathBaseline::new(root)?)
} else if let Some(rev) = args.baseline_rev.as_deref() {
let metadata = args.manifest.metadata().no_deps().exec()?;
let source = metadata.workspace_root.as_std_path();
let slug = rev
.chars()
.filter(|c| c.is_alphanumeric())
.collect::<String>();
let target = metadata
.target_directory
.as_std_path()
.join(format!("semver-checks/git-{}", slug));
Box::new(baseline::GitBaseline::with_rev(
source,
&target,
rev,
&mut config,
)?)
} else {
unreachable!("a member of the `baseline` group must be present");
};
Expand Down Expand Up @@ -134,6 +151,15 @@ struct CheckRelease {
)]
baseline_root: Option<PathBuf>,

/// Git revision to lookup for a baseline
#[clap(
long,
value_name = "REV",
help_heading = "BASELINE",
group = "baseline"
)]
baseline_rev: Option<String>,

/// The rustdoc json file to use as a semver baseline.
#[clap(
long,
Expand Down

0 comments on commit 6703d44

Please sign in to comment.