Skip to content

Commit f3f043a

Browse files
peffgitster
authored andcommitted
handle alternates paths the same as the main object dir
When we generate loose file paths for the main object directory, the caller provides a buffer to loose_object_path (formerly sha1_file_name). The callers generally keep their own static buffer to avoid excessive reallocations. But for alternate directories, each struct carries its own scratch buffer. This is needlessly different; let's unify them. We could go either direction here, but this patch moves the alternates struct over to the main directory style (rather than vice-versa). Technically the alternates style is more efficient, as it avoids rewriting the object directory name on each call. But this is unlikely to matter in practice, as we avoid reallocations either way (and nobody has ever noticed or complained that the main object directory is copying a few extra bytes before making a much more expensive system call). And this has the advantage that the reusable buffers are tied to particular calls, which makes the invalidation rules simpler (for example, the return value from stat_sha1_file() used to be invalidated by basically any other object call, but now it is affected only by other calls to stat_sha1_file()). We do steal the trick from alt_sha1_path() of returning a pointer to the filled buffer, which makes a few conversions more convenient. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b69fb86 commit f3f043a

File tree

4 files changed

+23
-44
lines changed

4 files changed

+23
-44
lines changed

object-store.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010
struct object_directory {
1111
struct object_directory *next;
1212

13-
/* see alt_scratch_buf() */
14-
struct strbuf scratch;
15-
size_t base_len;
16-
1713
/*
1814
* Used to store the results of readdir(3) calls when searching
1915
* for unique abbreviated hashes. This cache is never
@@ -54,14 +50,6 @@ void add_to_alternates_file(const char *dir);
5450
*/
5551
void add_to_alternates_memory(const char *dir);
5652

57-
/*
58-
* Returns a scratch strbuf pre-filled with the alternate object directory,
59-
* including a trailing slash, which can be used to access paths in the
60-
* alternate. Always use this over direct access to alt->scratch, as it
61-
* cleans up any previous use of the scratch buffer.
62-
*/
63-
struct strbuf *alt_scratch_buf(struct object_directory *odb);
64-
6553
struct packed_git {
6654
struct packed_git *next;
6755
struct list_head mru;
@@ -157,7 +145,7 @@ void raw_object_store_clear(struct raw_object_store *o);
157145
* Put in `buf` the name of the file in the local object database that
158146
* would be used to store a loose object with the specified sha1.
159147
*/
160-
void loose_object_path(struct repository *r, struct strbuf *buf, const unsigned char *sha1);
148+
const char *loose_object_path(struct repository *r, struct strbuf *buf, const unsigned char *sha1);
161149

162150
void *map_sha1_file(struct repository *r, const unsigned char *sha1, unsigned long *size);
163151

object.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,6 @@ struct raw_object_store *raw_object_store_new(void)
484484

485485
static void free_alt_odb(struct object_directory *odb)
486486
{
487-
strbuf_release(&odb->scratch);
488487
oid_array_clear(&odb->loose_objects_cache);
489488
free(odb);
490489
}

sha1-file.c

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -346,27 +346,20 @@ static void fill_sha1_path(struct strbuf *buf, const unsigned char *sha1)
346346
}
347347
}
348348

349-
void loose_object_path(struct repository *r, struct strbuf *buf,
350-
const unsigned char *sha1)
349+
static const char *odb_loose_path(const char *path, struct strbuf *buf,
350+
const unsigned char *sha1)
351351
{
352352
strbuf_reset(buf);
353-
strbuf_addstr(buf, r->objects->objectdir);
353+
strbuf_addstr(buf, path);
354354
strbuf_addch(buf, '/');
355355
fill_sha1_path(buf, sha1);
356+
return buf->buf;
356357
}
357358

358-
struct strbuf *alt_scratch_buf(struct object_directory *odb)
359+
const char *loose_object_path(struct repository *r, struct strbuf *buf,
360+
const unsigned char *sha1)
359361
{
360-
strbuf_setlen(&odb->scratch, odb->base_len);
361-
return &odb->scratch;
362-
}
363-
364-
static const char *alt_sha1_path(struct object_directory *odb,
365-
const unsigned char *sha1)
366-
{
367-
struct strbuf *buf = alt_scratch_buf(odb);
368-
fill_sha1_path(buf, sha1);
369-
return buf->buf;
362+
return odb_loose_path(r->objects->objectdir, buf, sha1);
370363
}
371364

