3 # git-submodules.sh: init, update or list git submodules
5 # Copyright (c) 2007 Lars Hjemli
7 USAGE
='[--quiet] [--cached] [status|init|update] [--] [<path>...]'
18 # print stuff on stdout unless -q was specified
29 # Map submodule path to submodule name
35 name
=$
(GIT_CONFIG
=.gitmodules git-config
--get-regexp '^submodule\..*\.path$' "$1" |
36 sed -nre 's/^submodule\.(.+)\.path .+$/\1/p')
38 die
"No submodule mapping found in .gitmodules for path '$path'"
50 # If there already is a directory at the submodule path,
51 # expect it to be empty (since that is the default checkout
52 # action) and try to remove it.
53 # Note: if $path is a symlink to a directory the test will
54 # succeed but the rmdir will fail. We might want to fix this.
57 rmdir "$path" 2>/dev
/null ||
58 die
"Directory '$path' exist, but is neither empty nor a git repository"
62 die
"A file already exist at path '$path'"
64 git-clone
-n "$url" "$path" ||
65 die
"Clone of '$url' into submodule path '$path' failed"
69 # Register submodules in .git/config
71 # $@ = requested paths (default to all)
75 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
76 while read mode sha1 stage path
78 # Skip already registered paths
79 name
=$
(module_name
"$path") ||
exit
80 url
=$
(git-config submodule.
"$name".url
)
81 test -z "$url" ||
continue
83 url
=$
(GIT_CONFIG
=.gitmodules git-config submodule.
"$name".url
)
85 die
"No url found for submodule path '$path' in .gitmodules"
87 git-config submodule.
"$name".url
"$url" ||
88 die
"Failed to register url for submodule path '$path'"
90 say
"Submodule '$name' ($url) registered for path '$path'"
95 # Update each submodule path to correct revision, using clone and checkout as needed
97 # $@ = requested paths (default to all)
101 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
102 while read mode sha1 stage path
104 name
=$
(module_name
"$path") ||
exit
105 url
=$
(git-config submodule.
"$name".url
)
108 # Only mention uninitialized submodules when its
109 # path have been specified
111 say
"Submodule path '$path' not initialized"
115 if ! test -d "$path"/.git
117 module_clone
"$path" "$url" ||
exit
120 subsha1
=$
(unset GIT_DIR
&& cd "$path" &&
121 git-rev-parse
--verify HEAD
) ||
122 die
"Unable to find current revision in submodule path '$path'"
125 if test "$subsha1" != "$sha1"
127 (unset GIT_DIR
&& cd "$path" && git-fetch
&&
128 git-checkout
-q "$sha1") ||
129 die
"Unable to checkout '$sha1' in submodule path '$path'"
131 say
"Submodule path '$path': checked out '$sha1'"
137 # List all submodules, prefixed with:
138 # - submodule not initialized
139 # + different revision checked out
141 # If --cached was specified the revision in the index will be printed
142 # instead of the currently checked out revision.
144 # $@ = requested paths (default to all)
148 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
149 while read mode sha1 stage path
151 name
=$
(module_name
"$path") ||
exit
152 url
=$
(git-config submodule.
"$name".url
)
153 if test -z "url" ||
! test -d "$path"/.git
158 revname
=$
(unset GIT_DIR
&& cd "$path" && git-describe
$sha1)
159 if git diff-files
--quiet -- "$path"
161 say
" $sha1 $path ($revname)"
165 sha1
=$
(unset GIT_DIR
&& cd "$path" && git-rev-parse
--verify HEAD
)
166 revname
=$
(unset GIT_DIR
&& cd "$path" && git-describe
$sha1)
168 say
"+$sha1 $path ($revname)"
173 while case "$#" in 0) break ;; esac
204 case "$init,$update,$status,$cached" in