fast-import: convert to struct object_id

Convert the remaining parts of fast-import.c to use struct object_id. Convert several instances of get_sha1_hex to parse_oid_hex to avoid needing to specify constants. Convert other hardcoded values to named constants. Finally, use the is_empty_tree_oid function instead of a direct comparison against a fixed string. Note that the odd computation with GIT_MAX_HEXSZ is due to the insertion of a slash between every two hex digits in the path, plus one for the terminating NUL. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed May 6, 2017 at 22:09 UTC 912c13d58faf355589a3f67fd55b2015561e0184
1 file changed +159 -158
fast-import.c
+159 -158
@@ -557,7 +557,7 @@ static void alloc_objects(unsigned int cnt)
557 alloc_count += cnt;
558 }
559
560 -static struct object_entry *new_object(unsigned char *sha1)
560 +static struct object_entry *new_object(struct object_id *oid)
561 {
562 struct object_entry *e;
563
@@ -565,32 +565,32 @@ static struct object_entry *new_object(unsigned char *sha1)
565 alloc_objects(object_entry_alloc);
566
567 e = blocks->next_free++;
568 - hashcpy(e->idx.sha1, sha1);
568 + hashcpy(e->idx.sha1, oid->hash);
569 return e;
570 }
571
572 -static struct object_entry *find_object(unsigned char *sha1)
572 +static struct object_entry *find_object(struct object_id *oid)
573 {
574 - unsigned int h = sha1[0] << 8 | sha1[1];
574 + unsigned int h = oid->hash[0] << 8 | oid->hash[1];
575 struct object_entry *e;
576 for (e = object_table[h]; e; e = e->next)
577 - if (!hashcmp(sha1, e->idx.sha1))
577 + if (!hashcmp(oid->hash, e->idx.sha1))
578 return e;
579 return NULL;
580 }
581
582 -static struct object_entry *insert_object(unsigned char *sha1)
582 +static struct object_entry *insert_object(struct object_id *oid)
583 {
584 - unsigned int h = sha1[0] << 8 | sha1[1];
584 + unsigned int h = oid->hash[0] << 8 | oid->hash[1];
585 struct object_entry *e = object_table[h];
586
587 while (e) {
588 - if (!hashcmp(sha1, e->idx.sha1))
588 + if (!hashcmp(oid->hash, e->idx.sha1))
589 return e;
590 e = e->next;
591 }
592
593 - e = new_object(sha1);
593 + e = new_object(oid);
594 e->next = object_table[h];
595 e->idx.offset = 0;
596 object_table[h] = e;
@@ -1007,17 +1007,17 @@ static void end_packfile(void)
1007 clear_delta_base_cache();
1008 if (object_count) {
1009 struct packed_git *new_p;
1010 - unsigned char cur_pack_sha1[20];
1010 + struct object_id cur_pack_oid;
1011 char *idx_name;
1012 int i;
1013 struct branch *b;
1014 struct tag *t;
1015
1016 close_pack_windows(pack_data);
1017 - sha1close(pack_file, cur_pack_sha1, 0);
1017 + sha1close(pack_file, cur_pack_oid.hash, 0);
1018 fixup_pack_header_footer(pack_data->pack_fd, pack_data->sha1,
1019 pack_data->pack_name, object_count,
1020 - cur_pack_sha1, pack_size);
1020 + cur_pack_oid.hash, pack_size);
1021
1022 if (object_count <= unpack_limit) {
1023 if (!loosen_small_pack(pack_data)) {
@@ -1083,13 +1083,13 @@ static int store_object(
1083 enum object_type type,
1084 struct strbuf *dat,
1085 struct last_object *last,
1086 - unsigned char *sha1out,
1086 + struct object_id *oidout,
1087 uintmax_t mark)
1088 {
1089 void *out, *delta;
1090 struct object_entry *e;
1091 unsigned char hdr[96];
1092 - unsigned char sha1[20];
1092 + struct object_id oid;
1093 unsigned long hdrlen, deltalen;
1094 git_SHA_CTX c;
1095 git_zstream s;
@@ -1099,17 +1099,17 @@ static int store_object(
1099 git_SHA1_Init(&c);
1100 git_SHA1_Update(&c, hdr, hdrlen);
1101 git_SHA1_Update(&c, dat->buf, dat->len);
1102 - git_SHA1_Final(sha1, &c);
1103 - if (sha1out)
1104 - hashcpy(sha1out, sha1);
1102 + git_SHA1_Final(oid.hash, &c);
1103 + if (oidout)
1104 + oidcpy(oidout, &oid);
1105
1106 - e = insert_object(sha1);
1106 + e = insert_object(&oid);
1107 if (mark)
1108 insert_mark(mark, e);
1109 if (e->idx.offset) {
1110 duplicate_count_by_type[type]++;
1111 return 1;
1112 - } else if (find_sha1_pack(sha1, packed_git)) {
1112 + } else if (find_sha1_pack(oid.hash, packed_git)) {
1113 e->type = type;
1114 e->pack_id = MAX_PACK_ID;
1115 e->idx.offset = 1; /* just not zero! */
@@ -1222,13 +1222,13 @@ static void truncate_pack(struct sha1file_checkpoint *checkpoint)
1222 pack_size = checkpoint->offset;
1223 }
1224
1225 -static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
1225 +static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark)
1226 {
1227 size_t in_sz = 64 * 1024, out_sz = 64 * 1024;
1228 unsigned char *in_buf = xmalloc(in_sz);
1229 unsigned char *out_buf = xmalloc(out_sz);
1230 struct object_entry *e;
1231 - unsigned char sha1[20];
1231 + struct object_id oid;
1232 unsigned long hdrlen;
1233 off_t offset;
1234 git_SHA_CTX c;
@@ -1291,12 +1291,12 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
1291 }
1292 }
1293 git_deflate_end(&s);
1294 - git_SHA1_Final(sha1, &c);
1294 + git_SHA1_Final(oid.hash, &c);
1295
1296 - if (sha1out)
1297 - hashcpy(sha1out, sha1);
1296 + if (oidout)
1297 + oidcpy(oidout, &oid);
1298
1299 - e = insert_object(sha1);
1299 + e = insert_object(&oid);
1300
1301 if (mark)
1302 insert_mark(mark, e);
@@ -1305,7 +1305,7 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
1305 duplicate_count_by_type[OBJ_BLOB]++;
1306 truncate_pack(&checkpoint);
1307
1308 - } else if (find_sha1_pack(sha1, packed_git)) {
1308 + } else if (find_sha1_pack(oid.hash, packed_git)) {
1309 e->type = OBJ_BLOB;
1310 e->pack_id = MAX_PACK_ID;
1311 e->idx.offset = 1; /* just not zero! */
@@ -1389,7 +1389,7 @@ static const char *get_mode(const char *str, uint16_t *modep)
1389
1390 static void load_tree(struct tree_entry *root)
1391 {
1392 - unsigned char *sha1 = root->versions[1].oid.hash;
1392 + struct object_id *oid = &root->versions[1].oid;
1393 struct object_entry *myoe;
1394 struct tree_content *t;
1395 unsigned long size;
@@ -1397,22 +1397,22 @@ static void load_tree(struct tree_entry *root)
1397 const char *c;
1398
1399 root->tree = t = new_tree_content(8);
1400 - if (is_null_sha1(sha1))
1400 + if (is_null_oid(oid))
1401 return;
1402
1403 - myoe = find_object(sha1);
1403 + myoe = find_object(oid);
1404 if (myoe && myoe->pack_id != MAX_PACK_ID) {
1405 if (myoe->type != OBJ_TREE)
1406 - die("Not a tree: %s", sha1_to_hex(sha1));
1406 + die("Not a tree: %s", oid_to_hex(oid));
1407 t->delta_depth = myoe->depth;
1408 buf = gfi_unpack_entry(myoe, &size);
1409 if (!buf)
1410 - die("Can't load tree %s", sha1_to_hex(sha1));
1410 + die("Can't load tree %s", oid_to_hex(oid));
1411 } else {
1412 enum object_type type;
1413 - buf = read_sha1_file(sha1, &type, &size);
1413 + buf = read_sha1_file(oid->hash, &type, &size);
1414 if (!buf || type != OBJ_TREE)
1415 - die("Can't load tree %s", sha1_to_hex(sha1));
1415 + die("Can't load tree %s", oid_to_hex(oid));
1416 }
1417
1418 c = buf;
@@ -1426,13 +1426,13 @@ static void load_tree(struct tree_entry *root)
1426 e->tree = NULL;
1427 c = get_mode(c, &e->versions[1].mode);
1428 if (!c)
1429 - die("Corrupt mode in %s", sha1_to_hex(sha1));
1429 + die("Corrupt mode in %s", oid_to_hex(oid));
1430 e->versions[0].mode = e->versions[1].mode;
1431 e->name = to_atom(c, strlen(c));
1432 c += e->name->str_len + 1;
1433 hashcpy(e->versions[0].oid.hash, (unsigned char *)c);
1434 hashcpy(e->versions[1].oid.hash, (unsigned char *)c);
1435 - c += 20;
1435 + c += GIT_SHA1_RAWSZ;
1436 }
1437 free(buf);
1438 }
@@ -1479,7 +1479,7 @@ static void mktree(struct tree_content *t, int v, struct strbuf *b)
1479 strbuf_addf(b, "%o %s%c",
1480 (unsigned int)(e->versions[v].mode & ~NO_DELTA),
1481 e->name->str_dat, '\0');
1482 - strbuf_add(b, e->versions[v].oid.hash, 20);
1482 + strbuf_add(b, e->versions[v].oid.hash, GIT_SHA1_RAWSZ);
1483 }
1484 }
1485
@@ -1503,7 +1503,7 @@ static void store_tree(struct tree_entry *root)
1503 }
1504
1505 if (!(root->versions[0].mode & NO_DELTA))
1506 - le = find_object(root->versions[0].oid.hash);
1506 + le = find_object(&root->versions[0].oid);
1507 if (S_ISDIR(root->versions[0].mode) && le && le->pack_id == pack_id) {
1508 mktree(t, 0, &old_tree);
1509 lo.data = old_tree;
@@ -1512,7 +1512,7 @@ static void store_tree(struct tree_entry *root)
1512 }
1513
1514 mktree(t, 1, &new_tree);
1515 - store_object(OBJ_TREE, &new_tree, &lo, root->versions[1].oid.hash, 0);
1515 + store_object(OBJ_TREE, &new_tree, &lo, &root->versions[1].oid, 0);
1516
1517 t->delta_depth = lo.depth;
1518 for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
@@ -1531,14 +1531,14 @@ static void store_tree(struct tree_entry *root)
1531
1532 static void tree_content_replace(
1533 struct tree_entry *root,
1534 - const unsigned char *sha1,
1534 + const struct object_id *oid,
1535 const uint16_t mode,
1536 struct tree_content *newtree)
1537 {
1538 if (!S_ISDIR(mode))
1539 die("Root cannot be a non-directory");
1540 oidclr(&root->versions[0].oid);
1541 - hashcpy(root->versions[1].oid.hash, sha1);
1541 + oidcpy(&root->versions[1].oid, oid);
1542 if (root->tree)
1543 release_tree_content_recursive(root->tree);
1544 root->tree = newtree;
@@ -1547,7 +1547,7 @@ static void tree_content_replace(
1547 static int tree_content_set(
1548 struct tree_entry *root,
1549 const char *p,
1550 - const unsigned char *sha1,
1550 + const struct object_id *oid,
1551 const uint16_t mode,
1552 struct tree_content *subtree)
1553 {
@@ -1572,10 +1572,10 @@ static int tree_content_set(
1572 if (!*slash1) {
1573 if (!S_ISDIR(mode)
1574 && e->versions[1].mode == mode
1575 - && !hashcmp(e->versions[1].oid.hash, sha1))
1575 + && !oidcmp(&e->versions[1].oid, oid))
1576 return 0;
1577 e->versions[1].mode = mode;
1578 - hashcpy(e->versions[1].oid.hash, sha1);
1578 + oidcpy(&e->versions[1].oid, oid);
1579 if (e->tree)
1580 release_tree_content_recursive(e->tree);
1581 e->tree = subtree;
@@ -1605,7 +1605,7 @@ static int tree_content_set(
1605 }
1606 if (!e->tree)
1607 load_tree(e);
1608 - if (tree_content_set(e, slash1 + 1, sha1, mode, subtree)) {
1608 + if (tree_content_set(e, slash1 + 1, oid, mode, subtree)) {
1609 oidclr(&root->versions[1].oid);
1610 return 1;
1611 }
@@ -1623,11 +1623,11 @@ static int tree_content_set(
1623 if (*slash1) {
1624 e->tree = new_tree_content(8);
1625 e->versions[1].mode = S_IFDIR;
1626 - tree_content_set(e, slash1 + 1, sha1, mode, subtree);
1626 + tree_content_set(e, slash1 + 1, oid, mode, subtree);
1627 } else {
1628 e->tree = subtree;
1629 e->versions[1].mode = mode;
1630 - hashcpy(e->versions[1].oid.hash, sha1);
1630 + oidcpy(&e->versions[1].oid, oid);
1631 }
1632 oidclr(&root->versions[1].oid);
1633 return 1;
@@ -1750,7 +1750,7 @@ static int update_branch(struct branch *b)
1750 {
1751 static const char *msg = "fast-import";
1752 struct ref_transaction *transaction;
1753 - unsigned char old_sha1[20];
1753 + struct object_id old_oid;
1754 struct strbuf err = STRBUF_INIT;
1755
1756 if (is_null_oid(&b->oid)) {
@@ -1758,12 +1758,12 @@ static int update_branch(struct branch *b)
1758 delete_ref(NULL, b->name, NULL, 0);
1759 return 0;
1760 }
1761 - if (read_ref(b->name, old_sha1))
1762 - hashclr(old_sha1);
1763 - if (!force_update && !is_null_sha1(old_sha1)) {
1761 + if (read_ref(b->name, old_oid.hash))
1762 + oidclr(&old_oid);
1763 + if (!force_update && !is_null_oid(&old_oid)) {
1764 struct commit *old_cmit, *new_cmit;
1765
1766 - old_cmit = lookup_commit_reference_gently(old_sha1, 0);
1766 + old_cmit = lookup_commit_reference_gently(old_oid.hash, 0);
1767 new_cmit = lookup_commit_reference_gently(b->oid.hash, 0);
1768 if (!old_cmit || !new_cmit)
1769 return error("Branch %s is missing commits.", b->name);
@@ -1772,13 +1772,13 @@ static int update_branch(struct branch *b)
1772 warning("Not updating %s"
1773 " (new tip %s does not contain %s)",
1774 b->name, oid_to_hex(&b->oid),
1775 - sha1_to_hex(old_sha1));
1775 + oid_to_hex(&old_oid));
1776 return -1;
1777 }
1778 }
1779 transaction = ref_transaction_begin(&err);
1780 if (!transaction ||
1781 - ref_transaction_update(transaction, b->name, b->oid.hash, old_sha1,
1781 + ref_transaction_update(transaction, b->name, b->oid.hash, old_oid.hash,
1782 0, msg, &err) ||
1783 ref_transaction_commit(transaction, &err)) {
1784 ref_transaction_free(transaction);
@@ -1898,7 +1898,7 @@ static void read_marks(void)
1898 while (fgets(line, sizeof(line), f)) {
1899 uintmax_t mark;
1900 char *end;
1901 - unsigned char sha1[20];
1901 + struct object_id oid;
1902 struct object_entry *e;
1903
1904 end = strchr(line, '\n');
@@ -1907,14 +1907,14 @@ static void read_marks(void)
1907 *end = 0;
1908 mark = strtoumax(line + 1, &end, 10);
1909 if (!mark || end == line + 1
1910 - || *end != ' ' || get_sha1_hex(end + 1, sha1))
1910 + || *end != ' ' || get_oid_hex(end + 1, &oid))
1911 die("corrupt mark line: %s", line);
1912 - e = find_object(sha1);
1912 + e = find_object(&oid);
1913 if (!e) {
1914 - enum object_type type = sha1_object_info(sha1, NULL);
1914 + enum object_type type = sha1_object_info(oid.hash, NULL);
1915 if (type < 0)
1916 - die("object not found: %s", sha1_to_hex(sha1));
1917 - e = insert_object(sha1);
1916 + die("object not found: %s", oid_to_hex(&oid));
1917 + e = insert_object(&oid);
1918 e->type = type;
1919 e->pack_id = MAX_PACK_ID;
1920 e->idx.offset = 1; /* just not zero! */
@@ -2122,21 +2122,21 @@ static char *parse_ident(const char *buf)
2122
2123 static void parse_and_store_blob(
2124 struct last_object *last,
2125 - unsigned char *sha1out,
2125 + struct object_id *oidout,
2126 uintmax_t mark)
2127 {
2128 static struct strbuf buf = STRBUF_INIT;
2129 uintmax_t len;
2130
2131 if (parse_data(&buf, big_file_threshold, &len))
2132 - store_object(OBJ_BLOB, &buf, last, sha1out, mark);
2132 + store_object(OBJ_BLOB, &buf, last, oidout, mark);
2133 else {
2134 if (last) {
2135 strbuf_release(&last->data);
2136 last->offset = 0;
2137 last->depth = 0;
2138 }
2139 - stream_blob(len, sha1out, mark);
2139 + stream_blob(len, oidout, mark);
2140 skip_optional_lf();
2141 }
2142 }
@@ -2212,21 +2212,21 @@ static void construct_path_with_fanout(const char *hex_sha1,
2212 path[i++] = '/';
2213 fanout--;
2214 }
2215 - memcpy(path + i, hex_sha1 + j, 40 - j);
2216 - path[i + 40 - j] = '\0';
2215 + memcpy(path + i, hex_sha1 + j, GIT_SHA1_HEXSZ - j);
2216 + path[i + GIT_SHA1_HEXSZ - j] = '\0';
2217 }
2218
2219 static uintmax_t do_change_note_fanout(
2220 struct tree_entry *orig_root, struct tree_entry *root,
2221 - char *hex_sha1, unsigned int hex_sha1_len,
2221 + char *hex_oid, unsigned int hex_oid_len,
2222 char *fullpath, unsigned int fullpath_len,
2223 unsigned char fanout)
2224 {
2225 struct tree_content *t;
2226 struct tree_entry *e, leaf;
2227 - unsigned int i, tmp_hex_sha1_len, tmp_fullpath_len;
2227 + unsigned int i, tmp_hex_oid_len, tmp_fullpath_len;
2228 uintmax_t num_notes = 0;
2229 - unsigned char sha1[20];
2229 + struct object_id oid;
2230 char realpath[60];
2231
2232 if (!root->tree)
@@ -2235,7 +2235,7 @@ static uintmax_t do_change_note_fanout(
2235
2236 for (i = 0; t && i < t->entry_count; i++) {
2237 e = t->entries[i];
2238 - tmp_hex_sha1_len = hex_sha1_len + e->name->str_len;
2238 + tmp_hex_oid_len = hex_oid_len + e->name->str_len;
2239 tmp_fullpath_len = fullpath_len;
2240
2241 /*
@@ -2247,12 +2247,12 @@ static uintmax_t do_change_note_fanout(
2247 * of 2 chars.
2248 */
2249 if (!e->versions[1].mode ||
2250 - tmp_hex_sha1_len > 40 ||
2250 + tmp_hex_oid_len > GIT_SHA1_HEXSZ ||
2251 e->name->str_len % 2)
2252 continue;
2253
2254 /* This _may_ be a note entry, or a subdir containing notes */
2255 - memcpy(hex_sha1 + hex_sha1_len, e->name->str_dat,
2255 + memcpy(hex_oid + hex_oid_len, e->name->str_dat,
2256 e->name->str_len);
2257 if (tmp_fullpath_len)
2258 fullpath[tmp_fullpath_len++] = '/';
@@ -2261,14 +2261,14 @@ static uintmax_t do_change_note_fanout(
2261 tmp_fullpath_len += e->name->str_len;
2262 fullpath[tmp_fullpath_len] = '\0';
2263
2264 - if (tmp_hex_sha1_len == 40 && !get_sha1_hex(hex_sha1, sha1)) {
2264 + if (tmp_hex_oid_len == GIT_SHA1_HEXSZ && !get_oid_hex(hex_oid, &oid)) {
2265 /* This is a note entry */
2266 if (fanout == 0xff) {
2267 /* Counting mode, no rename */
2268 num_notes++;
2269 continue;
2270 }
2271 - construct_path_with_fanout(hex_sha1, fanout, realpath);
2271 + construct_path_with_fanout(hex_oid, fanout, realpath);
2272 if (!strcmp(fullpath, realpath)) {
2273 /* Note entry is in correct location */
2274 num_notes++;
@@ -2279,13 +2279,13 @@ static uintmax_t do_change_note_fanout(
2279 if (!tree_content_remove(orig_root, fullpath, &leaf, 0))
2280 die("Failed to remove path %s", fullpath);
2281 tree_content_set(orig_root, realpath,
2282 - leaf.versions[1].oid.hash,
2282 + &leaf.versions[1].oid,
2283 leaf.versions[1].mode,
2284 leaf.tree);
2285 } else if (S_ISDIR(e->versions[1].mode)) {
2286 /* This is a subdir that may contain note entries */
2287 num_notes += do_change_note_fanout(orig_root, e,
2288 - hex_sha1, tmp_hex_sha1_len,
2288 + hex_oid, tmp_hex_oid_len,
2289 fullpath, tmp_fullpath_len, fanout);
2290 }
2291
@@ -2298,8 +2298,14 @@ static uintmax_t do_change_note_fanout(
2298 static uintmax_t change_note_fanout(struct tree_entry *root,
2299 unsigned char fanout)
2300 {
2301 - char hex_sha1[40], path[60];
2302 - return do_change_note_fanout(root, root, hex_sha1, 0, path, 0, fanout);
2301 + /*
2302 + * The size of path is due to one slash between every two hex digits,
2303 + * plus the terminating NUL. Note that there is no slash at the end, so
2304 + * the number of slashes is one less than half the number of hex
2305 + * characters.
2306 + */
2307 + char hex_oid[GIT_MAX_HEXSZ], path[GIT_MAX_HEXSZ + (GIT_MAX_HEXSZ / 2) - 1 + 1];
2308 + return do_change_note_fanout(root, root, hex_oid, 0, path, 0, fanout);
2309 }
2310
2311 /*
@@ -2360,7 +2366,7 @@ static void file_change_m(const char *p, struct branch *b)
2366 static struct strbuf uq = STRBUF_INIT;
2367 const char *endp;
2368 struct object_entry *oe;
2363 - unsigned char sha1[20];
2369 + struct object_id oid;
2370 uint16_t mode, inline_data = 0;
2371
2372 p = get_mode(p, &mode);
@@ -2383,15 +2389,14 @@ static void file_change_m(const char *p, struct branch *b)
2389
2390 if (*p == ':') {
2391 oe = find_mark(parse_mark_ref_space(&p));
2386 - hashcpy(sha1, oe->idx.sha1);
2392 + hashcpy(oid.hash, oe->idx.sha1);
2393 } else if (skip_prefix(p, "inline ", &p)) {
2394 inline_data = 1;
2395 oe = NULL; /* not used with inline_data, but makes gcc happy */
2396 } else {
2391 - if (get_sha1_hex(p, sha1))
2397 + if (parse_oid_hex(p, &oid, &p))
2398 die("Invalid dataref: %s", command_buf.buf);
2393 - oe = find_object(sha1);
2394 - p += 40;
2399 + oe = find_object(&oid);
2400 if (*p++ != ' ')
2401 die("Missing space after SHA1: %s", command_buf.buf);
2402 }
@@ -2404,7 +2409,7 @@ static void file_change_m(const char *p, struct branch *b)
2409 }
2410
2411 /* Git does not track empty, non-toplevel directories. */
2407 - if (S_ISDIR(mode) && !hashcmp(sha1, EMPTY_TREE_SHA1_BIN) && *p) {
2412 + if (S_ISDIR(mode) && is_empty_tree_oid(&oid) && *p) {
2413 tree_content_remove(&b->branch_tree, p, NULL, 0);
2414 return;
2415 }
@@ -2431,12 +2436,12 @@ static void file_change_m(const char *p, struct branch *b)
2436 p = uq.buf;
2437 }
2438 read_next_command();
2434 - parse_and_store_blob(&last_blob, sha1, 0);
2439 + parse_and_store_blob(&last_blob, &oid, 0);
2440 } else {
2441 enum object_type expected = S_ISDIR(mode) ?
2442 OBJ_TREE: OBJ_BLOB;
2443 enum object_type type = oe ? oe->type :
2439 - sha1_object_info(sha1, NULL);
2444 + sha1_object_info(oid.hash, NULL);
2445 if (type < 0)
2446 die("%s not found: %s",
2447 S_ISDIR(mode) ? "Tree" : "Blob",
@@ -2448,10 +2453,10 @@ static void file_change_m(const char *p, struct branch *b)
2453 }
2454
2455 if (!*p) {
2451 - tree_content_replace(&b->branch_tree, sha1, mode, NULL);
2456 + tree_content_replace(&b->branch_tree, &oid, mode, NULL);
2457 return;
2458 }
2454 - tree_content_set(&b->branch_tree, p, sha1, mode, NULL);
2459 + tree_content_set(&b->branch_tree, p, &oid, mode, NULL);
2460 }
2461
2462 static void file_change_d(const char *p, struct branch *b)
@@ -2509,13 +2514,13 @@ static void file_change_cr(const char *s, struct branch *b, int rename)
2514 die("Path %s not in branch", s);
2515 if (!*d) { /* C "path/to/subdir" "" */
2516 tree_content_replace(&b->branch_tree,
2512 - leaf.versions[1].oid.hash,
2517 + &leaf.versions[1].oid,
2518 leaf.versions[1].mode,
2519 leaf.tree);
2520 return;
2521 }
2522 tree_content_set(&b->branch_tree, d,
2518 - leaf.versions[1].oid.hash,
2523 + &leaf.versions[1].oid,
2524 leaf.versions[1].mode,
2525 leaf.tree);
2526 }
@@ -2525,7 +2530,7 @@ static void note_change_n(const char *p, struct branch *b, unsigned char *old_fa
2530 static struct strbuf uq = STRBUF_INIT;
2531 struct object_entry *oe;
2532 struct branch *s;
2528 - unsigned char sha1[20], commit_sha1[20];
2533 + struct object_id oid, commit_oid;
2534 char path[60];
2535 uint16_t inline_data = 0;
2536 unsigned char new_fanout;
@@ -2550,15 +2555,14 @@ static void note_change_n(const char *p, struct branch *b, unsigned char *old_fa
2555 /* <dataref> or 'inline' */
2556 if (*p == ':') {
2557 oe = find_mark(parse_mark_ref_space(&p));
2553 - hashcpy(sha1, oe->idx.sha1);
2558 + hashcpy(oid.hash, oe->idx.sha1);
2559 } else if (skip_prefix(p, "inline ", &p)) {
2560 inline_data = 1;
2561 oe = NULL; /* not used with inline_data, but makes gcc happy */
2562 } else {
2558 - if (get_sha1_hex(p, sha1))
2563 + if (parse_oid_hex(p, &oid, &p))
2564 die("Invalid dataref: %s", command_buf.buf);
2560 - oe = find_object(sha1);
2561 - p += 40;
2565 + oe = find_object(&oid);
2566 if (*p++ != ' ')
2567 die("Missing space after SHA1: %s", command_buf.buf);
2568 }
@@ -2568,17 +2572,17 @@ static void note_change_n(const char *p, struct branch *b, unsigned char *old_fa
2572 if (s) {
2573 if (is_null_oid(&s->oid))
2574 die("Can't add a note on empty branch.");
2571 - hashcpy(commit_sha1, s->oid.hash);
2575 + oidcpy(&commit_oid, &s->oid);
2576 } else if (*p == ':') {
2577 uintmax_t commit_mark = parse_mark_ref_eol(p);
2578 struct object_entry *commit_oe = find_mark(commit_mark);
2579 if (commit_oe->type != OBJ_COMMIT)
2580 die("Mark :%" PRIuMAX " not a commit", commit_mark);
2577 - hashcpy(commit_sha1, commit_oe->idx.sha1);
2578 - } else if (!get_sha1(p, commit_sha1)) {
2581 + hashcpy(commit_oid.hash, commit_oe->idx.sha1);
2582 + } else if (!get_oid(p, &commit_oid)) {
2583 unsigned long size;
2580 - char *buf = read_object_with_reference(commit_sha1,
2581 - commit_type, &size, commit_sha1);
2584 + char *buf = read_object_with_reference(commit_oid.hash,
2585 + commit_type, &size, commit_oid.hash);
2586 if (!buf || size < 46)
2587 die("Not a valid commit: %s", p);
2588 free(buf);
@@ -2591,13 +2595,13 @@ static void note_change_n(const char *p, struct branch *b, unsigned char *old_fa
2595 p = uq.buf;
2596 }
2597 read_next_command();
2594 - parse_and_store_blob(&last_blob, sha1, 0);
2598 + parse_and_store_blob(&last_blob, &oid, 0);
2599 } else if (oe) {
2600 if (oe->type != OBJ_BLOB)
2601 die("Not a blob (actually a %s): %s",
2602 typename(oe->type), command_buf.buf);
2599 - } else if (!is_null_sha1(sha1)) {
2600 - enum object_type type = sha1_object_info(sha1, NULL);
2603 + } else if (!is_null_oid(&oid)) {
2604 + enum object_type type = sha1_object_info(oid.hash, NULL);
2605 if (type < 0)
2606 die("Blob not found: %s", command_buf.buf);
2607 if (type != OBJ_BLOB)
@@ -2605,17 +2609,17 @@ static void note_change_n(const char *p, struct branch *b, unsigned char *old_fa
2609 typename(type), command_buf.buf);
2610 }
2611
2608 - construct_path_with_fanout(sha1_to_hex(commit_sha1), *old_fanout, path);
2612 + construct_path_with_fanout(oid_to_hex(&commit_oid), *old_fanout, path);
2613 if (tree_content_remove(&b->branch_tree, path, NULL, 0))
2614 b->num_notes--;
2615
2612 - if (is_null_sha1(sha1))
2616 + if (is_null_oid(&oid))
2617 return; /* nothing to insert */
2618
2619 b->num_notes++;
2620 new_fanout = convert_num_notes_to_fanout(b->num_notes);
2617 - construct_path_with_fanout(sha1_to_hex(commit_sha1), new_fanout, path);
2618 - tree_content_set(&b->branch_tree, path, sha1, S_IFREG | 0644, NULL);
2621 + construct_path_with_fanout(oid_to_hex(&commit_oid), new_fanout, path);
2622 + tree_content_set(&b->branch_tree, path, &oid, S_IFREG | 0644, NULL);
2623 }
2624
2625 static void file_change_deleteall(struct branch *b)
@@ -2629,10 +2633,10 @@ static void file_change_deleteall(struct branch *b)
2633
2634 static void parse_from_commit(struct branch *b, char *buf, unsigned long size)
2635 {
2632 - if (!buf || size < 46)
2636 + if (!buf || size < GIT_SHA1_HEXSZ + 6)
2637 die("Not a valid commit: %s", oid_to_hex(&b->oid));
2638 if (memcmp("tree ", buf, 5)
2635 - || get_sha1_hex(buf + 5, b->branch_tree.versions[1].oid.hash))
2639 + || get_oid_hex(buf + 5, &b->branch_tree.versions[1].oid))
2640 die("The commit %s is corrupt", oid_to_hex(&b->oid));
2641 oidcpy(&b->branch_tree.versions[0].oid,
2642 &b->branch_tree.versions[1].oid);
@@ -2659,21 +2663,21 @@ static int parse_from(struct branch *b)
2663 {
2664 const char *from;
2665 struct branch *s;
2662 - unsigned char sha1[20];
2666 + struct object_id oid;
2667
2668 if (!skip_prefix(command_buf.buf, "from ", &from))
2669 return 0;
2670
2667 - hashcpy(sha1, b->branch_tree.versions[1].oid.hash);
2671 + oidcpy(&oid, &b->branch_tree.versions[1].oid);
2672
2673 s = lookup_branch(from);
2674 if (b == s)
2675 die("Can't create a branch from itself: %s", b->name);
2676 else if (s) {
2673 - unsigned char *t = s->branch_tree.versions[1].oid.hash;
2677 + struct object_id *t = &s->branch_tree.versions[1].oid;
2678 oidcpy(&b->oid, &s->oid);
2675 - hashcpy(b->branch_tree.versions[0].oid.hash, t);
2676 - hashcpy(b->branch_tree.versions[1].oid.hash, t);
2679 + oidcpy(&b->branch_tree.versions[0].oid, t);
2680 + oidcpy(&b->branch_tree.versions[1].oid, t);
2681 } else if (*from == ':') {
2682 uintmax_t idnum = parse_mark_ref_eol(from);
2683 struct object_entry *oe = find_mark(idnum);
@@ -2689,7 +2693,7 @@ static int parse_from(struct branch *b)
2693 } else
2694 parse_from_existing(b);
2695 }
2692 - } else if (!get_sha1(from, b->oid.hash)) {
2696 + } else if (!get_oid(from, &b->oid)) {
2697 parse_from_existing(b);
2698 if (is_null_oid(&b->oid))
2699 b->delete = 1;
@@ -2697,7 +2701,7 @@ static int parse_from(struct branch *b)
2701 else
2702 die("Invalid ref name or SHA1 expression: %s", from);
2703
2700 - if (b->branch_tree.tree && hashcmp(sha1, b->branch_tree.versions[1].oid.hash)) {
2704 + if (b->branch_tree.tree && oidcmp(&oid, &b->branch_tree.versions[1].oid)) {
2705 release_tree_content_recursive(b->branch_tree.tree);
2706 b->branch_tree.tree = NULL;
2707 }
@@ -2724,12 +2728,10 @@ static struct hash_list *parse_merge(unsigned int *count)
2728 if (oe->type != OBJ_COMMIT)
2729 die("Mark :%" PRIuMAX " not a commit", idnum);
2730 hashcpy(n->oid.hash, oe->idx.sha1);
2727 - } else if (!get_sha1(from, n->oid.hash)) {
2731 + } else if (!get_oid(from, &n->oid)) {
2732 unsigned long size;
2733 char *buf = read_object_with_reference(n->oid.hash,
2730 - commit_type,
2731 - &size,
2732 - n->oid.hash);
2734 + commit_type, &size, n->oid.hash);
2735 if (!buf || size < 46)
2736 die("Not a valid commit: %s", from);
2737 free(buf);
@@ -2841,7 +2843,7 @@ static void parse_new_commit(const char *arg)
2843 free(author);
2844 free(committer);
2845
2844 - if (!store_object(OBJ_COMMIT, &new_data, NULL, b->oid.hash, next_mark))
2846 + if (!store_object(OBJ_COMMIT, &new_data, NULL, &b->oid, next_mark))
2847 b->pack_id = pack_id;
2848 b->last_commit = object_count_by_type[OBJ_COMMIT];
2849 }
@@ -2854,7 +2856,7 @@ static void parse_new_tag(const char *arg)
2856 struct branch *s;
2857 struct tag *t;
2858 uintmax_t from_mark = 0;
2857 - unsigned char sha1[20];
2859 + struct object_id oid;
2860 enum object_type type;
2861 const char *v;
2862
@@ -2875,18 +2877,18 @@ static void parse_new_tag(const char *arg)
2877 if (s) {
2878 if (is_null_oid(&s->oid))
2879 die("Can't tag an empty branch.");
2878 - hashcpy(sha1, s->oid.hash);
2880 + oidcpy(&oid, &s->oid);
2881 type = OBJ_COMMIT;
2882 } else if (*from == ':') {
2883 struct object_entry *oe;
2884 from_mark = parse_mark_ref_eol(from);
2885 oe = find_mark(from_mark);
2886 type = oe->type;
2885 - hashcpy(sha1, oe->idx.sha1);
2886 - } else if (!get_sha1(from, sha1)) {
2887 - struct object_entry *oe = find_object(sha1);
2887 + hashcpy(oid.hash, oe->idx.sha1);
2888 + } else if (!get_oid(from, &oid)) {
2889 + struct object_entry *oe = find_object(&oid);
2890 if (!oe) {
2889 - type = sha1_object_info(sha1, NULL);
2891 + type = sha1_object_info(oid.hash, NULL);
2892 if (type < 0)
2893 die("Not a valid object: %s", from);
2894 } else
@@ -2912,7 +2914,7 @@ static void parse_new_tag(const char *arg)
2914 "object %s\n"
2915 "type %s\n"
2916 "tag %s\n",
2915 - sha1_to_hex(sha1), typename(type), t->name);
2917 + oid_to_hex(&oid), typename(type), t->name);
2918 if (tagger)
2919 strbuf_addf(&new_data,
2920 "tagger %s\n", tagger);
@@ -2920,7 +2922,7 @@ static void parse_new_tag(const char *arg)
2922 strbuf_addbuf(&new_data, &msg);
2923 free(tagger);
2924
2923 - if (store_object(OBJ_TAG, &new_data, NULL, t->oid.hash, 0))
2925 + if (store_object(OBJ_TAG, &new_data, NULL, &t->oid, 0))
2926 t->pack_id = MAX_PACK_ID;
2927 else
2928 t->pack_id = pack_id;
@@ -2954,7 +2956,7 @@ static void cat_blob_write(const char *buf, unsigned long size)
2956 die_errno("Write to frontend failed");
2957 }
2958
2957 -static void cat_blob(struct object_entry *oe, unsigned char sha1[20])
2959 +static void cat_blob(struct object_entry *oe, struct object_id *oid)
2960 {
2961 struct strbuf line = STRBUF_INIT;
2962 unsigned long size;
@@ -2962,7 +2964,7 @@ static void cat_blob(struct object_entry *oe, unsigned char sha1[20])
2964 char *buf;
2965
2966 if (!oe || oe->pack_id == MAX_PACK_ID) {
2965 - buf = read_sha1_file(sha1, &type, &size);
2967 + buf = read_sha1_file(oid->hash, &type, &size);
2968 } else {
2969 type = oe->type;
2970 buf = gfi_unpack_entry(oe, &size);
@@ -2973,19 +2975,19 @@ static void cat_blob(struct object_entry *oe, unsigned char sha1[20])
2975 */
2976 if (type <= 0) {
2977 strbuf_reset(&line);
2976 - strbuf_addf(&line, "%s missing\n", sha1_to_hex(sha1));
2978 + strbuf_addf(&line, "%s missing\n", oid_to_hex(oid));
2979 cat_blob_write(line.buf, line.len);
2980 strbuf_release(&line);
2981 free(buf);
2982 return;
2983 }
2984 if (!buf)
2983 - die("Can't read object %s", sha1_to_hex(sha1));
2985 + die("Can't read object %s", oid_to_hex(oid));
2986 if (type != OBJ_BLOB)
2987 die("Object %s is a %s but a blob was expected.",
2986 - sha1_to_hex(sha1), typename(type));
2988 + oid_to_hex(oid), typename(type));
2989 strbuf_reset(&line);
2988 - strbuf_addf(&line, "%s %s %lu\n", sha1_to_hex(sha1),
2990 + strbuf_addf(&line, "%s %s %lu\n", oid_to_hex(oid),
2991 typename(type), size);
2992 cat_blob_write(line.buf, line.len);
2993 strbuf_release(&line);
@@ -3002,7 +3004,7 @@ static void cat_blob(struct object_entry *oe, unsigned char sha1[20])
3004 static void parse_get_mark(const char *p)
3005 {
3006 struct object_entry *oe = oe;
3005 - char output[42];
3007 + char output[GIT_MAX_HEXSZ + 2];
3008
3009 /* get-mark SP <object> LF */
3010 if (*p != ':')
@@ -3013,42 +3015,42 @@ static void parse_get_mark(const char *p)
3015 die("Unknown mark: %s", command_buf.buf);
3016
3017 xsnprintf(output, sizeof(output), "%s\n", sha1_to_hex(oe->idx.sha1));
3016 - cat_blob_write(output, 41);
3018 + cat_blob_write(output, GIT_SHA1_HEXSZ + 1);
3019 }
3020
3021 static void parse_cat_blob(const char *p)
3022 {
3023 struct object_entry *oe = oe;
3022 - unsigned char sha1[20];
3024 + struct object_id oid;
3025
3026 /* cat-blob SP <object> LF */
3027 if (*p == ':') {
3028 oe = find_mark(parse_mark_ref_eol(p));
3029 if (!oe)
3030 die("Unknown mark: %s", command_buf.buf);
3029 - hashcpy(sha1, oe->idx.sha1);
3031 + hashcpy(oid.hash, oe->idx.sha1);
3032 } else {
3031 - if (get_sha1_hex(p, sha1))
3033 + if (parse_oid_hex(p, &oid, &p))
3034 die("Invalid dataref: %s", command_buf.buf);
3033 - if (p[40])
3035 + if (*p)
3036 die("Garbage after SHA1: %s", command_buf.buf);
3035 - oe = find_object(sha1);
3037 + oe = find_object(&oid);
3038 }
3039
3038 - cat_blob(oe, sha1);
3040 + cat_blob(oe, &oid);
3041 }
3042
3043 static struct object_entry *dereference(struct object_entry *oe,
3042 - unsigned char sha1[20])
3044 + struct object_id *oid)
3045 {
3046 unsigned long size;
3047 char *buf = NULL;
3048 if (!oe) {
3047 - enum object_type type = sha1_object_info(sha1, NULL);
3049 + enum object_type type = sha1_object_info(oid->hash, NULL);
3050 if (type < 0)
3049 - die("object not found: %s", sha1_to_hex(sha1));
3051 + die("object not found: %s", oid_to_hex(oid));
3052 /* cache it! */
3051 - oe = insert_object(sha1);
3053 + oe = insert_object(oid);
3054 oe->type = type;
3055 oe->pack_id = MAX_PACK_ID;
3056 oe->idx.offset = 1;
@@ -3067,49 +3069,48 @@ static struct object_entry *dereference(struct object_entry *oe,
3069 buf = gfi_unpack_entry(oe, &size);
3070 } else {
3071 enum object_type unused;
3070 - buf = read_sha1_file(sha1, &unused, &size);
3072 + buf = read_sha1_file(oid->hash, &unused, &size);
3073 }
3074 if (!buf)
3073 - die("Can't load object %s", sha1_to_hex(sha1));
3075 + die("Can't load object %s", oid_to_hex(oid));
3076
3077 /* Peel one layer. */
3078 switch (oe->type) {
3079 case OBJ_TAG:
3078 - if (size < 40 + strlen("object ") ||
3079 - get_sha1_hex(buf + strlen("object "), sha1))
3080 + if (size < GIT_SHA1_HEXSZ + strlen("object ") ||
3081 + get_oid_hex(buf + strlen("object "), oid))
3082 die("Invalid SHA1 in tag: %s", command_buf.buf);
3083 break;
3084 case OBJ_COMMIT:
3083 - if (size < 40 + strlen("tree ") ||
3084 - get_sha1_hex(buf + strlen("tree "), sha1))
3085 + if (size < GIT_SHA1_HEXSZ + strlen("tree ") ||
3086 + get_oid_hex(buf + strlen("tree "), oid))
3087 die("Invalid SHA1 in commit: %s", command_buf.buf);
3088 }
3089
3090 free(buf);
3089 - return find_object(sha1);
3091 + return find_object(oid);
3092 }
3093
3094 static struct object_entry *parse_treeish_dataref(const char **p)
3095 {
3094 - unsigned char sha1[20];
3096 + struct object_id oid;
3097 struct object_entry *e;
3098
3099 if (**p == ':') { /* <mark> */
3100 e = find_mark(parse_mark_ref_space(p));
3101 if (!e)
3102 die("Unknown mark: %s", command_buf.buf);
3101 - hashcpy(sha1, e->idx.sha1);
3103 + hashcpy(oid.hash, e->idx.sha1);
3104 } else { /* <sha1> */
3103 - if (get_sha1_hex(*p, sha1))
3105 + if (parse_oid_hex(*p, &oid, p))
3106 die("Invalid dataref: %s", command_buf.buf);
3105 - e = find_object(sha1);
3106 - *p += 40;
3107 + e = find_object(&oid);
3108 if (*(*p)++ != ' ')
3109 die("Missing space after tree-ish: %s", command_buf.buf);
3110 }
3111
3112 while (!e || e->type != OBJ_TREE)
3112 - e = dereference(e, sha1);
3113 + e = dereference(e, &oid);
3114 return e;
3115 }
3116