git-submodule: provide easy way of adding new submodules
To make a submodule effectively usable, the path and a URL where the submodule can be cloned need to be stored in .gitmodules. This subcommand takes care of setting this information after cloning the new submodule. Only the index is updated, so, if needed, the user may still change the URL or switch to a different branch of the submodule before committing. Signed-off-by: Sven Verdoolaege <skimo@kotnet.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Sven Verdoolaege committed
Jun 24, 2007 at 23:06 UTC
ecda072380e54084d5160d7b6a9cdb23369da804
2 files changed
+107
-6
Documentation/git-submodule.txt
+11
@@ -8,11 +8,19 @@ git-submodule - Initialize, update or inspect submodules
8
9
SYNOPSIS
10
--------
11
+'git-submodule' [--quiet] [-b branch] add <repository> [<path>]
12
'git-submodule' [--quiet] [--cached] [status|init|update] [--] [<path>...]
13
14
15
COMMANDS
16
--------
17
+add::
18
+ Add the given repository as a submodule at the given path
19
+ to the changeset to be committed next. In particular, the
20
+ repository is cloned at the specified path, added to the
21
+ changeset and registered in .gitmodules. If no path is
22
+ specified, the path is deduced from the repository specification.
23
+
24
status::
25
Show the status of the submodules. This will print the SHA-1 of the
26
currently checked out commit for each submodule, along with the
@@ -39,6 +47,9 @@ OPTIONS
47
-q, --quiet::
48
Only print error messages.
49
50
+-b, --branch::
51
+ Branch of repository to add as submodule.
52
+
53
--cached::
54
Display the SHA-1 stored in the index, not the SHA-1 of the currently
55
checked out submodule commit. This option is only valid for the
git-submodule.sh
+96
-6
@@ -1,13 +1,15 @@
1
#!/bin/sh
2
#
3
-# git-submodules.sh: init, update or list git submodules
3
+# git-submodules.sh: add, init, update or list git submodules
4
#
5
# Copyright (c) 2007 Lars Hjemli
6
7
-USAGE='[--quiet] [--cached] [status|init|update] [--] [<path>...]'
7
+USAGE='[--quiet] [--cached] [add <repo> [-b branch]|status|init|update] [--] [<path>...]'
8
. git-sh-setup
9
require_work_tree
10
11
+add=
12
+branch=
13
init=
14
update=
15
status=
@@ -25,6 +27,18 @@ say()
27
fi
28
}
29
30
+# NEEDSWORK: identical function exists in get_repo_base in clone.sh
31
+get_repo_base() {
32
+ (
33
+ cd "`/bin/pwd`" &&
34
+ cd "$1" || cd "$1.git" &&
35
+ {
36
+ cd .git
37
+ pwd
38
+ }
39
+ ) 2>/dev/null
40
+}
41
+
42
#
43
# Map submodule path to submodule name
44
#
@@ -42,6 +56,11 @@ module_name()
56
#
57
# Clone a submodule
58
#
59
+# Prior to calling, modules_update checks that a possibly existing
60
+# path is not a git repository.
61
+# Likewise, module_add checks that path does not exist at all,
62
+# since it is the location of a new submodule.
63
+#
64
module_clone()
65
{
66
path=$1
@@ -65,6 +84,53 @@ module_clone()
84
die "Clone of '$url' into submodule path '$path' failed"
85
}
86
87
+#
88
+# Add a new submodule to the working tree, .gitmodules and the index
89
+#
90
+# $@ = repo [path]
91
+#
92
+# optional branch is stored in global branch variable
93
+#
94
+module_add()
95
+{
96
+ repo=$1
97
+ path=$2
98
+
99
+ if test -z "$repo"; then
100
+ usage
101
+ fi
102
+
103
+ # Turn the source into an absolute path if
104
+ # it is local
105
+ if base=$(get_repo_base "$repo"); then
106
+ repo="$base"
107
+ fi
108
+
109
+ # Guess path from repo if not specified or strip trailing slashes
110
+ if test -z "$path"; then
111
+ path=$(echo "$repo" | sed -e 's|/*$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
112
+ else
113
+ path=$(echo "$path" | sed -e 's|/*$||')
114
+ fi
115
+
116
+ test -e "$path" &&
117
+ die "'$path' already exists"
118
+
119
+ git-ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
120
+ die "'$path' already exists in the index"
121
+
122
+ module_clone "$path" "$repo" || exit
123
+ (unset GIT_DIR && cd "$path" && git checkout -q ${branch:+-b "$branch" "origin/$branch"}) ||
124
+ die "Unable to checkout submodule '$path'"
125
+ git add "$path" ||
126
+ die "Failed to add submodule '$path'"
127
+
128
+ GIT_CONFIG=.gitmodules git config submodule."$path".path "$path" &&
129
+ GIT_CONFIG=.gitmodules git config submodule."$path".url "$repo" &&
130
+ git add .gitmodules ||
131
+ die "Failed to register submodule '$path'"
132
+}
133
+
134
#
135
# Register submodules in .git/config
136
#
@@ -173,6 +239,9 @@ modules_list()
239
while case "$#" in 0) break ;; esac
240
do
241
case "$1" in
242
+ add)
243
+ add=1
244
+ ;;
245
init)
246
init=1
247
;;
@@ -185,6 +254,14 @@ do
254
-q|--quiet)
255
quiet=1
256
;;
257
+ -b|--branch)
258
+ case "$2" in
259
+ '')
260
+ usage
261
+ ;;
262
+ esac
263
+ branch="$2"; shift
264
+ ;;
265
--cached)
266
cached=1
267
;;
@@ -201,14 +278,27 @@ do
278
shift
279
done
280
204
-case "$init,$update,$status,$cached" in
205
-1,,,)
281
+case "$add,$branch" in
282
+1,*)
283
+ ;;
284
+,)
285
+ ;;
286
+,*)
287
+ usage
288
+ ;;
289
+esac
290
+
291
+case "$add,$init,$update,$status,$cached" in
292
+1,,,,)
293
+ module_add "$@"
294
+ ;;
295
+,1,,,)
296
modules_init "$@"
297
;;
208
-,1,,)
298
+,,1,,)
299
modules_update "$@"
300
;;
211
-,,*,*)
301
+,,,1,*)
302
modules_list "$@"
303
;;
304
*)