Skip to content

Commit

Permalink
Merge pull request #225 from goddenrich/tags-across-all-branches
Browse files Browse the repository at this point in the history
searches all branches when tag filter is provided and branch isn't
  • Loading branch information
vito committed Dec 4, 2018
2 parents 607f337 + db3bdbc commit 57614ae
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 11 deletions.
22 changes: 19 additions & 3 deletions assets/check
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,22 @@ configure_git_global "${git_config_payload}"

destination=$TMPDIR/git-resource-repo-cache

tagflag=""
if [ -d $tag_filter ]; then
tagflag="--tags"
fi

if [ -d $destination ]; then
cd $destination
git fetch
git fetch $tagflag
git reset --hard FETCH_HEAD
else
branchflag=""
if [ -n "$branch" ]; then
branchflag="--branch $branch"
fi

git clone --single-branch $uri $branchflag $destination
git clone --single-branch $uri $branchflag $destination $tagflag
cd $destination
fi

Expand All @@ -70,10 +75,21 @@ if [ "$skip_ci_disabled" != "true" ]; then
ci_skip="--grep \\[ci\\sskip\\] --grep \\[skip\\sci\\] --invert-grep"
fi

replace_escape_chars() {
sed -e 's/[]\/$*.^[]/\\&/g' <<< $1
}

lines_including_and_after() {
local escaped_string=$(replace_escape_chars $1)
sed -ne "/$escaped_string/,$ p"
}

