[email protected] | c1200a8 | 2012-10-25 19:26:31 | [diff] [blame] | 1 | #!/bin/bash |
2 | |||||
[email protected] | 69aea16 | 2013-02-15 18:52:51 | [diff] [blame] | 3 | update_toplevel () { |
4 | # Don't "pull" if checkout is not on a named branch | ||||
5 | if test "$2" = "pull" && ( ! git symbolic-ref HEAD >/dev/null 2>/dev/null ); then | ||||
6 | first_args="$1 fetch" | ||||
7 | else | ||||
8 | first_args="$1 $2" | ||||
9 | fi | ||||
10 | shift 2 | ||||
11 | echo "[$solution] $first_args $@" 1>&2 | ||||
12 | $first_args $@ | sed "s/^/[$solution] /g" 1>&2 | ||||
13 | status=$? | ||||
14 | if [ "$status" -ne 0 ]; then | ||||
15 | exit $status | ||||
16 | fi | ||||
17 | } | ||||
18 | |||||
[email protected] | e5599b1 | 2012-12-28 23:14:18 | [diff] [blame] | 19 | set_target_os () { |
20 | # Get the os we're building for. On first run, this will be unset. | ||||
21 | target_os=$(git config target.os 2>/dev/null) | ||||
22 | if [ -z "$target_os" ]; then | ||||
23 | case $(uname -s) in | ||||
24 | Linux) target_os=unix ;; | ||||
25 | Darwin) target_os=mac ;; | ||||
26 | CYGWIN*|MINGW*) target_os=win ;; | ||||
27 | *) | ||||
28 | echo "[$solution] *** No target.os set in .git/config, and I can't" 1>&2 | ||||
29 | echo "[$solution] *** figure it out from 'uname -s'" 1>&2 | ||||
30 | exit 1 | ||||
31 | ;; | ||||
32 | esac | ||||
33 | git config target.os "$target_os" | ||||
34 | fi | ||||
35 | } | ||||
36 | |||||
37 | update_submodule_url () { | ||||
38 | # If the submodule's URL in .gitmodules has changed, propagate the new | ||||
39 | # new URL down. This is the same as `git submodule sync`, but we do it | ||||
40 | # this way because `git submodule sync` is absurdly slow. | ||||
41 | new_url=$(git config -f .gitmodules "submodule.$1.url" 2>/dev/null) | ||||
42 | old_url=$(git config "submodule.$1.url" 2>/dev/null) | ||||
43 | if [ -n "$old_url" -a "$new_url" != "$old_url" ]; then | ||||
44 | git config "submodule.$1.url" "$new_url" | ||||
45 | if [ -e "$1"/.git ]; then | ||||
46 | ( cd $submod && git config remote.origin.url "$new_url" ) | ||||
47 | fi | ||||
48 | fi | ||||
49 | } | ||||
50 | |||||
51 | process_submodule () { | ||||
52 | # Check whether this submodule should be ignored or updated. | ||||
53 | # If it's a new submodule, match the os specified in .gitmodules against | ||||
54 | # the os specified in .git/config. | ||||
55 | update_policy=$(git config --get "submodule.$1.update") | ||||
56 | if [ -z "$update_policy" ]; then | ||||
57 | submod_os=$(git config -f .gitmodules --get "submodule.$1.os") | ||||
58 | if [ -n "$submod_os" -a \ | ||||
59 | "$submod_os" != "all" -a \ | ||||
60 | "${submod_os/${target_os}/}" = "${submod_os}" ]; then | ||||
61 | update_policy=none | ||||
62 | else | ||||
63 | git submodule --quiet init "$1" | ||||
64 | update_policy=checkout | ||||
65 | fi | ||||
66 | git config "submodule.$1.update" $update_policy | ||||
67 | fi | ||||
[email protected] | 3113c8b | 2013-02-27 19:50:36 | [diff] [blame^] | 68 | ignore_policy=$(git config --get "submodule.$1.ignore") |
69 | if [ -z "$ignore_policy" ]; then | ||||
70 | git config "submodule.$1.ignore" all | ||||
71 | fi | ||||
[email protected] | e5599b1 | 2012-12-28 23:14:18 | [diff] [blame] | 72 | if [ "$update_policy" != "none" ]; then |
73 | update_submodule_url "$1" | ||||
74 | echo "$solution/$1" | ||||
75 | fi | ||||
76 | } | ||||
77 | |||||
[email protected] | c1200a8 | 2012-10-25 19:26:31 | [diff] [blame] | 78 | if [ -z "$*" ]; then |
79 | exit 0 | ||||
80 | fi | ||||
81 | set -o pipefail | ||||
82 | dir="$1" | ||||
83 | solution="${1%%/*}" | ||||
[email protected] | 0c16fe6 | 2013-02-22 19:28:23 | [diff] [blame] | 84 | cd "$solution" 1>/dev/null |
[email protected] | e5599b1 | 2012-12-28 23:14:18 | [diff] [blame] | 85 | |
[email protected] | c1200a8 | 2012-10-25 19:26:31 | [diff] [blame] | 86 | if [ "$solution" = "$1" ]; then |
[email protected] | d2d1861 | 2013-01-23 19:36:51 | [diff] [blame] | 87 | # Skip git checkouts not managed by crup. |
88 | if ! grep -q -s "The Chromium Authors" ".git/description"; then | ||||
89 | echo "Skipping unmanaged git directory $1" 1>&2 | ||||
90 | exit 0 | ||||
91 | fi | ||||
92 | |||||
[email protected] | 3113c8b | 2013-02-27 19:50:36 | [diff] [blame^] | 93 | # Set default behavior to ignore diffs in submodule checkouts |
94 | diff_policy=$(git config --get "diff.ignoreSubmodules") | ||||
95 | if [ -z "$diff_policy" ]; then | ||||
96 | git config diff.ignoreSubmodules all | ||||
97 | fi | ||||
98 | |||||
99 | # Don't "pull" if checkout is not on a named branch | ||||
[email protected] | e5599b1 | 2012-12-28 23:14:18 | [diff] [blame] | 100 | shift |
[email protected] | 69aea16 | 2013-02-15 18:52:51 | [diff] [blame] | 101 | if test $# -ne 0; then |
102 | update_toplevel "$@" | ||||
[email protected] | c1200a8 | 2012-10-25 19:26:31 | [diff] [blame] | 103 | fi |
[email protected] | e5599b1 | 2012-12-28 23:14:18 | [diff] [blame] | 104 | |
105 | set_target_os | ||||
106 | |||||
[email protected] | c1200a8 | 2012-10-25 19:26:31 | [diff] [blame] | 107 | "$GIT_EXE" ls-files -s | grep ^160000 | awk '{print $4}' | |
[email protected] | a79f5cf | 2012-12-20 23:57:46 | [diff] [blame] | 108 | while read submod; do |
[email protected] | e5599b1 | 2012-12-28 23:14:18 | [diff] [blame] | 109 | process_submodule "$submod" |
[email protected] | a79f5cf | 2012-12-20 23:57:46 | [diff] [blame] | 110 | done |
[email protected] | c1200a8 | 2012-10-25 19:26:31 | [diff] [blame] | 111 | status=$? |
112 | else | ||||
113 | submodule="${1#*/}" | ||||
[email protected] | e5599b1 | 2012-12-28 23:14:18 | [diff] [blame] | 114 | echo "[$solution] updating $submodule" |
115 | "$GIT_EXE" submodule update --quiet "$submodule" | | ||||
[email protected] | c1200a8 | 2012-10-25 19:26:31 | [diff] [blame] | 116 | ( grep -v '^Skipping submodule' || true ) | sed "s|^|[$1] |g" |
117 | status=$? | ||||
118 | if [ "$status" -ne "0" ]; then | ||||
119 | echo "[$solution] FAILED to update $submodule" | ||||
120 | fi | ||||
121 | fi | ||||
122 | exit $status |