Skip to content

Commit ea02ffa

Browse files
apelissegitster
authored andcommitted
mailmap: simplify map_user() interface
Simplify map_user(), mostly to avoid copies of string buffers. It also simplifies caller functions. map_user() directly receive pointers and length from the commit buffer as mail and name. If mapping of the user and mail can be done, the pointer is updated to a new location. Lengths are also updated if necessary. The caller of map_user() can then copy the new email and name if necessary. Signed-off-by: Antoine Pelisse <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 388c7f8 commit ea02ffa

File tree

5 files changed

+126
-144
lines changed

5 files changed

+126
-144
lines changed

builtin/blame.c

Lines changed: 81 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,31 +1321,31 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
13211321
* Information on commits, used for output.
13221322
*/
13231323
struct commit_info {
1324-
const char *author;
1325-
const char *author_mail;
1324+
struct strbuf author;
1325+
struct strbuf author_mail;
13261326
unsigned long author_time;
1327-
const char *author_tz;
1327+
struct strbuf author_tz;
13281328

13291329
/* filled only when asked for details */
1330-
const char *committer;
1331-
const char *committer_mail;
1330+
struct strbuf committer;
1331+
struct strbuf committer_mail;
13321332
unsigned long committer_time;
1333-
const char *committer_tz;
1333+
struct strbuf committer_tz;
13341334

1335-
const char *summary;
1335+
struct strbuf summary;
13361336
};
13371337

