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 # Register submodules in .git/config
58 # $@ = requested paths (default to all)
62 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
63 while read mode sha1 stage path
65 # Skip already registered paths
66 url
=$
(git-config submodule.
"$path".url
)
67 test -z "$url" ||
continue
69 url
=$
(GIT_CONFIG
=.gitmodules git-config module.
"$path".url
)
71 die
"No url found for submodule '$path' in .gitmodules"
73 git-config submodule.
"$path".url
"$url" ||
74 die
"Failed to register url for submodule '$path'"
76 say
"Submodule '$path' registered with url '$url'"
81 # Update each submodule path to correct revision, using clone and checkout as needed
83 # $@ = requested paths (default to all)
87 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
88 while read mode sha1 stage path
90 url
=$
(git-config submodule.
"$path".url
)
93 # Only mention uninitialized submodules when its
94 # path have been specified
96 say
"Submodule '$path' not initialized"
100 if ! test -d "$path"/.git
102 module_clone
"$path" "$url" ||
exit
105 subsha1
=$
(unset GIT_DIR
&& cd "$path" &&
106 git-rev-parse
--verify HEAD
) ||
107 die
"Unable to find current revision of submodule '$path'"
109 if test "$subsha1" != "$sha1"
111 (unset GIT_DIR
&& cd "$path" && git-fetch
&&
112 git-checkout
-q "$sha1") ||
113 die
"Unable to checkout '$sha1' in submodule '$path'"
115 say
"Submodule '$path': checked out '$sha1'"
121 # List all registered submodules, prefixed with:
122 # - submodule not initialized
123 # + different revision checked out
125 # If --cached was specified the revision in the index will be printed
126 # instead of the currently checked out revision.
128 # $@ = requested paths (default to all)
132 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
133 while read mode sha1 stage path
135 if ! test -d "$path"/.git
140 revname
=$
(unset GIT_DIR
&& cd "$path" && git-describe
$sha1)
141 if git diff-files
--quiet -- "$path"
143 say
" $sha1 $path ($revname)"
147 sha1
=$
(unset GIT_DIR
&& cd "$path" && git-rev-parse
--verify HEAD
)
148 revname
=$
(unset GIT_DIR
&& cd "$path" && git-describe
$sha1)
150 say
"+$sha1 $path ($revname)"
155 while case "$#" in 0) break ;; esac
186 case "$init,$update,$status,$cached" in