Skip to content

Commit 92f56fa

Browse files
feat(in): use a custom in script to clone the required commit only
cut release 2.0.0-beta1
1 parent 3afadf2 commit 92f56fa

File tree

10 files changed

+123
-67
lines changed

10 files changed

+123
-67
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# Release Notes
22

3+
## 2.0.0-beta1
4+
5+
- Use a custom `in` script instead of the script provided by the embedded [git-resource](https://github.com/concourse/git-resource).
6+
This allows us to efficiently fetch shallow clones at exactly a specified
7+
revision without incurring needless `git fetch --deepen` roundtrips.
8+
9+
Note: this release can break configuration for the `get` step in your pipelines as
10+
it removes configuration options inherited from `git-resource` that don't make
11+
sense in the context of gate-resource anymore. This helps to achieve better `get` performance and is critical for large gate repository.
12+
13+
Please review the [README](./README.md) for updated configuration options.
14+
315
## 1.1.1
416

517
- Patch an issue with shallow-clones not fetching at the correct depth in git-resource.

Dockerfile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ FROM concourse/git-resource:1.7.0 as resource
22

33
RUN mv /opt/resource /opt/git-resource
44

5-
# patch the git-resource deepen script with our fixes, see https://github.com/concourse/git-resource/pull/316
6-
COPY git-resource/deepen_shallow_clone_until_ref_is_found_then_check_out /opt/git-resource/deepen_shallow_clone_until_ref_is_found_then_check_out
7-
85
ADD assets/ /opt/resource/
96
RUN chmod +x /opt/resource/*
107

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
user=meshcloud
22
name=gate-resource
33
image=$(user)/$(name)
4-
tag=1.1.1
4+
tag=2.0.0-beta1
55

66
docker=docker
77
dockerfile = Dockerfile

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ Since concourse detects that the `none` version already exists after the first t
5656

5757
## Source Configuration
5858

59-
* `git`: *Required.* Configuration of the repository. See [git-resource](https://github.com/concourse/git-resource) for options.
60-
* **note:** the `paths` and `ignore_path` parameters are not supported
61-
59+
* `git`: *Required.* Configuration of the repository. Supports the following subset of [git-resource](https://github.com/concourse/git-resource) configuration options (review the link for descriptions)
60+
* *Required*: `uri`, `branch`, `private_key`
61+
* *Optional*: `git_config`, `short_ref_format`,
6262
* `gate`: *Optional.* The gate to track.
6363

6464
## Behavior
@@ -77,6 +77,9 @@ Outputs 2 files:
7777
* `metadata`: Contains the contents of whatever was in your gate item. This is
7878
useful for environment configuration settings or documenting approval workflows.
7979

80+
> note: the git repository cloned during `in` is a shallow clone and does not track an upstream branch.
81+
> This is not a problem when using the `out` step to create or update gates.
82+
8083
### `out`: Pass an item through a gate
8184

8285
Performs one of the following actions to change the state of a gate.

assets/git-in

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/bin/bash
2+
# vim: set ft=sh
3+
4+
set -e
5+
6+
exec 3>&1 # make stdout available as fd 3 for the result
7+
exec 1>&2 # redirect all output to stderr for logging
8+
9+
source /opt/git-resource/common.sh
10+
11+
destination=$1
12+
13+
if [ -z "$destination" ]; then
14+
echo "usage: $0 <path/to/destination>" >&2
15+
exit 1
16+
fi
17+
18+
# for jq
19+
PATH=/usr/local/bin:$PATH
20+
21+
bin_dir="${0%/*}"
22+
if [ "${bin_dir#/}" == "$bin_dir" ]; then
23+
bin_dir="$PWD/$bin_dir"
24+
fi
25+
26+
payload=$(mktemp $TMPDIR/git-resource-request.XXXXXX)
27+
28+
cat > $payload <&0
29+
30+
load_pubkey $payload
31+
configure_https_tunnel $payload
32+
configure_git_ssl_verification $payload
33+
configure_credentials $payload
34+
35+
uri=$(jq -r '.source.uri // ""' < $payload)
36+
git_config_payload=$(jq -r '.source.git_config // []' < $payload)
37+
ref=$(jq -r '.version.ref // "HEAD"' < $payload)
38+
depth=$(jq -r '(.params.depth // 0)' < $payload)
39+
short_ref_format=$(jq -r '(.params.short_ref_format // "%s")' < $payload)
40+
41+
configure_git_global "${git_config_payload}"
42+
43+
if [ -z "$uri" ]; then
44+
echo "invalid payload (missing uri):" >&2
45+
cat $payload >&2
46+
exit 1
47+
fi
48+
49+
depthflag=""
50+
if test "$depth" -gt 0 2> /dev/null; then
51+
depthflag="--depth $depth"
52+
fi
53+
54+
git init $destination
55+
cd $destination
56+
57+
git remote add origin $uri
58+
git fetch origin "$ref" $depthflag
59+
60+
# this will set master to the current commit
61+
git reset --hard FETCH_HEAD
62+
63+
git log -1 --oneline
64+
git clean --force --force -d
65+
66+
if [ "$ref" == "HEAD" ]; then
67+
return_ref=$(git rev-parse HEAD)
68+
else
69+
return_ref=$ref
70+
fi
71+
72+
# Store committer email in .git/committer. Can be used to send email to last committer on failed build
73+
# Using https://github.com/mdomke/concourse-email-resource for example
74+
git --no-pager log -1 --pretty=format:"%ae" > .git/committer
75+
76+
# Store git-resource returned version ref .git/ref. Useful to know concourse
77+
# pulled ref in following tasks and resources.
78+
echo "${return_ref}" > .git/ref
79+
80+
# Store short ref with templating. Useful to build Docker images with
81+
# a custom tag
82+
echo "${return_ref}" | cut -c1-7 | awk "{ printf \"${short_ref_format}\", \$1 }" > .git/short_ref
83+
84+
# Store commit message in .git/commit_message. Can be used to inform about
85+
# the content of a successfull build.
86+
# Using https://github.com/cloudfoundry-community/slack-notification-resource
87+
# for example
88+
git log -1 --format=format:%B > .git/commit_message
89+
90+
metadata=$(git_metadata)
91+
92+
jq -n "{
93+
version: {ref: $(echo $return_ref | jq -R .)},
94+
metadata: $metadata
95+
}" >&3

assets/in

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,12 @@ else
3535
"source": '$git_source',
3636
"version": '$version',
3737
"params": {
38-
"disable_git_lfs": true,
39-
"submodules": "none",
4038
"depth": 2
4139
}
4240
}' | jq -r)
4341

4442
# forward to git-resource to let it fetch the repository
45-
echo "$git_payload" | /opt/git-resource/in $destination
43+
echo "$git_payload" | /opt/resource/git-in $destination
4644

4745
cd $destination
4846

assets/out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ jq -n "{
3030
}" > $git_source_payload
3131

3232
load_pubkey $git_source_payload
33-
load_git_crypt_key $git_source_payload
3433
configure_https_tunnel $git_source_payload
3534
configure_git_ssl_verification $git_source_payload
3635
configure_credentials $git_source_payload
@@ -116,7 +115,8 @@ try_autoclose() {
116115
echo "$dashed_line"
117116
}
118117

119-
# clone the repository. by default we only need a single branch and a single
118+
# clone the repository at its current latest commit - we want to make more commits on top.
119+
# by default we only need to fetch a single branch and the latest commit
120120
repository=$(mktemp -d "$TMPDIR/gate-resource-repo.XXXXXX")
121121
git clone $uri --branch $branch $repository --depth=1
122122

git-resource/deepen_shallow_clone_until_ref_is_found_then_check_out

Lines changed: 0 additions & 54 deletions
This file was deleted.

test/helpers.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ get_gate_at_ref() {
148148
jq -n "{
149149
source: {
150150
git: {
151-
uri: $(echo file://$uriPath | jq -R .)
151+
uri: $(echo file://$uriPath | jq -R .),
152+
branch: \"master\"
152153
},
153154
gate: $(echo $gate | jq -R .)
154155
},

test/in.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ it_can_get_from_regular_version() {
1212

1313
local gate_ref=$(make_commit_to_file $repo "$gate/$item")
1414

15+
echo "xxxx"
16+
git -C "$repo" branch -a
17+
echo "xxxx"
18+
1519
result=$(get_gate_at_ref "$repo" "$gate_ref" "$gate" "$dest")
1620
echo "$result" | jq -e '
1721
.version == { "ref": "'$gate_ref'" }

0 commit comments

Comments
 (0)