Skip to content

Commit 4db943b

Browse files
committed
build: fall back to rev-parse HEAD when git describe fails
Apparently `cargo install` clones the git repo in a way that doesn't fetch tags, thus `git describe` can't do anything useful and we don't get a revision at all in the `rg --version` output. Work around this by falling back to `git rev-parse --short=10 HEAD` if `git describe` returns nothing, like upstream ripgrep.
1 parent 8e0f71f commit 4db943b

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

build.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,30 @@ fn main() {
5252
}
5353
}
5454

55+
fn command_output(cmd: &[&str]) -> Option<String> {
56+
process::Command::new(cmd[0]).args(&cmd[1..]).output().ok().and_then(
57+
|output| {
58+
let v = String::from_utf8_lossy(&output.stdout).trim().to_string();
59+
if v.is_empty() {
60+
None
61+
} else {
62+
Some(v)
63+
}
64+
},
65+
)
66+
}
67+
5568
fn git_revision_hash() -> Option<String> {
56-
let result = process::Command::new("git")
57-
.args(&["describe", "--tags", "--match=[0-9]*", "--dirty=+"])
58-
.output();
59-
result.ok().and_then(|output| {
60-
let v = String::from_utf8_lossy(&output.stdout).trim().to_string();
61-
if v.is_empty() {
62-
None
63-
} else {
64-
Some(v)
65-
}
66-
})
69+
// try to use git tags and revision count when possible, but 'cargo install' clones
70+
// the git repo without fetching tags so fall back to just a rev hash if needed.
71+
command_output(&[
72+
"git",
73+
"describe",
74+
"--tags",
75+
"--match=[0-9]*",
76+
"--dirty=+",
77+
])
78+
.or_else(|| command_output(&["git", "rev-parse", "--short=10", "HEAD"]))
6779
}
6880

6981
fn generate_man_page<P: AsRef<Path>>(outdir: P) -> io::Result<()> {

0 commit comments

Comments
 (0)