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
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.
44 rmdir "$path" 2>/dev/null ||
45 die "Directory '$path' exist, but is neither empty nor a git repository"
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)
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)
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'"
81 # Update each submodule path to correct revision, using clone and checkout as needed
83 # $@ = requested paths (default to all)
87 git ls-files --stage -- "$@" | grep -e '^160000 ' |
88 while read mode sha1 stage path
90 url=$(git-config submodule."$path".url)
93 # Only mention uninitialized submodules when its
94 # path have been specified
96 say "Submodule '$path' not initialized"
100 if ! test -d "$path"/.git
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'"
110 if test "$subsha1" != "$sha1"
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'"
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)
133 git ls-files --stage -- "$@" | grep -e '^160000 ' |
134 while read mode sha1 stage path
136 if ! test -d "$path"/.git
141 revname=$(unset GIT_DIR && cd "$path" && git-describe $sha1)
142 if git diff-files --quiet -- "$path"
144 say " $sha1 $path ($revname)"
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)"
156 while case "$#" in 0) break ;; esac
187 case "$init,$update,$status,$cached" in