Skip to content

Commit 9d5b1c0

Browse files
committed
Merge branch 'jk/use-oid-pos'
Code clean-up to ensure our use of hashtables using object names as keys use the "struct object_id" objects, not the raw hash values. * jk/use-oid-pos: oid_pos(): access table through const pointers hash_pos(): convert to oid_pos() rerere: use strmap to store rerere directories rerere: tighten rr-cache dirname check rerere: check dirname format while iterating rr_cache directory commit_graft_pos(): take an oid instead of a bare hash
2 parents 1d4f231 + 8380dcd commit 9d5b1c0

File tree

10 files changed

+87
-94
lines changed

10 files changed

+87
-94
lines changed

builtin/name-rev.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,10 @@ static void name_tips(void)
390390
}
391391
}
392392

393-
static const unsigned char *nth_tip_table_ent(size_t ix, void *table_)
393+
static const struct object_id *nth_tip_table_ent(size_t ix, const void *table_)
394394
{
395-
struct tip_table_entry *table = table_;
396-
return table[ix].oid.hash;
395+
const struct tip_table_entry *table = table_;
396+
return &table[ix].oid;
397397
}
398398

399399
static const char *get_exact_ref_match(const struct object *o)
@@ -408,8 +408,8 @@ static const char *get_exact_ref_match(const struct object *o)
408408
tip_table.sorted = 1;
409409
}
410410

411-
found = hash_pos(o->oid.hash, tip_table.table, tip_table.nr,
412-
nth_tip_table_ent);
411+
found = oid_pos(&o->oid, tip_table.table, tip_table.nr,
412+
nth_tip_table_ent);
413413
if (0 <= found)
414414
return tip_table.table[found].refname;
415415
return NULL;

commit-graph.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,10 +1012,10 @@ static int write_graph_chunk_oids(struct hashfile *f,
10121012
return 0;
10131013
}
10141014

1015-
static const unsigned char *commit_to_sha1(size_t index, void *table)
1015+
static const struct object_id *commit_to_oid(size_t index, const void *table)
10161016
{
1017-
struct commit **commits = table;
1018-
return commits[index]->object.oid.hash;
1017+
const struct commit * const *commits = table;
1018+
return &commits[index]->object.oid;
10191019
}
10201020

