Transitively read alternatives
When adding an alternate object store then add entries from its info/alternates files, too. Relative entries are only allowed in the current repository. Loops and duplicate alternates through multiple repositories are ignored. Just to be sure that nothing breaks it is not allow to build deep nesting levels using info/alternates. Signed-off-by: Martin Waitz <tali@admingilde.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Martin Waitz committed
May 7, 2006 at 20:19 UTC
c2f493a4ae159ef99b20be490ae603cd13bab4eb
1 file changed
+106
-72
sha1_file.c
+106
-72
@@ -217,6 +217,8 @@ char *sha1_pack_index_name(const unsigned char *sha1)
217
struct alternate_object_database *alt_odb_list;
218
static struct alternate_object_database **alt_odb_tail;
219
220
+static void read_info_alternates(const char * alternates, int depth);
221
+
222
/*
223
* Prepare alternate object database registry.
224
*
@@ -232,14 +234,85 @@ static struct alternate_object_database **alt_odb_tail;
234
* SHA1, an extra slash for the first level indirection, and the
235
* terminating NUL.
236
*/
235
-static void link_alt_odb_entries(const char *alt, const char *ep, int sep,
236
- const char *relative_base)
237
+static int link_alt_odb_entry(const char * entry, int len, const char * relative_base, int depth)
238
{
238
- const char *cp, *last;
239
- struct alternate_object_database *ent;
239
+ struct stat st;
240
const char *objdir = get_object_directory();
241
+ struct alternate_object_database *ent;
242
+ struct alternate_object_database *alt;
243
+ /* 43 = 40-byte + 2 '/' + terminating NUL */
244
+ int pfxlen = len;
245
+ int entlen = pfxlen + 43;
246
int base_len = -1;
247
248
+ if (*entry != '/' && relative_base) {
249
+ /* Relative alt-odb */
250
+ if (base_len < 0)
251
+ base_len = strlen(relative_base) + 1;
252
+ entlen += base_len;
253
+ pfxlen += base_len;
254
+ }
255
+ ent = xmalloc(sizeof(*ent) + entlen);
256
+
257
+ if (*entry != '/' && relative_base) {
258
+ memcpy(ent->base, relative_base, base_len - 1);
259
+ ent->base[base_len - 1] = '/';
260
+ memcpy(ent->base + base_len, entry, len);
261
+ }
262
+ else
263
+ memcpy(ent->base, entry, pfxlen);
264
+
265
+ ent->name = ent->base + pfxlen + 1;
266
+ ent->base[pfxlen + 3] = '/';
267
+ ent->base[pfxlen] = ent->base[entlen-1] = 0;
268
+
269
+ /* Detect cases where alternate disappeared */
270
+ if (stat(ent->base, &st) || !S_ISDIR(st.st_mode)) {
271
+ error("object directory %s does not exist; "
272
+ "check .git/objects/info/alternates.",
273
+ ent->base);
274
+ free(ent);
275
+ return -1;
276
+ }
277
+
278
+ /* Prevent the common mistake of listing the same
279
+ * thing twice, or object directory itself.
280
+ */
281
+ for (alt = alt_odb_list; alt; alt = alt->next) {
282
+ if (!memcmp(ent->base, alt->base, pfxlen)) {
283
+ free(ent);
284
+ return -1;
285
+ }
286
+ }
287
+ if (!memcmp(ent->base, objdir, pfxlen)) {
288
+ free(ent);
289
+ return -1;
290
+ }
291
+
292
+ /* add the alternate entry */
293
+ *alt_odb_tail = ent;
294
+ alt_odb_tail = &(ent->next);
295
+ ent->next = NULL;
296
+
297
+ /* recursively add alternates */
298
+ read_info_alternates(ent->base, depth + 1);
299
+
300
+ ent->base[pfxlen] = '/';
301
+
302
+ return 0;
303
+}
304
+
305
+static void link_alt_odb_entries(const char *alt, const char *ep, int sep,
306
+ const char *relative_base, int depth)
307
+{
308
+ const char *cp, *last;
309
+
310
+ if (depth > 5) {
311
+ error("%s: ignoring alternate object stores, nesting too deep.",
312
+ relative_base);
313
+ return;
314
+ }
315
+
316
last = alt;
317
while (last < ep) {
318
cp = last;
@@ -249,60 +322,15 @@ static void link_alt_odb_entries(const char *alt, const char *ep, int sep,
322
last = cp + 1;
323
continue;
324
}
252
- for ( ; cp < ep && *cp != sep; cp++)
253
- ;
325
+ while (cp < ep && *cp != sep)
326
+ cp++;
327
if (last != cp) {
255
- struct stat st;
256
- struct alternate_object_database *alt;
257
- /* 43 = 40-byte + 2 '/' + terminating NUL */
258
- int pfxlen = cp - last;
259
- int entlen = pfxlen + 43;
260
-
261
- if (*last != '/' && relative_base) {
262
- /* Relative alt-odb */
263
- if (base_len < 0)
264
- base_len = strlen(relative_base) + 1;
265
- entlen += base_len;
266
- pfxlen += base_len;
267
- }
268
- ent = xmalloc(sizeof(*ent) + entlen);
269
-
270
- if (*last != '/' && relative_base) {
271
- memcpy(ent->base, relative_base, base_len - 1);
272
- ent->base[base_len - 1] = '/';
273
- memcpy(ent->base + base_len,
274
- last, cp - last);
275
- }
276
- else
277
- memcpy(ent->base, last, pfxlen);
278
-
279
- ent->name = ent->base + pfxlen + 1;
280
- ent->base[pfxlen + 3] = '/';
281
- ent->base[pfxlen] = ent->base[entlen-1] = 0;
282
-
283
- /* Detect cases where alternate disappeared */
284
- if (stat(ent->base, &st) || !S_ISDIR(st.st_mode)) {
285
- error("object directory %s does not exist; "
286
- "check .git/objects/info/alternates.",
287
- ent->base);
288
- goto bad;
289
- }
290
- ent->base[pfxlen] = '/';
291
-
292
- /* Prevent the common mistake of listing the same
293
- * thing twice, or object directory itself.
294
- */
295
- for (alt = alt_odb_list; alt; alt = alt->next)
296
- if (!memcmp(ent->base, alt->base, pfxlen))
297
- goto bad;
298
- if (!memcmp(ent->base, objdir, pfxlen)) {
299
- bad:
300
- free(ent);
301
- }
302
- else {
303
- *alt_odb_tail = ent;
304
- alt_odb_tail = &(ent->next);
305
- ent->next = NULL;
328
+ if ((*last != '/') && depth) {
329
+ error("%s: ignoring relative alternate object store %s",
330
+ relative_base, last);
331
+ } else {
332
+ link_alt_odb_entry(last, cp - last,
333
+ relative_base, depth);
334
}
335
}
336
while (cp < ep && *cp == sep)
@@ -311,23 +339,14 @@ static void link_alt_odb_entries(const char *alt, const char *ep, int sep,
339
}
340
}
341
314
-void prepare_alt_odb(void)
342
+static void read_info_alternates(const char * relative_base, int depth)
343
{
316
- char path[PATH_MAX];
344
char *map;
318
- int fd;
345
struct stat st;
320
- char *alt;
321
-
322
- alt = getenv(ALTERNATE_DB_ENVIRONMENT);
323
- if (!alt) alt = "";
324
-
325
- if (alt_odb_tail)
326
- return;
327
- alt_odb_tail = &alt_odb_list;
328
- link_alt_odb_entries(alt, alt + strlen(alt), ':', NULL);
346
+ char path[PATH_MAX];
347
+ int fd;
348
330
- sprintf(path, "%s/info/alternates", get_object_directory());
349
+ sprintf(path, "%s/info/alternates", relative_base);
350
fd = open(path, O_RDONLY);
351
if (fd < 0)
352
return;
@@ -340,11 +359,26 @@ void prepare_alt_odb(void)
359
if (map == MAP_FAILED)
360
return;
361
343
- link_alt_odb_entries(map, map + st.st_size, '\n',
344
- get_object_directory());
362
+ link_alt_odb_entries(map, map + st.st_size, '\n', relative_base, depth);
363
+
364
munmap(map, st.st_size);
365
}
366
367
+void prepare_alt_odb(void)
368
+{
369
+ char *alt;
370
+
371
+ alt = getenv(ALTERNATE_DB_ENVIRONMENT);
372
+ if (!alt) alt = "";
373
+
374
+ if (alt_odb_tail)
375
+ return;
376
+ alt_odb_tail = &alt_odb_list;
377
+ link_alt_odb_entries(alt, alt + strlen(alt), ':', NULL, 0);
378
+
379
+ read_info_alternates(get_object_directory(), 0);
380
+}
381
+
382
static char *find_sha1_file(const unsigned char *sha1, struct stat *st)
383
{
384
char *name = sha1_file_name(sha1);