gitweb: Lift any characters restriction on searched strings
[git/mingw/4msysgit/wingit-dll.git] / git-submodule.sh
blob3320998c76ba72094755ec60369a6266751889ff
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
43 # Map submodule path to submodule name
45 # $1 = path
47 module_name()
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' )
54 test -z "$name" &&
55 die "No submodule mapping found in .gitmodules for path '$path'"
56 echo "$name"
60 # Clone a submodule
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.
67 module_clone()
69 path=$1
70 url=$2
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"
83 test -e "$path" &&
84 die "A file already exist at path '$path'"
86 git-clone -n "$url" "$path" ||
87 die "Clone of '$url' into submodule path '$path' failed"
91 # Add a new submodule to the working tree, .gitmodules and the index
93 # $@ = repo [path]
95 # optional branch is stored in global branch variable
97 module_add()
99 repo=$1
100 path=$2
102 if test -z "$repo"; then
103 usage
106 # Turn the source into an absolute path if
107 # it is local
108 if base=$(get_repo_base "$repo"); then
109 repo="$base"
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|/*$||')
119 test -e "$path" &&
120 die "'$path' already exists"
122 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
123 die "'$path' already exists in the index"
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'"
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'"
138 # Register submodules in .git/config
140 # $@ = requested paths (default to all)
142 modules_init()
144 git ls-files --stage -- "$@" | grep -e '^160000 ' |
145 while read mode sha1 stage path
147 # Skip already registered paths
148 name=$(module_name "$path") || exit
149 url=$(git config submodule."$name".url)
150 test -z "$url" || continue
152 url=$(GIT_CONFIG=.gitmodules git config submodule."$name".url)
153 test -z "$url" &&
154 die "No url found for submodule path '$path' in .gitmodules"
156 git config submodule."$name".url "$url" ||
157 die "Failed to register url for submodule path '$path'"
159 say "Submodule '$name' ($url) registered for path '$path'"
160 done
164 # Update each submodule path to correct revision, using clone and checkout as needed
166 # $@ = requested paths (default to all)
168 modules_update()
170 git ls-files --stage -- "$@" | grep -e '^160000 ' |
171 while read mode sha1 stage path
173 name=$(module_name "$path") || exit
174 url=$(git config submodule."$name".url)
175 if test -z "$url"
176 then
177 # Only mention uninitialized submodules when its
178 # path have been specified
179 test "$#" != "0" &&
180 say "Submodule path '$path' not initialized"
181 continue
184 if ! test -d "$path"/.git
185 then
186 module_clone "$path" "$url" || exit
187 subsha1=
188 else
189 subsha1=$(unset GIT_DIR && cd "$path" &&
190 git rev-parse --verify HEAD) ||
191 die "Unable to find current revision in submodule path '$path'"
194 if test "$subsha1" != "$sha1"
195 then
196 (unset GIT_DIR && cd "$path" && git-fetch &&
197 git-checkout -q "$sha1") ||
198 die "Unable to checkout '$sha1' in submodule path '$path'"
200 say "Submodule path '$path': checked out '$sha1'"
202 done
205 set_name_rev () {
206 revname=$( (
207 unset GIT_DIR &&
208 cd "$1" && {
209 git describe "$2" 2>/dev/null ||
210 git describe --tags "$2" 2>/dev/null ||
211 git describe --contains --tags "$2"
214 test -z "$revname" || revname=" ($revname)"
218 # List all submodules, prefixed with:
219 # - submodule not initialized
220 # + different revision checked out
222 # If --cached was specified the revision in the index will be printed
223 # instead of the currently checked out revision.
225 # $@ = requested paths (default to all)
227 modules_list()
229 git ls-files --stage -- "$@" | grep -e '^160000 ' |
230 while read mode sha1 stage path
232 name=$(module_name "$path") || exit
233 url=$(git config submodule."$name".url)
234 if test -z "url" || ! test -d "$path"/.git
235 then
236 say "-$sha1 $path"
237 continue;
239 set_name_rev "$path" "$sha1"
240 if git diff-files --quiet -- "$path"
241 then
242 say " $sha1 $path$revname"
243 else
244 if test -z "$cached"
245 then
246 sha1=$(unset GIT_DIR && cd "$path" && git rev-parse --verify HEAD)
247 set_name_rev "$path" "$sha1"
249 say "+$sha1 $path$revname"
251 done
254 while case "$#" in 0) break ;; esac
256 case "$1" in
257 add)
258 add=1
260 init)
261 init=1
263 update)
264 update=1
266 status)
267 status=1
269 -q|--quiet)
270 quiet=1
272 -b|--branch)
273 case "$2" in
275 usage
277 esac
278 branch="$2"; shift
280 --cached)
281 cached=1
284 break
287 usage
290 break
292 esac
293 shift
294 done
296 case "$add,$branch" in
297 1,*)
302 usage
304 esac
306 case "$add,$init,$update,$status,$cached" in
307 1,,,,)
308 module_add "$@"
310 ,1,,,)
311 modules_init "$@"
313 ,,1,,)
314 modules_update "$@"
316 ,,,*,*)
317 modules_list "$@"
320 usage
322 esac