Skip to content

Commit 774751a

Browse files
committed
Re-fix "builtin-commit: fix --signoff"
An earlier fix to the said commit was incomplete; it mixed up the meaning of the flag parameter passed to the internal fmt_ident() function, so this corrects it. git_author_info() and git_committer_info() can be told to issue a warning when no usable user information is found, and optionally can be told to error out. Operations that actually use the information to record a new commit or a tag will still error out, but the caller to leave reflog record will just silently use bogus user information. Not warning on misconfigured user information while writing a reflog entry is somewhat debatable, but it is probably nicer to the users to silently let it pass, because the only information you are losing is who checked out the branch. * git_author_info() and git_committer_info() used to take 1 (positive int) to error out with a warning on misconfiguration; this is now signalled with a symbolic constant IDENT_ERROR_ON_NO_NAME. * These functions used to take -1 (negative int) to warn but continue; this is now signalled with a symbolic constant IDENT_WARN_ON_NO_NAME. * fmt_ident() function implements the above error reporting behaviour common to git_author_info() and git_committer_info(). A symbolic constant IDENT_NO_DATE can be or'ed in to the flag parameter to make it return only the "Name <email@address.xz>". * fmt_name() is a thin wrapper around fmt_ident() that always passes IDENT_ERROR_ON_NO_NAME and IDENT_NO_DATE. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 264474f commit 774751a

File tree

8 files changed

+26
-29
lines changed

8 files changed

+26
-29
lines changed

builtin-commit-tree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
9898
strbuf_addf(&buffer, "parent %s\n", sha1_to_hex(parent_sha1[i]));
9999

100100
/* Person/date information */
101-
strbuf_addf(&buffer, "author %s\n", git_author_info(1));
102-
strbuf_addf(&buffer, "committer %s\n", git_committer_info(1));
101+
strbuf_addf(&buffer, "author %s\n", git_author_info(IDENT_ERROR_ON_NO_NAME));
102+
strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
103103
if (!encoding_is_utf8)
104104
strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
105105
strbuf_addch(&buffer, '\n');

builtin-commit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ static void determine_author_info(struct strbuf *sb)
497497
email = xstrndup(lb + 2, rb - (lb + 2));
498498
}
499499

500-
strbuf_addf(sb, "author %s\n", fmt_ident(name, email, date, 1));
500+
strbuf_addf(sb, "author %s\n", fmt_ident(name, email, date, IDENT_ERROR_ON_NO_NAME));
501501
}
502502

503503
static int parse_and_validate_options(int argc, const char *argv[],
@@ -776,7 +776,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
776776
}
777777