if [ -n "$tag_filter" ]; then
{
if [ -n "$ref" ]; then
if [ -n "$ref" ] && [ -n "$branch" ]; then
git tag --list "$tag_filter" --sort=creatordate --contains $ref
elif [ -n "$ref" ]; then
git tag --list "$tag_filter" --list --sort=creatordate | lines_including_and_after $ref
else
git tag --list "$tag_filter" --sort=creatordate | tail -1
fi
Expand Down
5 changes: 3 additions & 2 deletions assets/deepen_shallow_clone_until_ref_is_found
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set -e
readonly max_depth=128

readonly ref="$1"
readonly tagflag="$2"

# the concourse_git_resource__depth variable is exported by the 'in' script

Expand Down Expand Up @@ -39,12 +40,12 @@ if [ "$concourse_git_resource__depth" -gt 0 ]; then

if [ "$depth" -gt "$max_depth" ]; then
echo "Reached depth threshold ${max_depth}, falling back to deep clone..."
git fetch --unshallow origin
git fetch --unshallow origin $tagflag

break
fi

echo "Deepening the shallow clone to depth ${depth}..."
git fetch --depth "$depth" origin
git fetch --depth "$depth" origin $tagflag
done
fi
14 changes: 10 additions & 4 deletions assets/in
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ submodule_recursive=$(jq -r '(.params.submodule_recursive // true)' < $payload)
submodule_remote=$(jq -r '(.params.submodule_remote // false)' < $payload)
commit_verification_key_ids=$(jq -r '(.source.commit_verification_key_ids // [])[]' < $payload)
commit_verification_keys=$(jq -r '(.source.commit_verification_keys // [])[]' < $payload)
tag_filter=$(jq -r '.source.tag_filter // ""' < $payload)
gpg_keyserver=$(jq -r '.source.gpg_keyserver // "hkp://ipv4.pool.sks-keyservers.net/"' < $payload)
disable_git_lfs=$(jq -r '(.params.disable_git_lfs // false)' < $payload)
clean_tags=$(jq -r '(.params.clean_tags // false)' < $payload)
Expand All @@ -59,22 +60,27 @@ fi

branchflag=""
if [ -n "$branch" ]; then
branchflag="--branch $branch"
branchflag="--single-branch --branch $branch"
fi

depthflag=""
if test "$depth" -gt 0 2> /dev/null; then
depthflag="--depth $depth"
fi

git clone --single-branch $depthflag $uri $branchflag $destination
tagflag=""
if [ -d $tag_filter ]; then
tagflag="--tags"
fi

git clone $depthflag $uri $branchflag $destination $tagflag

cd $destination

git fetch origin refs/notes/*:refs/notes/*
git fetch origin refs/notes/*:refs/notes/* $tagflag

concourse_git_resource__depth="$depth" \
"$bin_dir"/deepen_shallow_clone_until_ref_is_found "$ref"
"$bin_dir"/deepen_shallow_clone_until_ref_is_found "$ref" "$tagflag"

git checkout -q $ref

Expand Down
47 changes: 46 additions & 1 deletion test/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ it_can_check_from_head_only_fetching_single_branch() {

local cachedir="$TMPDIR/git-resource-repo-cache"

check_uri $repo | jq -e "
check_uri_with_branch $repo "master" | jq -e "
. == [{ref: $(echo $ref | jq -R .)}]
"

Expand Down Expand Up @@ -451,6 +451,49 @@ it_can_check_with_tag_filter_with_cursor() {
local ref12=$(make_annotated_tag $repo "3.0-production" "tag 6")
local ref13=$(make_commit $repo)

x=$(check_uri_with_tag_filter_from $repo "*-staging" "2.0-staging")
check_uri_with_tag_filter_from $repo "*-staging" "2.0-staging" | jq -e '
. == [{ref: "2.0-staging"}, {ref: "3.0-staging"}]
'
}

it_can_check_with_tag_filter_over_all_branches() {
local repo=$(init_repo)
local ref1=$(make_commit_to_branch $repo branch-a)
local ref2=$(make_annotated_tag $repo "1.0-staging" "a tag")
local ref3=$(make_commit_to_branch $repo branch-a)
local ref4=$(make_annotated_tag $repo "1.0-production" "another tag")
local ref5=$(make_commit_to_branch $repo branch-a)
local ref6=$(make_annotated_tag $repo "2.0-staging" "tag 3")
local ref7=$(make_commit_to_branch $repo branch-a)
local ref8=$(make_annotated_tag $repo "2.0-production" "tag 4")
local ref9=$(make_commit_to_branch $repo branch-a)
local ref10=$(make_annotated_tag $repo "3.0-staging" "tag 5")
local ref11=$(make_commit_to_branch $repo branch-a)
local ref12=$(make_annotated_tag $repo "3.0-production" "tag 6")
local ref13=$(make_commit_to_branch $repo branch-a)

check_uri_with_tag_filter $repo "*-staging" | jq -e '
. == [{ref: "3.0-staging"}]
'
}

it_can_check_with_tag_filter_over_all_branches_with_cursor() {
local repo=$(init_repo)
local ref1=$(make_commit_to_branch $repo branch-a)
local ref2=$(make_annotated_tag $repo "1.0-staging" "a tag")
local ref3=$(make_commit_to_branch $repo branch-a)
local ref4=$(make_annotated_tag $repo "1.0-production" "another tag")
local ref5=$(make_commit_to_branch $repo branch-a)
local ref6=$(make_annotated_tag $repo "2.0-staging" "tag 3")
local ref7=$(make_commit_to_branch $repo branch-a)
local ref8=$(make_annotated_tag $repo "2.0-production" "tag 4")
local ref9=$(make_commit_to_branch $repo branch-a)
local ref10=$(make_annotated_tag $repo "3.0-staging" "tag 5")
local ref11=$(make_commit_to_branch $repo branch-a)
local ref12=$(make_annotated_tag $repo "3.0-production" "tag 6")
local ref13=$(make_commit_to_branch $repo branch-a)

check_uri_with_tag_filter_from $repo "*-staging" "2.0-staging" | jq -e '
. == [{ref: "2.0-staging"}, {ref: "3.0-staging"}]
'
Expand Down Expand Up @@ -507,6 +550,8 @@ run it_clears_netrc_even_after_errors
run it_can_check_empty_commits
run it_can_check_with_tag_filter
run it_can_check_with_tag_filter_with_cursor
run it_can_check_with_tag_filter_over_all_branches
run it_can_check_with_tag_filter_over_all_branches_with_cursor
run it_can_check_with_tag_filter_with_bogus_ref
run it_can_check_from_head_only_fetching_single_branch
run it_can_check_and_set_git_config
Expand Down
2 changes: 1 addition & 1 deletion test/get.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ it_can_get_from_url_only_single_branch() {
local ref=$(make_commit $repo)
local dest=$TMPDIR/destination

get_uri $repo $dest | jq -e "
get_uri_with_branch $repo "master" $dest | jq -e "
.version == {ref: $(echo $ref | jq -R .)}
"

Expand Down
34 changes: 34 additions & 0 deletions test/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ check_uri() {
}" | ${resource_dir}/check | tee /dev/stderr
}

check_uri_with_branch() {
jq -n "{
source: {
uri: $(echo $1 | jq -R .),
branch: $(echo $2 | jq -R .)
}
}" | ${resource_dir}/check | tee /dev/stderr
}

get_initial_ref() {
local repo=$1

Expand Down Expand Up @@ -337,6 +346,19 @@ check_uri_with_tag_filter() {
}" | ${resource_dir}/check | tee /dev/stderr
}

check_uri_with_tag_filter_given_branch() {
local uri=$1
local tag_filter=$2
local branch=$3
jq -n "{
source: {
uri: $(echo $uri | jq -R .),
tag_filter: $(echo $tag_filter | jq -R .),
branch: $(echo $branch | jq -R .)
}
}" | ${resource_dir}/check | tee /dev/stderr
}

check_uri_with_tag_filter_from() {
local uri=$1
local tag_filter=$2
Expand Down Expand Up @@ -394,6 +416,18 @@ get_uri() {
}" | ${resource_dir}/in "$2" | tee /dev/stderr
}

get_uri_with_branch() {
jq -n "{
source: {
uri: $(echo $1 | jq -R .),
branch: $(echo $2 | jq -R .),
},
params: {
short_ref_format: \"test-%s\"
}
}" | ${resource_dir}/in "$3" | 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

0 comments on commit 57614ae

Please sign in to comment.