From patchwork Mon Jul 18 19:50:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Siddharth Asthana X-Patchwork-Id: 12921706 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 13951C433EF for ; Mon, 18 Jul 2022 19:51:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235027AbiGRTvW (ORCPT ); Mon, 18 Jul 2022 15:51:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230411AbiGRTvU (ORCPT ); Mon, 18 Jul 2022 15:51:20 -0400 Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 019911D0CB for ; Mon, 18 Jul 2022 12:51:20 -0700 (PDT) Received: by mail-pg1-x531.google.com with SMTP id r186so11559389pgr.2 for ; Mon, 18 Jul 2022 12:51:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UEbn6tqOY+nFz4lNvX5WR0D/0awwUgQFoQjJUrtaKa4=; b=pCiJW4cOWhNYlXs3M/AuJDSTRz2GAz6YO+mJ3KA2WH9AJJWy4ThfRb6pwPD4LqNVtJ irPyIMFtJxt9OSFJgDAQudNKD6knUxK/Hh1a8GSnQEYOd3Ino6KTSbBOrTLrY0B89qjv dlhHtftsSss8bAYWKY3Z7hcnwF1WflsjilKua5lHprvPtRtLPmcLUe24fe9TjaB9a8K/ e9Zfsv1XCcOr+Tx/28qwf9qdo8q17Fp/z7sW5rvuz9EDy0wp9yJltuFCJYTalvyGF5bs MQTgDqW49uLwQ+6epVQ8rfCpnqlikp0CB1mmdGdShYCgzTN0KehqNR0wqIY3RX/wGMM8 cxww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UEbn6tqOY+nFz4lNvX5WR0D/0awwUgQFoQjJUrtaKa4=; b=Vq0Coy3r+xh+PciFw0X5f9xK3srN3PoMrCYYW9RiuCzUqd+SWjOQQER1q7Lyhsk+Ie 3aNQ4V+hyWlLMo2xWxNPxldSP7OdgaNGYb5qWzm8XbCn4n9r5LHnyIVZGGuUF8W7BQuP MVLTSzOJws6RB+w3vIMBwnudWS7bbYy+WTiHVle1dRymH4UIgJqBhx+7VuEgFma2Lezh 3HaMmvOPkM6l0kbgZywRk1+T7fDAGvpL5bbsP/D/De91TClvUAtqkdWjDb8p9hz+dvIA COVVtNxXdBhOYORBqFJBa+2ayo5we5PkpNWEgk2f3zrinIRz769F049bOZoCO5fRtLgV w6Rg== X-Gm-Message-State: AJIora+ixmQxa5iDFTLdR398q0O5x6gh0e1qLJ71ivorsLHUwhh5lgmQ O81dq6RtKAequiVXw9RzJwpLE0zbFQ17nQ== X-Google-Smtp-Source: AGRyM1vD1ty/lFnWNQK5emXnEi4Aa7zJVQ/F4hQA6OdRSQ/DhPHPCHhjxm4i+Wdqt0u5VGde3W7ygQ== X-Received: by 2002:aa7:954a:0:b0:52a:bd44:d15a with SMTP id w10-20020aa7954a000000b0052abd44d15amr29466060pfq.35.1658173878961; Mon, 18 Jul 2022 12:51:18 -0700 (PDT) Received: from HB2.. ([106.212.244.137]) by smtp.gmail.com with ESMTPSA id s13-20020a170902b18d00b0016c35b21901sm9855838plr.195.2022.07.18.12.51.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Jul 2022 12:51:18 -0700 (PDT) From: Siddharth Asthana To: git@vger.kernel.org Cc: phillip.wood123@gmail.com, congdanhqx@gmail.com, christian.couder@gmail.com, avarab@gmail.com, gitster@pobox.com, Johannes.Schindelin@gmx.de, johncai86@gmail.com, Siddharth Asthana Subject: [PATCH v6 1/4] revision: improve commit_rewrite_person() Date: Tue, 19 Jul 2022 01:20:59 +0530 Message-Id: <20220718195102.66321-2-siddharthasthana31@gmail.com> X-Mailer: git-send-email 2.37.1.120.g63d6f8c201 In-Reply-To: <20220718195102.66321-1-siddharthasthana31@gmail.com> References: <20220716074055.1786231-1-siddharthasthana31@gmail.com> <20220718195102.66321-1-siddharthasthana31@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The function, commit_rewrite_person(), is designed to find and replace an ident string in the header part, and the way it avoids a random occurrence of "author A U Thor Mentored-by: John Cai Helped-by: Đoàn Trần Công Danh Helped-by: Johannes Schindelin Signed-off-by: Siddharth Asthana --- revision.c | 64 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/revision.c b/revision.c index 211352795c..3418a1b7f1 100644 --- a/revision.c +++ b/revision.c @@ -3755,25 +3755,18 @@ 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) +/* + * Returns the difference between the new and old length of the ident line. + */ +static ssize_t rewrite_ident_line(const char *person, size_t len, + struct strbuf *buf, + struct string_list *mailmap) { - char *person, *endp; - size_t len, namelen, maillen; + size_t 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; @@ -3784,6 +3777,7 @@ static int commit_rewrite_person(struct strbuf *buf, const char *what, struct st if (map_user(mailmap, &mail, &maillen, &name, &namelen)) { struct strbuf namemail = STRBUF_INIT; + size_t newlen; strbuf_addf(&namemail, "%.*s <%.*s>", (int)namelen, name, (int)maillen, mail); @@ -3791,15 +3785,50 @@ static int commit_rewrite_person(struct strbuf *buf, const char *what, struct st strbuf_splice(buf, ident.name_begin - buf->buf, ident.mail_end - ident.name_begin + 1, namemail.buf, namemail.len); + newlen = namemail.len; strbuf_release(&namemail); - return 1; + return newlen - (ident.mail_end - ident.name_begin); } return 0; } +static void commit_rewrite_person(struct strbuf *buf, const char **header, + struct string_list *mailmap) +{ + size_t buf_offset = 0; + + if (!mailmap) + return; + + for (;;) { + const char *person, *line; + size_t i; + int found_header = 0; + + line = buf->buf + buf_offset; + if (!*line || *line == '\n') + return; /* End of headers */ + + for (i = 0; header[i]; i++) + if (skip_prefix(line, header[i], &person)) { + const char *endp = strchrnul(person, '\n'); + found_header = 1; + buf_offset += endp - line; + buf_offset += rewrite_ident_line(person, endp - person, buf, mailmap); + break; + } + + if (!found_header) { + buf_offset = strchrnul(line, '\n') - buf->buf; + if (buf->buf[buf_offset] == '\n') + buf_offset++; + } + } +} + static int commit_match(struct commit *commit, struct rev_info *opt) { int retval; @@ -3832,11 +3861,12 @@ static int commit_match(struct commit *commit, struct rev_info *opt) strbuf_addstr(&buf, message); if (opt->grep_filter.header_list && opt->mailmap) { + const char *commit_headers[] = { "author ", "committer ", NULL }; + if (!buf.len) strbuf_addstr(&buf, message); - commit_rewrite_person(&buf, "\nauthor ", opt->mailmap); - commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap); + commit_rewrite_person(&buf, commit_headers, opt->mailmap); } /* Append "fake" message parts as needed */ From patchwork Mon Jul 18 19:51:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddharth Asthana X-Patchwork-Id: 12921707 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DA584C433EF for ; Mon, 18 Jul 2022 19:51:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236001AbiGRTv2 (ORCPT ); Mon, 18 Jul 2022 15:51:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230411AbiGRTv1 (ORCPT ); Mon, 18 Jul 2022 15:51:27 -0400 Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3CD8A31DFE for ; Mon, 18 Jul 2022 12:51:25 -0700 (PDT) Received: by mail-pf1-x42e.google.com with SMTP id y141so11589541pfb.7 for ; Mon, 18 Jul 2022 12:51:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Ll246e7O3IOFMGZ13KC/ppTRCoTgg67ztaj2y+kPCIw=; b=G/MjKxbcvngP0N+DCwj2MlpjAMeOTMx2eHzoTaC12N1+7CxYI1Tib4WHxS743xvwXD 89gW+gTU0Fh6Qwr2LgCu6coPmALRMadJdYB7Jz5OznsJmlnqxEGxOFMHtxFfsUx2zAJ2 g8PdhPg4he39UIDln/QMpZakEEcc4qEVAlk6EXS0muCQzjzxg4lpstDMKx/ZAgN7JiSV 0PoQg1muLd0xo/cOB9Ft1qXnKfT0bmkNxDb/WxCrELtYqQBya1WI2qm9G5s4uKGopg73 HncDSKiMM8ESEeDobkR+mnu5LFJrGsBiw52ADZDrfIx3P6K9mHTjBq8Ei/t9vJVlEyg+ /2Xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Ll246e7O3IOFMGZ13KC/ppTRCoTgg67ztaj2y+kPCIw=; b=ktFFXJm8npQbup1qaJm9J/SyMwd5ViVGY8eCO1MsYCa8A8RDqafgSCirfCmiX5+Od7 2FNJegCBYINcWS7Io5HzW+wIMa9w+A4bxi8DQhkR03RX8I1Zj6LplFjmsnNVY+4exv8V UA3COzLM3qXGlOJgiXakiDiQXfqaSapRgfCueb3dkaHWDhgIco5vx//F+O6/8L+qaLK0 xT6kTz12I/UPKcNY8fD8+2nvPArumrTjJ38H/4jXiZQRhjJ7JZ5+TMRKNMLIUWKOzsSl K4SJ539qYsAAADKLE3nZuAF0O5BkzjAEyD3RpgBYPT4eQBFj06XisQVW5ayX9jd3xMpP i52A== X-Gm-Message-State: AJIora9QjQqbHqmOBNrk8zKF7A/q8qpH8aTRn1e04ZyDBAmPubsaOSSn B81vWSie/HlvgoPht+w839/83S0nbEl+bQ== X-Google-Smtp-Source: AGRyM1vfNtuZtBqgvsibSj2VOcQMJ8swMn6H1XL4LNADt2iDuOsPkTZ7yDto7L+yGg7NskSW9NT/SQ== X-Received: by 2002:a05:6a00:198f:b0:52a:dc25:cc78 with SMTP id d15-20020a056a00198f00b0052adc25cc78mr29763694pfl.20.1658173884100; Mon, 18 Jul 2022 12:51:24 -0700 (PDT) Received: from HB2.. ([106.212.244.137]) by smtp.gmail.com with ESMTPSA id s13-20020a170902b18d00b0016c35b21901sm9855838plr.195.2022.07.18.12.51.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Jul 2022 12:51:23 -0700 (PDT) From: Siddharth Asthana To: git@vger.kernel.org Cc: phillip.wood123@gmail.com, congdanhqx@gmail.com, christian.couder@gmail.com, avarab@gmail.com, gitster@pobox.com, Johannes.Schindelin@gmx.de, johncai86@gmail.com, Siddharth Asthana Subject: [PATCH v6 2/4] ident: move commit_rewrite_person() to ident.c Date: Tue, 19 Jul 2022 01:21:00 +0530 Message-Id: <20220718195102.66321-3-siddharthasthana31@gmail.com> X-Mailer: git-send-email 2.37.1.120.g63d6f8c201 In-Reply-To: <20220718195102.66321-1-siddharthasthana31@gmail.com> References: <20220716074055.1786231-1-siddharthasthana31@gmail.com> <20220718195102.66321-1-siddharthasthana31@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org 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 Mentored-by: John Cai Signed-off-by: Siddharth Asthana --- cache.h | 6 +++++ ident.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ revision.c | 74 ------------------------------------------------------ 3 files changed, 80 insertions(+), 74 deletions(-) diff --git a/cache.h b/cache.h index ac5ab4ef9d..16a08aada2 100644 --- a/cache.h +++ b/cache.h @@ -1688,6 +1688,12 @@ struct ident_split { */ int split_ident_line(struct ident_split *, const char *, int); +/* + * Given a commit object buffer and the commit headers, replaces the idents + * in the headers with their canonical versions using the mailmap mechanism. + */ +void commit_rewrite_person(struct strbuf *, const char **, struct string_list *); + /* * 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..1eee4fd0e3 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,79 @@ int split_ident_line(struct ident_split *split, const char *line, int len) return 0; } +/* + * Returns the difference between the new and old length of the ident line. + */ +static ssize_t rewrite_ident_line(const char *person, size_t len, + struct strbuf *buf, + struct string_list *mailmap) +{ + size_t namelen, maillen; + const char *name; + const char *mail; + struct ident_split ident; + + 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; + size_t newlen; + + 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); + newlen = namemail.len; + + strbuf_release(&namemail); + + return newlen - (ident.mail_end - ident.name_begin); + } + + return 0; +} + +void commit_rewrite_person(struct strbuf *buf, const char **header, + struct string_list *mailmap) +{ + size_t buf_offset = 0; + + if (!mailmap) + return; + + for (;;) { + const char *person, *line; + size_t i; + int found_header = 0; + + line = buf->buf + buf_offset; + if (!*line || *line == '\n') + return; /* End of headers */ + + for (i = 0; header[i]; i++) + if (skip_prefix(line, header[i], &person)) { + const char *endp = strchrnul(person, '\n'); + found_header = 1; + buf_offset += endp - line; + buf_offset += rewrite_ident_line(person, endp - person, buf, mailmap); + break; + } + + if (!found_header) { + buf_offset = strchrnul(line, '\n') - buf->buf; + if (buf->buf[buf_offset] == '\n') + buf_offset++; + } + } +} static void ident_env_hint(enum want_ident whose_ident) { diff --git a/revision.c b/revision.c index 3418a1b7f1..14dca903b6 100644 --- a/revision.c +++ b/revision.c @@ -3755,80 +3755,6 @@ int rewrite_parents(struct rev_info *revs, struct commit *commit, return 0; } -/* - * Returns the difference between the new and old length of the ident line. - */ -static ssize_t rewrite_ident_line(const char *person, size_t len, - struct strbuf *buf, - struct string_list *mailmap) -{ - size_t namelen, maillen; - const char *name; - const char *mail; - struct ident_split ident; - - 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; - size_t newlen; - - 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); - newlen = namemail.len; - - strbuf_release(&namemail); - - return newlen - (ident.mail_end - ident.name_begin); - } - - return 0; -} - -static void commit_rewrite_person(struct strbuf *buf, const char **header, - struct string_list *mailmap) -{ - size_t buf_offset = 0; - - if (!mailmap) - return; - - for (;;) { - const char *person, *line; - size_t i; - int found_header = 0; - - line = buf->buf + buf_offset; - if (!*line || *line == '\n') - return; /* End of headers */ - - for (i = 0; header[i]; i++) - if (skip_prefix(line, header[i], &person)) { - const char *endp = strchrnul(person, '\n'); - found_header = 1; - buf_offset += endp - line; - buf_offset += rewrite_ident_line(person, endp - person, buf, mailmap); - break; - } - - if (!found_header) { - buf_offset = strchrnul(line, '\n') - buf->buf; - if (buf->buf[buf_offset] == '\n') - buf_offset++; - } - } -} - static int commit_match(struct commit *commit, struct rev_info *opt) { int retval; From patchwork Mon Jul 18 19:51:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddharth Asthana X-Patchwork-Id: 12921708 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 48AFEC43334 for ; Mon, 18 Jul 2022 19:51:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235287AbiGRTvo (ORCPT ); Mon, 18 Jul 2022 15:51:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42768 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235703AbiGRTvb (ORCPT ); Mon, 18 Jul 2022 15:51:31 -0400 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CA5632076 for ; Mon, 18 Jul 2022 12:51:30 -0700 (PDT) Received: by mail-pj1-x102d.google.com with SMTP id s18-20020a17090aa11200b001f1e9e2438cso833461pjp.2 for ; Mon, 18 Jul 2022 12:51:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wM/v15Qj1ww+RBCgAadbyRIjYsmkO3Gn9nN2VSkeD/0=; b=HZRB1SHN4ybasT1g1OHaDIMuPbNAw4l+prPJj+Ul15Xx3MPYeQZUcCj8ulefbnNUs4 j1RH+colZN7tzu9t64UD/EUkrjmALFBALBv3QuyllZ7BPicIngHZiW1AEeLiQ4ZYwNPJ N22rggOawv9Umb/bS47qJvso15lRP9uCka6EmrwYaL9RnJPDwyMsg2rjDUbLD2kVRLP9 wczGtprU7CuLIQuT0c7apCbQ+bb2uyTUWpd7ly4BKXqw/IvLK1++hkmsqrqK7b4GvKwF o0fxGModK1VGSgkCe2RXkmb1l8dK/KW+lX6h/iDwATJWyuXnju9qxu65900Uzn3AkQny X5Sg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wM/v15Qj1ww+RBCgAadbyRIjYsmkO3Gn9nN2VSkeD/0=; b=Ib5nqM6MeSZG4DwIFbMXoY9XA+YnfrronbdNKLZqfy4ANhSMlHDS9I5MyrlOR9Ip+B MjAIC1JRLAo0XXjF9S0UWfSdbMnMWDRbnJgZV9BQZUsgbLiPUuxtUtn8Tqm4OfzWc9Lx gHfJZfL1J4ZJVvrlP9NDtxhVmxup0qEx8aoazYnQUKfv6SmZSTW71M9NIk0N+zFxwaR7 ws3/3iHX91hhaL7qqIL9L39eUEW6fHRRCH7tHc5cRsYs2+mQBY7aXGi3QdOyuEU6UoRs ms7zabAIJMVFvsVT8sjuQK3JOBKa+RNil23CJa4KlUARUs8hC1HUKE9l5HjRTUIQLgXY jdcA== X-Gm-Message-State: AJIora+XkRblEcCoivN/WiFHoS1Vqv72hqOhy9aU/ixOyWCcck0oVNyT IbhPRbVniBIIlDjeTPfd0468fqb64bMCdw== X-Google-Smtp-Source: AGRyM1tKxGClYgLBFJno7RDbXEbJG0jp4rRCrpjmT/bQ60ddTppkblC0WuhXceQmkRkSc2Ncy8seaw== X-Received: by 2002:a17:90b:3e89:b0:1f0:4233:b20e with SMTP id rj9-20020a17090b3e8900b001f04233b20emr34217813pjb.0.1658173889310; Mon, 18 Jul 2022 12:51:29 -0700 (PDT) Received: from HB2.. ([106.212.244.137]) by smtp.gmail.com with ESMTPSA id s13-20020a170902b18d00b0016c35b21901sm9855838plr.195.2022.07.18.12.51.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Jul 2022 12:51:28 -0700 (PDT) From: Siddharth Asthana To: git@vger.kernel.org Cc: phillip.wood123@gmail.com, congdanhqx@gmail.com, christian.couder@gmail.com, avarab@gmail.com, gitster@pobox.com, Johannes.Schindelin@gmx.de, johncai86@gmail.com, Siddharth Asthana Subject: [PATCH v6 3/4] ident: rename commit_rewrite_person() to apply_mailmap_to_header() Date: Tue, 19 Jul 2022 01:21:01 +0530 Message-Id: <20220718195102.66321-4-siddharthasthana31@gmail.com> X-Mailer: git-send-email 2.37.1.120.g63d6f8c201 In-Reply-To: <20220718195102.66321-1-siddharthasthana31@gmail.com> References: <20220716074055.1786231-1-siddharthasthana31@gmail.com> <20220718195102.66321-1-siddharthasthana31@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org commit_rewrite_person() takes a commit buffer and replaces the idents in the header with their canonical versions using the mailmap mechanism. The name "commit_rewrite_person()" is misleading as it doesn't convey what kind of rewrite are we going to do to the buffer. It also doesn't clearly mention that the function will limit itself to the header part of the buffer. The new name, "apply_mailmap_to_header()", expresses the functionality of the function pretty clearly. We intend to use apply_mailmap_to_header() in git-cat-file to replace idents in the headers of commit and tag object buffers. So, we will be extending this function to take tag objects buffer as well and replace idents on the tagger header using the mailmap mechanism. Mentored-by: Christian Couder Mentored-by: John Cai Signed-off-by: Siddharth Asthana --- cache.h | 6 +++--- ident.c | 4 ++-- revision.c | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cache.h b/cache.h index 16a08aada2..4aa1bd079d 100644 --- a/cache.h +++ b/cache.h @@ -1689,10 +1689,10 @@ struct ident_split { int split_ident_line(struct ident_split *, const char *, int); /* - * Given a commit object buffer and the commit headers, replaces the idents - * in the headers with their canonical versions using the mailmap mechanism. + * Given a commit or tag object buffer and the commit or tag headers, replaces + * the idents in the headers with their canonical versions using the mailmap mechanism. */ -void commit_rewrite_person(struct strbuf *, const char **, struct string_list *); +void apply_mailmap_to_header(struct strbuf *, const char **, struct string_list *); /* * Compare split idents for equality or strict ordering. Note that we diff --git a/ident.c b/ident.c index 1eee4fd0e3..7f66beda42 100644 --- a/ident.c +++ b/ident.c @@ -387,8 +387,8 @@ static ssize_t rewrite_ident_line(const char *person, size_t len, return 0; } -void commit_rewrite_person(struct strbuf *buf, const char **header, - struct string_list *mailmap) +void apply_mailmap_to_header(struct strbuf *buf, const char **header, + struct string_list *mailmap) { size_t buf_offset = 0; diff --git a/revision.c b/revision.c index 14dca903b6..6ad3665204 100644 --- a/revision.c +++ b/revision.c @@ -3792,7 +3792,7 @@ static int commit_match(struct commit *commit, struct rev_info *opt) if (!buf.len) strbuf_addstr(&buf, message); - commit_rewrite_person(&buf, commit_headers, opt->mailmap); + apply_mailmap_to_header(&buf, commit_headers, opt->mailmap); } /* Append "fake" message parts as needed */ From patchwork Mon Jul 18 19:51:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddharth Asthana X-Patchwork-Id: 12921709 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4FCB0CCA479 for ; Mon, 18 Jul 2022 19:51:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236221AbiGRTvq (ORCPT ); Mon, 18 Jul 2022 15:51:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236334AbiGRTvh (ORCPT ); Mon, 18 Jul 2022 15:51:37 -0400 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F7CD32044 for ; Mon, 18 Jul 2022 12:51:36 -0700 (PDT) Received: by mail-pj1-x102f.google.com with SMTP id 89-20020a17090a09e200b001ef7638e536so19221990pjo.3 for ; Mon, 18 Jul 2022 12:51:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=DKCy/SfIGt6Fn8dq+wjVnzZz8PJRGxrn87FqhSSqHOQ=; b=eUNPpfgZje/w+WR5NHlLL3XcSogf75TjTcmmUhqdirNRGI+rY5y4aU4DmysVg2TJsv UYLaLxbZc8nE2RYVTXYWN00k3rU3VlGcu/UdroqSysZ8oe1hFg6bKgl4/VLJ7mwtHH9y ErsuVY4XonWwJYEftNEIR0//KjcwNs0EIJCFMxiqtu0Dhnt05l34vOcE1uojWSOs+TJ0 juSSaSpQNOUtPt0zb43t6J3BQNje1IBS/4cyR9vx0CDvh19cyrUKtRTNpg7ktTJWfkEn ei2kV/JSGit9HNPwUsgAhuAqXOwxhsBjZ4nEeAF0SsPrzpNYrziv9TQ+cPdK5ivlsYx0 PbvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=DKCy/SfIGt6Fn8dq+wjVnzZz8PJRGxrn87FqhSSqHOQ=; b=YgPF/pQJK8tqUx634B8bDB0oWnNsgbepd1JLKqKJLxU6fOKGqXl89CxW7OnabBZKZx U9osOMTqi3RXK+OSQntwNiFp6g18w0JdFgYI8AYtqRZUijq4w1yQ8BiuEyBnBd4bMtPs pRBBjuNcsXD2gQPDyfOH9mvEIb6m+j2h6HzfFXG5J/4pVMzM3l9QP5oPorS0ozmMT0Ak 33Bps6ef+ekNn7F43tT43aV2Tl2O4s6t1dYJ0KIZht3yrlN2pdzCDBcszxhZggSIU/Rr OwGKr3BUTT6DDafoKWYBQwwGA7nseHq1ozHOR7RxBo7CWUQAqX+/SXGS3frRfGSCVncS Px6w== X-Gm-Message-State: AJIora8joe/YKF5siZQeoR+8eKpXK5BltW9nntaoicb5odeTwcC3VQg9 VZ5sm7dOnLuheSWzvN/CdD1cB6IjqJvNgA== X-Google-Smtp-Source: AGRyM1uOJZxKI2HIbPO3DL1+uxX0wtt8c8onZpqsBPMpZSFdV8JB0V+y60j1Sck6M5gYBYP9TKk1yg== X-Received: by 2002:a17:90b:3b82:b0:1f0:73e1:8426 with SMTP id pc2-20020a17090b3b8200b001f073e18426mr35593651pjb.209.1658173894934; Mon, 18 Jul 2022 12:51:34 -0700 (PDT) Received: from HB2.. ([106.212.244.137]) by smtp.gmail.com with ESMTPSA id s13-20020a170902b18d00b0016c35b21901sm9855838plr.195.2022.07.18.12.51.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Jul 2022 12:51:34 -0700 (PDT) From: Siddharth Asthana To: git@vger.kernel.org Cc: phillip.wood123@gmail.com, congdanhqx@gmail.com, christian.couder@gmail.com, avarab@gmail.com, gitster@pobox.com, Johannes.Schindelin@gmx.de, johncai86@gmail.com, Siddharth Asthana , Phillip Wood Subject: [PATCH v6 4/4] cat-file: add mailmap support Date: Tue, 19 Jul 2022 01:21:02 +0530 Message-Id: <20220718195102.66321-5-siddharthasthana31@gmail.com> X-Mailer: git-send-email 2.37.1.120.g63d6f8c201 In-Reply-To: <20220718195102.66321-1-siddharthasthana31@gmail.com> References: <20220716074055.1786231-1-siddharthasthana31@gmail.com> <20220718195102.66321-1-siddharthasthana31@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org git-cat-file is used by tools like GitLab to get commit tag contents that are then displayed to users. This content which has author, committer or tagger information, could benefit from passing through the mailmap mechanism before being sent or displayed. This patch adds --[no-]use-mailmap command line option to the git cat-file command. It also adds --[no-]mailmap option as an alias to --[no-]use-mailmap. This patch also introduces new test cases to test the mailmap mechanism in git cat-file command. Mentored-by: Christian Couder Mentored-by: John Cai Helped-by: Phillip Wood Helped-by: Johannes Schindelin Signed-off-by: Siddharth Asthana --- Documentation/git-cat-file.txt | 6 ++++ builtin/cat-file.c | 43 ++++++++++++++++++++++++- t/t4203-mailmap.sh | 59 ++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 1 deletion(-) diff --git a/Documentation/git-cat-file.txt b/Documentation/git-cat-file.txt index 24a811f0ef..1880e9bba1 100644 --- a/Documentation/git-cat-file.txt +++ b/Documentation/git-cat-file.txt @@ -63,6 +63,12 @@ OPTIONS or to ask for a "blob" with `` being a tag object that points at it. +--[no-]mailmap:: +--[no-]use-mailmap:: + Use mailmap file to map author, committer and tagger names + and email addresses to canonical real names and email addresses. + See linkgit:git-shortlog[1]. + --textconv:: Show the content as transformed by a textconv filter. In this case, `` has to be of the form `:`, or `:` in diff --git a/builtin/cat-file.c b/builtin/cat-file.c index 50cf38999d..4b68216b51 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -16,6 +16,7 @@ #include "packfile.h" #include "object-store.h" #include "promisor-remote.h" +#include "mailmap.h" enum batch_mode { BATCH_MODE_CONTENTS, @@ -36,6 +37,22 @@ struct batch_options { static const char *force_path; +static struct string_list mailmap = STRING_LIST_INIT_NODUP; +static int use_mailmap; + +static char *replace_idents_using_mailmap(char *, size_t *); + +static char *replace_idents_using_mailmap(char *object_buf, size_t *size) +{ + struct strbuf sb = STRBUF_INIT; + const char *headers[] = { "author ", "committer ", "tagger ", NULL }; + + strbuf_attach(&sb, object_buf, *size, *size + 1); + apply_mailmap_to_header(&sb, headers, &mailmap); + *size = sb.len; + return strbuf_detach(&sb, NULL); +} + static int filter_object(const char *path, unsigned mode, const struct object_id *oid, char **buf, unsigned long *size) @@ -152,6 +169,12 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name, if (!buf) die("Cannot read object %s", obj_name); + if (use_mailmap) { + size_t s = size; + buf = replace_idents_using_mailmap(buf, &s); + size = cast_size_t_to_ulong(s); + } + /* otherwise just spit out the data */ break; @@ -183,6 +206,12 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name, } buf = read_object_with_reference(the_repository, &oid, exp_type_id, &size, NULL); + + if (use_mailmap) { + size_t s = size; + buf = replace_idents_using_mailmap(buf, &s); + size = cast_size_t_to_ulong(s); + } break; } default: @@ -348,11 +377,18 @@ static void print_object_or_die(struct batch_options *opt, struct expand_data *d void *contents; contents = read_object_file(oid, &type, &size); + + if (use_mailmap) { + size_t s = size; + contents = replace_idents_using_mailmap(contents, &s); + size = cast_size_t_to_ulong(s); + } + if (!contents) die("object %s disappeared", oid_to_hex(oid)); if (type != data->type) die("object %s changed type!?", oid_to_hex(oid)); - if (data->info.sizep && size != data->size) + if (data->info.sizep && size != data->size && !use_mailmap) die("object %s changed size!?", oid_to_hex(oid)); batch_write(opt, contents, size); @@ -843,6 +879,8 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix) OPT_CMDMODE('s', NULL, &opt, N_("show object size"), 's'), OPT_BOOL(0, "allow-unknown-type", &unknown_type, N_("allow -s and -t to work with broken/corrupt objects")), + OPT_BOOL(0, "use-mailmap", &use_mailmap, N_("use mail map file")), + OPT_ALIAS(0, "mailmap", "use-mailmap"), /* Batch mode */ OPT_GROUP(N_("Batch objects requested on stdin (or --batch-all-objects)")), OPT_CALLBACK_F(0, "batch", &batch, N_("format"), @@ -885,6 +923,9 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix) opt_cw = (opt == 'c' || opt == 'w'); opt_epts = (opt == 'e' || opt == 'p' || opt == 't' || opt == 's'); + if (use_mailmap) + read_mailmap(&mailmap); + /* --batch-all-objects? */ if (opt == 'b') batch.all_objects = 1; diff --git a/t/t4203-mailmap.sh b/t/t4203-mailmap.sh index 0b2d21ec55..cd1cab3e54 100755 --- a/t/t4203-mailmap.sh +++ b/t/t4203-mailmap.sh @@ -963,4 +963,63 @@ test_expect_success SYMLINKS 'symlinks not respected in-tree' ' test_cmp expect actual ' +test_expect_success 'prepare for cat-file --mailmap' ' + rm -f .mailmap && + git commit --allow-empty -m foo --author="Orig " +' + +test_expect_success '--no-use-mailmap disables mailmap in cat-file' ' + test_when_finished "rm .mailmap" && + cat >.mailmap <<-EOF && + A U Thor Orig + EOF + cat >expect <<-EOF && + author Orig + EOF + git cat-file --no-use-mailmap commit HEAD >log && + sed -n "/^author /s/\([^>]*>\).*/\1/p" log >actual && + test_cmp expect actual +' + +test_expect_success '--use-mailmap enables mailmap in cat-file' ' + test_when_finished "rm .mailmap" && + cat >.mailmap <<-EOF && + A U Thor Orig + EOF + cat >expect <<-EOF && + author A U Thor + EOF + git cat-file --use-mailmap commit HEAD >log && + sed -n "/^author /s/\([^>]*>\).*/\1/p" log >actual && + test_cmp expect actual +' + +test_expect_success '--no-mailmap disables mailmap in cat-file for annotated tag objects' ' + test_when_finished "rm .mailmap" && + cat >.mailmap <<-EOF && + Orig C O Mitter + EOF + cat >expect <<-EOF && + tagger C O Mitter + EOF + git tag -a -m "annotated tag" v1 && + git cat-file --no-mailmap -p v1 >log && + sed -n "/^tagger /s/\([^>]*>\).*/\1/p" log >actual && + test_cmp expect actual +' + +test_expect_success '--mailmap enables mailmap in cat-file for annotated tag objects' ' + test_when_finished "rm .mailmap" && + cat >.mailmap <<-EOF && + Orig C O Mitter + EOF + cat >expect <<-EOF && + tagger Orig + EOF + git tag -a -m "annotated tag" v2 && + git cat-file --mailmap -p v2 >log && + sed -n "/^tagger /s/\([^>]*>\).*/\1/p" log >actual && + test_cmp expect actual +' + test_done