From 7bcd3892b7fb5a178582cc9776a449715b461592 Mon Sep 17 00:00:00 2001 From: Pierre Boutillier Date: Tue, 29 Jan 2019 22:08:49 +0100 Subject: [PATCH] Reify the git environment at compile time into the code Use it to output a minimal `./tezos-node --version` Works both when you compile inside a git repository or from an archive generated by `git archive` (which is what gitlab does when you click on "Download zip/tar.gz/tar.bz2" in the web interface) --- .gitattributes | 4 +++ src/bin_node/main.ml | 4 +++ src/lib_base/current_git_info.ml | 45 +++++++++++++++++++++++++++++++ src/lib_base/current_git_info.mli | 28 +++++++++++++++++++ src/lib_base/dune | 6 +++++ src/lib_base/get-git-info.ml | 19 +++++++++++++ 6 files changed, 106 insertions(+) create mode 100644 .gitattributes create mode 100644 src/lib_base/current_git_info.ml create mode 100644 src/lib_base/current_git_info.mli create mode 100755 src/lib_base/get-git-info.ml diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 000000000..b702860fe --- /dev/null +++ b/.gitattributes @@ -0,0 +1,4 @@ +.gitignore export-ignore +.gitattributes export-ignore +.gitlab-ci.yml export-ignore +src/lib_base/current_git_info.ml export-subst diff --git a/src/bin_node/main.ml b/src/bin_node/main.ml index aa239b3f6..77a12535f 100644 --- a/src/bin_node/main.ml +++ b/src/bin_node/main.ml @@ -51,9 +51,13 @@ let man = Node_run_command.Manpage.examples let info = + let version = + Tezos_base.Current_git_info.abbreviated_commit_hash ^ + " ("^Tezos_base.Current_git_info.committer_date^")" in Cmdliner.Term.info ~doc:"The Tezos node" ~man + ~version "tezos-node" let commands = [ diff --git a/src/lib_base/current_git_info.ml b/src/lib_base/current_git_info.ml new file mode 100644 index 000000000..f1e1eba70 --- /dev/null +++ b/src/lib_base/current_git_info.ml @@ -0,0 +1,45 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2019 Nomadic Labs, *) +(* *) +(* Permission is hereby granted, free of charge, to any person obtaining a *) +(* copy of this software and associated documentation files (the "Software"),*) +(* to deal in the Software without restriction, including without limitation *) +(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) +(* and/or sell copies of the Software, and to permit persons to whom the *) +(* Software is furnished to do so, subject to the following conditions: *) +(* *) +(* The above copyright notice and this permission notice shall be included *) +(* in all copies or substantial portions of the Software. *) +(* *) +(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) +(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) +(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) +(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) +(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) +(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) +(* DEALINGS IN THE SOFTWARE. *) +(* *) +(*****************************************************************************) + +let raw_commit_hash = "$Format:%H$" + +let commit_hash = + if String.equal raw_commit_hash ("$Format:"^"%H$"(*trick to avoid git-subst*)) + then Generated_git_info.commit_hash + else raw_commit_hash + +let raw_abbreviated_commit_hash = "$Format:%h$" + +let abbreviated_commit_hash = + if String.equal raw_abbreviated_commit_hash ("$Format:"^"%h$") + then Generated_git_info.abbreviated_commit_hash + else raw_abbreviated_commit_hash + +let raw_committer_date = "$Format:%ci$" + +let committer_date = + if String.equal raw_committer_date ("$Format:"^"%ci$") + then Generated_git_info.committer_date + else raw_committer_date diff --git a/src/lib_base/current_git_info.mli b/src/lib_base/current_git_info.mli new file mode 100644 index 000000000..fc4f9eff6 --- /dev/null +++ b/src/lib_base/current_git_info.mli @@ -0,0 +1,28 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2019 Nomadic Labs, *) +(* *) +(* Permission is hereby granted, free of charge, to any person obtaining a *) +(* copy of this software and associated documentation files (the "Software"),*) +(* to deal in the Software without restriction, including without limitation *) +(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) +(* and/or sell copies of the Software, and to permit persons to whom the *) +(* Software is furnished to do so, subject to the following conditions: *) +(* *) +(* The above copyright notice and this permission notice shall be included *) +(* in all copies or substantial portions of the Software. *) +(* *) +(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) +(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) +(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) +(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) +(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) +(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) +(* DEALINGS IN THE SOFTWARE. *) +(* *) +(*****************************************************************************) + +val commit_hash : string +val abbreviated_commit_hash : string +val committer_date : string diff --git a/src/lib_base/dune b/src/lib_base/dune index 31f2ba3e8..2d3b672a3 100644 --- a/src/lib_base/dune +++ b/src/lib_base/dune @@ -1,6 +1,7 @@ (library (name tezos_base) (public_name tezos-base) + (modules :standard \ Get-git-info) (flags (:standard -w -30 -open Tezos_stdlib -open Tezos_crypto @@ -24,6 +25,11 @@ mtime.clock.os ipaddr.unix)) +(rule + (targets generated_git_info.ml) + (deps get-git-info.ml) + (action (with-stdout-to %{targets} (run %{ocaml} unix.cma %{deps})))) + (alias (name runtest_indent) (deps (glob_files *.ml{,i})) diff --git a/src/lib_base/get-git-info.ml b/src/lib_base/get-git-info.ml new file mode 100755 index 000000000..1ebb716d2 --- /dev/null +++ b/src/lib_base/get-git-info.ml @@ -0,0 +1,19 @@ +#!/usr/bin/env ocaml + + #load "unix.cma" + +let query cmd = + let chan = Unix.open_process_in cmd in + try + let out = input_line chan in + if Unix.close_process_in chan = Unix.WEXITED 0 then out + else "unkown" + with End_of_file -> "unkown" + +let () = + Format.printf "@[let commit_hash = \"%s\"@," + (query "git show -s --pretty=format:%H"); + Format.printf "let abbreviated_commit_hash = \"%s\"@," + (query "git show -s --pretty=format:%h"); + Format.printf "let committer_date = \"%s\"@]@." + (query "git show -s --pretty=format:%ci")