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: Filipe Brandenburger <filbranden@google.com>
This commit is contained in:
Filipe Brandenburger
2014-07-30 16:01:15 -07:00
parent 7e56609139
commit de405ac126
2 changed files with 25 additions and 7 deletions

View File

@@ -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