@matheusbsbs / matheus-bagliano / commits / 4c28e4ada0
Fork 0

commit: die before asking to edit the log message

When determine_author_info() returns to the calling prepare_to_commit(), we already know the pieces of information necessary to determine what author ident will be used in the final message, but deferred making a call to fmt_ident() before the final commit_tree(). Most importantly, we would open the editor to ask the user to compose the log message before it. As one important side effect of fmt_ident() is to error out when the given information is malformed, this resulted in us spawning the editor first and then refusing to commit due to error, even though we had enough information to detect the error before starting the editor, which was annoying. Move the fmt_ident() call to the end of determine_author_info() where we have final determination of author info to rectify this. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Dec 20, 2010 at 17:00 UTC 4c28e4ada03f5440251545cf91e0d81bce9b010d
1 file changed +32 -22
builtin/commit.c
+32 -22
@@ -69,7 +69,6 @@ static enum {
69 static const char *logfile, *force_author;
70 static const char *template_file;
71 static char *edit_message, *use_message;
72 -static char *author_name, *author_email, *author_date;
72 static int all, edit_flag, also, interactive, only, amend, signoff;
73 static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
74 static int no_post_rewrite, allow_empty_message;
@@ -459,7 +458,7 @@ static int is_a_merge(const unsigned char *sha1)
458
459 static const char sign_off_header[] = "Signed-off-by: ";
460
462 -static void determine_author_info(void)
461 +static void determine_author_info(struct strbuf *author_ident)
462 {
463 char *name, *email, *date;
464
@@ -503,10 +502,8 @@ static void determine_author_info(void)
502
503 if (force_date)
504 date = force_date;
506 -
507 - author_name = name;
508 - author_email = email;
509 - author_date = date;
505 + strbuf_addstr(author_ident, fmt_ident(name, email, date,
506 + IDENT_ERROR_ON_NO_NAME));
507 }
508
509 static int ends_rfc2822_footer(struct strbuf *sb)
@@ -550,10 +547,21 @@ static int ends_rfc2822_footer(struct strbuf *sb)
547 return 1;
548 }
549
550 +static char *cut_ident_timestamp_part(char *string)
551 +{
552 + char *ket = strrchr(string, '>');
553 + if (!ket || ket[1] != ' ')
554 + die("Malformed ident string: '%s'", string);
555 + *++ket = '\0';
556 + return ket;
557 +}
558 +
559 static int prepare_to_commit(const char *index_file, const char *prefix,
554 - struct wt_status *s)
560 + struct wt_status *s,
561 + struct strbuf *author_ident)
562 {
563 struct stat statbuf;
564 + struct strbuf committer_ident = STRBUF_INIT;
565 int commitable, saved_color_setting;
566 struct strbuf sb = STRBUF_INIT;
567 char *buffer;
@@ -637,14 +645,13 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
645
646 strbuf_release(&sb);
647
640 - determine_author_info();
648 + /* This checks and barfs if author is badly specified */
649 + determine_author_info(author_ident);
650
651 /* This checks if committer ident is explicitly given */
643 - git_committer_info(0);
652 + strbuf_addstr(&committer_ident, git_committer_info(0));
653 if (use_editor && include_status) {
645 - char *author_ident;
646 - const char *committer_ident;
647 -
654 + char *ai_tmp, *ci_tmp;
655 if (in_merge)
656 fprintf(fp,
657 "#\n"
@@ -672,23 +679,21 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
679 if (only_include_assumed)
680 fprintf(fp, "# %s\n", only_include_assumed);
681
675 - author_ident = xstrdup(fmt_name(author_name, author_email));
676 - committer_ident = fmt_name(getenv("GIT_COMMITTER_NAME"),
677 - getenv("GIT_COMMITTER_EMAIL"));
678 - if (strcmp(author_ident, committer_ident))
682 + ai_tmp = cut_ident_timestamp_part(author_ident->buf);
683 + ci_tmp = cut_ident_timestamp_part(committer_ident.buf);
684 + if (strcmp(author_ident->buf, committer_ident.buf))
685 fprintf(fp,
686 "%s"
687 "# Author: %s\n",
688 ident_shown++ ? "" : "#\n",
683 - author_ident);
684 - free(author_ident);
689 + author_ident->buf);
690
691 if (!user_ident_sufficiently_given())
692 fprintf(fp,
693 "%s"
694 "# Committer: %s\n",
695 ident_shown++ ? "" : "#\n",
691 - committer_ident);
696 + committer_ident.buf);
697
698 if (ident_shown)
699 fprintf(fp, "#\n");
@@ -697,6 +702,9 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
702 s->use_color = 0;
703 commitable = run_status(fp, index_file, prefix, 1, s);
704 s->use_color = saved_color_setting;
705 +
706 + *ai_tmp = ' ';
707 + *ci_tmp = ' ';
708 } else {
709 unsigned char sha1[20];
710 const char *parent = "HEAD";
@@ -712,6 +720,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
720 else
721 commitable = index_differs_from(parent, 0);
722 }
723 + strbuf_release(&committer_ident);
724
725 fclose(fp);
726
@@ -1246,6 +1255,7 @@ static int run_rewrite_hook(const unsigned char *oldsha1,
1255 int cmd_commit(int argc, const char **argv, const char *prefix)
1256 {
1257 struct strbuf sb = STRBUF_INIT;
1258 + struct strbuf author_ident = STRBUF_INIT;
1259 const char *index_file, *reflog_msg;
1260 char *nl, *p;
1261 unsigned char commit_sha1[20];
@@ -1273,7 +1283,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1283
1284 /* Set up everything for writing the commit object. This includes
1285 running hooks, writing the trees, and interacting with the user. */
1276 - if (!prepare_to_commit(index_file, prefix, &s)) {
1286 + if (!prepare_to_commit(index_file, prefix, &s, &author_ident)) {
1287 rollback_index_files();
1288 return 1;
1289 }
@@ -1352,11 +1362,11 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1362 }
1363
1364 if (commit_tree(sb.buf, active_cache_tree->sha1, parents, commit_sha1,
1355 - fmt_ident(author_name, author_email, author_date,
1356 - IDENT_ERROR_ON_NO_NAME))) {
1365 + author_ident.buf)) {
1366 rollback_index_files();
1367 die("failed to write commit object");
1368 }
1369 + strbuf_release(&author_ident);
1370
1371 ref_lock = lock_any_ref_for_update("HEAD",
1372 initial_commit ? NULL : head_sha1,