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>...]'
20 # print stuff on stdout unless -q was specified
30 # NEEDSWORK: identical function exists in get_repo_base in clone.sh
34 cd "$1" || cd "$1.git" &&
43 # Map submodule path to submodule name
49 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
50 re=$(printf '%s' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
51 name=$( GIT_CONFIG=.gitmodules \
52 git config --get-regexp '^submodule\..*\.path$' |
53 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
55 die "No submodule mapping found in .gitmodules for path '$path'"
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.
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.
79 rmdir "$path" 2>/dev/null ||
80 die "Directory '$path' exist, but is neither empty nor a git repository"
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
95 # optional branch is stored in global branch variable
102 if test -z "$repo"; then
106 # Turn the source into an absolute path if
108 if base=$(get_repo_base "$repo"); then
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')
116 path=$(echo "$path" | sed -e 's|/*$||')
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'"
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)
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)
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'"
164 # Update each submodule path to correct revision, using clone and checkout as needed
166 # $@ = requested paths (default to all)
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)
177 # Only mention uninitialized submodules when its
178 # path have been specified
180 say "Submodule path '$path' not initialized"
184 if ! test -d "$path"/.git
186 module_clone "$path" "$url" || exit
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"
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'"
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)
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
239 set_name_rev "$path" "$sha1"
240 if git diff-files --quiet -- "$path"
242 say " $sha1 $path$revname"
246 sha1=$(unset GIT_DIR && cd "$path" && git rev-parse --verify HEAD)
247 set_name_rev "$path" "$sha1"
249 say "+$sha1 $path$revname"
296 case "$add,$branch" in
306 case "$add,$init,$update,$status,$cached" in