Skip to content

Commit bb1ae3f

Browse files
sbejargitster
authored andcommitted
commit: Show committer if automatic
To warn the user in case he/she might be using an unintended committer identity. Signed-off-by: Santi Béjar <sbejar@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent e83dbe8 commit bb1ae3f

File tree

6 files changed

+40
-3
lines changed

6 files changed

+40
-3
lines changed

builtin-commit.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
446446
FILE *fp;
447447
const char *hook_arg1 = NULL;
448448
const char *hook_arg2 = NULL;
449+
int ident_shown = 0;
449450

450451
if (!no_verify && run_hook(index_file, "pre-commit", NULL))
451452
return 0;
@@ -527,6 +528,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
527528

528529
determine_author_info();
529530

531+
/* This checks if committer ident is explicitly given */
532+
git_committer_info(0);
530533
if (use_editor) {
531534
char *author_ident;
532535
const char *committer_ident;
@@ -558,12 +561,22 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
558561
getenv("GIT_COMMITTER_EMAIL"));
559562
if (strcmp(author_ident, committer_ident))
560563
fprintf(fp,
561-
"#\n"
562-
"# Author: %s\n"
563-
"#\n",
564+
"%s"
565+
"# Author: %s\n",
566+
ident_shown++ ? "" : "#\n",
564567
author_ident);
565568
free(author_ident);
566569

570+
if (!user_ident_explicitly_given)
571+
fprintf(fp,
572+
"%s"
573+
"# Committer: %s\n",
574+
ident_shown++ ? "" : "#\n",
575+
committer_ident);
576+
577+
if (ident_shown)
578+
fprintf(fp, "#\n");
579+
567580
saved_color_setting = wt_status_use_color;
568581
wt_status_use_color = 0;
569582
commitable = run_status(fp, index_file, prefix, 1);

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,7 @@ extern int config_error_nonbool(const char *);
719719
#define MAX_GITNAME (1000)
720720
extern char git_default_email[MAX_GITNAME];
721721
extern char git_default_name[MAX_GITNAME];
722+
extern int user_ident_explicitly_given;
722723

723724
extern const char *git_commit_encoding;
724725
extern const char *git_log_output_encoding;

config.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,13 +443,17 @@ int git_default_config(const char *var, const char *value)
443443
if (!value)
444444
return config_error_nonbool(var);
445445
strlcpy(git_default_name, value, sizeof(git_default_name));
446+
if (git_default_email[0])
447+
user_ident_explicitly_given = 1;
446448
return 0;
447449
}
448450

449451
if (!strcmp(var, "user.email")) {
450452
if (!value)
451453
return config_error_nonbool(var);
452454
strlcpy(git_default_email, value, sizeof(git_default_email));
455+
if (git_default_name[0])
456+
user_ident_explicitly_given = 1;
453457
return 0;
454458
}
455459

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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,21 @@ test_expect_success 'author different from committer' '
166166
test_cmp expect actual
167167
'
168168

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+
169184
pwd=`pwd`
170185
cat >> .git/FAKE_EDITOR << EOF
171186
#! /bin/sh

0 commit comments

Comments
 (0)