Skip to content

Commit fc748ce

Browse files
committed
Invoke Git a single time to retrieve commit info
1 parent 93d3d22 commit fc748ce

File tree

1 file changed

+18
-41
lines changed

1 file changed

+18
-41
lines changed

preproc/src/git.rs

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,72 +13,49 @@ use anyhow::Error;
1313

1414
#[derive(Debug)]
1515
pub struct Commit {
16-
hash: String,
17-
short_hash: String,
18-
timestamp: String,
16+
info: String,
17+
splits: [usize; 2],
1918
}
2019

2120
impl Commit {
2221
pub fn rev_parse(what: &str) -> Result<Self, Error> {
2322
let output = Command::new("git")
24-
.args(["rev-parse", what])
23+
.args(["show", "-s", "--format=%H%x00%h%x00%ci", what])
2524
.stderr(Stdio::inherit())
2625
.stdin(Stdio::null())
2726
.output()
28-
.expect("Failed to get commit hash");
27+
.expect("Failed to get commit info");
2928
if !output.status.success() {
3029
return Err(Error::msg(format!(
31-
"Git exited with {} while getting commit hash",
30+
"Git exited with status {} while getting commit info",
3231
output.status
3332
)));
3433
}
35-
let hash = String::from_utf8(output.stdout).expect("Commit hash is not valid UTF-8??");
34+
let info = String::from_utf8(output.stdout).expect("Commit info is not valid UTF-8??");
3635

37-
let output = Command::new("git")
38-
.args(["rev-parse", "--short", what])
39-
.stderr(Stdio::inherit())
40-
.stdin(Stdio::null())
41-
.output()
42-
.expect("Failed to get short commit hash");
43-
if !output.status.success() {
44-
return Err(Error::msg(format!(
45-
"Git exited with status {} while getting commit short hash",
46-
output.status
47-
)));
48-
}
49-
let short_hash =
50-
String::from_utf8(output.stdout).expect("Commit hash is not valid UTF-8??");
51-
52-
let output = Command::new("git")
53-
.args(["show", "-s", "--format=%ci", what])
54-
.stderr(Stdio::inherit())
55-
.stdin(Stdio::null())
56-
.output()
57-
.expect("Failed to get timestamp");
58-
if !output.status.success() {
59-
return Err(Error::msg(format!(
60-
"Git exited with status {} while getting timestamp",
61-
output.status
62-
)));
63-
}
64-
let timestamp = String::from_utf8(output.stdout).expect("Commit hash is not valid UTF-8??");
36+
let first_split = info
37+
.find('\0')
38+
.expect("Failed to split hash and short hash");
39+
let second_split = info[first_split + 1..]
40+
.find('\0')
41+
.expect("Failed to split short hash and timestamp")
42+
+ first_split;
6543

6644
Ok(Self {
67-
hash,
68-
short_hash,
69-
timestamp,
45+
info,
46+
splits: [first_split, second_split],
7047
})
7148
}
7249

7350
pub fn hash(&self) -> &str {
74-
self.hash.trim()
51+
&self.info[..self.splits[0]]
7552
}
7653

7754
pub fn short_hash(&self) -> &str {
78-
self.short_hash.trim()
55+
&self.info[self.splits[0] + 1..self.splits[1]]
7956
}
8057

8158
pub fn timestamp(&self) -> &str {
82-
self.timestamp.trim()
59+
&self.info[self.splits[1] + 1..]
8360
}
8461
}

0 commit comments

Comments
 (0)