alternates: provide helper for adding to alternates list

The submodule code wants to temporarily add an alternate object store to our in-memory alt_odb list, but does it manually. Let's provide a helper so it can reuse the code in link_alt_odb_entry(). While we're adding our new add_to_alternates_memory(), let's document add_to_alternates_file(), as the two are related. 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 a5b34d21521c015cd41ced4a3fdde57d79891eb3
3 files changed +25 -23
cache.h
+13 -1
@@ -1389,10 +1389,22 @@ extern struct alternate_object_database {
1389 extern void prepare_alt_odb(void);
1390 extern void read_info_alternates(const char * relative_base, int depth);
1391 extern char *compute_alternate_path(const char *path, struct strbuf *err);
1392 -extern void add_to_alternates_file(const char *reference);
1392 typedef int alt_odb_fn(struct alternate_object_database *, void *);
1393 extern int foreach_alt_odb(alt_odb_fn, void*);
1394
1395 +/*
1396 + * Add the directory to the on-disk alternates file; the new entry will also
1397 + * take effect in the current process.
1398 + */
1399 +extern void add_to_alternates_file(const char *dir);
1400 +
1401 +/*
1402 + * Add the directory to the in-memory list of alternates (along with any
1403 + * recursive alternates it points to), but do not modify the on-disk alternates
1404 + * file.
1405 + */
1406 +extern void add_to_alternates_memory(const char *dir);
1407 +
1408 struct pack_window {
1409 struct pack_window *next;
1410 unsigned char *base;
sha1_file.c
+11
@@ -440,6 +440,17 @@ void add_to_alternates_file(const char *reference)
440 free(alts);
441 }
442
443 +void add_to_alternates_memory(const char *reference)
444 +{
445 + /*
446 + * Make sure alternates are initialized, or else our entry may be
447 + * overwritten when they are.
448 + */
449 + prepare_alt_odb();
450 +
451 + link_alt_odb_entries(reference, strlen(reference), '\n', NULL, 0);
452 +}
453 +
454 /*
455 * Compute the exact path an alternate is at and returns it. In case of
456 * error NULL is returned and the human readable error is added to `err`
submodule.c
+1 -22
@@ -123,9 +123,7 @@ void stage_updated_gitmodules(void)
123 static int add_submodule_odb(const char *path)
124 {
125 struct strbuf objects_directory = STRBUF_INIT;
126 - struct alternate_object_database *alt_odb;
126 int ret = 0;
128 - size_t alloc;
127
128 ret = strbuf_git_path_submodule(&objects_directory, path, "objects/");
129 if (ret)
@@ -134,26 +132,7 @@ static int add_submodule_odb(const char *path)
132 ret = -1;
133 goto done;
134 }
137 - /* avoid adding it twice */
138 - prepare_alt_odb();
139 - for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
140 - if (alt_odb->name - alt_odb->base == objects_directory.len &&
141 - !strncmp(alt_odb->base, objects_directory.buf,
142 - objects_directory.len))
143 - goto done;
144 -
145 - alloc = st_add(objects_directory.len, 42); /* for "12/345..." sha1 */
146 - alt_odb = xmalloc(st_add(sizeof(*alt_odb), alloc));
147 - alt_odb->next = alt_odb_list;
148 - xsnprintf(alt_odb->base, alloc, "%s", objects_directory.buf);
149 - alt_odb->name = alt_odb->base + objects_directory.len;
150 - alt_odb->name[2] = '/';
151 - alt_odb->name[40] = '\0';
152 - alt_odb->name[41] = '\0';
153 - alt_odb_list = alt_odb;
154 -
155 - /* add possible alternates from the submodule */
156 - read_info_alternates(objects_directory.buf, 0);
135 + add_to_alternates_memory(objects_directory.buf);
136 done:
137 strbuf_release(&objects_directory);
138 return ret;