alternates: provide helper for allocating alternate
Allocating a struct alternate_object_database is tricky, as we must over-allocate the buffer to provide scratch space, and then put in particular '/' and NUL markers. Let's encapsulate this in a function so that the complexity doesn't leak into callers (and so that we can modify it later). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Oct 3, 2016 at 16:35 UTC
7f0fa2c02a6543bdadae3c4a492daae7dbc8c042
3 files changed
+26
-15
cache.h
+6
@@ -1392,6 +1392,12 @@ extern char *compute_alternate_path(const char *path, struct strbuf *err);
1392
typedef int alt_odb_fn(struct alternate_object_database *, void *);
1393
extern int foreach_alt_odb(alt_odb_fn, void*);
1394
1395
+/*
1396
+ * Allocate a "struct alternate_object_database" but do _not_ actually
1397
+ * add it to the list of alternates.
1398
+ */
1399
+struct alternate_object_database *alloc_alt_odb(const char *dir);
1400
+
1401
/*
1402
* Add the directory to the on-disk alternates file; the new entry will also
1403
* take effect in the current process.
sha1_file.c
+19
-9
@@ -283,7 +283,6 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base,
283
int depth, const char *normalized_objdir)
284
{
285
struct alternate_object_database *ent;
286
- size_t entlen;
286
struct strbuf pathbuf = STRBUF_INIT;
287
288
if (!is_absolute_path(entry) && relative_base) {
@@ -311,14 +310,7 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base,
310
return -1;
311
}
312
314
- entlen = st_add(pathbuf.len, 43); /* '/' + 2 hex + '/' + 38 hex + NUL */
315
- ent = xmalloc(st_add(sizeof(*ent), entlen));
316
- memcpy(ent->base, pathbuf.buf, pathbuf.len);
317
-
318
- ent->name = ent->base + pathbuf.len + 1;
319
- ent->base[pathbuf.len] = '/';
320
- ent->base[pathbuf.len + 3] = '/';
321
- ent->base[entlen-1] = 0;
313
+ ent = alloc_alt_odb(pathbuf.buf);
314
315
/* add the alternate entry */
316
*alt_odb_tail = ent;
@@ -395,6 +387,24 @@ void read_info_alternates(const char * relative_base, int depth)
387
munmap(map, mapsz);
388
}
389
390
+struct alternate_object_database *alloc_alt_odb(const char *dir)
391
+{
392
+ struct alternate_object_database *ent;
393
+ size_t dirlen = strlen(dir);
394
+ size_t entlen;
395
+
396
+ entlen = st_add(dirlen, 43); /* '/' + 2 hex + '/' + 38 hex + NUL */
397
+ ent = xmalloc(st_add(sizeof(*ent), entlen));
398
+ memcpy(ent->base, dir, dirlen);
399
+
400
+ ent->name = ent->base + dirlen + 1;
401
+ ent->base[dirlen] = '/';
402
+ ent->base[dirlen + 3] = '/';
403
+ ent->base[entlen-1] = 0;
404
+
405
+ return ent;
406
+}
407
+
408
void add_to_alternates_file(const char *reference)
409
{
410
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
sha1_name.c
+1
-6
@@ -86,12 +86,7 @@ static void find_short_object_filename(int len, const char *hex_pfx, struct disa
86
* alt->name/alt->base while iterating over the
87
* object databases including our own.
88
*/
89
- const char *objdir = get_object_directory();
90
- size_t objdir_len = strlen(objdir);
91
- fakeent = xmalloc(st_add3(sizeof(*fakeent), objdir_len, 43));
92
- memcpy(fakeent->base, objdir, objdir_len);
93
- fakeent->name = fakeent->base + objdir_len + 1;
94
- fakeent->name[-1] = '/';
89
+ fakeent = alloc_alt_odb(get_object_directory());
90
}
91
fakeent->next = alt_odb_list;
92