Skip to content

Commit 77ce907

Browse files
committed
Merge branch 'mt/submodule-reference'
* mt/submodule-reference: Add --reference option to git submodule.
2 parents 14afe77 + d92a395 commit 77ce907

File tree

3 files changed

+127
-6
lines changed

3 files changed

+127
-6
lines changed

Documentation/git-submodule.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ git-submodule - Initialize, update or inspect submodules
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git submodule' [--quiet] add [-b branch] [--] <repository> <path>
12+
'git submodule' [--quiet] add [-b branch]
13+
[--reference <repository>] [--] <repository> <path>
1314
'git submodule' [--quiet] status [--cached] [--] [<path>...]
1415
'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>...]
1618
'git submodule' [--quiet] summary [--summary-limit <n>] [commit] [--] [<path>...]
1719
'git submodule' [--quiet] foreach <command>
1820
'git submodule' [--quiet] sync [--] [<path>...]
@@ -177,6 +179,14 @@ OPTIONS
177179
This option is only valid for the update command.
178180
Don't fetch new objects from the remote site.
179181

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+
180190
<path>...::
181191
Paths to submodule(s). When specified this will restrict the command
182192
to only operate on the submodules found at the specified paths.

git-submodule.sh

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require_work_tree
1515
command=
1616
branch=
1717
quiet=
18+
reference=
1819
cached=
1920
nofetch=
2021

@@ -91,6 +92,7 @@ module_clone()
9192
{
9293
path=$1
9394
url=$2
95+
reference="$3"
9496

9597
# If there already is a directory at the submodule path,
9698
# expect it to be empty (since that is the default checkout
@@ -106,7 +108,12 @@ module_clone()
106108
test -e "$path" &&
107109
die "A file already exist at path '$path'"
108110

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 ||
110117
die "Clone of '$url' into submodule path '$path' failed"
111118
}
112119

@@ -131,6 +138,15 @@ cmd_add()
131138
-q|--quiet)
132139
quiet=1
133140
;;
141+
--reference)
142+
case "$2" in '') usage ;; esac
143+
reference="--reference=$2"
144+
shift
145+
;;
146+
--reference=*)
147+
reference="$1"
148+
shift
149+
;;
134150
--)
135151
shift
136152
break
@@ -203,7 +219,7 @@ cmd_add()
203219
git config submodule."$path".url "$url"
204220
else
205221

206-
module_clone "$path" "$realrepo" || exit
222+
module_clone "$path" "$realrepo" "$reference" || exit
207223
(
208224
unset GIT_DIR
209225
cd "$path" &&
@@ -314,13 +330,22 @@ cmd_update()
314330
quiet=1
315331
;;
316332
-i|--init)
333+
init=1
317334
shift
318-
cmd_init "$@" || return
319335
;;
320336
-N|--no-fetch)
321337
shift
322338
nofetch=1
323339
;;
340+
--reference)
341+
case "$2" in '') usage ;; esac
342+
reference="--reference=$2"
343+
shift 2
344+
;;
345+
--reference=*)
346+
reference="$1"
347+
shift
348+
;;
324349
--)
325350
shift
326351
break
@@ -334,6 +359,11 @@ cmd_update()
334359
esac
335360
done
336361

362+
if test -n "$init"
363+
then
364+
cmd_init "--" "$@" || return
365+
fi
366+
337367
module_list "$@" |
338368
while read mode sha1 stage path
339369
do
@@ -351,7 +381,7 @@ cmd_update()
351381

352382
if ! test -d "$path"/.git -o -f "$path"/.git
353383
then
354-
module_clone "$path" "$url" || exit
384+
module_clone "$path" "$url" "$reference"|| exit
355385
subsha1=
356386
else
357387
subsha1=$(unset GIT_DIR; cd "$path" &&

t/t7406-submodule-reference.sh

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)