sha1_file: convert write_sha1_file to object_id

Convert the definition and declaration of write_sha1_file to struct object_id and adjust usage of this function. This commit also converts static function write_sha1_file_prepare, as it is closely related. Rename these functions to write_object_file and write_object_file_prepare respectively. Replace sha1_to_hex, hashcpy and hashclr with their oid equivalents wherever possible. Signed-off-by: Patryk Obara <patryk.obara@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patryk Obara committed Jan 28, 2018 at 01:13 UTC a09c985eae694db1cf31c72db0e2e1bf42f1274f
18 files changed +65 -58
apply.c
+4 -4
@@ -3554,7 +3554,7 @@ static int try_threeway(struct apply_state *state,
3554
3555 /* Preimage the patch was prepared for */
3556 if (patch->is_new)
3557 - write_sha1_file("", 0, blob_type, pre_oid.hash);
3557 + write_object_file("", 0, blob_type, &pre_oid);
3558 else if (get_oid(patch->old_sha1_prefix, &pre_oid) ||
3559 read_blob_object(&buf, &pre_oid, patch->old_mode))
3560 return error(_("repository lacks the necessary blob to fall back on 3-way merge."));
@@ -3570,7 +3570,7 @@ static int try_threeway(struct apply_state *state,
3570 return -1;
3571 }
3572 /* post_oid is theirs */
3573 - write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, post_oid.hash);
3573 + write_object_file(tmp_image.buf, tmp_image.len, blob_type, &post_oid);
3574 clear_image(&tmp_image);
3575
3576 /* our_oid is ours */
@@ -3583,7 +3583,7 @@ static int try_threeway(struct apply_state *state,
3583 return error(_("cannot read the current contents of '%s'"),
3584 patch->old_name);
3585 }
3586 - write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, our_oid.hash);
3586 + write_object_file(tmp_image.buf, tmp_image.len, blob_type, &our_oid);
3587 clear_image(&tmp_image);
3588
3589 /* in-core three-way merge between post and our using pre as base */
@@ -4291,7 +4291,7 @@ static int add_index_file(struct apply_state *state,
4291 }
4292 fill_stat_cache_info(ce, &st);
4293 }
4294 - if (write_sha1_file(buf, size, blob_type, ce->oid.hash) < 0) {
4294 + if (write_object_file(buf, size, blob_type, &ce->oid) < 0) {
4295 free(ce);
4296 return error(_("unable to create backing store "
4297 "for newly created file %s"), path);
builtin/checkout.c
+1 -2
@@ -227,8 +227,7 @@ static int checkout_merged(int pos, const struct checkout *state)
227 * (it also writes the merge result to the object database even
228 * when it may contain conflicts).
229 */
230 - if (write_sha1_file(result_buf.ptr, result_buf.size,
231 - blob_type, oid.hash))
230 + if (write_object_file(result_buf.ptr, result_buf.size, blob_type, &oid))
231 die(_("Unable to add merge result for '%s'"), path);
232 free(result_buf.ptr);
233 ce = make_cache_entry(mode, oid.hash, path, 2, 0);
builtin/mktag.c
+3 -3
@@ -151,7 +151,7 @@ static int verify_tag(char *buffer, unsigned long size)
151 int cmd_mktag(int argc, const char **argv, const char *prefix)
152 {
153 struct strbuf buf = STRBUF_INIT;
154 - unsigned char result_sha1[20];
154 + struct object_id result;
155
156 if (argc != 1)
157 usage("git mktag");
@@ -165,10 +165,10 @@ int cmd_mktag(int argc, const char **argv, const char *prefix)
165 if (verify_tag(buf.buf, buf.len) < 0)
166 die("invalid tag signature file");
167
168 - if (write_sha1_file(buf.buf, buf.len, tag_type, result_sha1) < 0)
168 + if (write_object_file(buf.buf, buf.len, tag_type, &result) < 0)
169 die("unable to write tag file");
170
171 strbuf_release(&buf);
172 - printf("%s\n", sha1_to_hex(result_sha1));
172 + printf("%s\n", oid_to_hex(&result));
173 return 0;
174 }
builtin/mktree.c
+5 -5
@@ -40,7 +40,7 @@ static int ent_compare(const void *a_, const void *b_)
40 b->name, b->len, b->mode);
41 }
42
43 -static void write_tree(unsigned char *sha1)
43 +static void write_tree(struct object_id *oid)
44 {
45 struct strbuf buf;
46 size_t size;
@@ -57,7 +57,7 @@ static void write_tree(unsigned char *sha1)
57 strbuf_add(&buf, ent->sha1, 20);
58 }
59
60 - write_sha1_file(buf.buf, buf.len, tree_type, sha1);
60 + write_object_file(buf.buf, buf.len, tree_type, oid);
61 strbuf_release(&buf);
62 }
63
@@ -142,7 +142,7 @@ static void mktree_line(char *buf, size_t len, int nul_term_line, int allow_miss
142 int cmd_mktree(int ac, const char **av, const char *prefix)
143 {
144 struct strbuf sb = STRBUF_INIT;
145 - unsigned char sha1[20];
145 + struct object_id oid;
146 int nul_term_line = 0;
147 int allow_missing = 0;
148 int is_batch_mode = 0;
@@ -181,8 +181,8 @@ int cmd_mktree(int ac, const char **av, const char *prefix)
181 */
182 ; /* skip creating an empty tree */
183 } else {
184 - write_tree(sha1);
185 - puts(sha1_to_hex(sha1));
184 + write_tree(&oid);
185 + puts(oid_to_hex(&oid));
186 fflush(stdout);
187 }
188 used=0; /* reset tree entry buffer for re-use in batch mode */
builtin/notes.c
+4 -4
@@ -198,9 +198,9 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
198 }
199 }
200
201 -static void write_note_data(struct note_data *d, unsigned char *sha1)
201 +static void write_note_data(struct note_data *d, struct object_id *oid)
202 {
203 - if (write_sha1_file(d->buf.buf, d->buf.len, blob_type, sha1)) {
203 + if (write_object_file(d->buf.buf, d->buf.len, blob_type, oid)) {
204 error(_("unable to write note object"));
205 if (d->edit_path)
206 error(_("the note contents have been left in %s"),
@@ -459,7 +459,7 @@ static int add(int argc, const char **argv, const char *prefix)
459
460 prepare_note_data(&object, &d, note ? note->hash : NULL);
461 if (d.buf.len || allow_empty) {
462 - write_note_data(&d, new_note.hash);
462 + write_note_data(&d, &new_note);
463 if (add_note(t, &object, &new_note, combine_notes_overwrite))
464 die("BUG: combine_notes_overwrite failed");
465 commit_notes(t, "Notes added by 'git notes add'");
@@ -619,7 +619,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
619 }
620
621 if (d.buf.len || allow_empty) {
622 - write_note_data(&d, new_note.hash);
622 + write_note_data(&d, &new_note);
623 if (add_note(t, &object, &new_note, combine_notes_overwrite))
624 die("BUG: combine_notes_overwrite failed");
625 logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]);
builtin/receive-pack.c
+6 -5
@@ -69,7 +69,7 @@ static int sent_capabilities;
69 static int shallow_update;
70 static const char *alt_shallow_file;
71 static struct strbuf push_cert = STRBUF_INIT;
72 -static unsigned char push_cert_sha1[20];
72 +static struct object_id push_cert_oid;
73 static struct signature_check sigcheck;
74 static const char *push_cert_nonce;
75 static const char *cert_nonce_seed;
@@ -633,8 +633,9 @@ static void prepare_push_cert_sha1(struct child_process *proc)
633 int bogs /* beginning_of_gpg_sig */;
634
635 already_done = 1;
636 - if (write_sha1_file(push_cert.buf, push_cert.len, "blob", push_cert_sha1))
637 - hashclr(push_cert_sha1);
636 + if (write_object_file(push_cert.buf, push_cert.len, "blob",
637 + &push_cert_oid))
638 + oidclr(&push_cert_oid);
639
640 memset(&sigcheck, '\0', sizeof(sigcheck));
641 sigcheck.result = 'N';
@@ -655,9 +656,9 @@ static void prepare_push_cert_sha1(struct child_process *proc)
656 strbuf_release(&gpg_status);
657 nonce_status = check_nonce(push_cert.buf, bogs);
658 }
658 - if (!is_null_sha1(push_cert_sha1)) {
659 + if (!is_null_oid(&push_cert_oid)) {
660 argv_array_pushf(&proc->env_array, "GIT_PUSH_CERT=%s",
660 - sha1_to_hex(push_cert_sha1));
661 + oid_to_hex(&push_cert_oid));
662 argv_array_pushf(&proc->env_array, "GIT_PUSH_CERT_SIGNER=%s",
663 sigcheck.signer ? sigcheck.signer : "");
664 argv_array_pushf(&proc->env_array, "GIT_PUSH_CERT_KEY=%s",
builtin/replace.c
+1 -1
@@ -410,7 +410,7 @@ static int create_graft(int argc, const char **argv, int force)
410
411 check_mergetags(commit, argc, argv);
412
413 - if (write_sha1_file(buf.buf, buf.len, commit_type, new.hash))
413 + if (write_object_file(buf.buf, buf.len, commit_type, &new))
414 die(_("could not write replacement commit for: '%s'"), old_ref);
415
416 strbuf_release(&buf);
builtin/tag.c
+1 -1
@@ -187,7 +187,7 @@ static int build_tag_object(struct strbuf *buf, int sign, struct object_id *resu
187 {
188 if (sign && do_sign(buf) < 0)
189 return error(_("unable to sign the tag"));
190 - if (write_sha1_file(buf->buf, buf->len, tag_type, result->hash) < 0)
190 + if (write_object_file(buf->buf, buf->len, tag_type, result) < 0)
191 return error(_("unable to write tag file"));
192 return 0;
193 }
builtin/unpack-objects.c
+6 -3
@@ -172,7 +172,8 @@ static void write_cached_object(struct object *obj, struct obj_buffer *obj_buf)
172 {
173 struct object_id oid;
174
175 - if (write_sha1_file(obj_buf->buffer, obj_buf->size, typename(obj->type), oid.hash) < 0)
175 + if (write_object_file(obj_buf->buffer, obj_buf->size,
176 + typename(obj->type), &oid) < 0)
177 die("failed to write object %s", oid_to_hex(&obj->oid));
178 obj->flags |= FLAG_WRITTEN;
179 }
@@ -237,14 +238,16 @@ static void write_object(unsigned nr, enum object_type type,
238 void *buf, unsigned long size)
239 {
240 if (!strict) {
240 - if (write_sha1_file(buf, size, typename(type), obj_list[nr].oid.hash) < 0)
241 + if (write_object_file(buf, size, typename(type),
242 + &obj_list[nr].oid) < 0)
243 die("failed to write object");
244 added_object(nr, type, buf, size);
245 free(buf);
246 obj_list[nr].obj = NULL;
247 } else if (type == OBJ_BLOB) {
248 struct blob *blob;
247 - if (write_sha1_file(buf, size, typename(type), obj_list[nr].oid.hash) < 0)
249 + if (write_object_file(buf, size, typename(type),
250 + &obj_list[nr].oid) < 0)
251 die("failed to write object");
252 added_object(nr, type, buf, size);
253 free(buf);
cache-tree.c
+3 -2
@@ -406,9 +406,10 @@ static int update_one(struct cache_tree *it,
406 oidcpy(&it->oid, &oid);
407 else
408 to_invalidate = 1;
409 - } else if (dryrun)
409 + } else if (dryrun) {
410 hash_object_file(buffer.buf, buffer.len, tree_type, &it->oid);
411 - else if (write_sha1_file(buffer.buf, buffer.len, tree_type, it->oid.hash)) {
411 + } else if (write_object_file(buffer.buf, buffer.len, tree_type,
412 + &it->oid)) {
413 strbuf_release(&buffer);
414 return -1;
415 }
cache.h
+3 -1
@@ -1240,7 +1240,9 @@ extern int sha1_object_info(const unsigned char *, unsigned long *);
1240 extern int hash_object_file(const void *buf, unsigned long len,
1241 const char *type, struct object_id *oid);
1242
1243 -extern int write_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *return_sha1);
1243 +extern int write_object_file(const void *buf, unsigned long len,
1244 + const char *type, struct object_id *oid);
1245 +
1246 extern int hash_sha1_file_literally(const void *buf, unsigned long len, const char *type, struct object_id *oid, unsigned flags);
1247
1248 extern int pretend_object_file(void *, unsigned long, enum object_type,
commit.c
+1 -1
@@ -1567,7 +1567,7 @@ int commit_tree_extended(const char *msg, size_t msg_len,
1567 goto out;
1568 }
1569
1570 - result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret->hash);
1570 + result = write_object_file(buffer.buf, buffer.len, commit_type, ret);
1571 out:
1572 strbuf_release(&buffer);
1573 return result;
match-trees.c
+1 -1
@@ -214,7 +214,7 @@ static int splice_tree(const struct object_id *oid1, const char *prefix,
214 rewrite_with = oid2;
215 }
216 oidcpy(rewrite_here, rewrite_with);
217 - status = write_sha1_file(buf, sz, tree_type, result->hash);
217 + status = write_object_file(buf, sz, tree_type, result);
218 free(buf);
219 return status;
220 }
merge-recursive.c
+3 -2
@@ -1009,8 +1009,9 @@ static int merge_file_1(struct merge_options *o,
1009 if ((merge_status < 0) || !result_buf.ptr)
1010 ret = err(o, _("Failed to execute internal merge"));
1011
1012 - if (!ret && write_sha1_file(result_buf.ptr, result_buf.size,
1013 - blob_type, result->oid.hash))
1012 + if (!ret &&
1013 + write_object_file(result_buf.ptr, result_buf.size,
1014 + blob_type, &result->oid))
1015 ret = err(o, _("Unable to add %s to database"),
1016 a->path);
1017
notes-cache.c
+1 -1
@@ -88,7 +88,7 @@ int notes_cache_put(struct notes_cache *c, struct object_id *key_oid,
88 {
89 struct object_id value_oid;
90
91 - if (write_sha1_file(data, size, "blob", value_oid.hash) < 0)
91 + if (write_object_file(data, size, "blob", &value_oid) < 0)
92 return -1;
93 return add_note(&c->tree, key_oid, &value_oid, NULL);
94 }
notes.c
+4 -5
@@ -667,7 +667,7 @@ static int tree_write_stack_finish_subtree(struct tree_write_stack *tws)
667 ret = tree_write_stack_finish_subtree(n);
668 if (ret)
669 return ret;
670 - ret = write_sha1_file(n->buf.buf, n->buf.len, tree_type, s.hash);
670 + ret = write_object_file(n->buf.buf, n->buf.len, tree_type, &s);
671 if (ret)
672 return ret;
673 strbuf_release(&n->buf);
@@ -825,7 +825,7 @@ int combine_notes_concatenate(struct object_id *cur_oid,
825 free(new_msg);
826
827 /* create a new blob object from buf */
828 - ret = write_sha1_file(buf, buf_len, blob_type, cur_oid->hash);
828 + ret = write_object_file(buf, buf_len, blob_type, cur_oid);
829 free(buf);
830 return ret;
831 }
@@ -905,7 +905,7 @@ int combine_notes_cat_sort_uniq(struct object_id *cur_oid,
905 string_list_join_lines_helper, &buf))
906 goto out;
907
908 - ret = write_sha1_file(buf.buf, buf.len, blob_type, cur_oid->hash);
908 + ret = write_object_file(buf.buf, buf.len, blob_type, cur_oid);
909
910 out:
911 strbuf_release(&buf);
@@ -1147,8 +1147,7 @@ int write_notes_tree(struct notes_tree *t, struct object_id *result)
1147 ret = for_each_note(t, flags, write_each_note, &cb_data) ||
1148 write_each_non_note_until(NULL, &cb_data) ||
1149 tree_write_stack_finish_subtree(&root) ||
1150 - write_sha1_file(root.buf.buf, root.buf.len, tree_type,
1151 - result->hash);
1150 + write_object_file(root.buf.buf, root.buf.len, tree_type, result);
1151 strbuf_release(&root.buf);
1152 return ret;
1153 }
read-cache.c
+3 -3
@@ -631,10 +631,10 @@ static struct cache_entry *create_alias_ce(struct index_state *istate,
631
632 void set_object_name_for_intent_to_add_entry(struct cache_entry *ce)
633 {
634 - unsigned char sha1[20];
635 - if (write_sha1_file("", 0, blob_type, sha1))
634 + struct object_id oid;
635 + if (write_object_file("", 0, blob_type, &oid))
636 die("cannot create an empty blob in the object database");
637 - hashcpy(ce->oid.hash, sha1);
637 + oidcpy(&ce->oid, &oid);
638 }
639
640 int add_to_index(struct index_state *istate, const char *path, struct stat *st, int flags)
sha1_file.c
+15 -14
@@ -1419,9 +1419,9 @@ void *read_object_with_reference(const unsigned char *sha1,
1419 }
1420 }
1421
1422 -static void write_sha1_file_prepare(const void *buf, unsigned long len,
1423 - const char *type, unsigned char *sha1,
1424 - char *hdr, int *hdrlen)
1422 +static void write_object_file_prepare(const void *buf, unsigned long len,
1423 + const char *type, struct object_id *oid,
1424 + char *hdr, int *hdrlen)
1425 {
1426 git_SHA_CTX c;
1427
@@ -1432,7 +1432,7 @@ static void write_sha1_file_prepare(const void *buf, unsigned long len,
1432 git_SHA1_Init(&c);
1433 git_SHA1_Update(&c, hdr, *hdrlen);
1434 git_SHA1_Update(&c, buf, len);
1435 - git_SHA1_Final(sha1, &c);
1435 + git_SHA1_Final(oid->hash, &c);
1436 }
1437
1438 /*
@@ -1490,7 +1490,7 @@ int hash_object_file(const void *buf, unsigned long len, const char *type,
1490 {
1491 char hdr[32];
1492 int hdrlen = sizeof(hdr);
1493 - write_sha1_file_prepare(buf, len, type, oid->hash, hdr, &hdrlen);
1493 + write_object_file_prepare(buf, len, type, oid, hdr, &hdrlen);
1494 return 0;
1495 }
1496
@@ -1633,7 +1633,8 @@ static int freshen_packed_object(const unsigned char *sha1)
1633 return 1;
1634 }
1635
1636 -int write_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *sha1)
1636 +int write_object_file(const void *buf, unsigned long len, const char *type,
1637 + struct object_id *oid)
1638 {
1639 char hdr[32];
1640 int hdrlen = sizeof(hdr);
@@ -1641,10 +1642,10 @@ int write_sha1_file(const void *buf, unsigned long len, const char *type, unsign
1642 /* Normally if we have it in the pack then we do not bother writing
1643 * it out into .git/objects/??/?{38} file.
1644 */
1644 - write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
1645 - if (freshen_packed_object(sha1) || freshen_loose_object(sha1))
1645 + write_object_file_prepare(buf, len, type, oid, hdr, &hdrlen);
1646 + if (freshen_packed_object(oid->hash) || freshen_loose_object(oid->hash))
1647 return 0;
1647 - return write_loose_object(sha1, hdr, hdrlen, buf, len, 0);
1648 + return write_loose_object(oid->hash, hdr, hdrlen, buf, len, 0);
1649 }
1650
1651 int hash_sha1_file_literally(const void *buf, unsigned long len, const char *type,
@@ -1656,7 +1657,7 @@ int hash_sha1_file_literally(const void *buf, unsigned long len, const char *typ
1657 /* type string, SP, %lu of the length plus NUL must fit this */
1658 hdrlen = strlen(type) + 32;
1659 header = xmalloc(hdrlen);
1659 - write_sha1_file_prepare(buf, len, type, oid->hash, header, &hdrlen);
1660 + write_object_file_prepare(buf, len, type, oid, header, &hdrlen);
1661
1662 if (!(flags & HASH_WRITE_OBJECT))
1663 goto cleanup;
@@ -1767,7 +1768,7 @@ static int index_mem(struct object_id *oid, void *buf, size_t size,
1768 }
1769
1770 if (write_object)
1770 - ret = write_sha1_file(buf, size, typename(type), oid->hash);
1771 + ret = write_object_file(buf, size, typename(type), oid);
1772 else
1773 ret = hash_object_file(buf, size, typename(type), oid);
1774 if (re_allocated)
@@ -1789,8 +1790,8 @@ static int index_stream_convert_blob(struct object_id *oid, int fd,
1790 get_safe_crlf(flags));
1791
1792 if (write_object)
1792 - ret = write_sha1_file(sbuf.buf, sbuf.len, typename(OBJ_BLOB),
1793 - oid->hash);
1793 + ret = write_object_file(sbuf.buf, sbuf.len, typename(OBJ_BLOB),
1794 + oid);
1795 else
1796 ret = hash_object_file(sbuf.buf, sbuf.len, typename(OBJ_BLOB),
1797 oid);
@@ -1908,7 +1909,7 @@ int index_path(struct object_id *oid, const char *path, struct stat *st, unsigne
1909 return error_errno("readlink(\"%s\")", path);
1910 if (!(flags & HASH_WRITE_OBJECT))
1911 hash_object_file(sb.buf, sb.len, blob_type, oid);
1911 - else if (write_sha1_file(sb.buf, sb.len, blob_type, oid->hash))
1912 + else if (write_object_file(sb.buf, sb.len, blob_type, oid))
1913 rc = error("%s: failed to insert into database", path);
1914 strbuf_release(&sb);
1915 break;