Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
Sven Verdoolaege | ecda072 | 2007-06-24 23:06:07 +0200 | [diff] [blame] | 3 | # git-submodules.sh: add, init, update or list git submodules |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 4 | # |
| 5 | # Copyright (c) 2007 Lars Hjemli |
| 6 | |
Ping Yin | f2dc06a | 2008-03-11 21:52:17 +0800 | [diff] [blame] | 7 | USAGE="[--quiet] [--cached] \ |
Mark Levedahl | ec05df3 | 2008-07-09 21:05:40 -0400 | [diff] [blame] | 8 | [add <repo> [-b branch] <path>]|[status|init|update [-i|--init]|summary [-n|--summary-limit <n>] [<commit>]] \ |
David Aguilar | 2327f61 | 2008-08-24 12:43:37 -0700 | [diff] [blame] | 9 | [--] [<path>...]|[foreach <command>]|[sync [--] [<path>...]]" |
Junio C Hamano | 8f321a3 | 2007-11-06 01:50:02 -0800 | [diff] [blame] | 10 | OPTIONS_SPEC= |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 11 | . git-sh-setup |
Mark Levedahl | 98fcf84 | 2008-08-24 14:46:10 -0400 | [diff] [blame] | 12 | . git-parse-remote |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 13 | require_work_tree |
| 14 | |
Junio C Hamano | 5c08dbb | 2008-01-15 02:48:45 -0800 | [diff] [blame] | 15 | command= |
Sven Verdoolaege | ecda072 | 2007-06-24 23:06:07 +0200 | [diff] [blame] | 16 | branch= |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 17 | quiet= |
| 18 | cached= |
| 19 | |
| 20 | # |
| 21 | # print stuff on stdout unless -q was specified |
| 22 | # |
| 23 | say() |
| 24 | { |
| 25 | if test -z "$quiet" |
| 26 | then |
| 27 | echo "$@" |
| 28 | fi |
| 29 | } |
| 30 | |
Mark Levedahl | f31a522 | 2007-09-23 22:19:42 -0400 | [diff] [blame] | 31 | # Resolve relative url by appending to parent's url |
| 32 | resolve_relative_url () |
| 33 | { |
Mark Levedahl | 98fcf84 | 2008-08-24 14:46:10 -0400 | [diff] [blame] | 34 | remote=$(get_default_remote) |
Mark Levedahl | 8e7e6f3 | 2008-06-14 13:09:41 -0400 | [diff] [blame] | 35 | remoteurl=$(git config "remote.$remote.url") || |
| 36 | die "remote ($remote) does not have a url defined in .git/config" |
Mark Levedahl | f31a522 | 2007-09-23 22:19:42 -0400 | [diff] [blame] | 37 | url="$1" |
Mark Levedahl | 99b120a | 2008-08-21 19:54:01 -0400 | [diff] [blame] | 38 | remoteurl=${remoteurl%/} |
Mark Levedahl | f31a522 | 2007-09-23 22:19:42 -0400 | [diff] [blame] | 39 | while test -n "$url" |
| 40 | do |
| 41 | case "$url" in |
| 42 | ../*) |
| 43 | url="${url#../}" |
| 44 | remoteurl="${remoteurl%/*}" |
| 45 | ;; |
| 46 | ./*) |
| 47 | url="${url#./}" |
| 48 | ;; |
| 49 | *) |
| 50 | break;; |
| 51 | esac |
| 52 | done |
Mark Levedahl | 99b120a | 2008-08-21 19:54:01 -0400 | [diff] [blame] | 53 | echo "$remoteurl/${url%/}" |
Mark Levedahl | f31a522 | 2007-09-23 22:19:42 -0400 | [diff] [blame] | 54 | } |
| 55 | |
Lars Hjemli | 941987a | 2007-06-11 21:12:24 +0200 | [diff] [blame] | 56 | # |
David Aguilar | a7b3269 | 2008-08-22 00:30:50 -0700 | [diff] [blame] | 57 | # Get submodule info for registered submodules |
| 58 | # $@ = path to limit submodule list |
| 59 | # |
| 60 | module_list() |
| 61 | { |
| 62 | git ls-files --stage -- "$@" | grep '^160000 ' |
| 63 | } |
| 64 | |
| 65 | # |
Lars Hjemli | 941987a | 2007-06-11 21:12:24 +0200 | [diff] [blame] | 66 | # Map submodule path to submodule name |
| 67 | # |
| 68 | # $1 = path |
| 69 | # |
| 70 | module_name() |
| 71 | { |
Junio C Hamano | 537601a | 2007-07-25 15:51:26 -0700 | [diff] [blame] | 72 | # Do we have "submodule.<something>.path = $1" defined in .gitmodules file? |
Chris Ridd | fe22e54 | 2008-06-11 14:09:19 +0100 | [diff] [blame] | 73 | re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g') |
Imran M Yousuf | a5099bb | 2008-05-15 13:42:58 +0600 | [diff] [blame] | 74 | name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' | |
Junio C Hamano | 537601a | 2007-07-25 15:51:26 -0700 | [diff] [blame] | 75 | sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' ) |
Lars Hjemli | 941987a | 2007-06-11 21:12:24 +0200 | [diff] [blame] | 76 | test -z "$name" && |
| 77 | die "No submodule mapping found in .gitmodules for path '$path'" |
| 78 | echo "$name" |
| 79 | } |
Lars Hjemli | 33aa6ff | 2007-06-06 11:13:01 +0200 | [diff] [blame] | 80 | |
| 81 | # |
| 82 | # Clone a submodule |
| 83 | # |
Junio C Hamano | 23a485e | 2008-01-15 02:35:49 -0800 | [diff] [blame] | 84 | # Prior to calling, cmd_update checks that a possibly existing |
Sven Verdoolaege | ecda072 | 2007-06-24 23:06:07 +0200 | [diff] [blame] | 85 | # path is not a git repository. |
Junio C Hamano | 23a485e | 2008-01-15 02:35:49 -0800 | [diff] [blame] | 86 | # Likewise, cmd_add checks that path does not exist at all, |
Sven Verdoolaege | ecda072 | 2007-06-24 23:06:07 +0200 | [diff] [blame] | 87 | # since it is the location of a new submodule. |
| 88 | # |
Lars Hjemli | 33aa6ff | 2007-06-06 11:13:01 +0200 | [diff] [blame] | 89 | module_clone() |
| 90 | { |
| 91 | path=$1 |
| 92 | url=$2 |
| 93 | |
| 94 | # If there already is a directory at the submodule path, |
| 95 | # expect it to be empty (since that is the default checkout |
| 96 | # action) and try to remove it. |
| 97 | # Note: if $path is a symlink to a directory the test will |
| 98 | # succeed but the rmdir will fail. We might want to fix this. |
| 99 | if test -d "$path" |
| 100 | then |
| 101 | rmdir "$path" 2>/dev/null || |
| 102 | die "Directory '$path' exist, but is neither empty nor a git repository" |
| 103 | fi |
| 104 | |
| 105 | test -e "$path" && |
| 106 | die "A file already exist at path '$path'" |
| 107 | |
| 108 | git-clone -n "$url" "$path" || |
Lars Hjemli | 941987a | 2007-06-11 21:12:24 +0200 | [diff] [blame] | 109 | die "Clone of '$url' into submodule path '$path' failed" |
Lars Hjemli | 33aa6ff | 2007-06-06 11:13:01 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 112 | # |
Sven Verdoolaege | ecda072 | 2007-06-24 23:06:07 +0200 | [diff] [blame] | 113 | # Add a new submodule to the working tree, .gitmodules and the index |
| 114 | # |
Mark Levedahl | ec05df3 | 2008-07-09 21:05:40 -0400 | [diff] [blame] | 115 | # $@ = repo path |
Sven Verdoolaege | ecda072 | 2007-06-24 23:06:07 +0200 | [diff] [blame] | 116 | # |
| 117 | # optional branch is stored in global branch variable |
| 118 | # |
Junio C Hamano | 23a485e | 2008-01-15 02:35:49 -0800 | [diff] [blame] | 119 | cmd_add() |
Sven Verdoolaege | ecda072 | 2007-06-24 23:06:07 +0200 | [diff] [blame] | 120 | { |
Junio C Hamano | 5c08dbb | 2008-01-15 02:48:45 -0800 | [diff] [blame] | 121 | # parse $args after "submodule ... add". |
| 122 | while test $# -ne 0 |
| 123 | do |
| 124 | case "$1" in |
| 125 | -b | --branch) |
| 126 | case "$2" in '') usage ;; esac |
| 127 | branch=$2 |
| 128 | shift |
| 129 | ;; |
| 130 | -q|--quiet) |
| 131 | quiet=1 |
| 132 | ;; |
| 133 | --) |
| 134 | shift |
| 135 | break |
| 136 | ;; |
| 137 | -*) |
| 138 | usage |
| 139 | ;; |
| 140 | *) |
| 141 | break |
| 142 | ;; |
| 143 | esac |
| 144 | shift |
| 145 | done |
| 146 | |
Sven Verdoolaege | ecda072 | 2007-06-24 23:06:07 +0200 | [diff] [blame] | 147 | repo=$1 |
| 148 | path=$2 |
| 149 | |
Mark Levedahl | ec05df3 | 2008-07-09 21:05:40 -0400 | [diff] [blame] | 150 | if test -z "$repo" -o -z "$path"; then |
Sven Verdoolaege | ecda072 | 2007-06-24 23:06:07 +0200 | [diff] [blame] | 151 | usage |
| 152 | fi |
| 153 | |
Mark Levedahl | ec05df3 | 2008-07-09 21:05:40 -0400 | [diff] [blame] | 154 | # assure repo is absolute or relative to parent |
| 155 | case "$repo" in |
| 156 | ./*|../*) |
| 157 | # dereference source url relative to parent's url |
| 158 | realrepo=$(resolve_relative_url "$repo") || exit |
| 159 | ;; |
| 160 | *:*|/*) |
| 161 | # absolute url |
| 162 | realrepo=$repo |
| 163 | ;; |
| 164 | *) |
| 165 | die "repo URL: '$repo' must be absolute or begin with ./|../" |
| 166 | ;; |
| 167 | esac |
| 168 | |
| 169 | # strip trailing slashes from path |
| 170 | path=$(echo "$path" | sed -e 's|/*$||') |
Sven Verdoolaege | ecda072 | 2007-06-24 23:06:07 +0200 | [diff] [blame] | 171 | |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 172 | git ls-files --error-unmatch "$path" > /dev/null 2>&1 && |
Sven Verdoolaege | ecda072 | 2007-06-24 23:06:07 +0200 | [diff] [blame] | 173 | die "'$path' already exists in the index" |
| 174 | |
Mark Levedahl | d4264ca | 2008-03-04 20:15:02 -0500 | [diff] [blame] | 175 | # perhaps the path exists and is already a git repo, else clone it |
| 176 | if test -e "$path" |
| 177 | then |
Mark Levedahl | e965647 | 2008-07-07 22:36:40 -0400 | [diff] [blame] | 178 | if test -d "$path"/.git -o -f "$path"/.git |
Mark Levedahl | d4264ca | 2008-03-04 20:15:02 -0500 | [diff] [blame] | 179 | then |
| 180 | echo "Adding existing repo at '$path' to the index" |
| 181 | else |
| 182 | die "'$path' already exists and is not a valid git repo" |
| 183 | fi |
Mark Levedahl | c2f9391 | 2008-07-09 21:05:41 -0400 | [diff] [blame] | 184 | |
| 185 | case "$repo" in |
| 186 | ./*|../*) |
| 187 | url=$(resolve_relative_url "$repo") || exit |
| 188 | ;; |
| 189 | *) |
| 190 | url="$repo" |
| 191 | ;; |
| 192 | esac |
| 193 | git config submodule."$path".url "$url" |
Mark Levedahl | d4264ca | 2008-03-04 20:15:02 -0500 | [diff] [blame] | 194 | else |
Mark Levedahl | d4264ca | 2008-03-04 20:15:02 -0500 | [diff] [blame] | 195 | |
| 196 | module_clone "$path" "$realrepo" || exit |
Ping Yin | b9b378a | 2008-09-26 23:33:23 +0800 | [diff] [blame] | 197 | (unset GIT_DIR; cd "$path" && git checkout -f -q ${branch:+-b "$branch" "origin/$branch"}) || |
Mark Levedahl | d4264ca | 2008-03-04 20:15:02 -0500 | [diff] [blame] | 198 | die "Unable to checkout submodule '$path'" |
| 199 | fi |
| 200 | |
Sven Verdoolaege | ecda072 | 2007-06-24 23:06:07 +0200 | [diff] [blame] | 201 | git add "$path" || |
| 202 | die "Failed to add submodule '$path'" |
| 203 | |
Imran M Yousuf | a5099bb | 2008-05-15 13:42:58 +0600 | [diff] [blame] | 204 | git config -f .gitmodules submodule."$path".path "$path" && |
| 205 | git config -f .gitmodules submodule."$path".url "$repo" && |
Sven Verdoolaege | ecda072 | 2007-06-24 23:06:07 +0200 | [diff] [blame] | 206 | git add .gitmodules || |
| 207 | die "Failed to register submodule '$path'" |
| 208 | } |
| 209 | |
| 210 | # |
Mark Levedahl | 19a31f9 | 2008-08-10 19:10:04 -0400 | [diff] [blame] | 211 | # Execute an arbitrary command sequence in each checked out |
| 212 | # submodule |
| 213 | # |
| 214 | # $@ = command to execute |
| 215 | # |
| 216 | cmd_foreach() |
| 217 | { |
David Aguilar | a7b3269 | 2008-08-22 00:30:50 -0700 | [diff] [blame] | 218 | module_list | |
Mark Levedahl | 19a31f9 | 2008-08-10 19:10:04 -0400 | [diff] [blame] | 219 | while read mode sha1 stage path |
| 220 | do |
| 221 | if test -e "$path"/.git |
| 222 | then |
| 223 | say "Entering '$path'" |
| 224 | (cd "$path" && eval "$@") || |
| 225 | die "Stopping at '$path'; script returned non-zero status." |
| 226 | fi |
| 227 | done |
| 228 | } |
| 229 | |
| 230 | # |
Lars Hjemli | 211b7f1 | 2007-06-06 11:13:02 +0200 | [diff] [blame] | 231 | # Register submodules in .git/config |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 232 | # |
| 233 | # $@ = requested paths (default to all) |
| 234 | # |
Junio C Hamano | 23a485e | 2008-01-15 02:35:49 -0800 | [diff] [blame] | 235 | cmd_init() |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 236 | { |
Junio C Hamano | 5c08dbb | 2008-01-15 02:48:45 -0800 | [diff] [blame] | 237 | # parse $args after "submodule ... init". |
| 238 | while test $# -ne 0 |
| 239 | do |
| 240 | case "$1" in |
| 241 | -q|--quiet) |
| 242 | quiet=1 |
| 243 | ;; |
| 244 | --) |
| 245 | shift |
| 246 | break |
| 247 | ;; |
| 248 | -*) |
| 249 | usage |
| 250 | ;; |
| 251 | *) |
| 252 | break |
| 253 | ;; |
| 254 | esac |
| 255 | shift |
| 256 | done |
| 257 | |
David Aguilar | a7b3269 | 2008-08-22 00:30:50 -0700 | [diff] [blame] | 258 | module_list "$@" | |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 259 | while read mode sha1 stage path |
| 260 | do |
Lars Hjemli | 211b7f1 | 2007-06-06 11:13:02 +0200 | [diff] [blame] | 261 | # Skip already registered paths |
Lars Hjemli | 941987a | 2007-06-11 21:12:24 +0200 | [diff] [blame] | 262 | name=$(module_name "$path") || exit |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 263 | url=$(git config submodule."$name".url) |
Lars Hjemli | 211b7f1 | 2007-06-06 11:13:02 +0200 | [diff] [blame] | 264 | test -z "$url" || continue |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 265 | |
Imran M Yousuf | a5099bb | 2008-05-15 13:42:58 +0600 | [diff] [blame] | 266 | url=$(git config -f .gitmodules submodule."$name".url) |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 267 | test -z "$url" && |
Lars Hjemli | 941987a | 2007-06-11 21:12:24 +0200 | [diff] [blame] | 268 | die "No url found for submodule path '$path' in .gitmodules" |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 269 | |
Mark Levedahl | f31a522 | 2007-09-23 22:19:42 -0400 | [diff] [blame] | 270 | # Possibly a url relative to parent |
| 271 | case "$url" in |
| 272 | ./*|../*) |
Mark Levedahl | 8e7e6f3 | 2008-06-14 13:09:41 -0400 | [diff] [blame] | 273 | url=$(resolve_relative_url "$url") || exit |
Mark Levedahl | f31a522 | 2007-09-23 22:19:42 -0400 | [diff] [blame] | 274 | ;; |
| 275 | esac |
| 276 | |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 277 | git config submodule."$name".url "$url" || |
Lars Hjemli | 941987a | 2007-06-11 21:12:24 +0200 | [diff] [blame] | 278 | die "Failed to register url for submodule path '$path'" |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 279 | |
Lars Hjemli | 941987a | 2007-06-11 21:12:24 +0200 | [diff] [blame] | 280 | say "Submodule '$name' ($url) registered for path '$path'" |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 281 | done |
| 282 | } |
| 283 | |
| 284 | # |
Lars Hjemli | 211b7f1 | 2007-06-06 11:13:02 +0200 | [diff] [blame] | 285 | # Update each submodule path to correct revision, using clone and checkout as needed |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 286 | # |
| 287 | # $@ = requested paths (default to all) |
| 288 | # |
Junio C Hamano | 23a485e | 2008-01-15 02:35:49 -0800 | [diff] [blame] | 289 | cmd_update() |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 290 | { |
Junio C Hamano | 5c08dbb | 2008-01-15 02:48:45 -0800 | [diff] [blame] | 291 | # parse $args after "submodule ... update". |
| 292 | while test $# -ne 0 |
| 293 | do |
| 294 | case "$1" in |
| 295 | -q|--quiet) |
Pierre Habouzit | d1f63a3 | 2008-07-21 20:15:59 +0200 | [diff] [blame] | 296 | shift |
Junio C Hamano | 5c08dbb | 2008-01-15 02:48:45 -0800 | [diff] [blame] | 297 | quiet=1 |
| 298 | ;; |
Johannes Schindelin | be4d2c8 | 2008-05-16 11:23:03 +0100 | [diff] [blame] | 299 | -i|--init) |
| 300 | shift |
| 301 | cmd_init "$@" || return |
| 302 | ;; |
Junio C Hamano | 5c08dbb | 2008-01-15 02:48:45 -0800 | [diff] [blame] | 303 | --) |
| 304 | shift |
| 305 | break |
| 306 | ;; |
| 307 | -*) |
| 308 | usage |
| 309 | ;; |
| 310 | *) |
| 311 | break |
| 312 | ;; |
| 313 | esac |
Junio C Hamano | 5c08dbb | 2008-01-15 02:48:45 -0800 | [diff] [blame] | 314 | done |
| 315 | |
David Aguilar | a7b3269 | 2008-08-22 00:30:50 -0700 | [diff] [blame] | 316 | module_list "$@" | |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 317 | while read mode sha1 stage path |
| 318 | do |
Lars Hjemli | 941987a | 2007-06-11 21:12:24 +0200 | [diff] [blame] | 319 | name=$(module_name "$path") || exit |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 320 | url=$(git config submodule."$name".url) |
Lars Hjemli | 211b7f1 | 2007-06-06 11:13:02 +0200 | [diff] [blame] | 321 | if test -z "$url" |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 322 | then |
| 323 | # Only mention uninitialized submodules when its |
| 324 | # path have been specified |
| 325 | test "$#" != "0" && |
Alexandre Julliard | 989206f | 2008-11-11 22:09:16 +0100 | [diff] [blame] | 326 | say "Submodule path '$path' not initialized" && |
Johannes Schindelin | be4d2c8 | 2008-05-16 11:23:03 +0100 | [diff] [blame] | 327 | say "Maybe you want to use 'update --init'?" |
Lars Hjemli | 211b7f1 | 2007-06-06 11:13:02 +0200 | [diff] [blame] | 328 | continue |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 329 | fi |
Lars Hjemli | 211b7f1 | 2007-06-06 11:13:02 +0200 | [diff] [blame] | 330 | |
Lars Hjemli | ba88a1f | 2008-02-20 23:13:15 +0100 | [diff] [blame] | 331 | if ! test -d "$path"/.git -o -f "$path"/.git |
Lars Hjemli | 211b7f1 | 2007-06-06 11:13:02 +0200 | [diff] [blame] | 332 | then |
| 333 | module_clone "$path" "$url" || exit |
Lars Hjemli | bf2d824 | 2007-06-11 21:12:22 +0200 | [diff] [blame] | 334 | subsha1= |
| 335 | else |
H.Merijn Brand | 5188408 | 2007-12-04 22:45:16 +0000 | [diff] [blame] | 336 | subsha1=$(unset GIT_DIR; cd "$path" && |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 337 | git rev-parse --verify HEAD) || |
Lars Hjemli | 941987a | 2007-06-11 21:12:24 +0200 | [diff] [blame] | 338 | die "Unable to find current revision in submodule path '$path'" |
Lars Hjemli | 211b7f1 | 2007-06-06 11:13:02 +0200 | [diff] [blame] | 339 | fi |
| 340 | |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 341 | if test "$subsha1" != "$sha1" |
| 342 | then |
Ping Yin | b9b378a | 2008-09-26 23:33:23 +0800 | [diff] [blame] | 343 | force= |
| 344 | if test -z "$subsha1" |
| 345 | then |
| 346 | force="-f" |
| 347 | fi |
H.Merijn Brand | 5188408 | 2007-12-04 22:45:16 +0000 | [diff] [blame] | 348 | (unset GIT_DIR; cd "$path" && git-fetch && |
Ping Yin | b9b378a | 2008-09-26 23:33:23 +0800 | [diff] [blame] | 349 | git-checkout $force -q "$sha1") || |
Lars Hjemli | 941987a | 2007-06-11 21:12:24 +0200 | [diff] [blame] | 350 | die "Unable to checkout '$sha1' in submodule path '$path'" |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 351 | |
Lars Hjemli | 941987a | 2007-06-11 21:12:24 +0200 | [diff] [blame] | 352 | say "Submodule path '$path': checked out '$sha1'" |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 353 | fi |
| 354 | done |
| 355 | } |
| 356 | |
Emil Medve | bffe71f | 2007-06-26 18:40:58 -0500 | [diff] [blame] | 357 | set_name_rev () { |
| 358 | revname=$( ( |
H.Merijn Brand | 5188408 | 2007-12-04 22:45:16 +0000 | [diff] [blame] | 359 | unset GIT_DIR |
Emil Medve | bffe71f | 2007-06-26 18:40:58 -0500 | [diff] [blame] | 360 | cd "$1" && { |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 361 | git describe "$2" 2>/dev/null || |
| 362 | git describe --tags "$2" 2>/dev/null || |
Mark Levedahl | f669ac0 | 2008-04-14 22:48:06 -0400 | [diff] [blame] | 363 | git describe --contains "$2" 2>/dev/null || |
| 364 | git describe --all --always "$2" |
Emil Medve | bffe71f | 2007-06-26 18:40:58 -0500 | [diff] [blame] | 365 | } |
| 366 | ) ) |
| 367 | test -z "$revname" || revname=" ($revname)" |
| 368 | } |
Ping Yin | 28f9af5 | 2008-03-11 21:52:15 +0800 | [diff] [blame] | 369 | # |
| 370 | # Show commit summary for submodules in index or working tree |
| 371 | # |
| 372 | # If '--cached' is given, show summary between index and given commit, |
| 373 | # or between working tree and given commit |
| 374 | # |
| 375 | # $@ = [commit (default 'HEAD'),] requested paths (default all) |
| 376 | # |
| 377 | cmd_summary() { |
Ping Yin | f2dc06a | 2008-03-11 21:52:17 +0800 | [diff] [blame] | 378 | summary_limit=-1 |
Ping Yin | d0f64dd | 2008-04-12 23:05:31 +0800 | [diff] [blame] | 379 | for_status= |
Emil Medve | bffe71f | 2007-06-26 18:40:58 -0500 | [diff] [blame] | 380 | |
Ping Yin | 28f9af5 | 2008-03-11 21:52:15 +0800 | [diff] [blame] | 381 | # parse $args after "submodule ... summary". |
| 382 | while test $# -ne 0 |
| 383 | do |
| 384 | case "$1" in |
| 385 | --cached) |
| 386 | cached="$1" |
| 387 | ;; |
Ping Yin | d0f64dd | 2008-04-12 23:05:31 +0800 | [diff] [blame] | 388 | --for-status) |
| 389 | for_status="$1" |
| 390 | ;; |
Ping Yin | f2dc06a | 2008-03-11 21:52:17 +0800 | [diff] [blame] | 391 | -n|--summary-limit) |
| 392 | if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2" |
| 393 | then |
| 394 | : |
| 395 | else |
| 396 | usage |
| 397 | fi |
| 398 | shift |
| 399 | ;; |
Ping Yin | 28f9af5 | 2008-03-11 21:52:15 +0800 | [diff] [blame] | 400 | --) |
| 401 | shift |
| 402 | break |
| 403 | ;; |
| 404 | -*) |
| 405 | usage |
| 406 | ;; |
| 407 | *) |
| 408 | break |
| 409 | ;; |
| 410 | esac |
| 411 | shift |
| 412 | done |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 413 | |
Ping Yin | f2dc06a | 2008-03-11 21:52:17 +0800 | [diff] [blame] | 414 | test $summary_limit = 0 && return |
| 415 | |
Miklos Vajna | 30353a4 | 2008-12-03 14:26:52 +0100 | [diff] [blame] | 416 | if rev=$(git rev-parse -q --verify "$1^0") |
Ping Yin | 28f9af5 | 2008-03-11 21:52:15 +0800 | [diff] [blame] | 417 | then |
| 418 | head=$rev |
| 419 | shift |
| 420 | else |
| 421 | head=HEAD |
| 422 | fi |
| 423 | |
| 424 | cd_to_toplevel |
| 425 | # Get modified modules cared by user |
| 426 | modules=$(git diff-index $cached --raw $head -- "$@" | |
Jeff King | 759ad19 | 2008-10-22 15:22:53 -0400 | [diff] [blame] | 427 | egrep '^:([0-7]* )?160000' | |
Ping Yin | 28f9af5 | 2008-03-11 21:52:15 +0800 | [diff] [blame] | 428 | while read mod_src mod_dst sha1_src sha1_dst status name |
| 429 | do |
| 430 | # Always show modules deleted or type-changed (blob<->module) |
| 431 | test $status = D -o $status = T && echo "$name" && continue |
| 432 | # Also show added or modified modules which are checked out |
| 433 | GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 && |
| 434 | echo "$name" |
| 435 | done |
| 436 | ) |
Ping Yin | 1cb639e | 2008-03-11 21:52:16 +0800 | [diff] [blame] | 437 | |
Ping Yin | d0f64dd | 2008-04-12 23:05:31 +0800 | [diff] [blame] | 438 | test -z "$modules" && return |
| 439 | |
Ping Yin | 1cb639e | 2008-03-11 21:52:16 +0800 | [diff] [blame] | 440 | git diff-index $cached --raw $head -- $modules | |
Jeff King | 759ad19 | 2008-10-22 15:22:53 -0400 | [diff] [blame] | 441 | egrep '^:([0-7]* )?160000' | |
Ping Yin | 1cb639e | 2008-03-11 21:52:16 +0800 | [diff] [blame] | 442 | cut -c2- | |
| 443 | while read mod_src mod_dst sha1_src sha1_dst status name |
| 444 | do |
| 445 | if test -z "$cached" && |
| 446 | test $sha1_dst = 0000000000000000000000000000000000000000 |
| 447 | then |
| 448 | case "$mod_dst" in |
| 449 | 160000) |
| 450 | sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD) |
| 451 | ;; |
| 452 | 100644 | 100755 | 120000) |
| 453 | sha1_dst=$(git hash-object $name) |
| 454 | ;; |
| 455 | 000000) |
| 456 | ;; # removed |
| 457 | *) |
| 458 | # unexpected type |
| 459 | echo >&2 "unexpected mode $mod_dst" |
| 460 | continue ;; |
| 461 | esac |
| 462 | fi |
| 463 | missing_src= |
| 464 | missing_dst= |
| 465 | |
| 466 | test $mod_src = 160000 && |
Miklos Vajna | 30353a4 | 2008-12-03 14:26:52 +0100 | [diff] [blame] | 467 | ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null && |
Ping Yin | 1cb639e | 2008-03-11 21:52:16 +0800 | [diff] [blame] | 468 | missing_src=t |
| 469 | |
| 470 | test $mod_dst = 160000 && |
Miklos Vajna | 30353a4 | 2008-12-03 14:26:52 +0100 | [diff] [blame] | 471 | ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null && |
Ping Yin | 1cb639e | 2008-03-11 21:52:16 +0800 | [diff] [blame] | 472 | missing_dst=t |
| 473 | |
| 474 | total_commits= |
| 475 | case "$missing_src,$missing_dst" in |
| 476 | t,) |
| 477 | errmsg=" Warn: $name doesn't contain commit $sha1_src" |
| 478 | ;; |
| 479 | ,t) |
| 480 | errmsg=" Warn: $name doesn't contain commit $sha1_dst" |
| 481 | ;; |
| 482 | t,t) |
| 483 | errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst" |
| 484 | ;; |
| 485 | *) |
| 486 | errmsg= |
| 487 | total_commits=$( |
| 488 | if test $mod_src = 160000 -a $mod_dst = 160000 |
| 489 | then |
| 490 | range="$sha1_src...$sha1_dst" |
| 491 | elif test $mod_src = 160000 |
| 492 | then |
| 493 | range=$sha1_src |
| 494 | else |
| 495 | range=$sha1_dst |
| 496 | fi |
| 497 | GIT_DIR="$name/.git" \ |
| 498 | git log --pretty=oneline --first-parent $range | wc -l |
| 499 | ) |
Johannes Sixt | eed3559 | 2008-03-12 09:30:01 +0100 | [diff] [blame] | 500 | total_commits=" ($(($total_commits + 0)))" |
Ping Yin | 1cb639e | 2008-03-11 21:52:16 +0800 | [diff] [blame] | 501 | ;; |
| 502 | esac |
| 503 | |
| 504 | sha1_abbr_src=$(echo $sha1_src | cut -c1-7) |
| 505 | sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7) |
| 506 | if test $status = T |
| 507 | then |
| 508 | if test $mod_dst = 160000 |
| 509 | then |
| 510 | echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:" |
| 511 | else |
| 512 | echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:" |
| 513 | fi |
| 514 | else |
| 515 | echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:" |
| 516 | fi |
| 517 | if test -n "$errmsg" |
| 518 | then |
| 519 | # Don't give error msg for modification whose dst is not submodule |
| 520 | # i.e. deleted or changed to blob |
| 521 | test $mod_dst = 160000 && echo "$errmsg" |
| 522 | else |
| 523 | if test $mod_src = 160000 -a $mod_dst = 160000 |
| 524 | then |
Ping Yin | f2dc06a | 2008-03-11 21:52:17 +0800 | [diff] [blame] | 525 | limit= |
| 526 | test $summary_limit -gt 0 && limit="-$summary_limit" |
Ping Yin | 1cb639e | 2008-03-11 21:52:16 +0800 | [diff] [blame] | 527 | GIT_DIR="$name/.git" \ |
Ping Yin | f2dc06a | 2008-03-11 21:52:17 +0800 | [diff] [blame] | 528 | git log $limit --pretty='format: %m %s' \ |
Ping Yin | 1cb639e | 2008-03-11 21:52:16 +0800 | [diff] [blame] | 529 | --first-parent $sha1_src...$sha1_dst |
| 530 | elif test $mod_dst = 160000 |
| 531 | then |
| 532 | GIT_DIR="$name/.git" \ |
| 533 | git log --pretty='format: > %s' -1 $sha1_dst |
| 534 | else |
| 535 | GIT_DIR="$name/.git" \ |
| 536 | git log --pretty='format: < %s' -1 $sha1_src |
| 537 | fi |
| 538 | echo |
| 539 | fi |
| 540 | echo |
Ping Yin | d0f64dd | 2008-04-12 23:05:31 +0800 | [diff] [blame] | 541 | done | |
| 542 | if test -n "$for_status"; then |
| 543 | echo "# Modified submodules:" |
| 544 | echo "#" |
| 545 | sed -e 's|^|# |' -e 's|^# $|#|' |
| 546 | else |
| 547 | cat |
| 548 | fi |
Ping Yin | 28f9af5 | 2008-03-11 21:52:15 +0800 | [diff] [blame] | 549 | } |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 550 | # |
Lars Hjemli | 941987a | 2007-06-11 21:12:24 +0200 | [diff] [blame] | 551 | # List all submodules, prefixed with: |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 552 | # - submodule not initialized |
| 553 | # + different revision checked out |
| 554 | # |
| 555 | # If --cached was specified the revision in the index will be printed |
| 556 | # instead of the currently checked out revision. |
| 557 | # |
| 558 | # $@ = requested paths (default to all) |
| 559 | # |
Junio C Hamano | 23a485e | 2008-01-15 02:35:49 -0800 | [diff] [blame] | 560 | cmd_status() |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 561 | { |
Junio C Hamano | 5c08dbb | 2008-01-15 02:48:45 -0800 | [diff] [blame] | 562 | # parse $args after "submodule ... status". |
| 563 | while test $# -ne 0 |
| 564 | do |
| 565 | case "$1" in |
| 566 | -q|--quiet) |
| 567 | quiet=1 |
| 568 | ;; |
| 569 | --cached) |
| 570 | cached=1 |
| 571 | ;; |
| 572 | --) |
| 573 | shift |
| 574 | break |
| 575 | ;; |
| 576 | -*) |
| 577 | usage |
| 578 | ;; |
| 579 | *) |
| 580 | break |
| 581 | ;; |
| 582 | esac |
| 583 | shift |
| 584 | done |
| 585 | |
David Aguilar | a7b3269 | 2008-08-22 00:30:50 -0700 | [diff] [blame] | 586 | module_list "$@" | |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 587 | while read mode sha1 stage path |
| 588 | do |
Lars Hjemli | 941987a | 2007-06-11 21:12:24 +0200 | [diff] [blame] | 589 | name=$(module_name "$path") || exit |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 590 | url=$(git config submodule."$name".url) |
Lars Hjemli | ba88a1f | 2008-02-20 23:13:15 +0100 | [diff] [blame] | 591 | if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 592 | then |
| 593 | say "-$sha1 $path" |
| 594 | continue; |
| 595 | fi |
CJ van den Berg | 41c7c1b | 2007-07-04 18:22:14 +0200 | [diff] [blame] | 596 | set_name_rev "$path" "$sha1" |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 597 | if git diff-files --quiet -- "$path" |
| 598 | then |
Emil Medve | bffe71f | 2007-06-26 18:40:58 -0500 | [diff] [blame] | 599 | say " $sha1 $path$revname" |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 600 | else |
| 601 | if test -z "$cached" |
| 602 | then |
H.Merijn Brand | 5188408 | 2007-12-04 22:45:16 +0000 | [diff] [blame] | 603 | sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD) |
CJ van den Berg | 41c7c1b | 2007-07-04 18:22:14 +0200 | [diff] [blame] | 604 | set_name_rev "$path" "$sha1" |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 605 | fi |
Emil Medve | bffe71f | 2007-06-26 18:40:58 -0500 | [diff] [blame] | 606 | say "+$sha1 $path$revname" |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 607 | fi |
| 608 | done |
| 609 | } |
David Aguilar | 2327f61 | 2008-08-24 12:43:37 -0700 | [diff] [blame] | 610 | # |
| 611 | # Sync remote urls for submodules |
| 612 | # This makes the value for remote.$remote.url match the value |
| 613 | # specified in .gitmodules. |
| 614 | # |
| 615 | cmd_sync() |
| 616 | { |
| 617 | while test $# -ne 0 |
| 618 | do |
| 619 | case "$1" in |
| 620 | -q|--quiet) |
| 621 | quiet=1 |
| 622 | shift |
| 623 | ;; |
| 624 | --) |
| 625 | shift |
| 626 | break |
| 627 | ;; |
| 628 | -*) |
| 629 | usage |
| 630 | ;; |
| 631 | *) |
| 632 | break |
| 633 | ;; |
| 634 | esac |
| 635 | done |
| 636 | cd_to_toplevel |
| 637 | module_list "$@" | |
| 638 | while read mode sha1 stage path |
| 639 | do |
| 640 | name=$(module_name "$path") |
| 641 | url=$(git config -f .gitmodules --get submodule."$name".url) |
Johan Herland | baede9f | 2008-09-22 18:08:31 +0200 | [diff] [blame] | 642 | |
| 643 | # Possibly a url relative to parent |
| 644 | case "$url" in |
| 645 | ./*|../*) |
| 646 | url=$(resolve_relative_url "$url") || exit |
| 647 | ;; |
| 648 | esac |
| 649 | |
David Aguilar | 2327f61 | 2008-08-24 12:43:37 -0700 | [diff] [blame] | 650 | if test -e "$path"/.git |
| 651 | then |
| 652 | ( |
| 653 | unset GIT_DIR |
| 654 | cd "$path" |
| 655 | remote=$(get_default_remote) |
| 656 | say "Synchronizing submodule url for '$name'" |
| 657 | git config remote."$remote".url "$url" |
| 658 | ) |
| 659 | fi |
| 660 | done |
| 661 | } |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 662 | |
Junio C Hamano | 5c08dbb | 2008-01-15 02:48:45 -0800 | [diff] [blame] | 663 | # This loop parses the command line arguments to find the |
| 664 | # subcommand name to dispatch. Parsing of the subcommand specific |
| 665 | # options are primarily done by the subcommand implementations. |
| 666 | # Subcommand specific options such as --branch and --cached are |
| 667 | # parsed here as well, for backward compatibility. |
| 668 | |
| 669 | while test $# != 0 && test -z "$command" |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 670 | do |
| 671 | case "$1" in |
David Aguilar | 2327f61 | 2008-08-24 12:43:37 -0700 | [diff] [blame] | 672 | add | foreach | init | update | status | summary | sync) |
Junio C Hamano | 5c08dbb | 2008-01-15 02:48:45 -0800 | [diff] [blame] | 673 | command=$1 |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 674 | ;; |
| 675 | -q|--quiet) |
| 676 | quiet=1 |
| 677 | ;; |
Sven Verdoolaege | ecda072 | 2007-06-24 23:06:07 +0200 | [diff] [blame] | 678 | -b|--branch) |
| 679 | case "$2" in |
| 680 | '') |
| 681 | usage |
| 682 | ;; |
| 683 | esac |
| 684 | branch="$2"; shift |
| 685 | ;; |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 686 | --cached) |
Ping Yin | 28f9af5 | 2008-03-11 21:52:15 +0800 | [diff] [blame] | 687 | cached="$1" |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 688 | ;; |
| 689 | --) |
| 690 | break |
| 691 | ;; |
| 692 | -*) |
| 693 | usage |
| 694 | ;; |
| 695 | *) |
| 696 | break |
| 697 | ;; |
| 698 | esac |
| 699 | shift |
| 700 | done |
| 701 | |
Junio C Hamano | 5c08dbb | 2008-01-15 02:48:45 -0800 | [diff] [blame] | 702 | # No command word defaults to "status" |
| 703 | test -n "$command" || command=status |
Sven Verdoolaege | ecda072 | 2007-06-24 23:06:07 +0200 | [diff] [blame] | 704 | |
Junio C Hamano | 5c08dbb | 2008-01-15 02:48:45 -0800 | [diff] [blame] | 705 | # "-b branch" is accepted only by "add" |
| 706 | if test -n "$branch" && test "$command" != add |
| 707 | then |
Lars Hjemli | 70c7ac2 | 2007-05-26 15:56:40 +0200 | [diff] [blame] | 708 | usage |
Junio C Hamano | 5c08dbb | 2008-01-15 02:48:45 -0800 | [diff] [blame] | 709 | fi |
| 710 | |
Ping Yin | 28f9af5 | 2008-03-11 21:52:15 +0800 | [diff] [blame] | 711 | # "--cached" is accepted only by "status" and "summary" |
| 712 | if test -n "$cached" && test "$command" != status -a "$command" != summary |
Junio C Hamano | 5c08dbb | 2008-01-15 02:48:45 -0800 | [diff] [blame] | 713 | then |
| 714 | usage |
| 715 | fi |
| 716 | |
| 717 | "cmd_$command" "$@" |