Skip to content

Commit dc88e34

Browse files
edith007gitster
authored andcommitted
ident: move commit_rewrite_person() to ident.c
commit_rewrite_person() and rewrite_ident_line() are static functions defined in revision.c. Their usages are as follows: - commit_rewrite_person() takes a commit buffer and replaces the author and committer idents with their canonical versions using the mailmap mechanism - rewrite_ident_line() takes author/committer header lines from the commit buffer and replaces the idents with their canonical versions using the mailmap mechanism. This patch moves commit_rewrite_person() and rewrite_ident_line() to ident.c which contains many other functions related to idents like split_ident_line(). By moving commit_rewrite_person() to ident.c, we also intend to use it in git-cat-file to replace committer and author idents from the headers to their canonical versions using the mailmap mechanism. The function is moved as is for now to make it clear that there are no other changes, but it will be renamed in a following commit. Mentored-by: Christian Couder <[email protected]> Mentored-by: John Cai <[email protected]> Signed-off-by: Siddharth Asthana <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e9c1b0e commit dc88e34

File tree

3 files changed

+80
-74
lines changed

3 files changed

+80
-74
lines changed

cache.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,12 @@ struct ident_split {
16881688
*/
16891689
int split_ident_line(struct ident_split *, const char *, int);
16901690

1691+
/*
1692+
* Given a commit object buffer and the commit headers, replaces the idents
1693+
* in the headers with their canonical versions using the mailmap mechanism.
1694+
*/
1695+
void commit_rewrite_person(struct strbuf *, const char **, struct string_list *);
1696+
16911697
/*
16921698
* Compare split idents for equality or strict ordering. Note that we
16931699
* compare only the ident part of the line, ignoring any timestamp.

ident.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "cache.h"
99
#include "config.h"
1010
#include "date.h"
11+
#include "mailmap.h"
1112

1213
static struct strbuf git_default_name = STRBUF_INIT;
1314
static struct strbuf git_default_email = STRBUF_INIT;
@@ -346,6 +347,79 @@ int split_ident_line(struct ident_split *split, const char *line, int len)
346347
return 0;
347348
}
348349

350+
/*
351+
* Returns the difference between the new and old length of the ident line.
352+
*/
353+
static ssize_t rewrite_ident_line(const char *person, size_t len,
354+
struct strbuf *buf,
355+
struct string_list *mailmap)
356+
{
357+
size_t namelen, maillen;
358+
const char *name;
359+
const char *mail;
360+
struct ident_split ident;
361+
362+
if (split_ident_line(&ident, person, len))
363+
return 0;
364+
365+
mail = ident.mail_begin;
366+
maillen = ident.mail_end - ident.mail_begin;
367+
name = ident.name_begin;
368+
namelen = ident.name_end - ident.name_begin;
369+
370+
if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
371+
struct strbuf namemail = STRBUF_INIT;
372+
size_t newlen;
373+
374+
strbuf_addf(&namemail, "%.*s <%.*s>",
375+
(int)namelen, name, (int)maillen, mail);
376+
377+
strbuf_splice(buf, ident.name_begin - buf->buf,
378+
ident.mail_end - ident.name_begin + 1,
379+
namemail.buf, namemail.len);
380+
newlen = namemail.len;
381+
382+
strbuf_release(&namemail);
383+
384+
return newlen - (ident.mail_end - ident.name_begin);
385+
}
386+
387+
return 0;
388+
}
389+
390+
void commit_rewrite_person(struct strbuf *buf, const char **header,
391+
struct string_list *mailmap)
392+
{
393+
size_t buf_offset = 0;
394+
395+
if (!mailmap)
396+
return;
397+
398+
for (;;) {
399+
const char *person, *line;
400+
size_t i;
401+
int found_header = 0;
402+
403+
line = buf->buf + buf_offset;
404+
if (!*line || *line == '\n')
405+
return; /* End of headers */
406+
407+
for (i = 0; header[i]; i++)
408+
if (skip_prefix(line, header[i], &person)) {
409+
const char *endp = strchrnul(person, '\n');
410+
found_header = 1;
411+
buf_offset += endp - line;
412+
buf_offset += rewrite_ident_line(person, endp - person, buf, mailmap);
413+
break;
414+
}
415+
416+
if (!found_header) {
417+
buf_offset = strchrnul(line, '\n') - buf->buf;
418+
if (buf->buf[buf_offset] == '\n')
419+
buf_offset++;
420+
}
421+
}
422+
}
349423

350424
static void ident_env_hint(enum want_ident whose_ident)
351425
{

revision.c

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3755,80 +3755,6 @@ int rewrite_parents(struct rev_info *revs, struct commit *commit,
37553755
return 0;
37563756
}
37573757

3758-
/*
3759-
* Returns the difference between the new and old length of the ident line.
3760-
*/
3761-
static ssize_t rewrite_ident_line(const char *person, size_t len,
3762-
struct strbuf *buf,
3763-
struct string_list *mailmap)
3764-
{
3765-
size_t namelen, maillen;
3766-
const char *name;
3767-
const char *mail;
3768-
struct ident_split ident;
3769-
3770-
if (split_ident_line(&ident, person, len))
3771-
return 0;
3772-
3773-
mail = ident.mail_begin;
3774-
maillen = ident.mail_end - ident.mail_begin;
3775-
name = ident.name_begin;
3776-
namelen = ident.name_end - ident.name_begin;
3777-
3778-
if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
3779-
struct strbuf namemail = STRBUF_INIT;
3780-
size_t newlen;
3781-
3782-
strbuf_addf(&namemail, "%.*s <%.*s>",
3783-
(int)namelen, name, (int)maillen, mail);
3784-
3785-
strbuf_splice(buf, ident.name_begin - buf->buf,
3786-
ident.mail_end - ident.name_begin + 1,
3787-
namemail.buf, namemail.len);
3788-
newlen = namemail.len;
3789-
3790-
strbuf_release(&namemail);
3791-
3792-
return newlen - (ident.mail_end - ident.name_begin);
3793-
}
3794-
3795-
return 0;
3796-
}
3797-
3798-
static void commit_rewrite_person(struct strbuf *buf, const char **header,
3799-
struct string_list *mailmap)
3800-
{
3801-
size_t buf_offset = 0;
3802-
3803-
if (!mailmap)
3804-
return;
3805-
3806-
for (;;) {
3807-
const char *person, *line;
3808-
size_t i;
3809-
int found_header = 0;
3810-
3811-
line = buf->buf + buf_offset;
3812-
if (!*line || *line == '\n')
3813-
return; /* End of headers */
3814-
3815-
for (i = 0; header[i]; i++)
3816-
if (skip_prefix(line, header[i], &person)) {
3817-
const char *endp = strchrnul(person, '\n');
3818-
found_header = 1;
3819-
buf_offset += endp - line;
3820-
buf_offset += rewrite_ident_line(person, endp - person, buf, mailmap);
3821-
break;
3822-
}
3823-
3824-
if (!found_header) {
3825-
buf_offset = strchrnul(line, '\n') - buf->buf;
3826-
if (buf->buf[buf_offset] == '\n')
3827-
buf_offset++;
3828-
}
3829-
}
3830-
}
3831-
38323758
static int commit_match(struct commit *commit, struct rev_info *opt)
38333759
{
38343760
int retval;

0 commit comments

Comments
 (0)