More missing static
[git/kirr.git] / git-submodule.sh
blob6ed5a6ced23d99794de56e326bd46438d8c64707
1 #!/bin/sh
3 # git-submodules.sh: init, update or list git submodules
5 # Copyright (c) 2007 Lars Hjemli
7 USAGE='[--quiet] [--cached] [status|init|update] [--] [<path>...]'
8 . git-sh-setup
9 require_work_tree
11 init=
12 update=
13 status=
14 quiet=
15 cached=
18 # print stuff on stdout unless -q was specified
20 say()
22 if test -z "$quiet"
23 then
24 echo "$@"
29 # Run clone + checkout on missing submodules
31 # $@ = requested paths (default to all)
33 modules_init()
35 git ls-files --stage -- "$@" | grep -e '^160000 ' |
36 while read mode sha1 stage path
38 # Skip submodule paths that already contain a .git directory.
39 # This will also trigger if $path is a symlink to a git
40 # repository
41 test -d "$path"/.git && continue
43 # If there already is a directory at the submodule path,
44 # expect it to be empty (since that is the default checkout
45 # action) and try to remove it.
46 # Note: if $path is a symlink to a directory the test will
47 # succeed but the rmdir will fail. We might want to fix this.
48 if test -d "$path"
49 then
50 rmdir "$path" 2>/dev/null ||
51 die "Directory '$path' exist, but is neither empty nor a git repository"
54 test -e "$path" &&
55 die "A file already exist at path '$path'"
57 url=$(GIT_CONFIG=.gitmodules git-config module."$path".url)
58 test -z "$url" &&
59 die "No url found for submodule '$path' in .gitmodules"
61 # MAYBE FIXME: this would be the place to check GIT_CONFIG
62 # for a preferred url for this submodule, possibly like this:
64 # modname=$(GIT_CONFIG=.gitmodules git-config module."$path".name)
65 # alturl=$(git-config module."$modname".url)
67 # This would let the versioned .gitmodules file use the submodule
68 # path as key, while the unversioned GIT_CONFIG would use the
69 # logical modulename (if present) as key. But this would need
70 # another fallback mechanism if the module wasn't named.
72 git-clone -n "$url" "$path" ||
73 die "Clone of submodule '$path' failed"
75 (unset GIT_DIR && cd "$path" && git-checkout -q "$sha1") ||
76 die "Checkout of submodule '$path' failed"
78 say "Submodule '$path' initialized"
79 done
83 # Checkout correct revision of each initialized submodule
85 # $@ = requested paths (default to all)
87 modules_update()
89 git ls-files --stage -- "$@" | grep -e '^160000 ' |
90 while read mode sha1 stage path
92 if ! test -d "$path"/.git
93 then
94 # Only mention uninitialized submodules when its
95 # path have been specified
96 test "$#" != "0" &&
97 say "Submodule '$path' not initialized"
98 continue;
100 subsha1=$(unset GIT_DIR && cd "$path" &&
101 git-rev-parse --verify HEAD) ||
102 die "Unable to find current revision of submodule '$path'"
104 if test "$subsha1" != "$sha1"
105 then
106 (unset GIT_DIR && cd "$path" && git-fetch &&
107 git-checkout -q "$sha1") ||
108 die "Unable to checkout '$sha1' in submodule '$path'"
110 say "Submodule '$path': checked out '$sha1'"
112 done
116 # List all registered submodules, prefixed with:
117 # - submodule not initialized
118 # + different revision checked out
120 # If --cached was specified the revision in the index will be printed
121 # instead of the currently checked out revision.
123 # $@ = requested paths (default to all)
125 modules_list()
127 git ls-files --stage -- "$@" | grep -e '^160000 ' |
128 while read mode sha1 stage path
130 if ! test -d "$path"/.git
131 then
132 say "-$sha1 $path"
133 continue;
135 revname=$(unset GIT_DIR && cd "$path" && git-describe $sha1)
136 if git diff-files --quiet -- "$path"
137 then
138 say " $sha1 $path ($revname)"
139 else
140 if test -z "$cached"
141 then
142 sha1=$(unset GIT_DIR && cd "$path" && git-rev-parse --verify HEAD)
143 revname=$(unset GIT_DIR && cd "$path" && git-describe $sha1)
145 say "+$sha1 $path ($revname)"
147 done
150 while case "$#" in 0) break ;; esac
152 case "$1" in
153 init)
154 init=1
156 update)
157 update=1
159 status)
160 status=1
162 -q|--quiet)
163 quiet=1
165 --cached)
166 cached=1
169 break
172 usage
175 break
177 esac
178 shift
179 done
181 case "$init,$update,$status,$cached" in
182 1,,,)
183 modules_init "$@"
185 ,1,,)
186 modules_update "$@"
188 ,,*,*)
189 modules_list "$@"
192 usage
194 esac