10211021
static int write_graph_chunk_data(struct hashfile *f,
@@ -1043,10 +1043,10 @@ static int write_graph_chunk_data(struct hashfile *f,
10431043
if (!parent)
10441044
edge_value = GRAPH_PARENT_NONE;
10451045
else {
1046-
edge_value = hash_pos(parent->item->object.oid.hash,
1047-
ctx->commits.list,
1048-
ctx->commits.nr,
1049-
commit_to_sha1);
1046+
edge_value = oid_pos(&parent->item->object.oid,
1047+
ctx->commits.list,
1048+
ctx->commits.nr,
1049+
commit_to_oid);
10501050

10511051
if (edge_value >= 0)
10521052
edge_value += ctx->new_num_commits_in_base;
@@ -1074,10 +1074,10 @@ static int write_graph_chunk_data(struct hashfile *f,
10741074
else if (parent->next)
10751075
edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
10761076
else {
1077-
edge_value = hash_pos(parent->item->object.oid.hash,
1078-
ctx->commits.list,
1079-
ctx->commits.nr,
1080-
commit_to_sha1);
1077+
edge_value = oid_pos(&parent->item->object.oid,
1078+
ctx->commits.list,
1079+
ctx->commits.nr,
1080+
commit_to_oid);
10811081

10821082
if (edge_value >= 0)
10831083
edge_value += ctx->new_num_commits_in_base;
@@ -1143,10 +1143,10 @@ static int write_graph_chunk_extra_edges(struct hashfile *f,
11431143

11441144
/* Since num_parents > 2, this initializer is safe. */
11451145
for (parent = (*list)->parents->next; parent; parent = parent->next) {
1146-
int edge_value = hash_pos(parent->item->object.oid.hash,
1147-
ctx->commits.list,
1148-
ctx->commits.nr,
1149-
commit_to_sha1);
1146+
int edge_value = oid_pos(&parent->item->object.oid,
1147+
ctx->commits.list,
1148+
ctx->commits.nr,
1149+
commit_to_oid);
11501150

11511151
if (edge_value >= 0)
11521152
edge_value += ctx->new_num_commits_in_base;

commit.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,23 +105,23 @@ static timestamp_t parse_commit_date(const char *buf, const char *tail)
105105
return parse_timestamp(dateptr, NULL, 10);
106106
}
107107

108-
static const unsigned char *commit_graft_sha1_access(size_t index, void *table)
108+
static const struct object_id *commit_graft_oid_access(size_t index, const void *table)
109109
{
110-
struct commit_graft **commit_graft_table = table;
111-
return commit_graft_table[index]->oid.hash;
110+
const struct commit_graft * const *commit_graft_table = table;
111+
return &commit_graft_table[index]->oid;
112112
}
113113

114-
int commit_graft_pos(struct repository *r, const unsigned char *sha1)
114+
int commit_graft_pos(struct repository *r, const struct object_id *oid)
115115
{
116-
return hash_pos(sha1, r->parsed_objects->grafts,
117-
r->parsed_objects->grafts_nr,
118-
commit_graft_sha1_access);
116+
return oid_pos(oid, r->parsed_objects->grafts,
117+
r->parsed_objects->grafts_nr,
118+
commit_graft_oid_access);
119119
}
120120

121121
int register_commit_graft(struct repository *r, struct commit_graft *graft,
122122
int ignore_dups)
123123
{
124-
int pos = commit_graft_pos(r, graft->oid.hash);
124+
int pos = commit_graft_pos(r, &graft->oid);
125125

126126
if (0 <= pos) {
127127
if (ignore_dups)
@@ -232,7 +232,7 @@ struct commit_graft *lookup_commit_graft(struct repository *r, const struct obje
232232
{
233233
int pos;
234234
prepare_commit_graft(r);
235-
pos = commit_graft_pos(r, oid->hash);
235+
pos = commit_graft_pos(r, oid);
236236
if (pos < 0)
237237
return NULL;
238238
return r->parsed_objects->grafts[pos];

commit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ typedef int (*each_commit_graft_fn)(const struct commit_graft *, void *);
239239

240240
struct commit_graft *read_graft_line(struct strbuf *line);
241241
/* commit_graft_pos returns an index into r->parsed_objects->grafts. */
242-
int commit_graft_pos(struct repository *r, const unsigned char *sha1);
242+
int commit_graft_pos(struct repository *r, const struct object_id *oid);
243243
int register_commit_graft(struct repository *r, struct commit_graft *, int);
244244
void prepare_commit_graft(struct repository *r);
245245
struct commit_graft *lookup_commit_graft(struct repository *r, const struct object_id *oid);

hash-lookup.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include "cache.h"
22
#include "hash-lookup.h"
33

4-
static uint32_t take2(const unsigned char *hash)
4+
static uint32_t take2(const struct object_id *oid, size_t ofs)
55
{
6-
return ((hash[0] << 8) | hash[1]);
6+
return ((oid->hash[ofs] << 8) | oid->hash[ofs + 1]);
77
}
88

99
/*
@@ -47,11 +47,11 @@ static uint32_t take2(const unsigned char *hash)
4747
*/
4848
/*
4949
* The table should contain "nr" elements.
50-
* The hash of element i (between 0 and nr - 1) should be returned
50+
* The oid of element i (between 0 and nr - 1) should be returned
5151
* by "fn(i, table)".
5252
*/
53-
int hash_pos(const unsigned char *hash, void *table, size_t nr,
54-
hash_access_fn fn)
53+
int oid_pos(const struct object_id *oid, const void *table, size_t nr,
54+
oid_access_fn fn)
5555
{
5656
size_t hi = nr;
5757
size_t lo = 0;
@@ -64,9 +64,9 @@ int hash_pos(const unsigned char *hash, void *table, size_t nr,
6464
size_t lov, hiv, miv, ofs;
6565

6666
for (ofs = 0; ofs < the_hash_algo->rawsz - 2; ofs += 2) {
67-
lov = take2(fn(0, table) + ofs);
68-
hiv = take2(fn(nr - 1, table) + ofs);
69-
miv = take2(hash + ofs);
67+
lov = take2(fn(0, table), ofs);
68+
hiv = take2(fn(nr - 1, table), ofs);
69+
miv = take2(oid, ofs);
7070
if (miv < lov)
7171
return -1;
7272
if (hiv < miv)
@@ -88,7 +88,7 @@ int hash_pos(const unsigned char *hash, void *table, size_t nr,
8888

8989
do {
9090
int cmp;
91-
cmp = hashcmp(fn(mi, table), hash);
91+
cmp = oidcmp(fn(mi, table), oid);
9292
if (!cmp)
9393
return mi;
9494
if (cmp > 0)

hash-lookup.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#ifndef HASH_LOOKUP_H
22
#define HASH_LOOKUP_H
33

4-
typedef const unsigned char *hash_access_fn(size_t index, void *table);
4+
typedef const struct object_id *oid_access_fn(size_t index, const void *table);
55

6-
int hash_pos(const unsigned char *hash,
7-
void *table,
8-
size_t nr,
9-
hash_access_fn fn);
6+
int oid_pos(const struct object_id *oid,
7+
const void *table,
8+
size_t nr,
9+
oid_access_fn fn);
1010

1111
/*
1212
* Searches for hash in table, using the given fanout table to determine the

oid-array.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ void oid_array_sort(struct oid_array *array)
2222
array->sorted = 1;
2323
}
2424

25-
static const unsigned char *sha1_access(size_t index, void *table)
25+
static const struct object_id *oid_access(size_t index, const void *table)
2626
{
27-
struct object_id *array = table;
28-
return array[index].hash;
27+
const struct object_id *array = table;
28+
return &array[index];
2929
}
3030

3131
int oid_array_lookup(struct oid_array *array, const struct object_id *oid)
3232
{
3333
oid_array_sort(array);
34-
return hash_pos(oid->hash, array->oid, array->nr, sha1_access);
34+
return oid_pos(oid, array->oid, array->nr, oid_access);
3535
}
3636

3737
void oid_array_clear(struct oid_array *array)

pack-bitmap-write.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -610,10 +610,10 @@ static inline void dump_bitmap(struct hashfile *f, struct ewah_bitmap *bitmap)
610610
die("Failed to write bitmap index");
611611
}
612612

613-
static const unsigned char *sha1_access(size_t pos, void *table)
613+
static const struct object_id *oid_access(size_t pos, const void *table)
614614
{
615-
struct pack_idx_entry **index = table;
616-
return index[pos]->oid.hash;
615+
const struct pack_idx_entry * const *index = table;
616+
return &index[pos]->oid;
617617
}
618618

619619
static void write_selected_commits_v1(struct hashfile *f,
@@ -626,7 +626,7 @@ static void write_selected_commits_v1(struct hashfile *f,
626626
struct bitmapped_commit *stored = &writer.selected[i];
627627

628628
int commit_pos =
629-
hash_pos(stored->commit->object.oid.hash, index, index_nr, sha1_access);
629+
oid_pos(&stored->commit->object.oid, index, index_nr, oid_access);
630630

631631
if (commit_pos < 0)
632632
BUG("trying to write commit not in index");

rerere.c

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "pathspec.h"
1212
#include "object-store.h"
1313
#include "hash-lookup.h"
14+
#include "strmap.h"
1415

1516
#define RESOLVED 0
1617
#define PUNTED 1
@@ -23,26 +24,27 @@ static int rerere_enabled = -1;
2324
/* automatically update cleanly resolved paths to the index */
2425
static int rerere_autoupdate;
2526

26-
static int rerere_dir_nr;
27-
static int rerere_dir_alloc;
28-
2927
#define RR_HAS_POSTIMAGE 1
3028
#define RR_HAS_PREIMAGE 2
31-
static struct rerere_dir {
32-
unsigned char hash[GIT_MAX_HEXSZ];
29+
struct rerere_dir {
3330
int status_alloc, status_nr;
3431
unsigned char *status;
35-
} **rerere_dir;
32+
char name[FLEX_ARRAY];
33+
};
34+
35+
static struct strmap rerere_dirs = STRMAP_INIT;
3636

3737
static void free_rerere_dirs(void)
3838
{
39-
int i;
40-
for (i = 0; i < rerere_dir_nr; i++) {
41-
free(rerere_dir[i]->status);
42-
free(rerere_dir[i]);
39+
struct hashmap_iter iter;
40+
struct strmap_entry *ent;
41+
42+
strmap_for_each_entry(&rerere_dirs, &iter, ent) {
43+
struct rerere_dir *rr_dir = ent->value;
44+
free(rr_dir->status);
45+
free(rr_dir);
4346
}
44-
FREE_AND_NULL(rerere_dir);
45-
rerere_dir_nr = rerere_dir_alloc = 0;
47+
strmap_clear(&rerere_dirs, 0);
4648
}
4749

4850
static void free_rerere_id(struct string_list_item *item)
@@ -52,7 +54,7 @@ static void free_rerere_id(struct string_list_item *item)
5254

5355
static const char *rerere_id_hex(const struct rerere_id *id)
5456
{
55-
return hash_to_hex(id->collection->hash);
57+
return id->collection->name;
5658
}
5759

5860
static void fit_variant(struct rerere_dir *rr_dir, int variant)
@@ -115,7 +117,7 @@ static int is_rr_file(const char *name, const char *filename, int *variant)
115117
static void scan_rerere_dir(struct rerere_dir *rr_dir)
116118
{
117119
struct dirent *de;
118-
DIR *dir = opendir(git_path("rr-cache/%s", hash_to_hex(rr_dir->hash)));
120+
DIR *dir = opendir(git_path("rr-cache/%s", rr_dir->name));
119121

120122
if (!dir)
121123
return;
@@ -133,39 +135,21 @@ static void scan_rerere_dir(struct rerere_dir *rr_dir)
133135
closedir(dir);
134136
}
135137

136-
static const unsigned char *rerere_dir_hash(size_t i, void *table)
137-
{
138-
struct rerere_dir **rr_dir = table;
139-
return rr_dir[i]->hash;
140-
}
141-
142138
static struct rerere_dir *find_rerere_dir(const char *hex)
143139
{
144-
unsigned char hash[GIT_MAX_RAWSZ];
145140
struct rerere_dir *rr_dir;
146-
int pos;
147-
148-
if (get_sha1_hex(hex, hash))
149-
return NULL; /* BUG */
150-
pos = hash_pos(hash, rerere_dir, rerere_dir_nr, rerere_dir_hash);
151-
if (pos < 0) {
152-
rr_dir = xmalloc(sizeof(*rr_dir));
153-
hashcpy(rr_dir->hash, hash);
141+
142+
rr_dir = strmap_get(&rerere_dirs, hex);
143+
if (!rr_dir) {
144+
FLEX_ALLOC_STR(rr_dir, name, hex);
154145
rr_dir->status = NULL;
155146
rr_dir->status_nr = 0;
156147
rr_dir->status_alloc = 0;
157-
pos = -1 - pos;
158-
159-
/* Make sure the array is big enough ... */
160-
ALLOC_GROW(rerere_dir, rerere_dir_nr + 1, rerere_dir_alloc);
161-
/* ... and add it in. */
162-
rerere_dir_nr++;
163-
MOVE_ARRAY(rerere_dir + pos + 1, rerere_dir + pos,
164-
rerere_dir_nr - pos - 1);
165-
rerere_dir[pos] = rr_dir;
148+
strmap_put(&rerere_dirs, hex, rr_dir);
149+
166150
scan_rerere_dir(rr_dir);
167151
}
168-
return rerere_dir[pos];
152+
return rr_dir;
169153
}
170154

171155
static int has_rerere_resolution(const struct rerere_id *id)
@@ -1178,6 +1162,14 @@ static void prune_one(struct rerere_id *id,
11781162
unlink_rr_item(id);
11791163
}
11801164

1165+
/* Does the basename in "path" look plausibly like an rr-cache entry? */
1166+
static int is_rr_cache_dirname(const char *path)
1167+
{
1168+
struct object_id oid;
1169+
const char *end;
1170+
return !parse_oid_hex(path, &oid, &end) && !*end;
1171+
}
1172+
11811173
void rerere_gc(struct repository *r, struct string_list *rr)
11821174
{
11831175
struct string_list to_remove = STRING_LIST_INIT_DUP;
@@ -1205,10 +1197,11 @@ void rerere_gc(struct repository *r, struct string_list *rr)
12051197

12061198
if (is_dot_or_dotdot(e->d_name))
12071199
continue;
1208-
rr_dir = find_rerere_dir(e->d_name);
1209-
if (!rr_dir)
1200+
if (!is_rr_cache_dirname(e->d_name))
12101201
continue; /* or should we remove e->d_name? */
12111202

1203+
rr_dir = find_rerere_dir(e->d_name);
1204+
12121205
now_empty = 1;
12131206
for (id.variant = 0, id.collection = rr_dir;
12141207
id.variant < id.collection->status_nr;

shallow.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int register_shallow(struct repository *r, const struct object_id *oid)
4141

4242
int unregister_shallow(const struct object_id *oid)
4343
{
44-
int pos = commit_graft_pos(the_repository, oid->hash);
44+
int pos = commit_graft_pos(the_repository, oid);
4545
if (pos < 0)
4646
return -1;
4747
if (pos + 1 < the_repository->parsed_objects->grafts_nr)

0 commit comments

Comments
 (0)