Skip to content

Commit 8429f2b

Browse files
committed
Merge branch 'bc/object-id'
Move from unsigned char[20] to struct object_id continues. * bc/object-id: match-trees: convert several leaf functions to use struct object_id tree-walk: convert tree_entry_extract() to use struct object_id struct name_entry: use struct object_id instead of unsigned char sha1[20] match-trees: convert shift_tree() and shift_tree_by() to use object_id test-match-trees: convert to use struct object_id sha1-name: introduce a get_oid() function
2 parents 89d3eaf + b6aec86 commit 8429f2b

21 files changed

+109
-98
lines changed

builtin/grep.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
438438
strbuf_add(base, entry.path, te_len);
439439

440440
if (S_ISREG(entry.mode)) {
441-
hit |= grep_sha1(opt, entry.sha1, base->buf, tn_len,
441+
hit |= grep_sha1(opt, entry.oid->hash, base->buf, tn_len,
442442
check_attr ? base->buf + tn_len : NULL);
443443
}
444444
else if (S_ISDIR(entry.mode)) {
@@ -447,10 +447,10 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
447447
void *data;
448448
unsigned long size;
449449

450-
data = lock_and_read_sha1_file(entry.sha1, &type, &size);
450+
data = lock_and_read_sha1_file(entry.oid->hash, &type, &size);
451451
if (!data)
452452
die(_("unable to read tree (%s)"),
453-
sha1_to_hex(entry.sha1));
453+
oid_to_hex(entry.oid));
454454

455455
strbuf_addch(base, '/');
456456
init_tree_desc(&sub, data, size);

builtin/merge-tree.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,15 @@ static void show_result(void)
150150
/* An empty entry never compares same, not even to another empty entry */
151151
static int same_entry(struct name_entry *a, struct name_entry *b)
152152
{
153-
return a->sha1 &&
154-
b->sha1 &&
155-
!hashcmp(a->sha1, b->sha1) &&
153+
return a->oid &&
154+
b->oid &&
155+
!oidcmp(a->oid, b->oid) &&
156156
a->mode == b->mode;
157157
}
158158

159159
static int both_empty(struct name_entry *a, struct name_entry *b)
160160
{
161-
return !(a->sha1 || b->sha1);
161+
return !(a->oid || b->oid);
162162
}
163163

164164
static struct merge_list *create_entry(unsigned stage, unsigned mode, const unsigned char *sha1, const char *path)
@@ -188,8 +188,8 @@ static void resolve(const struct traverse_info *info, struct name_entry *ours, s
188188
return;
189189

190190
path = traverse_path(info, result);
191-
orig = create_entry(2, ours->mode, ours->sha1, path);
192-
final = create_entry(0, result->mode, result->sha1, path);
191+
orig = create_entry(2, ours->mode, ours->oid->hash, path);
192+
final = create_entry(0, result->mode, result->oid->hash, path);
193193

194194
final->link = orig;
195195

@@ -213,7 +213,7 @@ static void unresolved_directory(const struct traverse_info *info,
213213

214214
newbase = traverse_path(info, p);
215215

216-
#define ENTRY_SHA1(e) (((e)->mode && S_ISDIR((e)->mode)) ? (e)->sha1 : NULL)
216+
#define ENTRY_SHA1(e) (((e)->mode && S_ISDIR((e)->mode)) ? (e)->oid->hash : NULL)
217217
buf0 = fill_tree_descriptor(t+0, ENTRY_SHA1(n + 0));
218218
buf1 = fill_tree_descriptor(t+1, ENTRY_SHA1(n + 1));
219219
buf2 = fill_tree_descriptor(t+2, ENTRY_SHA1(n + 2));
@@ -239,7 +239,7 @@ static struct merge_list *link_entry(unsigned stage, const struct traverse_info
239239
path = entry->path;
240240
else
241241
path = traverse_path(info, n);
242-
link = create_entry(stage, n->mode, n->sha1, path);
242+
link = create_entry(stage, n->mode, n->oid->hash, path);
243243
link->link = entry;
244244
return link;
245245
}
@@ -314,7 +314,7 @@ static int threeway_callback(int n, unsigned long mask, unsigned long dirmask, s
314314
}
315315

316316
if (same_entry(entry+0, entry+1)) {
317-
if (entry[2].sha1 && !S_ISDIR(entry[2].mode)) {
317+
if (entry[2].oid && !S_ISDIR(entry[2].mode)) {
318318
/* We did not touch, they modified -- take theirs */
319319
resolve(info, entry+1, entry+2);
320320
return mask;

builtin/pack-objects.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,7 @@ static void add_pbase_object(struct tree_desc *tree,
11861186
if (cmp < 0)
11871187
return;
11881188
if (name[cmplen] != '/') {
1189-
add_object_entry(entry.sha1,
1189+
add_object_entry(entry.oid->hash,
11901190
object_type(entry.mode),
11911191
fullname, 1);
11921192
return;
@@ -1197,7 +1197,7 @@ static void add_pbase_object(struct tree_desc *tree,
11971197
const char *down = name+cmplen+1;
11981198
int downlen = name_cmp_len(down);
11991199

1200-
tree = pbase_tree_get(entry.sha1);
1200+
tree = pbase_tree_get(entry.oid->hash);
12011201
if (!tree)
12021202
return;
12031203
init_tree_desc(&sub, tree->tree_data, tree->tree_size);

builtin/reflog.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ static int tree_is_complete(const unsigned char *sha1)
8484
init_tree_desc(&desc, tree->buffer, tree->size);
8585
complete = 1;
8686
while (tree_entry(&desc, &entry)) {
87-
if (!has_sha1_file(entry.sha1) ||
88-
(S_ISDIR(entry.mode) && !tree_is_complete(entry.sha1))) {
87+
if (!has_sha1_file(entry.oid->hash) ||
88+
(S_ISDIR(entry.mode) && !tree_is_complete(entry.oid->hash))) {
8989
tree->object.flags |= INCOMPLETE;
9090
complete = 0;
9191
}

cache-tree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
663663
cnt++;
664664
else {
665665
struct cache_tree_sub *sub;
666-
struct tree *subtree = lookup_tree(entry.sha1);
666+
struct tree *subtree = lookup_tree(entry.oid->hash);
667667
if (!subtree->object.parsed)
668668
parse_tree(subtree);
669669
sub = cache_tree_sub(it, entry.path);
@@ -710,7 +710,7 @@ int cache_tree_matches_traversal(struct cache_tree *root,
710710

711711
it = find_cache_tree_from_traversal(root, info);
712712
it = cache_tree_find(it, ent->path);
713-
if (it && it->entry_count > 0 && !hashcmp(ent->sha1, it->sha1))
713+
if (it && it->entry_count > 0 && !hashcmp(ent->oid->hash, it->sha1))
714714
return it->entry_count;
715715
return 0;
716716
}

cache.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,8 @@ extern int get_sha1_blob(const char *str, unsigned char *sha1);
11541154
extern void maybe_die_on_misspelt_object_name(const char *name, const char *prefix);
11551155
extern int get_sha1_with_context(const char *str, unsigned flags, unsigned char *sha1, struct object_context *orc);
11561156

1157+
extern int get_oid(const char *str, struct object_id *oid);
1158+
11571159
typedef int each_abbrev_fn(const unsigned char *sha1, void *);
11581160
extern int for_each_abbrev(const char *prefix, each_abbrev_fn, void *);
11591161

@@ -1764,8 +1766,8 @@ int add_files_to_cache(const char *prefix, const struct pathspec *pathspec, int
17641766
extern int diff_auto_refresh_index;
17651767

17661768
/* match-trees.c */
1767-
void shift_tree(const unsigned char *, const unsigned char *, unsigned char *, int);
1768-
void shift_tree_by(const unsigned char *, const unsigned char *, unsigned char *, const char *);
1769+
void shift_tree(const struct object_id *, const struct object_id *, struct object_id *, int);
1770+
void shift_tree_by(const struct object_id *, const struct object_id *, struct object_id *, const char *);
17691771

17701772
/*
17711773
* whitespace rules.

fsck.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,9 @@ static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *op
312312
if (S_ISGITLINK(entry.mode))
313313
continue;
314314
if (S_ISDIR(entry.mode))
315-
result = options->walk(&lookup_tree(entry.sha1)->object, OBJ_TREE, data, options);
315+
result = options->walk(&lookup_tree(entry.oid->hash)->object, OBJ_TREE, data, options);
316316
else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode))
317-
result = options->walk(&lookup_blob(entry.sha1)->object, OBJ_BLOB, data, options);
317+
result = options->walk(&lookup_blob(entry.oid->hash)->object, OBJ_BLOB, data, options);
318318
else {
319319
result = error("in tree %s: entry %s has bad mode %.6o",
320320
oid_to_hex(&tree->object.oid), entry.path, entry.mode);
@@ -450,11 +450,11 @@ static int fsck_tree(struct tree *item, struct fsck_options *options)
450450
while (desc.size) {
451451
unsigned mode;
452452
const char *name;
453-
const unsigned char *sha1;
453+
const struct object_id *oid;
454454

455-
sha1 = tree_entry_extract(&desc, &name, &mode);
455+
oid = tree_entry_extract(&desc, &name, &mode);
456456

457-
has_null_sha1 |= is_null_sha1(sha1);
457+
has_null_sha1 |= is_null_oid(oid);
458458
has_full_path |= !!strchr(name, '/');
459459
has_empty_name |= !*name;
460460
has_dot |= !strcmp(name, ".");

http-push.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,10 +1312,10 @@ static struct object_list **process_tree(struct tree *tree,
13121312
while (tree_entry(&desc, &entry))
13131313
switch (object_type(entry.mode)) {
13141314
case OBJ_TREE:
1315-
p = process_tree(lookup_tree(entry.sha1), p);
1315+
p = process_tree(lookup_tree(entry.oid->hash), p);
13161316
break;
13171317
case OBJ_BLOB:
1318-
p = process_blob(lookup_blob(entry.sha1), p);
1318+
p = process_blob(lookup_blob(entry.oid->hash), p);
13191319
break;
13201320
default:
13211321
/* Subproject commit - not in this repository */

list-objects.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,16 @@ static void process_tree(struct rev_info *revs,
110110

111111
if (S_ISDIR(entry.mode))
112112
process_tree(revs,
113-
lookup_tree(entry.sha1),
113+
lookup_tree(entry.oid->hash),
114114
show, base, entry.path,
115115
cb_data);
116116
else if (S_ISGITLINK(entry.mode))
117-
process_gitlink(revs, entry.sha1,
117+
process_gitlink(revs, entry.oid->hash,
118118
show, base, entry.path,
119119
cb_data);
120120
else
121121
process_blob(revs,
122-
lookup_blob(entry.sha1),
122+
lookup_blob(entry.oid->hash),
123123
show, base, entry.path,
124124
cb_data);
125125
}

match-trees.c

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@ static int score_matches(unsigned mode1, unsigned mode2, const char *path)
4848
}
4949

5050
static void *fill_tree_desc_strict(struct tree_desc *desc,
51-
const unsigned char *hash)
51+
const struct object_id *hash)
5252
{
5353
void *buffer;
5454
enum object_type type;
5555
unsigned long size;
5656

57-
buffer = read_sha1_file(hash, &type, &size);
57+
buffer = read_sha1_file(hash->hash, &type, &size);
5858
if (!buffer)
59-
die("unable to read tree (%s)", sha1_to_hex(hash));
59+
die("unable to read tree (%s)", oid_to_hex(hash));
6060
if (type != OBJ_TREE)
61-
die("%s is not a tree", sha1_to_hex(hash));
61+
die("%s is not a tree", oid_to_hex(hash));
6262
init_tree_desc(desc, buffer, size);
6363
return buffer;
6464
}
@@ -73,7 +73,7 @@ static int base_name_entries_compare(const struct name_entry *a,
7373
/*
7474
* Inspect two trees, and give a score that tells how similar they are.
7575
*/
76-
static int score_trees(const unsigned char *hash1, const unsigned char *hash2)
76+
static int score_trees(const struct object_id *hash1, const struct object_id *hash2)
7777
{
7878
struct tree_desc one;
7979
struct tree_desc two;
@@ -104,7 +104,7 @@ static int score_trees(const unsigned char *hash1, const unsigned char *hash2)
104104
else if (cmp > 0)
105105
/* path2 does not appear in one */
106106
score += score_missing(e2.mode, e2.path);
107-
else if (hashcmp(e1.sha1, e2.sha1))
107+
else if (oidcmp(e1.oid, e2.oid))
108108
/* they are different */
109109
score += score_differs(e1.mode, e2.mode, e1.path);
110110
else
@@ -119,8 +119,8 @@ static int score_trees(const unsigned char *hash1, const unsigned char *hash2)
119119
/*
120120
* Match one itself and its subtrees with two and pick the best match.
121121
*/
122-
static void match_trees(const unsigned char *hash1,
123-
const unsigned char *hash2,
122+
static void match_trees(const struct object_id *hash1,
123+
const struct object_id *hash2,
124124
int *best_score,
125125
char **best_match,
126126
const char *base,
@@ -131,7 +131,7 @@ static void match_trees(const unsigned char *hash1,
131131

132132
while (one.size) {
133133
const char *path;
134-
const unsigned char *elem;
134+
const struct object_id *elem;
135135
unsigned mode;
136136
int score;
137137

@@ -191,15 +191,15 @@ static int splice_tree(const unsigned char *hash1,
191191
while (desc.size) {
192192
const char *name;
193193
unsigned mode;
194-
const unsigned char *sha1;
194+
const struct object_id *oid;
195195

196-
sha1 = tree_entry_extract(&desc, &name, &mode);
196+
oid = tree_entry_extract(&desc, &name, &mode);
197197
if (strlen(name) == toplen &&
198198
!memcmp(name, prefix, toplen)) {
199199
if (!S_ISDIR(mode))
200200
die("entry %s in tree %s is not a tree",
201201
name, sha1_to_hex(hash1));
202-
rewrite_here = (unsigned char *) sha1;
202+
rewrite_here = (unsigned char *) oid->hash;
203203
break;
204204
}
205205
update_tree_entry(&desc);
@@ -229,9 +229,9 @@ static int splice_tree(const unsigned char *hash1,
229229
* other hand, it could cover tree one and we might need to pick a
230230
* subtree of it.
231231
*/
232-
void shift_tree(const unsigned char *hash1,
233-
const unsigned char *hash2,
234-
unsigned char *shifted,
232+
void shift_tree(const struct object_id *hash1,
233+
const struct object_id *hash2,
234+
struct object_id *shifted,
235235
int depth_limit)
236236
{
237237
char *add_prefix;
@@ -262,7 +262,7 @@ void shift_tree(const unsigned char *hash1,
262262
match_trees(hash2, hash1, &del_score, &del_prefix, "", depth_limit);
263263

264264
/* Assume we do not have to do any shifting */
265-
hashcpy(shifted, hash2);
265+
oidcpy(shifted, hash2);
266266

267267
if (add_score < del_score) {
268268
/* We need to pick a subtree of two */
@@ -271,39 +271,39 @@ void shift_tree(const unsigned char *hash1,
271271
if (!*del_prefix)
272272
return;
273273

274-
if (get_tree_entry(hash2, del_prefix, shifted, &mode))
274+
if (get_tree_entry(hash2->hash, del_prefix, shifted->hash, &mode))
275275
die("cannot find path %s in tree %s",
276-
del_prefix, sha1_to_hex(hash2));
276+
del_prefix, oid_to_hex(hash2));
277277
return;
278278
}
279279

280280
if (!*add_prefix)
281281
return;
282282

283-
splice_tree(hash1, add_prefix, hash2, shifted);
283+
splice_tree(hash1->hash, add_prefix, hash2->hash, shifted->hash);
284284
}
285285

286286
/*
287287
* The user says the trees will be shifted by this much.
288288
* Unfortunately we cannot fundamentally tell which one to
289289
* be prefixed, as recursive merge can work in either direction.
290290
*/
291-
void shift_tree_by(const unsigned char *hash1,
292-
const unsigned char *hash2,
293-
unsigned char *shifted,
291+
void shift_tree_by(const struct object_id *hash1,
292+
const struct object_id *hash2,
293+
struct object_id *shifted,
294294
const char *shift_prefix)
295295
{
296-
unsigned char sub1[20], sub2[20];
296+
struct object_id sub1, sub2;
297297
unsigned mode1, mode2;
298298
unsigned candidate = 0;
299299

300300
/* Can hash2 be a tree at shift_prefix in tree hash1? */
301-
if (!get_tree_entry(hash1, shift_prefix, sub1, &mode1) &&
301+
if (!get_tree_entry(hash1->hash, shift_prefix, sub1.hash, &mode1) &&
302302
S_ISDIR(mode1))
303303
candidate |= 1;
304304

305305
/* Can hash1 be a tree at shift_prefix in tree hash2? */
306-
if (!get_tree_entry(hash2, shift_prefix, sub2, &mode2) &&
306+
if (!get_tree_entry(hash2->hash, shift_prefix, sub2.hash, &mode2) &&
307307
S_ISDIR(mode2))
308308
candidate |= 2;
309309

@@ -313,19 +313,19 @@ void shift_tree_by(const unsigned char *hash1,
313313
int score;
314314

315315
candidate = 0;
316-
score = score_trees(sub1, hash2);
316+
score = score_trees(&sub1, hash2);
317317
if (score > best_score) {
318318
candidate = 1;
319319
best_score = score;
320320
}
321-
score = score_trees(sub2, hash1);
321+
score = score_trees(&sub2, hash1);
322322
if (score > best_score)
323323
candidate = 2;
324324
}
325325

326326
if (!candidate) {
327327
/* Neither is plausible -- do not shift */
328-
hashcpy(shifted, hash2);
328+
oidcpy(shifted, hash2);
329329
return;
330330
}
331331

@@ -334,11 +334,11 @@ void shift_tree_by(const unsigned char *hash1,
334334
* shift tree2 down by adding shift_prefix above it
335335
* to match tree1.
336336
*/
337-
splice_tree(hash1, shift_prefix, hash2, shifted);
337+
splice_tree(hash1->hash, shift_prefix, hash2->hash, shifted->hash);
338338
else
339339
/*
340340
* shift tree2 up by removing shift_prefix from it
341341
* to match tree1.
342342
*/
343-
hashcpy(shifted, sub2);
343+
oidcpy(shifted, &sub2);
344344
}

0 commit comments

Comments
 (0)