Commit de405ac1 authored by Filipe Brandenburger's avatar Filipe Brandenburger

Improve generation of version information from the git tree

Detect whether the tree is dirty and append a "-dirty" indication to the git commit (common practice with other repos, e.g. kernel, docker.) Properly handle the case where a git tree is not found (e.g. building from archive.) In the sed expression, look for the variable to be updated (commitFromGit) instead of hardcoding a line number. Tested: - Built from a dirty tree: $ output/go/bin/kubelet -version Kubernetes version 0.1, build 2d784c684c75-dirty - Built from a clean tree: $ output/go/bin/kubelet -version Kubernetes version 0.1, build 505f23a31172 - Built from an archive: $ hack/build-go.sh WARNING: unable to find git commit, falling back to commitFromGit = `(none)` $ output/go/bin/kubelet -version Kubernetes version 0.1, build (none) Signed-off-by: 's avatarFilipe Brandenburger <filbranden@google.com>
parent 7e566091
......@@ -14,10 +14,28 @@
# See the License for the specific language governing permissions and
# limitations under the License.
set -o errexit
set -o nounset
set -o pipefail
topdir=$(dirname "$0")/..
cd "${topdir}"
# TODO: when we start making tags, switch to git describe?
desc=$(git rev-list --abbrev-commit --max-count=1 HEAD)
tab=$'\t'
script="22s/.*/${tab}commitFromGit = \`${desc}\`/"
infile="$(dirname $0)/../pkg/version/template.go"
outfile="$(dirname $0)/../pkg/version/autogenerated.go"
sed "${script}" "${infile}" > "${outfile}"
if git_commit=$(git rev-parse --short "HEAD^{commit}" 2>/dev/null); then
# Remove any invalid characters that might confuse "sed".
git_commit=${git_commit//[^a-f0-9]/}
# Check if the tree is dirty.
if ! dirty_tree=$(git status --porcelain) || [[ -n "${dirty_tree}" ]]; then
git_commit="${git_commit}-dirty"
fi
else
git_commit="(none)"
echo "WARNING: unable to find git commit, falling back to commitFromGit = \`${git_commit}\`" >&2
fi
# TODO: Instead of using an autogenerated file, we could pass this variable
# to the source through Go's -X ldflag.
sed "s/@@GIT_COMMIT@@/${git_commit}/g" \
pkg/version/template.go.tmpl >pkg/version/autogenerated.go
......@@ -19,5 +19,5 @@ package version
// This file is the template for the machine-edited autogenerated.go.
// Do not modify this file without also modifying hack/version-gen.sh.
var (
placeholder interface{}
commitFromGit = `@@GIT_COMMIT@@`
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment