forked from concourse/git-resource
/
check
executable file
·103 lines (84 loc) · 2.53 KB
/
check
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/bash
# vim: set ft=sh
set -e
exec 3>&1 # make stdout available as fd 3 for the result
exec 1>&2 # redirect all output to stderr for logging
source $(dirname $0)/common.sh
# for jq
PATH=/usr/local/bin:$PATH
payload=$TMPDIR/git-resource-request
cat > $payload <&0
load_pubkey $payload
configure_https_tunnel $payload
configure_git_ssl_verification $payload
configure_credentials $payload
uri=$(jq -r '.source.uri // ""' < $payload)
branch=$(jq -r '.source.branch // ""' < $payload)
paths="$(jq -r '(.source.paths // ["."])[]' < $payload)" # those "'s are important
ignore_paths="$(jq -r '":!" + (.source.ignore_paths // [])[]' < $payload)" # these ones too
tag_filter=$(jq -r '.source.tag_filter // ""' < $payload)
git_config_payload=$(jq -r '.source.git_config // []' < $payload)
ref=$(jq -r '.version.ref // ""' < $payload)
skip_ci_disabled=$(jq -r '.source.disable_ci_skip // false' < $payload)
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 $tagflag
git reset --hard FETCH_HEAD
else
branchflag=""
if [ -n "$branch" ]; then
branchflag="--branch $branch"
fi
git clone --single-branch $uri $branchflag $destination $tagflag
cd $destination
fi
if [ -n "$ref" ] && git cat-file -e "$ref"; then
init_commit=$(git rev-list --max-parents=0 HEAD)
if [ "${ref}" = "${init_commit}" ]; then
log_range="--reverse HEAD"
else
log_range="--reverse ${ref}~1..HEAD"
fi
else
log_range="-1"
ref=""
fi
if [ "$paths" = "." ] && [ -z "$ignore_paths" ]; then
paths_search=""
else
paths_search="-- $paths $ignore_paths"
fi
ci_skip=""
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" ] && [ -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
} | jq -R '.' | jq -s "map({ref: .})" >&3
else
{
set -f
git log $ci_skip --format='%H' --first-parent $log_range $paths_search
set +f
} | jq -R '.' | jq -s "map({ref: .})" >&3
fi