@matheusbsbs / matheus-bagliano / commits / 70c7ac22de
Fork 0

Add git-submodule command

This command can be used to initialize, update and inspect submodules. It uses a .gitmodules file, readable by git-config, in the top level directory of the 'superproject' to specify a mapping between submodule paths and repository url. Example .gitmodules layout: [module "git"] url = git://git.kernel.org/pub/scm/git/git.git With this entry in .gitmodules (and a commit reference in the index entry for the path "git"), the command 'git submodule init' will clone the repository at kernel.org into the directory "git". Known issues ============ There is currently no way to override the url found in the .gitmodules file, except by manually creating the subproject repository. The place to fix this in the script has a rather long comment about a possible plan. Funny paths will be quoted in the output from git-ls-files, but git-submodule does not attempt to unquote (or even detect the presence of) such paths. Signed-off-by: Lars Hjemli <hjemli@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Lars Hjemli committed May 26, 2007 at 15:56 UTC 70c7ac22de681a83621bda03e676348170c8d8a2
5 files changed +262 -1
.gitignore
+1
@@ -126,6 +126,7 @@ git-ssh-push
126 git-ssh-upload
127 git-status
128 git-stripspace
129 +git-submodule
130 git-svn
131 git-svnimport
132 git-symbolic-ref
Documentation/cmd-list.perl
+1
@@ -180,6 +180,7 @@ git-ssh-fetch synchingrepositories
180 git-ssh-upload synchingrepositories
181 git-status mainporcelain
182 git-stripspace purehelpers
183 +git-submodule mainporcelain
184 git-svn foreignscminterface
185 git-svnimport foreignscminterface
186 git-symbolic-ref plumbingmanipulators
Documentation/git-submodule.txt new
+65
@@ -0,0 +1,65 @@
1 +git-submodule(1)
2 +================
3 +
4 +NAME
5 +----
6 +git-submodule - Initialize, update or inspect submodules
7 +
8 +
9 +SYNOPSIS
10 +--------
11 +'git-submodule' [--quiet] [--cached] [status|init|update] [--] [<path>...]
12 +
13 +
14 +COMMANDS
15 +--------
16 +status::
17 + Show the status of the submodules. This will print the SHA-1 of the
18 + currently checked out commit for each submodule, along with the
19 + submodule path and the output of gitlink:git-describe[1] for the
20 + SHA-1. Each SHA-1 will be prefixed with `-` if the submodule is not
21 + initialized and `+` if the currently checked out submodule commit
22 + does not match the SHA-1 found in the index of the containing
23 + repository. This command is the default command for git-submodule.
24 +
25 +init::
26 + Initialize the submodules, i.e. clone the git repositories specified
27 + in the .gitmodules file and checkout the submodule commits specified
28 + in the index of the containing repository. This will make the
29 + submodules HEAD be detached.
30 +
31 +update::
32 + Update the initialized submodules, i.e. checkout the submodule commits
33 + specified in the index of the containing repository. This will make
34 + the submodules HEAD be detached.
35 +
36 +
37 +OPTIONS
38 +-------
39 +-q, --quiet::
40 + Only print error messages.
41 +
42 +--cached::
43 + Display the SHA-1 stored in the index, not the SHA-1 of the currently
44 + checked out submodule commit. This option is only valid for the
45 + status command.
46 +
47 +<path>::
48 + Path to submodule(s). When specified this will restrict the command
49 + to only operate on the submodules found at the specified paths.
50 +
51 +FILES
52 +-----
53 +When cloning submodules, a .gitmodules file in the top-level directory
54 +of the containing repository is used to find the url of each submodule.
55 +This file should be formatted in the same way as $GIR_DIR/config. The key
56 +to each submodule url is "module.$path.url".
57 +
58 +
59 +AUTHOR
60 +------
61 +Written by Lars Hjemli <hjemli@gmail.com>
62 +
63 +GIT
64 +---
65 +Part of the gitlink:git[7] suite
Makefile
+1 -1
@@ -209,7 +209,7 @@ SCRIPT_SH = \
209 git-applymbox.sh git-applypatch.sh git-am.sh \
210 git-merge.sh git-merge-stupid.sh git-merge-octopus.sh \
211 git-merge-resolve.sh git-merge-ours.sh \
212 - git-lost-found.sh git-quiltimport.sh
212 + git-lost-found.sh git-quiltimport.sh git-submodule.sh
213
214 SCRIPT_PERL = \
215 git-add--interactive.perl \
git-submodule.sh new
+194
@@ -0,0 +1,194 @@
1 +#!/bin/sh
2 +#
3 +# git-submodules.sh: init, update or list git submodules
4 +#
5 +# Copyright (c) 2007 Lars Hjemli
6 +
7 +USAGE='[--quiet] [--cached] [status|init|update] [--] [<path>...]'
8 +. git-sh-setup
9 +require_work_tree
10 +
11 +init=
12 +update=
13 +status=
14 +quiet=
15 +cached=
16 +
17 +#
18 +# print stuff on stdout unless -q was specified
19 +#
20 +say()
21 +{
22 + if test -z "$quiet"
23 + then
24 + echo "$@"
25 + fi
26 +}
27 +
28 +#
29 +# Run clone + checkout on missing submodules
30 +#
31 +# $@ = requested paths (default to all)
32 +#
33 +modules_init()
34 +{
35 + git ls-files --stage -- "$@" | grep -e '^160000 ' |
36 + while read mode sha1 stage path
37 + do
38 + # Skip submodule paths that already contain a .git directory.
39 + # This will also trigger if $path is a symlink to a git
40 + # repository
41 + test -d "$path"/.git && continue
42 +
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.
48 + if test -d "$path"
49 + then
50 + rmdir "$path" 2>/dev/null ||
51 + die "Directory '$path' exist, but is neither empty nor a git repository"
52 + fi
53 +
54 + test -e "$path" &&
55 + die "A file already exist at path '$path'"
56 +
57 + url=$(GIT_CONFIG=.gitmodules git-config module."$path".url)
58 + test -z "$url" &&
59 + die "No url found for submodule '$path' in .gitmodules"
60 +
61 + # MAYBE FIXME: this would be the place to check GIT_CONFIG
62 + # for a preferred url for this submodule, possibly like this:
63 + #
64 + # modname=$(GIT_CONFIG=.gitmodules git-config module."$path".name)
65 + # alturl=$(git-config module."$modname".url)
66 + #
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.
71 +
72 + git-clone -n "$url" "$path" ||
73 + die "Clone of submodule '$path' failed"
74 +
75 + (unset GIT_DIR && cd "$path" && git-checkout -q "$sha1") ||
76 + die "Checkout of submodule '$path' failed"
77 +
78 + say "Submodule '$path' initialized"
79 + done
80 +}
81 +
82 +#
83 +# Checkout correct revision of each initialized submodule
84 +#
85 +# $@ = requested paths (default to all)
86 +#
87 +modules_update()
88 +{
89 + git ls-files --stage -- "$@" | grep -e '^160000 ' |
90 + while read mode sha1 stage path
91 + do
92 + if ! test -d "$path"/.git
93 + then
94 + # Only mention uninitialized submodules when its
95 + # path have been specified
96 + test "$#" != "0" &&
97 + say "Submodule '$path' not initialized"
98 + continue;
99 + fi
100 + subsha1=$(unset GIT_DIR && cd "$path" &&
101 + git-rev-parse --verify HEAD) ||
102 + die "Unable to find current revision of submodule '$path'"
103 +
104 + if test "$subsha1" != "$sha1"
105 + then
106 + (unset GIT_DIR && cd "$path" && git-fetch &&
107 + git-checkout -q "$sha1") ||
108 + die "Unable to checkout '$sha1' in submodule '$path'"
109 +
110 + say "Submodule '$path': checked out '$sha1'"
111 + fi
112 + done
113 +}
114 +
115 +#
116 +# List all registered submodules, prefixed with:
117 +# - submodule not initialized
118 +# + different revision checked out
119 +#
120 +# If --cached was specified the revision in the index will be printed
121 +# instead of the currently checked out revision.
122 +#
123 +# $@ = requested paths (default to all)
124 +#
125 +modules_list()
126 +{
127 + git ls-files --stage -- "$@" | grep -e '^160000 ' |
128 + while read mode sha1 stage path
129 + do
130 + if ! test -d "$path"/.git
131 + then
132 + say "-$sha1 $path"
133 + continue;
134 + fi
135 + revname=$(unset GIT_DIR && cd "$path" && git-describe $sha1)
136 + if git diff-files --quiet -- "$path"
137 + then
138 + say " $sha1 $path ($revname)"
139 + else
140 + if test -z "$cached"
141 + then
142 + sha1=$(unset GIT_DIR && cd "$path" && git-rev-parse --verify HEAD)
143 + revname=$(unset GIT_DIR && cd "$path" && git-describe $sha1)
144 + fi
145 + say "+$sha1 $path ($revname)"
146 + fi
147 + done
148 +}
149 +
150 +while case "$#" in 0) break ;; esac
151 +do
152 + case "$1" in
153 + init)
154 + init=1
155 + ;;
156 + update)
157 + update=1
158 + ;;
159 + status)
160 + status=1
161 + ;;
162 + -q|--quiet)
163 + quiet=1
164 + ;;
165 + --cached)
166 + cached=1
167 + ;;
168 + --)
169 + break
170 + ;;
171 + -*)
172 + usage
173 + ;;
174 + *)
175 + break
176 + ;;
177 + esac
178 + shift
179 +done
180 +
181 +case "$init,$update,$status,$cached" in
182 +1,,,)
183 + modules_init "$@"
184 + ;;
185 +,1,,)
186 + modules_update "$@"
187 + ;;
188 +,,*,*)
189 + modules_list "$@"
190 + ;;
191 +*)
192 + usage
193 + ;;
194 +esac