rustyhorde / vergen

Generate cargo instructions at compile time in build scripts for use with the env! or option_env! macros
Apache License 2.0
378 stars 56 forks source link

Add VERGEN_GIT_DIRTY #237

Closed bouk closed 8 months ago

bouk commented 1 year ago

Should have the value 'true' or 'false' depending on whether the git tree was dirty

kwinz commented 11 months ago

I just had the same need. I programmed this myself:

    let has_uncommited_uncached_changes = Command::new("git")
        .args(&["diff", "--quiet"])
        .status()
        .unwrap()
        .code()
        .unwrap()
        != 0;

    let has_uncommited_cached_changes = Command::new("git")
        .args(&["diff", "--cached", "--quiet"])
        .status()
        .unwrap()
        .code()
        .unwrap()
        != 0;

    println!(
        "cargo:rustc-env=GIT_DIRTY={}",
        if has_uncommited_uncached_changes || has_uncommited_cached_changes {
            "uncommited-changes"
        } else {
            "clean"
        }
    );

I called it "uncommited-changes" instead of "dirty" because I also wanted to include changes that are staged but not commited yet.

It works mostly, but in some rare cases cargo doesn't notice that the dirty status of some file changed and it needs to rebuild. Any idea how to fix this?

hgomersall commented 8 months ago

I've just submitted a merge request to address this in #281 .

CraZySacX commented 8 months ago

This has been added as of release 8.2.7 for the git2 and gitcl feature. gix support will be added when gix-status implementation is more complete.