From 3d45cf9788a63091f5e146260eff2ad04cd13471 Mon Sep 17 00:00:00 2001 From: Rob Liebowitz Date: Tue, 21 May 2019 15:08:24 -0400 Subject: [PATCH] Add "returning" option for merge output ref Setting the `returning` param to `unmerged` allows using the original commit as the resource output when setting the `merge` param to `true`. Resolves #253 Signed-off-by: Rob Liebowitz --- README.md | 4 ++++ assets/out | 9 ++++++++- test/helpers.sh | 14 ++++++++++++++ test/put.sh | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9ddafda4..f997702d 100644 --- a/README.md +++ b/README.md @@ -214,6 +214,10 @@ and the `rebase` parameter is not provided, the push will fail. attempt to merge remote to local before pushing. Only one of `merge` or `rebase` can be provided, but not both. +* `returning`: *Optional.* When passing the `merge` flag, specify whether the + merge commit or the original, unmerged commit should be passed as the output + ref. Options are `merged` and `unmerged`. Defaults to `merged`. + * `tag`: *Optional.* If this is set then HEAD will be tagged. The value should be a path to a file containing the name of the tag. diff --git a/assets/out b/assets/out index 524fe280..2e165ec9 100755 --- a/assets/out +++ b/assets/out @@ -35,6 +35,7 @@ tag=$(jq -r '.params.tag // ""' < $payload) tag_prefix=$(jq -r '.params.tag_prefix // ""' < $payload) rebase=$(jq -r '.params.rebase // false' < $payload) merge=$(jq -r '.params.merge // false' < $payload) +returning=$(jq -r '.params.returning // "merged"' < $payload) force=$(jq -r '.params.force // false' < $payload) only_tag=$(jq -r '.params.only_tag // false' < $payload) annotation_file=$(jq -r '.params.annotate // ""' < $payload) @@ -196,7 +197,13 @@ else add_and_push_notes fi +if [ "$merge" = "true" ] && [ "$returning" = "unmerged" ]; then + version_ref="$(echo "$commit_to_push" | jq -R .)" +else + version_ref="$(git rev-parse HEAD | jq -R .)" +fi + jq -n "{ - version: {ref: $(git rev-parse HEAD | jq -R .)}, + version: {ref: $version_ref}, metadata: $(git_metadata) }" >&3 diff --git a/test/helpers.sh b/test/helpers.sh index f32bfeec..df5f77d2 100644 --- a/test/helpers.sh +++ b/test/helpers.sh @@ -790,6 +790,20 @@ put_uri_with_merge() { }" | ${resource_dir}/out "$2" | tee /dev/stderr } +put_uri_with_merge_returning_unmerged() { + jq -n "{ + source: { + uri: $(echo $1 | jq -R .), + branch: \"master\" + }, + params: { + repository: $(echo $3 | jq -R .), + merge: true, + returning: \"unmerged\" + } + }" | ${resource_dir}/out "$2" | tee /dev/stderr +} + put_uri_with_merge_and_rebase() { jq -n "{ source: { diff --git a/test/put.sh b/test/put.sh index 8c36a0fc..5c1d3230 100755 --- a/test/put.sh +++ b/test/put.sh @@ -274,6 +274,43 @@ it_can_put_to_url_with_merge_commit() { test $latest_merge_ref = $merged_ref } +it_chooses_the_unmerged_commit_ref() { + local repo1=$(init_repo) + + local src=$(mktemp -d $TMPDIR/put-src.XXXXXX) + local repo2=$src/repo + git clone $repo1 $repo2 + + # make a commit that will require rebasing + local baseref=$(make_commit_to_file $repo1 some-other-file) + + local unmerged_ref=$(make_commit $repo2) + + # cannot push to repo while it's checked out to a branch + git -C $repo1 checkout refs/heads/master + + local response=$(mktemp $TMPDIR/rebased-response.XXXXXX) + + put_uri_with_merge_returning_unmerged $repo1 $src repo > $response + + local merged_ref=$(git -C $repo2 rev-parse HEAD) + + jq -e " + .version == {ref: $(echo $unmerged_ref | jq -R .)} + " < $response + + # switch back to master + git -C $repo1 checkout master + + test -e $repo1/some-file + + test "$(git -C $repo1 rev-parse HEAD)" = $merged_ref + + local latest_merge_ref=$(git -C $repo1 log -n 1 --merges --pretty=format:"%H") + + test $latest_merge_ref = $merged_ref +} + it_will_fail_put_if_merge_and_rebase_are_set() { local repo1=$(init_repo) @@ -536,6 +573,7 @@ run it_can_put_to_url_with_rebase_with_tag run it_can_put_to_url_with_rebase_with_tag_and_prefix run it_will_fail_put_if_merge_and_rebase_are_set run it_can_put_to_url_with_merge_commit +run it_chooses_the_unmerged_commit_ref run it_can_put_to_url_with_only_tag run it_can_put_and_set_git_config run it_will_fail_put_if_conflicts_and_not_force_push