patch-id: convert to use the_hash_algo
Convert the two separate patch-id implementations to use the_hash_algo in their implementation. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
brian m. carlson committed
Aug 18, 2019 at 20:04 UTC
36261e42ec30477434f3325f279fd91a1d9eb434
3 files changed
+30
-29
builtin/patch-id.c
+6
-5
@@ -1,15 +1,16 @@
1
+#include "cache.h"
2
#include "builtin.h"
3
#include "config.h"
4
#include "diff.h"
5
6
static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result)
7
{
7
- char name[50];
8
+ char name[GIT_MAX_HEXSZ + 1];
9
10
if (!patchlen)
11
return;
12
12
- memcpy(name, oid_to_hex(id), GIT_SHA1_HEXSZ + 1);
13
+ memcpy(name, oid_to_hex(id), the_hash_algo->hexsz + 1);
14
printf("%s %s\n", oid_to_hex(result), name);
15
}
16
@@ -60,9 +61,9 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
61
{
62
int patchlen = 0, found_next = 0;
63
int before = -1, after = -1;
63
- git_SHA_CTX ctx;
64
+ git_hash_ctx ctx;
65
65
- git_SHA1_Init(&ctx);
66
+ the_hash_algo->init_fn(&ctx);
67
oidclr(result);
68
69
while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) {
@@ -122,7 +123,7 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
123
/* Compute the sha without whitespace */
124
len = remove_space(line);
125
patchlen += len;
125
- git_SHA1_Update(&ctx, line, len);
126
+ the_hash_algo->update_fn(&ctx, line, len);
127
}
128
129
if (!found_next)
diff.c
+23
-23
@@ -5978,7 +5978,7 @@ static void diff_summary(struct diff_options *opt, struct diff_filepair *p)
5978
}
5979
5980
struct patch_id_t {
5981
- git_SHA_CTX *ctx;
5981
+ git_hash_ctx *ctx;
5982
int patchlen;
5983
};
5984
@@ -5995,16 +5995,16 @@ static int remove_space(char *line, int len)
5995
return dst - line;
5996
}
5997
5998
-void flush_one_hunk(struct object_id *result, git_SHA_CTX *ctx)
5998
+void flush_one_hunk(struct object_id *result, git_hash_ctx *ctx)
5999
{
6000
unsigned char hash[GIT_MAX_RAWSZ];
6001
unsigned short carry = 0;
6002
int i;
6003
6004
- git_SHA1_Final(hash, ctx);
6005
- git_SHA1_Init(ctx);
6004
+ the_hash_algo->final_fn(hash, ctx);
6005
+ the_hash_algo->init_fn(ctx);
6006
/* 20-byte sum, with carry */
6007
- for (i = 0; i < GIT_SHA1_RAWSZ; ++i) {
6007
+ for (i = 0; i < the_hash_algo->rawsz; ++i) {
6008
carry += result->hash[i] + hash[i];
6009
result->hash[i] = carry;
6010
carry >>= 8;
@@ -6018,21 +6018,21 @@ static void patch_id_consume(void *priv, char *line, unsigned long len)
6018
6019
new_len = remove_space(line, len);
6020
6021
- git_SHA1_Update(data->ctx, line, new_len);
6021
+ the_hash_algo->update_fn(data->ctx, line, new_len);
6022
data->patchlen += new_len;
6023
}
6024
6025
-static void patch_id_add_string(git_SHA_CTX *ctx, const char *str)
6025
+static void patch_id_add_string(git_hash_ctx *ctx, const char *str)
6026
{
6027
- git_SHA1_Update(ctx, str, strlen(str));
6027
+ the_hash_algo->update_fn(ctx, str, strlen(str));
6028
}
6029
6030
-static void patch_id_add_mode(git_SHA_CTX *ctx, unsigned mode)
6030
+static void patch_id_add_mode(git_hash_ctx *ctx, unsigned mode)
6031
{
6032
/* large enough for 2^32 in octal */
6033
char buf[12];
6034
int len = xsnprintf(buf, sizeof(buf), "%06o", mode);
6035
- git_SHA1_Update(ctx, buf, len);
6035
+ the_hash_algo->update_fn(ctx, buf, len);
6036
}
6037
6038
/* returns 0 upon success, and writes result into oid */
@@ -6040,10 +6040,10 @@ static int diff_get_patch_id(struct diff_options *options, struct object_id *oid
6040
{
6041
struct diff_queue_struct *q = &diff_queued_diff;
6042
int i;
6043
- git_SHA_CTX ctx;
6043
+ git_hash_ctx ctx;
6044
struct patch_id_t data;
6045
6046
- git_SHA1_Init(&ctx);
6046
+ the_hash_algo->init_fn(&ctx);
6047
memset(&data, 0, sizeof(struct patch_id_t));
6048
data.ctx = &ctx;
6049
oidclr(oid);
@@ -6076,27 +6076,27 @@ static int diff_get_patch_id(struct diff_options *options, struct object_id *oid
6076
len2 = remove_space(p->two->path, strlen(p->two->path));
6077
patch_id_add_string(&ctx, "diff--git");
6078
patch_id_add_string(&ctx, "a/");
6079
- git_SHA1_Update(&ctx, p->one->path, len1);
6079
+ the_hash_algo->update_fn(&ctx, p->one->path, len1);
6080
patch_id_add_string(&ctx, "b/");
6081
- git_SHA1_Update(&ctx, p->two->path, len2);
6081
+ the_hash_algo->update_fn(&ctx, p->two->path, len2);
6082
6083
if (p->one->mode == 0) {
6084
patch_id_add_string(&ctx, "newfilemode");
6085
patch_id_add_mode(&ctx, p->two->mode);
6086
patch_id_add_string(&ctx, "---/dev/null");
6087
patch_id_add_string(&ctx, "+++b/");
6088
- git_SHA1_Update(&ctx, p->two->path, len2);
6088
+ the_hash_algo->update_fn(&ctx, p->two->path, len2);
6089
} else if (p->two->mode == 0) {
6090
patch_id_add_string(&ctx, "deletedfilemode");
6091
patch_id_add_mode(&ctx, p->one->mode);
6092
patch_id_add_string(&ctx, "---a/");
6093
- git_SHA1_Update(&ctx, p->one->path, len1);
6093
+ the_hash_algo->update_fn(&ctx, p->one->path, len1);
6094
patch_id_add_string(&ctx, "+++/dev/null");
6095
} else {
6096
patch_id_add_string(&ctx, "---a/");
6097
- git_SHA1_Update(&ctx, p->one->path, len1);
6097
+ the_hash_algo->update_fn(&ctx, p->one->path, len1);
6098
patch_id_add_string(&ctx, "+++b/");
6099
- git_SHA1_Update(&ctx, p->two->path, len2);
6099
+ the_hash_algo->update_fn(&ctx, p->two->path, len2);
6100
}
6101
6102
if (diff_header_only)
@@ -6108,10 +6108,10 @@ static int diff_get_patch_id(struct diff_options *options, struct object_id *oid
6108
6109
if (diff_filespec_is_binary(options->repo, p->one) ||
6110
diff_filespec_is_binary(options->repo, p->two)) {
6111
- git_SHA1_Update(&ctx, oid_to_hex(&p->one->oid),
6112
- GIT_SHA1_HEXSZ);
6113
- git_SHA1_Update(&ctx, oid_to_hex(&p->two->oid),
6114
- GIT_SHA1_HEXSZ);
6111
+ the_hash_algo->update_fn(&ctx, oid_to_hex(&p->one->oid),
6112
+ the_hash_algo->hexsz);
6113
+ the_hash_algo->update_fn(&ctx, oid_to_hex(&p->two->oid),
6114
+ the_hash_algo->hexsz);
6115
continue;
6116
}
6117
@@ -6128,7 +6128,7 @@ static int diff_get_patch_id(struct diff_options *options, struct object_id *oid
6128
}
6129
6130
if (!stable)
6131
- git_SHA1_Final(oid->hash, &ctx);
6131
+ the_hash_algo->final_fn(oid->hash, &ctx);
6132
6133
return 0;
6134
}
diff.h
+1
-1
@@ -438,7 +438,7 @@ int run_diff_index(struct rev_info *revs, int cached);
438
439
int do_diff_cache(const struct object_id *, struct diff_options *);
440
int diff_flush_patch_id(struct diff_options *, struct object_id *, int, int);
441
-void flush_one_hunk(struct object_id *, git_SHA_CTX *);
441
+void flush_one_hunk(struct object_id *result, git_hash_ctx *ctx);
442
443
int diff_result_code(struct diff_options *, int);
444