git-submodule: remember to checkout after clone
[git/dscho.git] / git-submodule.sh
blob4a6d64d61cdb2d0ca21cc4521a3955455ab3dd23
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 # Register submodules in .git/config
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 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)
70 test -z "$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'"
77 done
81 # Update each submodule path to correct revision, using clone and checkout as needed
83 # $@ = requested paths (default to all)
85 modules_update()
87 git ls-files --stage -- "$@" | grep -e '^160000 ' |
88 while read mode sha1 stage path
90 url=$(git-config submodule."$path".url)
91 if test -z "$url"
92 then
93 # Only mention uninitialized submodules when its
94 # path have been specified
95 test "$#" != "0" &&
96 say "Submodule '$path' not initialized"
97 continue
100 if ! test -d "$path"/.git
101 then
102 module_clone "$path" "$url" || exit
103 subsha1=
104 else
105 subsha1=$(unset GIT_DIR && cd "$path" &&
106 git-rev-parse --verify HEAD) ||
107 die "Unable to find current revision of submodule '$path'"
110 if test "$subsha1" != "$sha1"
111 then
112 (unset GIT_DIR && cd "$path" && git-fetch &&
113 git-checkout -q "$sha1") ||
114 die "Unable to checkout '$sha1' in submodule '$path'"
116 say "Submodule '$path': checked out '$sha1'"
118 done
122 # List all registered submodules, prefixed with:
123 # - submodule not initialized
124 # + different revision checked out
126 # If --cached was specified the revision in the index will be printed
127 # instead of the currently checked out revision.
129 # $@ = requested paths (default to all)
131 modules_list()
133 git ls-files --stage -- "$@" | grep -e '^160000 ' |
134 while read mode sha1 stage path
136 if ! test -d "$path"/.git
137 then
138 say "-$sha1 $path"
139 continue;
141 revname=$(unset GIT_DIR && cd "$path" && git-describe $sha1)
142 if git diff-files --quiet -- "$path"
143 then
144 say " $sha1 $path ($revname)"
145 else
146 if test -z "$cached"
147 then
148 sha1=$(unset GIT_DIR && cd "$path" && git-rev-parse --verify HEAD)
149 revname=$(unset GIT_DIR && cd "$path" && git-describe $sha1)
151 say "+$sha1 $path ($revname)"
153 done
156 while case "$#" in 0) break ;; esac
158 case "$1" in
159 init)
160 init=1
162 update)
163 update=1
165 status)
166 status=1
168 -q|--quiet)
169 quiet=1
171 --cached)
172 cached=1
175 break
178 usage
181 break
183 esac
184 shift
185 done
187 case "$init,$update,$status,$cached" in
188 1,,,)
189 modules_init "$@"
191 ,1,,)
192 modules_update "$@"
194 ,,*,*)
195 modules_list "$@"
198 usage
200 esac