Skip to content

Commit 89f348a

Browse files
authored
Support vendoring plain files (#47)
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 1dcbac4 commit 89f348a

6 files changed

Lines changed: 117 additions & 13 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ jobs:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- uses: actions/checkout@v2
14-
- run: shellcheck pull bootstrap
14+
- run: shellcheck pull bootstrap upgrade

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.DS_Store
12
# For self-testing purposes
23
vendor
34
DEPENDENCIES

README.markdown

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,23 @@ from the `DEPENDENCIES` file and running the following command:
120120
./vendor/vendorpull/pull vendorpull
121121
```
122122

123+
File dependencies
124+
-----------------
125+
126+
Apart from `git` repositories, you can vendor plain files. In this case, the
127+
first column defines the file path inside the `vendor` directory, the second
128+
column defines the URL to download the file from, and the third column defines
129+
the SHA-256 checksum of the file contents. For example:
130+
131+
```
132+
oauth-parameters.csv https://www.iana.org/assignments/oauth-parameters/parameters.csv 6ae9ec171be0d232ee4e267a90d3adb301a75ceac74c2eb6478294139ec55d34
133+
```
134+
135+
Masking and patches do not apply to file dependencies.
136+
137+
Running the `upgrade` command on a file dependency downloads the current
138+
contents of the URL and updates the checksum accordingly.
139+
123140
Masking
124141
-------
125142

bootstrap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ROOT="$(git rev-parse --show-toplevel)"
77
DEPENDENCIES="$ROOT/DEPENDENCIES"
88
URL="https://github.com/sourcemeta/vendorpull"
99

10-
TMP="$(mktemp -d -t vendorpull-install-XXXXX)"
10+
TMP="$(mktemp -d -t vendorpull-install-XXXXXX)"
1111
clean() { rm -rf "$TMP"; }
1212
trap clean EXIT
1313

pull

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,27 @@ log() {
1717
echo "-- $1" 1>&2
1818
}
1919

20+
# $1 = version
21+
is_file_dependency() {
22+
echo "$1" | grep -Eq '^[a-f0-9]{64}$'
23+
}
24+
25+
# $1 = path
26+
checksum() {
27+
if command -v sha256sum > /dev/null
28+
then
29+
sha256sum "$1" | cut -d ' ' -f 1
30+
elif command -v shasum > /dev/null
31+
then
32+
shasum --algorithm 256 "$1" | cut -d ' ' -f 1
33+
elif command -v openssl > /dev/null
34+
then
35+
openssl dgst -sha256 "$1" | awk '{ print $NF }'
36+
else
37+
fail "Cannot find sha256sum, shasum, or openssl to compute checksums"
38+
fi
39+
}
40+
2041
# $1 = name
2142
# $2 = url
2243
# $3 = version
@@ -77,6 +98,32 @@ vendor() {
7798
mv "$4/$1" "$OUTPUT"
7899
}
79100

101+
# $1 = name
102+
# $2 = url
103+
# $3 = checksum
104+
# $4 = tmp
105+
vendor_file() {
106+
# Downloading
107+
log "Downloading $2 into $4/$1"
108+
mkdir -p "$(dirname "$4/$1")"
109+
curl --fail --silent --show-error --location --output "$4/$1" "$2"
110+
111+
# Verification
112+
ACTUAL_CHECKSUM="$(checksum "$4/$1")"
113+
if [ "$ACTUAL_CHECKSUM" != "$3" ]
114+
then
115+
fail "Checksum mismatch for $1: expected $3 but got $ACTUAL_CHECKSUM"
116+
fi
117+
118+
OUTPUT="$VENDOR/$1"
119+
120+
# Swap
121+
log "Moving $4/$1 to $OUTPUT"
122+
rm -rf "$OUTPUT"
123+
mkdir -p "$(dirname "$OUTPUT")"
124+
mv "$4/$1" "$OUTPUT"
125+
}
126+
80127
if [ ! -f "$DEPENDENCIES" ]
81128
then
82129
fail "File not found: $DEPENDENCIES"
@@ -106,14 +153,19 @@ then
106153
fail "Invalid dependency definition: $DEPENDENCY"
107154
fi
108155

109-
TMP="$(mktemp -d -t vendorpull-clone-XXXXX)"
156+
TMP="$(mktemp -d -t vendorpull-clone-XXXXXX)"
110157
log "Setting up temporary directory at $TMP..."
111158
clean() { rm -rf "$TMP"; }
112159
trap clean EXIT
113160

114-
vendor "$NAME" "$URL" "$VERSION" "$TMP"
161+
if is_file_dependency "$VERSION"
162+
then
163+
vendor_file "$NAME" "$URL" "$VERSION" "$TMP"
164+
else
165+
vendor "$NAME" "$URL" "$VERSION" "$TMP"
166+
fi
115167
else
116-
TMP="$(mktemp -d -t vendorpull-clone-XXXXX)"
168+
TMP="$(mktemp -d -t vendorpull-clone-XXXXXX)"
117169
log "Setting up temporary directory at $TMP..."
118170
clean() { rm -rf "$TMP"; }
119171
trap clean EXIT
@@ -128,6 +180,11 @@ else
128180
fail "Invalid dependency definition"
129181
fi
130182

131-
vendor "$NAME" "$URL" "$VERSION" "$TMP"
183+
if is_file_dependency "$VERSION"
184+
then
185+
vendor_file "$NAME" "$URL" "$VERSION" "$TMP"
186+
else
187+
vendor "$NAME" "$URL" "$VERSION" "$TMP"
188+
fi
132189
done < "$DEPENDENCIES"
133190
fi

upgrade

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,27 @@ log() {
1515
echo "-- $1" 1>&2
1616
}
1717

18+
# $1 = version
19+
is_file_dependency() {
20+
echo "$1" | grep -Eq '^[a-f0-9]{64}$'
21+
}
22+
23+
# $1 = path
24+
checksum() {
25+
if command -v sha256sum > /dev/null
26+
then
27+
sha256sum "$1" | cut -d ' ' -f 1
28+
elif command -v shasum > /dev/null
29+
then
30+
shasum --algorithm 256 "$1" | cut -d ' ' -f 1
31+
elif command -v openssl > /dev/null
32+
then
33+
openssl dgst -sha256 "$1" | awk '{ print $NF }'
34+
else
35+
fail "Cannot find sha256sum, shasum, or openssl to compute checksums"
36+
fi
37+
}
38+
1839
if [ ! -f "$DEPENDENCIES" ]
1940
then
2041
fail "File not found: $DEPENDENCIES"
@@ -40,22 +61,30 @@ then
4061
fail "Invalid dependency definition: $DEPENDENCY"
4162
fi
4263

43-
TMP="$(mktemp -d -t vendorpull-clone-XXXXX)"
64+
TMP="$(mktemp -d -t vendorpull-clone-XXXXXX)"
4465
log "Setting up temporary directory at $TMP..."
4566
clean() { rm -rf "$TMP"; }
4667
trap clean EXIT
4768

48-
log "Fetching the tip of $URL into $TMP/$NAME"
49-
git clone --depth 1 --jobs 8 "$URL" "$TMP/$NAME"
69+
if is_file_dependency "$VERSION"
70+
then
71+
log "Downloading $URL into $TMP/$NAME"
72+
mkdir -p "$(dirname "$TMP/$NAME")"
73+
curl --fail --silent --show-error --location --output "$TMP/$NAME" "$URL"
74+
NEW_VERSION="$(checksum "$TMP/$NAME")"
75+
else
76+
log "Fetching the tip of $URL into $TMP/$NAME"
77+
git clone --depth 1 --jobs 8 "$URL" "$TMP/$NAME"
5078

51-
# Try to determine the tag, otherwise the commit hash
52-
NEW_VERSION="$(git -C "$TMP/$NAME" describe --tags --exact-match HEAD 2>/dev/null \
53-
|| git -C "$TMP/$NAME" rev-parse HEAD)"
79+
# Try to determine the tag, otherwise the commit hash
80+
NEW_VERSION="$(git -C "$TMP/$NAME" describe --tags --exact-match HEAD 2>/dev/null \
81+
|| git -C "$TMP/$NAME" rev-parse HEAD)"
82+
fi
5483

5584
log "Upgrading $NAME to $NEW_VERSION"
5685
awk -v name="$NAME" -v version="$NEW_VERSION" \
5786
'$1 == name {$3 = version} {print}' \
58-
DEPENDENCIES > "$TMP/DEPENDENCIES"
87+
"$DEPENDENCIES" > "$TMP/DEPENDENCIES"
5988

6089
mv "$TMP/DEPENDENCIES" "$DEPENDENCIES"
6190
git diff "$DEPENDENCIES"

0 commit comments

Comments
 (0)