Skip to content

Commit 5c93484

Browse files
authored
Merge pull request #372 from jamie-pate/git-gerrit
Add support for pulling from a previously pushed gerrit review commit
2 parents 2d4422f + f103729 commit 5c93484

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ Tracks the commits in a [git](http://git-scm.com/) repository.
121121

122122
* `version_depth`: *Optional.* The number of versions to return when performing a check
123123

124+
* `search_remote_refs`: *Optional.* True to search remote refs for the input version when checking out during the get step.
125+
This can be useful during the `get` step after a `put` step for unconventional workflows. One example workflow is the
126+
`refs/for/<branch>` workflow used by gerrit which 'magically' creates a `refs/changes/nnn` reference instead
127+
of the straight forward `refs/for/<branch>` reference that a git remote would usually create.
128+
See also `out params.refs_prefix`.
129+
124130
### Example
125131

126132
Resource configuration for a private repo with an HTTPS proxy:
@@ -332,6 +338,9 @@ pushed regardless of the upstream state.
332338

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

341+
Useful when paired with `source.search_remote_refs` in cases where the git remote
342+
renames the ref you pushed.
343+
335344
## Development
336345

337346
### Prerequisites

assets/in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ clean_tags=$(jq -r '(.params.clean_tags // false)' <<< "$payload")
5252
short_ref_format=$(jq -r '(.params.short_ref_format // "%s")' <<< "$payload")
5353
timestamp_format=$(jq -r '(.params.timestamp_format // "iso8601")' <<< "$payload")
5454
describe_ref_options=$(jq -r '(.params.describe_ref_options // "--always --dirty --broken")' <<< "$payload")
55+
search_remote_refs_flag=$(jq -r '(.source.search_remote_refs // false)' <<< "$payload")
5556

5657
# If params not defined, get it from source
5758
if [ -z "$fetch_tags" ] || [ "$fetch_tags" == "null" ] ; then
@@ -102,6 +103,15 @@ git fetch origin refs/notes/*:refs/notes/* $tagflag
102103
if [ "$depth" -gt 0 ]; then
103104
"$bin_dir"/deepen_shallow_clone_until_ref_is_found_then_check_out "$depth" "$ref" "$tagflag"
104105
else
106+
if [ "$search_remote_refs_flag" == "true" ] && ! [ -z "$branchflag" ] && ! git rev-list -1 $ref 2> /dev/null > /dev/null; then
107+
change_ref=$(git ls-remote origin | grep $ref | cut -f2)
108+
if ! [ -z "$change_ref" ]; then
109+
echo "$ref not found locally, but search_remote_refs is enabled. Attempting to fetch $change_ref first."
110+
git fetch origin $change_ref
111+
else
112+
echo "WARNING: couldn't find a ref for $ref listed on the remote"
113+
fi
114+
fi
105115
git checkout -q "$ref"
106116
fi
107117

test/get.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,38 @@ it_returns_list_of_all_tags_in_metadata() {
850850
"
851851
}
852852

853+
it_can_get_from_url_at_branch_with_search_remote_refs() {
854+
local repo=$(init_repo)
855+
local ref1=$(make_commit_to_branch $repo branch-a)
856+
local ref2=$(make_commit_to_branch $repo branch-a)
857+
git -C $repo update-ref refs/heads/branch-a $ref1
858+
git -C $repo update-ref refs/changes/1 $ref2
859+
git -C $repo log --all --oneline
860+
git -C $repo branch -v
861+
local dest=$TMPDIR/destination
862+
863+
# use file:// repo to force the regular git transport instead of local copying
864+
set +e
865+
output=$(get_uri_at_branch_with_ref file://$repo "branch-a" $ref2 $dest 2>&1)
866+
exit_code=$?
867+
set -e
868+
869+
echo $output $exit_code
870+
test "${exit_code}" = 128
871+
echo "$output" | grep "fatal: reference is not a tree: "
872+
test -e $dest/some-file
873+
test "$(git -C $dest rev-parse HEAD)" != $ref2
874+
875+
rm -rf $dest
876+
877+
get_uri_at_branch_with_search_remote_refs file://$repo "branch-a" $ref2 $dest | jq -e "
878+
.version == {ref: $(echo $ref2 | jq -R .)}
879+
"
880+
881+
test -e $dest/some-file
882+
test "$(git -C $dest rev-parse HEAD)" = $ref2
883+
}
884+
853885
run it_can_use_submodules_with_missing_paths
854886
run it_can_use_submodules_with_names_that_arent_paths
855887
run it_can_use_submodules_without_perl_warning
@@ -892,3 +924,4 @@ run it_retains_tags_by_default
892924
run it_retains_tags_with_clean_tags_param
893925
run it_returns_list_without_tags_in_metadata
894926
run it_returns_list_of_all_tags_in_metadata
927+
run it_can_get_from_url_at_branch_with_search_remote_refs

test/helpers.sh

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,31 @@ get_uri_at_branch_with_fetch_tags() {
860860
}" | ${resource_dir}/in "$3" | tee /dev/stderr
861861
}
862862

863+
get_uri_at_branch_with_ref() {
864+
jq -n "{
865+
source: {
866+
uri: $(echo $1 | jq -R .),
867+
branch: $(echo $2 | jq -R .)
868+
},
869+
version: {
870+
ref: $(echo $3 | jq -R .)
871+
}
872+
}" | ${resource_dir}/in "$4" | tee /dev/stderr
873+
}
874+
875+
get_uri_at_branch_with_search_remote_refs() {
876+
jq -n "{
877+
source: {
878+
uri: $(echo $1 | jq -R .),
879+
branch: $(echo $2 | jq -R .),
880+
search_remote_refs: true
881+
},
882+
version: {
883+
ref: $(echo $3 | jq -R .)
884+
}
885+
}" | ${resource_dir}/in "$4" | tee /dev/stderr
886+
}
887+
863888
get_uri_with_config() {
864889
jq -n "{
865890
source: {
@@ -1250,4 +1275,4 @@ put_uri_with_refs_prefix() {
12501275
refs_prefix: $(echo $4 | jq -R .),
12511276
}
12521277
}" | ${resource_dir}/out "$2" | tee /dev/stderr
1253-
}
1278+
}

0 commit comments

Comments
 (0)