[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. | ||||
[email protected] | 547c64b | 2013-04-12 10:06:47 | [diff] [blame] | 21 | target_os=$(git config --get-all target.os 2>/dev/null) |
[email protected] | e5599b1 | 2012-12-28 23:14:18 | [diff] [blame] | 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") | ||||
[email protected] | 547c64b | 2013-04-12 10:06:47 | [diff] [blame] | 58 | if [ -n "$submod_os" -a "$submod_os" != "all" ]; then |
[email protected] | e5599b1 | 2012-12-28 23:14:18 | [diff] [blame] | 59 | update_policy=none |
[email protected] | 547c64b | 2013-04-12 10:06:47 | [diff] [blame] | 60 | for os in $target_os; do |
61 | if [ "${submod_os/${os}/}" != "${submod_os}" ]; then | ||||
62 | update_policy=checkout | ||||
63 | fi | ||||
64 | done | ||||
[email protected] | e5599b1 | 2012-12-28 23:14:18 | [diff] [blame] | 65 | else |
[email protected] | e5599b1 | 2012-12-28 23:14:18 | [diff] [blame] | 66 | update_policy=checkout |
67 | fi | ||||
[email protected] | 547c64b | 2013-04-12 10:06:47 | [diff] [blame] | 68 | if [ "$update_policy" != "none" ]; then |
69 | git submodule --quiet init "$1" | ||||
70 | fi | ||||
[email protected] | e5599b1 | 2012-12-28 23:14:18 | [diff] [blame] | 71 | git config "submodule.$1.update" $update_policy |
72 | fi | ||||
[email protected] | 3113c8b | 2013-02-27 19:50:36 | [diff] [blame] | 73 | ignore_policy=$(git config --get "submodule.$1.ignore") |
74 | if [ -z "$ignore_policy" ]; then | ||||
75 | git config "submodule.$1.ignore" all | ||||
76 | fi | ||||
[email protected] | e5599b1 | 2012-12-28 23:14:18 | [diff] [blame] | 77 | if [ "$update_policy" != "none" ]; then |
78 | update_submodule_url "$1" | ||||
79 | echo "$solution/$1" | ||||
80 | fi | ||||
81 | } | ||||
82 | |||||
[email protected] | c1200a8 | 2012-10-25 19:26:31 | [diff] [blame] | 83 | if [ -z "$*" ]; then |
84 | exit 0 | ||||
85 | fi | ||||
86 | set -o pipefail | ||||
87 | dir="$1" | ||||
88 | solution="${1%%/*}" | ||||
[email protected] | 0c16fe6 | 2013-02-22 19:28:23 | [diff] [blame] | 89 | cd "$solution" 1>/dev/null |
[email protected] | e5599b1 | 2012-12-28 23:14:18 | [diff] [blame] | 90 | |
[email protected] | c1200a8 | 2012-10-25 19:26:31 | [diff] [blame] | 91 | if [ "$solution" = "$1" ]; then |
[email protected] | d2d1861 | 2013-01-23 19:36:51 | [diff] [blame] | 92 | # Skip git checkouts not managed by crup. |
93 | if ! grep -q -s "The Chromium Authors" ".git/description"; then | ||||
94 | echo "Skipping unmanaged git directory $1" 1>&2 | ||||
95 | exit 0 | ||||
96 | fi | ||||
97 | |||||
[email protected] | 3113c8b | 2013-02-27 19:50:36 | [diff] [blame] | 98 | # Set default behavior to ignore diffs in submodule checkouts |
99 | diff_policy=$(git config --get "diff.ignoreSubmodules") | ||||
100 | if [ -z "$diff_policy" ]; then | ||||
101 | git config diff.ignoreSubmodules all | ||||
102 | fi | ||||
103 | |||||
104 | # Don't "pull" if checkout is not on a named branch | ||||
[email protected] | e5599b1 | 2012-12-28 23:14:18 | [diff] [blame] | 105 | shift |
[email protected] | 69aea16 | 2013-02-15 18:52:51 | [diff] [blame] | 106 | if test $# -ne 0; then |
107 | update_toplevel "$@" | ||||
[email protected] | c1200a8 | 2012-10-25 19:26:31 | [diff] [blame] | 108 | fi |
[email protected] | e5599b1 | 2012-12-28 23:14:18 | [diff] [blame] | 109 | |
110 | set_target_os | ||||
111 | |||||
[email protected] | fdcecad | 2013-03-22 22:26:04 | [diff] [blame] | 112 | git ls-files -s | grep ^160000 | awk '{print $4}' | |
[email protected] | a79f5cf | 2012-12-20 23:57:46 | [diff] [blame] | 113 | while read submod; do |
[email protected] | e5599b1 | 2012-12-28 23:14:18 | [diff] [blame] | 114 | process_submodule "$submod" |
[email protected] | a79f5cf | 2012-12-20 23:57:46 | [diff] [blame] | 115 | done |
[email protected] | c1200a8 | 2012-10-25 19:26:31 | [diff] [blame] | 116 | status=$? |
117 | else | ||||
118 | submodule="${1#*/}" | ||||
[email protected] | e5599b1 | 2012-12-28 23:14:18 | [diff] [blame] | 119 | echo "[$solution] updating $submodule" |
[email protected] | fdcecad | 2013-03-22 22:26:04 | [diff] [blame] | 120 | git submodule update --recursive --quiet "$submodule" | |
121 | ( grep -v '^Skipping submodule' || true ) | sed "s|^|[$1] |g" 2>/dev/null | ||||
[email protected] | c1200a8 | 2012-10-25 19:26:31 | [diff] [blame] | 122 | status=$? |
123 | if [ "$status" -ne "0" ]; then | ||||
124 | echo "[$solution] FAILED to update $submodule" | ||||
125 | fi | ||||
126 | fi | ||||
127 | exit $status |