Skip to content

Commit e95c23f

Browse files
authored
Merge pull request RustPython#1434 from ChJR/feature/sys._git
Add sys._git (RustPython#1358)
2 parents ca24ca2 + ca48e64 commit e95c23f

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

vm/build.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@ fn main() {
66
"cargo:rustc-env=RUSTPYTHON_GIT_TIMESTAMP={}",
77
git_timestamp()
88
);
9+
println!("cargo:rustc-env=RUSTPYTHON_GIT_TAG={}", git_tag());
910
println!("cargo:rustc-env=RUSTPYTHON_GIT_BRANCH={}", git_branch());
1011
}
1112

1213
fn git_hash() -> String {
13-
git(&["rev-parse", "HEAD"])
14+
git(&["rev-parse", "--short", "HEAD"])
1415
}
1516

1617
fn git_timestamp() -> String {
1718
git(&["log", "-1", "--format=%cd"])
1819
}
1920

21+
fn git_tag() -> String {
22+
git(&["describe", "--all", "--always", "--dirty"])
23+
}
24+
2025
fn git_branch() -> String {
2126
git(&["rev-parse", "--abbrev-ref", "HEAD"])
2227
}

vm/src/sysmodule.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ fn sys_exc_info(vm: &VirtualMachine) -> PyResult {
163163
}))
164164
}
165165

166+
fn sys_git_info(vm: &VirtualMachine) -> PyObjectRef {
167+
vm.ctx.new_tuple(vec![
168+
vm.ctx.new_str("RustPython".to_string()),
169+
vm.ctx.new_str(version::get_git_identifier()),
170+
vm.ctx.new_str(version::get_git_revision()),
171+
])
172+
}
173+
166174
// TODO: raise a SystemExit here
167175
fn sys_exit(code: OptionalArg<i32>, _vm: &VirtualMachine) -> PyResult<()> {
168176
let code = code.unwrap_or(0);
@@ -360,6 +368,7 @@ settrace() -- set the global debug tracing function
360368
"settrace" => ctx.new_rustfunc(sys_settrace),
361369
"version" => vm.new_str(version::get_version()),
362370
"version_info" => version_info,
371+
"_git" => sys_git_info(vm),
363372
"exc_info" => ctx.new_rustfunc(sys_exc_info),
364373
"prefix" => ctx.new_str(prefix.to_string()),
365374
"base_prefix" => ctx.new_str(base_prefix.to_string()),

vm/src/version.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,23 @@ pub fn get_git_revision() -> String {
3838
option_env!("RUSTPYTHON_GIT_HASH").unwrap_or("").to_string()
3939
}
4040

41+
pub fn get_git_tag() -> String {
42+
option_env!("RUSTPYTHON_GIT_TAG").unwrap_or("").to_string()
43+
}
44+
4145
pub fn get_git_branch() -> String {
4246
option_env!("RUSTPYTHON_GIT_BRANCH")
4347
.unwrap_or("")
4448
.to_string()
4549
}
50+
51+
pub fn get_git_identifier() -> String {
52+
let git_tag = get_git_tag();
53+
let git_branch = get_git_branch();
54+
55+
if git_tag.is_empty() || git_tag == "undefined" {
56+
git_branch
57+
} else {
58+
git_tag
59+
}
60+
}

0 commit comments

Comments
 (0)