From patchwork Thu Jul 7 16:15:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddharth Asthana X-Patchwork-Id: 12909905 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 0F82CC433EF for ; Thu, 7 Jul 2022 16:16:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235644AbiGGQQv (ORCPT ); Thu, 7 Jul 2022 12:16:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56582 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235591AbiGGQQu (ORCPT ); Thu, 7 Jul 2022 12:16:50 -0400 Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8647A1E0 for ; Thu, 7 Jul 2022 09:16:49 -0700 (PDT) Received: by mail-pg1-x52c.google.com with SMTP id 73so3976983pgb.10 for ; Thu, 07 Jul 2022 09:16:49 -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=jGmLHt+fwanfeVPAmwUHIHJo7iGKT9PG/C9ONk37DJI=; b=ox5vr0wlNs7xB/GSwnzDyFe97iEQPe8H23d3+kpzBWqhFGN74CEr93jLL26DlVaz50 phYm1zOMiGmqEoSpHqtxW+mCA/BNAv8MjW+H0CLsc+5SslAk/A3frpL3ngimJDj6GgI0 4SXc5xq/qRCJ/wcSgbwH9gW8Djv8k2RLrK1pO7wChBARPc3Dck6hhkWVmqeFC4rSB5MW CQjFOhOV2e6YxZ6MZhF/BgvQRLUqGeIQjJrZS7TfUfsdwR+Oz3ajA1bCa/3O8Guw4ycy rCWUK+oIzJfufCp6lVNe7qDkHQxOaXlbxKLl65S5AOcAF6aAj4z8+2OCI/EWX9/fA4jf 7DLA== 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=jGmLHt+fwanfeVPAmwUHIHJo7iGKT9PG/C9ONk37DJI=; b=jiule9mfp7Ss2DI6oUCeoFQPE9iAY/WkUvXrpkvwcN1AVdGmLPC9pjCoVy6VERDkvp V3Z0OV+HlnKvLOR7VCGx/hPNTiUHdnrwp9eGEQZWTfl9Fj/t11aiyf8VB7dqJRqCrnLd 5WJuHC8FhuHmY3MJGbE2IwGM1pgIyJqZHg1ATockIN3Y8etpJcuirseH9gkUU1xJaP/N Nbw9xzCmgbCOTUx0Lr3hwdhe7m/amHBNHONKhqgkvxP0NOciA9dwop5voKmYSS2F4dJT sPLysMm1/wsBu53msbFUFvvIYj/nj5RnzAALXSHtFdS8rKJEK8+CK4qPD/kS4/HokPow RM6w== X-Gm-Message-State: AJIora8Qhp1zrjtxXZhAXe8AbltSNvGbd//gmvaDjIKZR13uVYFXgBd7 chm1HKlHKtU6FVaJ43ZIPiTGtK27+8QNh/Qr X-Google-Smtp-Source: AGRyM1vk1oEnNHzfP4f1qxjyR4YZVyZXJBzwhLJ79BRAIBMxhWkuuwr21H8vjaT/TOloEXrvyn/mDg== X-Received: by 2002:a17:90b:4d07:b0:1ef:521c:f051 with SMTP id mw7-20020a17090b4d0700b001ef521cf051mr6263620pjb.164.1657210608437; Thu, 07 Jul 2022 09:16:48 -0700 (PDT) Received: from HB2.. ([122.175.103.204]) by smtp.gmail.com with ESMTPSA id b2-20020a170902a9c200b0015ee985999dsm27996199plr.97.2022.07.07.09.16.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Jul 2022 09:16:47 -0700 (PDT) From: Siddharth Asthana To: git@vger.kernel.org Cc: Siddharth Asthana , Christian Couder , John Cai Subject: [PATCH v2 1/4] revision: improve commit_rewrite_person() Date: Thu, 7 Jul 2022 21:45:51 +0530 Message-Id: <20220707161554.6900-2-siddharthasthana31@gmail.com> X-Mailer: git-send-email 2.37.0.6.ga6a61a26c1.dirty In-Reply-To: <20220707161554.6900-1-siddharthasthana31@gmail.com> References: <20220630142444.651948-1-siddharthasthana31@gmail.com> <20220707161554.6900-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 occuranace of "author A U Thor Mentored-by: John Cai Signed-off-by: Siddharth Asthana --- revision.c | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/revision.c b/revision.c index 211352795c..83e68c1f97 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,39 @@ 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 = buf->buf + buf_offset; + int i, linelen = strchrnul(line, '\n') - line + 1; + + if (!linelen || 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; @@ -3835,8 +3859,8 @@ static int commit_match(struct commit *commit, struct rev_info *opt) if (!buf.len) strbuf_addstr(&buf, message); - commit_rewrite_person(&buf, "\nauthor ", opt->mailmap); - commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap); + const char *commit_headers[] = { "author ", "committer ", NULL }; + commit_rewrite_person(&buf, commit_headers, opt->mailmap); } /* Append "fake" message parts as needed */ From patchwork Thu Jul 7 16:15:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddharth Asthana X-Patchwork-Id: 12909906 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 42466C43334 for ; Thu, 7 Jul 2022 16:16:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235713AbiGGQQ5 (ORCPT ); Thu, 7 Jul 2022 12:16:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235591AbiGGQQ4 (ORCPT ); Thu, 7 Jul 2022 12:16:56 -0400 Received: from mail-pg1-x52d.google.com (mail-pg1-x52d.google.com [IPv6:2607:f8b0:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E222ED83 for ; Thu, 7 Jul 2022 09:16:54 -0700 (PDT) Received: by mail-pg1-x52d.google.com with SMTP id bh13so13446487pgb.4 for ; Thu, 07 Jul 2022 09:16:54 -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=Sb3Xt4/FdJpTEiPP63gXzuOYKvNKA1S1+kZ+WSpAdoY=; b=hI7tN6y9Hg6FQOKftvnX053MPCNmv5EjIoYUxXJvhsTuPYDPCoiCziGoSVQWiEPgF6 bsiDAS4MHsUFQ/FUjSYdoVTDtUbj6Yx9D1SdTzxiHnXs6ZDZ8AK/lQo1IlsEyFKij5bl Qh0d8vdes6jYph04j2/M9QYnJt+aH/Jmk6rc7TQiicFkf9E23TbvbPr2g0KeVLoAbZfg oi0RRDhaZi93co5Fjrw2UzvUZXe4agI5EMuR50kNt9YkXPoGwAgHNpm6evD6wfi7kd/n TaxJSKixryGJ0w60cgyZJi8E28TUrROkDOArRPsgWX9n05MuTaCEwRL1S46XFkVajlvn xDsA== 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=Sb3Xt4/FdJpTEiPP63gXzuOYKvNKA1S1+kZ+WSpAdoY=; b=H8G4SC7/lCIIcShKuuyztwSvWqHwvo1KusS3UdJvt/7gEJ4c6GUBWbp2HsQHQjQCop CRGMz4wpI7GDkBDyuMXXRX52cMndzNGYxqXfIO+ufUjflMA/Dz78FaQXPx85mYRpdwmS wkd6lsyb6IRBTmNPlWD7bDthd68vSRRHvpIyaacAvkfCffrEe1qD+Pa1PF8WXzshIGmh uHwMgn/uNNGJ7A1EROKeA36S/WkgOaVoZ9KIuzAGjs/EiRhFs307YxqjoznEexQWN9eg fsRdBjrAdHz1sH4OTbvaOCHz/VJ3hopPeFZSRybLW3UVEUyvCsf0KSIxR8wG44dnHwoX LgXg== X-Gm-Message-State: AJIora8N5t0U7W7JKRYUxjXmTGFiT3FJEZm7WhPyDJGf5kF981YNJ8NN Ir+7WaKOlInrLrfvfLZiY/idDlm4czBbLYAF X-Google-Smtp-Source: AGRyM1vNz4Rzq18Jjsd+d+hGfNfE9FMaA/xZvCEV7lk7eB5/Tx6tPQx4nm96IhRXCyITzNK492SvTg== X-Received: by 2002:a17:902:7807:b0:16b:e3d5:b2ce with SMTP id p7-20020a170902780700b0016be3d5b2cemr22632204pll.18.1657210613870; Thu, 07 Jul 2022 09:16:53 -0700 (PDT) Received: from HB2.. ([122.175.103.204]) by smtp.gmail.com with ESMTPSA id b2-20020a170902a9c200b0015ee985999dsm27996199plr.97.2022.07.07.09.16.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Jul 2022 09:16:53 -0700 (PDT) From: Siddharth Asthana To: git@vger.kernel.org Cc: Siddharth Asthana , Christian Couder , John Cai Subject: [PATCH v2 2/4] ident: move commit_rewrite_person() to ident.c Date: Thu, 7 Jul 2022 21:45:52 +0530 Message-Id: <20220707161554.6900-3-siddharthasthana31@gmail.com> X-Mailer: git-send-email 2.37.0.6.ga6a61a26c1.dirty In-Reply-To: <20220707161554.6900-1-siddharthasthana31@gmail.com> References: <20220630142444.651948-1-siddharthasthana31@gmail.com> <20220707161554.6900-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 | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ revision.c | 69 ------------------------------------------------------ 3 files changed, 75 insertions(+), 69 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..26cc60b2e1 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,74 @@ 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 = buf->buf + buf_offset; + int i, linelen = strchrnul(line, '\n') - line + 1; + + if (!linelen || 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 83e68c1f97..49d15e74ff 100644 --- a/revision.c +++ b/revision.c @@ -3755,75 +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 = buf->buf + buf_offset; - int i, linelen = strchrnul(line, '\n') - line + 1; - - if (!linelen || 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 Thu Jul 7 16:15:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddharth Asthana X-Patchwork-Id: 12909907 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 14032C43334 for ; Thu, 7 Jul 2022 16:17:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235763AbiGGQRE (ORCPT ); Thu, 7 Jul 2022 12:17:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56686 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235785AbiGGQRA (ORCPT ); Thu, 7 Jul 2022 12:17:00 -0400 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 419661114 for ; Thu, 7 Jul 2022 09:16:59 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id n12so20426558pfq.0 for ; Thu, 07 Jul 2022 09:16:59 -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=V0vdM8K2mbm+Cl+ZkmE3Ne+ZLtrnbM0pblWcQvZ3JZY=; b=ZqmZNb1smqIwXoU0SRH2MCQA2nZ6e+4lpZJ4/1kWNKWVfnrMvjSyLQ0hLFCZdTwM6e +42/Rrj8E2KxJhEhUC86XCqjWDYWDwQefBJ4xtODrh5Dl0njeV24CbbnY+HuPEjsbevC k/TqMXailHta9vzJ880N0p+qZVgLDO0HYr0xot817bwnY6MKugLw99CDYvjtqexpFwG7 dF4kS+Lmp4tNe5R4V+8OXEVwxzlhCP0Z7fdbXea9f6kqXqqsdaZrYqQI6KTtdMUzYvF2 Q8ba6hTdb5q+4hEMRg6DQ5bCKXc9K85Fris/lUn9KpDbQzo/HtJaF4sT4QcNGwadN2Vs XYbg== 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=V0vdM8K2mbm+Cl+ZkmE3Ne+ZLtrnbM0pblWcQvZ3JZY=; b=scTmrrkrH4gd2j4KQN6a3kr/i5+EVkEWLwwyTdxNFQA3xFl/ubP07wf1oldPkqE99D bjomoAXarcbYnU76VW7ECtv2bnektOkfnKwSJB3UunXpZIh05WihySBkcOTiPekLyDPf HxODVj1KjLQeLhZLxyYV6s2cKX1u5YxeIlomhGeykuUDtZv6BkPS9j/BwWfumyt6EzX6 phxgIHe9j0fXmMjgA1gTdGZc2mhPJW/ffO6xk47kZBv49PIwo6W+s0I8c5Ep8E6kb1qR 5AJUg9+Uf2ZknIBlAt9A2ZeLWGd5PZpnjg9e1kkhVn/qN6MyQ6qDjPOuNgRuWIq7OLOm 1kaA== X-Gm-Message-State: AJIora8tg9ZGG2OckVfQ41iuXGUYG6oJxEPno3pGR6Xzizs1fGg71bYk nlgmeY84y7/FJFn4lMIrcLx1Qjp50+0UqmkI X-Google-Smtp-Source: AGRyM1uIpjQwNSN1Qx392DmfZ17CPW71wsGEiXdPPczMESEImgDmgscTaVOF9sZO8+mVZth244icLQ== X-Received: by 2002:a05:6a00:23ca:b0:525:28b4:9e3b with SMTP id g10-20020a056a0023ca00b0052528b49e3bmr52505496pfc.43.1657210618193; Thu, 07 Jul 2022 09:16:58 -0700 (PDT) Received: from HB2.. ([122.175.103.204]) by smtp.gmail.com with ESMTPSA id b2-20020a170902a9c200b0015ee985999dsm27996199plr.97.2022.07.07.09.16.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Jul 2022 09:16:57 -0700 (PDT) From: Siddharth Asthana To: git@vger.kernel.org Cc: Siddharth Asthana , Christian Couder , John Cai Subject: [PATCH v2 3/4] ident: rename commit_rewrite_person() to apply_mailmap_to_header() Date: Thu, 7 Jul 2022 21:45:53 +0530 Message-Id: <20220707161554.6900-4-siddharthasthana31@gmail.com> X-Mailer: git-send-email 2.37.0.6.ga6a61a26c1.dirty In-Reply-To: <20220707161554.6900-1-siddharthasthana31@gmail.com> References: <20220630142444.651948-1-siddharthasthana31@gmail.com> <20220707161554.6900-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 26cc60b2e1..8503098f29 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 49d15e74ff..b561d6b5b5 100644 --- a/revision.c +++ b/revision.c @@ -3791,7 +3791,7 @@ static int commit_match(struct commit *commit, struct rev_info *opt) strbuf_addstr(&buf, message); const char *commit_headers[] = { "author ", "committer ", NULL }; - 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 Thu Jul 7 16:15:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddharth Asthana X-Patchwork-Id: 12909908 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 5D300C43334 for ; Thu, 7 Jul 2022 16:17:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235797AbiGGQRJ (ORCPT ); Thu, 7 Jul 2022 12:17:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56756 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235788AbiGGQRG (ORCPT ); Thu, 7 Jul 2022 12:17:06 -0400 Received: from mail-pg1-x52d.google.com (mail-pg1-x52d.google.com [IPv6:2607:f8b0:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03BC81114 for ; Thu, 7 Jul 2022 09:17:03 -0700 (PDT) Received: by mail-pg1-x52d.google.com with SMTP id bh13so13447191pgb.4 for ; Thu, 07 Jul 2022 09:17: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=bCBwbpnvVcG3k36puu/RKxkaLjeq7Lz6ei2RFDqlNHU=; b=N1MJDalJqin0KV+v+BK/IPuTNRsECo6QReYM/nJUNQintv6CGq+2ej7eaDTYrMhd2p ZmEkdV3t1iL58Ob2PgWQsP7HeUsFVqaJ6sh0i7dPdpjKtgbdMBBSwgA8T5yv8a2/hzV7 bNcHuVkOvIKrY/9THAnQJrx17tSaP05u5sJTZP5g7CYc7+JVJpi57L0cLQK6xfiMbWGt W7izFJ1J0gzFJUvHn6m75Kpu7LYDSm4+lYXZeF9y/o8V0iigQ0jXpYPLotyBYkHIDZoN tur4nHodIRu45YoISzN3/QYIZ/KXHIYSoEnE4mP0nG5oP11VXK9cUzJcd1Z2XDAEmbmL GPXA== 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=bCBwbpnvVcG3k36puu/RKxkaLjeq7Lz6ei2RFDqlNHU=; b=oVgeLUzs5LfDioX4GQVbQud1QvjvP8Hcc/vQilqIh6LN4xZstAz4oAzcALUrU1BY1b Nj45mD90GEiJObp2ZNajn+jgax2ifiadKAikHiCfHHA0muEuusKCSEbsWIoq8sAu5Yzl 58v2rsM/doUAipYxjyBdTHNFQNBsDcjvBL+ZSFNhMBKcWl2kW+CXQTt7tmiuUTfeAHwr XTJ7sthW4YvOtCq02PzV9m+QTHo3yGeFrr50dqjpfXFvcpnTXjeddJv06x0z1IfjczFz fvgXr/YeEZvTPILEcGBVqrrUC2yZ2VO2uYVgyoN/EZjGfmL57osA4GOcuTMuv97ShM4t sgCg== X-Gm-Message-State: AJIora8ZWVfhHZrqw/M+XjGXHOCFFO4mu0pjXxrOB7wjhL2pWA0IrZrX IiDOjeGNtLFodZu5ZYokbwdEoMD7A2WBg+PP X-Google-Smtp-Source: AGRyM1v7dZJRwETUe9X/oV4u2d9jH5NyzpNaM4qwbvzwrXGtsAMYoIEg9YlfTjjXk1ODyFA9m7LANQ== X-Received: by 2002:a17:902:ba93:b0:16b:f484:65d3 with SMTP id k19-20020a170902ba9300b0016bf48465d3mr14307607pls.39.1657210622784; Thu, 07 Jul 2022 09:17:02 -0700 (PDT) Received: from HB2.. ([122.175.103.204]) by smtp.gmail.com with ESMTPSA id b2-20020a170902a9c200b0015ee985999dsm27996199plr.97.2022.07.07.09.17.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Jul 2022 09:17:02 -0700 (PDT) From: Siddharth Asthana To: git@vger.kernel.org Cc: Siddharth Asthana , Christian Couder , John Cai , Phillip Wood Subject: [PATCH v2 4/4] cat-file: add mailmap support Date: Thu, 7 Jul 2022 21:45:54 +0530 Message-Id: <20220707161554.6900-5-siddharthasthana31@gmail.com> X-Mailer: git-send-email 2.37.0.6.ga6a61a26c1.dirty In-Reply-To: <20220707161554.6900-1-siddharthasthana31@gmail.com> References: <20220630142444.651948-1-siddharthasthana31@gmail.com> <20220707161554.6900-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. Mentored-by: Christian Couder Mentored-by: John Cai Helped-by: Phillip Wood Signed-off-by: Siddharth Asthana Signed-off-by: Johannes Schindelin --- Documentation/git-cat-file.txt | 6 ++++ builtin/cat-file.c | 31 ++++++++++++++++++- t/t4203-mailmap.sh | 54 ++++++++++++++++++++++++++++++++++ 3 files changed, 90 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..6dc750a367 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,19 @@ struct batch_options { static const char *force_path; +static struct string_list mailmap = STRING_LIST_INIT_NODUP; +static int use_mailmap; + +char *replace_idents_using_mailmap(char *object_buf, size_t *size) +{ + struct strbuf sb = STRBUF_INIT; + strbuf_attach(&sb, object_buf, *size, *size + 1); + const char *headers[] = { "author ", "committer ", "tagger ", NULL }; + 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 +166,9 @@ 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) + buf = replace_idents_using_mailmap(buf, &size); + /* otherwise just spit out the data */ break; @@ -183,6 +200,9 @@ 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) + buf = replace_idents_using_mailmap(buf, &size); break; } default: @@ -348,11 +368,15 @@ 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) + contents = replace_idents_using_mailmap(contents, &size); + 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 +867,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 +911,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..c60a90615c 100755 --- a/t/t4203-mailmap.sh +++ b/t/t4203-mailmap.sh @@ -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