summaryrefslogtreecommitdiff
path: root/sha1_file.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2016-10-03 20:35:51 (GMT)
committerJunio C Hamano <gitster@pobox.com>2016-10-10 20:52:36 (GMT)
commit597f9134ded20f882e2bf6bca8b0e1f03981b98d (patch)
treead7cd7eff31ed1dd109230e4ef99de551f0e908c /sha1_file.c
parent29ec6af2b81894d3236f2aec100323138023ef4d (diff)
downloadgit-597f9134ded20f882e2bf6bca8b0e1f03981b98d.zip
git-597f9134ded20f882e2bf6bca8b0e1f03981b98d.tar.gz
git-597f9134ded20f882e2bf6bca8b0e1f03981b98d.tar.bz2
alternates: use a separate scratch space
The alternate_object_database struct uses a single buffer both for storing the path to the alternate, and as a scratch buffer for forming object names. This is efficient (since otherwise we'd end up storing the path twice), but it makes life hard for callers who just want to know the path to the alternate. They have to remember to stop reading after "alt->name - alt->base" bytes, and to subtract one for the trailing '/'. It would be much simpler if they could simply access a NUL-terminated path string. We could encapsulate this in a function which puts a NUL in the scratch buffer and returns the string, but that opens up questions about the lifetime of the result. The first time another caller uses the alternate, the scratch buffer may get other data tacked onto it. Let's instead just store the root path separately from the scratch buffer. There aren't enough alternates being stored for the duplicated data to matter for performance, and this keeps things simple and safe for the callers. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'sha1_file.c')
-rw-r--r--sha1_file.c28
1 files changed, 12 insertions, 16 deletions
diff --git a/sha1_file.c b/sha1_file.c
index b284cea..011532a 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -208,7 +208,7 @@ static const char *alt_sha1_path(struct alternate_object_database *alt,
const unsigned char *sha1)
{
fill_sha1_path(alt->name, sha1);
- return alt->base;
+ return alt->scratch;
}
/*
@@ -261,8 +261,7 @@ static int alt_odb_usable(struct strbuf *path, const char *normalized_objdir)
* thing twice, or object directory itself.
*/
for (alt = alt_odb_list; alt; alt = alt->next) {
- if (path->len == alt->name - alt->base - 1 &&
- !memcmp(path->buf, alt->base, path->len))
+ if (!strcmp(path->buf, alt->path))
return 0;
}
if (!fspathcmp(path->buf, normalized_objdir))
@@ -401,13 +400,14 @@ struct alternate_object_database *alloc_alt_odb(const char *dir)
size_t entlen;
entlen = st_add(dirlen, 43); /* '/' + 2 hex + '/' + 38 hex + NUL */
- ent = xmalloc(st_add(sizeof(*ent), entlen));
- memcpy(ent->base, dir, dirlen);
+ FLEX_ALLOC_STR(ent, path, dir);
+ ent->scratch = xmalloc(entlen);
+ xsnprintf(ent->scratch, entlen, "%s/", dir);
- ent->name = ent->base + dirlen + 1;
- ent->base[dirlen] = '/';
- ent->base[dirlen + 3] = '/';
- ent->base[entlen-1] = 0;
+ ent->name = ent->scratch + dirlen + 1;
+ ent->scratch[dirlen] = '/';
+ ent->scratch[dirlen + 3] = '/';
+ ent->scratch[entlen-1] = 0;
return ent;
}
@@ -1485,11 +1485,8 @@ void prepare_packed_git(void)
return;
prepare_packed_git_one(get_object_directory(), 1);
prepare_alt_odb();
- for (alt = alt_odb_list; alt; alt = alt->next) {
- alt->name[-1] = 0;
- prepare_packed_git_one(alt->base, 0);
- alt->name[-1] = '/';
- }
+ for (alt = alt_odb_list; alt; alt = alt->next)
+ prepare_packed_git_one(alt->path, 0);
rearrange_packed_git();
prepare_packed_git_mru();
prepare_packed_git_run_once = 1;
@@ -3692,8 +3689,7 @@ static int loose_from_alt_odb(struct alternate_object_database *alt,
struct strbuf buf = STRBUF_INIT;
int r;
- /* copy base not including trailing '/' */
- strbuf_add(&buf, alt->base, alt->name - alt->base - 1);
+ strbuf_addstr(&buf, alt->path);
r = for_each_loose_file_in_objdir_buf(&buf,
data->cb, NULL, NULL,
data->data);