git-submodule: rename shell functions for consistency
[git/git.git] / git-submodule.sh
CommitLineData
70c7ac22
LH
1#!/bin/sh
2#
ecda0723 3# git-submodules.sh: add, init, update or list git submodules
70c7ac22
LH
4#
5# Copyright (c) 2007 Lars Hjemli
6
ecda0723 7USAGE='[--quiet] [--cached] [add <repo> [-b branch]|status|init|update] [--] [<path>...]'
8f321a39 8OPTIONS_SPEC=
70c7ac22
LH
9. git-sh-setup
10require_work_tree
11
ecda0723
SV
12add=
13branch=
70c7ac22
LH
14init=
15update=
16status=
17quiet=
18cached=
19
20#
21# print stuff on stdout unless -q was specified
22#
23say()
24{
25 if test -z "$quiet"
26 then
27 echo "$@"
28 fi
29}
30
ecda0723
SV
31# NEEDSWORK: identical function exists in get_repo_base in clone.sh
32get_repo_base() {
33 (
34 cd "`/bin/pwd`" &&
35 cd "$1" || cd "$1.git" &&
36 {
37 cd .git
38 pwd
39 }
40 ) 2>/dev/null
41}
42
f31a522a
ML
43# Resolve relative url by appending to parent's url
44resolve_relative_url ()
45{
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"
53 do
54 case "$url" in
55 ../*)
56 url="${url#../}"
57 remoteurl="${remoteurl%/*}"
58 ;;
59 ./*)
60 url="${url#./}"
61 ;;
62 *)
63 break;;
64 esac
65 done
66 echo "$remoteurl/$url"
67}
68
941987a5
LH
69#
70# Map submodule path to submodule name
71#
72# $1 = path
73#
74module_name()
75{
537601ac 76 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
4f2e94c0 77 re=$(printf '%s' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
537601ac
JH
78 name=$( GIT_CONFIG=.gitmodules \
79 git config --get-regexp '^submodule\..*\.path$' |
80 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
941987a5
LH
81 test -z "$name" &&
82 die "No submodule mapping found in .gitmodules for path '$path'"
83 echo "$name"
84}
33aa6fff
LH
85
86#
87# Clone a submodule
88#
23a485e3 89# Prior to calling, cmd_update checks that a possibly existing
ecda0723 90# path is not a git repository.
23a485e3 91# Likewise, cmd_add checks that path does not exist at all,
ecda0723
SV
92# since it is the location of a new submodule.
93#
33aa6fff
LH
94module_clone()
95{
96 path=$1
97 url=$2
98
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"
108 fi
109
110 test -e "$path" &&
111 die "A file already exist at path '$path'"
112
113 git-clone -n "$url" "$path" ||
941987a5 114 die "Clone of '$url' into submodule path '$path' failed"
33aa6fff
LH
115}
116
ecda0723
SV
117#
118# Add a new submodule to the working tree, .gitmodules and the index
119#
120# $@ = repo [path]
121#
122# optional branch is stored in global branch variable
123#
23a485e3 124cmd_add()
ecda0723
SV
125{
126 repo=$1
127 path=$2
128
129 if test -z "$repo"; then
130 usage
131 fi
132
f31a522a
ML
133 case "$repo" in
134 ./*|../*)
135 # dereference source url relative to parent's url
136 realrepo="$(resolve_relative_url $repo)" ;;
137 *)
138 # Turn the source into an absolute path if
139 # it is local
140 if base=$(get_repo_base "$repo"); then
141 repo="$base"
f31a522a 142 fi
fe50eef5 143 realrepo=$repo
f31a522a
ML
144 ;;
145 esac
ecda0723
SV
146
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|/*$||')
152 fi
153
154 test -e "$path" &&
155 die "'$path' already exists"
156
5be60078 157 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
ecda0723
SV
158 die "'$path' already exists in the index"
159
f31a522a 160 module_clone "$path" "$realrepo" || exit
51884080 161 (unset GIT_DIR; cd "$path" && git checkout -q ${branch:+-b "$branch" "origin/$branch"}) ||
ecda0723
SV
162 die "Unable to checkout submodule '$path'"
163 git add "$path" ||
164 die "Failed to add submodule '$path'"
165
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'"
170}
171
70c7ac22 172#
211b7f19 173# Register submodules in .git/config
70c7ac22
LH
174#
175# $@ = requested paths (default to all)
176#
23a485e3 177cmd_init()
70c7ac22
LH
178{
179 git ls-files --stage -- "$@" | grep -e '^160000 ' |
180 while read mode sha1 stage path
181 do
211b7f19 182 # Skip already registered paths
941987a5 183 name=$(module_name "$path") || exit
5be60078 184 url=$(git config submodule."$name".url)
211b7f19 185 test -z "$url" || continue
70c7ac22 186
5be60078 187 url=$(GIT_CONFIG=.gitmodules git config submodule."$name".url)
70c7ac22 188 test -z "$url" &&
941987a5 189 die "No url found for submodule path '$path' in .gitmodules"
70c7ac22 190
f31a522a
ML
191 # Possibly a url relative to parent
192 case "$url" in
193 ./*|../*)
194 url="$(resolve_relative_url "$url")"
195 ;;
196 esac
197
5be60078 198 git config submodule."$name".url "$url" ||
941987a5 199 die "Failed to register url for submodule path '$path'"
70c7ac22 200
941987a5 201 say "Submodule '$name' ($url) registered for path '$path'"
70c7ac22
LH
202 done
203}
204
205#
211b7f19 206# Update each submodule path to correct revision, using clone and checkout as needed
70c7ac22
LH
207#
208# $@ = requested paths (default to all)
209#
23a485e3 210cmd_update()
70c7ac22
LH
211{
212 git ls-files --stage -- "$@" | grep -e '^160000 ' |
213 while read mode sha1 stage path
214 do
941987a5 215 name=$(module_name "$path") || exit
5be60078 216 url=$(git config submodule."$name".url)
211b7f19 217 if test -z "$url"
70c7ac22
LH
218 then
219 # Only mention uninitialized submodules when its
220 # path have been specified
221 test "$#" != "0" &&
941987a5 222 say "Submodule path '$path' not initialized"
211b7f19
LH
223 continue
224 fi
225
226 if ! test -d "$path"/.git
227 then
228 module_clone "$path" "$url" || exit
bf2d8246
LH
229 subsha1=
230 else
51884080 231 subsha1=$(unset GIT_DIR; cd "$path" &&
5be60078 232 git rev-parse --verify HEAD) ||
941987a5 233 die "Unable to find current revision in submodule path '$path'"
70c7ac22 234 fi
211b7f19 235
70c7ac22
LH
236 if test "$subsha1" != "$sha1"
237 then
51884080 238 (unset GIT_DIR; cd "$path" && git-fetch &&
70c7ac22 239 git-checkout -q "$sha1") ||
941987a5 240 die "Unable to checkout '$sha1' in submodule path '$path'"
70c7ac22 241
941987a5 242 say "Submodule path '$path': checked out '$sha1'"
70c7ac22
LH
243 fi
244 done
245}
246
bffe71f4
EM
247set_name_rev () {
248 revname=$( (
51884080 249 unset GIT_DIR
bffe71f4 250 cd "$1" && {
5be60078
JH
251 git describe "$2" 2>/dev/null ||
252 git describe --tags "$2" 2>/dev/null ||
253 git describe --contains --tags "$2"
bffe71f4
EM
254 }
255 ) )
256 test -z "$revname" || revname=" ($revname)"
257}
258
70c7ac22 259#
941987a5 260# List all submodules, prefixed with:
70c7ac22
LH
261# - submodule not initialized
262# + different revision checked out
263#
264# If --cached was specified the revision in the index will be printed
265# instead of the currently checked out revision.
266#
267# $@ = requested paths (default to all)
268#
23a485e3 269cmd_status()
70c7ac22
LH
270{
271 git ls-files --stage -- "$@" | grep -e '^160000 ' |
272 while read mode sha1 stage path
273 do
941987a5 274 name=$(module_name "$path") || exit
5be60078 275 url=$(git config submodule."$name".url)
941987a5 276 if test -z "url" || ! test -d "$path"/.git
70c7ac22
LH
277 then
278 say "-$sha1 $path"
279 continue;
280 fi
41c7c1bd 281 set_name_rev "$path" "$sha1"
70c7ac22
LH
282 if git diff-files --quiet -- "$path"
283 then
bffe71f4 284 say " $sha1 $path$revname"
70c7ac22
LH
285 else
286 if test -z "$cached"
287 then
51884080 288 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
41c7c1bd 289 set_name_rev "$path" "$sha1"
70c7ac22 290 fi
bffe71f4 291 say "+$sha1 $path$revname"
70c7ac22
LH
292 fi
293 done
294}
295
822f7c73 296while test $# != 0
70c7ac22
LH
297do
298 case "$1" in
ecda0723
SV
299 add)
300 add=1
301 ;;
70c7ac22
LH
302 init)
303 init=1
304 ;;
305 update)
306 update=1
307 ;;
308 status)
309 status=1
310 ;;
311 -q|--quiet)
312 quiet=1
313 ;;
ecda0723
SV
314 -b|--branch)
315 case "$2" in
316 '')
317 usage
318 ;;
319 esac
320 branch="$2"; shift
321 ;;
70c7ac22
LH
322 --cached)
323 cached=1
324 ;;
325 --)
326 break
327 ;;
328 -*)
329 usage
330 ;;
331 *)
332 break
333 ;;
334 esac
335 shift
336done
337
ecda0723
SV
338case "$add,$branch" in
3391,*)
340 ;;
341,)
342 ;;
343,*)
344 usage
345 ;;
346esac
347
348case "$add,$init,$update,$status,$cached" in
3491,,,,)
23a485e3 350 cmd_add "$@"
ecda0723
SV
351 ;;
352,1,,,)
23a485e3 353 cmd_init "$@"
70c7ac22 354 ;;
ecda0723 355,,1,,)
23a485e3 356 cmd_update "$@"
70c7ac22 357 ;;
97a5d8cc 358,,,*,*)
23a485e3 359 cmd_status "$@"
70c7ac22
LH
360 ;;
361*)
362 usage
363 ;;
364esac