Skip to content

Commit 0d7c419

Browse files
bk2204gitster
authored andcommitted
rerere: convert to use the_hash_algo
Since this data is stored in the .git directory, it makes sense for us to use the same hash algorithm for it as for everything else. Convert the remaining uses of SHA-1 to use the_hash_algo. Use GIT_MAX_RAWSZ for allocations. Rename various struct members, local variables, and a function to be named "hash" instead of "sha1". Signed-off-by: brian m. carlson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dda6346 commit 0d7c419

File tree

1 file changed

+42
-39
lines changed

1 file changed

+42
-39
lines changed

rerere.c

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static int rerere_dir_alloc;
2929
#define RR_HAS_POSTIMAGE 1
3030
#define RR_HAS_PREIMAGE 2
3131
static struct rerere_dir {
32-
unsigned char sha1[20];
32+
unsigned char hash[GIT_MAX_HEXSZ];
3333
int status_alloc, status_nr;
3434
unsigned char *status;
3535
} **rerere_dir;
@@ -52,7 +52,7 @@ static void free_rerere_id(struct string_list_item *item)
5252

5353
static const char *rerere_id_hex(const struct rerere_id *id)
5454
{
55-
return sha1_to_hex(id->collection->sha1);
55+
return sha1_to_hex(id->collection->hash);
5656
}
5757

5858
static void fit_variant(struct rerere_dir *rr_dir, int variant)
@@ -115,7 +115,7 @@ static int is_rr_file(const char *name, const char *filename, int *variant)
115115
static void scan_rerere_dir(struct rerere_dir *rr_dir)
116116
{
117117
struct dirent *de;
118-
DIR *dir = opendir(git_path("rr-cache/%s", sha1_to_hex(rr_dir->sha1)));
118+
DIR *dir = opendir(git_path("rr-cache/%s", sha1_to_hex(rr_dir->hash)));
119119

120120
if (!dir)
121121
return;
@@ -133,24 +133,24 @@ static void scan_rerere_dir(struct rerere_dir *rr_dir)
133133
closedir(dir);
134134
}
135135

136-
static const unsigned char *rerere_dir_sha1(size_t i, void *table)
136+
static const unsigned char *rerere_dir_hash(size_t i, void *table)
137137
{
138138
struct rerere_dir **rr_dir = table;
139-
return rr_dir[i]->sha1;
139+
return rr_dir[i]->hash;
140140
}
141141

