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