From patchwork Tue Jun 27 19:52:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Wan X-Patchwork-Id: 13294941 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 8E2A3EB64D9 for ; Tue, 27 Jun 2023 19:53:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230189AbjF0TxL (ORCPT ); Tue, 27 Jun 2023 15:53:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229610AbjF0TxK (ORCPT ); Tue, 27 Jun 2023 15:53:10 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7F55610F5 for ; Tue, 27 Jun 2023 12:53:08 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-c1039cbba72so528904276.0 for ; Tue, 27 Jun 2023 12:53:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687895587; x=1690487587; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=hZuSZCZII7PvO9amGQkSEKppFNjLgyI2N/nPiwfLb6s=; b=5OPo0PJnT9OAl0S8vTyQaOUdMFnXRxI9Tz1DZE5H3elfUaz29riHmelB9DjRIaagnn fEhoZT9xXFZqmHfkgk2NGISGzgrptF1XIkSozjGnA8qf32gVRUr0PplGQdVzx0kNQKph hWe/2HszIkx6Ecp70HuKwGAjZU4LP7NN5c8m3TUZ+TNJ3BTgSTt/D8GUzBYOKJHZ42oK Twu774MC2pHtqhzhdM5qzJtX1BUw/DvJzke3iZb8VjdLv2zsMqeVcXswTZ9XQhLmQ9HB aPS1iKoIB/ZdNUM7j4yUnThEEvLtCUaBA/byzR0pc4KiBtSVzp+8mtdzfkssGKDhAjjM QfVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687895587; x=1690487587; 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=hZuSZCZII7PvO9amGQkSEKppFNjLgyI2N/nPiwfLb6s=; b=K0CwhesbB0KXMKZRn8A1ZpwhKfY3mYJ+CYrI2a9T1nekxcIaszTbYpiTaQBa6g9sb+ iN0AWoehwwpEJFd9EVR6Urq67gWUuHCnPiSAI0LUXHj0ePOaUZ0RMs2hIYoHNZlymWyD 5GhjIOmvwbxYoGU57JrhpiqPBVb5TUKcAthd61bKH5DKzV1esuQHMMWinP5x9UlJSJ5C rOnr2vQ4xSWQNsngFM/amuBe6u59phU0NM/TP1Jj4n8rqxMsQUOzBzd/nXQARfFQwQaE sNqgmuTItOM+HQ/qW387UEPZdroWfUssJo9BMohl9X415OkRc4TGzpC/rUU3h6o8ILtF Rf9g== X-Gm-Message-State: AC+VfDzppqN5iMrK4gWLBZjx/dH7KmvGCQ4/wq8IplLJ8fcUrwH1QexX pHH1sl7//La94pVzRu4Dpe83YqEL26ePlyaBwBZeKl3zc/zaJpzXUoJz+PXoiSadAyDOVGfq3fh QtYS+Ip8vK40Qg8UdwdLxFc/8IizXZyNDaaHkbr1/07m5l5dhO3gsOVaC+AV5J6appw== X-Google-Smtp-Source: ACHHUZ7j1CFsJ6PGAOULwi388ZswykZJdvEhVJMqZ0L4HrvAoxJQlA6KHNca6ViyWy42JR10WoUnY/LZfIbfl5Y= X-Received: from barleywine.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3bd4]) (user=calvinwan job=sendgmr) by 2002:a25:ac92:0:b0:bc4:78ac:920f with SMTP id x18-20020a25ac92000000b00bc478ac920fmr14891939ybi.0.1687895587651; Tue, 27 Jun 2023 12:53:07 -0700 (PDT) Date: Tue, 27 Jun 2023 19:52:44 +0000 In-Reply-To: <20230627195251.1973421-1-calvinwan@google.com> Mime-Version: 1.0 References: <20230627195251.1973421-1-calvinwan@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230627195251.1973421-2-calvinwan@google.com> Subject: [RFC PATCH 1/8] trace2: log fsync stats in trace2 rather than wrapper From: Calvin Wan To: git@vger.kernel.org Cc: Calvin Wan , nasamuffin@google.com, chooglen@google.com, johnathantanmy@google.com Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org As a library boundary, wrapper.c should not directly log trace2 statistics, but instead provide those statistics upon request. Therefore, move the trace2 logging code to trace2.[ch.]. This also allows wrapper.c to not be dependent on trace2.h and repository.h. Signed-off-by: Calvin Wan --- trace2.c | 13 +++++++++++++ trace2.h | 5 +++++ wrapper.c | 17 ++++++----------- wrapper.h | 4 ++-- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/trace2.c b/trace2.c index 0efc4e7b95..f367a1ce31 100644 --- a/trace2.c +++ b/trace2.c @@ -915,3 +915,16 @@ const char *trace2_session_id(void) { return tr2_sid_get(); } + +static void log_trace_fsync_if(const char *key) +{ + intmax_t value = get_trace_git_fsync_stats(key); + if (value) + trace2_data_intmax("fsync", the_repository, key, value); +} + +void trace_git_fsync_stats(void) +{ + log_trace_fsync_if("fsync/writeout-only"); + log_trace_fsync_if("fsync/hardware-flush"); +} diff --git a/trace2.h b/trace2.h index 4ced30c0db..689e9a4027 100644 --- a/trace2.h +++ b/trace2.h @@ -581,4 +581,9 @@ void trace2_collect_process_info(enum trace2_process_info_reason reason); const char *trace2_session_id(void); +/* + * Writes out trace statistics for fsync + */ +void trace_git_fsync_stats(void); + #endif /* TRACE2_H */ diff --git a/wrapper.c b/wrapper.c index 22be9812a7..bd7f0a9752 100644 --- a/wrapper.c +++ b/wrapper.c @@ -6,9 +6,7 @@ #include "config.h" #include "gettext.h" #include "object.h" -#include "repository.h" #include "strbuf.h" -#include "trace2.h" static intmax_t count_fsync_writeout_only; static intmax_t count_fsync_hardware_flush; @@ -600,16 +598,13 @@ int git_fsync(int fd, enum fsync_action action) } } -static void log_trace_fsync_if(const char *key, intmax_t value) +intmax_t get_trace_git_fsync_stats(const char *key) { - if (value) - trace2_data_intmax("fsync", the_repository, key, value); -} - -void trace_git_fsync_stats(void) -{ - log_trace_fsync_if("fsync/writeout-only", count_fsync_writeout_only); - log_trace_fsync_if("fsync/hardware-flush", count_fsync_hardware_flush); + if (!strcmp(key, "fsync/writeout-only")) + return count_fsync_writeout_only; + if (!strcmp(key, "fsync/hardware-flush")) + return count_fsync_hardware_flush; + return 0; } static int warn_if_unremovable(const char *op, const char *file, int rc) diff --git a/wrapper.h b/wrapper.h index c85b1328d1..db1bc109ed 100644 --- a/wrapper.h +++ b/wrapper.h @@ -88,9 +88,9 @@ enum fsync_action { int git_fsync(int fd, enum fsync_action action); /* - * Writes out trace statistics for fsync using the trace2 API. + * Returns trace statistics for fsync using the trace2 API. */ -void trace_git_fsync_stats(void); +intmax_t get_trace_git_fsync_stats(const char *key); /* * Preserves errno, prints a message, but gives no warning for ENOENT. From patchwork Tue Jun 27 19:52:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Wan X-Patchwork-Id: 13294942 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 F1EEBEB64D9 for ; Tue, 27 Jun 2023 19:53:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230345AbjF0TxQ (ORCPT ); Tue, 27 Jun 2023 15:53:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230073AbjF0TxL (ORCPT ); Tue, 27 Jun 2023 15:53:11 -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 DC481FA for ; Tue, 27 Jun 2023 12:53:09 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-666ecb24eb3so2353981b3a.1 for ; Tue, 27 Jun 2023 12:53:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687895589; x=1690487589; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=SG2lNsGTpCG7S67feE8zRGMcN1OLvtaAvYVrWUXtG2A=; b=r1366Tm1WZBOS8hpzdwHTqPklYoBFREWQcQXf89BAtbu23XgqFqwQaTz3UE9SF+shb cnSOAyM3sFriMWprzIahV7vFqa9tcZBi/Kbw8xK6lgZZ8g+2RpCof3ZWDebTKvuzKoZh 6wA+gttglx9HBRNdfJqWHZi3E7wtdaOa4/djobGKpMKHpZpkCtdUTR0CyduZQtyFdEAY mF1pHB3G18dW27skm04HIy9nCpb04fnAe5O72CrBd4qeycIz26tfXXnsu1J5Igo00XYU mOfikAFV+h4ilfmVxJw8QU65K2S9Da44YCFC1E262gTbHxhm+IEtiiz6tZ2aWDAApGxf Wesg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687895589; x=1690487589; 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=SG2lNsGTpCG7S67feE8zRGMcN1OLvtaAvYVrWUXtG2A=; b=XZWL1fVe3B4Le7IhIr7TORm9rQOlP2U5CO9/CUTTSCmWMJmoRi07OUHvfwp3RcfV3K XvZQ0m1uBDRdT+XpVZnNGjDHg/7FPiR1gCtuArnFK1Myx0ZvnXLIn+QvczavBLCvqLsW tej10RWuPql+a29ipUCmBg80w0gUcQfISwwW+s08kQcW/4m8wLeAzMfA306gOuI80h6L q73gW+eb780YoozGJ5sjnZSjUCKnpxXfWCGS8l9B7uqKXGT6fjWTOF//l/QDAyghLklx MOu3VP2ax5HRQTWD7BXdM5EriXTBK7bDjQZsB2+NkJMjJ3N1//DNX2yCD8vgLUDkpizk +ZfA== X-Gm-Message-State: AC+VfDwnmHnRAfm51bujMnPwJPFq6gO2KMSuB7m86rN8HwE7j1G4tP1x 7HercAeKvqG/gS5cLfzN9t2++hUoMBboh7lgidqTlFhG6uVileuJ1y0bI6/Osr4y3crOg4RAtsE 11AAaYNNCyR0XEErjhI1MCq50Avk/tktkqUblzr5BtFRikAlIpqf3uWeeOkj9Mvt1Zg== X-Google-Smtp-Source: ACHHUZ6wPnnb7obgrKoy1t/ewZa1vEyMBgGjkuOvcJo71ZEcvcXUqdakqhuKjzjKD57lVWCbZoWGA0kLfJExazk= X-Received: from barleywine.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3bd4]) (user=calvinwan job=sendgmr) by 2002:a05:6a00:24cd:b0:673:e4b4:fa35 with SMTP id d13-20020a056a0024cd00b00673e4b4fa35mr2293523pfv.2.1687895589227; Tue, 27 Jun 2023 12:53:09 -0700 (PDT) Date: Tue, 27 Jun 2023 19:52:45 +0000 In-Reply-To: <20230627195251.1973421-1-calvinwan@google.com> Mime-Version: 1.0 References: <20230627195251.1973421-1-calvinwan@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230627195251.1973421-3-calvinwan@google.com> Subject: [RFC PATCH 2/8] hex-ll: split out functionality from hex From: Calvin Wan To: git@vger.kernel.org Cc: Calvin Wan , nasamuffin@google.com, chooglen@google.com, johnathantanmy@google.com Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Separate out hex functionality that doesn't require a hash algo into hex-ll.[ch]. Since the hash algo is currently a global that sits in repository, this separation removes that dependency for files that only need basic hex manipulation functions. Signed-off-by: Calvin Wan --- Makefile | 1 + color.c | 2 +- hex-ll.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ hex-ll.h | 27 +++++++++++++++++++++++++++ hex.c | 47 ----------------------------------------------- hex.h | 24 +----------------------- mailinfo.c | 2 +- strbuf.c | 2 +- url.c | 2 +- urlmatch.c | 2 +- 10 files changed, 83 insertions(+), 75 deletions(-) create mode 100644 hex-ll.c create mode 100644 hex-ll.h diff --git a/Makefile b/Makefile index 045e2187c4..83b385b0be 100644 --- a/Makefile +++ b/Makefile @@ -1040,6 +1040,7 @@ LIB_OBJS += hash-lookup.o LIB_OBJS += hashmap.o LIB_OBJS += help.o LIB_OBJS += hex.o +LIB_OBJS += hex-ll.o LIB_OBJS += hook.o LIB_OBJS += ident.o LIB_OBJS += json-writer.o diff --git a/color.c b/color.c index 83abb11eda..f3c0a4659b 100644 --- a/color.c +++ b/color.c @@ -3,7 +3,7 @@ #include "color.h" #include "editor.h" #include "gettext.h" -#include "hex.h" +#include "hex-ll.h" #include "pager.h" #include "strbuf.h" diff --git a/hex-ll.c b/hex-ll.c new file mode 100644 index 0000000000..4d7ece1de5 --- /dev/null +++ b/hex-ll.c @@ -0,0 +1,49 @@ +#include "git-compat-util.h" +#include "hex-ll.h" + +const signed char hexval_table[256] = { + -1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */ + -1, -1, -1, -1, -1, -1, -1, -1, /* 08-0f */ + -1, -1, -1, -1, -1, -1, -1, -1, /* 10-17 */ + -1, -1, -1, -1, -1, -1, -1, -1, /* 18-1f */ + -1, -1, -1, -1, -1, -1, -1, -1, /* 20-27 */ + -1, -1, -1, -1, -1, -1, -1, -1, /* 28-2f */ + 0, 1, 2, 3, 4, 5, 6, 7, /* 30-37 */ + 8, 9, -1, -1, -1, -1, -1, -1, /* 38-3f */ + -1, 10, 11, 12, 13, 14, 15, -1, /* 40-47 */ + -1, -1, -1, -1, -1, -1, -1, -1, /* 48-4f */ + -1, -1, -1, -1, -1, -1, -1, -1, /* 50-57 */ + -1, -1, -1, -1, -1, -1, -1, -1, /* 58-5f */ + -1, 10, 11, 12, 13, 14, 15, -1, /* 60-67 */ + -1, -1, -1, -1, -1, -1, -1, -1, /* 68-67 */ + -1, -1, -1, -1, -1, -1, -1, -1, /* 70-77 */ + -1, -1, -1, -1, -1, -1, -1, -1, /* 78-7f */ + -1, -1, -1, -1, -1, -1, -1, -1, /* 80-87 */ + -1, -1, -1, -1, -1, -1, -1, -1, /* 88-8f */ + -1, -1, -1, -1, -1, -1, -1, -1, /* 90-97 */ + -1, -1, -1, -1, -1, -1, -1, -1, /* 98-9f */ + -1, -1, -1, -1, -1, -1, -1, -1, /* a0-a7 */ + -1, -1, -1, -1, -1, -1, -1, -1, /* a8-af */ + -1, -1, -1, -1, -1, -1, -1, -1, /* b0-b7 */ + -1, -1, -1, -1, -1, -1, -1, -1, /* b8-bf */ + -1, -1, -1, -1, -1, -1, -1, -1, /* c0-c7 */ + -1, -1, -1, -1, -1, -1, -1, -1, /* c8-cf */ + -1, -1, -1, -1, -1, -1, -1, -1, /* d0-d7 */ + -1, -1, -1, -1, -1, -1, -1, -1, /* d8-df */ + -1, -1, -1, -1, -1, -1, -1, -1, /* e0-e7 */ + -1, -1, -1, -1, -1, -1, -1, -1, /* e8-ef */ + -1, -1, -1, -1, -1, -1, -1, -1, /* f0-f7 */ + -1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */ +}; + +int hex_to_bytes(unsigned char *binary, const char *hex, size_t len) +{ + for (; len; len--, hex += 2) { + unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]); + + if (val & ~0xff) + return -1; + *binary++ = val; + } + return 0; +} diff --git a/hex-ll.h b/hex-ll.h new file mode 100644 index 0000000000..a381fa8556 --- /dev/null +++ b/hex-ll.h @@ -0,0 +1,27 @@ +#ifndef HEX_LL_H +#define HEX_LL_H + +extern const signed char hexval_table[256]; +static inline unsigned int hexval(unsigned char c) +{ + return hexval_table[c]; +} + +/* + * Convert two consecutive hexadecimal digits into a char. Return a + * negative value on error. Don't run over the end of short strings. + */ +static inline int hex2chr(const char *s) +{ + unsigned int val = hexval(s[0]); + return (val & ~0xf) ? val : (val << 4) | hexval(s[1]); +} + +/* + * Read `len` pairs of hexadecimal digits from `hex` and write the + * values to `binary` as `len` bytes. Return 0 on success, or -1 if + * the input does not consist of hex digits). + */ +int hex_to_bytes(unsigned char *binary, const char *hex, size_t len); + +#endif diff --git a/hex.c b/hex.c index 7bb440e794..03e55841ed 100644 --- a/hex.c +++ b/hex.c @@ -2,53 +2,6 @@ #include "hash.h" #include "hex.h" -const signed char hexval_table[256] = { - -1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */ - -1, -1, -1, -1, -1, -1, -1, -1, /* 08-0f */ - -1, -1, -1, -1, -1, -1, -1, -1, /* 10-17 */ - -1, -1, -1, -1, -1, -1, -1, -1, /* 18-1f */ - -1, -1, -1, -1, -1, -1, -1, -1, /* 20-27 */ - -1, -1, -1, -1, -1, -1, -1, -1, /* 28-2f */ - 0, 1, 2, 3, 4, 5, 6, 7, /* 30-37 */ - 8, 9, -1, -1, -1, -1, -1, -1, /* 38-3f */ - -1, 10, 11, 12, 13, 14, 15, -1, /* 40-47 */ - -1, -1, -1, -1, -1, -1, -1, -1, /* 48-4f */ - -1, -1, -1, -1, -1, -1, -1, -1, /* 50-57 */ - -1, -1, -1, -1, -1, -1, -1, -1, /* 58-5f */ - -1, 10, 11, 12, 13, 14, 15, -1, /* 60-67 */ - -1, -1, -1, -1, -1, -1, -1, -1, /* 68-67 */ - -1, -1, -1, -1, -1, -1, -1, -1, /* 70-77 */ - -1, -1, -1, -1, -1, -1, -1, -1, /* 78-7f */ - -1, -1, -1, -1, -1, -1, -1, -1, /* 80-87 */ - -1, -1, -1, -1, -1, -1, -1, -1, /* 88-8f */ - -1, -1, -1, -1, -1, -1, -1, -1, /* 90-97 */ - -1, -1, -1, -1, -1, -1, -1, -1, /* 98-9f */ - -1, -1, -1, -1, -1, -1, -1, -1, /* a0-a7 */ - -1, -1, -1, -1, -1, -1, -1, -1, /* a8-af */ - -1, -1, -1, -1, -1, -1, -1, -1, /* b0-b7 */ - -1, -1, -1, -1, -1, -1, -1, -1, /* b8-bf */ - -1, -1, -1, -1, -1, -1, -1, -1, /* c0-c7 */ - -1, -1, -1, -1, -1, -1, -1, -1, /* c8-cf */ - -1, -1, -1, -1, -1, -1, -1, -1, /* d0-d7 */ - -1, -1, -1, -1, -1, -1, -1, -1, /* d8-df */ - -1, -1, -1, -1, -1, -1, -1, -1, /* e0-e7 */ - -1, -1, -1, -1, -1, -1, -1, -1, /* e8-ef */ - -1, -1, -1, -1, -1, -1, -1, -1, /* f0-f7 */ - -1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */ -}; - -int hex_to_bytes(unsigned char *binary, const char *hex, size_t len) -{ - for (; len; len--, hex += 2) { - unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]); - - if (val & ~0xff) - return -1; - *binary++ = val; - } - return 0; -} - static int get_hash_hex_algop(const char *hex, unsigned char *hash, const struct git_hash_algo *algop) { diff --git a/hex.h b/hex.h index 7df4b3c460..c07c8b34c2 100644 --- a/hex.h +++ b/hex.h @@ -2,22 +2,7 @@ #define HEX_H #include "hash-ll.h" - -extern const signed char hexval_table[256]; -static inline unsigned int hexval(unsigned char c) -{ - return hexval_table[c]; -} - -/* - * Convert two consecutive hexadecimal digits into a char. Return a - * negative value on error. Don't run over the end of short strings. - */ -static inline int hex2chr(const char *s) -{ - unsigned int val = hexval(s[0]); - return (val & ~0xf) ? val : (val << 4) | hexval(s[1]); -} +#include "hex-ll.h" /* * Try to read a SHA1 in hexadecimal format from the 40 characters @@ -32,13 +17,6 @@ int get_oid_hex(const char *hex, struct object_id *sha1); /* Like get_oid_hex, but for an arbitrary hash algorithm. */ int get_oid_hex_algop(const char *hex, struct object_id *oid, const struct git_hash_algo *algop); -/* - * Read `len` pairs of hexadecimal digits from `hex` and write the - * values to `binary` as `len` bytes. Return 0 on success, or -1 if - * the input does not consist of hex digits). - */ -int hex_to_bytes(unsigned char *binary, const char *hex, size_t len); - /* * Convert a binary hash in "unsigned char []" or an object name in * "struct object_id *" to its hex equivalent. The `_r` variant is reentrant, diff --git a/mailinfo.c b/mailinfo.c index 2aeb20e5e6..eb34c30be7 100644 --- a/mailinfo.c +++ b/mailinfo.c @@ -1,7 +1,7 @@ #include "git-compat-util.h" #include "config.h" #include "gettext.h" -#include "hex.h" +#include "hex-ll.h" #include "utf8.h" #include "strbuf.h" #include "mailinfo.h" diff --git a/strbuf.c b/strbuf.c index 8dac52b919..a2a05fe168 100644 --- a/strbuf.c +++ b/strbuf.c @@ -1,6 +1,6 @@ #include "git-compat-util.h" #include "gettext.h" -#include "hex.h" +#include "hex-ll.h" #include "strbuf.h" #include "string-list.h" #include "utf8.h" diff --git a/url.c b/url.c index 2e1a9f6fee..282b12495a 100644 --- a/url.c +++ b/url.c @@ -1,5 +1,5 @@ #include "git-compat-util.h" -#include "hex.h" +#include "hex-ll.h" #include "strbuf.h" #include "url.h" diff --git a/urlmatch.c b/urlmatch.c index eba0bdd77f..f1aa87d1dd 100644 --- a/urlmatch.c +++ b/urlmatch.c @@ -1,6 +1,6 @@ #include "git-compat-util.h" #include "gettext.h" -#include "hex.h" +#include "hex-ll.h" #include "strbuf.h" #include "urlmatch.h" From patchwork Tue Jun 27 19:52:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Wan X-Patchwork-Id: 13294943 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 9D10CEB64DC for ; Tue, 27 Jun 2023 19:53:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230410AbjF0TxU (ORCPT ); Tue, 27 Jun 2023 15:53:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55984 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230265AbjF0TxL (ORCPT ); Tue, 27 Jun 2023 15:53:11 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2B2A42117 for ; Tue, 27 Jun 2023 12:53:11 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id 98e67ed59e1d1-26304c2e178so1402590a91.3 for ; Tue, 27 Jun 2023 12:53:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687895590; x=1690487590; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=R7hzoPmQMHKHQiFGKhhcMXV7prE5a3DF/FvGwezczmQ=; b=5p2sHUZ2u5r1h6wLectQvMDnhFQSBns01eddNz0Gr66Jit5U24L5INgjv8Cf5rLGJw PcOuN9C4ZbrSCckL0Op08b+VktqmGYxmCP45P7vitWTnQitJqw6ir6Fezzy5pf+KYxAM Yh15pvn5hrltlZ6a10EbOLlpNCyUosgO5NWSNyT49X/rAmJ12kbckL4iimnPO+ecCu0V ybuiGQpySoShh+E4Z3Bu78ARaUdC2nhgsB8Yz8gz7u+kTIoZlvQZtkieXZxJW/dj2w8K Z4MQSUpojrQuZ/g04498PxPqcKjKr08SpaYbyLxv0O+s1PIjCi91nrZlLYtnnDePXAT4 Nl6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687895590; x=1690487590; 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=R7hzoPmQMHKHQiFGKhhcMXV7prE5a3DF/FvGwezczmQ=; b=lSfFkOCe1bSRUeJcRWO9dm0XYA3mLtKvQplZUeHhrl61mvVmgIUBh6lLJNPDzqXgfJ cy+j1ZZrcy7GkyVUnnI+VYZ390y8V5sTpBI0jI2w/wkNyuSxwbi03K6DX/bcOwbZloLj Q/HisCZIspVyb/sPIWnl2x9X6mrCicsXjXixzBddgMDY5XX3ucFLD8ORmobO6AYKZiPO PFRAYEgJX94M6k6pB4XFFXGU+21oV/igSpJD8xYTtrgVZrZCkISk1XzdSnUuzsLlgj4o 5tvs07+nUw+20DUUxFrgOpp5+xcNoKuokxnNdtKFXahAT32j3u381/yQMDN0rswownu+ f5iw== X-Gm-Message-State: AC+VfDxgBcGk1OUJZMLKsJmUyHHQTeUkiKkvbyrjnWu8zQvBJ1ndoAsL VdwPb/oPUxYf0kWCzjbif0n8/hZp7CjMmKpVD3EUUupq1mw9/l07pMM3n/565R2H6QOuQdYYzxR vVGTx1aaKOGFmMc9ZcgKDHMvlnE1AaYO5rM/bDVj5qzgTLSrBXGTicQVmdASW4rw2oQ== X-Google-Smtp-Source: ACHHUZ7R+nLChvpX5614BuS/TuWq4Cbs99gxlec4D0npWNFSKTiJDYg0oOEQ7yOZhWnhiek8xhzEzboK/pKW4TM= X-Received: from barleywine.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3bd4]) (user=calvinwan job=sendgmr) by 2002:a17:90a:ce8b:b0:262:ff1c:bc2f with SMTP id g11-20020a17090ace8b00b00262ff1cbc2fmr853614pju.5.1687895590610; Tue, 27 Jun 2023 12:53:10 -0700 (PDT) Date: Tue, 27 Jun 2023 19:52:46 +0000 In-Reply-To: <20230627195251.1973421-1-calvinwan@google.com> Mime-Version: 1.0 References: <20230627195251.1973421-1-calvinwan@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230627195251.1973421-4-calvinwan@google.com> Subject: [RFC PATCH 3/8] object: move function to object.c From: Calvin Wan To: git@vger.kernel.org Cc: Calvin Wan , nasamuffin@google.com, chooglen@google.com, johnathantanmy@google.com Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org While remove_or_warn() is a simple ternary operator to call two other wrapper functions, it creates an unnecessary dependency to object.h in wrapper.c. Therefore move the function to object.[ch] where the concept of GITLINKs is first defined. Signed-off-by: Calvin Wan --- object.c | 5 +++++ object.h | 6 ++++++ wrapper.c | 6 ------ wrapper.h | 5 ----- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/object.c b/object.c index 60f954194f..cb29fcc304 100644 --- a/object.c +++ b/object.c @@ -617,3 +617,8 @@ void parsed_object_pool_clear(struct parsed_object_pool *o) FREE_AND_NULL(o->object_state); FREE_AND_NULL(o->shallow_stat); } + +int remove_or_warn(unsigned int mode, const char *file) +{ + return S_ISGITLINK(mode) ? rmdir_or_warn(file) : unlink_or_warn(file); +} diff --git a/object.h b/object.h index 5871615fee..e908ef6515 100644 --- a/object.h +++ b/object.h @@ -284,4 +284,10 @@ void clear_object_flags(unsigned flags); */ void repo_clear_commit_marks(struct repository *r, unsigned int flags); +/* + * Calls the correct function out of {unlink,rmdir}_or_warn based on + * the supplied file mode. + */ +int remove_or_warn(unsigned int mode, const char *path); + #endif /* OBJECT_H */ diff --git a/wrapper.c b/wrapper.c index bd7f0a9752..62c04aeb17 100644 --- a/wrapper.c +++ b/wrapper.c @@ -5,7 +5,6 @@ #include "abspath.h" #include "config.h" #include "gettext.h" -#include "object.h" #include "strbuf.h" static intmax_t count_fsync_writeout_only; @@ -642,11 +641,6 @@ int rmdir_or_warn(const char *file) return warn_if_unremovable("rmdir", file, rmdir(file)); } -int remove_or_warn(unsigned int mode, const char *file) -{ - return S_ISGITLINK(mode) ? rmdir_or_warn(file) : unlink_or_warn(file); -} - static int access_error_is_ok(int err, unsigned flag) { return (is_missing_file_error(err) || diff --git a/wrapper.h b/wrapper.h index db1bc109ed..166740ae60 100644 --- a/wrapper.h +++ b/wrapper.h @@ -111,11 +111,6 @@ int unlink_or_msg(const char *file, struct strbuf *err); * not exist. */ int rmdir_or_warn(const char *path); -/* - * Calls the correct function out of {unlink,rmdir}_or_warn based on - * the supplied file mode. - */ -int remove_or_warn(unsigned int mode, const char *path); /* * Call access(2), but warn for any error except "missing file" From patchwork Tue Jun 27 19:52:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Wan X-Patchwork-Id: 13294944 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 9B41FC001B0 for ; Tue, 27 Jun 2023 19:53:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230364AbjF0TxX (ORCPT ); Tue, 27 Jun 2023 15:53:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229610AbjF0TxN (ORCPT ); Tue, 27 Jun 2023 15:53:13 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7268010F5 for ; Tue, 27 Jun 2023 12:53:12 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1b806d07935so12753085ad.0 for ; Tue, 27 Jun 2023 12:53:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687895592; x=1690487592; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=K2T39xnHDYEY7hV4G88h6xY2GmEwlx6kKroIelOcchI=; b=hg2QRtNt/awVF/zRxQ9wIWnd3pI97pMCA4Uumr+nOOhk/ZoEE5CKZILsEoSs9dW/5Z +8kATu/BNSuhGyWd5DXlW8moxQz1oU+fyBBbUVOXKkB8Q8TLY78LMZ0YRlhI/Wu99OjH LWQYBouh5qfsqzqqQF7FW1PG0ukHuUTRue22eknW+7x0427EwQGmhVVy9S+qZrIBrAjb q4VCnWfXh6PAuxcuGdnf6pLzOZqX5lN/XWdCUxXRMOoiTVFwDDKMJhUpJPMIJmlapxN+ X3quuJ92Qtzai9S4CDW/98qYW/e2kD7sRwSG+LoqH6nknykCG3VUu/bLZHp5pN1epiw+ Y4Ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687895592; x=1690487592; 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=K2T39xnHDYEY7hV4G88h6xY2GmEwlx6kKroIelOcchI=; b=i0s1ZbAGq4bjn0skbmw1DKcVCbzRAQNdM6CkU1JcUiHWWIof8NNMC7EgctwGc33NCO 3/vulqoMK1XU7Hag5v/xb/y2aiqbCMchwPajzdJK+veFgBvaTqMURyg9udjxGBXQLR7B xD/+0IRJZiiawPoOzo+jO+uh8g3EnwovQpbo/HR5iODt2UqqVLAhKyxf4y4tvrw+HlUq uVMTVWSp5kw2dYFvk0FtLyfVvx7pezExSHc98DITw/BEzkOEzDDVO9GrbazrQ0zh64K2 e23ApGWs6FS5dTTQFha9sZzLsYqfWX2d0wiPLy98m5Rxy6DZcVxCNzC+aU0ShoG24TXF Tv4w== X-Gm-Message-State: AC+VfDzFX45XWZIYgqcnvrajIJ8Vk8y1Jyl6OGSMrIIAetY9g7j/gtjM HyTiLcg8vzwFWgF64/Q14lR9E3qN//Z7d0oijsXUhbNoB0IC+R40rdN8IvNXO6K8HvZpRCRvwDM KD5zckFoAyFfgN4CcVh19ZV47nhs+p7N1vkUxEOBcXVGi5cIA7NAydgB66thRiI0CzQ== X-Google-Smtp-Source: ACHHUZ4QFoIvLBmzNiaoo/uj6tq0DoLhIAmwIK+qu9LbY0oPUQetj6OeTGAMMKGBKOUflMJEHL4ZfPh7ej0JV9I= X-Received: from barleywine.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3bd4]) (user=calvinwan job=sendgmr) by 2002:a17:902:c946:b0:1b5:a0c:fddf with SMTP id i6-20020a170902c94600b001b50a0cfddfmr802909pla.6.1687895591947; Tue, 27 Jun 2023 12:53:11 -0700 (PDT) Date: Tue, 27 Jun 2023 19:52:47 +0000 In-Reply-To: <20230627195251.1973421-1-calvinwan@google.com> Mime-Version: 1.0 References: <20230627195251.1973421-1-calvinwan@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230627195251.1973421-5-calvinwan@google.com> Subject: [RFC PATCH 4/8] config: correct bad boolean env value error message From: Calvin Wan To: git@vger.kernel.org Cc: Calvin Wan , nasamuffin@google.com, chooglen@google.com, johnathantanmy@google.com Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org An incorrectly defined boolean environment value would result in the following error message: bad boolean config value '%s' for '%s' This is a misnomer since environment value != config value. Instead of calling git_config_bool() to parse the environment value, mimic the functionality inside of git_config_bool() but with the correct error message. Signed-off-by: Calvin Wan --- config.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/config.c b/config.c index 09851a6909..5b71ef1624 100644 --- a/config.c +++ b/config.c @@ -2172,7 +2172,14 @@ void git_global_config(char **user_out, char **xdg_out) int git_env_bool(const char *k, int def) { const char *v = getenv(k); - return v ? git_config_bool(k, v) : def; + int val; + if (!v) + return def; + val = git_parse_maybe_bool(v); + if (val < 0) + die(_("bad boolean environment value '%s' for '%s'"), + v, k); + return val; } /* From patchwork Tue Jun 27 19:52:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Wan X-Patchwork-Id: 13294945 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 113C9C001B3 for ; Tue, 27 Jun 2023 19:53:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230385AbjF0TxY (ORCPT ); Tue, 27 Jun 2023 15:53:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56024 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230363AbjF0TxT (ORCPT ); Tue, 27 Jun 2023 15:53:19 -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 22AE1FA for ; Tue, 27 Jun 2023 12:53:14 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-262c488c1d3so1784029a91.3 for ; Tue, 27 Jun 2023 12:53:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687895593; x=1690487593; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=CFLigByeLp7/F/OtroClxP0BU3K/wJhvItmEBI3Pp5w=; b=bxBPx5epcrQnkQwzw1fz7Mpt6TQ4UiHWoNRsfPEnMMafillir+TVgSVgnw3lyggn5K RzoOfmu3dZTJkMhtpaDULccOuDk+B6uqgeyurbtWsBcwdXQEcWE0QS4MXpiFEdQJX4mt 8LqfzpwdPSFTa4jW1Am28C8ad5I4/esRm0hBugHjfWwj3L//RyV2fuYNatHGGsuUYi5n RbCr/dgR5fYSt9yxuuBlLAgkVyTkm8chAuLoAmbesbZ3xrP9UiUmbWPi7EnrgllfYSaZ xqZs5JmNz1ElqBETpy9u+/nOYptcvhJCSg5X+VxsVs+iEbYpEQfSz/JtPiPpNDwlD9b6 ZXkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687895593; x=1690487593; 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=CFLigByeLp7/F/OtroClxP0BU3K/wJhvItmEBI3Pp5w=; b=Y+qP2+mpCshatsHG7z5JpMGkhSfeDG2vEkwfnQolHWu4TVLrxyHEGUQcVFvP175Ko8 QdvkDqfcW+Wh1dWgCeC/5IMLcFtFtLsE6/zcAbePorc8IOBozknY63JEyl4ox9jQjwf8 d0m5OK1hV0b4KghRrBVO6iiAK22SVcUfQB/M4kOQNFZqSf9qSihzCrv4bPp18898YWAV G1c83pJP7mBKkzaJzT/Ik/5NJt+yRs/4Okc03nzG3sX/YEy0lhyETB/uPZFfoORlCg31 2MPYcbz8tTkq4tR75Jml7Tc56yZdwVxcMLZh8LqLeU4VOG26XdEmo8iQgEpsWaaHWKYj idhg== X-Gm-Message-State: AC+VfDw2lNroW6oSGdmxmALlgBRr4ocpEMjhhqZL54EkyGO/PSj61udD eCu5YRMOj7JDjuhAQXytTp/WgegFTNOI73GZ9Dbiusw8168HqS2rSdGgQgoF1TzHoxL4K0jHWEE oHpkl73wiupW4S5izu8bL/hD5lUb8x+46YFGOqscwg/moK3zqeTpnRG9nd8IAzEL11A== X-Google-Smtp-Source: ACHHUZ5jM3vSJpopnuycHmnlfIn0Qi/9nmKr0mlvZuOIvdZRZk9qKsPcbppTm69Ae73+dbaHAOcsicjOJD9qkLM= X-Received: from barleywine.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3bd4]) (user=calvinwan job=sendgmr) by 2002:a17:90a:a791:b0:256:3e18:39e with SMTP id f17-20020a17090aa79100b002563e18039emr4898784pjq.3.1687895593506; Tue, 27 Jun 2023 12:53:13 -0700 (PDT) Date: Tue, 27 Jun 2023 19:52:48 +0000 In-Reply-To: <20230627195251.1973421-1-calvinwan@google.com> Mime-Version: 1.0 References: <20230627195251.1973421-1-calvinwan@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230627195251.1973421-6-calvinwan@google.com> Subject: [RFC PATCH 5/8] parse: create new library for parsing strings and env values From: Calvin Wan To: git@vger.kernel.org Cc: Calvin Wan , nasamuffin@google.com, chooglen@google.com, johnathantanmy@google.com Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org While string and environment value parsing is mainly consumed by config.c, there are other files that only need parsing functionality and not config functionality. By separating out string and environment value parsing from config, those files can instead be dependent on parse, which has a much smaller dependency chain than config. Move general string and env parsing functions from config.[ch] to parse.[ch]. Signed-off-by: Calvin Wan --- Makefile | 1 + attr.c | 2 +- config.c | 180 +----------------------------------- config.h | 14 +-- pack-objects.c | 2 +- pack-revindex.c | 2 +- parse-options.c | 3 +- parse.c | 182 +++++++++++++++++++++++++++++++++++++ parse.h | 20 ++++ pathspec.c | 2 +- preload-index.c | 2 +- progress.c | 2 +- prompt.c | 2 +- rebase.c | 2 +- t/helper/test-env-helper.c | 2 +- unpack-trees.c | 2 +- wrapper.c | 2 +- write-or-die.c | 2 +- 18 files changed, 219 insertions(+), 205 deletions(-) create mode 100644 parse.c create mode 100644 parse.h diff --git a/Makefile b/Makefile index 83b385b0be..e9ad9f9ef1 100644 --- a/Makefile +++ b/Makefile @@ -1091,6 +1091,7 @@ LIB_OBJS += pack-write.o LIB_OBJS += packfile.o LIB_OBJS += pager.o LIB_OBJS += parallel-checkout.o +LIB_OBJS += parse.o LIB_OBJS += parse-options-cb.o LIB_OBJS += parse-options.o LIB_OBJS += patch-delta.o diff --git a/attr.c b/attr.c index e9c81b6e07..cb047b4618 100644 --- a/attr.c +++ b/attr.c @@ -7,7 +7,7 @@ */ #include "git-compat-util.h" -#include "config.h" +#include "parse.h" #include "environment.h" #include "exec-cmd.h" #include "attr.h" diff --git a/config.c b/config.c index 5b71ef1624..cdd70999aa 100644 --- a/config.c +++ b/config.c @@ -11,6 +11,7 @@ #include "date.h" #include "branch.h" #include "config.h" +#include "parse.h" #include "convert.h" #include "environment.h" #include "gettext.h" @@ -1204,129 +1205,6 @@ static int git_parse_source(struct config_source *cs, config_fn_t fn, return error_return; } -static uintmax_t get_unit_factor(const char *end) -{ - if (!*end) - return 1; - else if (!strcasecmp(end, "k")) - return 1024; - else if (!strcasecmp(end, "m")) - return 1024 * 1024; - else if (!strcasecmp(end, "g")) - return 1024 * 1024 * 1024; - return 0; -} - -static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max) -{ - if (value && *value) { - char *end; - intmax_t val; - intmax_t factor; - - if (max < 0) - BUG("max must be a positive integer"); - - errno = 0; - val = strtoimax(value, &end, 0); - if (errno == ERANGE) - return 0; - if (end == value) { - errno = EINVAL; - return 0; - } - factor = get_unit_factor(end); - if (!factor) { - errno = EINVAL; - return 0; - } - if ((val < 0 && -max / factor > val) || - (val > 0 && max / factor < val)) { - errno = ERANGE; - return 0; - } - val *= factor; - *ret = val; - return 1; - } - errno = EINVAL; - return 0; -} - -static int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max) -{ - if (value && *value) { - char *end; - uintmax_t val; - uintmax_t factor; - - /* negative values would be accepted by strtoumax */ - if (strchr(value, '-')) { - errno = EINVAL; - return 0; - } - errno = 0; - val = strtoumax(value, &end, 0); - if (errno == ERANGE) - return 0; - if (end == value) { - errno = EINVAL; - return 0; - } - factor = get_unit_factor(end); - if (!factor) { - errno = EINVAL; - return 0; - } - if (unsigned_mult_overflows(factor, val) || - factor * val > max) { - errno = ERANGE; - return 0; - } - val *= factor; - *ret = val; - return 1; - } - errno = EINVAL; - return 0; -} - -int git_parse_int(const char *value, int *ret) -{ - intmax_t tmp; - if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int))) - return 0; - *ret = tmp; - return 1; -} - -static int git_parse_int64(const char *value, int64_t *ret) -{ - intmax_t tmp; - if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int64_t))) - return 0; - *ret = tmp; - return 1; -} - -int git_parse_ulong(const char *value, unsigned long *ret) -{ - uintmax_t tmp; - if (!git_parse_unsigned(value, &tmp, maximum_unsigned_value_of_type(long))) - return 0; - *ret = tmp; - return 1; -} - -int git_parse_ssize_t(const char *value, ssize_t *ret) -{ - intmax_t tmp; - if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(ssize_t))) - return 0; - *ret = tmp; - return 1; -} - static int reader_config_name(struct config_reader *reader, const char **out); static int reader_origin_type(struct config_reader *reader, enum config_origin_type *type); @@ -1404,23 +1282,6 @@ ssize_t git_config_ssize_t(const char *name, const char *value) return ret; } -static int git_parse_maybe_bool_text(const char *value) -{ - if (!value) - return 1; - if (!*value) - return 0; - if (!strcasecmp(value, "true") - || !strcasecmp(value, "yes") - || !strcasecmp(value, "on")) - return 1; - if (!strcasecmp(value, "false") - || !strcasecmp(value, "no") - || !strcasecmp(value, "off")) - return 0; - return -1; -} - static const struct fsync_component_name { const char *name; enum fsync_component component_bits; @@ -1495,16 +1356,6 @@ static enum fsync_component parse_fsync_components(const char *var, const char * return (current & ~negative) | positive; } -int git_parse_maybe_bool(const char *value) -{ - int v = git_parse_maybe_bool_text(value); - if (0 <= v) - return v; - if (git_parse_int(value, &v)) - return !!v; - return -1; -} - int git_config_bool_or_int(const char *name, const char *value, int *is_bool) { int v = git_parse_maybe_bool_text(value); @@ -2165,35 +2016,6 @@ void git_global_config(char **user_out, char **xdg_out) *xdg_out = xdg_config; } -/* - * Parse environment variable 'k' as a boolean (in various - * possible spellings); if missing, use the default value 'def'. - */ -int git_env_bool(const char *k, int def) -{ - const char *v = getenv(k); - int val; - if (!v) - return def; - val = git_parse_maybe_bool(v); - if (val < 0) - die(_("bad boolean environment value '%s' for '%s'"), - v, k); - return val; -} - -/* - * Parse environment variable 'k' as ulong with possibly a unit - * suffix; if missing, use the default value 'val'. - */ -unsigned long git_env_ulong(const char *k, unsigned long val) -{ - const char *v = getenv(k); - if (v && !git_parse_ulong(v, &val)) - die(_("failed to parse %s"), k); - return val; -} - int git_config_system(void) { return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0); diff --git a/config.h b/config.h index 247b572b37..7a7f53e503 100644 --- a/config.h +++ b/config.h @@ -3,7 +3,7 @@ #include "hashmap.h" #include "string-list.h" - +#include "parse.h" /** * The config API gives callers a way to access Git configuration files @@ -205,16 +205,6 @@ int config_with_options(config_fn_t fn, void *, * The following helper functions aid in parsing string values */ -int git_parse_ssize_t(const char *, ssize_t *); -int git_parse_ulong(const char *, unsigned long *); -int git_parse_int(const char *value, int *ret); - -/** - * Same as `git_config_bool`, except that it returns -1 on error rather - * than dying. - */ -int git_parse_maybe_bool(const char *); - /** * Parse the string to an integer, including unit factors. Dies on error; * otherwise, returns the parsed result. @@ -343,8 +333,6 @@ int git_config_rename_section(const char *, const char *); int git_config_rename_section_in_file(const char *, const char *, const char *); int git_config_copy_section(const char *, const char *); int git_config_copy_section_in_file(const char *, const char *, const char *); -int git_env_bool(const char *, int); -unsigned long git_env_ulong(const char *, unsigned long); int git_config_system(void); int config_error_nonbool(const char *); #if defined(__GNUC__) diff --git a/pack-objects.c b/pack-objects.c index 1b8052bece..f403ca6986 100644 --- a/pack-objects.c +++ b/pack-objects.c @@ -3,7 +3,7 @@ #include "pack.h" #include "pack-objects.h" #include "packfile.h" -#include "config.h" +#include "parse.h" static uint32_t locate_object_entry_hash(struct packing_data *pdata, const struct object_id *oid, diff --git a/pack-revindex.c b/pack-revindex.c index 7fffcad912..a01a2a4640 100644 --- a/pack-revindex.c +++ b/pack-revindex.c @@ -6,7 +6,7 @@ #include "packfile.h" #include "strbuf.h" #include "trace2.h" -#include "config.h" +#include "parse.h" #include "midx.h" #include "csum-file.h" diff --git a/parse-options.c b/parse-options.c index f8a155ee13..9f542950a7 100644 --- a/parse-options.c +++ b/parse-options.c @@ -1,11 +1,12 @@ #include "git-compat-util.h" #include "parse-options.h" #include "abspath.h" -#include "config.h" +#include "parse.h" #include "commit.h" #include "color.h" #include "gettext.h" #include "strbuf.h" +#include "string-list.h" #include "utf8.h" static int disallow_abbreviated_options; diff --git a/parse.c b/parse.c new file mode 100644 index 0000000000..42d691a0fb --- /dev/null +++ b/parse.c @@ -0,0 +1,182 @@ +#include "git-compat-util.h" +#include "gettext.h" +#include "parse.h" + +static uintmax_t get_unit_factor(const char *end) +{ + if (!*end) + return 1; + else if (!strcasecmp(end, "k")) + return 1024; + else if (!strcasecmp(end, "m")) + return 1024 * 1024; + else if (!strcasecmp(end, "g")) + return 1024 * 1024 * 1024; + return 0; +} + +int git_parse_signed(const char *value, intmax_t *ret, intmax_t max) +{ + if (value && *value) { + char *end; + intmax_t val; + intmax_t factor; + + if (max < 0) + BUG("max must be a positive integer"); + + errno = 0; + val = strtoimax(value, &end, 0); + if (errno == ERANGE) + return 0; + if (end == value) { + errno = EINVAL; + return 0; + } + factor = get_unit_factor(end); + if (!factor) { + errno = EINVAL; + return 0; + } + if ((val < 0 && -max / factor > val) || + (val > 0 && max / factor < val)) { + errno = ERANGE; + return 0; + } + val *= factor; + *ret = val; + return 1; + } + errno = EINVAL; + return 0; +} + +static int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max) +{ + if (value && *value) { + char *end; + uintmax_t val; + uintmax_t factor; + + /* negative values would be accepted by strtoumax */ + if (strchr(value, '-')) { + errno = EINVAL; + return 0; + } + errno = 0; + val = strtoumax(value, &end, 0); + if (errno == ERANGE) + return 0; + if (end == value) { + errno = EINVAL; + return 0; + } + factor = get_unit_factor(end); + if (!factor) { + errno = EINVAL; + return 0; + } + if (unsigned_mult_overflows(factor, val) || + factor * val > max) { + errno = ERANGE; + return 0; + } + val *= factor; + *ret = val; + return 1; + } + errno = EINVAL; + return 0; +} + +int git_parse_int(const char *value, int *ret) +{ + intmax_t tmp; + if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int))) + return 0; + *ret = tmp; + return 1; +} + +int git_parse_int64(const char *value, int64_t *ret) +{ + intmax_t tmp; + if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int64_t))) + return 0; + *ret = tmp; + return 1; +} + +int git_parse_ulong(const char *value, unsigned long *ret) +{ + uintmax_t tmp; + if (!git_parse_unsigned(value, &tmp, maximum_unsigned_value_of_type(long))) + return 0; + *ret = tmp; + return 1; +} + +int git_parse_ssize_t(const char *value, ssize_t *ret) +{ + intmax_t tmp; + if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(ssize_t))) + return 0; + *ret = tmp; + return 1; +} + +int git_parse_maybe_bool_text(const char *value) +{ + if (!value) + return 1; + if (!*value) + return 0; + if (!strcasecmp(value, "true") + || !strcasecmp(value, "yes") + || !strcasecmp(value, "on")) + return 1; + if (!strcasecmp(value, "false") + || !strcasecmp(value, "no") + || !strcasecmp(value, "off")) + return 0; + return -1; +} + +int git_parse_maybe_bool(const char *value) +{ + int v = git_parse_maybe_bool_text(value); + if (0 <= v) + return v; + if (git_parse_int(value, &v)) + return !!v; + return -1; +} + +/* + * Parse environment variable 'k' as a boolean (in various + * possible spellings); if missing, use the default value 'def'. + */ +int git_env_bool(const char *k, int def) +{ + const char *v = getenv(k); + int val; + if (!v) + return def; + val = git_parse_maybe_bool(v); + if (val < 0) + die(_("bad boolean environment value '%s' for '%s'"), + v, k); + return val; +} + +/* + * Parse environment variable 'k' as ulong with possibly a unit + * suffix; if missing, use the default value 'val'. + */ +unsigned long git_env_ulong(const char *k, unsigned long val) +{ + const char *v = getenv(k); + if (v && !git_parse_ulong(v, &val)) + die(_("failed to parse %s"), k); + return val; +} diff --git a/parse.h b/parse.h new file mode 100644 index 0000000000..07d2193d69 --- /dev/null +++ b/parse.h @@ -0,0 +1,20 @@ +#ifndef PARSE_H +#define PARSE_H + +int git_parse_signed(const char *value, intmax_t *ret, intmax_t max); +int git_parse_ssize_t(const char *, ssize_t *); +int git_parse_ulong(const char *, unsigned long *); +int git_parse_int(const char *value, int *ret); +int git_parse_int64(const char *value, int64_t *ret); + +/** + * Same as `git_config_bool`, except that it returns -1 on error rather + * than dying. + */ +int git_parse_maybe_bool(const char *); +int git_parse_maybe_bool_text(const char *value); + +int git_env_bool(const char *, int); +unsigned long git_env_ulong(const char *, unsigned long); + +#endif /* PARSE_H */ diff --git a/pathspec.c b/pathspec.c index 4991455281..39337999d4 100644 --- a/pathspec.c +++ b/pathspec.c @@ -1,6 +1,6 @@ #include "git-compat-util.h" #include "abspath.h" -#include "config.h" +#include "parse.h" #include "dir.h" #include "environment.h" #include "gettext.h" diff --git a/preload-index.c b/preload-index.c index e44530c80c..63fd35d64b 100644 --- a/preload-index.c +++ b/preload-index.c @@ -7,7 +7,7 @@ #include "environment.h" #include "fsmonitor.h" #include "gettext.h" -#include "config.h" +#include "parse.h" #include "preload-index.h" #include "progress.h" #include "read-cache.h" diff --git a/progress.c b/progress.c index f695798aca..c83cb60bf1 100644 --- a/progress.c +++ b/progress.c @@ -17,7 +17,7 @@ #include "trace.h" #include "trace2.h" #include "utf8.h" -#include "config.h" +#include "parse.h" #define TP_IDX_MAX 8 diff --git a/prompt.c b/prompt.c index 3baa33f63d..8935fe4dfb 100644 --- a/prompt.c +++ b/prompt.c @@ -1,5 +1,5 @@ #include "git-compat-util.h" -#include "config.h" +#include "parse.h" #include "environment.h" #include "run-command.h" #include "strbuf.h" diff --git a/rebase.c b/rebase.c index 17a570f1ff..69a1822da3 100644 --- a/rebase.c +++ b/rebase.c @@ -1,6 +1,6 @@ #include "git-compat-util.h" #include "rebase.h" -#include "config.h" +#include "parse.h" #include "gettext.h" /* diff --git a/t/helper/test-env-helper.c b/t/helper/test-env-helper.c index 66c88b8ff3..1c486888a4 100644 --- a/t/helper/test-env-helper.c +++ b/t/helper/test-env-helper.c @@ -1,5 +1,5 @@ #include "test-tool.h" -#include "config.h" +#include "parse.h" #include "parse-options.h" static char const * const env__helper_usage[] = { diff --git a/unpack-trees.c b/unpack-trees.c index 87517364dc..761562a96e 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -2,7 +2,7 @@ #include "advice.h" #include "strvec.h" #include "repository.h" -#include "config.h" +#include "parse.h" #include "dir.h" #include "environment.h" #include "gettext.h" diff --git a/wrapper.c b/wrapper.c index 62c04aeb17..3e554f50c6 100644 --- a/wrapper.c +++ b/wrapper.c @@ -3,7 +3,7 @@ */ #include "git-compat-util.h" #include "abspath.h" -#include "config.h" +#include "parse.h" #include "gettext.h" #include "strbuf.h" diff --git a/write-or-die.c b/write-or-die.c index d8355c0c3e..42a2dc73cd 100644 --- a/write-or-die.c +++ b/write-or-die.c @@ -1,5 +1,5 @@ #include "git-compat-util.h" -#include "config.h" +#include "parse.h" #include "run-command.h" #include "write-or-die.h" From patchwork Tue Jun 27 19:52:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Wan X-Patchwork-Id: 13294946 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 5C60CEB64D9 for ; Tue, 27 Jun 2023 19:53:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230453AbjF0Tx0 (ORCPT ); Tue, 27 Jun 2023 15:53:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230301AbjF0TxU (ORCPT ); Tue, 27 Jun 2023 15:53:20 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D685D2716 for ; Tue, 27 Jun 2023 12:53:15 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1b7fd27e107so12697355ad.1 for ; Tue, 27 Jun 2023 12:53:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687895595; x=1690487595; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=NgO53NfzPnoedZITujD9udrpcJdfIMWANeDb3IGDuBQ=; b=AKMUea7BBF90KUBhDSsrXgVympAbQrMZA5UOOwzLssPOuckO4mQIWIRtbDsHLmQU3t 1UXDGktPdEZ1GKy4CQkkqjw/0YBIPYLFkQpNOCr++qmGlzRGdI6s26jrQfZAYV30YHli 65Cl0PtUIrgC4uSbSMlHfU9SDLjyolJpdnWqN5ZXkvEEqRHCbMd1MAhrFf3PdoASEtCl 7iCJQXYMfA3zVfbV4fEdMHAIQu6VfTm3aNBmuSM2Ah+DgKp25TZg8vhS3R+N7c4+h8JD J6CPamicT6chg9/g+UGgNe6I5xdq8GEX/nF7yfVhSSHC1qqUNOE7JxuxbWOgo5a4IWVU CCVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687895595; x=1690487595; 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=NgO53NfzPnoedZITujD9udrpcJdfIMWANeDb3IGDuBQ=; b=agJDm1DDEXngB81FybE+5OMIhPBBWQ61EeI56L10X74g50tU1zvGFmbSEAIXWRXkqv PXS3ipoJ7WVkooCSjNbMIMwE7NonU6yB1+djQDYYq95bZWM0cc/Rzq2IZmRfZgtBm3Ct 4M8UtuR9pBNHnI/LAnrilKVGo/mn/XFC1ekXl7pMY/JNR9ZgK/2d1NhkqCz1bEeUc3T2 /f6PpkqBlWdV4hqenz0yd7w8aWhrDn3AHFN5sZDi4Z1F7qAAtDkhYtAcZN4YiOnQjX79 LKyADQH5UbjbO3sdIlsOO18OlV20GEh9vMYIU1K2cXKFVW69ZgdX5lbTzwijNLVWmPDA ecbw== X-Gm-Message-State: AC+VfDyjycETkk9IzRb3+6BOjzsVRE4PgzKmWHzsspeB+XiosFb1d9lg xcO4kgCLx1yVzLJqA7tiW3c904L5p69ejr+cQ7kJ/z3wKNaCK7uWBUxSmnj5ZWw+BLpPsnwOKV1 vMLyMs6hVREmDEiYdL/RgQP9g7ZYQyfAyN5pG4wKtVny0o7cRqi9NIqXolEfW6rwEuQ== X-Google-Smtp-Source: ACHHUZ7W9lpU/psh6iMlPflz39D5G84QVMz2Ww3DtBfEnPcQzabxqSYoSvqncS9LyrnrGp9CxCKQlb9tB0fYyO0= X-Received: from barleywine.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3bd4]) (user=calvinwan job=sendgmr) by 2002:a17:902:f082:b0:1b7:edcd:8dcf with SMTP id p2-20020a170902f08200b001b7edcd8dcfmr822075pla.4.1687895595258; Tue, 27 Jun 2023 12:53:15 -0700 (PDT) Date: Tue, 27 Jun 2023 19:52:49 +0000 In-Reply-To: <20230627195251.1973421-1-calvinwan@google.com> Mime-Version: 1.0 References: <20230627195251.1973421-1-calvinwan@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230627195251.1973421-7-calvinwan@google.com> Subject: [RFC PATCH 6/8] pager: remove pager_in_use() From: Calvin Wan To: git@vger.kernel.org Cc: Calvin Wan , nasamuffin@google.com, chooglen@google.com, johnathantanmy@google.com Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org pager_in_use() is simply a wrapper around git_env_bool("GIT_PAGER_IN_USE", 0). Other places that call git_env_bool() in this fashion also do not have a wrapper function around it. By removing pager_in_use(), we can also get rid of the pager.h dependency from a few files. Signed-off-by: Calvin Wan --- builtin/log.c | 2 +- color.c | 2 +- column.c | 2 +- date.c | 4 ++-- git.c | 2 +- pager.c | 5 ----- pager.h | 1 - 7 files changed, 6 insertions(+), 12 deletions(-) diff --git a/builtin/log.c b/builtin/log.c index 03954fb749..d5e979932f 100644 --- a/builtin/log.c +++ b/builtin/log.c @@ -82,7 +82,7 @@ struct line_opt_callback_data { static int session_is_interactive(void) { - return isatty(1) || pager_in_use(); + return isatty(1) || git_env_bool("GIT_PAGER_IN_USE", 0); } static int auto_decoration_style(void) diff --git a/color.c b/color.c index f3c0a4659b..dd6f26b8db 100644 --- a/color.c +++ b/color.c @@ -388,7 +388,7 @@ static int check_auto_color(int fd) int *is_tty_p = fd == 1 ? &color_stdout_is_tty : &color_stderr_is_tty; if (*is_tty_p < 0) *is_tty_p = isatty(fd); - if (*is_tty_p || (fd == 1 && pager_in_use() && pager_use_color)) { + if (*is_tty_p || (fd == 1 && git_env_bool("GIT_PAGER_IN_USE", 0) && pager_use_color)) { if (!is_terminal_dumb()) return 1; } diff --git a/column.c b/column.c index ff2f0abf39..e15ca70f36 100644 --- a/column.c +++ b/column.c @@ -214,7 +214,7 @@ int finalize_colopts(unsigned int *colopts, int stdout_is_tty) if (stdout_is_tty < 0) stdout_is_tty = isatty(1); *colopts &= ~COL_ENABLE_MASK; - if (stdout_is_tty || pager_in_use()) + if (stdout_is_tty || git_env_bool("GIT_PAGER_IN_USE", 0)) *colopts |= COL_ENABLED; } return 0; diff --git a/date.c b/date.c index 619ada5b20..95c0f568ba 100644 --- a/date.c +++ b/date.c @@ -7,7 +7,7 @@ #include "git-compat-util.h" #include "date.h" #include "gettext.h" -#include "pager.h" +#include "parse.h" #include "strbuf.h" /* @@ -1009,7 +1009,7 @@ void parse_date_format(const char *format, struct date_mode *mode) /* "auto:foo" is "if tty/pager, then foo, otherwise normal" */ if (skip_prefix(format, "auto:", &p)) { - if (isatty(1) || pager_in_use()) + if (isatty(1) || git_env_bool("GIT_PAGER_IN_USE", 0)) format = p; else format = "default"; diff --git a/git.c b/git.c index eb69f4f997..3bfb673a4c 100644 --- a/git.c +++ b/git.c @@ -131,7 +131,7 @@ static void commit_pager_choice(void) void setup_auto_pager(const char *cmd, int def) { - if (use_pager != -1 || pager_in_use()) + if (use_pager != -1 || git_env_bool("GIT_PAGER_IN_USE", 0)) return; use_pager = check_pager_config(cmd); if (use_pager == -1) diff --git a/pager.c b/pager.c index 63055d0873..9b392622d2 100644 --- a/pager.c +++ b/pager.c @@ -149,11 +149,6 @@ void setup_pager(void) atexit(wait_for_pager_atexit); } -int pager_in_use(void) -{ - return git_env_bool("GIT_PAGER_IN_USE", 0); -} - /* * Return cached value (if set) or $COLUMNS environment variable (if * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive), diff --git a/pager.h b/pager.h index b77433026d..6832c6168d 100644 --- a/pager.h +++ b/pager.h @@ -5,7 +5,6 @@ struct child_process; const char *git_pager(int stdout_is_tty); void setup_pager(void); -int pager_in_use(void); int term_columns(void); void term_clear_line(void); int decimal_width(uintmax_t); From patchwork Tue Jun 27 19:52:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Wan X-Patchwork-Id: 13294948 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 8B8D6C001B0 for ; Tue, 27 Jun 2023 19:53:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230488AbjF0Txd (ORCPT ); Tue, 27 Jun 2023 15:53:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230415AbjF0TxU (ORCPT ); Tue, 27 Jun 2023 15:53:20 -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 8B059295B for ; Tue, 27 Jun 2023 12:53:17 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-2630c837b85so870087a91.2 for ; Tue, 27 Jun 2023 12:53:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687895597; x=1690487597; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=LlVT+KPD+T9RWFovvrmKT61e3O814ep9SnoSsrWKRBw=; b=H9Sm9yUsk6D2UanQjTmFEUkNW2oC1bAWAk9kqQ0MmmkzoutYoY4+MSUQSLogXorXk4 3+mD3dL83811C3+Dr0wSeM1JfB0/6LhxQvrcoRePtDr+oa/I7yZV+P8BeZkNkhNQL3Dm R7InzHPu5kMHb0fj/OpyaPZbG7mqiMaGvyfjZqpgeO0kB63+ygSdRmwFgnZEfk45/+Ut iTANnNq1ba8Jkcn3xS+6Our5JmTVgEC6WeYtWz/3uiAqhx/qscW0X6oQYw4US9Ika/wN NZyiQztec9uYsx3yu0bzFPQrX+a1xzjDvY365qQdl8OvrhREv1EcpQ1I4d+aStwrQQNx 6mUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687895597; x=1690487597; 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=LlVT+KPD+T9RWFovvrmKT61e3O814ep9SnoSsrWKRBw=; b=YPghHqlOxDk926W8yZKzhXWSCBT11+tew3I1wOhf1eFjiafy7DyJF4RU4rXTfiL4oK cUjF9z6ZcZ+2sySxnxaU7ld2ok8kL9bmhoo3SM8Y2LW2dA5/cO5+bF5076gLFCGTU7Zx dDzI51W7ARSWpuJZUzBc8GsZ9XE8+S6D0MNukoHKFHOYJg6CK6uQp+VrWO9AbhSYE02S B2WqFYdts6voqmIwGoWZKOPfJzXnZJrdX9lpnTVIDVlWNJWClRaX1+t8cHXBOl1aq5Je I1wRGM8q+JjpOEcbx4+cF4HpIoBSrcDgCxQ16nYq0+I6oius1FfGRjwnzgkedbgr/DE7 ihPQ== X-Gm-Message-State: AC+VfDxrLySXa0Y3GdwqOij/EqPyG955Z09/TgC1M4XADSbECB0DvzKA nRnDvkik3oMQAqVFL10TmXKTHd+IBNQgUs3QBYkFnO2ye/6/8lrATXLY1cwTzvC8jCj12h4kLxf YL4XnmCBS20spIKCsjiCrdEz1i2blCMizsFjXPxMwfJngTuZkVZ0T0eUJZ/PN7ochcA== X-Google-Smtp-Source: ACHHUZ4FN2f++xtAjopizAA/F16dJer2fmB4+GxuP2OCE8dV/K44dmOhayR7YcS7RZm5EFK8GJVgRdra7CR1PEE= X-Received: from barleywine.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3bd4]) (user=calvinwan job=sendgmr) by 2002:a17:90a:f013:b0:262:e6b6:e0b1 with SMTP id bt19-20020a17090af01300b00262e6b6e0b1mr1099268pjb.4.1687895596983; Tue, 27 Jun 2023 12:53:16 -0700 (PDT) Date: Tue, 27 Jun 2023 19:52:50 +0000 In-Reply-To: <20230627195251.1973421-1-calvinwan@google.com> Mime-Version: 1.0 References: <20230627195251.1973421-1-calvinwan@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230627195251.1973421-8-calvinwan@google.com> Subject: [RFC PATCH 7/8] git-std-lib: introduce git standard library From: Calvin Wan To: git@vger.kernel.org Cc: Calvin Wan , nasamuffin@google.com, chooglen@google.com, johnathantanmy@google.com Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The Git Standard Library intends to serve as the foundational library and root dependency that other libraries in Git will be built off of. That is to say, suppose we have libraries X and Y; a user that wants to use X and Y would need to include X, Y, and this Git Standard Library. Add Documentation/technical/git-std-lib.txt to further explain the design and rationale. Signed-off-by: Calvin Wan --- Documentation/technical/git-std-lib.txt | 182 ++++++++++++++++++++++++ Makefile | 28 +++- git-compat-util.h | 7 +- symlinks.c | 2 + usage.c | 8 ++ 5 files changed, 225 insertions(+), 2 deletions(-) create mode 100644 Documentation/technical/git-std-lib.txt diff --git a/Documentation/technical/git-std-lib.txt b/Documentation/technical/git-std-lib.txt new file mode 100644 index 0000000000..3dce36c9f9 --- /dev/null +++ b/Documentation/technical/git-std-lib.txt @@ -0,0 +1,182 @@ +Git Standard Library +================ + +The Git Standard Library intends to serve as the foundational library +and root dependency that other libraries in Git will be built off of. +That is to say, suppose we have libraries X and Y; a user that wants to +use X and Y would need to include X, Y, and this Git Standard Library. +This does not mean that the Git Standard Library will be the only +possible root dependency in the future, but rather the most significant +and widely used one. + +Dependency graph in libified Git +================ + +If you look in the Git Makefile, all of the objects defined in the Git +library are compiled and archived into a singular file, libgit.a, which +is linked against by common-main.o with other external dependencies and +turned into the Git executable. In other words, the Git executable has +dependencies on libgit.a and a couple of external libraries. The +libfication of Git will not affect this current build flow, but instead +will provide an alternate method for building Git. + +With our current method of building Git, we can imagine the dependency +graph as such: + + Git + /\ + / \ + / \ + libgit.a ext deps + +In libifying parts of Git, we want to shrink the dependency graph to +only the minimal set of dependencies, so libraries should not use +libgit.a. Instead, it would look like: + + Git + /\ + / \ + / \ + libgit.a ext deps + /\ + / \ + / \ +object-store.a (other lib) + | / + | / + | / + config.a / + | / + | / + | / +git-std-lib.a + +Instead of containing all of the objects in Git, libgit.a would contain +objects that are not built by libraries it links against. Consequently, +if someone wanted their own custom build of Git with their own custom +implementation of the object store, they would only have to swap out +object-store.a rather than do a hard fork of Git. + +Rationale behind Git Standard Library +================ + +The rationale behind Git Standard Library essentially is the result of +two observations within the Git codebase: every file includes +git-compat-util.h which defines functions in a couple of different +files, and wrapper.c + usage.c have difficult-to-separate circular +dependencies with each other and other files. + +Ubiquity of git-compat-util.h and circular dependencies +======== + +Every file in the Git codebase includes git-compat-util.h. It serves as +"a compatibility aid that isolates the knowledge of platform specific +inclusion order and what feature macros to define before including which +system header" (Junio[1]). Since every file includes git-compat-util.h, and +git-compat-util.h includes wrapper.h and usage.h, it would make sense +for wrapper.c and usage.c to be a part of the root library. They have +difficult to separate circular dependencies with each other so they +can't be independent libraries. Wrapper.c has dependencies on parse.c, +abspath.c, strbuf.c, which in turn also have dependencies on usage.c and +wrapper.c -- more circular dependencies. + +Tradeoff between swappability and refactoring +======== + +From the above dependency graph, we can see that git-std-lib.a could be +many smaller libraries rather than a singular library. So why choose a +singular library when multiple libraries can be individually easier to +swap and are more modular? A singular library requires less work to +separate out circular dependencies within itself so it becomes a +tradeoff question between work and reward. While there may be a point in +the future where a file like usage.c would want its own library so that +someone can have custom die() or error(), the work required to refactor +out the circular dependencies in some files would be enormous due to +their ubiquity so therefore I believe it is not worth the tradeoff +currently. Additionally, we can in the future choose to do this refactor +and change the API for the library if there becomes enough of a reason +to do so (remember we are avoiding promising stability of the interfaces +of those libraries). + +Reuse of compatibility functions in git-compat-util.h +======== + +Most functions defined in git-compat-util.h are implemented in compat/ +and have dependencies limited to strbuf.h and wrapper.h so they can be +easily included in git-std-lib.a, which as a root dependency means that +higher level libraries do not have to worry about compatibility files in +compat/. The rest of the functions defined in git-compat-util.h are +implemented in top level files and, in this patch set, are hidden behind +an #ifdef if their implementation is not in git-std-lib.a. + +Rationale summary +======== + +The Git Standard Library allows us to get the libification ball rolling +with other libraries in Git. By not spending many +more months attempting to refactor difficult circular dependencies and +instead spending that time getting to a state where we can test out +swapping a library out such as config or object store, we can prove the +viability of Git libification on a much faster time scale. Additionally +the code cleanups that have happened so far have been minor and +beneficial for the codebase. It is probable that making large movements +would negatively affect code clarity. + +Git Standard Library boundary +================ + +While I have described above some useful heuristics for identifying +potential candidates for git-std-lib.a, a standard library should not +have a shaky definition for what belongs in it. + + - Low-level files (aka operates only on other primitive types) that are + used everywhere within the codebase (wrapper.c, usage.c, strbuf.c) + - Dependencies that are low-level and widely used + (abspath.c, date.c, hex-ll.c, parse.c, utf8.c) + - low-level git/* files with functions defined in git-compat-util.h + (ctype.c) + - compat/* + +There are other files that might fit this definition, but that does not +mean it should belong in git-std-lib.a. Those files should start as +their own separate library since any file added to git-std-lib.a loses +its flexibility of being easily swappable. + +Files inside of Git Standard Library +================ + +The initial set of files in git-std-lib.a are: +abspath.c +ctype.c +date.c +hex-ll.c +parse.c +strbuf.c +usage.c +utf8.c +wrapper.c +relevant compat/ files + +Pitfalls +================ + +In patch 7, I use #ifdef GIT_STD_LIB to both stub out code and hide +certain function headers. As other parts of Git are libified, if we +have to use more ifdefs for each different library, then the codebase +will become uglier and harder to understand. + +There are a small amount of files under compat/* that have dependencies +not inside of git-std-lib.a. While those functions are not called on +Linux, other OSes might call those problematic functions. I don't see +this as a major problem, just moreso an observation that libification in +general may also require some minor compatibility work in the future. + +Testing +================ + +Unit tests should catch any breakages caused by changes to files in +git-std-lib.a (i.e. introduction of a out of scope dependency) and new +functions introduced to git-std-lib.a will require unit tests written +for them. + +[1] https://lore.kernel.org/git/xmqqwn17sydw.fsf@gitster.g/ \ No newline at end of file diff --git a/Makefile b/Makefile index e9ad9f9ef1..255bd10b82 100644 --- a/Makefile +++ b/Makefile @@ -2162,6 +2162,11 @@ ifdef FSMONITOR_OS_SETTINGS COMPAT_OBJS += compat/fsmonitor/fsm-path-utils-$(FSMONITOR_OS_SETTINGS).o endif +ifdef GIT_STD_LIB + BASIC_CFLAGS += -DGIT_STD_LIB + BASIC_CFLAGS += -DNO_GETTEXT +endif + ifeq ($(TCLTK_PATH),) NO_TCLTK = NoThanks endif @@ -3654,7 +3659,7 @@ clean: profile-clean coverage-clean cocciclean $(RM) po/git.pot po/git-core.pot $(RM) git.res $(RM) $(OBJECTS) - $(RM) $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB) $(REFTABLE_TEST_LIB) + $(RM) $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB) $(REFTABLE_TEST_LIB) $(STD_LIB_FILE) $(RM) $(ALL_PROGRAMS) $(SCRIPT_LIB) $(BUILT_INS) $(OTHER_PROGRAMS) $(RM) $(TEST_PROGRAMS) $(RM) $(FUZZ_PROGRAMS) @@ -3834,3 +3839,24 @@ $(FUZZ_PROGRAMS): all $(XDIFF_OBJS) $(EXTLIBS) git.o $@.o $(LIB_FUZZING_ENGINE) -o $@ fuzz-all: $(FUZZ_PROGRAMS) + +### Libified Git rules + +# git-std-lib +# `make git-std-lib GIT_STD_LIB=YesPlease` +STD_LIB = git-std-lib.a + +GIT_STD_LIB_OBJS += abspath.o +GIT_STD_LIB_OBJS += ctype.o +GIT_STD_LIB_OBJS += date.o +GIT_STD_LIB_OBJS += hex-ll.o +GIT_STD_LIB_OBJS += parse.o +GIT_STD_LIB_OBJS += strbuf.o +GIT_STD_LIB_OBJS += usage.o +GIT_STD_LIB_OBJS += utf8.o +GIT_STD_LIB_OBJS += wrapper.o + +$(STD_LIB): $(GIT_STD_LIB_OBJS) $(COMPAT_OBJS) + $(QUIET_AR)$(RM) $@ && $(AR) $(ARFLAGS) $@ $^ + +git-std-lib: $(STD_LIB) diff --git a/git-compat-util.h b/git-compat-util.h index 481dac22b0..75aa9b263e 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -396,8 +396,8 @@ static inline int noop_core_config(const char *var UNUSED, #define platform_core_config noop_core_config #endif +#if !defined(__MINGW32__) && !defined(_MSC_VER) && !defined(GIT_STD_LIB) int lstat_cache_aware_rmdir(const char *path); -#if !defined(__MINGW32__) && !defined(_MSC_VER) #define rmdir lstat_cache_aware_rmdir #endif @@ -787,9 +787,11 @@ const char *inet_ntop(int af, const void *src, char *dst, size_t size); #endif #ifdef NO_PTHREADS +#ifdef GIT_STD_LIB #define atexit git_atexit int git_atexit(void (*handler)(void)); #endif +#endif /* * Limit size of IO chunks, because huge chunks only cause pain. OS X @@ -951,14 +953,17 @@ int git_access(const char *path, int mode); # endif #endif +#ifndef GIT_STD_LIB int cmd_main(int, const char **); /* * Intercept all calls to exit() and route them to trace2 to * optionally emit a message before calling the real exit(). */ + int common_exit(const char *file, int line, int code); #define exit(code) exit(common_exit(__FILE__, __LINE__, (code))) +#endif /* * You can mark a stack variable with UNLEAK(var) to avoid it being diff --git a/symlinks.c b/symlinks.c index b29e340c2d..bced721a0c 100644 --- a/symlinks.c +++ b/symlinks.c @@ -337,6 +337,7 @@ void invalidate_lstat_cache(void) reset_lstat_cache(&default_cache); } +#if !defined(__MINGW32__) && !defined(_MSC_VER) && !defined(GIT_STD_LIB) #undef rmdir int lstat_cache_aware_rmdir(const char *path) { @@ -348,3 +349,4 @@ int lstat_cache_aware_rmdir(const char *path) return ret; } +#endif diff --git a/usage.c b/usage.c index 09f0ed509b..58994e0d5c 100644 --- a/usage.c +++ b/usage.c @@ -5,7 +5,15 @@ */ #include "git-compat-util.h" #include "gettext.h" + +#ifdef GIT_STD_LIB +#undef trace2_cmd_name +#undef trace2_cmd_error_va +#define trace2_cmd_name(x) +#define trace2_cmd_error_va(x, y) +#else #include "trace2.h" +#endif static void vreportf(const char *prefix, const char *err, va_list params) { From patchwork Tue Jun 27 19:52:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Wan X-Patchwork-Id: 13294947 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 CFD52EB64D9 for ; Tue, 27 Jun 2023 19:53:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230474AbjF0Tx3 (ORCPT ); Tue, 27 Jun 2023 15:53:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56024 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230353AbjF0TxU (ORCPT ); Tue, 27 Jun 2023 15:53:20 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0647D10F5 for ; Tue, 27 Jun 2023 12:53:19 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id 41be03b00d2f7-55ac8fffd76so2572367a12.3 for ; Tue, 27 Jun 2023 12:53:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687895598; x=1690487598; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=3Id5qH++otR8DcNfddDdY5YXdM3OYIMoGcTKZICpQL0=; b=EU1x4V9NVtPxIUgk650J4V+dESu7aRiKD0i4WFKnEosniKDZq0E6oo3zXdgDXk9yIM 9sWXE4iwmz3TT32EXpwLxnoskegTwyJQQksH20Dg9kTz9obX345m0NHTnebx6nl3P9rB 79A65yfBdoZZdEklpWRbfFN4Ui2Llyt3gJfp9u/fs+h0RIJHeefgUtcdN5eJpfF8Ykei KRoLnwpnLr/jFd40z9W6CF1KnOu+6QzjM7VSezwjilXuMRR+rB0mzRVkJgSj0KYBoNDJ ITxKw3HqZzd3VqvBktqIqJd+Jthc3RpYPry9z1v31LTsls0JM2Ph7i8IC+7eDUoE/A9I QsVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687895598; x=1690487598; 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=3Id5qH++otR8DcNfddDdY5YXdM3OYIMoGcTKZICpQL0=; b=JyLdonR7Uo7bIgKCLt0uKxeGPSS1OHaAFQ7BJo9spdYE3HUSHkF2FcFwFK7UHj1+p4 bSiH/EdU++6reCp4+684hMPxCN8HQo5cHldidVWNhS6JUMSD7U4J67PiPYY31XU8wnPX LmZRKBSWeCm/pcXBkpGAbo0aO9uSsCYDLntb2exIYEm0+gfn3VUocjIdS64NuQe8zcKR WSPOnWu0o8KG02fegUBiqYeET9J4zHk08tafPL79Hdn4FmaTiXphuL9ABLaJEuXTsnEl ub/Dwzrbv/CE4+Hxpz/BwSFmrN0ZlJG5L9vYQpUpyMXplw0DVN9XMbUZEDOSGHBM2VnZ scGA== X-Gm-Message-State: AC+VfDz+xotJOzHW++FMNNUroxgevZXMFQm949OnlcQQwI5484RyZ1/C zGmytMG2AZnToX3+pXbqGtOu5JualoxAQEbqAqbordbJYC+XkDfD+bGm0XLRLvZCgEaxyNnXGn8 HYzb4Q1PFPIFPIiwSjWJHlu+1pgoc/QiiTG5xLv9c0SP2+y8N0zgBRZ2MRV5UfBp1BA== X-Google-Smtp-Source: ACHHUZ62v9qWA5hRdqqjTFt2DjR84RiGSZDxZki9Txo30xK0tt/L0ETQmEKOnN1M6jKzwMSB7texF/Dl5YORFGs= X-Received: from barleywine.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3bd4]) (user=calvinwan job=sendgmr) by 2002:a17:902:f111:b0:1b7:f077:c8e7 with SMTP id e17-20020a170902f11100b001b7f077c8e7mr1528975plb.11.1687895598473; Tue, 27 Jun 2023 12:53:18 -0700 (PDT) Date: Tue, 27 Jun 2023 19:52:51 +0000 In-Reply-To: <20230627195251.1973421-1-calvinwan@google.com> Mime-Version: 1.0 References: <20230627195251.1973421-1-calvinwan@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230627195251.1973421-9-calvinwan@google.com> Subject: [RFC PATCH 8/8] git-std-lib: add test file to call git-std-lib.a functions From: Calvin Wan To: git@vger.kernel.org Cc: Calvin Wan , nasamuffin@google.com, chooglen@google.com, johnathantanmy@google.com Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add test file that directly or indirectly calls all functions defined in git-std-lib.a object files to showcase that they do not reference missing objects and that git-std-lib.a can stand on its own. Certain functions that cause the program to exit or are already called by other functions are commented out. TODO: replace with unit tests Signed-off-by: Calvin Wan --- t/Makefile | 4 + t/stdlib-test.c | 239 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 243 insertions(+) create mode 100644 t/stdlib-test.c diff --git a/t/Makefile b/t/Makefile index 3e00cdd801..b6d0bc9daa 100644 --- a/t/Makefile +++ b/t/Makefile @@ -150,3 +150,7 @@ perf: .PHONY: pre-clean $(T) aggregate-results clean valgrind perf \ check-chainlint clean-chainlint test-chainlint + +test-git-std-lib: + cc -It -o stdlib-test stdlib-test.c -L. -l:../git-std-lib.a + ./stdlib-test diff --git a/t/stdlib-test.c b/t/stdlib-test.c new file mode 100644 index 0000000000..0e4f6d5807 --- /dev/null +++ b/t/stdlib-test.c @@ -0,0 +1,239 @@ +#include "../git-compat-util.h" +#include "../abspath.h" +#include "../hex-ll.h" +#include "../parse.h" +#include "../strbuf.h" +#include "../string-list.h" + +/* + * Calls all functions from git-std-lib + * Some inline/trivial functions are skipped + */ + +void abspath_funcs(void) { + struct strbuf sb = STRBUF_INIT; + + fprintf(stderr, "calling abspath functions\n"); + is_directory("foo"); + strbuf_realpath(&sb, "foo", 0); + strbuf_realpath_forgiving(&sb, "foo", 0); + real_pathdup("foo", 0); + absolute_path("foo"); + absolute_pathdup("foo"); + prefix_filename("foo/", "bar"); + prefix_filename_except_for_dash("foo/", "bar"); + is_absolute_path("foo"); + strbuf_add_absolute_path(&sb, "foo"); + strbuf_add_real_path(&sb, "foo"); +} + +void hex_ll_funcs(void) { + unsigned char c; + + fprintf(stderr, "calling hex-ll functions\n"); + + hexval('c'); + hex2chr("A1"); + hex_to_bytes(&c, "A1", 2); +} + +void parse_funcs(void) { + intmax_t foo; + ssize_t foo1 = -1; + unsigned long foo2; + int foo3; + int64_t foo4; + + fprintf(stderr, "calling parse functions\n"); + + git_parse_signed("42", &foo, maximum_signed_value_of_type(int)); + git_parse_ssize_t("42", &foo1); + git_parse_ulong("42", &foo2); + git_parse_int("42", &foo3); + git_parse_int64("42", &foo4); + git_parse_maybe_bool("foo"); + git_parse_maybe_bool_text("foo"); + git_env_bool("foo", 1); + git_env_ulong("foo", 1); +} + +static int allow_unencoded_fn(char ch) { + return 0; +} + +void strbuf_funcs(void) { + struct strbuf *sb = xmalloc(sizeof(void*)); + struct strbuf *sb2 = xmalloc(sizeof(void*)); + struct strbuf sb3 = STRBUF_INIT; + struct string_list list = STRING_LIST_INIT_NODUP; + char *buf = "foo"; + struct strbuf_expand_dict_entry dict[] = { + { "foo", NULL, }, + { "bar", NULL, }, + }; + int fd = open("/dev/null", O_RDONLY); + + fprintf(stderr, "calling strbuf functions\n"); + + starts_with("foo", "bar"); + istarts_with("foo", "bar"); + // skip_to_optional_arg_default(const char *str, const char *prefix, + // const char **arg, const char *def) + strbuf_init(sb, 0); + strbuf_init(sb2, 0); + strbuf_release(sb); + strbuf_attach(sb, strbuf_detach(sb, NULL), 0, 0); // calls strbuf_grow + strbuf_swap(sb, sb2); + strbuf_setlen(sb, 0); + strbuf_trim(sb); // calls strbuf_rtrim, strbuf_ltrim + // strbuf_rtrim() called by strbuf_trim() + // strbuf_ltrim() called by strbuf_trim() + strbuf_trim_trailing_dir_sep(sb); + strbuf_trim_trailing_newline(sb); + strbuf_reencode(sb, "foo", "bar"); + strbuf_tolower(sb); + strbuf_add_separated_string_list(sb, " ", &list); + strbuf_list_free(strbuf_split_buf("foo bar", 8, ' ', -1)); + strbuf_cmp(sb, sb2); + strbuf_addch(sb, 1); + strbuf_splice(sb, 0, 1, "foo", 3); + strbuf_insert(sb, 0, "foo", 3); + // strbuf_vinsertf() called by strbuf_insertf + strbuf_insertf(sb, 0, "%s", "foo"); + strbuf_remove(sb, 0, 1); + strbuf_add(sb, "foo", 3); + strbuf_addbuf(sb, sb2); + strbuf_join_argv(sb, 0, NULL, ' '); + strbuf_addchars(sb, 1, 1); + strbuf_addf(sb, "%s", "foo"); + strbuf_add_commented_lines(sb, "foo", 3, '#'); + strbuf_commented_addf(sb, '#', "%s", "foo"); + // strbuf_vaddf() called by strbuf_addf() + strbuf_expand(sb, "%s", strbuf_expand_literal_cb, NULL); + strbuf_expand(sb, "%s", strbuf_expand_dict_cb, &dict); + // strbuf_expand_literal_cb() called by strbuf_expand() + // strbuf_expand_dict_cb() called by strbuf_expand() + strbuf_addbuf_percentquote(sb, &sb3); + strbuf_add_percentencode(sb, "foo", STRBUF_ENCODE_SLASH); + strbuf_fread(sb, 0, stdin); + strbuf_read(sb, fd, 0); + strbuf_read_once(sb, fd, 0); + strbuf_write(sb, stderr); + strbuf_readlink(sb, "/dev/null", 0); + strbuf_getcwd(sb); + strbuf_getwholeline(sb, stderr, '\n'); + strbuf_appendwholeline(sb, stderr, '\n'); + strbuf_getline(sb, stderr); + strbuf_getline_lf(sb, stderr); + strbuf_getline_nul(sb, stderr); + strbuf_getwholeline_fd(sb, fd, '\n'); + strbuf_read_file(sb, "/dev/null", 0); + strbuf_add_lines(sb, "foo", "bar", 0); + strbuf_addstr_xml_quoted(sb, "foo"); + strbuf_addstr_urlencode(sb, "foo", allow_unencoded_fn); + strbuf_humanise_bytes(sb, 42); + strbuf_humanise_rate(sb, 42); + printf_ln("%s", sb); + fprintf_ln(stderr, "%s", sb); + xstrdup_tolower("foo"); + xstrdup_toupper("foo"); + // xstrvfmt() called by xstrfmt() + xstrfmt("%s", "foo"); + // strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm, + // int tz_offset, int suppress_tz_name) + // strbuf_stripspace(struct strbuf *sb, char comment_line_char) + // strbuf_strip_suffix(struct strbuf *sb, const char *suffix) + // strbuf_strip_file_from_path(struct strbuf *sb) +} + +static void error_builtin(const char *err, va_list params) {} +static void warn_builtin(const char *err, va_list params) {} + +static report_fn error_routine = error_builtin; +static report_fn warn_routine = warn_builtin; + +void usage_funcs(void) { + fprintf(stderr, "calling usage functions\n"); + // Functions that call exit() are commented out + + // usage() + // usagef() + // die() + // die_errno(); + error("foo"); + error_errno("foo"); + die_message("foo"); + die_message_errno("foo"); + warning("foo"); + warning_errno("foo"); + + // set_die_routine(); + get_die_message_routine(); + set_error_routine(error_builtin); + get_error_routine(); + set_warn_routine(warn_builtin); + get_warn_routine(); + // set_die_is_recursing_routine(); +} + +void wrapper_funcs(void) { + void *ptr = xmalloc(1); + int fd = open("/dev/null", O_RDONLY); + struct strbuf sb = STRBUF_INIT; + int mode = 0444; + char host[PATH_MAX], path[PATH_MAX], path1[PATH_MAX]; + xsnprintf(path, sizeof(path), "out-XXXXXX"); + xsnprintf(path1, sizeof(path1), "out-XXXXXX"); + int tmp; + + fprintf(stderr, "calling wrapper functions\n"); + + xstrdup("foo"); + xmalloc(1); + xmallocz(1); + xmallocz_gently(1); + xmemdupz("foo", 3); + xstrndup("foo", 3); + xrealloc(ptr, 2); + xcalloc(1, 1); + xsetenv("foo", "bar", 0); + xopen("/dev/null", O_RDONLY); + xread(fd, &sb, 1); + xwrite(fd, &sb, 1); + xpread(fd, &sb, 1, 0); + xdup(fd); + xfopen("/dev/null", "r"); + xfdopen(fd, "r"); + tmp = xmkstemp(path); + close(tmp); + unlink(path); + tmp = xmkstemp_mode(path1, mode); + close(tmp); + unlink(path1); + xgetcwd(); + fopen_for_writing(path); + fopen_or_warn(path, "r"); + xstrncmpz("foo", "bar", 3); + // xsnprintf() called above + xgethostname(host, 3); + tmp = git_mkstemps_mode(path, 1, mode); + close(tmp); + unlink(path); + tmp = git_mkstemp_mode(path, mode); + close(tmp); + unlink(path); + read_in_full(fd, &sb, 1); + write_in_full(fd, &sb, 1); + pread_in_full(fd, &sb, 1, 0); +} + +int main() { + abspath_funcs(); + hex_ll_funcs(); + parse_funcs(); + strbuf_funcs(); + usage_funcs(); + wrapper_funcs(); + fprintf(stderr, "all git-std-lib functions finished calling\n"); + return 0; +} \ No newline at end of file