Skip to content

Commit a5b34d2

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4ea8247 commit a5b34d2

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

cache.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1389,10 +1389,22 @@ extern struct alternate_object_database {
13891389
extern void prepare_alt_odb(void);
13901390
extern void read_info_alternates(const char * relative_base, int depth);
13911391
extern char *compute_alternate_path(const char *path, struct strbuf *err);
1392-
extern void add_to_alternates_file(const char *reference);
13931392
typedef int alt_odb_fn(struct alternate_object_database *, void *);
13941393
extern int foreach_alt_odb(alt_odb_fn, void*);
13951394

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+
13961408
struct pack_window {
13971409
struct pack_window *next;
13981410
unsigned char *base;

sha1_file.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,17 @@ void add_to_alternates_file(const char *reference)
440440
free(alts);
441441
}
442442

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+
443454
/*
444455
* Compute the exact path an alternate is at and returns it. In case of
445456
* error NULL is returned and the human readable error is added to `err`

submodule.c

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ void stage_updated_gitmodules(void)
123123
static int add_submodule_odb(const char *path)
124124
{
125125
struct strbuf objects_directory = STRBUF_INIT;
126-
struct alternate_object_database *alt_odb;
127126
int ret = 0;
128-
size_t alloc;
129127

130128
ret = strbuf_git_path_submodule(&objects_directory, path, "objects/");
131129
if (ret)
@@ -134,26 +132,7 @@ static int add_submodule_odb(const char *path)
134132
ret = -1;
135133
goto done;
136134
}
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);
157136
done:
158137
strbuf_release(&objects_directory);
159138
return ret;

0 commit comments

Comments
 (0)