Skip to content
This repository has been archived by the owner on Jul 30, 2021. It is now read-only.

build/git-version: Only call 'git' once #951

Merged

Conversation

wking
Copy link
Contributor

@wking wking commented Mar 29, 2018

Simplify the logic from #47 to:

  • Move from Bash to a generic POSIX shell. This makes the script more portable (it will work on POSIX systems without Bash). It does mean we need to drop the set (which is not in POSIX), but that's not a major problem because:

    • We can use &&-chaining instead of set -e.
    • We do only have the one locally-defined variable, so we don't need set -u.
    • The simpler logic has no pipes, so set -o pipefail would no longer matter.
  • Replace the previous multiple Git calls with a single call to git describe.

    • Use --dirty so we can drop the previous diff call.

    • Drop --tags, because we don't want to use lightweight (non-annotated) tags. We want to use annotated tags, because we want to use signed release tags. There's no code check for tag signatures here, but RELEASING.md requests tag -s vX.Y.Z, which will create signed, annotated tags.

    • Git's SHA-1 hashes are 40 hex digits, but I've used --abbrev=100 to get unabbreviated hashes even in the face of future hash transitions. This is a bit of a hack compared to the previous approach, because at some point Git may have >100-char hashes. But if that happens, it will be far enough in the future that I think the simplification we get here is worth it (and this constant is easy to bump if we need to).

    • The new call always returns the most recent tag, and (for commits which are not exact tag matches) also includes the abbreviated commit. To isolate the hash (and possible dirty-mark) for commits that are not exact tags, I use POSIX's ${parameter##[word]} prefix-pattern removal. Exact tags are unlikely to include -g (unless it is in the tag itself), so for them the prefix-removal is a no-op. Again, this is a bit of a hack, since a tag might include -g, but I think that's unlikely enough (as mentioned above, RELEASING.md requests vX.Y.Z for tags) that the simplification we get here is worth it.

Fixes #132.

@coreosbot
Copy link

Can one of the admins verify this patch?

3 similar comments
@coreosbot
Copy link

Can one of the admins verify this patch?

@coreosbot
Copy link

Can one of the admins verify this patch?

@coreosbot
Copy link

Can one of the admins verify this patch?

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Mar 29, 2018
Simplify the logic from 04848d1 (Add release build process,
2016-05-27, kubernetes-retired#47) to:

* Move from Bash to a generic POSIX shell.  This makes the script more
  portable (it will work on POSIX systems without Bash).  It does mean
  we need to drop the 'set' (which is not in POSIX [1]), but that's
  not a major problem because:

  * We can use &&-chaining instead of 'set -e'.
  * We do only have the one locally-defined variable, so we don't need
    'set -u'.
  * The simpler logic has no pipes, so 'set -o pipefail' would no
    longer matter.

* Replace the previous multiple Git calls with a single call to 'git
  describe'.

  * Use --dirty so we can drop the previous diff call.

  * Drop --tags, because we don't want to use lightweight
    (non-annotated) tags.  We want to use annotated tags, because we
    want to use signed release tags.  There's no code check for tag
    signatures here, but RELEASING.md requests 'tag -s vX.Y.Z', which
    will create signed, annotated tags.

  * Git's SHA-1 hashes are 40 hex digits, but I've used --abbrev=100
    to get unabbreviated hashes even in the face of future hash
    transitions [2].  This is a bit of a hack compared to the previous
    approach, because at some point Git may have >100-char hashes.
    But if that happens, it will be far enough in the future that I
    think the simplification we get here is worth it (and this
    constant is easy to bump if we need to).

  * The new call always returns the most recent tag, and (for commits
    which are not exact tag matches) also includes the abbreviated
    commit.  To isolate the hash (and possible dirty-mark) for commits
    that are not exact tags, I use POSIX's ${parameter##[word]}
    prefix-pattern removal [3].  Exact tags are unlikely to include
    '-g' (unless it is in the tag itself), so for them the
    prefix-removal is a no-op.  Again, this is a bit of a hack, since
    a tag might include '-g', but I think that's unlikely enough (as
    mentioned above, RELEASING.md requests vX.Y.Z for tags) that the
    simplification we get here is worth it.

[1]: http://pubs.opengroup.org/onlinepubs/9699919799/idx/utilities.html
[2]: https://github.com/git/git/blob/v2.16.3/Documentation/technical/hash-function-transition.txt
[3]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02
@k8s-ci-robot k8s-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Mar 29, 2018
@wking wking force-pushed the git-describe-simplification branch from 193ce7c to 3b0431f Compare March 29, 2018 18:37
@rphillips
Copy link
Contributor

ok to test

fi

echo $VERSION
DESCRIPTION=$(git describe --abbrev=100 --dirty) &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

has an extra && at the end

Copy link
Contributor Author

@wking wking Apr 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

has an extra && at the end

Yup, that's the &&-chaining I mention in the initial post. It ensured that the script exits with a non-zero exit code if for some reason the subshell here does. You can test that behavior by calling the script from outside of aa Git repo (e.g. from /) and confirming its non-zero exit code.

@rphillips rphillips merged commit 8f601b5 into kubernetes-retired:master Apr 3, 2018
@wking wking deleted the git-describe-simplification branch April 3, 2018 16:13
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

hack/git-version.sh doesn't check for staged/indexed files
4 participants