hrkfdn / ncspot

Cross-platform ncurses Spotify client written in Rust, inspired by ncmpc and the likes.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Include Git Commit Hash in Binary

ThomasFrans opened this issue · comments

Is your feature request related to a problem? Please describe.
I encountered a bug today that caused ncspot to freeze and ignore any signals (SIGTERM, 'q'...). I wanted to create a bug report for it but it's not reproducible and since I installed with cargo install --git https://github.com/hrkfdn/ncspot ncspot, I don't know the exact version I'm using.

Describe the solution you'd like
Since it makes sense for developers to dogfood the latest release, it would be handy to include the Git commit hash so the exact commit can be added to bug reports.

I looked online and found there are crates which let you do this. Even simpler would be to take the approach Alacritty takes. It gets the commit hash in the build.rs and makes it available to the actual build.

fn main() {
    let mut version = String::from(env!("CARGO_PKG_VERSION"));
    if let Some(commit_hash) = commit_hash() {
        version = format!("{version} ({commit_hash})");
    }
    println!("cargo:rustc-env=VERSION={version}");

    ...
}

fn commit_hash() -> Option<String> {
    Command::new("git")
        .args(["rev-parse", "--short", "HEAD"])
        .output()
        .ok()
        .filter(|output| output.status.success())
        .and_then(|output| String::from_utf8(output.stdout).ok())
        .map(|hash| hash.trim().into())
}

The Alacritty approach looks good