Skip to content

Commit d587c8a

Browse files
authored
Merge pull request #1701 from kinnison/kinnison/better-git-version
Switch to git-testament rather than old build.rs
2 parents c954c57 + 3772516 commit d587c8a

File tree

4 files changed

+58
-55
lines changed

4 files changed

+58
-55
lines changed

Cargo.lock

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ wait-timeout = "0.2"
4747
walkdir = "2"
4848
xz2 = "0.1.3"
4949
openssl = { version = "0.10", optional = true }
50+
git-testament = "0.1"
51+
lazy_static = "1"
5052

5153
[target."cfg(windows)".dependencies]
5254
winapi = { version = "0.3", features = ["combaseapi", "errhandlingapi", "fileapi", "handleapi", "ioapiset", "jobapi", "jobapi2", "minwindef", "processthreadsapi", "psapi", "shlobj", "shtypes", "synchapi", "sysinfoapi", "tlhelp32", "userenv", "winbase", "winerror", "winioctl", "winnt", "winuser"] }

build.rs

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,6 @@
11
use std::env;
2-
use std::error::Error;
3-
use std::fs::File;
4-
use std::io::Write;
5-
use std::path::PathBuf;
6-
use std::process::Command;
7-
8-
struct Ignore;
9-
10-
impl<E> From<E> for Ignore
11-
where
12-
E: Error,
13-
{
14-
fn from(_: E) -> Ignore {
15-
Ignore
16-
}
17-
}
182

193
fn main() {
204
let target = env::var("TARGET").unwrap();
215
println!("cargo:rustc-env=TARGET={}", target);
22-
23-
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
24-
File::create(out_dir.join("commit-info.txt"))
25-
.unwrap()
26-
.write_all(commit_info().as_bytes())
27-
.unwrap();
28-
29-
println!("cargo:rerun-if-changed=build.rs");
30-
}
31-
32-
// Try to get hash and date of the last commit on a best effort basis. If anything goes wrong
33-
// (git not installed or if this is not a git repository) just return an empty string.
34-
fn commit_info() -> String {
35-
match (commit_hash(), commit_date()) {
36-
(Ok(hash), Ok(date)) => format!(" ({} {})", hash.trim_end(), date),
37-
_ => String::new(),
38-
}
39-
}
40-
41-
fn commit_hash() -> Result<String, Ignore> {
42-
Ok(String::from_utf8(
43-
Command::new("git")
44-
.args(&["rev-parse", "--short=9", "HEAD"])
45-
.output()?
46-
.stdout,
47-
)?)
48-
}
49-
50-
fn commit_date() -> Result<String, Ignore> {
51-
Ok(String::from_utf8(
52-
Command::new("git")
53-
.args(&["log", "-1", "--date=short", "--pretty=format:%cd"])
54-
.output()?
55-
.stdout,
56-
)?)
576
}

src/cli/common.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use crate::errors::*;
44
use crate::self_update;
55
use crate::term2;
6+
use git_testament::{git_testament, render_testament};
7+
use lazy_static::lazy_static;
68
use rustup::utils::notify::NotificationLevel;
79
use rustup::utils::utils;
810
use rustup::{Cfg, Notification, Toolchain, UpdateStatus};
@@ -402,11 +404,13 @@ pub fn list_overrides(cfg: &Cfg) -> Result<()> {
402404
Ok(())
403405
}
404406

407+
git_testament!(TESTAMENT);
408+
405409
pub fn version() -> &'static str {
406-
concat!(
407-
env!("CARGO_PKG_VERSION"),
408-
include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt"))
409-
)
410+
lazy_static! {
411+
static ref RENDERED: String = render_testament!(TESTAMENT);
412+
}
413+
&RENDERED
410414
}
411415

412416
pub fn report_error(e: &Error) {

0 commit comments

Comments
 (0)