Skip to content

Commit b69fb86

Browse files
peffgitster
authored andcommitted
sha1_file_name(): overwrite buffer instead of appending
The sha1_file_name() function is used to generate the path to a loose object in the object directory. It doesn't make much sense for it to append, since the the path we write may be absolute (i.e., you cannot reliably build up a path with it). Because many callers use it with a static buffer, they have to strbuf_reset() manually before each call (and the other callers always use an empty buffer, so they don't care either way). Let's handle this automatically. Since we're changing the semantics, let's take the opportunity to give it a more hash-neutral name (which will also catch any callers from topics in flight). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 263db40 commit b69fb86

File tree

4 files changed

+12
-14
lines changed

4 files changed

+12
-14
lines changed

http-walker.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ static int fetch_object(struct walker *walker, unsigned char *sha1)
547547
ret = error("File %s has bad hash", hex);
548548
} else if (req->rename < 0) {
549549
struct strbuf buf = STRBUF_INIT;
550-
sha1_file_name(the_repository, &buf, req->sha1);
550+
loose_object_path(the_repository, &buf, req->sha1);
551551
ret = error("unable to write sha1 filename %s", buf.buf);
552552
strbuf_release(&buf);
553553
}

http.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2314,7 +2314,7 @@ struct http_object_request *new_http_object_request(const char *base_url,
23142314
hashcpy(freq->sha1, sha1);
23152315
freq->localfile = -1;
23162316

2317-
sha1_file_name(the_repository, &filename, sha1);
2317+
loose_object_path(the_repository, &filename, sha1);
23182318
strbuf_addf(&freq->tmpfile, "%s.temp", filename.buf);
23192319

23202320
strbuf_addf(&prevfile, "%s.prev", filename.buf);
@@ -2465,7 +2465,7 @@ int finish_http_object_request(struct http_object_request *freq)
24652465
unlink_or_warn(freq->tmpfile.buf);
24662466
return -1;
24672467
}
2468-
sha1_file_name(the_repository, &filename, freq->sha1);
2468+
loose_object_path(the_repository, &filename, freq->sha1);
24692469
freq->rename = finalize_object_file(freq->tmpfile.buf, filename.buf);
24702470
strbuf_release(&filename);
24712471

object-store.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ void raw_object_store_clear(struct raw_object_store *o);
157157
* Put in `buf` the name of the file in the local object database that
158158
* would be used to store a loose object with the specified sha1.
159159
*/
160-
void sha1_file_name(struct repository *r, struct strbuf *buf, const unsigned char *sha1);
160+
void loose_object_path(struct repository *r, struct strbuf *buf, const unsigned char *sha1);
161161

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

sha1-file.c

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

349-
void sha1_file_name(struct repository *r, struct strbuf *buf, const unsigned char *sha1)
349+
void loose_object_path(struct repository *r, struct strbuf *buf,
350+
const unsigned char *sha1)
350351
{
352+
strbuf_reset(buf);
351353
strbuf_addstr(buf, r->objects->objectdir);
352354
strbuf_addch(buf, '/');
353355
fill_sha1_path(buf, sha1);
@@ -735,8 +737,7 @@ static int check_and_freshen_local(const struct object_id *oid, int freshen)
735737
{
736738
static struct strbuf buf = STRBUF_INIT;
737739

738-
strbuf_reset(&buf);
739-
sha1_file_name(the_repository, &buf, oid->hash);
740+
loose_object_path(the_repository, &buf, oid->hash);
740741

741742
return check_and_freshen_file(buf.buf, freshen);
742743
}
@@ -888,16 +889,15 @@ int git_open_cloexec(const char *name, int flags)
888889
*
889890
* The "path" out-parameter will give the path of the object we found (if any).
890891
* Note that it may point to static storage and is only valid until another
891-
* call to sha1_file_name(), etc.
892+
* call to loose_object_path(), etc.
892893
*/
893894
static int stat_sha1_file(struct repository *r, const unsigned char *sha1,
894895
struct stat *st, const char **path)
895896
{
896897
struct object_directory *odb;
897898
static struct strbuf buf = STRBUF_INIT;
898899

899-
strbuf_reset(&buf);
900-
sha1_file_name(r, &buf, sha1);
900+
loose_object_path(r, &buf, sha1);
901901
*path = buf.buf;
902902

903903
if (!lstat(*path, st))
@@ -926,8 +926,7 @@ static int open_sha1_file(struct repository *r,
926926
int most_interesting_errno;
927927
static struct strbuf buf = STRBUF_INIT;
928928

929-
strbuf_reset(&buf);
930-
sha1_file_name(r, &buf, sha1);
929+
loose_object_path(r, &buf, sha1);
931930
*path = buf.buf;
932931

933932
fd = git_open(*path);
@@ -1626,8 +1625,7 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
16261625
static struct strbuf tmp_file = STRBUF_INIT;
16271626
static struct strbuf filename = STRBUF_INIT;
16281627

1629-
strbuf_reset(&filename);
1630-
sha1_file_name(the_repository, &filename, oid->hash);
1628+
loose_object_path(the_repository, &filename, oid->hash);
16311629

16321630
fd = create_tmpfile(&tmp_file, filename.buf);
16331631
if (fd < 0) {

0 commit comments

Comments
 (0)