Add --reference option to git submodule.
This adds --reference option to git submodule add and git submodule update commands, which is passed to git clone. Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Michael S. Tsirkin committed
May 4, 2009 at 22:30 UTC
d92a39590d1126e195f1bbccf182a2cdb79218e7
3 files changed
+127
-6
Documentation/git-submodule.txt
+12
-2
@@ -9,10 +9,12 @@ git-submodule - Initialize, update or inspect submodules
9
SYNOPSIS
10
--------
11
[verse]
12
-'git submodule' [--quiet] add [-b branch] [--] <repository> <path>
12
+'git submodule' [--quiet] add [-b branch]
13
+ [--reference <repository>] [--] <repository> <path>
14
'git submodule' [--quiet] status [--cached] [--] [<path>...]
15
'git submodule' [--quiet] init [--] [<path>...]
15
-'git submodule' [--quiet] update [--init] [-N|--no-fetch] [--] [<path>...]
16
+'git submodule' [--quiet] update [--init] [-N|--no-fetch]
17
+ [--reference <repository>] [--] [<path>...]
18
'git submodule' [--quiet] summary [--summary-limit <n>] [commit] [--] [<path>...]
19
'git submodule' [--quiet] foreach <command>
20
'git submodule' [--quiet] sync [--] [<path>...]
@@ -177,6 +179,14 @@ OPTIONS
179
This option is only valid for the update command.
180
Don't fetch new objects from the remote site.
181
182
+--reference <repository>::
183
+ This option is only valid for add and update commands. These
184
+ commands sometimes need to clone a remote repository. In this case,
185
+ this option will be passed to the linkgit:git-clone[1] command.
186
++
187
+*NOTE*: Do *not* use this option unless you have read the note
188
+for linkgit:git-clone[1]'s --reference and --shared options carefully.
189
+
190
<path>...::
191
Paths to submodule(s). When specified this will restrict the command
192
to only operate on the submodules found at the specified paths.
git-submodule.sh
+34
-4
@@ -15,6 +15,7 @@ require_work_tree
15
command=
16
branch=
17
quiet=
18
+reference=
19
cached=
20
nofetch=
21
@@ -91,6 +92,7 @@ module_clone()
92
{
93
path=$1
94
url=$2
95
+ reference="$3"
96
97
# If there already is a directory at the submodule path,
98
# expect it to be empty (since that is the default checkout
@@ -106,7 +108,12 @@ module_clone()
108
test -e "$path" &&
109
die "A file already exist at path '$path'"
110
109
- git-clone -n "$url" "$path" ||
111
+ if test -n "$reference"
112
+ then
113
+ git-clone "$reference" -n "$url" "$path"
114
+ else
115
+ git-clone -n "$url" "$path"
116
+ fi ||
117
die "Clone of '$url' into submodule path '$path' failed"
118
}
119
@@ -131,6 +138,15 @@ cmd_add()
138
-q|--quiet)
139
quiet=1
140
;;
141
+ --reference)
142
+ case "$2" in '') usage ;; esac
143
+ reference="--reference=$2"
144
+ shift
145
+ ;;
146
+ --reference=*)
147
+ reference="$1"
148
+ shift
149
+ ;;
150
--)
151
shift
152
break
@@ -203,7 +219,7 @@ cmd_add()
219
git config submodule."$path".url "$url"
220
else
221
206
- module_clone "$path" "$realrepo" || exit
222
+ module_clone "$path" "$realrepo" "$reference" || exit
223
(
224
unset GIT_DIR
225
cd "$path" &&
@@ -314,13 +330,22 @@ cmd_update()
330
quiet=1
331
;;
332
-i|--init)
333
+ init=1
334
shift
318
- cmd_init "$@" || return
335
;;
336
-N|--no-fetch)
337
shift
338
nofetch=1
339
;;
340
+ --reference)
341
+ case "$2" in '') usage ;; esac
342
+ reference="--reference=$2"
343
+ shift 2
344
+ ;;
345
+ --reference=*)
346
+ reference="$1"
347
+ shift
348
+ ;;
349
--)
350
shift
351
break
@@ -334,6 +359,11 @@ cmd_update()
359
esac
360
done
361
362
+ if test -n "$init"
363
+ then
364
+ cmd_init "--" "$@" || return
365
+ fi
366
+
367
module_list "$@" |
368
while read mode sha1 stage path
369
do
@@ -351,7 +381,7 @@ cmd_update()
381
382
if ! test -d "$path"/.git -o -f "$path"/.git
383
then
354
- module_clone "$path" "$url" || exit
384
+ module_clone "$path" "$url" "$reference"|| exit
385
subsha1=
386
else
387
subsha1=$(unset GIT_DIR; cd "$path" &&
t/t7406-submodule-reference.sh
new
+81
@@ -0,0 +1,81 @@
1
+#!/bin/sh
2
+#
3
+# Copyright (c) 2009, Red Hat Inc, Author: Michael S. Tsirkin (mst@redhat.com)
4
+#
5
+
6
+test_description='test clone --reference'
7
+. ./test-lib.sh
8
+
9
+base_dir=`pwd`
10
+
11
+U=$base_dir/UPLOAD_LOG
12
+
13
+test_expect_success 'preparing first repository' \
14
+'test_create_repo A && cd A &&
15
+echo first > file1 &&
16
+git add file1 &&
17
+git commit -m A-initial'
18
+
19
+cd "$base_dir"
20
+
21
+test_expect_success 'preparing second repository' \
22
+'git clone A B && cd B &&
23
+echo second > file2 &&
24
+git add file2 &&
25
+git commit -m B-addition &&
26
+git repack -a -d &&
27
+git prune'
28
+
29
+cd "$base_dir"
30
+
31
+test_expect_success 'preparing supermodule' \
32
+'test_create_repo super && cd super &&
33
+echo file > file &&
34
+git add file &&
35
+git commit -m B-super-initial'
36
+
37
+cd "$base_dir"
38
+
39
+test_expect_success 'submodule add --reference' \
40
+'cd super && git submodule add --reference ../B "file://$base_dir/A" sub &&
41
+git commit -m B-super-added'
42
+
43
+cd "$base_dir"
44
+
45
+test_expect_success 'after add: existence of info/alternates' \
46
+'test `wc -l <super/sub/.git/objects/info/alternates` = 1'
47
+
48
+cd "$base_dir"
49
+
50
+test_expect_success 'that reference gets used with add' \
51
+'cd super/sub &&
52
+echo "0 objects, 0 kilobytes" > expected &&
53
+git count-objects > current &&
54
+diff expected current'
55
+
56
+cd "$base_dir"
57
+
58
+test_expect_success 'cloning supermodule' \
59
+'git clone super super-clone'
60
+
61
+cd "$base_dir"
62
+
63
+test_expect_success 'update with reference' \
64
+'cd super-clone && git submodule update --init --reference ../B'
65
+
66
+cd "$base_dir"
67
+
68
+test_expect_success 'after update: existence of info/alternates' \
69
+'test `wc -l <super-clone/sub/.git/objects/info/alternates` = 1'
70
+
71
+cd "$base_dir"
72
+
73
+test_expect_success 'that reference gets used with update' \
74
+'cd super-clone/sub &&
75
+echo "0 objects, 0 kilobytes" > expected &&
76
+git count-objects > current &&
77
+diff expected current'
78
+
79
+cd "$base_dir"
80
+
81
+test_done