vendor-update.fish

 1#!/usr/bin/env fish
 2#
 3# Update git-subtree-vendored repositories listed in vendor/sources.
 4#
 5# vendor/sources format (tab-separated):
 6#   name	url	branch
 7#
 8# Lines starting with # are ignored.
 9#
10# Usage:
11#   scripts/vendor-update.fish           # update all vendored repos
12#   scripts/vendor-update.fish forge     # update only vendor/forge
13
14set repo_root (git -C (status dirname) rev-parse --show-toplevel)
15
16if test $status -ne 0
17    echo "error: not inside a git repository" >&2
18    exit 1
19end
20
21cd $repo_root
22
23set sources_file vendor/sources
24
25if not test -f $sources_file
26    echo "error: $sources_file not found" >&2
27    exit 1
28end
29
30set filter $argv
31set failed 0
32set found 0
33
34while read -l line
35    # Skip comments and blank lines
36    string match -qr '^\s*#' -- $line; and continue
37    string match -qr '^\s*$' -- $line; and continue
38
39    set parts (string split \t -- $line)
40    if test (count $parts) -ne 3
41        echo "skip: malformed line: $line" >&2
42        set failed (math $failed + 1)
43        continue
44    end
45
46    set name $parts[1]
47    set url $parts[2]
48    set branch $parts[3]
49
50    # If specific targets were given, skip non-matching entries
51    if test (count $filter) -gt 0
52        contains $name $filter; or continue
53    end
54
55    set found (math $found + 1)
56
57    if not test -d vendor/$name
58        echo "skip: vendor/$name does not exist (run 'git subtree add' first)" >&2
59        set failed (math $failed + 1)
60        continue
61    end
62
63    echo ":: updating vendor/$name from $url ($branch)"
64    git subtree pull --prefix=vendor/$name $url $branch --squash
65    if test $status -ne 0
66        echo "error: failed to update vendor/$name" >&2
67        set failed (math $failed + 1)
68    else
69        echo ":: vendor/$name updated"
70    end
71    echo
72end <$sources_file
73
74if test $found -eq 0
75    echo "No matching vendored repos found." >&2
76    exit 1
77end
78
79if test $failed -gt 0
80    echo "$failed vendor update(s) failed" >&2
81    exit 1
82end