778778
determine_author_info(&sb);
779-
strbuf_addf(&sb, "committer %s\n", git_committer_info(1));
779+
strbuf_addf(&sb, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
780780
if (!is_encoding_utf8(git_commit_encoding))
781781
strbuf_addf(&sb, "encoding %s\n", git_commit_encoding);
782782
strbuf_addch(&sb, '\n');

builtin-log.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const cha
557557

558558
static void gen_message_id(char *dest, unsigned int length, char *base)
559559
{
560-
const char *committer = git_committer_info(-1);
560+
const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
561561
const char *email_start = strrchr(committer, '<');
562562
const char *email_end = strrchr(committer, '>');
563563
if(!email_start || !email_end || email_start > email_end - 1)
@@ -665,7 +665,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
665665
!strcmp(argv[i], "-s")) {
666666
const char *committer;
667667
const char *endpos;
668-
committer = git_committer_info(1);
668+
committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
669669
endpos = strchr(committer, '>');
670670
if (!endpos)
671671
die("bogos committer info %s\n", committer);

builtin-tag.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ static int do_sign(struct strbuf *buffer)
188188
int len;
189189

190190
if (!*signingkey) {
191-
if (strlcpy(signingkey, git_committer_info(1),
191+
if (strlcpy(signingkey, git_committer_info(IDENT_ERROR_ON_NO_NAME),
192192
sizeof(signingkey)) > sizeof(signingkey) - 1)
193193
return error("committer info too long.");
194194
bracket = strchr(signingkey, '>');
@@ -298,7 +298,7 @@ static void create_tag(const unsigned char *object, const char *tag,
298298
sha1_to_hex(object),
299299
typename(type),
300300
tag,
301-
git_committer_info(1));
301+
git_committer_info(IDENT_ERROR_ON_NO_NAME));
302302

303303
if (header_len > sizeof(header_buf) - 1)
304304
die("tag header too big.");

cache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,9 @@ void datestamp(char *buf, int bufsize);
453453
unsigned long approxidate(const char *);
454454
enum date_mode parse_date_format(const char *format);
455455

456+
#define IDENT_WARN_ON_NO_NAME 1
457+
#define IDENT_ERROR_ON_NO_NAME 2
458+
#define IDENT_NO_DATE 4
456459
extern const char *git_author_info(int);
457460
extern const char *git_committer_info(int);
458461
extern const char *fmt_ident(const char *name, const char *email, const char *date_str, int);

ident.c

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,15 @@ static const char *env_hint =
182182
"Omit --global to set the identity only in this repository.\n"
183183
"\n";
184184

185-
static const char *fmt_ident_1(const char *name, const char *email,
186-
const char *date_str, int flag)
185+
const char *fmt_ident(const char *name, const char *email,
186+
const char *date_str, int flag)
187187
{
188188
static char buffer[1000];
189189
char date[50];
190190
int i;
191-
int error_on_no_name = !!(flag & 01);
192-
int name_addr_only = !!(flag & 02);
191+
int error_on_no_name = (flag & IDENT_ERROR_ON_NO_NAME);
192+
int warn_on_no_name = (flag & IDENT_WARN_ON_NO_NAME);
193+
int name_addr_only = (flag & IDENT_NO_DATE);
193194

194195
setup_ident();
195196
if (!name)
@@ -200,12 +201,12 @@ static const char *fmt_ident_1(const char *name, const char *email,
200201
if (!*name) {
201202
struct passwd *pw;
202203

203-
if (0 <= error_on_no_name &&
204+
if ((warn_on_no_name || error_on_no_name) &&
204205
name == git_default_name && env_hint) {
205206
fprintf(stderr, env_hint, au_env, co_env);
206207
env_hint = NULL; /* warn only once, for "git-var -l" */
207208
}
208-
if (0 < error_on_no_name)
209+
if (error_on_no_name)
209210
die("empty ident %s <%s> not allowed", name, email);
210211
pw = getpwuid(getuid());
211212
if (!pw)
@@ -234,30 +235,23 @@ static const char *fmt_ident_1(const char *name, const char *email,
234235
return buffer;
235236
}
236237

237-
const char *fmt_ident(const char *name, const char *email,
238-
const char *date_str, int error_on_no_name)
239-
{
240-
int flag = (error_on_no_name ? 01 : 0);
241-
return fmt_ident_1(name, email, date_str, flag);
242-
}
243-
244238
const char *fmt_name(const char *name, const char *email)
245239
{
246-
return fmt_ident_1(name, email, NULL, 03);
240+
return fmt_ident(name, email, NULL, IDENT_ERROR_ON_NO_NAME | IDENT_NO_DATE);
247241
}
248242

249-
const char *git_author_info(int error_on_no_name)
243+
const char *git_author_info(int flag)
250244
{
251245
return fmt_ident(getenv("GIT_AUTHOR_NAME"),
252246
getenv("GIT_AUTHOR_EMAIL"),
253247
getenv("GIT_AUTHOR_DATE"),
254-
error_on_no_name);
248+
flag);
255249
}
256250

257-
const char *git_committer_info(int error_on_no_name)
251+
const char *git_committer_info(int flag)
258252
{
259253
return fmt_ident(getenv("GIT_COMMITTER_NAME"),
260254
getenv("GIT_COMMITTER_EMAIL"),
261255
getenv("GIT_COMMITTER_DATE"),
262-
error_on_no_name);
256+
flag);
263257
}

refs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
10941094
adjust_shared_perm(log_file);
10951095

10961096
msglen = msg ? strlen(msg) : 0;
1097-
committer = git_committer_info(-1);
1097+
committer = git_committer_info(0);
10981098
maxlen = strlen(committer) + msglen + 100;
10991099
logrec = xmalloc(maxlen);
11001100
len = sprintf(logrec, "%s %s %s\n",

var.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static void list_vars(void)
2121
{
2222
struct git_var *ptr;
2323
for(ptr = git_vars; ptr->read; ptr++) {
24-
printf("%s=%s\n", ptr->name, ptr->read(0));
24+
printf("%s=%s\n", ptr->name, ptr->read(IDENT_WARN_ON_NO_NAME));
2525
}
2626
}
2727

@@ -32,7 +32,7 @@ static const char *read_var(const char *var)
3232
val = NULL;
3333
for(ptr = git_vars; ptr->read; ptr++) {
3434
if (strcmp(var, ptr->name) == 0) {
35-
val = ptr->read(1);
35+
val = ptr->read(IDENT_ERROR_ON_NO_NAME);
3636
break;
3737
}
3838
}

0 commit comments

Comments
 (0)