Skip to content

Commit 060be00

Browse files
committed
Merge branch 'mh/object-code-cleanup'
* mh/object-code-cleanup: sha1_file.c: document a bunch of functions defined in the file sha1_file_name(): declare to return a const string find_pack_entry(): document last_found_pack replace_object: use struct members instead of an array
2 parents 85ff22e + d40d535 commit 060be00

File tree

4 files changed

+112
-41
lines changed

4 files changed

+112
-41
lines changed

cache.h

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,28 @@ extern char *git_path(const char *fmt, ...) __attribute__((format (printf, 1, 2)
660660
extern char *git_path_submodule(const char *path, const char *fmt, ...)
661661
__attribute__((format (printf, 2, 3)));
662662

663-
extern char *sha1_file_name(const unsigned char *sha1);
663+
/*
664+
* Return the name of the file in the local object database that would
665+
* be used to store a loose object with the specified sha1. The
666+
* return value is a pointer to a statically allocated buffer that is
667+
* overwritten each time the function is called.
668+
*/
669+
extern const char *sha1_file_name(const unsigned char *sha1);
670+
671+
/*
672+
* Return the name of the (local) packfile with the specified sha1 in
673+
* its name. The return value is a pointer to memory that is
674+
* overwritten each time this function is called.
675+
*/
664676
extern char *sha1_pack_name(const unsigned char *sha1);
677+
678+
/*
679+
* Return the name of the (local) pack index file with the specified
680+
* sha1 in its name. The return value is a pointer to memory that is
681+
* overwritten each time this function is called.
682+
*/
665683
extern char *sha1_pack_index_name(const unsigned char *sha1);
684+
666685
extern const char *find_unique_abbrev(const unsigned char *sha1, int);
667686
extern const unsigned char null_sha1[20];
668687

@@ -822,7 +841,19 @@ extern int check_sha1_signature(const unsigned char *sha1, void *buf, unsigned l
822841
extern int move_temp_to_file(const char *tmpfile, const char *filename);
823842

824843
extern int has_sha1_pack(const unsigned char *sha1);
844+
845+
/*
846+
* Return true iff we have an object named sha1, whether local or in
847+
* an alternate object database, and whether packed or loose. This
848+
* function does not respect replace references.
849+
*/
825850
extern int has_sha1_file(const unsigned char *sha1);
851+
852+
/*
853+
* Return true iff an alternate object database has a loose object
854+
* with the specified name. This function does not respect replace
855+
* references.
856+
*/
826857
extern int has_loose_object_nonlocal(const unsigned char *sha1);
827858

828859
extern int has_pack_index(const unsigned char *sha1);
@@ -1086,17 +1117,46 @@ extern struct packed_git *find_sha1_pack(const unsigned char *sha1,
10861117
struct packed_git *packs);
10871118

10881119
extern void pack_report(void);
1120+
1121+
/*
1122+
* mmap the index file for the specified packfile (if it is not
1123+
* already mmapped). Return 0 on success.
1124+
*/
10891125
extern int open_pack_index(struct packed_git *);
1126+
1127+
/*
1128+
* munmap the index file for the specified packfile (if it is
1129+
* currently mmapped).
1130+
*/
10901131
extern void close_pack_index(struct packed_git *);
1132+
10911133
extern unsigned char *use_pack(struct packed_git *, struct pack_window **, off_t, unsigned long *);
10921134
extern void close_pack_windows(struct packed_git *);
10931135
extern void unuse_pack(struct pack_window **);
10941136
extern void free_pack_by_name(const char *);
10951137
extern void clear_delta_base_cache(void);
10961138
extern struct packed_git *add_packed_git(const char *, int, int);
1097-
extern const unsigned char *nth_packed_object_sha1(struct packed_git *, uint32_t);
1098-
extern off_t nth_packed_object_offset(const struct packed_git *, uint32_t);
1099-
extern off_t find_pack_entry_one(const unsigned char *, struct packed_git *);
1139+
1140+
/*
1141+
* Return the SHA-1 of the nth object within the specified packfile.
1142+
* Open the index if it is not already open. The return value points
1143+
* at the SHA-1 within the mmapped index. Return NULL if there is an
1144+
* error.
1145+
*/
1146+
extern const unsigned char *nth_packed_object_sha1(struct packed_git *, uint32_t n);
1147+
1148+
/*
1149+
* Return the offset of the nth object within the specified packfile.
1150+
* The index must already be opened.
1151+
*/
1152+
extern off_t nth_packed_object_offset(const struct packed_git *, uint32_t n);
1153+
1154+
/*
1155+
* If the object named sha1 is present in the specified packfile,
1156+
* return its offset within the packfile; otherwise, return 0.
1157+
*/
1158+
extern off_t find_pack_entry_one(const unsigned char *sha1, struct packed_git *);
1159+
11001160
extern int is_pack_valid(struct packed_git *);
11011161
extern void *unpack_entry(struct packed_git *, off_t, enum object_type *, unsigned long *);
11021162
extern unsigned long unpack_object_header_buffer(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);

http.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,7 @@ struct http_object_request *new_http_object_request(const char *base_url,
13901390
unsigned char *sha1)
13911391
{
13921392
char *hex = sha1_to_hex(sha1);
1393-
char *filename;
1393+
const char *filename;
13941394
char prevfile[PATH_MAX];
13951395
int prevlocal;
13961396
char prev_buf[PREV_BUF_SIZE];

replace_object.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@
33
#include "refs.h"
44
#include "commit.h"
55

6+
/*
7+
* An array of replacements. The array is kept sorted by the original
8+
* sha1.
9+
*/
610
static struct replace_object {
7-
unsigned char sha1[2][20];
11+
unsigned char original[20];
12+
unsigned char replacement[20];
813
} **replace_object;
914

1015
static int replace_object_alloc, replace_object_nr;
1116

1217
static const unsigned char *replace_sha1_access(size_t index, void *table)
1318
{
1419
struct replace_object **replace = table;
15-
return replace[index]->sha1[0];
20+
return replace[index]->original;
1621
}
1722

1823
static int replace_object_pos(const unsigned char *sha1)
@@ -24,7 +29,7 @@ static int replace_object_pos(const unsigned char *sha1)
2429
static int register_replace_object(struct replace_object *replace,
2530
int ignore_dups)
2631
{
27-
int pos = replace_object_pos(replace->sha1[0]);
32+
int pos = replace_object_pos(replace->original);
2833

2934
if (0 <= pos) {
3035
if (ignore_dups)
@@ -60,14 +65,14 @@ static int register_replace_ref(const char *refname,
6065
const char *hash = slash ? slash + 1 : refname;
6166
struct replace_object *repl_obj = xmalloc(sizeof(*repl_obj));
6267

63-
if (strlen(hash) != 40 || get_sha1_hex(hash, repl_obj->sha1[0])) {
68+
if (strlen(hash) != 40 || get_sha1_hex(hash, repl_obj->original)) {
6469
free(repl_obj);
6570
warning("bad replace ref name: %s", refname);
6671
return 0;
6772
}
6873

6974
/* Copy sha1 from the read ref */
70-
hashcpy(repl_obj->sha1[1], sha1);
75+
hashcpy(repl_obj->replacement, sha1);
7176

7277
/* Register new object */
7378
if (register_replace_object(repl_obj, 1))
@@ -107,7 +112,7 @@ const unsigned char *do_lookup_replace_object(const unsigned char *sha1)
107112

108113
pos = replace_object_pos(cur);
109114
if (0 <= pos)
110-
cur = replace_object[pos]->sha1[1];
115+
cur = replace_object[pos]->replacement;
111116
} while (0 <= pos);
112117

113118
return cur;

sha1_file.c

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ static struct cached_object empty_tree = {
6060
0
6161
};
6262

63+
/*
64+
* A pointer to the last packed_git in which an object was found.
65+
* When an object is sought, we look in this packfile first, because
66+
* objects that are looked up at similar times are often in the same
67+
* packfile as one another.
68+
*/
6369
static struct packed_git *last_found_pack;
6470

6571
static struct cached_object *find_cached_object(const unsigned char *sha1)
@@ -178,17 +184,7 @@ static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
178184
}
179185
}
180186

181-
/*
182-
* NOTE! This returns a statically allocated buffer, so you have to be
183-
* careful about using it. Do an "xstrdup()" if you need to save the
184-
* filename.
185-
*
186-
* Also note that this returns the location for creating. Reading
187-
* SHA1 file can happen from any alternate directory listed in the
188-
* DB_ENVIRONMENT environment variable if it is not found in
189-
* the primary object database.
190-
*/
191-
char *sha1_file_name(const unsigned char *sha1)
187+
const char *sha1_file_name(const unsigned char *sha1)
192188
{
193189
static char buf[PATH_MAX];
194190
const char *objdir;
@@ -208,6 +204,11 @@ char *sha1_file_name(const unsigned char *sha1)
208204
return buf;
209205
}
210206

207+
/*
208+
* Return the name of the pack or index file with the specified sha1
209+
* in its filename. *base and *name are scratch space that must be
210+
* provided by the caller. which should be "pack" or "idx".
211+
*/
211212
static char *sha1_get_pack_name(const unsigned char *sha1,
212213
char **name, char **base, const char *which)
213214
{
@@ -436,8 +437,7 @@ void prepare_alt_odb(void)
436437

437438
static int has_loose_object_local(const unsigned char *sha1)
438439
{
439-
char *name = sha1_file_name(sha1);
440-
return !access(name, F_OK);
440+
return !access(sha1_file_name(sha1), F_OK);
441441
}
442442

443443
int has_loose_object_nonlocal(const unsigned char *sha1)
@@ -489,7 +489,12 @@ void pack_report(void)
489489
sz_fmt(pack_mapped), sz_fmt(peak_pack_mapped));
490490
}
491491

492-
static int check_packed_git_idx(const char *path, struct packed_git *p)
492+
/*
493+
* Open and mmap the index file at path, perform a couple of
494+
* consistency checks, then record its information to p. Return 0 on
495+
* success.
496+
*/
497+
static int check_packed_git_idx(const char *path, struct packed_git *p)
493498
{
494499
void *idx_map;
495500
struct pack_idx_header *hdr;
@@ -1412,17 +1417,15 @@ int git_open_noatime(const char *name)
14121417

14131418
static int stat_sha1_file(const unsigned char *sha1, struct stat *st)
14141419
{
1415-
char *name = sha1_file_name(sha1);
14161420
struct alternate_object_database *alt;
14171421

1418-
if (!lstat(name, st))
1422+
if (!lstat(sha1_file_name(sha1), st))
14191423
return 0;
14201424

14211425
prepare_alt_odb();
14221426
errno = ENOENT;
14231427
for (alt = alt_odb_list; alt; alt = alt->next) {
1424-
name = alt->name;
1425-
fill_sha1_path(name, sha1);
1428+
fill_sha1_path(alt->name, sha1);
14261429
if (!lstat(alt->base, st))
14271430
return 0;
14281431
}
@@ -1433,18 +1436,16 @@ static int stat_sha1_file(const unsigned char *sha1, struct stat *st)
14331436
static int open_sha1_file(const unsigned char *sha1)
14341437
{
14351438
int fd;
1436-
char *name = sha1_file_name(sha1);
14371439
struct alternate_object_database *alt;
14381440

1439-
fd = git_open_noatime(name);
1441+
fd = git_open_noatime(sha1_file_name(sha1));
14401442
if (fd >= 0)
14411443
return fd;
14421444

14431445
prepare_alt_odb();
14441446
errno = ENOENT;
14451447
for (alt = alt_odb_list; alt; alt = alt->next) {
1446-
name = alt->name;
1447-
fill_sha1_path(name, sha1);
1448+
fill_sha1_path(alt->name, sha1);
14481449
fd = git_open_noatime(alt->base);
14491450
if (fd >= 0)
14501451
return fd;
@@ -2446,6 +2447,10 @@ static int fill_pack_entry(const unsigned char *sha1,
24462447
return 1;
24472448
}
24482449

2450+
/*
2451+
* Iff a pack file contains the object named by sha1, return true and
2452+
* store its location to e.
2453+
*/
24492454
static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
24502455
{
24512456
struct packed_git *p;
@@ -2458,11 +2463,13 @@ static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
24582463
return 1;
24592464

24602465
for (p = packed_git; p; p = p->next) {
2461-
if (p == last_found_pack || !fill_pack_entry(sha1, e, p))
2462-
continue;
2466+
if (p == last_found_pack)
2467+
continue; /* we already checked this one */
24632468

2464-
last_found_pack = p;
2465-
return 1;
2469+
if (fill_pack_entry(sha1, e, p)) {
2470+
last_found_pack = p;
2471+
return 1;
2472+
}
24662473
}
24672474
return 0;
24682475
}
@@ -2677,7 +2684,6 @@ void *read_sha1_file_extended(const unsigned char *sha1,
26772684
unsigned flag)
26782685
{
26792686
void *data;
2680-
char *path;
26812687
const struct packed_git *p;
26822688
const unsigned char *repl = lookup_replace_object_extended(sha1, flag);
26832689

@@ -2695,7 +2701,8 @@ void *read_sha1_file_extended(const unsigned char *sha1,
26952701
sha1_to_hex(repl), sha1_to_hex(sha1));
26962702

26972703
if (has_loose_object(repl)) {
2698-
path = sha1_file_name(sha1);
2704+
const char *path = sha1_file_name(sha1);
2705+
26992706
die("loose object %s (stored in %s) is corrupt",
27002707
sha1_to_hex(repl), path);
27012708
}
@@ -2893,10 +2900,9 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
28932900
git_zstream stream;
28942901
git_SHA_CTX c;
28952902
unsigned char parano_sha1[20];
2896-
char *filename;
28972903
static char tmp_file[PATH_MAX];
2904+
const char *filename = sha1_file_name(sha1);
28982905

2899-
filename = sha1_file_name(sha1);
29002906
fd = create_tmpfile(tmp_file, sizeof(tmp_file), filename);
29012907
if (fd < 0) {
29022908
if (errno == EACCES)

0 commit comments

Comments
 (0)