Skip to content

Commit 4d110dd

Browse files
bors[bot]matklad
andauthored
Merge #8643
8643: fix: correct version string to contain hash, build date and channel r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents 43ea1bb + 8d54fd1 commit 4d110dd

File tree

2 files changed

+46
-24
lines changed

2 files changed

+46
-24
lines changed

crates/rust-analyzer/build.rs

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
//! Just embed git-hash to `--version`
1+
//! Construct version in the `commit-hash date chanel` format
22
33
use std::{env, path::PathBuf, process::Command};
44

55
fn main() {
66
set_rerun();
7-
8-
let rev =
9-
env::var("RUST_ANALYZER_REV").ok().or_else(rev).unwrap_or_else(|| "???????".to_string());
10-
println!("cargo:rustc-env=REV={}", rev)
7+
println!("cargo:rustc-env=REV={}", rev())
118
}
129

1310
fn set_rerun() {
@@ -18,29 +15,52 @@ fn set_rerun() {
1815
);
1916

2017
while manifest_dir.parent().is_some() {
21-
if manifest_dir.join(".git/HEAD").exists() {
22-
let git_dir = manifest_dir.join(".git");
23-
24-
println!("cargo:rerun-if-changed={}", git_dir.join("HEAD").display());
25-
// current branch ref
26-
if let Ok(output) =
27-
Command::new("git").args(&["rev-parse", "--symbolic-full-name", "HEAD"]).output()
28-
{
29-
if let Ok(ref_link) = String::from_utf8(output.stdout) {
30-
println!("cargo:rerun-if-changed={}", git_dir.join(ref_link).display());
31-
}
32-
}
18+
let head_ref = manifest_dir.join(".git/HEAD");
19+
if head_ref.exists() {
20+
println!("cargo:rerun-if-changed={}", head_ref.display());
3321
return;
3422
}
3523

3624
manifest_dir.pop();
3725
}
26+
3827
println!("cargo:warning=Could not find `.git/HEAD` from manifest dir!");
3928
}
4029

41-
fn rev() -> Option<String> {
42-
let output =
43-
Command::new("git").args(&["describe", "--tags", "--exclude", "nightly"]).output().ok()?;
30+
fn rev() -> String {
31+
if let Ok(rev) = env::var("RUST_ANALYZER_REV") {
32+
return rev;
33+
}
34+
35+
if let Some(commit_hash) = commit_hash() {
36+
let mut buf = commit_hash;
37+
38+
if let Some(date) = build_date() {
39+
buf.push(' ');
40+
buf.push_str(&date);
41+
}
42+
43+
let channel = env::var("RUST_ANALYZER_CHANNEL").unwrap_or_else(|_| "dev".to_string());
44+
buf.push(' ');
45+
buf.push_str(&channel);
46+
47+
return buf;
48+
}
49+
50+
"???????".to_string()
51+
}
52+
53+
fn commit_hash() -> Option<String> {
54+
output_to_string("git rev-parse --short HEAD")
55+
}
56+
57+
fn build_date() -> Option<String> {
58+
output_to_string("date --iso --utc")
59+
}
60+
61+
fn output_to_string(command: &str) -> Option<String> {
62+
let args = command.split_ascii_whitespace().collect::<Vec<_>>();
63+
let output = Command::new(args[0]).args(&args[1..]).output().ok()?;
4464
let stdout = String::from_utf8(output.stdout).ok()?;
45-
Some(stdout)
65+
Some(stdout.trim().to_string())
4666
}

xtask/src/dist.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77

88
use anyhow::Result;
99
use flate2::{write::GzEncoder, Compression};
10-
use xshell::{cmd, cp, mkdir_p, pushd, read_file, rm_rf, write_file};
10+
use xshell::{cmd, cp, mkdir_p, pushd, pushenv, read_file, rm_rf, write_file};
1111

1212
use crate::{date_iso, project_root};
1313

@@ -26,7 +26,8 @@ impl DistCmd {
2626
let release_tag = if self.nightly { "nightly".to_string() } else { date_iso()? };
2727
dist_client(&version, &release_tag)?;
2828
}
29-
dist_server()?;
29+
let release_channel = if self.nightly { "nightly" } else { "stable" };
30+
dist_server(release_channel)?;
3031
Ok(())
3132
}
3233
}
@@ -59,7 +60,8 @@ fn dist_client(version: &str, release_tag: &str) -> Result<()> {
5960
Ok(())
6061
}
6162

62-
fn dist_server() -> Result<()> {
63+
fn dist_server(release_channel: &str) -> Result<()> {
64+
let _e = pushenv("RUST_ANALYZER_CHANNEL", release_channel);
6365
let target = get_target();
6466
if target.contains("-linux-gnu") || target.contains("-linux-musl") {
6567
env::set_var("CC", "clang");

0 commit comments

Comments
 (0)