relax usage of the progress API
[git/gitweb.git] / git-submodule.sh
blob4aaaaab0d8450094efb8d2f8e82cc63383b91b48
1 #!/bin/sh
3 # git-submodules.sh: add, init, update or list git submodules
5 # Copyright (c) 2007 Lars Hjemli
7 USAGE='[--quiet] [--cached] [add <repo> [-b branch]|status|init|update] [--] [<path>...]'
8 . git-sh-setup
9 require_work_tree
11 add=
12 branch=
13 init=
14 update=
15 status=
16 quiet=
17 cached=
20 # print stuff on stdout unless -q was specified
22 say()
24 if test -z "$quiet"
25 then
26 echo "$@"
30 # NEEDSWORK: identical function exists in get_repo_base in clone.sh
31 get_repo_base() {
33 cd "`/bin/pwd`" &&
34 cd "$1" || cd "$1.git" &&
36 cd .git
37 pwd
39 ) 2>/dev/null
42 # Resolve relative url by appending to parent's url
43 resolve_relative_url ()
45 branch="$(git symbolic-ref HEAD 2>/dev/null)"
46 remote="$(git config branch.${branch#refs/heads/}.remote)"
47 remote="${remote:-origin}"
48 remoteurl="$(git config remote.$remote.url)" ||
49 die "remote ($remote) does not have a url in .git/config"
50 url="$1"
51 while test -n "$url"
53 case "$url" in
54 ../*)
55 url="${url#../}"
56 remoteurl="${remoteurl%/*}"
58 ./*)
59 url="${url#./}"
62 break;;
63 esac
64 done
65 echo "$remoteurl/$url"
69 # Map submodule path to submodule name
71 # $1 = path
73 module_name()
75 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
76 re=$(printf '%s' "$1" | sed -e 's/\([^a-zA-Z0-9_]\)/\\\1/g')
77 name=$( GIT_CONFIG=.gitmodules \
78 git config --get-regexp '^submodule\..*\.path$' |
79 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
80 test -z "$name" &&
81 die "No submodule mapping found in .gitmodules for path '$path'"
82 echo "$name"
86 # Clone a submodule
88 # Prior to calling, modules_update checks that a possibly existing
89 # path is not a git repository.
90 # Likewise, module_add checks that path does not exist at all,
91 # since it is the location of a new submodule.
93 module_clone()
95 path=$1
96 url=$2
98 # If there already is a directory at the submodule path,
99 # expect it to be empty (since that is the default checkout
100 # action) and try to remove it.
101 # Note: if $path is a symlink to a directory the test will
102 # succeed but the rmdir will fail. We might want to fix this.
103 if test -d "$path"
104 then
105 rmdir "$path" 2>/dev/null ||
106 die "Directory '$path' exist, but is neither empty nor a git repository"
109 test -e "$path" &&
110 die "A file already exist at path '$path'"
112 git-clone -n "$url" "$path" ||
113 die "Clone of '$url' into submodule path '$path' failed"
117 # Add a new submodule to the working tree, .gitmodules and the index
119 # $@ = repo [path]
121 # optional branch is stored in global branch variable
123 module_add()
125 repo=$1
126 path=$2
128 if test -z "$repo"; then
129 usage
132 case "$repo" in
133 ./*|../*)
134 # dereference source url relative to parent's url
135 realrepo="$(resolve_relative_url $repo)" ;;
137 # Turn the source into an absolute path if
138 # it is local
139 if base=$(get_repo_base "$repo"); then
140 repo="$base"
142 realrepo=$repo
144 esac
146 # Guess path from repo if not specified or strip trailing slashes
147 if test -z "$path"; then
148 path=$(echo "$repo" | sed -e 's|/*$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
149 else
150 path=$(echo "$path" | sed -e 's|/*$||')
153 test -e "$path" &&
154 die "'$path' already exists"
156 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
157 die "'$path' already exists in the index"
159 module_clone "$path" "$realrepo" || exit
160 (unset GIT_DIR && cd "$path" && git checkout -q ${branch:+-b "$branch" "origin/$branch"}) ||
161 die "Unable to checkout submodule '$path'"
162 git add "$path" ||
163 die "Failed to add submodule '$path'"
165 GIT_CONFIG=.gitmodules git config submodule."$path".path "$path" &&
166 GIT_CONFIG=.gitmodules git config submodule."$path".url "$repo" &&
167 git add .gitmodules ||
168 die "Failed to register submodule '$path'"
172 # Register submodules in .git/config
174 # $@ = requested paths (default to all)
176 modules_init()
178 git ls-files --stage -- "$@" | grep -e '^160000 ' |
179 while read mode sha1 stage path
181 # Skip already registered paths
182 name=$(module_name "$path") || exit
183 url=$(git config submodule."$name".url)
184 test -z "$url" || continue
186 url=$(GIT_CONFIG=.gitmodules git config submodule."$name".url)
187 test -z "$url" &&
188 die "No url found for submodule path '$path' in .gitmodules"
190 # Possibly a url relative to parent
191 case "$url" in
192 ./*|../*)
193 url="$(resolve_relative_url "$url")"
195 esac
197 git config submodule."$name".url "$url" ||
198 die "Failed to register url for submodule path '$path'"
200 say "Submodule '$name' ($url) registered for path '$path'"
201 done
205 # Update each submodule path to correct revision, using clone and checkout as needed
207 # $@ = requested paths (default to all)
209 modules_update()
211 git ls-files --stage -- "$@" | grep -e '^160000 ' |
212 while read mode sha1 stage path
214 name=$(module_name "$path") || exit
215 url=$(git config submodule."$name".url)
216 if test -z "$url"
217 then
218 # Only mention uninitialized submodules when its
219 # path have been specified
220 test "$#" != "0" &&
221 say "Submodule path '$path' not initialized"
222 continue
225 if ! test -d "$path"/.git
226 then
227 module_clone "$path" "$url" || exit
228 subsha1=
229 else
230 subsha1=$(unset GIT_DIR && cd "$path" &&
231 git rev-parse --verify HEAD) ||
232 die "Unable to find current revision in submodule path '$path'"
235 if test "$subsha1" != "$sha1"
236 then
237 (unset GIT_DIR && cd "$path" && git-fetch &&
238 git-checkout -q "$sha1") ||
239 die "Unable to checkout '$sha1' in submodule path '$path'"
241 say "Submodule path '$path': checked out '$sha1'"
243 done
246 set_name_rev () {
247 revname=$( (
248 unset GIT_DIR &&
249 cd "$1" && {
250 git describe "$2" 2>/dev/null ||
251 git describe --tags "$2" 2>/dev/null ||
252 git describe --contains --tags "$2"
255 test -z "$revname" || revname=" ($revname)"
259 # List all submodules, prefixed with:
260 # - submodule not initialized
261 # + different revision checked out
263 # If --cached was specified the revision in the index will be printed
264 # instead of the currently checked out revision.
266 # $@ = requested paths (default to all)
268 modules_list()
270 git ls-files --stage -- "$@" | grep -e '^160000 ' |
271 while read mode sha1 stage path
273 name=$(module_name "$path") || exit
274 url=$(git config submodule."$name".url)
275 if test -z "url" || ! test -d "$path"/.git
276 then
277 say "-$sha1 $path"
278 continue;
280 set_name_rev "$path" "$sha1"
281 if git diff-files --quiet -- "$path"
282 then
283 say " $sha1 $path$revname"
284 else
285 if test -z "$cached"
286 then
287 sha1=$(unset GIT_DIR && cd "$path" && git rev-parse --verify HEAD)
288 set_name_rev "$path" "$sha1"
290 say "+$sha1 $path$revname"
292 done
295 while test $# != 0
297 case "$1" in
298 add)
299 add=1
301 init)
302 init=1
304 update)
305 update=1
307 status)
308 status=1
310 -q|--quiet)
311 quiet=1
313 -b|--branch)
314 case "$2" in
316 usage
318 esac
319 branch="$2"; shift
321 --cached)
322 cached=1
325 break
328 usage
331 break
333 esac
334 shift
335 done
337 case "$add,$branch" in
338 1,*)
343 usage
345 esac
347 case "$add,$init,$update,$status,$cached" in
348 1,,,,)
349 module_add "$@"
351 ,1,,,)
352 modules_init "$@"
354 ,,1,,)
355 modules_update "$@"
357 ,,,*,*)
358 modules_list "$@"
361 usage
363 esac