builtin/apply: convert static functions to struct object_id

There were several static functions using unsigned char arrays for SHA-1 values. Convert them to use struct object_id. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Sep 5, 2016 at 20:07 UTC eb1c9c7328f203d84f5f67e6ac7f3991c4bc189d
1 file changed +48 -48
builtin/apply.c
+48 -48
@@ -3101,16 +3101,16 @@ static int apply_binary(struct apply_state *state,
3101 struct patch *patch)
3102 {
3103 const char *name = patch->old_name ? patch->old_name : patch->new_name;
3104 - unsigned char sha1[20];
3104 + struct object_id oid;
3105
3106 /*
3107 * For safety, we require patch index line to contain
3108 * full 40-byte textual SHA1 for old and new, at least for now.
3109 */
3110 - if (strlen(patch->old_sha1_prefix) != 40 ||
3111 - strlen(patch->new_sha1_prefix) != 40 ||
3112 - get_sha1_hex(patch->old_sha1_prefix, sha1) ||
3113 - get_sha1_hex(patch->new_sha1_prefix, sha1))
3110 + if (strlen(patch->old_sha1_prefix) != GIT_SHA1_HEXSZ ||
3111 + strlen(patch->new_sha1_prefix) != GIT_SHA1_HEXSZ ||
3112 + get_oid_hex(patch->old_sha1_prefix, &oid) ||
3113 + get_oid_hex(patch->new_sha1_prefix, &oid))
3114 return error("cannot apply binary patch to '%s' "
3115 "without full index line", name);
3116
@@ -3119,12 +3119,12 @@ static int apply_binary(struct apply_state *state,
3119 * See if the old one matches what the patch
3120 * applies to.
3121 */
3122 - hash_sha1_file(img->buf, img->len, blob_type, sha1);
3123 - if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
3122 + hash_sha1_file(img->buf, img->len, blob_type, oid.hash);
3123 + if (strcmp(oid_to_hex(&oid), patch->old_sha1_prefix))
3124 return error("the patch applies to '%s' (%s), "
3125 "which does not match the "
3126 "current contents.",
3127 - name, sha1_to_hex(sha1));
3127 + name, oid_to_hex(&oid));
3128 }
3129 else {
3130 /* Otherwise, the old one must be empty. */
@@ -3133,19 +3133,19 @@ static int apply_binary(struct apply_state *state,
3133 "'%s' but it is not empty", name);
3134 }
3135
3136 - get_sha1_hex(patch->new_sha1_prefix, sha1);
3137 - if (is_null_sha1(sha1)) {
3136 + get_oid_hex(patch->new_sha1_prefix, &oid);
3137 + if (is_null_oid(&oid)) {
3138 clear_image(img);
3139 return 0; /* deletion patch */
3140 }
3141
3142 - if (has_sha1_file(sha1)) {
3142 + if (has_sha1_file(oid.hash)) {
3143 /* We already have the postimage */
3144 enum object_type type;
3145 unsigned long size;
3146 char *result;
3147
3148 - result = read_sha1_file(sha1, &type, &size);
3148 + result = read_sha1_file(oid.hash, &type, &size);
3149 if (!result)
3150 return error("the necessary postimage %s for "
3151 "'%s' cannot be read",
@@ -3164,10 +3164,10 @@ static int apply_binary(struct apply_state *state,
3164 name);
3165
3166 /* verify that the result matches */
3167 - hash_sha1_file(img->buf, img->len, blob_type, sha1);
3168 - if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))
3167 + hash_sha1_file(img->buf, img->len, blob_type, oid.hash);
3168 + if (strcmp(oid_to_hex(&oid), patch->new_sha1_prefix))
3169 return error(_("binary patch to '%s' creates incorrect result (expecting %s, got %s)"),
3170 - name, patch->new_sha1_prefix, sha1_to_hex(sha1));
3170 + name, patch->new_sha1_prefix, oid_to_hex(&oid));
3171 }
3172
3173 return 0;
@@ -3197,17 +3197,17 @@ static int apply_fragments(struct apply_state *state, struct image *img, struct
3197 return 0;
3198 }
3199
3200 -static int read_blob_object(struct strbuf *buf, const unsigned char *sha1, unsigned mode)
3200 +static int read_blob_object(struct strbuf *buf, const struct object_id *oid, unsigned mode)
3201 {
3202 if (S_ISGITLINK(mode)) {
3203 strbuf_grow(buf, 100);
3204 - strbuf_addf(buf, "Subproject commit %s\n", sha1_to_hex(sha1));
3204 + strbuf_addf(buf, "Subproject commit %s\n", oid_to_hex(oid));
3205 } else {
3206 enum object_type type;
3207 unsigned long sz;
3208 char *result;
3209
3210 - result = read_sha1_file(sha1, &type, &sz);
3210 + result = read_sha1_file(oid->hash, &type, &sz);
3211 if (!result)
3212 return -1;
3213 /* XXX read_sha1_file NUL-terminates */
@@ -3220,7 +3220,7 @@ static int read_file_or_gitlink(const struct cache_entry *ce, struct strbuf *buf
3220 {
3221 if (!ce)
3222 return 0;
3223 - return read_blob_object(buf, ce->oid.hash, ce->ce_mode);
3223 + return read_blob_object(buf, &ce->oid, ce->ce_mode);
3224 }
3225
3226 static struct patch *in_fn_table(struct apply_state *state, const char *name)
@@ -3427,17 +3427,17 @@ static int load_preimage(struct apply_state *state,
3427
3428 static int three_way_merge(struct image *image,
3429 char *path,
3430 - const unsigned char *base,
3431 - const unsigned char *ours,
3432 - const unsigned char *theirs)
3430 + const struct object_id *base,
3431 + const struct object_id *ours,
3432 + const struct object_id *theirs)
3433 {
3434 mmfile_t base_file, our_file, their_file;
3435 mmbuffer_t result = { NULL };
3436 int status;
3437
3438 - read_mmblob(&base_file, base);
3439 - read_mmblob(&our_file, ours);
3440 - read_mmblob(&their_file, theirs);
3438 + read_mmblob(&base_file, base->hash);
3439 + read_mmblob(&our_file, ours->hash);
3440 + read_mmblob(&their_file, theirs->hash);
3441 status = ll_merge(&result, path,
3442 &base_file, "base",
3443 &our_file, "ours",
@@ -3506,7 +3506,7 @@ static int try_threeway(struct apply_state *state,
3506 struct stat *st,
3507 const struct cache_entry *ce)
3508 {
3509 - unsigned char pre_sha1[20], post_sha1[20], our_sha1[20];
3509 + struct object_id pre_oid, post_oid, our_oid;
3510 struct strbuf buf = STRBUF_INIT;
3511 size_t len;
3512 int status;
@@ -3520,9 +3520,9 @@ static int try_threeway(struct apply_state *state,
3520
3521 /* Preimage the patch was prepared for */
3522 if (patch->is_new)
3523 - write_sha1_file("", 0, blob_type, pre_sha1);
3524 - else if (get_sha1(patch->old_sha1_prefix, pre_sha1) ||
3525 - read_blob_object(&buf, pre_sha1, patch->old_mode))
3523 + write_sha1_file("", 0, blob_type, pre_oid.hash);
3524 + else if (get_sha1(patch->old_sha1_prefix, pre_oid.hash) ||
3525 + read_blob_object(&buf, &pre_oid, patch->old_mode))
3526 return error("repository lacks the necessary blob to fall back on 3-way merge.");
3527
3528 fprintf(stderr, "Falling back to three-way merge...\n");
@@ -3535,7 +3535,7 @@ static int try_threeway(struct apply_state *state,
3535 return -1;
3536 }
3537 /* post_sha1[] is theirs */
3538 - write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, post_sha1);
3538 + write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, post_oid.hash);
3539 clear_image(&tmp_image);
3540
3541 /* our_sha1[] is ours */
@@ -3548,12 +3548,12 @@ static int try_threeway(struct apply_state *state,
3548 return error("cannot read the current contents of '%s'",
3549 patch->old_name);
3550 }
3551 - write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, our_sha1);
3551 + write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, our_oid.hash);
3552 clear_image(&tmp_image);
3553
3554 /* in-core three-way merge between post and our using pre as base */
3555 status = three_way_merge(image, patch->new_name,
3556 - pre_sha1, our_sha1, post_sha1);
3556 + &pre_oid, &our_oid, &post_oid);
3557 if (status < 0) {
3558 fprintf(stderr, "Failed to fall back on three-way merge...\n");
3559 return status;
@@ -3564,9 +3564,9 @@ static int try_threeway(struct apply_state *state,
3564 if (patch->is_new)
3565 oidclr(&patch->threeway_stage[0]);
3566 else
3567 - hashcpy(patch->threeway_stage[0].hash, pre_sha1);
3568 - hashcpy(patch->threeway_stage[1].hash, our_sha1);
3569 - hashcpy(patch->threeway_stage[2].hash, post_sha1);
3567 + oidcpy(&patch->threeway_stage[0], &pre_oid);
3568 + oidcpy(&patch->threeway_stage[1], &our_oid);
3569 + oidcpy(&patch->threeway_stage[2], &post_oid);
3570 fprintf(stderr, "Applied patch to '%s' with conflicts.\n", patch->new_name);
3571 } else {
3572 fprintf(stderr, "Applied patch to '%s' cleanly.\n", patch->new_name);
@@ -3949,8 +3949,8 @@ static int check_patch_list(struct apply_state *state, struct patch *patch)
3949 return err;
3950 }
3951
3952 -/* This function tries to read the sha1 from the current index */
3953 -static int get_current_sha1(const char *path, unsigned char *sha1)
3952 +/* This function tries to read the object ID from the current index */
3953 +static int get_current_oid(const char *path, struct object_id *oid)
3954 {
3955 int pos;
3956
@@ -3959,11 +3959,11 @@ static int get_current_sha1(const char *path, unsigned char *sha1)
3959 pos = cache_name_pos(path, strlen(path));
3960 if (pos < 0)
3961 return -1;
3962 - hashcpy(sha1, active_cache[pos]->oid.hash);
3962 + oidcpy(oid, &active_cache[pos]->oid);
3963 return 0;
3964 }
3965
3966 -static int preimage_sha1_in_gitlink_patch(struct patch *p, unsigned char sha1[20])
3966 +static int preimage_oid_in_gitlink_patch(struct patch *p, struct object_id *oid)
3967 {
3968 /*
3969 * A usable gitlink patch has only one fragment (hunk) that looks like:
@@ -3987,14 +3987,14 @@ static int preimage_sha1_in_gitlink_patch(struct patch *p, unsigned char sha1[20
3987 (preimage = memchr(hunk->patch, '\n', hunk->size)) != NULL &&
3988 starts_with(++preimage, heading) &&
3989 /* does it record full SHA-1? */
3990 - !get_sha1_hex(preimage + sizeof(heading) - 1, sha1) &&
3991 - preimage[sizeof(heading) + 40 - 1] == '\n' &&
3990 + !get_oid_hex(preimage + sizeof(heading) - 1, oid) &&
3991 + preimage[sizeof(heading) + GIT_SHA1_HEXSZ - 1] == '\n' &&
3992 /* does the abbreviated name on the index line agree with it? */
3993 starts_with(preimage + sizeof(heading) - 1, p->old_sha1_prefix))
3994 return 0; /* it all looks fine */
3995
3996 /* we may have full object name on the index line */
3997 - return get_sha1_hex(p->old_sha1_prefix, sha1);
3997 + return get_oid_hex(p->old_sha1_prefix, oid);
3998 }
3999
4000 /* Build an index that contains the just the files needed for a 3way merge */
@@ -4008,7 +4008,7 @@ static void build_fake_ancestor(struct patch *list, const char *filename)
4008 * worth showing the new sha1 prefix, but until then...
4009 */
4010 for (patch = list; patch; patch = patch->next) {
4011 - unsigned char sha1[20];
4011 + struct object_id oid;
4012 struct cache_entry *ce;
4013 const char *name;
4014
@@ -4017,23 +4017,23 @@ static void build_fake_ancestor(struct patch *list, const char *filename)
4017 continue;
4018
4019 if (S_ISGITLINK(patch->old_mode)) {
4020 - if (!preimage_sha1_in_gitlink_patch(patch, sha1))
4020 + if (!preimage_oid_in_gitlink_patch(patch, &oid))
4021 ; /* ok, the textual part looks sane */
4022 else
4023 die("sha1 information is lacking or useless for submodule %s",
4024 name);
4025 - } else if (!get_sha1_blob(patch->old_sha1_prefix, sha1)) {
4025 + } else if (!get_sha1_blob(patch->old_sha1_prefix, oid.hash)) {
4026 ; /* ok */
4027 } else if (!patch->lines_added && !patch->lines_deleted) {
4028 /* mode-only change: update the current */
4029 - if (get_current_sha1(patch->old_name, sha1))
4029 + if (get_current_oid(patch->old_name, &oid))
4030 die("mode change for %s, which is not "
4031 "in current HEAD", name);
4032 } else
4033 die("sha1 information is lacking or useless "
4034 "(%s).", name);
4035
4036 - ce = make_cache_entry(patch->old_mode, sha1, name, 0, 0);
4036 + ce = make_cache_entry(patch->old_mode, oid.hash, name, 0, 0);
4037 if (!ce)
4038 die(_("make_cache_entry failed for path '%s'"), name);
4039 if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD))
@@ -4211,7 +4211,7 @@ static void add_index_file(struct apply_state *state,
4211 const char *s;
4212
4213 if (!skip_prefix(buf, "Subproject commit ", &s) ||
4214 - get_sha1_hex(s, ce->oid.hash))
4214 + get_oid_hex(s, &ce->oid))
4215 die(_("corrupt patch for submodule %s"), path);
4216 } else {
4217 if (!state->cached) {