Skip to content

Commit 539e750

Browse files
grumbelbartgitster
authored andcommitted
sha1_file: do not add own object directory as alternate
When adding alternate object directories, we try not to add the directory of the current repository to avoid cycles. Unfortunately, that test was broken, since it compared an absolute with a relative path. Signed-off-by: Ephrim Khong <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 45067fc commit 539e750

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

sha1_file.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,9 @@ static int git_open_noatime(const char *name);
256256
* SHA1, an extra slash for the first level indirection, and the
257257
* terminating NUL.
258258
*/
259-
static int link_alt_odb_entry(const char *entry, const char *relative_base, int depth)
259+
static int link_alt_odb_entry(const char *entry, const char *relative_base,
260+
int depth, const char *normalized_objdir)
260261
{
261-
const char *objdir = get_object_directory();
262262
struct alternate_object_database *ent;
263263
struct alternate_object_database *alt;
264264
int pfxlen, entlen;
@@ -308,7 +308,7 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base, int
308308
return -1;
309309
}
310310
}
311-
if (!strcmp(ent->base, objdir)) {
311+
if (!strcmp_icase(ent->base, normalized_objdir)) {
312312
free(ent);
313313
return -1;
314314
}
@@ -332,13 +332,17 @@ static void link_alt_odb_entries(const char *alt, int len, int sep,
332332
struct string_list entries = STRING_LIST_INIT_NODUP;
333333
char *alt_copy;
334334
int i;
335+
struct strbuf objdirbuf = STRBUF_INIT;
335336

336337
if (depth > 5) {
337338
error("%s: ignoring alternate object stores, nesting too deep.",
338339
relative_base);
339340
return;
340341
}
341342

343+
strbuf_addstr(&objdirbuf, absolute_path(get_object_directory()));
344+
normalize_path_copy(objdirbuf.buf, objdirbuf.buf);
345+
342346
alt_copy = xmemdupz(alt, len);
343347
string_list_split_in_place(&entries, alt_copy, sep, -1);
344348
for (i = 0; i < entries.nr; i++) {
@@ -349,11 +353,12 @@ static void link_alt_odb_entries(const char *alt, int len, int sep,
349353
error("%s: ignoring relative alternate object store %s",
350354
relative_base, entry);
351355
} else {
352-
link_alt_odb_entry(entry, relative_base, depth);
356+
link_alt_odb_entry(entry, relative_base, depth, objdirbuf.buf);
353357
}
354358
}
355359
string_list_clear(&entries, 0);
356360
free(alt_copy);
361+
strbuf_release(&objdirbuf);
357362
}
358363

359364
void read_info_alternates(const char * relative_base, int depth)

t/t7702-repack-cyclic-alternate.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (c) 2014 Ephrim Khong
4+
#
5+
6+
test_description='repack involving cyclic alternate'
7+
. ./test-lib.sh
8+
9+
test_expect_success setup '
10+
GIT_OBJECT_DIRECTORY=.git//../.git/objects &&
11+
export GIT_OBJECT_DIRECTORY &&
12+
touch a &&
13+
git add a &&
14+
git commit -m 1 &&
15+
git repack -adl &&
16+
echo "$(pwd)"/.git/objects/../objects >.git/objects/info/alternates
17+
'
18+
19+
test_expect_success 're-packing repository with itsself as alternate' '
20+
git repack -adl &&
21+
git fsck
22+
'
23+
24+
test_done

0 commit comments

Comments
 (0)