Re: [PATCH 1/3] ident: move commit_rewrite_person() to ident.c
- From
Đoàn Trần Công Danh <congdanhqx@gmail.com>
- Date
- Jun 30, 2022, 16:00 UTC
- Message-ID
- <Yr3Iii8rRfnXzoUI@danh.dev>
- In-Reply-To
- <20220630142444.651948-2-siddharthasthana31@gmail.com>
On 2022-06-30 19:54:42+0530, Siddharth Asthana <siddharthasthana31@gmail.com> wrote:
Show 34 quoted lines
> commit_rewrite_person() is a static function defined in revision.c.
> As the name suggests, this function can be used to replace author's,
> committer's or tagger's name in the commit/tag object buffer.
>
> This patch moves this function from revision.c to ident.c which contains
> many other functions related to identification like split_ident_line. By
> moving this function to ident.c, we intend to use it in git-cat-file to
> replace committer's, author's and tagger's names and emails with their
> canonical name and email using the mailmap mechanism. The function
> is moved as is for now to make it clear that there are no other changes,
> but will be renamed in a following commit.
>
> Signed-off-by: Siddharth Asthana <siddharthasthana31@gmail.com>
> Mentored-by: Christian Couder <christian.couder@gmail.com>
> Mentored-by: John Cai <johncai86@gmail.com>
> ---
> cache.h | 8 ++++++++
> ident.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
> revision.c | 45 ---------------------------------------------
> 3 files changed, 53 insertions(+), 45 deletions(-)
>
> diff --git a/cache.h b/cache.h
> index ac5ab4ef9d..442bfe5f6a 100644
> --- a/cache.h
> +++ b/cache.h
> @@ -1688,6 +1688,14 @@ struct ident_split {
> */
> int split_ident_line(struct ident_split *, const char *, int);
>
> +/*
> + * Given a commit or tag object buffer, replaces the person's
> + * (author/committer/tagger) name and email with their canonical
> + * name and email using mailmap mechanism. Signals a success with
> + * 1 and failure with a 0.I'm not sure if this is important or not. However, in this project, a function which does something usually return 0 on success and a negative integer on failure.
However, the old code is there, so ...
Show 128 quoted lines
> + */
> +int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap);
> +
> /*
> * Compare split idents for equality or strict ordering. Note that we
> * compare only the ident part of the line, ignoring any timestamp.
> diff --git a/ident.c b/ident.c
> index 89ca5b4700..8c890bd474 100644
> --- a/ident.c
> +++ b/ident.c
> @@ -8,6 +8,7 @@
> #include "cache.h"
> #include "config.h"
> #include "date.h"
> +#include "mailmap.h"
>
> static struct strbuf git_default_name = STRBUF_INIT;
> static struct strbuf git_default_email = STRBUF_INIT;
> @@ -346,6 +347,50 @@ int split_ident_line(struct ident_split *split, const char *line, int len)
> return 0;
> }
>
> +int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
> +{
> + char *person, *endp;
> + size_t len, namelen, maillen;
> + const char *name;
> + const char *mail;
> + struct ident_split ident;
> +
> + person = strstr(buf->buf, what);
> + if (!person)
> + return 0;
> +
> + person += strlen(what);
> + endp = strchr(person, '\n');
> + if (!endp)
> + return 0;
> +
> + len = endp - person;
> +
> + if (split_ident_line(&ident, person, len))
> + return 0;
> +
> + mail = ident.mail_begin;
> + maillen = ident.mail_end - ident.mail_begin;
> + name = ident.name_begin;
> + namelen = ident.name_end - ident.name_begin;
> +
> + if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
> + struct strbuf namemail = STRBUF_INIT;
> +
> + strbuf_addf(&namemail, "%.*s <%.*s>",
> + (int)namelen, name, (int)maillen, mail);
> +
> + strbuf_splice(buf, ident.name_begin - buf->buf,
> + ident.mail_end - ident.name_begin + 1,
> + namemail.buf, namemail.len);
> +
> + strbuf_release(&namemail);
> +
> + return 1;
> + }
> +
> + return 0;
> +}
>
> static void ident_env_hint(enum want_ident whose_ident)
> {
> diff --git a/revision.c b/revision.c
> index 211352795c..da49e73cd6 100644
> --- a/revision.c
> +++ b/revision.c
> @@ -3755,51 +3755,6 @@ int rewrite_parents(struct rev_info *revs, struct commit *commit,
> return 0;
> }
>
> -static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
> -{
> - char *person, *endp;
> - size_t len, namelen, maillen;
> - const char *name;
> - const char *mail;
> - struct ident_split ident;
> -
> - person = strstr(buf->buf, what);
> - if (!person)
> - return 0;
> -
> - person += strlen(what);
> - endp = strchr(person, '\n');
> - if (!endp)
> - return 0;
> -
> - len = endp - person;
> -
> - if (split_ident_line(&ident, person, len))
> - return 0;
> -
> - mail = ident.mail_begin;
> - maillen = ident.mail_end - ident.mail_begin;
> - name = ident.name_begin;
> - namelen = ident.name_end - ident.name_begin;
> -
> - if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
> - struct strbuf namemail = STRBUF_INIT;
> -
> - strbuf_addf(&namemail, "%.*s <%.*s>",
> - (int)namelen, name, (int)maillen, mail);
> -
> - strbuf_splice(buf, ident.name_begin - buf->buf,
> - ident.mail_end - ident.name_begin + 1,
> - namemail.buf, namemail.len);
> -
> - strbuf_release(&namemail);
> -
> - return 1;
> - }
> -
> - return 0;
> -}
> -
> static int commit_match(struct commit *commit, struct rev_info *opt)
> {
> int retval;
> --
> 2.37.0.3.g2093cce7fe.dirty
> -- Danh