Skip to content

Commit

Permalink
Merge pull request #333 from hy0044/add-branch-out-param
Browse files Browse the repository at this point in the history
Add 'branch' parameter for a put step
  • Loading branch information
aoldershaw authored Oct 2, 2020
2 parents 966b31d + 504a3aa commit 7585948
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,12 @@ pushed regardless of the upstream state.
* `notes`: *Optional.* If this is set then notes will be added to HEAD to the
`refs/notes/commits` ref. The value should be a path to a file containing the notes.

* `branch`: *Optional.* The branch to push commits.

Note that the version produced by the `put` step will be picked up by subsequent `get` steps
even if the `branch` differs from the `branch` specified in the source.
To avoid this, you should use two resources of read-only and write-only.

## Development

### Prerequisites
Expand Down
6 changes: 6 additions & 0 deletions assets/in
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ uri=$(jq -r '.source.uri // ""' < $payload)
branch=$(jq -r '.source.branch // ""' < $payload)
git_config_payload=$(jq -r '.source.git_config // []' < $payload)
ref=$(jq -r '.version.ref // "HEAD"' < $payload)
override_branch=$(jq -r '.version.branch // ""' < $payload)
depth=$(jq -r '(.params.depth // 0)' < $payload)
fetch=$(jq -r '(.params.fetch // [])[]' < $payload)
submodules=$(jq -r '(.params.submodules // "all")' < $payload)
Expand Down Expand Up @@ -71,6 +72,11 @@ if [ -n "$branch" ]; then
branchflag="--branch $branch"
fi

if [ -n "$override_branch" ]; then
echo "Override $branch with $override_branch"
branchflag="--branch $override_branch"
fi

depthflag=""
if test "$depth" -gt 0 2> /dev/null; then
depthflag="--depth $depth"
Expand Down
23 changes: 18 additions & 5 deletions assets/out
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ force=$(jq -r '.params.force // false' < $payload)
only_tag=$(jq -r '.params.only_tag // false' < $payload)
annotation_file=$(jq -r '.params.annotate // ""' < $payload)
notes_file=$(jq -r '.params.notes // ""' < $payload)
override_branch=$(jq -r '.params.branch // ""' < $payload)

configure_git_global "${git_config_payload}"

Expand All @@ -48,7 +49,7 @@ if [ -z "$uri" ]; then
exit 1
fi

if [ -z "$branch" ] && [ "$only_tag" != "true" ]; then
if [ -z "$branch" ] && [ "$only_tag" != "true" ] && [ -z "$override_branch" ]; then
echo "invalid payload (missing branch)"
exit 1
fi
Expand Down Expand Up @@ -80,6 +81,11 @@ if [ $force = "true" ]; then
forceflag="--force"
fi

if [ -n "$override_branch" ]; then
echo "Override $branch with $override_branch"
branch=$override_branch
fi

tag_name=""
if [ -n "$tag" ]; then
tag_name="$(cat $tag)"
Expand Down Expand Up @@ -203,7 +209,14 @@ else
version_ref="$(git rev-parse HEAD | jq -R .)"
fi

jq -n "{
version: {ref: $version_ref},
metadata: $(git_metadata)
}" >&3
if [ -n "$override_branch" ]; then
jq -n "{
version: {branch: $(echo $override_branch | jq -R .), ref: $version_ref},
metadata: $(git_metadata)
}" >&3
else
jq -n "{
version: {ref: $version_ref},
metadata: $(git_metadata)
}" >&3
fi
15 changes: 15 additions & 0 deletions test/get.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ it_can_get_from_url_only_single_branch() {
! git -C $dest rev-parse origin/bogus
}

it_can_get_from_url_at_override_branch() {
local repo=$(init_repo)
local branch="branch-a"
local ref=$(make_commit_to_branch $repo $branch)
local dest=$TMPDIR/destination

get_uri_with_override_branch $repo $branch $ref $dest | jq -e "
.version == {ref: $(echo $ref | jq -R .)}
"

test -e $dest/some-file
test "$(git -C $dest rev-parse HEAD)" = $ref
}

it_omits_empty_branch_in_metadata() {
local repo=$(init_repo)
local ref1=$(make_commit_to_branch $repo branch-a)
Expand Down Expand Up @@ -835,6 +849,7 @@ run it_can_get_from_url
run it_can_get_from_url_at_ref
run it_can_get_from_url_at_branch
run it_can_get_from_url_only_single_branch
run it_can_get_from_url_at_override_branch
run it_omits_empty_branch_in_metadata
run it_returns_branch_in_metadata
run it_omits_empty_tags_in_metadata
Expand Down
27 changes: 27 additions & 0 deletions test/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,21 @@ get_uri_with_branch() {
}" | ${resource_dir}/in "$3" | tee /dev/stderr
}

get_uri_with_override_branch() {
jq -n "{
source: {
uri: $(echo $1 | jq -R .)
},
params: {
short_ref_format: \"test-%s\"
},
version: {
branch: $(echo $2 | jq -R .),
ref: $(echo $3 | jq -R .)
}
}" | ${resource_dir}/in "$4" | tee /dev/stderr
}

get_uri_with_git_crypt_key() {
local git_crypt_key_path=$(git_crypt_fixture_key_path)
local git_crypt_key_base64_encoded=$(cat $git_crypt_key_path | base64)
Expand Down Expand Up @@ -897,6 +912,18 @@ put_uri() {
}" | ${resource_dir}/out "$2" | tee /dev/stderr
}

put_uri_with_branch() {
jq -n "{
source: {
uri: $(echo $1 | jq -R .)
},
params: {
repository: $(echo $3 | jq -R .),
branch: $(echo $4 | jq -R .),
}
}" | ${resource_dir}/out "$2" | tee /dev/stderr
}

put_uri_with_force() {
jq -n "{
source: {
Expand Down
25 changes: 25 additions & 0 deletions test/put.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,30 @@ it_can_put_to_url() {
test "$(git -C $repo1 rev-parse some-tag)" = $ref
}

it_can_put_to_url_with_branch() {
local repo1=$(init_repo)

local src=$(mktemp -d $TMPDIR/put-src.XXXXXX)
local repo2=$src/repo
git clone $repo1 $repo2

local branch="branch-a"
local ref=$(make_commit_to_branch $repo2 $branch)

# cannot push to repo while it's checked out to a branch
git -C $repo1 checkout refs/heads/master

put_uri_with_branch $repo1 $src repo $branch | jq -e "
.version == {branch: $(echo $branch | jq -R .), ref: $(echo $ref | jq -R .)}
"

# switch to branch-a
git -C $repo1 checkout $branch

test -e $repo1/some-file
test "$(git -C $repo1 rev-parse HEAD)" = $ref
}

it_returns_branch_in_metadata() {
local repo1=$(init_repo)

Expand Down Expand Up @@ -562,6 +586,7 @@ it_will_fail_put_with_conflicting_tag_and_not_force_push() {
}

run it_can_put_to_url
run it_can_put_to_url_with_branch
run it_returns_branch_in_metadata
run it_can_put_to_url_with_tag
run it_can_put_to_url_with_tag_and_prefix
Expand Down

0 comments on commit 7585948

Please sign in to comment.