Skip to content

Commit

Permalink
Merge pull request #371 from jamie-pate/master
Browse files Browse the repository at this point in the history
Add the ability to push to refs other than refs/heads
  • Loading branch information
taylorsilva authored Sep 2, 2021
2 parents 75b966e + cc85431 commit 2d4422f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ pushed regardless of the upstream state.
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.

* `refs_prefix`: *Optional.* Allows pushing to refs other than heads. Defaults to `refs/heads`.

## Development

### Prerequisites
Expand Down
4 changes: 3 additions & 1 deletion assets/out
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ 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")
# useful for pushing to special ref types like refs/for in gerrit.
refs_prefix=$(jq -r '.params.refs_prefix // "refs/heads"' <<< "$payload")

configure_git_global "${git_config_payload}"

Expand Down Expand Up @@ -103,7 +105,7 @@ tag() {
}

push_src_and_tags() {
git push --tags push-target HEAD:refs/heads/$branch $forceflag
git push --tags push-target HEAD:$refs_prefix/$branch $forceflag
}

push_tags() {
Expand Down
13 changes: 13 additions & 0 deletions test/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1238,3 +1238,16 @@ put_uri_with_config() {
}
}" | ${resource_dir}/out "$2" | tee /dev/stderr
}

put_uri_with_refs_prefix() {
jq -n "{
source: {
uri: $(echo $1 | jq -R .),
branch: \"master\"
},
params: {
repository: $(echo $3 | jq -R .),
refs_prefix: $(echo $4 | jq -R .),
}
}" | ${resource_dir}/out "$2" | tee /dev/stderr
}
24 changes: 24 additions & 0 deletions test/put.sh
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,29 @@ it_will_fail_put_with_conflicting_tag_and_not_force_push() {
test "$(git -C $repo1 rev-parse some-only-tag)" = $expected_ref
}

it_can_put_with_refs_prefix() {
local repo1=$(init_repo)

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

local ref=$(make_commit $repo2)

# cannot push to repo while it's checked out to a branch
git -C $repo1 checkout refs/heads/master
set -x
put_uri_with_refs_prefix $repo1 $src repo refs/for | jq -e "
.version == {ref: $(echo $ref | jq -R .)}
"

# switch back to master
git -C $repo1 checkout refs/for/master

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

run it_can_put_to_url
run it_can_put_to_url_with_branch
run it_returns_branch_in_metadata
Expand All @@ -605,3 +628,4 @@ run it_will_fail_put_if_conflicts_and_not_force_push
run it_can_put_and_force_the_push
run it_can_put_to_url_with_only_tag_and_force_the_push
run it_will_fail_put_with_conflicting_tag_and_not_force_push
run it_can_put_with_refs_prefix

0 comments on commit 2d4422f

Please sign in to comment.