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 <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Oct 15, 2018 at 00:02 UTC 0d7c419a94b7524ac854d5a6002b7541abab4f12
1 file changed +42 -39
rerere.c
+42 -39
@@ -29,7 +29,7 @@ static int rerere_dir_alloc;
29 #define RR_HAS_POSTIMAGE 1
30 #define RR_HAS_PREIMAGE 2
31 static struct rerere_dir {
32 - unsigned char sha1[20];
32 + unsigned char hash[GIT_MAX_HEXSZ];
33 int status_alloc, status_nr;
34 unsigned char *status;
35 } **rerere_dir;
@@ -52,7 +52,7 @@ static void free_rerere_id(struct string_list_item *item)
52
53 static const char *rerere_id_hex(const struct rerere_id *id)
54 {
55 - return sha1_to_hex(id->collection->sha1);
55 + return sha1_to_hex(id->collection->hash);
56 }
57
58 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)
115 static void scan_rerere_dir(struct rerere_dir *rr_dir)
116 {
117 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)));
119
120 if (!dir)
121 return;
@@ -133,24 +133,24 @@ static void scan_rerere_dir(struct rerere_dir *rr_dir)
133 closedir(dir);
134 }
135
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)
137 {
138 struct rerere_dir **rr_dir = table;
139 - return rr_dir[i]->sha1;
139 + return rr_dir[i]->hash;
140 }
141
142 static struct rerere_dir *find_rerere_dir(const char *hex)
143 {
144 - unsigned char sha1[20];
144 + unsigned char hash[GIT_MAX_RAWSZ];
145 struct rerere_dir *rr_dir;
146 int pos;
147
148 - if (get_sha1_hex(hex, sha1))
148 + if (get_sha1_hex(hex, hash))
149 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);
151 if (pos < 0) {
152 rr_dir = xmalloc(sizeof(*rr_dir));
153 - hashcpy(rr_dir->sha1, sha1);
153 + hashcpy(rr_dir->hash, hash);
154 rr_dir->status = NULL;
155 rr_dir->status_nr = 0;
156 rr_dir->status_alloc = 0;
@@ -207,26 +207,27 @@ static void read_rr(struct string_list *rr)
207 return;
208 while (!strbuf_getwholeline(&buf, in, '\0')) {
209 char *path;
210 - unsigned char sha1[20];
210 + unsigned char hash[GIT_MAX_RAWSZ];
211 struct rerere_id *id;
212 int variant;
213 + const unsigned hexsz = the_hash_algo->hexsz;
214
215 /* 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))
217 die(_("corrupt MERGE_RR"));
218
218 - if (buf.buf[40] != '.') {
219 + if (buf.buf[hexsz] != '.') {
220 variant = 0;
220 - path = buf.buf + 40;
221 + path = buf.buf + hexsz;
222 } else {
223 errno = 0;
223 - variant = strtol(buf.buf + 41, &path, 10);
224 + variant = strtol(buf.buf + hexsz + 1, &path, 10);
225 if (errno)
226 die(_("corrupt MERGE_RR"));
227 }
228 if (*(path++) != '\t')
229 die(_("corrupt MERGE_RR"));
229 - buf.buf[40] = '\0';
230 + buf.buf[hexsz] = '\0';
231 id = new_rerere_id_hex(buf.buf);
232 id->variant = variant;
233 string_list_insert(rr, path)->util = id;
@@ -360,7 +361,7 @@ static void rerere_strbuf_putconflict(struct strbuf *buf, int ch, size_t size)
361 }
362
363 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)
365 {
366 enum {
367 RR_SIDE_1 = 0, RR_SIDE_2, RR_ORIGINAL
@@ -398,10 +399,12 @@ static int handle_conflict(struct strbuf *out, struct rerere_io *io,
399 strbuf_addbuf(out, &two);
400 rerere_strbuf_putconflict(out, '>', marker_size);
401 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);
408 }
409 break;
410 } else if (hunk == RR_SIDE_1)
@@ -430,18 +433,18 @@ static int handle_conflict(struct strbuf *out, struct rerere_io *io,
433 * Return 1 if conflict hunks are found, 0 if there are no conflict
434 * hunks and -1 if an error occured.
435 */
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)
437 {
435 - git_SHA_CTX ctx;
438 + git_hash_ctx ctx;
439 struct strbuf buf = STRBUF_INIT, out = STRBUF_INIT;
440 int has_conflicts = 0;
438 - if (sha1)
439 - git_SHA1_Init(&ctx);
441 + if (hash)
442 + the_hash_algo->init_fn(&ctx);
443
444 while (!io->getline(&buf, io)) {
445 if (is_cmarker(buf.buf, '<', marker_size)) {
446 has_conflicts = handle_conflict(&out, io, marker_size,
444 - sha1 ? &ctx : NULL);
447 + hash ? &ctx : NULL);
448 if (has_conflicts < 0)
449 break;
450 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
455 strbuf_release(&buf);
456 strbuf_release(&out);
457
455 - if (sha1)
456 - git_SHA1_Final(sha1, &ctx);
458 + if (hash)
459 + the_hash_algo->final_fn(hash, &ctx);
460
461 return has_conflicts;
462 }
@@ -462,7 +465,7 @@ static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_siz
465 * Scan the path for conflicts, do the "handle_path()" thing above, and
466 * return the number of conflict hunks found.
467 */
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)
469 {
470 int has_conflicts = 0;
471 struct rerere_io_file io;
@@ -484,7 +487,7 @@ static int handle_file(const char *path, unsigned char *sha1, const char *output
487 }
488 }
489
487 - has_conflicts = handle_path(sha1, (struct rerere_io *)&io, marker_size);
490 + has_conflicts = handle_path(hash, (struct rerere_io *)&io, marker_size);
491
492 fclose(io.input);
493 if (io.io.wrerror)
@@ -814,7 +817,7 @@ static int do_plain_rerere(struct string_list *rr, int fd)
817 */
818 for (i = 0; i < conflict.nr; i++) {
819 struct rerere_id *id;
817 - unsigned char sha1[20];
820 + unsigned char hash[GIT_MAX_RAWSZ];
821 const char *path = conflict.items[i].string;
822 int ret;
823
@@ -823,7 +826,7 @@ static int do_plain_rerere(struct string_list *rr, int fd)
826 * conflict ID. No need to write anything out
827 * yet.
828 */
826 - ret = handle_file(path, sha1, NULL);
829 + ret = handle_file(path, hash, NULL);
830 if (ret != 0 && string_list_has_string(rr, path)) {
831 remove_variant(string_list_lookup(rr, path)->util);
832 string_list_remove(rr, path, 1);
@@ -831,7 +834,7 @@ static int do_plain_rerere(struct string_list *rr, int fd)
834 if (ret < 1)
835 continue;
836
834 - id = new_rerere_id(sha1);
837 + id = new_rerere_id(hash);
838 string_list_insert(rr, path)->util = id;
839
840 /* Ensure that the directory exists. */
@@ -942,7 +945,7 @@ static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
945 return 0;
946 }
947
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)
949 {
950 mmfile_t mmfile[3] = {{NULL}};
951 mmbuffer_t result = {NULL, 0};
@@ -1001,7 +1004,7 @@ static int handle_cache(const char *path, unsigned char *sha1, const char *outpu
1004 * Grab the conflict ID and optionally write the original
1005 * contents with conflict markers out.
1006 */
1004 - has_conflicts = handle_path(sha1, (struct rerere_io *)&io, marker_size);
1007 + has_conflicts = handle_path(hash, (struct rerere_io *)&io, marker_size);
1008 strbuf_release(&io.input);
1009 if (io.io.output)
1010 fclose(io.io.output);
@@ -1012,7 +1015,7 @@ static int rerere_forget_one_path(const char *path, struct string_list *rr)
1015 {
1016 const char *filename;
1017 struct rerere_id *id;
1015 - unsigned char sha1[20];
1018 + unsigned char hash[GIT_MAX_RAWSZ];
1019 int ret;
1020 struct string_list_item *item;
1021
@@ -1020,12 +1023,12 @@ static int rerere_forget_one_path(const char *path, struct string_list *rr)
1023 * Recreate the original conflict from the stages in the
1024 * index and compute the conflict ID
1025 */
1023 - ret = handle_cache(path, sha1, NULL);
1026 + ret = handle_cache(path, hash, NULL);
1027 if (ret < 1)
1028 return error(_("could not parse conflict hunks in '%s'"), path);
1029
1030 /* Nuke the recorded resolution for the conflict */
1028 - id = new_rerere_id(sha1);
1031 + id = new_rerere_id(hash);
1032
1033 for (id->variant = 0;
1034 id->variant < id->collection->status_nr;
@@ -1037,7 +1040,7 @@ static int rerere_forget_one_path(const char *path, struct string_list *rr)
1040 if (!has_rerere_resolution(id))
1041 continue;
1042
1040 - handle_cache(path, sha1, rerere_path(id, "thisimage"));
1043 + handle_cache(path, hash, rerere_path(id, "thisimage"));
1044 if (read_mmfile(&cur, rerere_path(id, "thisimage"))) {
1045 free(cur.ptr);
1046 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)
1072 * conflict in the working tree, run us again to record
1073 * the postimage.
1074 */
1072 - handle_cache(path, sha1, rerere_path(id, "preimage"));
1075 + handle_cache(path, hash, rerere_path(id, "preimage"));
1076 fprintf_ln(stderr, _("Updated preimage for '%s'"), path);
1077
1078 /*