]> git.ipfire.org Git - thirdparty/git.git/blobdiff - builtin/am.c
Merge branch 'bw/object-id'
[thirdparty/git.git] / builtin / am.c
index dd60fad1e664d2ab066e561f332b3e2583234c44..3985f9a89f985baa139ea69f5fde8ce1384871f6 100644 (file)
@@ -563,7 +563,7 @@ static int copy_notes_for_rebase(const struct am_state *state)
                        goto finish;
                }
 
-               if (copy_note_for_rewrite(c, from_obj.hash, to_obj.hash))
+               if (copy_note_for_rewrite(c, &from_obj, &to_obj))
                        ret = error(_("Failed to copy notes from '%s' to '%s'"),
                                        oid_to_hex(&from_obj), oid_to_hex(&to_obj));
        }
@@ -879,12 +879,12 @@ static int hg_patch_to_mail(FILE *out, FILE *in, int keep_cr)
                if (skip_prefix(sb.buf, "# User ", &str))
                        fprintf(out, "From: %s\n", str);
                else if (skip_prefix(sb.buf, "# Date ", &str)) {
-                       unsigned long timestamp;
+                       timestamp_t timestamp;
                        long tz, tz2;
                        char *end;
 
                        errno = 0;
-                       timestamp = strtoul(str, &end, 10);
+                       timestamp = parse_timestamp(str, &end, 10);
                        if (errno)
                                return error(_("invalid timestamp"));
 
@@ -1145,7 +1145,7 @@ static int index_has_changes(struct strbuf *sb)
                DIFF_OPT_SET(&opt, EXIT_WITH_STATUS);
                if (!sb)
                        DIFF_OPT_SET(&opt, QUICK);
-               do_diff_cache(head.hash, &opt);
+               do_diff_cache(&head, &opt);
                diffcore_std(&opt);
                for (i = 0; sb && i < diff_queued_diff.nr; i++) {
                        if (i)
@@ -1275,12 +1275,8 @@ static int parse_mail(struct am_state *state, const char *mail)
                die("BUG: invalid value for state->scissors");
        }
 
-       mi.input = fopen(mail, "r");
-       if (!mi.input)
-               die("could not open input");
-       mi.output = fopen(am_path(state, "info"), "w");
-       if (!mi.output)
-               die("could not open output 'info'");
+       mi.input = xfopen(mail, "r");
+       mi.output = xfopen(am_path(state, "info"), "w");
        if (mailinfo(&mi, am_path(state, "msg"), am_path(state, "patch")))
                die("could not parse patch");
 
@@ -1351,19 +1347,16 @@ static int get_mail_commit_oid(struct object_id *commit_id, const char *mail)
        struct strbuf sb = STRBUF_INIT;
        FILE *fp = xfopen(mail, "r");
        const char *x;
+       int ret = 0;
 
-       if (strbuf_getline_lf(&sb, fp))
-               return -1;
-
-       if (!skip_prefix(sb.buf, "From ", &x))
-               return -1;
-
-       if (get_oid_hex(x, commit_id) < 0)
-               return -1;
+       if (strbuf_getline_lf(&sb, fp) ||
+           !skip_prefix(sb.buf, "From ", &x) ||
+           get_oid_hex(x, commit_id) < 0)
+               ret = -1;
 
        strbuf_release(&sb);
        fclose(fp);
-       return 0;
+       return ret;
 }
 
 /**
@@ -1372,40 +1365,33 @@ static int get_mail_commit_oid(struct object_id *commit_id, const char *mail)
  */
 static void get_commit_info(struct am_state *state, struct commit *commit)
 {
-       const char *buffer, *ident_line, *author_date, *msg;
+       const char *buffer, *ident_line, *msg;
        size_t ident_len;
-       struct ident_split ident_split;
-       struct strbuf sb = STRBUF_INIT;
+       struct ident_split id;
 
        buffer = logmsg_reencode(commit, NULL, get_commit_output_encoding());
 
        ident_line = find_commit_header(buffer, "author", &ident_len);
 
-       if (split_ident_line(&ident_split, ident_line, ident_len) < 0) {
-               strbuf_add(&sb, ident_line, ident_len);
-               die(_("invalid ident line: %s"), sb.buf);
-       }
+       if (split_ident_line(&id, ident_line, ident_len) < 0)
+               die(_("invalid ident line: %.*s"), (int)ident_len, ident_line);
 
        assert(!state->author_name);
-       if (ident_split.name_begin) {
-               strbuf_add(&sb, ident_split.name_begin,
-                       ident_split.name_end - ident_split.name_begin);
-               state->author_name = strbuf_detach(&sb, NULL);
-       } else
+       if (id.name_begin)
+               state->author_name =
+                       xmemdupz(id.name_begin, id.name_end - id.name_begin);
+       else
                state->author_name = xstrdup("");
 
        assert(!state->author_email);
-       if (ident_split.mail_begin) {
-               strbuf_add(&sb, ident_split.mail_begin,
-                       ident_split.mail_end - ident_split.mail_begin);
-               state->author_email = strbuf_detach(&sb, NULL);
-       } else
+       if (id.mail_begin)
+               state->author_email =
+                       xmemdupz(id.mail_begin, id.mail_end - id.mail_begin);
+       else
                state->author_email = xstrdup("");
 
-       author_date = show_ident_date(&ident_split, DATE_MODE(NORMAL));
-       strbuf_addstr(&sb, author_date);
        assert(!state->author_date);
-       state->author_date = strbuf_detach(&sb, NULL);
+       state->author_date = xstrdup(show_ident_date(&id, DATE_MODE(NORMAL)));
 
        assert(!state->msg);
        msg = strstr(buffer, "\n\n");
@@ -1413,6 +1399,7 @@ static void get_commit_info(struct am_state *state, struct commit *commit)
                die(_("unable to parse commit %s"), oid_to_hex(&commit->object.oid));
        state->msg = xstrdup(msg + 2);
        state->msg_len = strlen(state->msg);
+       unuse_commit_buffer(commit, buffer);
 }
 
 /**
@@ -1453,9 +1440,9 @@ static void write_index_patch(const struct am_state *state)
        FILE *fp;
 
        if (!get_sha1_tree("HEAD", head.hash))
-               tree = lookup_tree(head.hash);
+               tree = lookup_tree(&head);
        else
-               tree = lookup_tree(EMPTY_TREE_SHA1_BIN);
+               tree = lookup_tree(&empty_tree_oid);
 
        fp = xfopen(am_path(state, "patch"), "w");
        init_revisions(&rev_info, NULL);
@@ -1488,7 +1475,7 @@ static int parse_mail_rebase(struct am_state *state, const char *mail)
        if (get_mail_commit_oid(&commit_oid, mail) < 0)
                die(_("could not parse %s"), mail);
 
-       commit = lookup_commit_or_die(commit_oid.hash, mail);
+       commit = lookup_commit_or_die(&commit_oid, mail);
 
        get_commit_info(state, commit);
 
@@ -1618,7 +1605,7 @@ static int fall_back_threeway(const struct am_state *state, const char *index_pa
                init_revisions(&rev_info, NULL);
                rev_info.diffopt.output_format = DIFF_FORMAT_NAME_STATUS;
                diff_opt_parse(&rev_info.diffopt, &diff_filter_str, 1, rev_info.prefix);
-               add_pending_sha1(&rev_info, "HEAD", our_tree.hash, 0);
+               add_pending_oid(&rev_info, "HEAD", &our_tree, 0);
                diff_setup_done(&rev_info.diffopt);
                run_diff_index(&rev_info, 1);
        }
@@ -1683,7 +1670,7 @@ static void do_commit(const struct am_state *state)
 
        if (!get_sha1_commit("HEAD", parent.hash)) {
                old_oid = &parent;
-               commit_list_insert(lookup_commit(parent.hash), &parents);
+               commit_list_insert(lookup_commit(&parent), &parents);
        } else {
                old_oid = NULL;
                say(state, stderr, _("applying to an empty history"));
@@ -2046,11 +2033,11 @@ static int clean_index(const struct object_id *head, const struct object_id *rem
        struct tree *head_tree, *remote_tree, *index_tree;
        struct object_id index;
 
-       head_tree = parse_tree_indirect(head->hash);
+       head_tree = parse_tree_indirect(head);
        if (!head_tree)
                return error(_("Could not parse object '%s'."), oid_to_hex(head));
 
-       remote_tree = parse_tree_indirect(remote->hash);
+       remote_tree = parse_tree_indirect(remote);
        if (!remote_tree)
                return error(_("Could not parse object '%s'."), oid_to_hex(remote));
 
@@ -2062,7 +2049,7 @@ static int clean_index(const struct object_id *head, const struct object_id *rem
        if (write_cache_as_tree(index.hash, 0, NULL))
                return -1;
 
-       index_tree = parse_tree_indirect(index.hash);
+       index_tree = parse_tree_indirect(&index);
        if (!index_tree)
                return error(_("Could not parse object '%s'."), oid_to_hex(&index));
 
@@ -2157,7 +2144,7 @@ static void am_abort(struct am_state *state)
        am_rerere_clear();
 
        curr_branch = resolve_refdup("HEAD", 0, curr_head.hash, NULL);
-       has_curr_head = !is_null_oid(&curr_head);
+       has_curr_head = curr_branch && !is_null_oid(&curr_head);
        if (!has_curr_head)
                hashcpy(curr_head.hash, EMPTY_TREE_SHA1_BIN);
 
@@ -2320,6 +2307,9 @@ int cmd_am(int argc, const char **argv, const char *prefix)
                OPT_END()
        };
 
+       if (argc == 2 && !strcmp(argv[1], "-h"))
+               usage_with_options(usage, options);
+
        git_config(git_am_config, NULL);
 
        am_state_init(&state);