Skip to content

Commit b66ae79

Browse files
committed
Merge branch 'sb/committer'
* sb/committer: commit: Show committer if automatic commit: Show author if different from committer Preparation to call determine_author_info from prepare_to_commit
2 parents 761adeb + bb1ae3f commit b66ae79

File tree

6 files changed

+109
-40
lines changed

6 files changed

+109
-40
lines changed

builtin-commit.c

Lines changed: 73 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ static enum {
4747

4848
static char *logfile, *force_author, *template_file;
4949
static char *edit_message, *use_message;
50+
static char *author_name, *author_email, *author_date;
5051
static int all, edit_flag, also, interactive, only, amend, signoff;
5152
static int quiet, verbose, untracked_files, no_verify, allow_empty;
5253
/*
@@ -397,6 +398,47 @@ static int is_a_merge(const unsigned char *sha1)
397398

398399
static const char sign_off_header[] = "Signed-off-by: ";
399400

401+
static void determine_author_info(void)
402+
{
403+
char *name, *email, *date;
404+
405+
name = getenv("GIT_AUTHOR_NAME");
406+
email = getenv("GIT_AUTHOR_EMAIL");
407+
date = getenv("GIT_AUTHOR_DATE");
408+
409+
if (use_message) {
410+
const char *a, *lb, *rb, *eol;
411+
412+
a = strstr(use_message_buffer, "\nauthor ");
413+
if (!a)
414+
die("invalid commit: %s", use_message);
415+
416+
lb = strstr(a + 8, " <");
417+
rb = strstr(a + 8, "> ");
418+
eol = strchr(a + 8, '\n');
419+
if (!lb || !rb || !eol)
420+
die("invalid commit: %s", use_message);
421+
422+
name = xstrndup(a + 8, lb - (a + 8));
423+
email = xstrndup(lb + 2, rb - (lb + 2));
424+
date = xstrndup(rb + 2, eol - (rb + 2));
425+
}
426+
427+
if (force_author) {
428+
const char *lb = strstr(force_author, " <");
429+
const char *rb = strchr(force_author, '>');
430+
431+
if (!lb || !rb)
432+
die("malformed --author parameter");
433+
name = xstrndup(force_author, lb - force_author);
434+
email = xstrndup(lb + 2, rb - (lb + 2));
435+
}
436+
437+
author_name = name;
438+
author_email = email;
439+
author_date = date;
440+
}
441+
400442
static int prepare_to_commit(const char *index_file, const char *prefix)
401443
{
402444
struct stat statbuf;
@@ -406,6 +448,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
406448
FILE *fp;
407449
const char *hook_arg1 = NULL;
408450
const char *hook_arg2 = NULL;
451+
int ident_shown = 0;
409452

410453
if (!no_verify && run_hook(index_file, "pre-commit", NULL))
411454
return 0;
@@ -485,7 +528,14 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
485528

486529
strbuf_release(&sb);
487530

531+
determine_author_info();
532+
533+
/* This checks if committer ident is explicitly given */
534+
git_committer_info(0);
488535
if (use_editor) {
536+
char *author_ident;
537+
const char *committer_ident;
538+
489539
if (in_merge)
490540
fprintf(fp,
491541
"#\n"
@@ -508,6 +558,27 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
508558
if (only_include_assumed)
509559
fprintf(fp, "# %s\n", only_include_assumed);
510560

561+
author_ident = xstrdup(fmt_name(author_name, author_email));
562+
committer_ident = fmt_name(getenv("GIT_COMMITTER_NAME"),
563+
getenv("GIT_COMMITTER_EMAIL"));
564+
if (strcmp(author_ident, committer_ident))
565+
fprintf(fp,
566+
"%s"
567+
"# Author: %s\n",
568+
ident_shown++ ? "" : "#\n",
569+
author_ident);
570+
free(author_ident);
571+
572+
if (!user_ident_explicitly_given)
573+
fprintf(fp,
574+
"%s"
575+
"# Committer: %s\n",
576+
ident_shown++ ? "" : "#\n",
577+
committer_ident);
578+
579+
if (ident_shown)
580+
fprintf(fp, "#\n");
581+
511582
saved_color_setting = wt_status_use_color;
512583
wt_status_use_color = 0;
513584
commitable = run_status(fp, index_file, prefix, 1);
@@ -624,45 +695,6 @@ static int message_is_empty(struct strbuf *sb, int start)
624695
return 1;
625696
}
626697

627-
static void determine_author_info(struct strbuf *sb)
628-
{
629-
char *name, *email, *date;
630-
631-
name = getenv("GIT_AUTHOR_NAME");
632-
email = getenv("GIT_AUTHOR_EMAIL");
633-
date = getenv("GIT_AUTHOR_DATE");
634-
635-
if (use_message) {
636-
const char *a, *lb, *rb, *eol;
637-
638-
a = strstr(use_message_buffer, "\nauthor ");
639-
if (!a)
640-
die("invalid commit: %s", use_message);
641-
642-
lb = strstr(a + 8, " <");
643-
rb = strstr(a + 8, "> ");
644-
eol = strchr(a + 8, '\n');
645-
if (!lb || !rb || !eol)
646-
die("invalid commit: %s", use_message);
647-
648-
name = xstrndup(a + 8, lb - (a + 8));
649-
email = xstrndup(lb + 2, rb - (lb + 2));
650-
date = xstrndup(rb + 2, eol - (rb + 2));
651-
}
652-
653-
if (force_author) {
654-
const char *lb = strstr(force_author, " <");
655-
const char *rb = strchr(force_author, '>');
656-
657-
if (!lb || !rb)
658-
die("malformed --author parameter");
659-
name = xstrndup(force_author, lb - force_author);
660-
email = xstrndup(lb + 2, rb - (lb + 2));
661-
}
662-
663-
strbuf_addf(sb, "author %s\n", fmt_ident(name, email, date, IDENT_ERROR_ON_NO_NAME));
664-
}
665-
666698
static int parse_and_validate_options(int argc, const char *argv[],
667699
const char * const usage[])
668700
{
@@ -922,7 +954,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
922954
strbuf_addf(&sb, "parent %s\n", sha1_to_hex(head_sha1));
923955
}
924956

925-
determine_author_info(&sb);
957+
strbuf_addf(&sb, "author %s\n",
958+
fmt_ident(author_name, author_email, author_date, IDENT_ERROR_ON_NO_NAME));
926959
strbuf_addf(&sb, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
927960
if (!is_encoding_utf8(git_commit_encoding))
928961
strbuf_addf(&sb, "encoding %s\n", git_commit_encoding);

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,7 @@ extern int config_error_nonbool(const char *);
735735
#define MAX_GITNAME (1000)
736736
extern char git_default_email[MAX_GITNAME];
737737
extern char git_default_name[MAX_GITNAME];
738+
extern int user_ident_explicitly_given;
738739

739740
extern const char *git_commit_encoding;
740741
extern const char *git_log_output_encoding;

config.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,13 +448,17 @@ int git_default_config(const char *var, const char *value)
448448
if (!value)
449449
return config_error_nonbool(var);
450450
strlcpy(git_default_name, value, sizeof(git_default_name));
451+
if (git_default_email[0])
452+
user_ident_explicitly_given = 1;
451453
return 0;
452454
}
453455

454456
if (!strcmp(var, "user.email")) {
455457
if (!value)
456458
return config_error_nonbool(var);
457459
strlcpy(git_default_email, value, sizeof(git_default_email));
460+
if (git_default_name[0])
461+
user_ident_explicitly_given = 1;
458462
return 0;
459463
}
460464

environment.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
char git_default_email[MAX_GITNAME];
1313
char git_default_name[MAX_GITNAME];
14+
int user_ident_explicitly_given;
1415
int trust_executable_bit = 1;
1516
int quote_path_fully = 1;
1617
int has_symlinks = 1;

ident.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ const char *git_author_info(int flag)
250250

251251
const char *git_committer_info(int flag)
252252
{
253+
if (getenv("GIT_COMMITTER_NAME") &&
254+
getenv("GIT_COMMITTER_EMAIL"))
255+
user_ident_explicitly_given = 1;
253256
return fmt_ident(getenv("GIT_COMMITTER_NAME"),
254257
getenv("GIT_COMMITTER_EMAIL"),
255258
getenv("GIT_COMMITTER_DATE"),

t/t7502-commit.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,33 @@ test_expect_success 'cleanup commit messages (strip,-F,-e)' '
154154
155155
'
156156

157+
echo "#
158+
# Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
159+
#" >> expect
160+
161+
test_expect_success 'author different from committer' '
162+
163+
echo >>negative &&
164+
git commit -e -m "sample"
165+
head -n 7 .git/COMMIT_EDITMSG >actual &&
166+
test_cmp expect actual
167+
'
168+
169+
sed -i '$d' expect
170+
echo "# Committer:
171+
#" >> expect
172+
unset GIT_COMMITTER_EMAIL
173+
unset GIT_COMMITTER_NAME
174+
175+
test_expect_success 'committer is automatic' '
176+
177+
echo >>negative &&
178+
git commit -e -m "sample"
179+
head -n 8 .git/COMMIT_EDITMSG | \
180+
sed "s/^# Committer: .*/# Committer:/" >actual &&
181+
test_cmp expect actual
182+
'
183+
157184
pwd=`pwd`
158185
cat >> .git/FAKE_EDITOR << EOF
159186
#! /bin/sh

0 commit comments

Comments
 (0)