summaryrefslogtreecommitdiff
path: root/sha1_file.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2016-10-03 20:35:31 (GMT)
committerJunio C Hamano <gitster@pobox.com>2016-10-10 20:52:36 (GMT)
commit7f0fa2c02a6543bdadae3c4a492daae7dbc8c042 (patch)
tree5fc856fb9a95e4e5edc2c5d2c980d14f984ce8a8 /sha1_file.c
parenta5b34d21521c015cd41ced4a3fdde57d79891eb3 (diff)
downloadgit-7f0fa2c02a6543bdadae3c4a492daae7dbc8c042.zip
git-7f0fa2c02a6543bdadae3c4a492daae7dbc8c042.tar.gz
git-7f0fa2c02a6543bdadae3c4a492daae7dbc8c042.tar.bz2
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>
Diffstat (limited to 'sha1_file.c')
-rw-r--r--sha1_file.c28
1 files changed, 19 insertions, 9 deletions
diff --git a/sha1_file.c b/sha1_file.c
index 7a3b745..636d07a 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -283,7 +283,6 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base,
int depth, const char *normalized_objdir)
{
struct alternate_object_database *ent;
- size_t entlen;
struct strbuf pathbuf = STRBUF_INIT;
if (!is_absolute_path(entry) && relative_base) {
@@ -311,14 +310,7 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base,
return -1;
}
- entlen = st_add(pathbuf.len, 43); /* '/' + 2 hex + '/' + 38 hex + NUL */
- ent = xmalloc(st_add(sizeof(*ent), entlen));
- memcpy(ent->base, pathbuf.buf, pathbuf.len);
-
- ent->name = ent->base + pathbuf.len + 1;
- ent->base[pathbuf.len] = '/';
- ent->base[pathbuf.len + 3] = '/';
- ent->base[entlen-1] = 0;
+ ent = alloc_alt_odb(pathbuf.buf);
/* add the alternate entry */
*alt_odb_tail = ent;
@@ -395,6 +387,24 @@ void read_info_alternates(const char * relative_base, int depth)
munmap(map, mapsz);
}
+struct alternate_object_database *alloc_alt_odb(const char *dir)
+{
+ struct alternate_object_database *ent;
+ size_t dirlen = strlen(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);
+
+ ent->name = ent->base + dirlen + 1;
+ ent->base[dirlen] = '/';
+ ent->base[dirlen + 3] = '/';
+ ent->base[entlen-1] = 0;
+
+ return ent;
+}
+
void add_to_alternates_file(const char *reference)
{
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));