From 047b36639db7d6ed2e8c124618f8070987c3e5a6 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Tue, 23 Jun 2020 21:43:55 -0700 Subject: [PATCH] Add `--no-git` flag to `gen all` The flag skips steps whichr require a git checkout, currently only the changelog generator. type: added --- bin/gen/src/changelog.rs | 6 ++++-- bin/gen/src/error.rs | 2 +- bin/gen/src/project.rs | 20 ++++++++------------ bin/gen/src/subcommand.rs | 21 ++++++++++++++++----- 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/bin/gen/src/changelog.rs b/bin/gen/src/changelog.rs index 1c9821c..fbd2cdc 100644 --- a/bin/gen/src/changelog.rs +++ b/bin/gen/src/changelog.rs @@ -7,7 +7,9 @@ pub(crate) struct Changelog { impl Changelog { #[throws] pub(crate) fn new(project: &Project) -> Self { - let mut current = project.repo.head()?.peel_to_commit()?; + let repo = project.repo()?; + + let mut current = repo.head()?.peel_to_commit()?; let mut entries = Vec::new(); @@ -24,7 +26,7 @@ impl Changelog { let manifest_bytes = current .tree()? .get_path("Cargo.toml".as_ref())? - .to_object(&project.repo)? + .to_object(&repo)? .as_blob() .unwrap() .content() diff --git a/bin/gen/src/error.rs b/bin/gen/src/error.rs index 1a676b9..73852cf 100644 --- a/bin/gen/src/error.rs +++ b/bin/gen/src/error.rs @@ -68,7 +68,7 @@ pub(crate) enum Error { Git { source: git2::Error }, #[snafu(display("Regex compilation error: {}", source))] Regex { source: regex::Error }, - #[snafu(display("Failed to find repository from `{}`: {}", start_dir.display(), source))] + #[snafu(display("Failed to find Git repository from `{}`: {}", start_dir.display(), source))] RepositoryDiscover { start_dir: PathBuf, source: git2::Error, diff --git a/bin/gen/src/project.rs b/bin/gen/src/project.rs index 1201b15..655b451 100644 --- a/bin/gen/src/project.rs +++ b/bin/gen/src/project.rs @@ -1,7 +1,6 @@ use crate::common::*; pub(crate) struct Project { - pub(crate) repo: Repository, pub(crate) root: PathBuf, pub(crate) config: Config, pub(crate) bin: Bin, @@ -11,16 +10,7 @@ pub(crate) struct Project { impl Project { #[throws] pub(crate) fn load(options: &Options) -> Self { - let start_dir = env::current_dir().context(error::CurrentDir)?; - - let repo = Repository::discover(&start_dir).context(error::RepositoryDiscover { start_dir })?; - - let root = repo - .workdir() - .ok_or_else(|| Error::Workdir { - repo: repo.path().to_owned(), - })? - .to_owned(); + let root = env::current_dir().context(error::CurrentDir)?; let config = Config::load(&root)?; @@ -49,7 +39,6 @@ impl Project { executable: options.bin.clone(), bin, config, - repo, root, } } @@ -64,4 +53,11 @@ impl Project { gen } + + #[throws] + pub(crate) fn repo(&self) -> Repository { + Repository::discover(&self.root).context(error::RepositoryDiscover { + start_dir: &self.root, + })? + } } diff --git a/bin/gen/src/subcommand.rs b/bin/gen/src/subcommand.rs index 29b9daa..2e5839b 100644 --- a/bin/gen/src/subcommand.rs +++ b/bin/gen/src/subcommand.rs @@ -3,7 +3,14 @@ use crate::common::*; #[derive(StructOpt)] pub(crate) enum Subcommand { #[structopt(about("Update all generated docs"))] - All, + All { + #[structopt( + long = "no-git", + help = "Skip generated outputs that require a Git repository. Currently this only includes \ + the changelog." + )] + no_git: bool, + }, #[structopt(about("Generate book"))] Book, #[structopt(about("Generate the changelog"))] @@ -73,13 +80,15 @@ impl Subcommand { Self::Book => Self::book(&project)?, Self::Man => Self::man(&project)?, Self::Diff => Self::diff(&project)?, - Self::All => Self::all(&project)?, + Self::All { no_git } => Self::all(&project, no_git)?, } } #[throws] - pub(crate) fn all(project: &Project) { - Self::changelog(&project)?; + pub(crate) fn all(project: &Project, no_git: bool) { + if !no_git { + Self::changelog(&project)?; + } Self::completion_scripts(&project)?; Self::readme(&project)?; Self::book(&project)?; @@ -131,7 +140,9 @@ impl Subcommand { gen(HEAD)?; - let head = project.repo.head()?; + let repo = project.repo()?; + + let head = repo.head()?; let head_commit = head.peel_to_commit()?;