Skip to content

Commit b552b56

Browse files
aschrabgitster
authored andcommitted
clone: Allow repo using gitfile as a reference
Try reading gitfile files when processing --reference options to clone. This will allow, among other things, using a submodule checked out with a recent version of git as a reference repository without requiring the user to have internal knowledge of submodule layout. Signed-off-by: Aaron Schrab <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0658569 commit b552b56

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

builtin/clone.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,21 @@ static void strip_trailing_slashes(char *dir)
232232
static int add_one_reference(struct string_list_item *item, void *cb_data)
233233
{
234234
char *ref_git;
235+
const char *repo;
235236
struct strbuf alternate = STRBUF_INIT;
236237

237-
/* Beware: real_path() and mkpath() return static buffer */
238+
/* Beware: read_gitfile(), real_path() and mkpath() return static buffer */
238239
ref_git = xstrdup(real_path(item->string));
239-
if (is_directory(mkpath("%s/.git/objects", ref_git))) {
240+
241+
repo = read_gitfile(ref_git);
242+
if (!repo)
243+
repo = read_gitfile(mkpath("%s/.git", ref_git));
244+
if (repo) {
245+
free(ref_git);
246+
ref_git = xstrdup(repo);
247+
}
248+
249+
if (!repo && is_directory(mkpath("%s/.git/objects", ref_git))) {
240250
char *ref_git_git = mkpathdup("%s/.git", ref_git);
241251
free(ref_git);
242252
ref_git = ref_git_git;

t/t5700-clone-reference.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,17 @@ test_expect_success 'fetch with incomplete alternates' '
185185
! grep " want $tag_object" "$U.K"
186186
'
187187

188+
test_expect_success 'clone using repo with gitfile as a reference' '
189+
git clone --separate-git-dir=L A M &&
190+
git clone --reference=M A N &&
191+
echo "$base_dir/L/objects" >expected &&
192+
test_cmp expected "$base_dir/N/.git/objects/info/alternates"
193+
'
194+
195+
test_expect_success 'clone using repo pointed at by gitfile as reference' '
196+
git clone --reference=M/.git A O &&
197+
echo "$base_dir/L/objects" >expected &&
198+
test_cmp expected "$base_dir/O/.git/objects/info/alternates"
199+
'
200+
188201
test_done

0 commit comments

Comments
 (0)