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
37 # If there already is a directory at the submodule path,
38 # expect it to be empty (since that is the default checkout
39 # action) and try to remove it.
40 # Note: if $path is a symlink to a directory the test will
41 # succeed but the rmdir will fail. We might want to fix this.
44 rmdir "$path" 2>/dev
/null ||
45 die
"Directory '$path' exist, but is neither empty nor a git repository"
49 die
"A file already exist at path '$path'"
51 git-clone
-n "$url" "$path" ||
52 die
"Clone of submodule '$path' failed"
56 # Run clone + checkout on missing submodules
58 # $@ = requested paths (default to all)
62 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
63 while read mode sha1 stage path
65 # Skip submodule paths that already contain a .git directory.
66 # This will also trigger if $path is a symlink to a git
68 test -d "$path"/.git
&& continue
70 url
=$
(GIT_CONFIG
=.gitmodules git-config module.
"$path".url
)
72 die
"No url found for submodule '$path' in .gitmodules"
74 # MAYBE FIXME: this would be the place to check GIT_CONFIG
75 # for a preferred url for this submodule, possibly like this:
77 # modname=$(GIT_CONFIG=.gitmodules git-config module."$path".name)
78 # alturl=$(git-config module."$modname".url)
80 # This would let the versioned .gitmodules file use the submodule
81 # path as key, while the unversioned GIT_CONFIG would use the
82 # logical modulename (if present) as key. But this would need
83 # another fallback mechanism if the module wasn't named.
85 module_clone
"$path" "$url" ||
exit
87 (unset GIT_DIR
&& cd "$path" && git-checkout
-q "$sha1") ||
88 die
"Checkout of submodule '$path' failed"
90 say
"Submodule '$path' initialized"
95 # Checkout correct revision of each initialized submodule
97 # $@ = requested paths (default to all)
101 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
102 while read mode sha1 stage path
104 if ! test -d "$path"/.git
106 # Only mention uninitialized submodules when its
107 # path have been specified
109 say
"Submodule '$path' not initialized"
112 subsha1
=$
(unset GIT_DIR
&& cd "$path" &&
113 git-rev-parse
--verify HEAD
) ||
114 die
"Unable to find current revision of submodule '$path'"
116 if test "$subsha1" != "$sha1"
118 (unset GIT_DIR
&& cd "$path" && git-fetch
&&
119 git-checkout
-q "$sha1") ||
120 die
"Unable to checkout '$sha1' in submodule '$path'"
122 say
"Submodule '$path': checked out '$sha1'"
128 # List all registered submodules, prefixed with:
129 # - submodule not initialized
130 # + different revision checked out
132 # If --cached was specified the revision in the index will be printed
133 # instead of the currently checked out revision.
135 # $@ = requested paths (default to all)
139 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
140 while read mode sha1 stage path
142 if ! test -d "$path"/.git
147 revname
=$
(unset GIT_DIR
&& cd "$path" && git-describe
$sha1)
148 if git diff-files
--quiet -- "$path"
150 say
" $sha1 $path ($revname)"
154 sha1
=$
(unset GIT_DIR
&& cd "$path" && git-rev-parse
--verify HEAD
)
155 revname
=$
(unset GIT_DIR
&& cd "$path" && git-describe
$sha1)
157 say
"+$sha1 $path ($revname)"
162 while case "$#" in 0) break ;; esac
193 case "$init,$update,$status,$cached" in