diff options
author | Junio C Hamano <gitster@pobox.com> | 2018-02-13 22:39:10 +0100 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2018-02-13 22:39:10 +0100 |
commit | 9238941618a3d10f77d20e7f2a72533e701eb18f (patch) | |
tree | 36047b304b012cd39a13194d8a3e1fcadf5e2546 /sha1_file.c | |
parent | Merge branch 'nd/trace-with-env' (diff) | |
parent | sha1_file: improve sha1_file_name() perfs (diff) | |
download | git-9238941618a3d10f77d20e7f2a72533e701eb18f.tar.xz git-9238941618a3d10f77d20e7f2a72533e701eb18f.zip |
Merge branch 'cc/sha1-file-name'
Code clean-up.
* cc/sha1-file-name:
sha1_file: improve sha1_file_name() perfs
sha1_file: remove static strbuf from sha1_file_name()
Diffstat (limited to 'sha1_file.c')
-rw-r--r-- | sha1_file.c | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/sha1_file.c b/sha1_file.c index 4d3f282c80..831d9e7343 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -321,15 +321,11 @@ static void fill_sha1_path(struct strbuf *buf, const unsigned char *sha1) } } -const char *sha1_file_name(const unsigned char *sha1) +void sha1_file_name(struct strbuf *buf, const unsigned char *sha1) { - static struct strbuf buf = STRBUF_INIT; - - strbuf_reset(&buf); - strbuf_addf(&buf, "%s/", get_object_directory()); - - fill_sha1_path(&buf, sha1); - return buf.buf; + strbuf_addstr(buf, get_object_directory()); + strbuf_addch(buf, '/'); + fill_sha1_path(buf, sha1); } struct strbuf *alt_scratch_buf(struct alternate_object_database *alt) @@ -710,7 +706,12 @@ int check_and_freshen_file(const char *fn, int freshen) static int check_and_freshen_local(const unsigned char *sha1, int freshen) { - return check_and_freshen_file(sha1_file_name(sha1), freshen); + static struct strbuf buf = STRBUF_INIT; + + strbuf_reset(&buf); + sha1_file_name(&buf, sha1); + + return check_and_freshen_file(buf.buf, freshen); } static int check_and_freshen_nonlocal(const unsigned char *sha1, int freshen) @@ -866,8 +867,12 @@ static int stat_sha1_file(const unsigned char *sha1, struct stat *st, const char **path) { struct alternate_object_database *alt; + static struct strbuf buf = STRBUF_INIT; + + strbuf_reset(&buf); + sha1_file_name(&buf, sha1); + *path = buf.buf; - *path = sha1_file_name(sha1); if (!lstat(*path, st)) return 0; @@ -891,8 +896,12 @@ static int open_sha1_file(const unsigned char *sha1, const char **path) int fd; struct alternate_object_database *alt; int most_interesting_errno; + static struct strbuf buf = STRBUF_INIT; + + strbuf_reset(&buf); + sha1_file_name(&buf, sha1); + *path = buf.buf; - *path = sha1_file_name(sha1); fd = git_open(*path); if (fd >= 0) return fd; @@ -1572,9 +1581,12 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen, git_SHA_CTX c; unsigned char parano_sha1[20]; static struct strbuf tmp_file = STRBUF_INIT; - const char *filename = sha1_file_name(sha1); + static struct strbuf filename = STRBUF_INIT; + + strbuf_reset(&filename); + sha1_file_name(&filename, sha1); - fd = create_tmpfile(&tmp_file, filename); + fd = create_tmpfile(&tmp_file, filename.buf); if (fd < 0) { if (errno == EACCES) return error("insufficient permission for adding an object to repository database %s", get_object_directory()); @@ -1627,7 +1639,7 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen, warning_errno("failed utime() on %s", tmp_file.buf); } - return finalize_object_file(tmp_file.buf, filename); + return finalize_object_file(tmp_file.buf, filename.buf); } static int freshen_loose_object(const unsigned char *sha1) |