git-submodule: move cloning into a separate function
[git/mingw.git] / git-submodule.sh
blob486d3b212484b2ac8d33f1fb8a1c65c6fded7dc5
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 "$@"
30 # Clone a submodule
32 module_clone()
34 path=$1
35 url=$2
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.
42 if test -d "$path"
43 then
44 rmdir "$path" 2>/dev/null ||
45 die "Directory '$path' exist, but is neither empty nor a git repository"
48 test -e "$path" &&
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)
60 modules_init()
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
67 # repository
68 test -d "$path"/.git && continue
70 url=$(GIT_CONFIG=.gitmodules git-config module."$path".url)
71 test -z "$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"
91 done
95 # Checkout correct revision of each initialized submodule
97 # $@ = requested paths (default to all)
99 modules_update()
101 git ls-files --stage -- "$@" | grep -e '^160000 ' |
102 while read mode sha1 stage path
104 if ! test -d "$path"/.git
105 then
106 # Only mention uninitialized submodules when its
107 # path have been specified
108 test "$#" != "0" &&
109 say "Submodule '$path' not initialized"
110 continue;
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"
117 then
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'"
124 done
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)
137 modules_list()
139 git ls-files --stage -- "$@" | grep -e '^160000 ' |
140 while read mode sha1 stage path
142 if ! test -d "$path"/.git
143 then
144 say "-$sha1 $path"
145 continue;
147 revname=$(unset GIT_DIR && cd "$path" && git-describe $sha1)
148 if git diff-files --quiet -- "$path"
149 then
150 say " $sha1 $path ($revname)"
151 else
152 if test -z "$cached"
153 then
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)"
159 done
162 while case "$#" in 0) break ;; esac
164 case "$1" in
165 init)
166 init=1
168 update)
169 update=1
171 status)
172 status=1
174 -q|--quiet)
175 quiet=1
177 --cached)
178 cached=1
181 break
184 usage
187 break
189 esac
190 shift
191 done
193 case "$init,$update,$status,$cached" in
194 1,,,)
195 modules_init "$@"
197 ,1,,)
198 modules_update "$@"
200 ,,*,*)
201 modules_list "$@"
204 usage
206 esac