Skip to content

Commit 9238941

Browse files
committed
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()
2 parents 3efeec3 + 3449847 commit 9238941

File tree

4 files changed

+43
-27
lines changed

4 files changed

+43
-27
lines changed

cache.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -960,12 +960,10 @@ extern void check_repository_format(void);
960960
#define TYPE_CHANGED 0x0040
961961

962962
/*
963-
* Return the name of the file in the local object database that would
964-
* be used to store a loose object with the specified sha1. The
965-
* return value is a pointer to a statically allocated buffer that is
966-
* overwritten each time the function is called.
963+
* Put in `buf` the name of the file in the local object database that
964+
* would be used to store a loose object with the specified sha1.
967965
*/
968-
extern const char *sha1_file_name(const unsigned char *sha1);
966+
extern void sha1_file_name(struct strbuf *buf, const unsigned char *sha1);
969967

970968
/*
971969
* Return an abbreviated sha1 unique within this repository's object database.

http-walker.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,8 +544,10 @@ static int fetch_object(struct walker *walker, unsigned char *sha1)
544544
} else if (hashcmp(obj_req->sha1, req->real_sha1)) {
545545
ret = error("File %s has bad hash", hex);
546546
} else if (req->rename < 0) {
547-
ret = error("unable to write sha1 filename %s",
548-
sha1_file_name(req->sha1));
547+
struct strbuf buf = STRBUF_INIT;
548+
sha1_file_name(&buf, req->sha1);
549+
ret = error("unable to write sha1 filename %s", buf.buf);
550+
strbuf_release(&buf);
549551
}
550552

551553
release_http_object_request(req);

http.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,7 +2168,7 @@ struct http_object_request *new_http_object_request(const char *base_url,
21682168
unsigned char *sha1)
21692169
{
21702170
char *hex = sha1_to_hex(sha1);
2171-
const char *filename;
2171+
struct strbuf filename = STRBUF_INIT;
21722172
char prevfile[PATH_MAX];
21732173
int prevlocal;
21742174
char prev_buf[PREV_BUF_SIZE];
@@ -2180,14 +2180,15 @@ struct http_object_request *new_http_object_request(const char *base_url,
21802180
hashcpy(freq->sha1, sha1);
21812181
freq->localfile = -1;
21822182

2183-
filename = sha1_file_name(sha1);
2183+
sha1_file_name(&filename, sha1);
21842184
snprintf(freq->tmpfile, sizeof(freq->tmpfile),
2185-
"%s.temp", filename);
2185+
"%s.temp", filename.buf);
21862186

2187-
snprintf(prevfile, sizeof(prevfile), "%s.prev", filename);
2187+
snprintf(prevfile, sizeof(prevfile), "%s.prev", filename.buf);
21882188
unlink_or_warn(prevfile);
21892189
rename(freq->tmpfile, prevfile);
21902190
unlink_or_warn(freq->tmpfile);
2191+
strbuf_release(&filename);
21912192

21922193
if (freq->localfile != -1)
21932194
error("fd leakage in start: %d", freq->localfile);
@@ -2302,6 +2303,7 @@ void process_http_object_request(struct http_object_request *freq)
23022303
int finish_http_object_request(struct http_object_request *freq)
23032304
{
23042305
struct stat st;
2306+
struct strbuf filename = STRBUF_INIT;
23052307

23062308
close(freq->localfile);
23072309
freq->localfile = -1;
@@ -2327,8 +2329,10 @@ int finish_http_object_request(struct http_object_request *freq)
23272329
unlink_or_warn(freq->tmpfile);
23282330
return -1;
23292331
}
2330-
freq->rename =
2331-
finalize_object_file(freq->tmpfile, sha1_file_name(freq->sha1));
2332+
2333+
sha1_file_name(&filename, freq->sha1);
2334+
freq->rename = finalize_object_file(freq->tmpfile, filename.buf);
2335+
strbuf_release(&filename);
23322336

23332337
return freq->rename;
23342338
}

sha1_file.c

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -321,15 +321,11 @@ static void fill_sha1_path(struct strbuf *buf, const unsigned char *sha1)
321321
}
322322
}
323323

324-
const char *sha1_file_name(const unsigned char *sha1)
324+
void sha1_file_name(struct strbuf *buf, const unsigned char *sha1)
325325
{
326-
static struct strbuf buf = STRBUF_INIT;
327-
328-
strbuf_reset(&buf);
329-
strbuf_addf(&buf, "%s/", get_object_directory());
330-
331-
fill_sha1_path(&buf, sha1);
332-
return buf.buf;
326+
strbuf_addstr(buf, get_object_directory());
327+
strbuf_addch(buf, '/');
328+
fill_sha1_path(buf, sha1);
333329
}
334330

335331
struct strbuf *alt_scratch_buf(struct alternate_object_database *alt)
@@ -710,7 +706,12 @@ int check_and_freshen_file(const char *fn, int freshen)
710706

711707
static int check_and_freshen_local(const unsigned char *sha1, int freshen)
712708
{
713-
return check_and_freshen_file(sha1_file_name(sha1), freshen);
709+
static struct strbuf buf = STRBUF_INIT;
710+
711+
strbuf_reset(&buf);
712+
sha1_file_name(&buf, sha1);
713+
714+
return check_and_freshen_file(buf.buf, freshen);
714715
}
715716

716717
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,
866867
const char **path)
867868
{
868869
struct alternate_object_database *alt;
870+
static struct strbuf buf = STRBUF_INIT;
871+
872+
strbuf_reset(&buf);
873+
sha1_file_name(&buf, sha1);
874+
*path = buf.buf;
869875

870-
*path = sha1_file_name(sha1);
871876
if (!lstat(*path, st))
872877
return 0;
873878

@@ -891,8 +896,12 @@ static int open_sha1_file(const unsigned char *sha1, const char **path)
891896
int fd;
892897
struct alternate_object_database *alt;
893898
int most_interesting_errno;
899+
static struct strbuf buf = STRBUF_INIT;
900+
901+
strbuf_reset(&buf);
902+
sha1_file_name(&buf, sha1);
903+
*path = buf.buf;
894904

895-
*path = sha1_file_name(sha1);
896905
fd = git_open(*path);
897906
if (fd >= 0)
898907
return fd;
@@ -1572,9 +1581,12 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
15721581
git_SHA_CTX c;
15731582
unsigned char parano_sha1[20];
15741583
static struct strbuf tmp_file = STRBUF_INIT;
1575-
const char *filename = sha1_file_name(sha1);
1584+
static struct strbuf filename = STRBUF_INIT;
1585+
1586+
strbuf_reset(&filename);
1587+
sha1_file_name(&filename, sha1);
15761588

1577-
fd = create_tmpfile(&tmp_file, filename);
1589+
fd = create_tmpfile(&tmp_file, filename.buf);
15781590
if (fd < 0) {
15791591
if (errno == EACCES)
15801592
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,
16271639
warning_errno("failed utime() on %s", tmp_file.buf);
16281640
}
16291641

1630-
return finalize_object_file(tmp_file.buf, filename);
1642+
return finalize_object_file(tmp_file.buf, filename.buf);
16311643
}
16321644

16331645
static int freshen_loose_object(const unsigned char *sha1)

0 commit comments

Comments
 (0)