From patchwork Tue Jul 12 16:06:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Siddharth Asthana X-Patchwork-Id: 12915264 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 DD93AC43334 for ; Tue, 12 Jul 2022 16:07:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234228AbiGLQHP (ORCPT ); Tue, 12 Jul 2022 12:07:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234203AbiGLQGz (ORCPT ); Tue, 12 Jul 2022 12:06:55 -0400 Received: from mail-pg1-x530.google.com (mail-pg1-x530.google.com [IPv6:2607:f8b0:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 963DECB442 for ; Tue, 12 Jul 2022 09:06:51 -0700 (PDT) Received: by mail-pg1-x530.google.com with SMTP id 72so8032117pge.0 for ; Tue, 12 Jul 2022 09:06:51 -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=pVNAQ+ZfOMv2yKI1iHlzflyugwyXQWFM6NDBeidxkPg=; b=Vzix40OPA+/09+orxMoKzuPHq5MHUVCIoKrHATq4+1ENqtaeYqxEd+2/V4ezZfywmp 3QXCUzpUmz5st/EFbWoOczmObqQAHnTS1dTsDi4NFaPQEAxX53xC4l+z5HNNpa1TsGj1 kilEyCOOC+ORWlaB7daoFCf+RB34IiTlONbNE9YwU6MahY3vDMSH302vWQXFi/37FBxN ga8BKxJbkRq+Ge0lYu8jRazyLTiK85C+dD4x4jGAPgDh9jOIaFpl+a157bAQKu9hBqEZ Zu2baEWvg9sIIQsZxkt1/u5E4D0Ba3q+gffAqDhU9QMQHcRKuqjN2r68daDQIGQZ3f5W mctQ== 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=pVNAQ+ZfOMv2yKI1iHlzflyugwyXQWFM6NDBeidxkPg=; b=mmhE2JjB23C+S0svTmiwGZZskUxKuLT0JwqmQRJXfhJkrUz+Jt1QOl3sSDcxehElwe LsJ5mlLy+0vhjCP8YeYA5LOS+7KNdNXmBD2iEo9j1iPyE4FhKEkHfWt+neeg+1uL14Ap H1JFT0WPG/2U/k2/fFFZxou4/lSIT+po05FhPaWS3Dwm1kxeHBzfFOJPmEkJHyQRkbVf Um2/wvT+QDdOm2I+W3z74Cem64z3jbvMIHe/NlLJ87PUaP6QTatDkX6q1NvmojA3cNbA FZZx/BGmKd648cegVIVN2ZkbP4vSOdEncgTSuPcC3YsjbBcrz2YvJgSP+YPLC3iPakXA AEeA== X-Gm-Message-State: AJIora9b9ZLyszeaF2PudOsVd+w6ck9d7ov7UDowjGO5BcTB6ILQZBmt gmtmCbSbAOgfuRpuA7QDU+RPpvKxvFnDiDio X-Google-Smtp-Source: AGRyM1s/I61GRxh8hZpaHt/mLd7cAN5I6p/EkGOBLHNXpFiPtt52j6m2mPQtCBSCdjcPUgeP+uCeLg== X-Received: by 2002:a63:d14b:0:b0:419:57b9:d444 with SMTP id c11-20020a63d14b000000b0041957b9d444mr3920430pgj.619.1657642010604; Tue, 12 Jul 2022 09:06:50 -0700 (PDT) Received: from HB2.. ([223.230.62.254]) by smtp.gmail.com with ESMTPSA id u188-20020a6260c5000000b0050dc7628183sm7232376pfb.93.2022.07.12.09.06.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Jul 2022 09:06:49 -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 v4 1/4] revision: improve commit_rewrite_person() Date: Tue, 12 Jul 2022 21:36:31 +0530 Message-Id: <20220712160634.213956-2-siddharthasthana31@gmail.com> X-Mailer: git-send-email 2.37.0.6.g69b7ad898b In-Reply-To: <20220712160634.213956-1-siddharthasthana31@gmail.com> References: <20220709154149.165524-1-siddharthasthana31@gmail.com> <20220712160634.213956-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 Signed-off-by: Siddharth Asthana --- revision.c | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/revision.c b/revision.c index 211352795c..1939c56c67 100644 --- a/revision.c +++ b/revision.c @@ -3755,19 +3755,17 @@ 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, struct strbuf *buf, struct string_list *mailmap) { - char *person, *endp; + char *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; @@ -3784,6 +3782,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); @@ -3792,14 +3791,42 @@ static int commit_rewrite_person(struct strbuf *buf, const char *what, struct st 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 + 1); } return 0; } +static void commit_rewrite_person(struct strbuf *buf, const char **headers, struct string_list *mailmap) +{ + size_t buf_offset = 0; + + if (!mailmap) + return; + + for (;;) { + const char *person, *line; + size_t i, linelen; + + line = buf->buf + buf_offset; + linelen = strchrnul(line, '\n') - line + 1; + + if (linelen <= 1) + /* End of header */ + return; + + buf_offset += linelen; + + for (i = 0; headers[i]; i++) + if (skip_prefix(line, headers[i], &person)) + buf_offset += rewrite_ident_line(person, buf, mailmap); + } +} + static int commit_match(struct commit *commit, struct rev_info *opt) { int retval; @@ -3832,11 +3859,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 Tue Jul 12 16:06:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddharth Asthana X-Patchwork-Id: 12915265 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 A5394C43334 for ; Tue, 12 Jul 2022 16:07:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233916AbiGLQHX (ORCPT ); Tue, 12 Jul 2022 12:07:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233613AbiGLQG6 (ORCPT ); Tue, 12 Jul 2022 12:06:58 -0400 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CEAEEC8EB9 for ; Tue, 12 Jul 2022 09:06:57 -0700 (PDT) Received: by mail-pj1-x1036.google.com with SMTP id v4-20020a17090abb8400b001ef966652a3so11935814pjr.4 for ; Tue, 12 Jul 2022 09:06:57 -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=Phvkfw8wl/cag2XeSqKzJu4tEeNHTaSonhL7JNi8m2c=; b=q6o1k0MPfeoQid8UgOK6F2lO2p2xD/hFPAoKb1aXf3rL7H+k5Tea5FcAwoTyYCQFQ4 c7d18/dhDxBIaSv/SQcKh0rS83XE5LPmxJLDdYwEjRjw/RBIPciArSltFiDLqiagKxgV UfZd7LDEgbiyIqRGYp+pS7B/UpfMt7NvRTIstVfOVw8/rndqL2Sfdi8/tbcVJSdi/Ts0 2Blu7S6wIputFSvyElv/cGtEJGg13KNP4gD6daWq7e5uZ3RqxSdKpsm7Y4qNa//XINZj rkLLZwVP2cMwQH3NnSj0G4S7vYZVW3JX+sONBtOVivOuTFLNqi9s3Lnj4/xQ4lkGE1o4 DFgQ== 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=Phvkfw8wl/cag2XeSqKzJu4tEeNHTaSonhL7JNi8m2c=; b=0ei0EfdzmT7I0LPws3Ww7hRALkzKXvs6dLSRtTW42uQpMVyVwAR9GfE2OmSOdqMq4M 1Uau9t7HtIRiIPXwTXY64d72NhgxZYyIQ1yPpDU+m83LYcyjT9qeX2/ve+aTFmNbNZTx 16uCtVuyvp1WnkCmvhQeML7mXR6bXa3a+CyG0DjF5KLkyGxBcRkbFMrRCLZorM1X4xVa U7TK7MkQDnSF7xS5sV1+ZUSuSiQQqIhXQqmIdX5Mx0OziSpt2z78KOcP4j5Mjvzilw3X NfSC9MIKVpti5N3zgdM8nmkwk+4wacqZxvf/3sUXhazPJ91X9eypru5E7m2YL8mHitaT 0bsA== X-Gm-Message-State: AJIora/0rsZZ5yfx+jzrWn91q2CNkzYU17SBRxJeTxbztQi4PbTbTNYJ trVjhsWOHvWWxeDuZtILCVXo2g4HANfTRQ== X-Google-Smtp-Source: AGRyM1sPCGa3iTwbVj8CYdFr/0Psv8x8SSwimQhq/R8NVvG7YbytVWOoZeikRgjnPFFszDwNHk4eYg== X-Received: by 2002:a17:903:22c7:b0:16b:fa15:63d4 with SMTP id y7-20020a17090322c700b0016bfa1563d4mr24843161plg.2.1657642016819; Tue, 12 Jul 2022 09:06:56 -0700 (PDT) Received: from HB2.. ([223.230.62.254]) by smtp.gmail.com with ESMTPSA id u188-20020a6260c5000000b0050dc7628183sm7232376pfb.93.2022.07.12.09.06.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Jul 2022 09:06:56 -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 v4 2/4] ident: move commit_rewrite_person() to ident.c Date: Tue, 12 Jul 2022 21:36:32 +0530 Message-Id: <20220712160634.213956-3-siddharthasthana31@gmail.com> X-Mailer: git-send-email 2.37.0.6.g69b7ad898b In-Reply-To: <20220712160634.213956-1-siddharthasthana31@gmail.com> References: <20220709154149.165524-1-siddharthasthana31@gmail.com> <20220712160634.213956-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 | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ revision.c | 72 ------------------------------------------------------ 3 files changed, 78 insertions(+), 72 deletions(-) diff --git a/cache.h b/cache.h index ac5ab4ef9d..c9dbe1c29a 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 *buf, const char **commit_headers, 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..9f4f6e9071 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,77 @@ 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, struct strbuf *buf, struct string_list *mailmap) +{ + char *endp; + size_t len, namelen, maillen; + const char *name; + const char *mail; + struct ident_split ident; + + 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; + 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 + 1); + } + + return 0; +} + +void commit_rewrite_person(struct strbuf *buf, const char **headers, struct string_list *mailmap) +{ + size_t buf_offset = 0; + + if (!mailmap) + return; + + for (;;) { + const char *person, *line; + size_t i, linelen; + + line = buf->buf + buf_offset; + linelen = strchrnul(line, '\n') - line + 1; + + if (linelen <= 1) + /* End of header */ + return; + + buf_offset += linelen; + + for (i = 0; headers[i]; i++) + if (skip_prefix(line, headers[i], &person)) + buf_offset += rewrite_ident_line(person, buf, mailmap); + } +} static void ident_env_hint(enum want_ident whose_ident) { diff --git a/revision.c b/revision.c index 1939c56c67..14dca903b6 100644 --- a/revision.c +++ b/revision.c @@ -3755,78 +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, struct strbuf *buf, struct string_list *mailmap) -{ - char *endp; - size_t len, namelen, maillen; - const char *name; - const char *mail; - struct ident_split ident; - - 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; - 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 + 1); - } - - return 0; -} - -static void commit_rewrite_person(struct strbuf *buf, const char **headers, struct string_list *mailmap) -{ - size_t buf_offset = 0; - - if (!mailmap) - return; - - for (;;) { - const char *person, *line; - size_t i, linelen; - - line = buf->buf + buf_offset; - linelen = strchrnul(line, '\n') - line + 1; - - if (linelen <= 1) - /* End of header */ - return; - - buf_offset += linelen; - - for (i = 0; headers[i]; i++) - if (skip_prefix(line, headers[i], &person)) - buf_offset += rewrite_ident_line(person, buf, mailmap); - } -} - static int commit_match(struct commit *commit, struct rev_info *opt) { int retval; From patchwork Tue Jul 12 16:06:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddharth Asthana X-Patchwork-Id: 12915266 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 2805BC433EF for ; Tue, 12 Jul 2022 16:07:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229760AbiGLQHZ (ORCPT ); Tue, 12 Jul 2022 12:07:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233926AbiGLQHE (ORCPT ); Tue, 12 Jul 2022 12:07:04 -0400 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A957BC84F8 for ; Tue, 12 Jul 2022 09:07:03 -0700 (PDT) Received: by mail-pj1-x102a.google.com with SMTP id 89-20020a17090a09e200b001ef7638e536so11931140pjo.3 for ; Tue, 12 Jul 2022 09:07:03 -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=cFJvu+AqqPOe9XgzLb+7cbOS+hj7hA1P6aUDOl8dz74=; b=pwcFjxZ3S+idz33d+xSg1lXSYv6oyIhXGROrOTNyeP4LBFauEBnfh3/0NKhjjEGcEa H7clM95WYADgPN0WgBV8XTOM4eWomGXA5T0wUQ8VTNgYJYbHofckbpJhxxmDLxOWXzJT MsVnIE9ZUUY9laoOzS7Y0cvPFXz73SeBQlDoEHH6wThftj+Yf5Syn2kYBxIomxfCWfC5 hpBN9fmMLbD9oIwNnkHyQE4NTwBYB7hDCD/2B/svLBceXxMvsEj7cM6KTBbqAUKAUn/v g0wEugg8QPngiyoWz5ckd9RUf2M7Nn27epMRvN3sLZ5VylDlBxbeMvDU6bsW4xJm0mG9 DF8w== 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=cFJvu+AqqPOe9XgzLb+7cbOS+hj7hA1P6aUDOl8dz74=; b=h0puhWpTlykMwulnsMgCmkVKJZwSrPcKHruJP0RYjpXRqrQ30FvuipoG24RVfU8u8N it4wgAAq64fFSL9o/Wj4Q/hHs4XiQdVl5XH8bfEjWxvZwtic9x4o12p+meCEqNsDed5e NPvR5G/siPRNd8TqiqaNpA76+o5aGQToaKkohuiVOl0kkzbg+82CMwPQO/3FrWUkUBCl 3u+DvS6Vd5nw8B82yLiBgnQ9uAU8hMQf2c6cUvRzENipikRohyh4xUPds35+OWx660sy /7M3mWOuBwldueom1z7GRFoBOQ4hEsvyWXck3VPukzC5QHfatBY2gKCzFcthNgwjhnlB UA/w== X-Gm-Message-State: AJIora8yjb9OITgYJVHZiCOhYLs19t7pIFoLguhiSibLer85dqQTtg1E 8dKrbXeT2VMLAyCtmaKQMQgQTgRUhalIzw== X-Google-Smtp-Source: AGRyM1vlBH8NEpwVUEbv/i+rMUr5J6xUXN1pxtvPrawy2aSjSR08JPIttr44qbJi+0XNmf/61m6W9A== X-Received: by 2002:a17:902:ccd0:b0:16c:5d4f:99f3 with SMTP id z16-20020a170902ccd000b0016c5d4f99f3mr3526967ple.139.1657642022689; Tue, 12 Jul 2022 09:07:02 -0700 (PDT) Received: from HB2.. ([223.230.62.254]) by smtp.gmail.com with ESMTPSA id u188-20020a6260c5000000b0050dc7628183sm7232376pfb.93.2022.07.12.09.06.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Jul 2022 09:07:02 -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 v4 3/4] ident: rename commit_rewrite_person() to apply_mailmap_to_header() Date: Tue, 12 Jul 2022 21:36:33 +0530 Message-Id: <20220712160634.213956-4-siddharthasthana31@gmail.com> X-Mailer: git-send-email 2.37.0.6.g69b7ad898b In-Reply-To: <20220712160634.213956-1-siddharthasthana31@gmail.com> References: <20220709154149.165524-1-siddharthasthana31@gmail.com> <20220712160634.213956-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 | 2 +- revision.c | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cache.h b/cache.h index c9dbe1c29a..9edb7fefd3 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 *buf, const char **commit_headers, struct string_list *mailmap); +void apply_mailmap_to_header(struct strbuf *buf, const char **headers, struct string_list *mailmap); /* * Compare split idents for equality or strict ordering. Note that we diff --git a/ident.c b/ident.c index 9f4f6e9071..5f17bd607d 100644 --- a/ident.c +++ b/ident.c @@ -393,7 +393,7 @@ static ssize_t rewrite_ident_line(const char* person, struct strbuf *buf, struct return 0; } -void commit_rewrite_person(struct strbuf *buf, const char **headers, struct string_list *mailmap) +void apply_mailmap_to_header(struct strbuf *buf, const char **headers, 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 Tue Jul 12 16:06:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddharth Asthana X-Patchwork-Id: 12915267 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 1A473C433EF for ; Tue, 12 Jul 2022 16:07:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234204AbiGLQHc (ORCPT ); Tue, 12 Jul 2022 12:07:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49368 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234211AbiGLQHL (ORCPT ); Tue, 12 Jul 2022 12:07:11 -0400 Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3BD81C84CB for ; Tue, 12 Jul 2022 09:07:10 -0700 (PDT) Received: by mail-pf1-x430.google.com with SMTP id o12so7846148pfp.5 for ; Tue, 12 Jul 2022 09:07:10 -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=FBuJyKZhgWkLGgD0Rnbj4lGJBfJ9y5BUbI3MJC8ywjM=; b=o2rpD4j40058DG6uzttnIHjmgVIak5K9qd/pMGMEamfEsQH3+BW9qdSG4OIDtNXsLw f468dx+nPjLYDq1J+ldZ9Qb2VCVOStqOUKy4A6xd1uLHD4bUcw6aY6JDNBaQYJjtZOX4 Wrt37UMvRdrwVFpg2dpAR+2/mTDnP1W4rWOZr2EsAp2X+W68trB8kSaNi2P9ADGITqmh tJ9BhMlqDG47b2r2uZsfg4Ijq3jo2mo8iFlM/MS6eBFONghugxpngqVN6pNoNKooxTNU S3+/J80z10Fkp5NDK87B4nDGFFeCwX48O8n7SQrUaT0/J7ujmOTnQLTfk87CNNjZxi8H q5tg== 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=FBuJyKZhgWkLGgD0Rnbj4lGJBfJ9y5BUbI3MJC8ywjM=; b=Ji0IsJE2+IF4QHa+5nkI4ntD423/y6KU1knjKCrM7joxcZgo6/wAfaANzQjSnAbLw4 btFA6O4H1Yj/eK2N9Zr5owhzkfhH8Jkt7JhG8pZc2MWlE7DWJ3z0OB/vu2jC3S1yBTt7 ukgwyziHYgpThTA7nbESNWtQfiimO2vDyqvuQkPm9gYRviAbqCFIAgvvppr56WRBKAlA jedaI5l+ubin208CGQJAAkvJMX2GiBzjk6SpfBzGwhL11UTM31uAa6dfaZYc/KjMK5Bc m5oIgZDWwOdVQH3CcJIXWrX2QcKzth1p0ValCWWg4Fxok+Sv42SUawOoOKTQ5fSkKlKZ gmAQ== X-Gm-Message-State: AJIora+jQ9TrrGSlFa1TAY3zllTsivuAxeNdGpVkg5Ck03relSCsyfix NNUSkluiFL7Jj8NPoY/gqnpnAmOk33cf+nsI X-Google-Smtp-Source: AGRyM1tZoxCBguF7RWZ+H8o7uBUAIWmd64c7f3vj2lANc8++7cJMBXBLeoefRGEkwKnukSeL8wAnFg== X-Received: by 2002:a65:6b92:0:b0:39c:c97b:1b57 with SMTP id d18-20020a656b92000000b0039cc97b1b57mr21306597pgw.517.1657642029096; Tue, 12 Jul 2022 09:07:09 -0700 (PDT) Received: from HB2.. ([223.230.62.254]) by smtp.gmail.com with ESMTPSA id u188-20020a6260c5000000b0050dc7628183sm7232376pfb.93.2022.07.12.09.07.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Jul 2022 09:07:07 -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 v4 4/4] cat-file: add mailmap support Date: Tue, 12 Jul 2022 21:36:34 +0530 Message-Id: <20220712160634.213956-5-siddharthasthana31@gmail.com> X-Mailer: git-send-email 2.37.0.6.g69b7ad898b In-Reply-To: <20220712160634.213956-1-siddharthasthana31@gmail.com> References: <20220709154149.165524-1-siddharthasthana31@gmail.com> <20220712160634.213956-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. The tests added in this patch series rely on the side effects of the earlier test case `set up symlink tests`. However, that test case is guarded behind the `SYMLINKS` prereq, therefore it is not run e.g. on Windows which can cause the added tests to fail on Windows. So, fix that by removing the prereq from the `set up` test case, and adjusting its title to reflect its broadened responsibility. 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 | 56 +++++++++++++++++++++++++++++++++- 3 files changed, 103 insertions(+), 2 deletions(-) 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..dba0a8ac56 100755 --- a/t/t4203-mailmap.sh +++ b/t/t4203-mailmap.sh @@ -932,7 +932,7 @@ test_expect_success 'find top-level mailmap from subdir' ' test_cmp expect actual ' -test_expect_success SYMLINKS 'set up symlink tests' ' +test_expect_success 'prepare for symlink/--use-mailmap tests' ' git commit --allow-empty -m foo --author="Orig " && echo "New " >map && rm -f .mailmap @@ -963,4 +963,58 @@ test_expect_success SYMLINKS 'symlinks not respected in-tree' ' test_cmp expect actual ' +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