Merge branch 'jk/svn-color'
[git/mingw/j6t.git] / git-submodule.sh
blobad9fe628fdf0f7d5a9a968abeb03d6213e3772d7
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 OPTIONS_SPEC=
9 . git-sh-setup
10 require_work_tree
12 add=
13 branch=
14 init=
15 update=
16 status=
17 quiet=
18 cached=
21 # print stuff on stdout unless -q was specified
23 say()
25 if test -z "$quiet"
26 then
27 echo "$@"
31 # NEEDSWORK: identical function exists in get_repo_base in clone.sh
32 get_repo_base() {
34 cd "`/bin/pwd`" &&
35 cd "$1" || cd "$1.git" &&
37 cd .git
38 pwd
40 ) 2>/dev/null
43 # Resolve relative url by appending to parent's url
44 resolve_relative_url ()
46 branch="$(git symbolic-ref HEAD 2>/dev/null)"
47 remote="$(git config branch.${branch#refs/heads/}.remote)"
48 remote="${remote:-origin}"
49 remoteurl="$(git config remote.$remote.url)" ||
50 die "remote ($remote) does not have a url in .git/config"
51 url="$1"
52 while test -n "$url"
54 case "$url" in
55 ../*)
56 url="${url#../}"
57 remoteurl="${remoteurl%/*}"
59 ./*)
60 url="${url#./}"
63 break;;
64 esac
65 done
66 echo "$remoteurl/$url"
70 # Map submodule path to submodule name
72 # $1 = path
74 module_name()
76 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
77 re=$(printf '%s' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
78 name=$( GIT_CONFIG=.gitmodules \
79 git config --get-regexp '^submodule\..*\.path$' |
80 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
81 test -z "$name" &&
82 die "No submodule mapping found in .gitmodules for path '$path'"
83 echo "$name"
87 # Clone a submodule
89 # Prior to calling, modules_update checks that a possibly existing
90 # path is not a git repository.
91 # Likewise, module_add checks that path does not exist at all,
92 # since it is the location of a new submodule.
94 module_clone()
96 path=$1
97 url=$2
99 # If there already is a directory at the submodule path,
100 # expect it to be empty (since that is the default checkout
101 # action) and try to remove it.
102 # Note: if $path is a symlink to a directory the test will
103 # succeed but the rmdir will fail. We might want to fix this.
104 if test -d "$path"
105 then
106 rmdir "$path" 2>/dev/null ||
107 die "Directory '$path' exist, but is neither empty nor a git repository"
110 test -e "$path" &&
111 die "A file already exist at path '$path'"
113 git-clone -n "$url" "$path" ||
114 die "Clone of '$url' into submodule path '$path' failed"
118 # Add a new submodule to the working tree, .gitmodules and the index
120 # $@ = repo [path]
122 # optional branch is stored in global branch variable
124 module_add()
126 repo=$1
127 path=$2
129 if test -z "$repo"; then
130 usage
133 case "$repo" in
134 ./*|../*)
135 # dereference source url relative to parent's url
136 realrepo="$(resolve_relative_url $repo)" ;;
138 # Turn the source into an absolute path if
139 # it is local
140 if base=$(get_repo_base "$repo"); then
141 repo="$base"
143 realrepo=$repo
145 esac
147 # Guess path from repo if not specified or strip trailing slashes
148 if test -z "$path"; then
149 path=$(echo "$repo" | sed -e 's|/*$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
150 else
151 path=$(echo "$path" | sed -e 's|/*$||')
154 test -e "$path" &&
155 die "'$path' already exists"
157 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
158 die "'$path' already exists in the index"
160 module_clone "$path" "$realrepo" || exit
161 (unset GIT_DIR; cd "$path" && git checkout -q ${branch:+-b "$branch" "origin/$branch"}) ||
162 die "Unable to checkout submodule '$path'"
163 git add "$path" ||
164 die "Failed to add submodule '$path'"
166 GIT_CONFIG=.gitmodules git config submodule."$path".path "$path" &&
167 GIT_CONFIG=.gitmodules git config submodule."$path".url "$repo" &&
168 git add .gitmodules ||
169 die "Failed to register submodule '$path'"
173 # Register submodules in .git/config
175 # $@ = requested paths (default to all)
177 modules_init()
179 git ls-files --stage -- "$@" | grep -e '^160000 ' |
180 while read mode sha1 stage path
182 # Skip already registered paths
183 name=$(module_name "$path") || exit
184 url=$(git config submodule."$name".url)
185 test -z "$url" || continue
187 url=$(GIT_CONFIG=.gitmodules git config submodule."$name".url)
188 test -z "$url" &&
189 die "No url found for submodule path '$path' in .gitmodules"
191 # Possibly a url relative to parent
192 case "$url" in
193 ./*|../*)
194 url="$(resolve_relative_url "$url")"
196 esac
198 git config submodule."$name".url "$url" ||
199 die "Failed to register url for submodule path '$path'"
201 say "Submodule '$name' ($url) registered for path '$path'"
202 done
206 # Update each submodule path to correct revision, using clone and checkout as needed
208 # $@ = requested paths (default to all)
210 modules_update()
212 git ls-files --stage -- "$@" | grep -e '^160000 ' |
213 while read mode sha1 stage path
215 name=$(module_name "$path") || exit
216 url=$(git config submodule."$name".url)
217 if test -z "$url"
218 then
219 # Only mention uninitialized submodules when its
220 # path have been specified
221 test "$#" != "0" &&
222 say "Submodule path '$path' not initialized"
223 continue
226 if ! test -d "$path"/.git
227 then
228 module_clone "$path" "$url" || exit
229 subsha1=
230 else
231 subsha1=$(unset GIT_DIR; cd "$path" &&
232 git rev-parse --verify HEAD) ||
233 die "Unable to find current revision in submodule path '$path'"
236 if test "$subsha1" != "$sha1"
237 then
238 (unset GIT_DIR; cd "$path" && git-fetch &&
239 git-checkout -q "$sha1") ||
240 die "Unable to checkout '$sha1' in submodule path '$path'"
242 say "Submodule path '$path': checked out '$sha1'"
244 done
247 set_name_rev () {
248 revname=$( (
249 unset GIT_DIR
250 cd "$1" && {
251 git describe "$2" 2>/dev/null ||
252 git describe --tags "$2" 2>/dev/null ||
253 git describe --contains --tags "$2"
256 test -z "$revname" || revname=" ($revname)"
260 # List all submodules, prefixed with:
261 # - submodule not initialized
262 # + different revision checked out
264 # If --cached was specified the revision in the index will be printed
265 # instead of the currently checked out revision.
267 # $@ = requested paths (default to all)
269 modules_list()
271 git ls-files --stage -- "$@" | grep -e '^160000 ' |
272 while read mode sha1 stage path
274 name=$(module_name "$path") || exit
275 url=$(git config submodule."$name".url)
276 if test -z "url" || ! test -d "$path"/.git
277 then
278 say "-$sha1 $path"
279 continue;
281 set_name_rev "$path" "$sha1"
282 if git diff-files --quiet -- "$path"
283 then
284 say " $sha1 $path$revname"
285 else
286 if test -z "$cached"
287 then
288 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
289 set_name_rev "$path" "$sha1"
291 say "+$sha1 $path$revname"
293 done
296 while test $# != 0
298 case "$1" in
299 add)
300 add=1
302 init)
303 init=1
305 update)
306 update=1
308 status)
309 status=1
311 -q|--quiet)
312 quiet=1
314 -b|--branch)
315 case "$2" in
317 usage
319 esac
320 branch="$2"; shift
322 --cached)
323 cached=1
326 break
329 usage
332 break
334 esac
335 shift
336 done
338 case "$add,$branch" in
339 1,*)
344 usage
346 esac
348 case "$add,$init,$update,$status,$cached" in
349 1,,,,)
350 module_add "$@"
352 ,1,,,)
353 modules_init "$@"
355 ,,1,,)
356 modules_update "$@"
358 ,,,*,*)
359 modules_list "$@"
362 usage
364 esac