diff --git a/README.md b/README.md index 17680d7b..35cb2511 100644 --- a/README.md +++ b/README.md @@ -237,6 +237,8 @@ correct key is provided set in `git_crypt_key`. * `short_ref_format`: *Optional.* When populating `.git/short_ref` use this `printf` format. Defaults to `%s`. +* `timestamp_format`: *Optional.* When populating `.git/commit_timestamp` use this options to pass to [`git log --date`](https://git-scm.com/docs/git-log#Documentation/git-log.txt---dateltformatgt). Defaults to `iso8601`. + * `describe_ref_options`: *Optional.* When populating `.git/describe_ref` use this options to call [`git describe`](https://git-scm.com/docs/git-describe). Defaults to `--always --dirty --broken`. #### GPG signature verification @@ -261,6 +263,8 @@ the case. * `.git/commit_message`: For publishing the Git commit message on successful builds. + * `.git/commit_timestamp`: For tagging builds with a timestamp. + * `.git/describe_ref`: Version reference detected and checked out. Can be templated with `describe_ref_options` parameter. By default, it will contain the `--g` (eg. `v1.6.2-1-g13dfd7b`). If the repo was never tagged before, this falls back to a short commit SHA-1 ref. diff --git a/assets/in b/assets/in index e0d0134a..786784ef 100755 --- a/assets/in +++ b/assets/in @@ -50,6 +50,7 @@ gpg_keyserver=$(jq -r '.source.gpg_keyserver // "hkp://ipv4.pool.sks-keyservers. disable_git_lfs=$(jq -r '(.params.disable_git_lfs // false)' < $payload) clean_tags=$(jq -r '(.params.clean_tags // false)' < $payload) short_ref_format=$(jq -r '(.params.short_ref_format // "%s")' < $payload) +timestamp_format=$(jq -r '(.params.timestamp_format // "iso8601")' < $payload) describe_ref_options=$(jq -r '(.params.describe_ref_options // "--always --dirty --broken")' < $payload) # If params not defined, get it from source @@ -197,6 +198,9 @@ echo "${return_ref}" | cut -c1-7 | awk "{ printf \"${short_ref_format}\", \$1 }" # for example git log -1 --format=format:%B > .git/commit_message +# Store commit date in .git/commit_timestamp. Can be used for tagging builds +git log -1 --format=%cd --date=${timestamp_format} > .git/commit_timestamp + # Store describe_ref when available. Useful to build Docker images with # a custom tag, or package to publish echo "$(git describe ${describe_ref_options})" > .git/describe_ref diff --git a/test/get.sh b/test/get.sh index 3ecf739d..a60a8e59 100755 --- a/test/get.sh +++ b/test/get.sh @@ -718,6 +718,32 @@ it_can_get_commit_message() { ( echo "Commit message does not match."; return 1 ) } +it_can_get_commit_timestamps() { + run test_commit_timestamp_format "iso8601" + run test_commit_timestamp_format "iso-strict" + run test_commit_timestamp_format "rfc" + run test_commit_timestamp_format "short" + run test_commit_timestamp_format "raw" + run test_commit_timestamp_format "unix" +} + +test_commit_timestamp_format() { + local repo=$(init_repo) + local commit_message='Time-is-relevant!' + local ref=$(make_commit $repo $commit_message) + local dest=$TMPDIR/destination + + get_uri_with_custom_timestamp $repo $dest $1 + + pushd $dest + local expected_timestamp=$(git log -1 --date=$1 --format=format:%cd) + popd + + test -e $dest/.git/commit_timestamp || ( echo ".git/commit_timestamp does not exist."; return 1 ) + test "$(cat $dest/.git/commit_timestamp)" = "$expected_timestamp" || \ + ( echo "Commit timestamp for format $1 differs from expectation."; return 1 ) +} + it_decrypts_git_crypted_files() { local repo=$(git_crypt_fixture_repo_path) local dest=$TMPDIR/destination @@ -831,6 +857,7 @@ run it_can_get_signed_commit_via_tag run it_can_get_committer_email run it_can_get_returned_ref run it_can_get_commit_message +run it_can_get_commit_timestamps run it_decrypts_git_crypted_files run it_clears_tags_with_clean_tags_param run it_retains_tags_by_default diff --git a/test/helpers.sh b/test/helpers.sh index 70f00f25..2266ead5 100644 --- a/test/helpers.sh +++ b/test/helpers.sh @@ -874,6 +874,17 @@ get_uri_with_clean_tags() { }" | ${resource_dir}/in "$2" | tee /dev/stderr } +get_uri_with_custom_timestamp() { + jq -n "{ + source: { + uri: $(echo $1 | jq -R .), + }, + params: { + timestamp_format: \"$3\" + } + }" | ${resource_dir}/in "$2" | tee /dev/stderr +} + put_uri() { jq -n "{ source: {