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
29 # Run clone + checkout on missing submodules
31 # $@ = requested paths (default to all)
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
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.
50 rmdir "$path" 2>/dev/null ||
51 die "Directory '$path' exist, but is neither empty nor a git repository"
55 die "A file already exist at path '$path'"
57 url=$(GIT_CONFIG=.gitmodules git-config module."$path".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"
83 # Checkout correct revision of each initialized submodule
85 # $@ = requested paths (default to all)
89 git ls-files --stage -- "$@" | grep -e '^160000 ' |
90 while read mode sha1 stage path
92 if ! test -d "$path"/.git
94 # Only mention uninitialized submodules when its
95 # path have been specified
97 say "Submodule '$path' not initialized"
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"
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'"
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)
127 git ls-files --stage -- "$@" | grep -e '^160000 ' |
128 while read mode sha1 stage path
130 if ! test -d "$path"/.git
135 revname=$(unset GIT_DIR && cd "$path" && git-describe $sha1)
136 if git diff-files --quiet -- "$path"
138 say " $sha1 $path ($revname)"
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)"
150 while case "$#" in 0) break ;; esac
181 case "$init,$update,$status,$cached" in