git-svn: test for creating new directories over svn://
[git/jrn.git] / git-submodule.sh
blob8bdd99a2f37a8c76fd4b08e6736d7a67da47be1e
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
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"
110 then
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'"
117 done
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)
130 modules_list()
132 git ls-files --stage -- "$@" | grep -e '^160000 ' |
133 while read mode sha1 stage path
135 if ! test -d "$path"/.git
136 then
137 say "-$sha1 $path"
138 continue;
140 revname=$(unset GIT_DIR && cd "$path" && git-describe $sha1)
141 if git diff-files --quiet -- "$path"
142 then
143 say " $sha1 $path ($revname)"
144 else
145 if test -z "$cached"
146 then
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)"
152 done
155 while case "$#" in 0) break ;; esac
157 case "$1" in
158 init)
159 init=1
161 update)
162 update=1
164 status)
165 status=1
167 -q|--quiet)
168 quiet=1
170 --cached)
171 cached=1
174 break
177 usage
180 break
182 esac
183 shift
184 done
186 case "$init,$update,$status,$cached" in
187 1,,,)
188 modules_init "$@"
190 ,1,,)
191 modules_update "$@"
193 ,,*,*)
194 modules_list "$@"
197 usage
199 esac