372365
/*
@@ -547,9 +540,6 @@ struct object_directory *alloc_alt_odb(const char *dir)
547540
struct object_directory *ent;
548541

549542
FLEX_ALLOC_STR(ent, path, dir);
550-
strbuf_init(&ent->scratch, 0);
551-
strbuf_addf(&ent->scratch, "%s/", dir);
552-
ent->base_len = ent->scratch.len;
553543

554544
return ent;
555545
}
@@ -745,10 +735,12 @@ static int check_and_freshen_local(const struct object_id *oid, int freshen)
745735
static int check_and_freshen_nonlocal(const struct object_id *oid, int freshen)
746736
{
747737
struct object_directory *odb;
738+
static struct strbuf path = STRBUF_INIT;
739+
748740
prepare_alt_odb(the_repository);
749741
for (odb = the_repository->objects->alt_odb_list; odb; odb = odb->next) {
750-
const char *path = alt_sha1_path(odb, oid->hash);
751-
if (check_and_freshen_file(path, freshen))
742+
odb_loose_path(odb->path, &path, oid->hash);
743+
if (check_and_freshen_file(path.buf, freshen))
752744
return 1;
753745
}
754746
return 0;
@@ -889,24 +881,22 @@ int git_open_cloexec(const char *name, int flags)
889881
*
890882
* The "path" out-parameter will give the path of the object we found (if any).
891883
* Note that it may point to static storage and is only valid until another
892-
* call to loose_object_path(), etc.
884+
* call to stat_sha1_file().
893885
*/
894886
static int stat_sha1_file(struct repository *r, const unsigned char *sha1,
895887
struct stat *st, const char **path)
896888
{
897889
struct object_directory *odb;
898890
static struct strbuf buf = STRBUF_INIT;
899891

900-
loose_object_path(r, &buf, sha1);
901-
*path = buf.buf;
902-
892+
*path = loose_object_path(r, &buf, sha1);
903893
if (!lstat(*path, st))
904894
return 0;
905895

906896
prepare_alt_odb(r);
907897
errno = ENOENT;
908898
for (odb = r->objects->alt_odb_list; odb; odb = odb->next) {
909-
*path = alt_sha1_path(odb, sha1);
899+
*path = odb_loose_path(odb->path, &buf, sha1);
910900
if (!lstat(*path, st))
911901
return 0;
912902
}
@@ -926,17 +916,15 @@ static int open_sha1_file(struct repository *r,
926916
int most_interesting_errno;
927917
static struct strbuf buf = STRBUF_INIT;
928918

929-
loose_object_path(r, &buf, sha1);
930-
*path = buf.buf;
931-
919+
*path = loose_object_path(r, &buf, sha1);
932920
fd = git_open(*path);
933921
if (fd >= 0)
934922
return fd;
935923
most_interesting_errno = errno;
936924

937925
prepare_alt_odb(r);
938926
for (odb = r->objects->alt_odb_list; odb; odb = odb->next) {
939-
*path = alt_sha1_path(odb, sha1);
927+
*path = odb_loose_path(odb->path, &buf, sha1);
940928
fd = git_open(*path);
941929
if (fd >= 0)
942930
return fd;

sha1-name.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ static void find_short_object_filename(struct disambiguate_state *ds)
9797
int subdir_nr = ds->bin_pfx.hash[0];
9898
struct object_directory *odb;
9999
static struct object_directory *fakeent;
100+
struct strbuf buf = STRBUF_INIT;
100101

101102
if (!fakeent) {
102103
/*
@@ -114,8 +115,9 @@ static void find_short_object_filename(struct disambiguate_state *ds)
114115
int pos;
115116

116117
if (!odb->loose_objects_subdir_seen[subdir_nr]) {
117-
struct strbuf *buf = alt_scratch_buf(odb);
118-
for_each_file_in_obj_subdir(subdir_nr, buf,
118+
strbuf_reset(&buf);
119+
strbuf_addstr(&buf, odb->path);
120+
for_each_file_in_obj_subdir(subdir_nr, &buf,
119121
append_loose_object,
120122
NULL, NULL,
121123
&odb->loose_objects_cache);
@@ -134,6 +136,8 @@ static void find_short_object_filename(struct disambiguate_state *ds)
134136
pos++;
135137
}
136138
}
139+
140+
strbuf_release(&buf);
137141
}
138142

139143
static int match_sha(unsigned len, const unsigned char *a, const unsigned char *b)

0 commit comments

Comments
 (0)