Merge branch 'maint'
[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>...]'
70c7ac22
LH
8. git-sh-setup
9require_work_tree
10
ecda0723
SV
11add=
12branch=
70c7ac22
LH
13init=
14update=
15status=
16quiet=
17cached=
18
19#
20# print stuff on stdout unless -q was specified
21#
22say()
23{
24 if test -z "$quiet"
25 then
26 echo "$@"
27 fi
28}
29
ecda0723
SV
30# NEEDSWORK: identical function exists in get_repo_base in clone.sh
31get_repo_base() {
32 (
33 cd "`/bin/pwd`" &&
34 cd "$1" || cd "$1.git" &&
35 {
36 cd .git
37 pwd
38 }
39 ) 2>/dev/null
40}
41
941987a5
LH
42#
43# Map submodule path to submodule name
44#
45# $1 = path
46#
47module_name()
48{
537601ac
JH
49 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
50 re=$(printf '%s' "$1" | sed -e 's/\([^a-zA-Z0-9_]\)/\\\1/g')
51 name=$( GIT_CONFIG=.gitmodules \
52 git config --get-regexp '^submodule\..*\.path$' |
53 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
941987a5
LH
54 test -z "$name" &&
55 die "No submodule mapping found in .gitmodules for path '$path'"
56 echo "$name"
57}
33aa6fff
LH
58
59#
60# Clone a submodule
61#
ecda0723
SV
62# Prior to calling, modules_update checks that a possibly existing
63# path is not a git repository.
64# Likewise, module_add checks that path does not exist at all,
65# since it is the location of a new submodule.
66#
33aa6fff
LH
67module_clone()
68{
69 path=$1
70 url=$2
71
72 # If there already is a directory at the submodule path,
73 # expect it to be empty (since that is the default checkout
74 # action) and try to remove it.
75 # Note: if $path is a symlink to a directory the test will
76 # succeed but the rmdir will fail. We might want to fix this.
77 if test -d "$path"
78 then
79 rmdir "$path" 2>/dev/null ||
80 die "Directory '$path' exist, but is neither empty nor a git repository"
81 fi
82
83 test -e "$path" &&
84 die "A file already exist at path '$path'"
85
86 git-clone -n "$url" "$path" ||
941987a5 87 die "Clone of '$url' into submodule path '$path' failed"
33aa6fff
LH
88}
89
ecda0723
SV
90#
91# Add a new submodule to the working tree, .gitmodules and the index
92#
93# $@ = repo [path]
94#
95# optional branch is stored in global branch variable
96#
97module_add()
98{
99 repo=$1
100 path=$2
101
102 if test -z "$repo"; then
103 usage
104 fi
105
106 # Turn the source into an absolute path if
107 # it is local
108 if base=$(get_repo_base "$repo"); then
109 repo="$base"
110 fi
111
112 # Guess path from repo if not specified or strip trailing slashes
113 if test -z "$path"; then
114 path=$(echo "$repo" | sed -e 's|/*$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
115 else
116 path=$(echo "$path" | sed -e 's|/*$||')
117 fi
118
119 test -e "$path" &&
120 die "'$path' already exists"
121
5be60078 122 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
ecda0723
SV
123 die "'$path' already exists in the index"
124
125 module_clone "$path" "$repo" || exit
126 (unset GIT_DIR && cd "$path" && git checkout -q ${branch:+-b "$branch" "origin/$branch"}) ||
127 die "Unable to checkout submodule '$path'"
128 git add "$path" ||
129 die "Failed to add submodule '$path'"
130
131 GIT_CONFIG=.gitmodules git config submodule."$path".path "$path" &&
132 GIT_CONFIG=.gitmodules git config submodule."$path".url "$repo" &&
133 git add .gitmodules ||
134 die "Failed to register submodule '$path'"
135}
136
70c7ac22 137#
211b7f19 138# Register submodules in .git/config
70c7ac22
LH
139#
140# $@ = requested paths (default to all)
141#
142modules_init()
143{
144 git ls-files --stage -- "$@" | grep -e '^160000 ' |
145 while read mode sha1 stage path
146 do
211b7f19 147 # Skip already registered paths
941987a5 148 name=$(module_name "$path") || exit
5be60078 149 url=$(git config submodule."$name".url)
211b7f19 150 test -z "$url" || continue
70c7ac22 151
5be60078 152 url=$(GIT_CONFIG=.gitmodules git config submodule."$name".url)
70c7ac22 153 test -z "$url" &&
941987a5 154 die "No url found for submodule path '$path' in .gitmodules"
70c7ac22 155
5be60078 156 git config submodule."$name".url "$url" ||
941987a5 157 die "Failed to register url for submodule path '$path'"
70c7ac22 158
941987a5 159 say "Submodule '$name' ($url) registered for path '$path'"
70c7ac22
LH
160 done
161}
162
163#
211b7f19 164# Update each submodule path to correct revision, using clone and checkout as needed
70c7ac22
LH
165#
166# $@ = requested paths (default to all)
167#
168modules_update()
169{
170 git ls-files --stage -- "$@" | grep -e '^160000 ' |
171 while read mode sha1 stage path
172 do
941987a5 173 name=$(module_name "$path") || exit
5be60078 174 url=$(git config submodule."$name".url)
211b7f19 175 if test -z "$url"
70c7ac22
LH
176 then
177 # Only mention uninitialized submodules when its
178 # path have been specified
179 test "$#" != "0" &&
941987a5 180 say "Submodule path '$path' not initialized"
211b7f19
LH
181 continue
182 fi
183
184 if ! test -d "$path"/.git
185 then
186 module_clone "$path" "$url" || exit
bf2d8246
LH
187 subsha1=
188 else
189 subsha1=$(unset GIT_DIR && cd "$path" &&
5be60078 190 git rev-parse --verify HEAD) ||
941987a5 191 die "Unable to find current revision in submodule path '$path'"
70c7ac22 192 fi
211b7f19 193
70c7ac22
LH
194 if test "$subsha1" != "$sha1"
195 then
196 (unset GIT_DIR && cd "$path" && git-fetch &&
197 git-checkout -q "$sha1") ||
941987a5 198 die "Unable to checkout '$sha1' in submodule path '$path'"
70c7ac22 199
941987a5 200 say "Submodule path '$path': checked out '$sha1'"
70c7ac22
LH
201 fi
202 done
203}
204
bffe71f4
EM
205set_name_rev () {
206 revname=$( (
207 unset GIT_DIR &&
208 cd "$1" && {
5be60078
JH
209 git describe "$2" 2>/dev/null ||
210 git describe --tags "$2" 2>/dev/null ||
211 git describe --contains --tags "$2"
bffe71f4
EM
212 }
213 ) )
214 test -z "$revname" || revname=" ($revname)"
215}
216
70c7ac22 217#
941987a5 218# List all submodules, prefixed with:
70c7ac22
LH
219# - submodule not initialized
220# + different revision checked out
221#
222# If --cached was specified the revision in the index will be printed
223# instead of the currently checked out revision.
224#
225# $@ = requested paths (default to all)
226#
227modules_list()
228{
229 git ls-files --stage -- "$@" | grep -e '^160000 ' |
230 while read mode sha1 stage path
231 do
941987a5 232 name=$(module_name "$path") || exit
5be60078 233 url=$(git config submodule."$name".url)
941987a5 234 if test -z "url" || ! test -d "$path"/.git
70c7ac22
LH
235 then
236 say "-$sha1 $path"
237 continue;
238 fi
41c7c1bd 239 set_name_rev "$path" "$sha1"
70c7ac22
LH
240 if git diff-files --quiet -- "$path"
241 then
bffe71f4 242 say " $sha1 $path$revname"
70c7ac22
LH
243 else
244 if test -z "$cached"
245 then
5be60078 246 sha1=$(unset GIT_DIR && cd "$path" && git rev-parse --verify HEAD)
41c7c1bd 247 set_name_rev "$path" "$sha1"
70c7ac22 248 fi
bffe71f4 249 say "+$sha1 $path$revname"
70c7ac22
LH
250 fi
251 done
252}
253
822f7c73 254while test $# != 0
70c7ac22
LH
255do
256 case "$1" in
ecda0723
SV
257 add)
258 add=1
259 ;;
70c7ac22
LH
260 init)
261 init=1
262 ;;
263 update)
264 update=1
265 ;;
266 status)
267 status=1
268 ;;
269 -q|--quiet)
270 quiet=1
271 ;;
ecda0723
SV
272 -b|--branch)
273 case "$2" in
274 '')
275 usage
276 ;;
277 esac
278 branch="$2"; shift
279 ;;
70c7ac22
LH
280 --cached)
281 cached=1
282 ;;
283 --)
284 break
285 ;;
286 -*)
287 usage
288 ;;
289 *)
290 break
291 ;;
292 esac
293 shift
294done
295
ecda0723
SV
296case "$add,$branch" in
2971,*)
298 ;;
299,)
300 ;;
301,*)
302 usage
303 ;;
304esac
305
306case "$add,$init,$update,$status,$cached" in
3071,,,,)
308 module_add "$@"
309 ;;
310,1,,,)
70c7ac22
LH
311 modules_init "$@"
312 ;;
ecda0723 313,,1,,)
70c7ac22
LH
314 modules_update "$@"
315 ;;
97a5d8cc 316,,,*,*)
70c7ac22
LH
317 modules_list "$@"
318 ;;
319*)
320 usage
321 ;;
322esac