142142
static struct rerere_dir *find_rerere_dir(const char *hex)
143143
{
144-
unsigned char sha1[20];
144+
unsigned char hash[GIT_MAX_RAWSZ];
145145
struct rerere_dir *rr_dir;
146146
int pos;
147147

148-
if (get_sha1_hex(hex, sha1))
148+
if (get_sha1_hex(hex, hash))
149149
return NULL; /* BUG */
150-
pos = sha1_pos(sha1, rerere_dir, rerere_dir_nr, rerere_dir_sha1);
150+
pos = sha1_pos(hash, rerere_dir, rerere_dir_nr, rerere_dir_hash);
151151
if (pos < 0) {
152152
rr_dir = xmalloc(sizeof(*rr_dir));
153-
hashcpy(rr_dir->sha1, sha1);
153+
hashcpy(rr_dir->hash, hash);
154154
rr_dir->status = NULL;
155155
rr_dir->status_nr = 0;
156156
rr_dir->status_alloc = 0;
@@ -207,26 +207,27 @@ static void read_rr(struct string_list *rr)
207207
return;
208208
while (!strbuf_getwholeline(&buf, in, '\0')) {
209209
char *path;
210-
unsigned char sha1[20];
210+
unsigned char hash[GIT_MAX_RAWSZ];
211211
struct rerere_id *id;
212212
int variant;
213+
const unsigned hexsz = the_hash_algo->hexsz;
213214

214215
/* There has to be the hash, tab, path and then NUL */
215-
if (buf.len < 42 || get_sha1_hex(buf.buf, sha1))
216+
if (buf.len < hexsz + 2 || get_sha1_hex(buf.buf, hash))
216217
die(_("corrupt MERGE_RR"));
217218

218-
if (buf.buf[40] != '.') {
219+
if (buf.buf[hexsz] != '.') {
219220
variant = 0;
220-
path = buf.buf + 40;
221+
path = buf.buf + hexsz;
221222
} else {
222223
errno = 0;
223-
variant = strtol(buf.buf + 41, &path, 10);
224+
variant = strtol(buf.buf + hexsz + 1, &path, 10);
224225
if (errno)
225226
die(_("corrupt MERGE_RR"));
226227
}
227228
if (*(path++) != '\t')
228229
die(_("corrupt MERGE_RR"));
229-
buf.buf[40] = '\0';
230+
buf.buf[hexsz] = '\0';
230231
id = new_rerere_id_hex(buf.buf);
231232
id->variant = variant;
232233
string_list_insert(rr, path)->util = id;
@@ -360,7 +361,7 @@ static void rerere_strbuf_putconflict(struct strbuf *buf, int ch, size_t size)
360361
}
361362

362363
static int handle_conflict(struct strbuf *out, struct rerere_io *io,
363-
int marker_size, git_SHA_CTX *ctx)
364+
int marker_size, git_hash_ctx *ctx)
364365
{
365366
enum {
366367
RR_SIDE_1 = 0, RR_SIDE_2, RR_ORIGINAL
@@ -398,10 +399,12 @@ static int handle_conflict(struct strbuf *out, struct rerere_io *io,
398399
strbuf_addbuf(out, &two);
399400
rerere_strbuf_putconflict(out, '>', marker_size);
400401
if (ctx) {
401-
git_SHA1_Update(ctx, one.buf ? one.buf : "",
402-
one.len + 1);
403-
git_SHA1_Update(ctx, two.buf ? two.buf : "",
404-
two.len + 1);
402+
the_hash_algo->update_fn(ctx, one.buf ?
403+
one.buf : "",
404+
one.len + 1);
405+
the_hash_algo->update_fn(ctx, two.buf ?
406+
two.buf : "",
407+
two.len + 1);
405408
}
406409
break;
407410
} else if (hunk == RR_SIDE_1)
@@ -430,18 +433,18 @@ static int handle_conflict(struct strbuf *out, struct rerere_io *io,
430433
* Return 1 if conflict hunks are found, 0 if there are no conflict
431434
* hunks and -1 if an error occured.
432435
*/
433-
static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_size)
436+
static int handle_path(unsigned char *hash, struct rerere_io *io, int marker_size)
434437
{
435-
git_SHA_CTX ctx;
438+
git_hash_ctx ctx;
436439
struct strbuf buf = STRBUF_INIT, out = STRBUF_INIT;
437440
int has_conflicts = 0;
438-
if (sha1)
439-
git_SHA1_Init(&ctx);
441+
if (hash)
442+
the_hash_algo->init_fn(&ctx);
440443

441444
while (!io->getline(&buf, io)) {
442445
if (is_cmarker(buf.buf, '<', marker_size)) {
443446
has_conflicts = handle_conflict(&out, io, marker_size,
444-
sha1 ? &ctx : NULL);
447+
hash ? &ctx : NULL);
445448
if (has_conflicts < 0)
446449
break;
447450
rerere_io_putmem(out.buf, out.len, io);
@@ -452,8 +455,8 @@ static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_siz
452455
strbuf_release(&buf);
453456
strbuf_release(&out);
454457

455-
if (sha1)
456-
git_SHA1_Final(sha1, &ctx);
458+
if (hash)
459+
the_hash_algo->final_fn(hash, &ctx);
457460

458461
return has_conflicts;
459462
}
@@ -462,7 +465,7 @@ static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_siz
462465
* Scan the path for conflicts, do the "handle_path()" thing above, and
463466
* return the number of conflict hunks found.
464467
*/
465-
static int handle_file(const char *path, unsigned char *sha1, const char *output)
468+
static int handle_file(const char *path, unsigned char *hash, const char *output)
466469
{
467470
int has_conflicts = 0;
468471
struct rerere_io_file io;
@@ -484,7 +487,7 @@ static int handle_file(const char *path, unsigned char *sha1, const char *output
484487
}
485488
}
486489

487-
has_conflicts = handle_path(sha1, (struct rerere_io *)&io, marker_size);
490+
has_conflicts = handle_path(hash, (struct rerere_io *)&io, marker_size);
488491

489492
fclose(io.input);
490493
if (io.io.wrerror)
@@ -814,7 +817,7 @@ static int do_plain_rerere(struct string_list *rr, int fd)
814817
*/
815818
for (i = 0; i < conflict.nr; i++) {
816819
struct rerere_id *id;
817-
unsigned char sha1[20];
820+
unsigned char hash[GIT_MAX_RAWSZ];
818821
const char *path = conflict.items[i].string;
819822
int ret;
820823

@@ -823,15 +826,15 @@ static int do_plain_rerere(struct string_list *rr, int fd)
823826
* conflict ID. No need to write anything out
824827
* yet.
825828
*/
826-
ret = handle_file(path, sha1, NULL);
829+
ret = handle_file(path, hash, NULL);
827830
if (ret != 0 && string_list_has_string(rr, path)) {
828831
remove_variant(string_list_lookup(rr, path)->util);
829832
string_list_remove(rr, path, 1);
830833
}
831834
if (ret < 1)
832835
continue;
833836

834-
id = new_rerere_id(sha1);
837+
id = new_rerere_id(hash);
835838
string_list_insert(rr, path)->util = id;
836839

837840
/* Ensure that the directory exists. */
@@ -942,7 +945,7 @@ static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
942945
return 0;
943946
}
944947

945-
static int handle_cache(const char *path, unsigned char *sha1, const char *output)
948+
static int handle_cache(const char *path, unsigned char *hash, const char *output)
946949
{
947950
mmfile_t mmfile[3] = {{NULL}};
948951
mmbuffer_t result = {NULL, 0};
@@ -1001,7 +1004,7 @@ static int handle_cache(const char *path, unsigned char *sha1, const char *outpu
10011004
* Grab the conflict ID and optionally write the original
10021005
* contents with conflict markers out.
10031006
*/
1004-
has_conflicts = handle_path(sha1, (struct rerere_io *)&io, marker_size);
1007+
has_conflicts = handle_path(hash, (struct rerere_io *)&io, marker_size);
10051008
strbuf_release(&io.input);
10061009
if (io.io.output)
10071010
fclose(io.io.output);
@@ -1012,20 +1015,20 @@ static int rerere_forget_one_path(const char *path, struct string_list *rr)
10121015
{
10131016
const char *filename;
10141017
struct rerere_id *id;
1015-
unsigned char sha1[20];
1018+
unsigned char hash[GIT_MAX_RAWSZ];
10161019
int ret;
10171020
struct string_list_item *item;
10181021

10191022
/*
10201023
* Recreate the original conflict from the stages in the
10211024
* index and compute the conflict ID
10221025
*/
1023-
ret = handle_cache(path, sha1, NULL);
1026+
ret = handle_cache(path, hash, NULL);
10241027
if (ret < 1)
10251028
return error(_("could not parse conflict hunks in '%s'"), path);
10261029

10271030
/* Nuke the recorded resolution for the conflict */
1028-
id = new_rerere_id(sha1);
1031+
id = new_rerere_id(hash);
10291032

10301033
for (id->variant = 0;
10311034
id->variant < id->collection->status_nr;
@@ -1037,7 +1040,7 @@ static int rerere_forget_one_path(const char *path, struct string_list *rr)
10371040
if (!has_rerere_resolution(id))
10381041
continue;
10391042

1040-
handle_cache(path, sha1, rerere_path(id, "thisimage"));
1043+
handle_cache(path, hash, rerere_path(id, "thisimage"));
10411044
if (read_mmfile(&cur, rerere_path(id, "thisimage"))) {
10421045
free(cur.ptr);
10431046
error(_("failed to update conflicted state in '%s'"), path);
@@ -1069,7 +1072,7 @@ static int rerere_forget_one_path(const char *path, struct string_list *rr)
10691072
* conflict in the working tree, run us again to record
10701073
* the postimage.
10711074
*/
1072-
handle_cache(path, sha1, rerere_path(id, "preimage"));
1075+
handle_cache(path, hash, rerere_path(id, "preimage"));
10731076
fprintf_ln(stderr, _("Updated preimage for '%s'"), path);
10741077

10751078
/*

0 commit comments

Comments
 (0)