Skip to content

Commit 30d6c6e

Browse files
mhaggergitster
authored andcommitted
sha1_file_name(): declare to return a const string
Change the return value of sha1_file_name() to (const char *). (Callers have no business mucking about here.) Change callers accordingly, deleting a few superfluous temporary variables along the way. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1b1005d commit 30d6c6e

File tree

3 files changed

+11
-17
lines changed

3 files changed

+11
-17
lines changed

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ extern char *git_path(const char *fmt, ...) __attribute__((format (printf, 1, 2)
659659
extern char *git_path_submodule(const char *path, const char *fmt, ...)
660660
__attribute__((format (printf, 2, 3)));
661661

662-
extern char *sha1_file_name(const unsigned char *sha1);
662+
extern const char *sha1_file_name(const unsigned char *sha1);
663663
extern char *sha1_pack_name(const unsigned char *sha1);
664664
extern char *sha1_pack_index_name(const unsigned char *sha1);
665665
extern const char *find_unique_abbrev(const unsigned char *sha1, int);

http.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1384,7 +1384,7 @@ struct http_object_request *new_http_object_request(const char *base_url,
13841384
unsigned char *sha1)
13851385
{
13861386
char *hex = sha1_to_hex(sha1);
1387-
char *filename;
1387+
const char *filename;
13881388
char prevfile[PATH_MAX];
13891389
int prevlocal;
13901390
char prev_buf[PREV_BUF_SIZE];

sha1_file.c

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
194194
* DB_ENVIRONMENT environment variable if it is not found in
195195
* the primary object database.
196196
*/
197-
char *sha1_file_name(const unsigned char *sha1)
197+
const char *sha1_file_name(const unsigned char *sha1)
198198
{
199199
static char buf[PATH_MAX];
200200
const char *objdir;
@@ -444,8 +444,7 @@ void prepare_alt_odb(void)
444444

445445
static int has_loose_object_local(const unsigned char *sha1)
446446
{
447-
char *name = sha1_file_name(sha1);
448-
return !access(name, F_OK);
447+
return !access(sha1_file_name(sha1), F_OK);
449448
}
450449

451450
int has_loose_object_nonlocal(const unsigned char *sha1)
@@ -1420,17 +1419,15 @@ static int git_open_noatime(const char *name)
14201419

14211420
static int stat_sha1_file(const unsigned char *sha1, struct stat *st)
14221421
{
1423-
char *name = sha1_file_name(sha1);
14241422
struct alternate_object_database *alt;
14251423

1426-
if (!lstat(name, st))
1424+
if (!lstat(sha1_file_name(sha1), st))
14271425
return 0;
14281426

14291427
prepare_alt_odb();
14301428
errno = ENOENT;
14311429
for (alt = alt_odb_list; alt; alt = alt->next) {
1432-
name = alt->name;
1433-
fill_sha1_path(name, sha1);
1430+
fill_sha1_path(alt->name, sha1);
14341431
if (!lstat(alt->base, st))
14351432
return 0;
14361433
}
@@ -1441,18 +1438,16 @@ static int stat_sha1_file(const unsigned char *sha1, struct stat *st)
14411438
static int open_sha1_file(const unsigned char *sha1)
14421439
{
14431440
int fd;
1444-
char *name = sha1_file_name(sha1);
14451441
struct alternate_object_database *alt;
14461442

1447-
fd = git_open_noatime(name);
1443+
fd = git_open_noatime(sha1_file_name(sha1));
14481444
if (fd >= 0)
14491445
return fd;
14501446

14511447
prepare_alt_odb();
14521448
errno = ENOENT;
14531449
for (alt = alt_odb_list; alt; alt = alt->next) {
1454-
name = alt->name;
1455-
fill_sha1_path(name, sha1);
1450+
fill_sha1_path(alt->name, sha1);
14561451
fd = git_open_noatime(alt->base);
14571452
if (fd >= 0)
14581453
return fd;
@@ -2687,7 +2682,6 @@ void *read_sha1_file_extended(const unsigned char *sha1,
26872682
unsigned flag)
26882683
{
26892684
void *data;
2690-
char *path;
26912685
const struct packed_git *p;
26922686
const unsigned char *repl = lookup_replace_object_extended(sha1, flag);
26932687

@@ -2705,7 +2699,8 @@ void *read_sha1_file_extended(const unsigned char *sha1,
27052699
sha1_to_hex(repl), sha1_to_hex(sha1));
27062700

27072701
if (has_loose_object(repl)) {
2708-
path = sha1_file_name(sha1);
2702+
const char *path = sha1_file_name(sha1);
2703+
27092704
die("loose object %s (stored in %s) is corrupt",
27102705
sha1_to_hex(repl), path);
27112706
}
@@ -2903,10 +2898,9 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
29032898
git_zstream stream;
29042899
git_SHA_CTX c;
29052900
unsigned char parano_sha1[20];
2906-
char *filename;
29072901
static char tmp_file[PATH_MAX];
2902+
const char *filename = sha1_file_name(sha1);
29082903

2909-
filename = sha1_file_name(sha1);
29102904
fd = create_tmpfile(tmp_file, sizeof(tmp_file), filename);
29112905
if (fd < 0) {
29122906
if (errno == EACCES)

0 commit comments

Comments
 (0)