-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not include git commit timestamp in notary server + use git2 inste…
…ad of git command + add dirty suffix (#643) build: improved commit info on notary/info page This changes uses git2 Rust library instead of calling out to external git The timestamp was removed
- Loading branch information
Showing
7 changed files
with
58 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,55 @@ | ||
use std::{env, process::Command}; | ||
use chrono::DateTime; | ||
use git2::{Commit, Repository, StatusOptions}; | ||
use std::{env, error::Error}; | ||
|
||
fn main() { | ||
if env::var("GIT_COMMIT_HASH").is_err() || env::var("GIT_COMMIT_TIMESTAMP").is_err() { | ||
// Used to extract latest HEAD commit hash and timestamp for the /info endpoint | ||
let output = Command::new("git") | ||
.args(["show", "HEAD", "-s", "--format=%H,%cI"]) | ||
.output() | ||
.expect( | ||
"Git command to get commit hash and timestamp should work during build process", | ||
); | ||
|
||
let output_string = String::from_utf8(output.stdout) | ||
.expect("Git command should produce valid string output"); | ||
fn main() -> Result<(), Box<dyn Error>> { | ||
if env::var("GIT_COMMIT_HASH").is_err() { | ||
match get_commithash_with_dirty_suffix() { | ||
Ok(commit_hash_with_suffix) => { | ||
// Pass value as env var to the notary server | ||
println!("cargo:rustc-env=GIT_COMMIT_HASH={commit_hash_with_suffix}"); | ||
} | ||
Err(e) => { | ||
eprintln!("Failed to get commit hash in notary server build"); | ||
eprintln!("Fix the error or configure GIT_COMMIT_HASH as environment variable"); | ||
return Err(e.message().into()); | ||
} | ||
}; | ||
} | ||
Ok(()) | ||
} | ||
|
||
let (commit_hash, commit_timestamp) = output_string | ||
.as_str() | ||
.split_once(',') | ||
.expect("Git commit hash and timestamp string output should be comma separated"); | ||
fn get_commithash_with_dirty_suffix() -> Result<String, git2::Error> { | ||
let repo = Repository::discover(".")?; | ||
let commit = get_commit(&repo)?; | ||
let commit_hash = commit.id().to_string(); | ||
let _timestamp = get_commit_timestamp(&commit)?; | ||
let has_changes = check_local_changes(&repo)?; | ||
|
||
// Pass these 2 values as env var to the program | ||
println!("cargo:rustc-env=GIT_COMMIT_HASH={}", commit_hash); | ||
println!("cargo:rustc-env=GIT_COMMIT_TIMESTAMP={}", commit_timestamp); | ||
if has_changes { | ||
Ok(format!("{commit_hash} (with local changes)")) | ||
} else { | ||
Ok(commit_hash) | ||
} | ||
} | ||
|
||
fn get_commit(repo: &Repository) -> Result<Commit, git2::Error> { | ||
let head = repo.head()?; | ||
head.peel_to_commit() | ||
} | ||
|
||
fn get_commit_timestamp(commit: &Commit) -> Result<String, git2::Error> { | ||
let timestamp = commit.time().seconds(); | ||
let date_time = DateTime::from_timestamp(timestamp, 0) | ||
.ok_or_else(|| git2::Error::from_str("Invalid timestamp"))?; | ||
Ok(date_time.to_rfc2822()) | ||
} | ||
|
||
fn check_local_changes(repo: &Repository) -> Result<bool, git2::Error> { | ||
let mut status_options = StatusOptions::new(); | ||
status_options | ||
.include_untracked(false) | ||
.include_ignored(false); | ||
let statuses = repo.statuses(Some(&mut status_options))?; | ||
Ok(!statuses.is_empty()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 4 additions & 12 deletions
16
crates/notary/server/notary-server.Dockerfile.dockerignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,4 @@ | ||
# exclude everything | ||
* | ||
|
||
# include notary and core library dependencies | ||
!/crates | ||
!/Cargo.toml | ||
|
||
# include .git for program to get git info | ||
!/.git | ||
|
||
# exclude any /target folders inside the included folders above | ||
**/target* | ||
# exclude Rust build artifacts | ||
./target | ||
./crates/wasm/pkg/ | ||
./crates/wasm-test-runner/static/generated/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters