Bug 1375207 - Display hash from whence geckodriver came in --version; r=jgraham

Because we no longer can tag geckodriver releases in mozilla-central,
we need to include build information in the program itself.  In the
version information message displayed when passing the --version flag,
we now include the current tip's SHA1 and build date following the
version number.

This patch could be made simpler by dumping this information correctly
formatted into a text file in the output directory, but it was requested
in https://bugzilla.mozilla.org/show_bug.cgi?id=1374977 to also include
the version information in the log output, which means we need to access
it differently and in different places.

MozReview-Commit-ID: CbFQn7IV8ew

--HG--
extra : rebase_source : dcc38ba7b5f209e9878755d5d75b611e22b5253d
This commit is contained in:
Andreas Tolfsen 2017-06-22 16:28:18 +01:00
parent d27785f266
commit 4a968ad679
2 changed files with 106 additions and 2 deletions

View File

@ -0,0 +1,73 @@
/// Writes build information to ${OUT_DIR}/build-info.rs which is included in
/// the program during compilation:
///
/// ```no_run
/// const COMMIT_HASH: Option<&'static str> = Some("c31a366");
/// const COMMIT_DATE: Option<&'static str> = Some("1988-05-10");
/// ```
///
/// The values are `None` if running hg failed, e.g. if it is not installed or
/// if we are not in an hg repo.
use std::env;
use std::ffi::OsStr;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;
fn main() {
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let mut fh = File::create(out_dir.join("build-info.rs")).unwrap();
writeln!(
fh,
"const COMMIT_HASH: Option<&'static str> = {:?};",
commit_hash()
).unwrap();
writeln!(
fh,
"const COMMIT_DATE: Option<&'static str> = {:?};",
commit_date()
).unwrap();
}
fn commit_hash() -> Option<String> {
exec(&"hg", &["log", "-r.", "-T '{node|short}'"]).or_else(
|| {
exec(&"git", &["rev-parse", "HEAD"]).and_then(hg2git_sha)
},
)
}
fn commit_date() -> Option<String> {
exec(&"hg", &["log", "-r.", "-T '{date|isodate}'"]).or_else(|| {
exec(
&"git",
&["log", "-1", "--date=short", "--pretty=format:%cd"],
)
})
}
fn exec<S, I>(program: S, args: I) -> Option<String>
where
S: AsRef<OsStr>,
I: IntoIterator<Item = S>,
{
let mut cmd = Command::new(program);
for arg in args {
cmd.arg(arg.as_ref());
}
cmd.output()
.ok()
.and_then(|r| if r.status.success() {
Some(r.stdout)
} else {
None
})
.and_then(|o| String::from_utf8(o).ok())
.map(|s| s.trim_right().into())
}
fn hg2git_sha(hg_sha: String) -> Option<String> {
exec(&"git", &["cinnabar", "git2hg", &hg_sha])
}

View File

@ -21,8 +21,10 @@ extern crate webdriver;
extern crate log;
use std::borrow::ToOwned;
use std::fmt;
use std::fmt::Display;
use std::io::Write;
use std::net::{SocketAddr, IpAddr};
use std::net::{IpAddr, SocketAddr};
use std::path::PathBuf;
use std::str::FromStr;
@ -45,6 +47,35 @@ mod capabilities;
use logging::LogLevel;
use marionette::{MarionetteHandler, MarionetteSettings, extension_routes};
include!(concat!(env!("OUT_DIR"), "/build-info.rs"));
struct BuildInfo;
impl BuildInfo {
pub fn version() -> &'static str {
crate_version!()
}
pub fn hash() -> Option<&'static str> {
COMMIT_HASH
}
pub fn date() -> Option<&'static str> {
COMMIT_DATE
}
}
impl Display for BuildInfo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", BuildInfo::version())?;
match (BuildInfo::hash(), BuildInfo::date()) {
(Some(hash), Some(date)) => write!(f, " ({} {})", hash, date)?,
(Some(hash), None) => write!(f, " ({})", hash)?,
_ => {},
}
Ok(())
}
}
type ProgramResult = std::result::Result<(), (ExitCode, String)>;
enum ExitCode {
@ -104,7 +135,7 @@ fn run() -> ProgramResult {
let matches = app().get_matches();
if matches.is_present("version") {
println!("geckodriver {}\n\n{}", crate_version!(),
println!("geckodriver {}\n\n{}", BuildInfo,
"The source code of this program is available at
https://github.com/mozilla/geckodriver.