Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SoftwareVersion: Add git hash suffix with git-version crate #6

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ jobs:
use-cross: ${{ matrix.job.use-cross }}
command: run
args: --target ${{ matrix.job.target }} --example=simple -- "'test value' on command line" "two" "three"
- uses: actions-rs/cargo@v1
name: "example 'simple' without git_hash feature"
with:
use-cross: ${{ matrix.job.use-cross }}
command: run
args: --target ${{ matrix.job.target }} --example=simple --no-default-features --features collector_operating_system,format_markdown
- uses: actions-rs/cargo@v1
name: "example 'custom_collector'"
with:
Expand Down
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ edition = "2018"
version = "0.3.0"

[features]
default = ["collector_operating_system", "format_markdown"]
default = ["collector_operating_system", "git_hash", "format_markdown"]

collector_operating_system = ["sys-info"]

git_hash = ["git-version"]

format_markdown = []
format_plaintext = []

[dependencies]
sys-info = { version = "0.7", optional = true }
git-version = { version = "0.3", optional = true }
snailquote = "0.3"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ generates bug report information that [looks like this](example-report.md).

## Collectors

- [x] Crate information (name and version)
- [x] Crate information (name, version, git hash)
- [x] Operating system (type, name, version)
- [x] Command line (including all arguments)
- [x] Environment variables (e.g. `SHELL`, `PATH`, …)
Expand Down
2 changes: 1 addition & 1 deletion example-report.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#### Software version

bugreport 0.3.0
bugreport 0.3.0 (a984113)

#### Operating system

Expand Down
10 changes: 8 additions & 2 deletions src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,16 @@ impl Collector for SoftwareVersion {
}

fn collect(&mut self, crate_info: &CrateInfo) -> Result<ReportEntry> {
let git_hash_suffix = match crate_info.git_hash {
Some(git_hash) => format!(" ({})", git_hash),
None => format!(""),
};

Ok(ReportEntry::Text(format!(
"{} {}",
"{} {}{}",
crate_info.pkg_name,
self.version.as_deref().unwrap_or(&crate_info.pkg_version)
self.version.as_deref().unwrap_or(&crate_info.pkg_version),
git_hash_suffix,
)))
}
}
Expand Down
37 changes: 33 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub(crate) type Result<T> = result::Result<T, CollectionError>;
pub struct CrateInfo<'a> {
pkg_name: &'a str,
pkg_version: &'a str,
git_hash: Option<&'a str>,
}

/// The main struct for collecting bug report information.
Expand All @@ -50,11 +51,17 @@ impl<'a> BugReport<'a> {
info: CrateInfo {
pkg_name,
pkg_version,
git_hash: None,
},
collectors: vec![],
}
}

#[doc(hidden)]
pub fn set_git_hash(&mut self, git_hash: Option<&'a str>) {
self.info.git_hash = git_hash;
}

/// Add a [`Collector`] to the bug report.
pub fn info<C: Collector + 'static>(mut self, collector: C) -> Self {
self.collectors.push(Box::new(collector));
Expand Down Expand Up @@ -89,15 +96,37 @@ impl<'a> BugReport<'a> {
}
}

/// Re-export so dependent project does not have to manually depend on git-version crate
#[cfg(feature = "git_hash")]
pub use git_version::git_version;

#[cfg(feature = "git_hash")]
#[doc(hidden)]
#[macro_export]
macro_rules! bugreport_set_git_hash {
($br:ident) => {
$br.set_git_hash(Some(bugreport::git_version!(fallback = "<no git>")));
};
}

#[cfg(not(feature = "git_hash"))]
#[doc(hidden)]
#[macro_export]
macro_rules! bugreport_set_git_hash {
($br:ident) => {};
}

/// Generate a new [`BugReport`] object.
#[macro_export]
macro_rules! bugreport {
() => {
bugreport::BugReport::from_name_and_version(
() => {{
let mut br = bugreport::BugReport::from_name_and_version(
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION"),
)
};
);
bugreport::bugreport_set_git_hash!(br);
br
}};
}

#[cfg(test)]
Expand Down