diff --git a/builtin-commit.c b/builtin-commit.c index a65c2b8c37c2f7785d76ec1f4e3692ee52dab8e9..0baec6db6ac76de81b93f4c990f8347814618be7 100644 --- a/builtin-commit.c +++ b/builtin-commit.c @@ -47,6 +47,7 @@ static enum { static char *logfile, *force_author, *template_file; static char *edit_message, *use_message; +static char *author_name, *author_email, *author_date; static int all, edit_flag, also, interactive, only, amend, signoff; static int quiet, verbose, untracked_files, no_verify, allow_empty; /* @@ -397,6 +398,47 @@ static int is_a_merge(const unsigned char *sha1) static const char sign_off_header[] = "Signed-off-by: "; +static void determine_author_info(void) +{ + char *name, *email, *date; + + name = getenv("GIT_AUTHOR_NAME"); + email = getenv("GIT_AUTHOR_EMAIL"); + date = getenv("GIT_AUTHOR_DATE"); + + if (use_message) { + const char *a, *lb, *rb, *eol; + + a = strstr(use_message_buffer, "\nauthor "); + if (!a) + die("invalid commit: %s", use_message); + + lb = strstr(a + 8, " <"); + rb = strstr(a + 8, "> "); + eol = strchr(a + 8, '\n'); + if (!lb || !rb || !eol) + die("invalid commit: %s", use_message); + + name = xstrndup(a + 8, lb - (a + 8)); + email = xstrndup(lb + 2, rb - (lb + 2)); + date = xstrndup(rb + 2, eol - (rb + 2)); + } + + if (force_author) { + const char *lb = strstr(force_author, " <"); + const char *rb = strchr(force_author, '>'); + + if (!lb || !rb) + die("malformed --author parameter"); + name = xstrndup(force_author, lb - force_author); + email = xstrndup(lb + 2, rb - (lb + 2)); + } + + author_name = name; + author_email = email; + author_date = date; +} + static int prepare_to_commit(const char *index_file, const char *prefix) { struct stat statbuf; @@ -406,6 +448,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix) FILE *fp; const char *hook_arg1 = NULL; const char *hook_arg2 = NULL; + int ident_shown = 0; if (!no_verify && run_hook(index_file, "pre-commit", NULL)) return 0; @@ -485,7 +528,14 @@ static int prepare_to_commit(const char *index_file, const char *prefix) strbuf_release(&sb); + determine_author_info(); + + /* This checks if committer ident is explicitly given */ + git_committer_info(0); if (use_editor) { + char *author_ident; + const char *committer_ident; + if (in_merge) fprintf(fp, "#\n" @@ -508,6 +558,27 @@ static int prepare_to_commit(const char *index_file, const char *prefix) if (only_include_assumed) fprintf(fp, "# %s\n", only_include_assumed); + author_ident = xstrdup(fmt_name(author_name, author_email)); + committer_ident = fmt_name(getenv("GIT_COMMITTER_NAME"), + getenv("GIT_COMMITTER_EMAIL")); + if (strcmp(author_ident, committer_ident)) + fprintf(fp, + "%s" + "# Author: %s\n", + ident_shown++ ? "" : "#\n", + author_ident); + free(author_ident); + + if (!user_ident_explicitly_given) + fprintf(fp, + "%s" + "# Committer: %s\n", + ident_shown++ ? "" : "#\n", + committer_ident); + + if (ident_shown) + fprintf(fp, "#\n"); + saved_color_setting = wt_status_use_color; wt_status_use_color = 0; commitable = run_status(fp, index_file, prefix, 1); @@ -624,45 +695,6 @@ static int message_is_empty(struct strbuf *sb, int start) return 1; } -static void determine_author_info(struct strbuf *sb) -{ - char *name, *email, *date; - - name = getenv("GIT_AUTHOR_NAME"); - email = getenv("GIT_AUTHOR_EMAIL"); - date = getenv("GIT_AUTHOR_DATE"); - - if (use_message) { - const char *a, *lb, *rb, *eol; - - a = strstr(use_message_buffer, "\nauthor "); - if (!a) - die("invalid commit: %s", use_message); - - lb = strstr(a + 8, " <"); - rb = strstr(a + 8, "> "); - eol = strchr(a + 8, '\n'); - if (!lb || !rb || !eol) - die("invalid commit: %s", use_message); - - name = xstrndup(a + 8, lb - (a + 8)); - email = xstrndup(lb + 2, rb - (lb + 2)); - date = xstrndup(rb + 2, eol - (rb + 2)); - } - - if (force_author) { - const char *lb = strstr(force_author, " <"); - const char *rb = strchr(force_author, '>'); - - if (!lb || !rb) - die("malformed --author parameter"); - name = xstrndup(force_author, lb - force_author); - email = xstrndup(lb + 2, rb - (lb + 2)); - } - - strbuf_addf(sb, "author %s\n", fmt_ident(name, email, date, IDENT_ERROR_ON_NO_NAME)); -} - static int parse_and_validate_options(int argc, const char *argv[], const char * const usage[]) { @@ -922,7 +954,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix) strbuf_addf(&sb, "parent %s\n", sha1_to_hex(head_sha1)); } - determine_author_info(&sb); + strbuf_addf(&sb, "author %s\n", + fmt_ident(author_name, author_email, author_date, IDENT_ERROR_ON_NO_NAME)); strbuf_addf(&sb, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME)); if (!is_encoding_utf8(git_commit_encoding)) strbuf_addf(&sb, "encoding %s\n", git_commit_encoding); diff --git a/cache.h b/cache.h index 9cee9a5f3fb2e51f617dcf9f92defcd596b97c10..093f04cec0459e67c54f3fc5b536f59fb0f343dc 100644 --- a/cache.h +++ b/cache.h @@ -735,6 +735,7 @@ extern int config_error_nonbool(const char *); #define MAX_GITNAME (1000) extern char git_default_email[MAX_GITNAME]; extern char git_default_name[MAX_GITNAME]; +extern int user_ident_explicitly_given; extern const char *git_commit_encoding; extern const char *git_log_output_encoding; diff --git a/config.c b/config.c index e42ada78f8b10e7fb5600a6c99f81f27accd42fa..5195de10c9289a25053756ce799e654d5e01483e 100644 --- a/config.c +++ b/config.c @@ -448,6 +448,8 @@ int git_default_config(const char *var, const char *value) if (!value) return config_error_nonbool(var); strlcpy(git_default_name, value, sizeof(git_default_name)); + if (git_default_email[0]) + user_ident_explicitly_given = 1; return 0; } @@ -455,6 +457,8 @@ int git_default_config(const char *var, const char *value) if (!value) return config_error_nonbool(var); strlcpy(git_default_email, value, sizeof(git_default_email)); + if (git_default_name[0]) + user_ident_explicitly_given = 1; return 0; } diff --git a/environment.c b/environment.c index 55c252706317818db64af8ff3fbfde6cd23e3488..4fcb471248fb5276a68b9f86a77a2f5cde7dd884 100644 --- a/environment.c +++ b/environment.c @@ -11,6 +11,7 @@ char git_default_email[MAX_GITNAME]; char git_default_name[MAX_GITNAME]; +int user_ident_explicitly_given; int trust_executable_bit = 1; int quote_path_fully = 1; int has_symlinks = 1; diff --git a/ident.c b/ident.c index ed44a5345a47d4843e8328c0f879ac925dde2b07..b35504a8d25594a8d243ae7490733eae5a262712 100644 --- a/ident.c +++ b/ident.c @@ -250,6 +250,9 @@ const char *git_author_info(int flag) const char *git_committer_info(int flag) { + if (getenv("GIT_COMMITTER_NAME") && + getenv("GIT_COMMITTER_EMAIL")) + user_ident_explicitly_given = 1; return fmt_ident(getenv("GIT_COMMITTER_NAME"), getenv("GIT_COMMITTER_EMAIL"), getenv("GIT_COMMITTER_DATE"), diff --git a/t/t7502-commit.sh b/t/t7502-commit.sh index 284c9412474754be649ccc51df996e3e370ac6ec..018060c60f590946c8e02181bfb1f572972bb787 100755 --- a/t/t7502-commit.sh +++ b/t/t7502-commit.sh @@ -154,6 +154,33 @@ test_expect_success 'cleanup commit messages (strip,-F,-e)' ' ' +echo "# +# Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> +#" >> expect + +test_expect_success 'author different from committer' ' + + echo >>negative && + git commit -e -m "sample" + head -n 7 .git/COMMIT_EDITMSG >actual && + test_cmp expect actual +' + +sed -i '$d' expect +echo "# Committer: +#" >> expect +unset GIT_COMMITTER_EMAIL +unset GIT_COMMITTER_NAME + +test_expect_success 'committer is automatic' ' + + echo >>negative && + git commit -e -m "sample" + head -n 8 .git/COMMIT_EDITMSG | \ + sed "s/^# Committer: .*/# Committer:/" >actual && + test_cmp expect actual +' + pwd=`pwd` cat >> .git/FAKE_EDITOR << EOF #! /bin/sh