13381338
/*
13391339
* Parse author/committer line in the commit object buffer
13401340
*/
13411341
static void get_ac_line(const char *inbuf, const char *what,
1342-
int person_len, char *person,
1343-
int mail_len, char *mail,
1344-
unsigned long *time, const char **tz)
1342+
struct strbuf *name, struct strbuf *mail,
1343+
unsigned long *time, struct strbuf *tz)
13451344
{
13461345
struct ident_split ident;
1347-
int len, tzlen, maillen, namelen;
1348-
char *tmp, *endp, *mailpos;
1346+
size_t len, maillen, namelen;
1347+
char *tmp, *endp;
1348+
const char *namebuf, *mailbuf;
13491349

13501350
tmp = strstr(inbuf, what);
13511351
if (!tmp)
@@ -1356,51 +1356,61 @@ static void get_ac_line(const char *inbuf, const char *what,
13561356
len = strlen(tmp);
13571357
else
13581358
len = endp - tmp;
1359-
if (person_len <= len)
1360-
goto error_out;
13611359

13621360
if (split_ident_line(&ident, tmp, len)) {
13631361
error_out:
13641362
/* Ugh */
1365-
*tz = "(unknown)";
1366-
strcpy(person, *tz);
1367-
strcpy(mail, *tz);
1363+
tmp = "(unknown)";
1364+
strbuf_addstr(name, tmp);
1365+
strbuf_addstr(mail, tmp);
1366+
strbuf_addstr(tz, tmp);
13681367
*time = 0;
13691368
return;
13701369
}
13711370

13721371
namelen = ident.name_end - ident.name_begin;
1373-
memcpy(person, ident.name_begin, namelen);
1374-
person[namelen] = 0;
1372+
namebuf = ident.name_begin;
13751373

1376-
maillen = ident.mail_end - ident.mail_begin + 2;
1377-
memcpy(mail, ident.mail_begin - 1, maillen);
1378-
mail[maillen] = 0;
1374+
maillen = ident.mail_end - ident.mail_begin;
1375+
mailbuf = ident.mail_begin;
13791376

13801377
*time = strtoul(ident.date_begin, NULL, 10);
13811378

1382-
tzlen = ident.tz_end - ident.tz_begin;
1383-
1384-
/* Place tz at the end of person */
1385-
*tz = tmp = person + person_len - (tzlen + 1);
1386-
memcpy(tmp, ident.tz_begin, tzlen);
1387-
tmp[tzlen] = 0;
1388-
1389-
if (!mailmap.nr)
1390-
return;
1379+
len = ident.tz_end - ident.tz_begin;
1380+
strbuf_add(tz, ident.tz_begin, len);
13911381

13921382
/*
13931383
* Now, convert both name and e-mail using mailmap
13941384
*/
1395-
if (map_user(&mailmap, mail+1, mail_len-1, person, tmp-person-1)) {
1396-
/* Add a trailing '>' to email, since map_user returns plain emails
1397-
Note: It already has '<', since we replace from mail+1 */
1398-
mailpos = memchr(mail, '\0', mail_len);
1399-
if (mailpos && mailpos-mail < mail_len - 1) {
1400-
*mailpos = '>';
1401-
*(mailpos+1) = '\0';
1402-
}
1403-
}
1385+
map_user(&mailmap, &mailbuf, &maillen,
1386+
&namebuf, &namelen);
1387+
1388+
strbuf_addf(mail, "<%.*s>", (int)maillen, mailbuf);
1389+
strbuf_add(name, namebuf, namelen);
1390+
}
1391+
1392+
static void commit_info_init(struct commit_info *ci)
1393+
{
1394+
1395+
strbuf_init(&ci->author, 0);
1396+
strbuf_init(&ci->author_mail, 0);
1397+
strbuf_init(&ci->author_tz, 0);
1398+
strbuf_init(&ci->committer, 0);
1399+
strbuf_init(&ci->committer_mail, 0);
1400+
strbuf_init(&ci->committer_tz, 0);
1401+
strbuf_init(&ci->summary, 0);
1402+
}
1403+
1404+
static void commit_info_destroy(struct commit_info *ci)
1405+
{
1406+
1407+
strbuf_release(&ci->author);
1408+
strbuf_release(&ci->author_mail);
1409+
strbuf_release(&ci->author_tz);
1410+
strbuf_release(&ci->committer);
1411+
strbuf_release(&ci->committer_mail);
1412+
strbuf_release(&ci->committer_tz);
1413+
strbuf_release(&ci->summary);
14041414
}
14051415

14061416
static void get_commit_info(struct commit *commit,
@@ -1410,11 +1420,8 @@ static void get_commit_info(struct commit *commit,
14101420
int len;
14111421
const char *subject, *encoding;
14121422
char *reencoded, *message;
1413-
static char author_name[1024];
1414-
static char author_mail[1024];
1415-
static char committer_name[1024];
1416-
static char committer_mail[1024];
1417-
static char summary_buf[1024];
1423+
1424+
commit_info_init(ret);
14181425

14191426
/*
14201427
* We've operated without save_commit_buffer, so
@@ -1432,33 +1439,25 @@ static void get_commit_info(struct commit *commit,
14321439
encoding = get_log_output_encoding();
14331440
reencoded = logmsg_reencode(commit, encoding);
14341441
message = reencoded ? reencoded : commit->buffer;
1435-
ret->author = author_name;
1436-
ret->author_mail = author_mail;
14371442
get_ac_line(message, "\nauthor ",
1438-
sizeof(author_name), author_name,
1439-
sizeof(author_mail), author_mail,
1443+
&ret->author, &ret->author_mail,
14401444
&ret->author_time, &ret->author_tz);
14411445

14421446
if (!detailed) {
14431447
free(reencoded);
14441448
return;
14451449
}
14461450

1447-
ret->committer = committer_name;
1448-
ret->committer_mail = committer_mail;
14491451
get_ac_line(message, "\ncommitter ",
1450-
sizeof(committer_name), committer_name,
1451-
sizeof(committer_mail), committer_mail,
1452+
&ret->committer, &ret->committer_mail,
14521453
&ret->committer_time, &ret->committer_tz);
14531454

1454-
ret->summary = summary_buf;
14551455
len = find_commit_subject(message, &subject);
1456-
if (len && len < sizeof(summary_buf)) {
1457-
memcpy(summary_buf, subject, len);
1458-
summary_buf[len] = 0;
1459-
} else {
1460-
sprintf(summary_buf, "(%s)", sha1_to_hex(commit->object.sha1));
1461-
}
1456+
if (len)
1457+
strbuf_add(&ret->summary, subject, len);
1458+
else
1459+
strbuf_addf(&ret->summary, "(%s)", sha1_to_hex(commit->object.sha1));
1460+
14621461
free(reencoded);
14631462
}
14641463

@@ -1487,22 +1486,25 @@ static int emit_one_suspect_detail(struct origin *suspect, int repeat)
14871486

14881487
suspect->commit->object.flags |= METAINFO_SHOWN;
14891488
get_commit_info(suspect->commit, &ci, 1);
1490-
printf("author %s\n", ci.author);
1491-
printf("author-mail %s\n", ci.author_mail);
1489+
printf("author %s\n", ci.author.buf);
1490+
printf("author-mail %s\n", ci.author_mail.buf);
14921491
printf("author-time %lu\n", ci.author_time);
1493-
printf("author-tz %s\n", ci.author_tz);
1494-
printf("committer %s\n", ci.committer);
1495-
printf("committer-mail %s\n", ci.committer_mail);
1492+
printf("author-tz %s\n", ci.author_tz.buf);
1493+
printf("committer %s\n", ci.committer.buf);
1494+
printf("committer-mail %s\n", ci.committer_mail.buf);
14961495
printf("committer-time %lu\n", ci.committer_time);
1497-
printf("committer-tz %s\n", ci.committer_tz);
1498-
printf("summary %s\n", ci.summary);
1496+
printf("committer-tz %s\n", ci.committer_tz.buf);
1497+
printf("summary %s\n", ci.summary.buf);
14991498
if (suspect->commit->object.flags & UNINTERESTING)
15001499
printf("boundary\n");
15011500
if (suspect->previous) {
15021501
struct origin *prev = suspect->previous;
15031502
printf("previous %s ", sha1_to_hex(prev->commit->object.sha1));
15041503
write_name_quoted(prev->path, stdout, '\n');
15051504
}
1505+
1506+
commit_info_destroy(&ci);
1507+
15061508
return 1;
15071509
}
15081510

@@ -1689,11 +1691,11 @@ static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt)
16891691
if (opt & OUTPUT_ANNOTATE_COMPAT) {
16901692
const char *name;
16911693
if (opt & OUTPUT_SHOW_EMAIL)
1692-
name = ci.author_mail;
1694+
name = ci.author_mail.buf;
16931695
else
1694-
name = ci.author;
1696+
name = ci.author.buf;
16951697
printf("\t(%10s\t%10s\t%d)", name,
1696-
format_time(ci.author_time, ci.author_tz,
1698+
format_time(ci.author_time, ci.author_tz.buf,
16971699
show_raw_time),
16981700
ent->lno + 1 + cnt);
16991701
} else {
@@ -1712,14 +1714,14 @@ static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt)
17121714
const char *name;
17131715
int pad;
17141716
if (opt & OUTPUT_SHOW_EMAIL)
1715-
name = ci.author_mail;
1717+
name = ci.author_mail.buf;
17161718
else
1717-
name = ci.author;
1719+
name = ci.author.buf;
17181720
pad = longest_author - utf8_strwidth(name);
17191721
printf(" (%s%*s %10s",
17201722
name, pad, "",
17211723
format_time(ci.author_time,
1722-
ci.author_tz,
1724+
ci.author_tz.buf,
17231725
show_raw_time));
17241726
}
17251727
printf(" %*d) ",
@@ -1734,6 +1736,8 @@ static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt)
17341736

17351737
if (sb->final_buf_size && cp[-1] != '\n')
17361738
putchar('\n');
1739+
1740+
commit_info_destroy(&ci);
17371741
}
17381742

17391743
static void output(struct scoreboard *sb, int option)
@@ -1858,9 +1862,9 @@ static void find_alignment(struct scoreboard *sb, int *option)
18581862
suspect->commit->object.flags |= METAINFO_SHOWN;
18591863
get_commit_info(suspect->commit, &ci, 1);
18601864
if (*option & OUTPUT_SHOW_EMAIL)
1861-
num = utf8_strwidth(ci.author_mail);
1865+
num = utf8_strwidth(ci.author_mail.buf);
18621866
else
1863-
num = utf8_strwidth(ci.author);
1867+
num = utf8_strwidth(ci.author.buf);
18641868
if (longest_author < num)
18651869
longest_author = num;
18661870
}
@@ -1872,6 +1876,8 @@ static void find_alignment(struct scoreboard *sb, int *option)
18721876
longest_dst_lines = num;
18731877
if (largest_score < ent_score(sb, e))
18741878
largest_score = ent_score(sb, e);
1879+
1880+
commit_info_destroy(&ci);
18751881
}
18761882
max_orig_digits = decimal_width(longest_src_lines);
18771883
max_digits = decimal_width(longest_dst_lines);

builtin/shortlog.c

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,36 +36,28 @@ static void insert_one_record(struct shortlog *log,
3636
const char *dot3 = log->common_repo_prefix;
3737
char *buffer, *p;
3838
struct string_list_item *item;
39-
char namebuf[1024];
40-
char emailbuf[1024];
41-
size_t len;
39+
const char *mailbuf, *namebuf;
40+
size_t namelen, maillen;
4241
const char *eol;
4342
struct strbuf subject = STRBUF_INIT;
43+
struct strbuf namemailbuf = STRBUF_INIT;
4444
struct ident_split ident;
4545

4646
if (split_ident_line(&ident, author, strlen(author)))
4747
return;
4848

49-
/* copy author name to namebuf, to support matching on both name and email */
50-
len = ident.name_end - ident.name_begin;
51-
memcpy(namebuf, ident.name_begin, len);
52-
namebuf[len] = 0;
49+
namebuf = ident.name_begin;
50+
mailbuf = ident.mail_begin;
51+
namelen = ident.name_end - ident.name_begin;
52+
maillen = ident.mail_end - ident.mail_begin;
5353

54-
/* copy email name to emailbuf, to allow email replacement as well */
55-
len = ident.mail_end - ident.mail_begin;
56-
memcpy(emailbuf, ident.mail_begin, len);
57-
emailbuf[len] = 0;
54+
map_user(&log->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
55+
strbuf_add(&namemailbuf, namebuf, namelen);
5856

59-
map_user(&log->mailmap, emailbuf, sizeof(emailbuf), namebuf, sizeof(namebuf));
60-
len = strlen(namebuf);
57+
if (log->email)
58+
strbuf_addf(&namemailbuf, " <%.*s>", (int)maillen, mailbuf);
6159

62-
if (log->email) {
63-
size_t room = sizeof(namebuf) - len - 1;
64-
int maillen = strlen(emailbuf);
65-
snprintf(namebuf + len, room, " <%.*s>", maillen, emailbuf);
66-
}
67-
68-
item = string_list_insert(&log->list, namebuf);
60+
item = string_list_insert(&log->list, namemailbuf.buf);
6961
if (item->util == NULL)
7062
item->util = xcalloc(1, sizeof(struct string_list));
7163

0 commit comments

Comments
 (0)