Skip to content

Commit 6ecfb96

Browse files
More git info
1 parent ec3a178 commit 6ecfb96

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

xpd-common/build.rs

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,25 @@ use std::{
44
};
55

66
fn main() {
7-
let commit_msg = match get_sha() {
7+
let commit_sha = match get_sha() {
88
Ok(v) => v,
99
Err(err) => {
1010
println!("cargo::warning={err:?}");
11-
"(Failed to get version)".to_owned()
11+
"(Failed to get git commit sha)".to_owned()
1212
}
1313
};
1414

15-
println!("cargo::rustc-env=GIT_HASH_EXPERIENCED={}", commit_msg);
15+
let rev_num = match get_rev() {
16+
Ok(v) => v,
17+
Err(err) => {
18+
println!("cargo::warning={err:?}");
19+
"(Failed to get revision number)".to_owned()
20+
}
21+
};
22+
23+
println!("cargo::rustc-env=GIT_HASH_EXPERIENCED={}", commit_sha);
24+
println!("cargo::rustc-env=GIT_REV_COUNT_EXPERIENCED={}", rev_num)
25+
1626
}
1727

1828
fn get_sha() -> Result<String, Error> {
@@ -34,24 +44,43 @@ fn get_sha() -> Result<String, Error> {
3444
Ok(output)
3545
}
3646

47+
fn get_rev() -> Result<String, Error> {
48+
let output = Command::new("git")
49+
.arg("rev-list")
50+
.arg("--count")
51+
.arg("HEAD")
52+
.stdout(Stdio::piped())
53+
.stderr(Stdio::piped())
54+
.stdin(Stdio::null())
55+
.spawn()?
56+
.wait_with_output()?;
57+
if !output.status.success() {
58+
return Err(Error::BadStatus(output.status));
59+
}
60+
let output = String::from_utf8(output.stdout)?.trim().to_string();
61+
Ok(output)
62+
}
63+
3764
#[derive(Debug)]
3865
enum Error {
3966
TryFromString,
4067
BadStatus(ExitStatus),
4168
Io(std::io::Error),
69+
ParseInt(std::num::ParseIntError),
4270
NoOutput,
4371
}
4472

4573
impl std::fmt::Display for Error {
4674
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4775
match self {
48-
Self::TryFromString => write!(f, "Invalid UTF-8 in `git rev-parse HEAD` output")?,
76+
Self::TryFromString => write!(f, "Invalid UTF-8 in `git` output")?,
4977
Self::BadStatus(exit_status) => write!(
5078
f,
51-
"`git rev-parse HEAD` exited with non-zero status {exit_status}"
79+
"`git` exited with non-zero status {exit_status}"
5280
)?,
53-
Self::Io(error) => write!(f, "I/O error trying to run `git rev-parse HEAD`: {error}")?,
54-
Self::NoOutput => write!(f, "No output from git-rev-parse")?,
81+
Self::Io(error) => write!(f, "I/O error trying to run `git`: {error}")?,
82+
Self::NoOutput => write!(f, "No output from `git`")?,
83+
Self::ParseInt(error) => write!(f, "Could not convert to int: {error}")?
5584
}
5685
Ok(())
5786
}
@@ -74,3 +103,10 @@ impl From<std::io::Error> for Error {
74103
Self::Io(value)
75104
}
76105
}
106+
107+
108+
impl From<std::num::ParseIntError> for Error {
109+
fn from(value: std::num::ParseIntError) -> Self {
110+
Self::ParseInt(value)
111+
}
112+
}

xpd-common/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use twilight_model::{
2020
};
2121

2222
pub const CURRENT_GIT_SHA: &str = env!("GIT_HASH_EXPERIENCED");
23+
pub const CURRENT_GIT_REV_COUNT: &str = env!("GIT_REV_COUNT_EXPERIENCED");
2324
pub const DISCORD_EPOCH_MS: i64 = 1_420_070_400_000;
2425
pub const DISCORD_EPOCH_SECS: i64 = DISCORD_EPOCH_MS / 1000;
2526

xpd-slash/src/admin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use twilight_model::{
88
},
99
};
1010
use twilight_util::builder::embed::EmbedBuilder;
11-
use xpd_common::{CURRENT_GIT_SHA, DEFAULT_MESSAGE_COOLDOWN, DISCORD_EPOCH_SECS};
11+
use xpd_common::{CURRENT_GIT_SHA, DEFAULT_MESSAGE_COOLDOWN, DISCORD_EPOCH_SECS, CURRENT_GIT_REV_COUNT};
1212
use xpd_slash_defs::admin::{
1313
self, AdminCommand, AdminCommandBanGuild, AdminCommandGuildStats, AdminCommandInspectCooldown,
1414
AdminCommandLeave, AdminCommandPardonGuild, AdminCommandResetGuild, AdminCommandResetUser,
@@ -123,7 +123,7 @@ fn fmt_opt_u64(item: Option<u64>) -> impl Display {
123123
async fn get_bot_stats(state: SlashState) -> Result<String, Error> {
124124
let levels_held = xpd_database::total_levels(&state.db).await?;
125125
Ok(format!(
126-
"Roughly {levels_held} levels in database. Bot version `git-{CURRENT_GIT_SHA}`"
126+
"Roughly {levels_held} levels in database. Bot git revision `{CURRENT_GIT_SHA}`, commit number `{CURRENT_GIT_REV_COUNT}`"
127127
))
128128
}
129129

0 commit comments

Comments
 (0)