builtin/am: convert to struct object_id

Convert uses of unsigned char [20] to struct object_id. Rename the generically-named "ptr" to "old_oid" and make it const. 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:08 UTC 8c88769ba422e451fb50eb2ea843233580befed4
1 file changed +70 -70
builtin/am.c
+70 -70
@@ -108,7 +108,7 @@ struct am_state {
108 size_t msg_len;
109
110 /* when --rebasing, records the original commit the patch came from */
111 - unsigned char orig_commit[GIT_SHA1_RAWSZ];
111 + struct object_id orig_commit;
112
113 /* number of digits in patch filename */
114 int prec;
@@ -428,8 +428,8 @@ static void am_load(struct am_state *state)
428 read_commit_msg(state);
429
430 if (read_state_file(&sb, state, "original-commit", 1) < 0)
431 - hashclr(state->orig_commit);
432 - else if (get_sha1_hex(sb.buf, state->orig_commit) < 0)
431 + oidclr(&state->orig_commit);
432 + else if (get_oid_hex(sb.buf, &state->orig_commit) < 0)
433 die(_("could not parse %s"), am_path(state, "original-commit"));
434
435 read_state_file(&sb, state, "threeway", 1);
@@ -555,14 +555,14 @@ static int copy_notes_for_rebase(const struct am_state *state)
555 fp = xfopen(am_path(state, "rewritten"), "r");
556
557 while (!strbuf_getline_lf(&sb, fp)) {
558 - unsigned char from_obj[GIT_SHA1_RAWSZ], to_obj[GIT_SHA1_RAWSZ];
558 + struct object_id from_obj, to_obj;
559
560 if (sb.len != GIT_SHA1_HEXSZ * 2 + 1) {
561 ret = error(invalid_line, sb.buf);
562 goto finish;
563 }
564
565 - if (get_sha1_hex(sb.buf, from_obj)) {
565 + if (get_oid_hex(sb.buf, &from_obj)) {
566 ret = error(invalid_line, sb.buf);
567 goto finish;
568 }
@@ -572,14 +572,14 @@ static int copy_notes_for_rebase(const struct am_state *state)
572 goto finish;
573 }
574
575 - if (get_sha1_hex(sb.buf + GIT_SHA1_HEXSZ + 1, to_obj)) {
575 + if (get_oid_hex(sb.buf + GIT_SHA1_HEXSZ + 1, &to_obj)) {
576 ret = error(invalid_line, sb.buf);
577 goto finish;
578 }
579
580 - if (copy_note_for_rewrite(c, from_obj, to_obj))
580 + if (copy_note_for_rewrite(c, from_obj.hash, to_obj.hash))
581 ret = error(_("Failed to copy notes from '%s' to '%s'"),
582 - sha1_to_hex(from_obj), sha1_to_hex(to_obj));
582 + oid_to_hex(&from_obj), oid_to_hex(&to_obj));
583 }
584
585 finish:
@@ -985,7 +985,7 @@ static int split_mail(struct am_state *state, enum patch_format patch_format,
985 static void am_setup(struct am_state *state, enum patch_format patch_format,
986 const char **paths, int keep_cr)
987 {
988 - unsigned char curr_head[GIT_SHA1_RAWSZ];
988 + struct object_id curr_head;
989 const char *str;
990 struct strbuf sb = STRBUF_INIT;
991
@@ -1053,10 +1053,10 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
1053 else
1054 write_state_text(state, "applying", "");
1055
1056 - if (!get_sha1("HEAD", curr_head)) {
1057 - write_state_text(state, "abort-safety", sha1_to_hex(curr_head));
1056 + if (!get_oid("HEAD", &curr_head)) {
1057 + write_state_text(state, "abort-safety", oid_to_hex(&curr_head));
1058 if (!state->rebasing)
1059 - update_ref("am", "ORIG_HEAD", curr_head, NULL, 0,
1059 + update_ref_oid("am", "ORIG_HEAD", &curr_head, NULL, 0,
1060 UPDATE_REFS_DIE_ON_ERR);
1061 } else {
1062 write_state_text(state, "abort-safety", "");
@@ -1081,7 +1081,7 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
1081 */
1082 static void am_next(struct am_state *state)
1083 {
1084 - unsigned char head[GIT_SHA1_RAWSZ];
1084 + struct object_id head;
1085
1086 free(state->author_name);
1087 state->author_name = NULL;
@@ -1099,11 +1099,11 @@ static void am_next(struct am_state *state)
1099 unlink(am_path(state, "author-script"));
1100 unlink(am_path(state, "final-commit"));
1101
1102 - hashclr(state->orig_commit);
1102 + oidclr(&state->orig_commit);
1103 unlink(am_path(state, "original-commit"));
1104
1105 - if (!get_sha1("HEAD", head))
1106 - write_state_text(state, "abort-safety", sha1_to_hex(head));
1105 + if (!get_oid("HEAD", &head))
1106 + write_state_text(state, "abort-safety", oid_to_hex(&head));
1107 else
1108 write_state_text(state, "abort-safety", "");
1109
@@ -1145,17 +1145,17 @@ static void refresh_and_write_cache(void)
1145 */
1146 static int index_has_changes(struct strbuf *sb)
1147 {
1148 - unsigned char head[GIT_SHA1_RAWSZ];
1148 + struct object_id head;
1149 int i;
1150
1151 - if (!get_sha1_tree("HEAD", head)) {
1151 + if (!get_sha1_tree("HEAD", head.hash)) {
1152 struct diff_options opt;
1153
1154 diff_setup(&opt);
1155 DIFF_OPT_SET(&opt, EXIT_WITH_STATUS);
1156 if (!sb)
1157 DIFF_OPT_SET(&opt, QUICK);
1158 - do_diff_cache(head, &opt);
1158 + do_diff_cache(head.hash, &opt);
1159 diffcore_std(&opt);
1160 for (i = 0; sb && i < diff_queued_diff.nr; i++) {
1161 if (i)
@@ -1362,7 +1362,7 @@ finish:
1362 * Sets commit_id to the commit hash where the mail was generated from.
1363 * Returns 0 on success, -1 on failure.
1364 */
1365 -static int get_mail_commit_sha1(unsigned char *commit_id, const char *mail)
1365 +static int get_mail_commit_oid(struct object_id *commit_id, const char *mail)
1366 {
1367 struct strbuf sb = STRBUF_INIT;
1368 FILE *fp = xfopen(mail, "r");
@@ -1374,7 +1374,7 @@ static int get_mail_commit_sha1(unsigned char *commit_id, const char *mail)
1374 if (!skip_prefix(sb.buf, "From ", &x))
1375 return -1;
1376
1377 - if (get_sha1_hex(x, commit_id) < 0)
1377 + if (get_oid_hex(x, commit_id) < 0)
1378 return -1;
1379
1380 strbuf_release(&sb);
@@ -1464,12 +1464,12 @@ static void write_commit_patch(const struct am_state *state, struct commit *comm
1464 static void write_index_patch(const struct am_state *state)
1465 {
1466 struct tree *tree;
1467 - unsigned char head[GIT_SHA1_RAWSZ];
1467 + struct object_id head;
1468 struct rev_info rev_info;
1469 FILE *fp;
1470
1471 - if (!get_sha1_tree("HEAD", head))
1472 - tree = lookup_tree(head);
1471 + if (!get_sha1_tree("HEAD", head.hash))
1472 + tree = lookup_tree(head.hash);
1473 else
1474 tree = lookup_tree(EMPTY_TREE_SHA1_BIN);
1475
@@ -1499,19 +1499,19 @@ static void write_index_patch(const struct am_state *state)
1499 static int parse_mail_rebase(struct am_state *state, const char *mail)
1500 {
1501 struct commit *commit;
1502 - unsigned char commit_sha1[GIT_SHA1_RAWSZ];
1502 + struct object_id commit_oid;
1503
1504 - if (get_mail_commit_sha1(commit_sha1, mail) < 0)
1504 + if (get_mail_commit_oid(&commit_oid, mail) < 0)
1505 die(_("could not parse %s"), mail);
1506
1507 - commit = lookup_commit_or_die(commit_sha1, mail);
1507 + commit = lookup_commit_or_die(commit_oid.hash, mail);
1508
1509 get_commit_info(state, commit);
1510
1511 write_commit_patch(state, commit);
1512
1513 - hashcpy(state->orig_commit, commit_sha1);
1514 - write_state_text(state, "original-commit", sha1_to_hex(commit_sha1));
1513 + oidcpy(&state->orig_commit, &commit_oid);
1514 + write_state_text(state, "original-commit", oid_to_hex(&commit_oid));
1515
1516 return 0;
1517 }
@@ -1665,9 +1665,8 @@ static int fall_back_threeway(const struct am_state *state, const char *index_pa
1665 */
1666 static void do_commit(const struct am_state *state)
1667 {
1668 - unsigned char tree[GIT_SHA1_RAWSZ], parent[GIT_SHA1_RAWSZ],
1669 - commit[GIT_SHA1_RAWSZ];
1670 - unsigned char *ptr;
1668 + struct object_id tree, parent, commit;
1669 + const struct object_id *old_oid;
1670 struct commit_list *parents = NULL;
1671 const char *reflog_msg, *author;
1672 struct strbuf sb = STRBUF_INIT;
@@ -1675,14 +1674,14 @@ static void do_commit(const struct am_state *state)
1674 if (run_hook_le(NULL, "pre-applypatch", NULL))
1675 exit(1);
1676
1678 - if (write_cache_as_tree(tree, 0, NULL))
1677 + if (write_cache_as_tree(tree.hash, 0, NULL))
1678 die(_("git write-tree failed to write a tree"));
1679
1681 - if (!get_sha1_commit("HEAD", parent)) {
1682 - ptr = parent;
1683 - commit_list_insert(lookup_commit(parent), &parents);
1680 + if (!get_sha1_commit("HEAD", parent.hash)) {
1681 + old_oid = &parent;
1682 + commit_list_insert(lookup_commit(parent.hash), &parents);
1683 } else {
1685 - ptr = NULL;
1684 + old_oid = NULL;
1685 say(state, stderr, _("applying to an empty history"));
1686 }
1687
@@ -1694,7 +1693,7 @@ static void do_commit(const struct am_state *state)
1693 setenv("GIT_COMMITTER_DATE",
1694 state->ignore_date ? "" : state->author_date, 1);
1695
1697 - if (commit_tree(state->msg, state->msg_len, tree, parents, commit,
1696 + if (commit_tree(state->msg, state->msg_len, tree.hash, parents, commit.hash,
1697 author, state->sign_commit))
1698 die(_("failed to write commit object"));
1699
@@ -1705,14 +1704,15 @@ static void do_commit(const struct am_state *state)
1704 strbuf_addf(&sb, "%s: %.*s", reflog_msg, linelen(state->msg),
1705 state->msg);
1706
1708 - update_ref(sb.buf, "HEAD", commit, ptr, 0, UPDATE_REFS_DIE_ON_ERR);
1707 + update_ref_oid(sb.buf, "HEAD", &commit, old_oid, 0,
1708 + UPDATE_REFS_DIE_ON_ERR);
1709
1710 if (state->rebasing) {
1711 FILE *fp = xfopen(am_path(state, "rewritten"), "a");
1712
1713 - assert(!is_null_sha1(state->orig_commit));
1714 - fprintf(fp, "%s ", sha1_to_hex(state->orig_commit));
1715 - fprintf(fp, "%s\n", sha1_to_hex(commit));
1713 + assert(!is_null_oid(&state->orig_commit));
1714 + fprintf(fp, "%s ", oid_to_hex(&state->orig_commit));
1715 + fprintf(fp, "%s\n", oid_to_hex(&commit));
1716 fclose(fp);
1717 }
1718
@@ -2033,30 +2033,30 @@ static int merge_tree(struct tree *tree)
2033 * Clean the index without touching entries that are not modified between
2034 * `head` and `remote`.
2035 */
2036 -static int clean_index(const unsigned char *head, const unsigned char *remote)
2036 +static int clean_index(const struct object_id *head, const struct object_id *remote)
2037 {
2038 struct tree *head_tree, *remote_tree, *index_tree;
2039 - unsigned char index[GIT_SHA1_RAWSZ];
2039 + struct object_id index;
2040
2041 - head_tree = parse_tree_indirect(head);
2041 + head_tree = parse_tree_indirect(head->hash);
2042 if (!head_tree)
2043 - return error(_("Could not parse object '%s'."), sha1_to_hex(head));
2043 + return error(_("Could not parse object '%s'."), oid_to_hex(head));
2044
2045 - remote_tree = parse_tree_indirect(remote);
2045 + remote_tree = parse_tree_indirect(remote->hash);
2046 if (!remote_tree)
2047 - return error(_("Could not parse object '%s'."), sha1_to_hex(remote));
2047 + return error(_("Could not parse object '%s'."), oid_to_hex(remote));
2048
2049 read_cache_unmerged();
2050
2051 if (fast_forward_to(head_tree, head_tree, 1))
2052 return -1;
2053
2054 - if (write_cache_as_tree(index, 0, NULL))
2054 + if (write_cache_as_tree(index.hash, 0, NULL))
2055 return -1;
2056
2057 - index_tree = parse_tree_indirect(index);
2057 + index_tree = parse_tree_indirect(index.hash);
2058 if (!index_tree)
2059 - return error(_("Could not parse object '%s'."), sha1_to_hex(index));
2059 + return error(_("Could not parse object '%s'."), oid_to_hex(&index));
2060
2061 if (fast_forward_to(index_tree, remote_tree, 0))
2062 return -1;
@@ -2084,14 +2084,14 @@ static void am_rerere_clear(void)
2084 */
2085 static void am_skip(struct am_state *state)
2086 {
2087 - unsigned char head[GIT_SHA1_RAWSZ];
2087 + struct object_id head;
2088
2089 am_rerere_clear();
2090
2091 - if (get_sha1("HEAD", head))
2092 - hashcpy(head, EMPTY_TREE_SHA1_BIN);
2091 + if (get_oid("HEAD", &head))
2092 + hashcpy(head.hash, EMPTY_TREE_SHA1_BIN);
2093
2094 - if (clean_index(head, head))
2094 + if (clean_index(&head, &head))
2095 die(_("failed to clean index"));
2096
2097 am_next(state);
@@ -2109,21 +2109,21 @@ static void am_skip(struct am_state *state)
2109 static int safe_to_abort(const struct am_state *state)
2110 {
2111 struct strbuf sb = STRBUF_INIT;
2112 - unsigned char abort_safety[GIT_SHA1_RAWSZ], head[GIT_SHA1_RAWSZ];
2112 + struct object_id abort_safety, head;
2113
2114 if (file_exists(am_path(state, "dirtyindex")))
2115 return 0;
2116
2117 if (read_state_file(&sb, state, "abort-safety", 1) > 0) {
2118 - if (get_sha1_hex(sb.buf, abort_safety))
2118 + if (get_oid_hex(sb.buf, &abort_safety))
2119 die(_("could not parse %s"), am_path(state, "abort_safety"));
2120 } else
2121 - hashclr(abort_safety);
2121 + oidclr(&abort_safety);
2122
2123 - if (get_sha1("HEAD", head))
2124 - hashclr(head);
2123 + if (get_oid("HEAD", &head))
2124 + oidclr(&head);
2125
2126 - if (!hashcmp(head, abort_safety))
2126 + if (!oidcmp(&head, &abort_safety))
2127 return 1;
2128
2129 error(_("You seem to have moved HEAD since the last 'am' failure.\n"
@@ -2137,7 +2137,7 @@ static int safe_to_abort(const struct am_state *state)
2137 */
2138 static void am_abort(struct am_state *state)
2139 {
2140 - unsigned char curr_head[GIT_SHA1_RAWSZ], orig_head[GIT_SHA1_RAWSZ];
2140 + struct object_id curr_head, orig_head;
2141 int has_curr_head, has_orig_head;
2142 char *curr_branch;
2143
@@ -2148,20 +2148,20 @@ static void am_abort(struct am_state *state)
2148
2149 am_rerere_clear();
2150
2151 - curr_branch = resolve_refdup("HEAD", 0, curr_head, NULL);
2152 - has_curr_head = !is_null_sha1(curr_head);
2151 + curr_branch = resolve_refdup("HEAD", 0, curr_head.hash, NULL);
2152 + has_curr_head = !is_null_oid(&curr_head);
2153 if (!has_curr_head)
2154 - hashcpy(curr_head, EMPTY_TREE_SHA1_BIN);
2154 + hashcpy(curr_head.hash, EMPTY_TREE_SHA1_BIN);
2155
2156 - has_orig_head = !get_sha1("ORIG_HEAD", orig_head);
2156 + has_orig_head = !get_oid("ORIG_HEAD", &orig_head);
2157 if (!has_orig_head)
2158 - hashcpy(orig_head, EMPTY_TREE_SHA1_BIN);
2158 + hashcpy(orig_head.hash, EMPTY_TREE_SHA1_BIN);
2159
2160 - clean_index(curr_head, orig_head);
2160 + clean_index(&curr_head, &orig_head);
2161
2162 if (has_orig_head)
2163 - update_ref("am --abort", "HEAD", orig_head,
2164 - has_curr_head ? curr_head : NULL, 0,
2163 + update_ref_oid("am --abort", "HEAD", &orig_head,
2164 + has_curr_head ? &curr_head : NULL, 0,
2165 UPDATE_REFS_DIE_ON_ERR);
2166 else if (curr_branch)
2167 delete_ref(curr_branch, NULL, REF_NODEREF);