Skip to content

Commit cc85431

Browse files
committed
Add the ability to set refs_prefix for the push
Normal branches all exist in refs/heads but some git workflows require pushing to a different type of reference. For example Gerrit Reviews are created/updated by pushing to a 'magic' ref at `refs/for/<branch>` Signed-off-by: Jamie Pate <jpate@fortinet.com>
1 parent 88e8e43 commit cc85431

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ pushed regardless of the upstream state.
330330
even if the `branch` differs from the `branch` specified in the source.
331331
To avoid this, you should use two resources of read-only and write-only.
332332

333+
* `refs_prefix`: *Optional.* Allows pushing to refs other than heads. Defaults to `refs/heads`.
334+
333335
## Development
334336

335337
### Prerequisites

assets/out

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ only_tag=$(jq -r '.params.only_tag // false' <<< "$payload")
3939
annotation_file=$(jq -r '.params.annotate // ""' <<< "$payload")
4040
notes_file=$(jq -r '.params.notes // ""' <<< "$payload")
4141
override_branch=$(jq -r '.params.branch // ""' <<< "$payload")
42+
# useful for pushing to special ref types like refs/for in gerrit.
43+
refs_prefix=$(jq -r '.params.refs_prefix // "refs/heads"' <<< "$payload")
4244

4345
configure_git_global "${git_config_payload}"
4446

@@ -103,7 +105,7 @@ tag() {
103105
}
104106

105107
push_src_and_tags() {
106-
git push --tags push-target HEAD:refs/heads/$branch $forceflag
108+
git push --tags push-target HEAD:$refs_prefix/$branch $forceflag
107109
}
108110

109111
push_tags() {

test/helpers.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,3 +1238,16 @@ put_uri_with_config() {
12381238
}
12391239
}" | ${resource_dir}/out "$2" | tee /dev/stderr
12401240
}
1241+
1242+
put_uri_with_refs_prefix() {
1243+
jq -n "{
1244+
source: {
1245+
uri: $(echo $1 | jq -R .),
1246+
branch: \"master\"
1247+
},
1248+
params: {
1249+
repository: $(echo $3 | jq -R .),
1250+
refs_prefix: $(echo $4 | jq -R .),
1251+
}
1252+
}" | ${resource_dir}/out "$2" | tee /dev/stderr
1253+
}

test/put.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,29 @@ it_will_fail_put_with_conflicting_tag_and_not_force_push() {
585585
test "$(git -C $repo1 rev-parse some-only-tag)" = $expected_ref
586586
}
587587

588+
it_can_put_with_refs_prefix() {
589+
local repo1=$(init_repo)
590+
591+
local src=$(mktemp -d $TMPDIR/put-src.XXXXXX)
592+
local repo2=$src/repo
593+
git clone $repo1 $repo2
594+
595+
local ref=$(make_commit $repo2)
596+
597+
# cannot push to repo while it's checked out to a branch
598+
git -C $repo1 checkout refs/heads/master
599+
set -x
600+
put_uri_with_refs_prefix $repo1 $src repo refs/for | jq -e "
601+
.version == {ref: $(echo $ref | jq -R .)}
602+
"
603+
604+
# switch back to master
605+
git -C $repo1 checkout refs/for/master
606+
607+
test -e $repo1/some-file
608+
test "$(git -C $repo1 rev-parse HEAD)" = $ref
609+
}
610+
588611
run it_can_put_to_url
589612
run it_can_put_to_url_with_branch
590613
run it_returns_branch_in_metadata
@@ -605,3 +628,4 @@ run it_will_fail_put_if_conflicts_and_not_force_push
605628
run it_can_put_and_force_the_push
606629
run it_can_put_to_url_with_only_tag_and_force_the_push
607630
run it_will_fail_put_with_conflicting_tag_and_not_force_push
631+
run it_can_put_with_refs_prefix

0 commit comments

Comments
 (0)