From 1c1e83ca4579fa298d0e6680fe0f106bff38aa68 Mon Sep 17 00:00:00 2001 From: Norman Gehrsitz Date: Mon, 3 Aug 2020 18:49:29 +0200 Subject: [PATCH 1/4] Add Timestamp git metadata file Signed-off-by: Norman Gehrsitz --- README.md | 2 ++ assets/in | 3 +++ 2 files changed, 5 insertions(+) diff --git a/README.md b/README.md index 17680d7b..d7a2e7ee 100644 --- a/README.md +++ b/README.md @@ -261,6 +261,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..559e0031 100755 --- a/assets/in +++ b/assets/in @@ -197,6 +197,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=iso8601 > .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 From d3457cf900a54bd2e7f014039f45f0a30e59b572 Mon Sep 17 00:00:00 2001 From: Norman Gehrsitz Date: Mon, 3 Aug 2020 18:49:42 +0200 Subject: [PATCH 2/4] Support custom timestamp format Signed-off-by: Norman Gehrsitz --- README.md | 2 ++ assets/in | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d7a2e7ee..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 diff --git a/assets/in b/assets/in index 559e0031..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 @@ -198,7 +199,7 @@ echo "${return_ref}" | cut -c1-7 | awk "{ printf \"${short_ref_format}\", \$1 }" 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=iso8601 > .git/commit_timestamp +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 From 316463e63288abd488be7578652cd33fb51416ee Mon Sep 17 00:00:00 2001 From: Norman Gehrsitz Date: Mon, 3 Aug 2020 18:49:45 +0200 Subject: [PATCH 3/4] Add tests for default iso8601 timestamps Signed-off-by: Norman Gehrsitz --- test/get.sh | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/get.sh b/test/get.sh index 3ecf739d..66a16473 100755 --- a/test/get.sh +++ b/test/get.sh @@ -3,6 +3,7 @@ set -e source $(dirname $0)/helpers.sh +source $(dirname $0)/common.sh it_can_get_from_url() { local repo=$(init_repo) @@ -718,6 +719,23 @@ it_can_get_commit_message() { ( echo "Commit message does not match."; return 1 ) } +it_can_get_commit_timestamp() { + local repo=$(init_repo) + local commit_message='Time-is-relevant!' + local ref=$(make_commit $repo $commit_message) + local dest=$TMPDIR/destination + + get_uri $repo $dest $1 + + pushd $dest + local expected_timestamp=$(git_metadata | jq -r '.[] | select(.name == "committer_date") | .value') + 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 differs from expectation."; return 1 ) +} + it_decrypts_git_crypted_files() { local repo=$(git_crypt_fixture_repo_path) local dest=$TMPDIR/destination @@ -831,6 +849,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_timestamp run it_decrypts_git_crypted_files run it_clears_tags_with_clean_tags_param run it_retains_tags_by_default From e5ad50417380b94deef27cb44444dbe31231ef04 Mon Sep 17 00:00:00 2001 From: Norman Gehrsitz Date: Mon, 10 Aug 2020 12:14:00 +0200 Subject: [PATCH 4/4] Add tests for custom timestamp formats Signed-off-by: Norman Gehrsitz --- test/get.sh | 20 ++++++++++++++------ test/helpers.sh | 11 +++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/test/get.sh b/test/get.sh index 66a16473..a60a8e59 100755 --- a/test/get.sh +++ b/test/get.sh @@ -3,7 +3,6 @@ set -e source $(dirname $0)/helpers.sh -source $(dirname $0)/common.sh it_can_get_from_url() { local repo=$(init_repo) @@ -719,21 +718,30 @@ it_can_get_commit_message() { ( echo "Commit message does not match."; return 1 ) } -it_can_get_commit_timestamp() { +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 $repo $dest $1 + get_uri_with_custom_timestamp $repo $dest $1 pushd $dest - local expected_timestamp=$(git_metadata | jq -r '.[] | select(.name == "committer_date") | .value') + 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 differs from expectation."; return 1 ) + ( echo "Commit timestamp for format $1 differs from expectation."; return 1 ) } it_decrypts_git_crypted_files() { @@ -849,7 +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_timestamp +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: {