From patchwork Tue Jun 6 19:48:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Wan X-Patchwork-Id: 13269697 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 49F7CC7EE37 for ; Tue, 6 Jun 2023 19:48:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237982AbjFFTs4 (ORCPT ); Tue, 6 Jun 2023 15:48:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234174AbjFFTss (ORCPT ); Tue, 6 Jun 2023 15:48:48 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76B8A10D7 for ; Tue, 6 Jun 2023 12:48:47 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5659c7dad06so108706897b3.0 for ; Tue, 06 Jun 2023 12:48:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686080926; x=1688672926; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Me8fC0GJGFHq0cZ62aFPQeZZE7cpulW1nJzI6lf+GjI=; b=sv8BWLS+Wm8uigw9H/FKRmRhzqsTHSois3tBDx6Aq67I8kkkVD83eQTS0fkBWuKy2T OxRJcLXS4xPn43VKqs/k5ztlxkhHkXBiR+zcu71mJVLErcHl7ldkAuPfoXTEixk1G6lM Wp9C11Qv+Ngb7qN3bARb35wPTZLuYzrCgAxniXjWfgaZLFhSTV1zBbxOD/WRWP6nQ7nf JAa2XkAKQZhEj/MJHRsKZ1WPtJQtYyMS0T4BdzMe2waiQ2N8fWxJ449hMprqLgU3TB+5 R75QhdMLEsn0FaUcTAFXUPKG9lfTZKXRvcwRQ00sDfu2Hz3qt6N7PXzOro43nn7cMTT3 ljSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686080926; x=1688672926; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Me8fC0GJGFHq0cZ62aFPQeZZE7cpulW1nJzI6lf+GjI=; b=l+o+Fci67L5SH23OWtXCiSjbQNTCwmEYhW+QioggwoEJ6LfNGMqLRHZSI+r0FF4Lks d5tJJjAyks1Hk3gKUi8nEBeXJv+xnaEzVC1aXf0PKYHi8YidCV6PM0PeKATDYhxj36Fe x6AjF5JKx9wQ8OWyE+H3Y6avk0+4BiWG+64YDVwjE9/Ds2AGxsWcPsV7uT/DDcNvI+Pk 7/3jWjuJr8rT4Z4catCZE+yEpSMWuAhqi7EfQ7vkE6jSXDd9L+OJPhKiimFlnDWhjPQ/ rcbRJmev5k3LIfm3BE3IbKpY3wxCEC9StMNbjZQV2o4Zp4fS4BEKyMfoqocibeJaKv7e shMA== X-Gm-Message-State: AC+VfDyulsbzzsNvKhS9iXCggcoPGgorhCDLmraQWKyjOZEQPzDblQFU j/GEiIAP4Ew8uoneILNHfL844W1FW2RdPmKUUvLibai6LymenyuCnStUFbAN6ZRsGcCjoiDz2HL tG8Sz3GkRFLjXLha9vHwIUsew3klcL2lbBdppS1RrEIHzliewmCXQ+2AWflsQAPWLIQ== X-Google-Smtp-Source: ACHHUZ6E/Yn7qj6o4XP2a26cG8zTEQlimm1HGhdWKYxvPr0DS2EXc+GqHOF8RrG91qbewpUzTno4dL9HVRZcXn0= X-Received: from barleywine.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3bd4]) (user=calvinwan job=sendgmr) by 2002:a81:dc03:0:b0:55d:8472:4597 with SMTP id h3-20020a81dc03000000b0055d84724597mr1651848ywj.10.1686080926669; Tue, 06 Jun 2023 12:48:46 -0700 (PDT) Date: Tue, 6 Jun 2023 19:48:37 +0000 In-Reply-To: <20230606194720.2053551-1-calvinwan@google.com> Mime-Version: 1.0 References: <20230606194720.2053551-1-calvinwan@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230606194843.2054314-1-calvinwan@google.com> Subject: [PATCH v7 1/7] strbuf: clarify API boundary From: Calvin Wan To: git@vger.kernel.org Cc: Calvin Wan Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org strbuf, as a generic and widely used structure across the codebase, should be limited as a library to only interact with primitives. Add documentation so future functions can appropriately be placed. Older functions that do not follow this boundary should eventually be moved or refactored. Signed-off-by: Calvin Wan --- strbuf.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/strbuf.h b/strbuf.h index 3dfeadb44c..70778c6e10 100644 --- a/strbuf.h +++ b/strbuf.h @@ -1,6 +1,14 @@ #ifndef STRBUF_H #define STRBUF_H +/* + * NOTE FOR STRBUF DEVELOPERS + * + * strbuf is a low-level primitive; as such it should interact only + * with other low-level primitives. Do not introduce new functions + * which interact with higher-level APIs. + */ + struct string_list; /** From patchwork Tue Jun 6 19:48:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Wan X-Patchwork-Id: 13269698 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 E2445C77B7A for ; Tue, 6 Jun 2023 19:48:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238612AbjFFTs5 (ORCPT ); Tue, 6 Jun 2023 15:48:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237920AbjFFTst (ORCPT ); Tue, 6 Jun 2023 15:48:49 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06BB018E for ; Tue, 6 Jun 2023 12:48:49 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1b03f9dfd52so25900095ad.3 for ; Tue, 06 Jun 2023 12:48:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686080928; x=1688672928; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=2aGN9nzp+rpkMNIrIGBkrs93WwNFnCMhWBLomfxJlUo=; b=sZO58U7FA8Kj3tO42n49+Ny3USwgu9HVeLkgbqE1pa0rKLlQm+RIHXLI9AltqelIAo ImOwjB/AMo11bHoUX54zdgpqZsfOTEHn4faaUpw7c6ToWMgEZ2Zp25JRHuCXHPFDHQdS hOtmxvP1q0J16w0FHdudyZnh1vhua4hSC3nl/zr0URNJtHBxi/Y5M9rWsdXboTMPUtOp 5JYj/e3WfkNI5xbls74SP+oV0WXKQC5aw8WVFq0uho0DX+cKMi90j9j7In/V3EZx3Jlt 78d3/TErryWQFbDMoAcx1nEe1WbEUsahvBB24+SY7WxDZ3Wb4QmGkHnCDNaKqXBVlONz LsSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686080928; x=1688672928; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=2aGN9nzp+rpkMNIrIGBkrs93WwNFnCMhWBLomfxJlUo=; b=LrFOUwQqnHQnXrZxJ3Oh3NpLN0rp1InXSs2z/ZeKkCWK54wiTJmNyWXyph0URrQuiZ xJi1CAn81D9huidKj18UAYK6sdiaPZKT84RVVhc+NOyUhzsxEOZzz8fEomrkmzhXXCy3 RSHfsNDtC+1pfV++CxhwnuJ2+UuTsbtdniXbet3M260CW3Fvy0HztZt3/SXUCnJME0TU mgnOo0dwYS1b06vl3b112Kt2t2j/ahMP+2tVB/p9iVzMyRmuHlP42YlpOlHscKzYc2CP ktcNMsXgZ92rkGGmkyYSl5Y26x5FVy6/B06oDfwoqRvxeB/HDkIUOuwRUr9pIxAUCAQM 9kGw== X-Gm-Message-State: AC+VfDzQHPf4eKI7bodXjn0fJ2Kln9NDaWuJ0AcGVhdRCw5ijK3vVww9 YGvrtqsW2mnGdfZeuJpQpnVfVHs/f8iU6Ci7yG53mjNxpZGr+0sgJb7z0SyRxH78xWPDB8hqcyk bpI9Y0KE4jHlqW6uIsSMHJEV+PqTiS+UantcBD6mLN6JGCBy2+dASLyKwQrKKq/UQPQ== X-Google-Smtp-Source: ACHHUZ4MoCK1q9wTj1v91WF6KTpJ75ahn1/EoWgNAR5e1vZ+drewHYX0lXGGMOzdGAw9ugbwXrwYTz8wbZgZGzM= X-Received: from barleywine.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3bd4]) (user=calvinwan job=sendgmr) by 2002:a17:902:dacc:b0:1ab:18eb:17c8 with SMTP id q12-20020a170902dacc00b001ab18eb17c8mr1001607plx.2.1686080928431; Tue, 06 Jun 2023 12:48:48 -0700 (PDT) Date: Tue, 6 Jun 2023 19:48:38 +0000 In-Reply-To: <20230606194720.2053551-1-calvinwan@google.com> Mime-Version: 1.0 References: <20230606194720.2053551-1-calvinwan@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230606194843.2054314-2-calvinwan@google.com> Subject: [PATCH v7 2/7] strbuf: clarify dependency From: Calvin Wan To: git@vger.kernel.org Cc: Calvin Wan Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org refs.h was once needed but is no longer so as of 6bab74e7fb8 ("strbuf: move strbuf_branchname to sha1_name.c", 2010-11-06). strbuf.h was included thru refs.h, so removing refs.h requires strbuf.h to be added back. Signed-off-by: Calvin Wan --- strbuf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strbuf.c b/strbuf.c index 08eec8f1d8..f5dfd093a0 100644 --- a/strbuf.c +++ b/strbuf.c @@ -5,8 +5,8 @@ #include "gettext.h" #include "hex.h" #include "object-name.h" -#include "refs.h" #include "repository.h" +#include "strbuf.h" #include "string-list.h" #include "utf8.h" #include "date.h" From patchwork Tue Jun 6 19:48:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Wan X-Patchwork-Id: 13269700 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 50D56C7EE37 for ; Tue, 6 Jun 2023 19:49:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239544AbjFFTtB (ORCPT ); Tue, 6 Jun 2023 15:49:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238417AbjFFTs4 (ORCPT ); Tue, 6 Jun 2023 15:48:56 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A177418E for ; Tue, 6 Jun 2023 12:48:50 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-6530a0823d8so1635146b3a.1 for ; Tue, 06 Jun 2023 12:48:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686080930; x=1688672930; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=eKwO+A+pzoDvN+IgHHmp+Gqw0htYhDVzxEzOCQqikIg=; b=xaCMP91cI2TuynuZ20ggMDoUaSJs/xEgVIiByX93webYTfJIZy4ywSOFVat3NByseD lJGKkI8Peohbfldry10aPQFSUZbIMoUOi3hWUh+soGQ3Q/h8SUM0WayiCFCUFobFI6Fu akvItkwynZ8Et+EKE3OEJNSHYjvcBA+0IBBXHk27NiF5Mpx51dVniRa7iNuc+AB4K9Pn 0PzERFE7Fubad1iOSMJBAmNg1DsVlG7RGuhuP/c/wPQlwrMyQbGn8IXAFhdYG3tNbFrO 6fTF4kQLovSrbB4SUKeWV80WxFfq3HKLLBFDdZblaKX+ixpwcy8Cv7mhYXd4yHtj9IFg O1eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686080930; x=1688672930; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=eKwO+A+pzoDvN+IgHHmp+Gqw0htYhDVzxEzOCQqikIg=; b=mAB/02v5G4wknqPYUSQrhQhUX7lvr0CAfy7v0h/fxiI0vTKwypGY7xzGV1s29mB73r KjgYvKlKjrCK3bpMp2XpySQg2r/MtIPJkCCQqexqHWG6fcycGO3OzkxXSByFgK1FyC/L QQBd0OS+uap3y/UZOzqlGspPuVApv6oa9BdXX9GqfdltGbdRJwPhRVmZqvsoqMTUnNSp ytHYSsXBsyQ88tyJexh3G03w3560bNGnqu9kg1W+6rVxYPFBmDIiM3lXq42pFp90ur8e ngDzleaMK8BLAuXAf/T6MAjI8N8t+1/R+PXeEMCIqgS2dvW1j9R+1tjdLO88aWfnZSNj ksaQ== X-Gm-Message-State: AC+VfDxoXv1sMiL/zDeJbpuzL7U8jg4msoh60c+s9KPbTu/61tB0r0b+ VoUC8g0gaCmvDjW+1DSTMw2B/l+X14L5H/juhLHccMhKvnWFTGQeAcbSx2UwFruIP89aoSASqem mdchzvwIgErhnbG8tzy7GS5biQshVzTuHv36ni0NXzciOzS+UuSndlA3fHW622TKbqg== X-Google-Smtp-Source: ACHHUZ4AgnsjHKnqx3Jay+zdEBvKyO8tHIgtmeY+IzAPwb6Btf6SZQCn3lHuBF4qkpLcbCUp5k/NzhF3AVwRtUM= X-Received: from barleywine.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3bd4]) (user=calvinwan job=sendgmr) by 2002:a05:6a00:2d24:b0:64f:4614:e0a1 with SMTP id fa36-20020a056a002d2400b0064f4614e0a1mr1133960pfb.3.1686080930050; Tue, 06 Jun 2023 12:48:50 -0700 (PDT) Date: Tue, 6 Jun 2023 19:48:39 +0000 In-Reply-To: <20230606194720.2053551-1-calvinwan@google.com> Mime-Version: 1.0 References: <20230606194720.2053551-1-calvinwan@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230606194843.2054314-3-calvinwan@google.com> Subject: [PATCH v7 3/7] abspath: move related functions to abspath From: Calvin Wan To: git@vger.kernel.org Cc: Calvin Wan Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Move abspath-related functions from strbuf.[ch] to abspath.[ch] so that strbuf is focused on string manipulation routines with minimal dependencies. Signed-off-by: Calvin Wan --- abspath.c | 36 ++++++++++++++++++++++++++++++++++++ abspath.h | 21 +++++++++++++++++++++ hook.c | 1 + strbuf.c | 37 ------------------------------------- strbuf.h | 22 ---------------------- tempfile.c | 1 + 6 files changed, 59 insertions(+), 59 deletions(-) diff --git a/abspath.c b/abspath.c index d032f5dce5..1202cde23d 100644 --- a/abspath.c +++ b/abspath.c @@ -289,3 +289,39 @@ char *prefix_filename_except_for_dash(const char *pfx, const char *arg) return xstrdup(arg); return prefix_filename(pfx, arg); } + +void strbuf_add_absolute_path(struct strbuf *sb, const char *path) +{ + if (!*path) + die("The empty string is not a valid path"); + if (!is_absolute_path(path)) { + struct stat cwd_stat, pwd_stat; + size_t orig_len = sb->len; + char *cwd = xgetcwd(); + char *pwd = getenv("PWD"); + if (pwd && strcmp(pwd, cwd) && + !stat(cwd, &cwd_stat) && + (cwd_stat.st_dev || cwd_stat.st_ino) && + !stat(pwd, &pwd_stat) && + pwd_stat.st_dev == cwd_stat.st_dev && + pwd_stat.st_ino == cwd_stat.st_ino) + strbuf_addstr(sb, pwd); + else + strbuf_addstr(sb, cwd); + if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1])) + strbuf_addch(sb, '/'); + free(cwd); + } + strbuf_addstr(sb, path); +} + +void strbuf_add_real_path(struct strbuf *sb, const char *path) +{ + if (sb->len) { + struct strbuf resolved = STRBUF_INIT; + strbuf_realpath(&resolved, path, 1); + strbuf_addbuf(sb, &resolved); + strbuf_release(&resolved); + } else + strbuf_realpath(sb, path, 1); +} diff --git a/abspath.h b/abspath.h index 7cd3de5e9d..4653080d5e 100644 --- a/abspath.h +++ b/abspath.h @@ -30,4 +30,25 @@ static inline int is_absolute_path(const char *path) return is_dir_sep(path[0]) || has_dos_drive_prefix(path); } +/** + * Add a path to a buffer, converting a relative path to an + * absolute one in the process. Symbolic links are not + * resolved. + */ +void strbuf_add_absolute_path(struct strbuf *sb, const char *path); + +/** + * Canonize `path` (make it absolute, resolve symlinks, remove extra + * slashes) and append it to `sb`. Die with an informative error + * message if there is a problem. + * + * The directory part of `path` (i.e., everything up to the last + * dir_sep) must denote a valid, existing directory, but the last + * component need not exist. + * + * Callers that don't mind links should use the more lightweight + * strbuf_add_absolute_path() instead. + */ +void strbuf_add_real_path(struct strbuf *sb, const char *path); + #endif /* ABSPATH_H */ diff --git a/hook.c b/hook.c index 3ca5e60895..f6306d72b3 100644 --- a/hook.c +++ b/hook.c @@ -1,4 +1,5 @@ #include "git-compat-util.h" +#include "abspath.h" #include "advice.h" #include "gettext.h" #include "hook.h" diff --git a/strbuf.c b/strbuf.c index f5dfd093a0..a2249ae7ed 100644 --- a/strbuf.c +++ b/strbuf.c @@ -1,5 +1,4 @@ #include "git-compat-util.h" -#include "abspath.h" #include "alloc.h" #include "environment.h" #include "gettext.h" @@ -900,42 +899,6 @@ void strbuf_humanise_rate(struct strbuf *buf, off_t bytes) strbuf_humanise(buf, bytes, 1); } -void strbuf_add_absolute_path(struct strbuf *sb, const char *path) -{ - if (!*path) - die("The empty string is not a valid path"); - if (!is_absolute_path(path)) { - struct stat cwd_stat, pwd_stat; - size_t orig_len = sb->len; - char *cwd = xgetcwd(); - char *pwd = getenv("PWD"); - if (pwd && strcmp(pwd, cwd) && - !stat(cwd, &cwd_stat) && - (cwd_stat.st_dev || cwd_stat.st_ino) && - !stat(pwd, &pwd_stat) && - pwd_stat.st_dev == cwd_stat.st_dev && - pwd_stat.st_ino == cwd_stat.st_ino) - strbuf_addstr(sb, pwd); - else - strbuf_addstr(sb, cwd); - if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1])) - strbuf_addch(sb, '/'); - free(cwd); - } - strbuf_addstr(sb, path); -} - -void strbuf_add_real_path(struct strbuf *sb, const char *path) -{ - if (sb->len) { - struct strbuf resolved = STRBUF_INIT; - strbuf_realpath(&resolved, path, 1); - strbuf_addbuf(sb, &resolved); - strbuf_release(&resolved); - } else - strbuf_realpath(sb, path, 1); -} - int printf_ln(const char *fmt, ...) { int ret; diff --git a/strbuf.h b/strbuf.h index 70778c6e10..207efb4f98 100644 --- a/strbuf.h +++ b/strbuf.h @@ -535,28 +535,6 @@ int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term); */ int strbuf_getcwd(struct strbuf *sb); -/** - * Add a path to a buffer, converting a relative path to an - * absolute one in the process. Symbolic links are not - * resolved. - */ -void strbuf_add_absolute_path(struct strbuf *sb, const char *path); - -/** - * Canonize `path` (make it absolute, resolve symlinks, remove extra - * slashes) and append it to `sb`. Die with an informative error - * message if there is a problem. - * - * The directory part of `path` (i.e., everything up to the last - * dir_sep) must denote a valid, existing directory, but the last - * component need not exist. - * - * Callers that don't mind links should use the more lightweight - * strbuf_add_absolute_path() instead. - */ -void strbuf_add_real_path(struct strbuf *sb, const char *path); - - /** * Normalize in-place the path contained in the strbuf. See * normalize_path_copy() for details. If an error occurs, the contents of "sb" diff --git a/tempfile.c b/tempfile.c index 50c377134c..6c88a63b42 100644 --- a/tempfile.c +++ b/tempfile.c @@ -43,6 +43,7 @@ */ #include "git-compat-util.h" +#include "abspath.h" #include "path.h" #include "tempfile.h" #include "sigchain.h" From patchwork Tue Jun 6 19:48:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Wan X-Patchwork-Id: 13269702 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 EAB9AC77B7A for ; Tue, 6 Jun 2023 19:49:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239542AbjFFTtH (ORCPT ); Tue, 6 Jun 2023 15:49:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239117AbjFFTs5 (ORCPT ); Tue, 6 Jun 2023 15:48:57 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B82810D7 for ; Tue, 6 Jun 2023 12:48:52 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-2562fbb193bso2191596a91.1 for ; Tue, 06 Jun 2023 12:48:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686080932; x=1688672932; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ea/r2aFX5xsJ9Rb3DThSB+ojzHdABHASIBU1nJqtQJs=; b=g58QzathEK6gu8X8o6jrh57ejV3U8lcT9Fh0qJCArSKRI6iN8ltn1bU+ZNwkrlWKma xNgrvy0zbH3NWV6F+moFU7FC9BqmagLHKOAfOGqNl5wNfrCo+F0WLfTP+nU0EvCTzfMH acgZWnaGdWfBxTunznE7V9KfJJ6N83gV2Rh3ZOFYpOP2KIcyEvdzug1vcrzNVsb4b6sn QXt7HcMEYjpedCBDguCzJZWHYKOkQCEqfM9a91YR9cATDLnnXwQmXnZ2Hmr/A5aU3sUZ caH1JarG2GiWe2xnirIxLzZd/Pg4WSR3BWloBpUxIEaSPFdQI3LtdcmkyG6EUsgSmJsJ eQfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686080932; x=1688672932; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ea/r2aFX5xsJ9Rb3DThSB+ojzHdABHASIBU1nJqtQJs=; b=jP5lkuqQh5Fl97uVQbAy3Ke5bXov9fSQUEesteXDfyX1YIXi5maifNSnbflXOiMibf G/g+YjIsDfiSQeIM/7AVZLMX/qbVhVXvcWwmIuRhBUc0xCSY6+JNqS2rhC1aked+aJpV ufY71uIJEYAg7I1DTNi0uz5X0uRvdwUaAEeBVQHzSHp/4LRFJ6ZPYfcesTusM0iG+ftd WeXo3Y8FUip5IuFG7XYu9Opuk++JlvYTUMe4Q8TBoG5praD+AGIjfTNSjVloS61ykxGp CvaLVhcQ5EWwPAwwmbeAQ3SlIrjU3buEqLTiadDbVmucElu/vyAmy6ekEnJ/BqG6KCNU pvsA== X-Gm-Message-State: AC+VfDzKrD/O+5gpobmvOWCInKzZSBkJ8oiPVUMXtUTsF6Q/WI7X7mBm G9Q+psYHbbAtsIvEMM8aytZCdd32vzWUIJRBuP7ihgJjkMjkwTujmNeqrfKoIviAGmc2P6dMjxb RX5Fm7rVEkkcgFODnBTgsSIsGLRGMA/2NbwcpLhwu6knqCJ2Z4VCVEdYVHZP1uAxU+w== X-Google-Smtp-Source: ACHHUZ7r/sH2zYxEyuiCnPFF5QnVnYbZDoT4DM/O/gHwtkGdvG+D51oEvKXBetQCAcMAKFRHD/ebFNwauLmQOqg= X-Received: from barleywine.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3bd4]) (user=calvinwan job=sendgmr) by 2002:a17:90a:d311:b0:246:6a3a:6aec with SMTP id p17-20020a17090ad31100b002466a3a6aecmr849501pju.4.1686080931904; Tue, 06 Jun 2023 12:48:51 -0700 (PDT) Date: Tue, 6 Jun 2023 19:48:40 +0000 In-Reply-To: <20230606194720.2053551-1-calvinwan@google.com> Mime-Version: 1.0 References: <20230606194720.2053551-1-calvinwan@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230606194843.2054314-4-calvinwan@google.com> Subject: [PATCH v7 4/7] credential-store: move related functions to credential-store file From: Calvin Wan To: git@vger.kernel.org Cc: Calvin Wan Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org is_rfc3986_unreserved() and is_rfc3986_reserved_or_unreserved() are only called from builtin/credential-store.c and they are only relevant to that file so move those functions and make them static. Signed-off-by: Calvin Wan --- builtin/credential-store.c | 19 +++++++++++++++++++ strbuf.c | 19 ------------------- strbuf.h | 3 --- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/builtin/credential-store.c b/builtin/credential-store.c index 30c6ccf56c..5eea5bdb58 100644 --- a/builtin/credential-store.c +++ b/builtin/credential-store.c @@ -74,6 +74,25 @@ static void rewrite_credential_file(const char *fn, struct credential *c, die_errno("unable to write credential store"); } +static int is_rfc3986_unreserved(char ch) +{ + return isalnum(ch) || + ch == '-' || ch == '_' || ch == '.' || ch == '~'; +} + +static int is_rfc3986_reserved_or_unreserved(char ch) +{ + if (is_rfc3986_unreserved(ch)) + return 1; + switch (ch) { + case '!': case '*': case '\'': case '(': case ')': case ';': + case ':': case '@': case '&': case '=': case '+': case '$': + case ',': case '/': case '?': case '#': case '[': case ']': + return 1; + } + return 0; +} + static void store_credential_file(const char *fn, struct credential *c) { struct strbuf buf = STRBUF_INIT; diff --git a/strbuf.c b/strbuf.c index a2249ae7ed..5244029d91 100644 --- a/strbuf.c +++ b/strbuf.c @@ -810,25 +810,6 @@ void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s) } } -int is_rfc3986_reserved_or_unreserved(char ch) -{ - if (is_rfc3986_unreserved(ch)) - return 1; - switch (ch) { - case '!': case '*': case '\'': case '(': case ')': case ';': - case ':': case '@': case '&': case '=': case '+': case '$': - case ',': case '/': case '?': case '#': case '[': case ']': - return 1; - } - return 0; -} - -int is_rfc3986_unreserved(char ch) -{ - return isalnum(ch) || - ch == '-' || ch == '_' || ch == '.' || ch == '~'; -} - static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len, char_predicate allow_unencoded_fn) { diff --git a/strbuf.h b/strbuf.h index 207efb4f98..114ad0c024 100644 --- a/strbuf.h +++ b/strbuf.h @@ -690,9 +690,6 @@ int strbuf_check_branch_ref(struct strbuf *sb, const char *name); typedef int (*char_predicate)(char ch); -int is_rfc3986_unreserved(char ch); -int is_rfc3986_reserved_or_unreserved(char ch); - void strbuf_addstr_urlencode(struct strbuf *sb, const char *name, char_predicate allow_unencoded_fn); From patchwork Tue Jun 6 19:48:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Wan X-Patchwork-Id: 13269701 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 61AE6C77B7A for ; Tue, 6 Jun 2023 19:49:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239545AbjFFTtE (ORCPT ); Tue, 6 Jun 2023 15:49:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234174AbjFFTs4 (ORCPT ); Tue, 6 Jun 2023 15:48:56 -0400 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1343B10EC for ; Tue, 6 Jun 2023 12:48:54 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id d2e1a72fcca58-65fd8267042so586401b3a.0 for ; Tue, 06 Jun 2023 12:48:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686080933; x=1688672933; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=JLwJPLnEZ3/zrMEJGdaz9Us+UEY0SRoC+u4/9wL+WfY=; b=DPnfS6lhMk2ASVcv2e6BBHqG9qCLenpIzqwmcdYircuFv7CvO0kPjSVLTHrVSEfcJU SJde85vkMQIWxrhXoCuHzQ9AMrlxH6em3LiK3iFsPk6UHq0pVVCVdhG3p8NLq6XDfzE8 nu5X1zLjAA1cj9t5TdpgKMRof/UMxUsgb1rfWYtDUfA/NOkRrPEt5jUf1IZB/O1XGJyS 42flEyxEiGCpKK0lTDnv6EIqnvOmK9QFTbyB6fpjL4je2j6LEfwy2/6S11lxxfASkvru kdZXZzl/gIdTX4100fsTp6RP9L1tyzYRBgqAUvG7QGxSDCfn3P7dnw6K8+D7xzuNN/+Y JocQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686080933; x=1688672933; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=JLwJPLnEZ3/zrMEJGdaz9Us+UEY0SRoC+u4/9wL+WfY=; b=XvFm4Lh5SD0xfdPUzPLRhoLivtksHGbhKaIYE5JEgaVMvOB3xlyKErYatQIedT8tLy XkbiKfeRjq3NF4FUqrcbCNlJyX/DY5/XQ2qY4UTxx2aqVWUF0P4T1A3XpWe4ZcLdOkiH CldNfmYHjcmfnqpyTC5Q3Ho8ws2SXAA6iwM74YAb7h2rWah+V5a1bdkF5RrpDgXGc/lm Zx1sUMr6rXzGruzKF+o1MGN6tI5iQ1IpqAofmjpF9dIw6566ImVa+n910a8P5Ou08rWn pgtIFwjKBPi1Y3eyyR/YlKlEJtUGv7rWCA3zrvyCp9IyJ6hoZPtrxXzNIBKHj6TrRfZx uyPA== X-Gm-Message-State: AC+VfDw8J39lIKtT0dEqPHcKHsJUXVobkJPgIsvC0RAVeaWGCEnyot9I gm+VI1fGR6LINHEbPggUYqewCD09itktpV/N/ZeLY5rbm/WZT3qKrybOuRzKrOVS8Q2LpT1FZrt L7Z1H3QLCCezKlV8hKVl2FIGKP/VHHaNzpf/h8ZfseSV231Q50vEBN7RKIqKf7pTAQg== X-Google-Smtp-Source: ACHHUZ51fPbfd0PaF7zY9geaCgEpF2/yEXRcYJQZnUK5pk8LlIBMAxXg8SdsOh07rxs2EegXAG1P3Hr9PGbp7/Q= X-Received: from barleywine.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3bd4]) (user=calvinwan job=sendgmr) by 2002:a05:6a00:2d18:b0:653:3fbc:3188 with SMTP id fa24-20020a056a002d1800b006533fbc3188mr1135286pfb.6.1686080933500; Tue, 06 Jun 2023 12:48:53 -0700 (PDT) Date: Tue, 6 Jun 2023 19:48:41 +0000 In-Reply-To: <20230606194720.2053551-1-calvinwan@google.com> Mime-Version: 1.0 References: <20230606194720.2053551-1-calvinwan@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230606194843.2054314-5-calvinwan@google.com> Subject: [PATCH v7 5/7] object-name: move related functions to object-name From: Calvin Wan To: git@vger.kernel.org Cc: Calvin Wan Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Move object-name-related functions from strbuf.[ch] to object-name.[ch] so that strbuf is focused on string manipulation routines with minimal dependencies. dir.h relied on the forward declration of the repository struct in strbuf.h. Since that is removed in this patch, add the forward declaration to dir.h. Signed-off-by: Calvin Wan --- dir.h | 2 ++ object-name.c | 15 +++++++++++++++ object-name.h | 9 +++++++++ pretty.c | 1 + strbuf.c | 16 ---------------- strbuf.h | 10 ---------- 6 files changed, 27 insertions(+), 26 deletions(-) diff --git a/dir.h b/dir.h index 79b85a01ee..4d83febe9e 100644 --- a/dir.h +++ b/dir.h @@ -40,6 +40,8 @@ * */ +struct repository; + struct dir_entry { unsigned int len; char name[FLEX_ARRAY]; /* more */ diff --git a/object-name.c b/object-name.c index 6fc3fa595b..34e0f56703 100644 --- a/object-name.c +++ b/object-name.c @@ -768,6 +768,21 @@ static void find_abbrev_len_packed(struct min_abbrev_data *mad) find_abbrev_len_for_pack(p, mad); } +void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo, + const struct object_id *oid, int abbrev_len) +{ + int r; + strbuf_grow(sb, GIT_MAX_HEXSZ + 1); + r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len); + strbuf_setlen(sb, sb->len + r); +} + +void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid, + int abbrev_len) +{ + strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len); +} + int repo_find_unique_abbrev_r(struct repository *r, char *hex, const struct object_id *oid, int len) { diff --git a/object-name.h b/object-name.h index 1d63698f42..9ae5223071 100644 --- a/object-name.h +++ b/object-name.h @@ -40,6 +40,15 @@ struct object_context { const char *repo_find_unique_abbrev(struct repository *r, const struct object_id *oid, int len); int repo_find_unique_abbrev_r(struct repository *r, char *hex, const struct object_id *oid, int len); +/** + * Add the abbreviation, as generated by repo_find_unique_abbrev(), of `sha1` to + * the strbuf `sb`. + */ +void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo, + const struct object_id *oid, int abbrev_len); +void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid, + int abbrev_len); + int repo_get_oid(struct repository *r, const char *str, struct object_id *oid); __attribute__((format (printf, 2, 3))) int get_oidf(struct object_id *oid, const char *fmt, ...); diff --git a/pretty.c b/pretty.c index 0bb938021b..78bac2d818 100644 --- a/pretty.c +++ b/pretty.c @@ -18,6 +18,7 @@ #include "gpg-interface.h" #include "trailer.h" #include "run-command.h" +#include "object-name.h" /* * The limit for formatting directives, which enable the caller to append diff --git a/strbuf.c b/strbuf.c index 5244029d91..80b7e051cd 100644 --- a/strbuf.c +++ b/strbuf.c @@ -3,7 +3,6 @@ #include "environment.h" #include "gettext.h" #include "hex.h" -#include "object-name.h" #include "repository.h" #include "strbuf.h" #include "string-list.h" @@ -1024,21 +1023,6 @@ void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm, strbuf_setlen(sb, sb->len + len); } -void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo, - const struct object_id *oid, int abbrev_len) -{ - int r; - strbuf_grow(sb, GIT_MAX_HEXSZ + 1); - r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len); - strbuf_setlen(sb, sb->len + r); -} - -void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid, - int abbrev_len) -{ - strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len); -} - /* * Returns the length of a line, without trailing spaces. * diff --git a/strbuf.h b/strbuf.h index 114ad0c024..5d97e27b99 100644 --- a/strbuf.h +++ b/strbuf.h @@ -616,16 +616,6 @@ void strbuf_add_separated_string_list(struct strbuf *str, */ void strbuf_list_free(struct strbuf **list); -/** - * Add the abbreviation, as generated by repo_find_unique_abbrev(), of `sha1` to - * the strbuf `sb`. - */ -struct repository; -void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo, - const struct object_id *oid, int abbrev_len); -void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid, - int abbrev_len); - /* * Remove the filename from the provided path string. If the path * contains a trailing separator, then the path is considered a directory From patchwork Tue Jun 6 19:48:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Wan X-Patchwork-Id: 13269699 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 820C3C7EE37 for ; Tue, 6 Jun 2023 19:49:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239534AbjFFTs7 (ORCPT ); Tue, 6 Jun 2023 15:48:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39106 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238806AbjFFTs5 (ORCPT ); Tue, 6 Jun 2023 15:48:57 -0400 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 65CEB10F1 for ; Tue, 6 Jun 2023 12:48:55 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id d2e1a72fcca58-65297b2ccc6so5299788b3a.1 for ; Tue, 06 Jun 2023 12:48:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686080935; x=1688672935; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=MlZZ/zhPaT5qoIadbzJMRlj8FQbinMfECChGVXvG4Wg=; b=HPfO4FTSJKLl40zvkdm8tInfvqYgpit77UYNPKr6Vqgzq7+orvzxbnrtPfqUfSU015 /hEHtDE2Jia09usZJlesYD1tT/COeC1EAWv9nkDKqzFP/ydxGYG3YCa1Fii/cj23mRxk VcNKB+8vbFvVrP2q3JZwLSuFkCyoNRXdI71F9RPYfcarpft5V8vVcBrlpEPpbxkI6yGQ m12i6BYXVYNFnlOEXuJ7mt7eiqtblBdQJk/+moYEk5IZol5r0/oV6VQSVgxAI2QpDXle MYBECiQbsSAQWqfUyuxQZ+vwwCv558RZ0RqKD3ntDhiYaKgtGFVsXGX//qIl76J6FKDo gHyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686080935; x=1688672935; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=MlZZ/zhPaT5qoIadbzJMRlj8FQbinMfECChGVXvG4Wg=; b=X21ZUHxG3bIMHVOoiQPbX07bL/wfkkPHVJ4SO66FCKcbFDmkeNSzFpW1T7xzRlsN8H 1s7+f0N4bTnfa0dScOLlqNJE9UlWXZNfSLLxPkK4WCIIMZVp4mDZ11yBRzMDmnY/ln54 HLcv+n1ecNLlywpRqc3P69jA8sJbLO2bHm9tkiGpdBDU+4Pn4BOokQqixlsrU8oLmQm8 EIlPbMBpqx4rY2avKcLshZGB6dhyMbDO4LdJkkLYFIwjuRs7y1SPS1fxA0fRCaz7oqQZ SO9yrplUg1Y+YVrX58NLEtJro4oAtklcpUbLC69y1FB7aGvPlvZZzSEpeIKG1Cafjzlm DS9Q== X-Gm-Message-State: AC+VfDzI11My937xDqAFOe1YJP6SuB4k8eG05pX70WnlzDlrIn8xTYpJ K0/g8J5zl/RO7LNrttQOBvw86Ikg9vRL1KXpSZylr7rEJ9gG1NibZq8JFm5foam6421UHGYGx4t ZKJ4LPTb448mDxATY65Oe1w3QTSKQQVJGfGo1qq8OYVXNAWtdcutz4vv39sRvffUq9w== X-Google-Smtp-Source: ACHHUZ59AduhcgZI9ibxpNOQYpxUDis3x/R30bGs6aHKBj2AqTzQp5HrMQkDblwfw34kYQefGz2+De1zkf19p28= X-Received: from barleywine.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3bd4]) (user=calvinwan job=sendgmr) by 2002:a05:6a00:13a6:b0:657:f26e:b025 with SMTP id t38-20020a056a0013a600b00657f26eb025mr1365867pfg.6.1686080934913; Tue, 06 Jun 2023 12:48:54 -0700 (PDT) Date: Tue, 6 Jun 2023 19:48:42 +0000 In-Reply-To: <20230606194720.2053551-1-calvinwan@google.com> Mime-Version: 1.0 References: <20230606194720.2053551-1-calvinwan@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230606194843.2054314-6-calvinwan@google.com> Subject: [PATCH v7 6/7] path: move related function to path From: Calvin Wan To: git@vger.kernel.org Cc: Calvin Wan Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Move path-related function from strbuf.[ch] to path.[ch] so that strbuf is focused on string manipulation routines with minimal dependencies. repository.h is no longer a necessary dependency after moving this function out. Signed-off-by: Calvin Wan --- path.c | 20 ++++++++++++++++++++ path.h | 5 +++++ strbuf.c | 21 --------------------- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/path.c b/path.c index 7c1cd8182a..e17a2613c5 100644 --- a/path.c +++ b/path.c @@ -1213,6 +1213,26 @@ int normalize_path_copy(char *dst, const char *src) return normalize_path_copy_len(dst, src, NULL); } +int strbuf_normalize_path(struct strbuf *src) +{ + struct strbuf dst = STRBUF_INIT; + + strbuf_grow(&dst, src->len); + if (normalize_path_copy(dst.buf, src->buf) < 0) { + strbuf_release(&dst); + return -1; + } + + /* + * normalize_path does not tell us the new length, so we have to + * compute it by looking for the new NUL it placed + */ + strbuf_setlen(&dst, strlen(dst.buf)); + strbuf_swap(src, &dst); + strbuf_release(&dst); + return 0; +} + /* * path = Canonical absolute path * prefixes = string_list containing normalized, absolute paths without diff --git a/path.h b/path.h index 60e83a49a9..639372edd9 100644 --- a/path.h +++ b/path.h @@ -191,6 +191,11 @@ const char *remove_leading_path(const char *in, const char *prefix); const char *relative_path(const char *in, const char *prefix, struct strbuf *sb); int normalize_path_copy_len(char *dst, const char *src, int *prefix_len); int normalize_path_copy(char *dst, const char *src); +/** + * Normalize in-place the path contained in the strbuf. If an error occurs, + * the contents of "sb" are left untouched, and -1 is returned. + */ +int strbuf_normalize_path(struct strbuf *src); int longest_ancestor_length(const char *path, struct string_list *prefixes); char *strip_path_suffix(const char *path, const char *suffix); int daemon_avoid_alias(const char *path); diff --git a/strbuf.c b/strbuf.c index 80b7e051cd..d5978fee4e 100644 --- a/strbuf.c +++ b/strbuf.c @@ -3,7 +3,6 @@ #include "environment.h" #include "gettext.h" #include "hex.h" -#include "repository.h" #include "strbuf.h" #include "string-list.h" #include "utf8.h" @@ -1089,26 +1088,6 @@ void strbuf_stripspace(struct strbuf *sb, int skip_comments) strbuf_setlen(sb, j); } -int strbuf_normalize_path(struct strbuf *src) -{ - struct strbuf dst = STRBUF_INIT; - - strbuf_grow(&dst, src->len); - if (normalize_path_copy(dst.buf, src->buf) < 0) { - strbuf_release(&dst); - return -1; - } - - /* - * normalize_path does not tell us the new length, so we have to - * compute it by looking for the new NUL it placed - */ - strbuf_setlen(&dst, strlen(dst.buf)); - strbuf_swap(src, &dst); - strbuf_release(&dst); - return 0; -} - void strbuf_strip_file_from_path(struct strbuf *sb) { char *path_sep = find_last_dir_sep(sb->buf); From patchwork Tue Jun 6 19:48:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Wan X-Patchwork-Id: 13269703 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 049F4C7EE37 for ; Tue, 6 Jun 2023 19:49:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239564AbjFFTtI (ORCPT ); Tue, 6 Jun 2023 15:49:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39142 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239541AbjFFTs7 (ORCPT ); Tue, 6 Jun 2023 15:48:59 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7403111B for ; Tue, 6 Jun 2023 12:48:57 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-568a8704f6dso96884067b3.1 for ; Tue, 06 Jun 2023 12:48:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686080936; x=1688672936; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=sIiUv9DIIsygv4/ml+WGhsRtYlJ6Qq19t0jg1JIhX8Q=; b=5MYYIswO5Q3B9zrjYU8XuoAO4kX+mMk0bslwg1v8pS8qfhcORxNhnaQrKrfHmR2K3b C/qY6Rq+zTdmG5tiWJ6z/xYh311KweRV4uBICarhR1Q0JM9wv6bo1bIMwi6OFIL13aFz kxW5kgebCmdEUVg5jFZAPGcAU5WVfDVnrHm9iiiI8qSsM/bOiQ4BNqLIGahSpgU70xHy zzF1gcLG6l5mv7UcQV15AEM8KhbW7mbdvV6dOpO2xKlpQk8WAZa285hOoFoJ24rl7Pi0 yeg9nk58a6OHWuDbrd7V9zhyOQnQXH8EUfOGkNudcDH/l4ZVQW6bXz6AXYcEfygOqb2h H1Dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686080936; x=1688672936; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=sIiUv9DIIsygv4/ml+WGhsRtYlJ6Qq19t0jg1JIhX8Q=; b=Cyz9uxijN2uhMb0b4/2Tws4VCHCGabL2H12eMoTpBq2Iu2YPcrK0GnidB2l2FUWqmS 55xyUGcElJJ7b+AJXeVIrnmly07BclI5vzbzK/a/r1u9XvDCLUTFlXj80uYjywWA4SoQ PrMyj+1B6TaVXs8UloIkmdExLV+W+5Nh4CXfkyrONCd9zFRs4Pxe6gjyol22BtRvkMRq eCIMn/i51vSfaZfH9Koi++H8H3VYNZ03I12K5ibjY75ubx1F4UDJeaKMsicwOME3lgZS DJRGS3BVWe91j2QYDEpAnUSaPxQlO6ZDNPe0xK98Dg7QuUdsDZIJZPZUxEoBi4o2YV4n SzxA== X-Gm-Message-State: AC+VfDz9mNY1K/BOJ9OAKgS5XuEivgrk60T2uzAxgbiyBvamPyOjdqaY X56rNKu564gfzPXJIg9jlmB8b6yBalbk7Le9d7pxHPyhP0gHvg6Q9XQnmbZe8mX7/OLXRQWsEgM UjM/3NjzaOlZZIQAfqyhMAuXcObZI/0sDiVc2PqcFBi0kt5AOkcEhCgPlYKn1bjOaJw== X-Google-Smtp-Source: ACHHUZ5Iii61M+A4denOhW3XHYt5Kzbp7LIkKhjXAF+8p95euI1+7FfkjdJWWDhs+Y7VV6v9B6Y+sHV5wg24/8o= X-Received: from barleywine.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3bd4]) (user=calvinwan job=sendgmr) by 2002:a25:9d90:0:b0:bb3:8945:d6fa with SMTP id v16-20020a259d90000000b00bb38945d6famr1118422ybp.7.1686080936612; Tue, 06 Jun 2023 12:48:56 -0700 (PDT) Date: Tue, 6 Jun 2023 19:48:43 +0000 In-Reply-To: <20230606194720.2053551-1-calvinwan@google.com> Mime-Version: 1.0 References: <20230606194720.2053551-1-calvinwan@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230606194843.2054314-7-calvinwan@google.com> Subject: [PATCH v7 7/7] strbuf: remove global variable From: Calvin Wan To: git@vger.kernel.org Cc: Calvin Wan Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org As a library that only interacts with other primitives, strbuf should not utilize the comment_line_char global variable within its functions. Therefore, add an additional parameter for functions that use comment_line_char and refactor callers to pass it in instead. strbuf_stripspace() removes the skip_comments boolean and checks if comment_line_char is a non-NUL character to determine whether to skip comments or not. Signed-off-by: Calvin Wan --- add-patch.c | 12 +++++++----- builtin/am.c | 2 +- builtin/branch.c | 4 ++-- builtin/commit.c | 2 +- builtin/merge.c | 10 ++++++---- builtin/notes.c | 16 +++++++++------- builtin/rebase.c | 2 +- builtin/stripspace.c | 6 ++++-- builtin/tag.c | 9 ++++++--- fmt-merge-msg.c | 9 ++++++--- gpg-interface.c | 5 +++-- rebase-interactive.c | 15 ++++++++------- sequencer.c | 24 +++++++++++++++--------- strbuf.c | 18 ++++++++++-------- strbuf.h | 14 ++++++++------ wt-status.c | 6 +++--- 16 files changed, 90 insertions(+), 64 deletions(-) diff --git a/add-patch.c b/add-patch.c index 8d770d203f..9702c1aabd 100644 --- a/add-patch.c +++ b/add-patch.c @@ -1105,10 +1105,11 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk) size_t i; strbuf_reset(&s->buf); - strbuf_commented_addf(&s->buf, _("Manual hunk edit mode -- see bottom for " - "a quick guide.\n")); + strbuf_commented_addf(&s->buf, comment_line_char, + _("Manual hunk edit mode -- see bottom for " + "a quick guide.\n")); render_hunk(s, hunk, 0, 0, &s->buf); - strbuf_commented_addf(&s->buf, + strbuf_commented_addf(&s->buf, comment_line_char, _("---\n" "To remove '%c' lines, make them ' ' lines " "(context).\n" @@ -1117,12 +1118,13 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk) s->mode->is_reverse ? '+' : '-', s->mode->is_reverse ? '-' : '+', comment_line_char); - strbuf_commented_addf(&s->buf, "%s", _(s->mode->edit_hunk_hint)); + strbuf_commented_addf(&s->buf, comment_line_char, "%s", + _(s->mode->edit_hunk_hint)); /* * TRANSLATORS: 'it' refers to the patch mentioned in the previous * messages. */ - strbuf_commented_addf(&s->buf, + strbuf_commented_addf(&s->buf, comment_line_char, _("If it does not apply cleanly, you will be " "given an opportunity to\n" "edit again. If all lines of the hunk are " diff --git a/builtin/am.c b/builtin/am.c index 5c83f2e003..9ece2e3066 100644 --- a/builtin/am.c +++ b/builtin/am.c @@ -1283,7 +1283,7 @@ static int parse_mail(struct am_state *state, const char *mail) strbuf_addstr(&msg, "\n\n"); strbuf_addbuf(&msg, &mi.log_message); - strbuf_stripspace(&msg, 0); + strbuf_stripspace(&msg, '\0'); assert(!state->author_name); state->author_name = strbuf_detach(&author_name, NULL); diff --git a/builtin/branch.c b/builtin/branch.c index e6c2655af6..40a79e60f5 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -674,7 +674,7 @@ static int edit_branch_description(const char *branch_name) exists = !read_branch_desc(&buf, branch_name); if (!buf.len || buf.buf[buf.len-1] != '\n') strbuf_addch(&buf, '\n'); - strbuf_commented_addf(&buf, + strbuf_commented_addf(&buf, comment_line_char, _("Please edit the description for the branch\n" " %s\n" "Lines starting with '%c' will be stripped.\n"), @@ -685,7 +685,7 @@ static int edit_branch_description(const char *branch_name) strbuf_release(&buf); return -1; } - strbuf_stripspace(&buf, 1); + strbuf_stripspace(&buf, comment_line_char); strbuf_addf(&name, "branch.%s.description", branch_name); if (buf.len || exists) diff --git a/builtin/commit.c b/builtin/commit.c index e67c4be221..e002ebf070 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -893,7 +893,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix, s->hints = 0; if (clean_message_contents) - strbuf_stripspace(&sb, 0); + strbuf_stripspace(&sb, '\0'); if (signoff) append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0); diff --git a/builtin/merge.c b/builtin/merge.c index 8da3e46abb..1d14767c0c 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -879,13 +879,15 @@ static void prepare_to_commit(struct commit_list *remoteheads) strbuf_addch(&msg, '\n'); if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) { wt_status_append_cut_line(&msg); - strbuf_commented_addf(&msg, "\n"); + strbuf_commented_addf(&msg, comment_line_char, "\n"); } - strbuf_commented_addf(&msg, _(merge_editor_comment)); + strbuf_commented_addf(&msg, comment_line_char, + _(merge_editor_comment)); if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) - strbuf_commented_addf(&msg, _(scissors_editor_comment)); + strbuf_commented_addf(&msg, comment_line_char, + _(scissors_editor_comment)); else - strbuf_commented_addf(&msg, + strbuf_commented_addf(&msg, comment_line_char, _(no_scissors_editor_comment), comment_line_char); } if (signoff) diff --git a/builtin/notes.c b/builtin/notes.c index d5788352b6..3bad5b458b 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -11,6 +11,7 @@ #include "config.h" #include "builtin.h" #include "editor.h" +#include "environment.h" #include "gettext.h" #include "hex.h" #include "notes.h" @@ -157,7 +158,7 @@ static void write_commented_object(int fd, const struct object_id *object) if (strbuf_read(&buf, show.out, 0) < 0) die_errno(_("could not read 'show' output")); - strbuf_add_commented_lines(&cbuf, buf.buf, buf.len); + strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_char); write_or_die(fd, cbuf.buf, cbuf.len); strbuf_release(&cbuf); @@ -185,9 +186,10 @@ static void prepare_note_data(const struct object_id *object, struct note_data * copy_obj_to_fd(fd, old_note); strbuf_addch(&buf, '\n'); - strbuf_add_commented_lines(&buf, "\n", strlen("\n")); - strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template))); - strbuf_add_commented_lines(&buf, "\n", strlen("\n")); + strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char); + strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)), + comment_line_char); + strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char); write_or_die(fd, buf.buf, buf.len); write_commented_object(fd, object); @@ -199,7 +201,7 @@ static void prepare_note_data(const struct object_id *object, struct note_data * if (launch_editor(d->edit_path, &d->buf, NULL)) { die(_("please supply the note contents using either -m or -F option")); } - strbuf_stripspace(&d->buf, 1); + strbuf_stripspace(&d->buf, comment_line_char); } } @@ -225,7 +227,7 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset) if (d->buf.len) strbuf_addch(&d->buf, '\n'); strbuf_addstr(&d->buf, arg); - strbuf_stripspace(&d->buf, 0); + strbuf_stripspace(&d->buf, '\0'); d->given = 1; return 0; @@ -244,7 +246,7 @@ static int parse_file_arg(const struct option *opt, const char *arg, int unset) die_errno(_("cannot read '%s'"), arg); } else if (strbuf_read_file(&d->buf, arg, 1024) < 0) die_errno(_("could not open or read '%s'"), arg); - strbuf_stripspace(&d->buf, 0); + strbuf_stripspace(&d->buf, '\0'); d->given = 1; return 0; diff --git a/builtin/rebase.c b/builtin/rebase.c index ace1d5e8d1..b1ba9fb05d 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -209,7 +209,7 @@ static int edit_todo_file(unsigned flags) if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0) return error_errno(_("could not read '%s'."), todo_file); - strbuf_stripspace(&todo_list.buf, 1); + strbuf_stripspace(&todo_list.buf, comment_line_char); res = edit_todo_list(the_repository, &todo_list, &new_todo, NULL, NULL, flags); if (!res && todo_list_write_to_file(the_repository, &new_todo, todo_file, NULL, NULL, -1, flags & ~(TODO_LIST_SHORTEN_IDS))) diff --git a/builtin/stripspace.c b/builtin/stripspace.c index 9451eb69ff..1987752359 100644 --- a/builtin/stripspace.c +++ b/builtin/stripspace.c @@ -1,6 +1,7 @@ #include "builtin.h" #include "cache.h" #include "config.h" +#include "environment.h" #include "gettext.h" #include "parse-options.h" #include "setup.h" @@ -13,7 +14,7 @@ static void comment_lines(struct strbuf *buf) size_t len; msg = strbuf_detach(buf, &len); - strbuf_add_commented_lines(buf, msg, len); + strbuf_add_commented_lines(buf, msg, len, comment_line_char); free(msg); } @@ -58,7 +59,8 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix) die_errno("could not read the input"); if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS) - strbuf_stripspace(&buf, mode == STRIP_COMMENTS); + strbuf_stripspace(&buf, + mode == STRIP_COMMENTS ? comment_line_char : '\0'); else comment_lines(&buf); diff --git a/builtin/tag.c b/builtin/tag.c index 1850a6a6fd..b79e0a88e6 100644 --- a/builtin/tag.c +++ b/builtin/tag.c @@ -311,9 +311,11 @@ static void create_tag(const struct object_id *object, const char *object_ref, struct strbuf buf = STRBUF_INIT; strbuf_addch(&buf, '\n'); if (opt->cleanup_mode == CLEANUP_ALL) - strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char); + strbuf_commented_addf(&buf, comment_line_char, + _(tag_template), tag, comment_line_char); else - strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char); + strbuf_commented_addf(&buf, comment_line_char, + _(tag_template_nocleanup), tag, comment_line_char); write_or_die(fd, buf.buf, buf.len); strbuf_release(&buf); } @@ -327,7 +329,8 @@ static void create_tag(const struct object_id *object, const char *object_ref, } if (opt->cleanup_mode != CLEANUP_NONE) - strbuf_stripspace(buf, opt->cleanup_mode == CLEANUP_ALL); + strbuf_stripspace(buf, + opt->cleanup_mode == CLEANUP_ALL ? comment_line_char : '\0'); if (!opt->message_given && !buf->len) die(_("no tag message?")); diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c index 5af0d4715b..5d15e2387d 100644 --- a/fmt-merge-msg.c +++ b/fmt-merge-msg.c @@ -508,7 +508,8 @@ static void fmt_tag_signature(struct strbuf *tagbuf, strbuf_complete_line(tagbuf); if (sig->len) { strbuf_addch(tagbuf, '\n'); - strbuf_add_commented_lines(tagbuf, sig->buf, sig->len); + strbuf_add_commented_lines(tagbuf, sig->buf, sig->len, + comment_line_char); } } @@ -554,7 +555,8 @@ static void fmt_merge_msg_sigs(struct strbuf *out) strbuf_addch(&tagline, '\n'); strbuf_add_commented_lines(&tagline, origins.items[first_tag].string, - strlen(origins.items[first_tag].string)); + strlen(origins.items[first_tag].string), + comment_line_char); strbuf_insert(&tagbuf, 0, tagline.buf, tagline.len); strbuf_release(&tagline); @@ -562,7 +564,8 @@ static void fmt_merge_msg_sigs(struct strbuf *out) strbuf_addch(&tagbuf, '\n'); strbuf_add_commented_lines(&tagbuf, origins.items[i].string, - strlen(origins.items[i].string)); + strlen(origins.items[i].string), + comment_line_char); fmt_tag_signature(&tagbuf, &sig, buf, len); } strbuf_release(&payload); diff --git a/gpg-interface.c b/gpg-interface.c index 19a3471a0b..6a3817bbca 100644 --- a/gpg-interface.c +++ b/gpg-interface.c @@ -13,6 +13,7 @@ #include "tempfile.h" #include "alias.h" #include "wrapper.h" +#include "environment.h" static int git_gpg_config(const char *, const char *, void *); @@ -586,8 +587,8 @@ static int verify_ssh_signed_buffer(struct signature_check *sigc, } } - strbuf_stripspace(&ssh_keygen_out, 0); - strbuf_stripspace(&ssh_keygen_err, 0); + strbuf_stripspace(&ssh_keygen_out, '\0'); + strbuf_stripspace(&ssh_keygen_err, '\0'); /* Add stderr outputs to show the user actual ssh-keygen errors */ strbuf_add(&ssh_keygen_out, ssh_principals_err.buf, ssh_principals_err.len); strbuf_add(&ssh_keygen_out, ssh_keygen_err.buf, ssh_keygen_err.len); diff --git a/rebase-interactive.c b/rebase-interactive.c index 852a331318..f286404d4b 100644 --- a/rebase-interactive.c +++ b/rebase-interactive.c @@ -72,13 +72,14 @@ void append_todo_help(int command_count, if (!edit_todo) { strbuf_addch(buf, '\n'); - strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)", - "Rebase %s onto %s (%d commands)", - command_count), + strbuf_commented_addf(buf, comment_line_char, + Q_("Rebase %s onto %s (%d command)", + "Rebase %s onto %s (%d commands)", + command_count), shortrevisions, shortonto, command_count); } - strbuf_add_commented_lines(buf, msg, strlen(msg)); + strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char); if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR) msg = _("\nDo not remove any line. Use 'drop' " @@ -87,7 +88,7 @@ void append_todo_help(int command_count, msg = _("\nIf you remove a line here " "THAT COMMIT WILL BE LOST.\n"); - strbuf_add_commented_lines(buf, msg, strlen(msg)); + strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char); if (edit_todo) msg = _("\nYou are editing the todo file " @@ -98,7 +99,7 @@ void append_todo_help(int command_count, msg = _("\nHowever, if you remove everything, " "the rebase will be aborted.\n\n"); - strbuf_add_commented_lines(buf, msg, strlen(msg)); + strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char); } int edit_todo_list(struct repository *r, struct todo_list *todo_list, @@ -130,7 +131,7 @@ int edit_todo_list(struct repository *r, struct todo_list *todo_list, if (launch_sequence_editor(todo_file, &new_todo->buf, NULL)) return -2; - strbuf_stripspace(&new_todo->buf, 1); + strbuf_stripspace(&new_todo->buf, comment_line_char); if (initial && new_todo->buf.len == 0) return -3; diff --git a/sequencer.c b/sequencer.c index bceb6abcb6..a8a70ab235 100644 --- a/sequencer.c +++ b/sequencer.c @@ -660,11 +660,12 @@ void append_conflicts_hint(struct index_state *istate, } strbuf_addch(msgbuf, '\n'); - strbuf_commented_addf(msgbuf, "Conflicts:\n"); + strbuf_commented_addf(msgbuf, comment_line_char, "Conflicts:\n"); for (i = 0; i < istate->cache_nr;) { const struct cache_entry *ce = istate->cache[i++]; if (ce_stage(ce)) { - strbuf_commented_addf(msgbuf, "\t%s\n", ce->name); + strbuf_commented_addf(msgbuf, comment_line_char, + "\t%s\n", ce->name); while (i < istate->cache_nr && !strcmp(ce->name, istate->cache[i]->name)) i++; @@ -1143,7 +1144,8 @@ void cleanup_message(struct strbuf *msgbuf, cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) strbuf_setlen(msgbuf, wt_status_locate_end(msgbuf->buf, msgbuf->len)); if (cleanup_mode != COMMIT_MSG_CLEANUP_NONE) - strbuf_stripspace(msgbuf, cleanup_mode == COMMIT_MSG_CLEANUP_ALL); + strbuf_stripspace(msgbuf, + cleanup_mode == COMMIT_MSG_CLEANUP_ALL ? comment_line_char : '\0'); } /* @@ -1174,7 +1176,8 @@ int template_untouched(const struct strbuf *sb, const char *template_file, if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0) return 0; - strbuf_stripspace(&tmpl, cleanup_mode == COMMIT_MSG_CLEANUP_ALL); + strbuf_stripspace(&tmpl, + cleanup_mode == COMMIT_MSG_CLEANUP_ALL ? comment_line_char : '\0'); if (!skip_prefix(sb->buf, tmpl.buf, &start)) start = sb->buf; strbuf_release(&tmpl); @@ -1546,7 +1549,8 @@ static int try_to_commit(struct repository *r, cleanup = opts->default_msg_cleanup; if (cleanup != COMMIT_MSG_CLEANUP_NONE) - strbuf_stripspace(msg, cleanup == COMMIT_MSG_CLEANUP_ALL); + strbuf_stripspace(msg, + cleanup == COMMIT_MSG_CLEANUP_ALL ? comment_line_char : '\0'); if ((flags & EDIT_MSG) && message_is_empty(msg, cleanup)) { res = 1; /* run 'git commit' to display error message */ goto out; @@ -1840,7 +1844,7 @@ static void add_commented_lines(struct strbuf *buf, const void *str, size_t len) s += count; len -= count; } - strbuf_add_commented_lines(buf, s, len); + strbuf_add_commented_lines(buf, s, len, comment_line_char); } /* Does the current fixup chain contain a squash command? */ @@ -1939,7 +1943,7 @@ static int append_squash_message(struct strbuf *buf, const char *body, strbuf_addf(buf, _(nth_commit_msg_fmt), ++opts->current_fixup_count + 1); strbuf_addstr(buf, "\n\n"); - strbuf_add_commented_lines(buf, body, commented_len); + strbuf_add_commented_lines(buf, body, commented_len, comment_line_char); /* buf->buf may be reallocated so store an offset into the buffer */ fixup_off = buf->len; strbuf_addstr(buf, body + commented_len); @@ -2029,7 +2033,8 @@ static int update_squash_messages(struct repository *r, _(first_commit_msg_str)); strbuf_addstr(&buf, "\n\n"); if (is_fixup_flag(command, flag)) - strbuf_add_commented_lines(&buf, body, strlen(body)); + strbuf_add_commented_lines(&buf, body, strlen(body), + comment_line_char); else strbuf_addstr(&buf, body); @@ -2048,7 +2053,8 @@ static int update_squash_messages(struct repository *r, strbuf_addf(&buf, _(skip_nth_commit_msg_fmt), ++opts->current_fixup_count + 1); strbuf_addstr(&buf, "\n\n"); - strbuf_add_commented_lines(&buf, body, strlen(body)); + strbuf_add_commented_lines(&buf, body, strlen(body), + comment_line_char); } else return error(_("unknown command: %d"), command); repo_unuse_commit_buffer(r, commit, message); diff --git a/strbuf.c b/strbuf.c index d5978fee4e..67e399b60a 100644 --- a/strbuf.c +++ b/strbuf.c @@ -1,6 +1,5 @@ #include "git-compat-util.h" #include "alloc.h" -#include "environment.h" #include "gettext.h" #include "hex.h" #include "strbuf.h" @@ -362,7 +361,8 @@ static void add_lines(struct strbuf *out, strbuf_complete_line(out); } -void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size) +void strbuf_add_commented_lines(struct strbuf *out, const char *buf, + size_t size, char comment_line_char) { static char prefix1[3]; static char prefix2[2]; @@ -374,7 +374,8 @@ void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size add_lines(out, prefix1, prefix2, buf, size); } -void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...) +void strbuf_commented_addf(struct strbuf *sb, char comment_line_char, + const char *fmt, ...) { va_list params; struct strbuf buf = STRBUF_INIT; @@ -384,7 +385,7 @@ void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...) strbuf_vaddf(&buf, fmt, params); va_end(params); - strbuf_add_commented_lines(sb, buf.buf, buf.len); + strbuf_add_commented_lines(sb, buf.buf, buf.len, comment_line_char); if (incomplete_line) sb->buf[--sb->len] = '\0'; @@ -1051,10 +1052,10 @@ static size_t cleanup(char *line, size_t len) * * If last line does not have a newline at the end, one is added. * - * Enable skip_comments to skip every line starting with comment - * character. + * Pass a non-NUL comment_line_char to skip every line starting + * with it. */ -void strbuf_stripspace(struct strbuf *sb, int skip_comments) +void strbuf_stripspace(struct strbuf *sb, char comment_line_char) { size_t empties = 0; size_t i, j, len, newlen; @@ -1067,7 +1068,8 @@ void strbuf_stripspace(struct strbuf *sb, int skip_comments) eol = memchr(sb->buf + i, '\n', sb->len - i); len = eol ? eol - (sb->buf + i) + 1 : sb->len - i; - if (skip_comments && len && sb->buf[i] == comment_line_char) { + if (comment_line_char && len && + sb->buf[i] == comment_line_char) { newlen = 0; continue; } diff --git a/strbuf.h b/strbuf.h index 5d97e27b99..da91c17257 100644 --- a/strbuf.h +++ b/strbuf.h @@ -291,7 +291,8 @@ void strbuf_splice(struct strbuf *sb, size_t pos, size_t len, * by a comment character and a blank. */ void strbuf_add_commented_lines(struct strbuf *out, - const char *buf, size_t size); + const char *buf, size_t size, + char comment_line_char); /** @@ -420,8 +421,8 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...); * Add a formatted string prepended by a comment character and a * blank to the buffer. */ -__attribute__((format (printf, 2, 3))) -void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...); +__attribute__((format (printf, 3, 4))) +void strbuf_commented_addf(struct strbuf *sb, char comment_line_char, const char *fmt, ...); __attribute__((format (printf,2,0))) void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap); @@ -543,10 +544,11 @@ int strbuf_getcwd(struct strbuf *sb); int strbuf_normalize_path(struct strbuf *sb); /** - * Strip whitespace from a buffer. The second parameter controls if - * comments are considered contents to be removed or not. + * Strip whitespace from a buffer. If comment_line_char is non-NUL, + * then lines beginning with that character are considered comments, + * thus removed. */ -void strbuf_stripspace(struct strbuf *buf, int skip_comments); +void strbuf_stripspace(struct strbuf *buf, char comment_line_char); static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix) { diff --git a/wt-status.c b/wt-status.c index 068b76ef6d..677465b10c 100644 --- a/wt-status.c +++ b/wt-status.c @@ -1024,7 +1024,7 @@ static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncom if (s->display_comment_prefix) { size_t len; summary_content = strbuf_detach(&summary, &len); - strbuf_add_commented_lines(&summary, summary_content, len); + strbuf_add_commented_lines(&summary, summary_content, len, comment_line_char); free(summary_content); } @@ -1099,8 +1099,8 @@ void wt_status_append_cut_line(struct strbuf *buf) { const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored."); - strbuf_commented_addf(buf, "%s", cut_line); - strbuf_add_commented_lines(buf, explanation, strlen(explanation)); + strbuf_commented_addf(buf, comment_line_char, "%s", cut_line); + strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_char); } void wt_status_add_cut_line(FILE *fp)