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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Nov 12, 2018 at 09:48 UTC b69fb867b4bb9e30e705d2176fe8a0a90b208325
4 files changed +12 -14
http-walker.c
+1 -1
@@ -547,7 +547,7 @@ static int fetch_object(struct walker *walker, unsigned char *sha1)
547 ret = error("File %s has bad hash", hex);
548 } else if (req->rename < 0) {
549 struct strbuf buf = STRBUF_INIT;
550 - sha1_file_name(the_repository, &buf, req->sha1);
550 + loose_object_path(the_repository, &buf, req->sha1);
551 ret = error("unable to write sha1 filename %s", buf.buf);
552 strbuf_release(&buf);
553 }
http.c
+2 -2
@@ -2314,7 +2314,7 @@ struct http_object_request *new_http_object_request(const char *base_url,
2314 hashcpy(freq->sha1, sha1);
2315 freq->localfile = -1;
2316
2317 - sha1_file_name(the_repository, &filename, sha1);
2317 + loose_object_path(the_repository, &filename, sha1);
2318 strbuf_addf(&freq->tmpfile, "%s.temp", filename.buf);
2319
2320 strbuf_addf(&prevfile, "%s.prev", filename.buf);
@@ -2465,7 +2465,7 @@ int finish_http_object_request(struct http_object_request *freq)
2465 unlink_or_warn(freq->tmpfile.buf);
2466 return -1;
2467 }
2468 - sha1_file_name(the_repository, &filename, freq->sha1);
2468 + loose_object_path(the_repository, &filename, freq->sha1);
2469 freq->rename = finalize_object_file(freq->tmpfile.buf, filename.buf);
2470 strbuf_release(&filename);
2471
object-store.h
+1 -1
@@ -157,7 +157,7 @@ void raw_object_store_clear(struct raw_object_store *o);
157 * Put in `buf` the name of the file in the local object database that
158 * would be used to store a loose object with the specified sha1.
159 */
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);
161
162 void *map_sha1_file(struct repository *r, const unsigned char *sha1, unsigned long *size);
163
sha1-file.c
+8 -10
@@ -346,8 +346,10 @@ static void fill_sha1_path(struct strbuf *buf, const unsigned char *sha1)
346 }
347 }
348
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)
351 {
352 + strbuf_reset(buf);
353 strbuf_addstr(buf, r->objects->objectdir);
354 strbuf_addch(buf, '/');
355 fill_sha1_path(buf, sha1);
@@ -735,8 +737,7 @@ static int check_and_freshen_local(const struct object_id *oid, int freshen)
737 {
738 static struct strbuf buf = STRBUF_INIT;
739
738 - strbuf_reset(&buf);
739 - sha1_file_name(the_repository, &buf, oid->hash);
740 + loose_object_path(the_repository, &buf, oid->hash);
741
742 return check_and_freshen_file(buf.buf, freshen);
743 }
@@ -888,7 +889,7 @@ int git_open_cloexec(const char *name, int flags)
889 *
890 * The "path" out-parameter will give the path of the object we found (if any).
891 * 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.
893 */
894 static int stat_sha1_file(struct repository *r, const unsigned char *sha1,
895 struct stat *st, const char **path)
@@ -896,8 +897,7 @@ static int stat_sha1_file(struct repository *r, const unsigned char *sha1,
897 struct object_directory *odb;
898 static struct strbuf buf = STRBUF_INIT;
899
899 - strbuf_reset(&buf);
900 - sha1_file_name(r, &buf, sha1);
900 + loose_object_path(r, &buf, sha1);
901 *path = buf.buf;
902
903 if (!lstat(*path, st))
@@ -926,8 +926,7 @@ static int open_sha1_file(struct repository *r,
926 int most_interesting_errno;
927 static struct strbuf buf = STRBUF_INIT;
928
929 - strbuf_reset(&buf);
930 - sha1_file_name(r, &buf, sha1);
929 + loose_object_path(r, &buf, sha1);
930 *path = buf.buf;
931
932 fd = git_open(*path);
@@ -1626,8 +1625,7 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
1625 static struct strbuf tmp_file = STRBUF_INIT;
1626 static struct strbuf filename = STRBUF_INIT;
1627
1629 - strbuf_reset(&filename);
1630 - sha1_file_name(the_repository, &filename, oid->hash);
1628 + loose_object_path(the_repository, &filename, oid->hash);
1629
1630 fd = create_tmpfile(&tmp_file, filename.buf);
1631 if (fd < 0) {