Skip to content

Commit 1dcbac4

Browse files
committed
Implement an upgrade command
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 55f9940 commit 1dcbac4

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

README.markdown

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ git server:
100100
./vendor/vendorpull/pull my-dependency ../projects/my-dependency
101101
```
102102

103+
For convenience, you can also update `DEPENDENCIES` to bump a specific
104+
dependency to the latest commit hash or tag that corresponds to `HEAD` as
105+
follows:
106+
107+
```sh
108+
./vendor/vendorpull/upgrade my-dependency
109+
```
110+
103111
Updating
104112
--------
105113

upgrade

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/bin/sh
2+
3+
set -o errexit
4+
set -o nounset
5+
6+
ROOT="$(git rev-parse --show-toplevel)"
7+
DEPENDENCIES="$ROOT/DEPENDENCIES"
8+
9+
fail() {
10+
echo "$1" 1>&2
11+
exit 1
12+
}
13+
14+
log() {
15+
echo "-- $1" 1>&2
16+
}
17+
18+
if [ ! -f "$DEPENDENCIES" ]
19+
then
20+
fail "File not found: $DEPENDENCIES"
21+
fi
22+
23+
DEPENDENCY="${1-}"
24+
if [ -z "$DEPENDENCY" ]
25+
then
26+
fail "Usage: $0 <dependency>"
27+
fi
28+
29+
LINE="$(grep "^$DEPENDENCY " < "$DEPENDENCIES" || true)"
30+
if [ -z "$LINE" ]
31+
then
32+
fail "Unknown dependency: $DEPENDENCY"
33+
fi
34+
35+
NAME="$(echo "$LINE" | cut -d ' ' -f 1)"
36+
URL="$(echo "$LINE" | cut -d ' ' -f 2)"
37+
VERSION="$(echo "$LINE" | cut -d ' ' -f 3)"
38+
if [ -z "$NAME" ] || [ -z "$URL" ] || [ -z "$VERSION" ]
39+
then
40+
fail "Invalid dependency definition: $DEPENDENCY"
41+
fi
42+
43+
TMP="$(mktemp -d -t vendorpull-clone-XXXXX)"
44+
log "Setting up temporary directory at $TMP..."
45+
clean() { rm -rf "$TMP"; }
46+
trap clean EXIT
47+
48+
log "Fetching the tip of $URL into $TMP/$NAME"
49+
git clone --depth 1 --jobs 8 "$URL" "$TMP/$NAME"
50+
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)"
54+
55+
log "Upgrading $NAME to $NEW_VERSION"
56+
awk -v name="$NAME" -v version="$NEW_VERSION" \
57+
'$1 == name {$3 = version} {print}' \
58+
DEPENDENCIES > "$TMP/DEPENDENCIES"
59+
60+
mv "$TMP/DEPENDENCIES" "$DEPENDENCIES"
61+
git diff "$DEPENDENCIES"

0 commit comments

Comments
 (0)