From patchwork Fri Sep 29 21:20:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Tan X-Patchwork-Id: 13404849 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 C0593E743F0 for ; Fri, 29 Sep 2023 21:21:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233828AbjI2VVJ (ORCPT ); Fri, 29 Sep 2023 17:21:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34730 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229545AbjI2VVD (ORCPT ); Fri, 29 Sep 2023 17:21:03 -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 BE6B71AA for ; Fri, 29 Sep 2023 14:21:00 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-59f8134eb83so187077437b3.2 for ; Fri, 29 Sep 2023 14:21:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696022460; x=1696627260; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=dSqK0HweFHjWdsnPW0bdmckN6lsXfMyrTavCbIKCkBw=; b=JImwSZB/VpRWn/i60mZfn8uF8mq56/bCDH+0qBKqcJjPa4I/XsZsDiKkBWVZ+bSrIf hvJlT05jDzV0/H1lEoNisgUZCtkm9u723HQwu49boJY6plrWurdwL54Ud3NL4QhTmeMG CLfisUylr6A1MKl9vPXm5L6QQ/MlSXFOgH/brqLoAi9iEhD36ymZwrSBbBoLeoQg22vs elqt32WWoOiUSfnsSl1wpa5NTUyfGvGGYnRRjfkpzk66gEel8lNojaOdavSDfXF/G3lT JoBWHOhAd1DjTkqLpp4D7p3GF/KvzsEkUW5WkLolwgp9yq72wKkBNLph7JJJfjHVsBdE LZcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696022460; x=1696627260; 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=dSqK0HweFHjWdsnPW0bdmckN6lsXfMyrTavCbIKCkBw=; b=EKA1vQEAIzpP77nUltzgc87JQfNv4/4pkCR5qB19ZSyd7ynp0NfJtzoh9QUTs9DRma 8kUn/wg8tittfb23DXY2xzsDW2AsVOpv940mvZW3W+nuoDyw6NJVSh+L4nYiUSMBV3GE UFVNiAUC2YItQHHw14LjiQAbu2gW4ahK9mBZukSGlii1nNjnT9KBz81sxarlgCOptIwR 1U2mzwpw+sfmNwBo0boo4iyEwPzZ4on1SkYick1iZNPrPLSqfRwdjPmYy/QIjWuENhGR Waisrtu1LayUYMyoOKtr5wynGoLVULfbMK7aPjXVGsizt6lxhWxzMbdjJB3jlc1uh2J2 bpQA== X-Gm-Message-State: AOJu0Yy8wGePdoxXHdjk6JXpKd+GGPI8IaBI3kyDjjHwrfXsDnimlZ8A 3it/Pe09TbirnQQVRvFIcPEB+Oj10MSdCANndDfe2I/i05q+5ZDZ2U4aa2v/evJylovz5DN3aF4 oh1WD1r4skI3O7ekOu5x+QNK7uTUq32zjlhMJoZROuBKKIPa8F+NdQ8u4bUa7fyxdW3m4RcqEqO qk X-Google-Smtp-Source: AGHT+IEbJV1VezkcZub8ipGi5MENaibZrUlZhuna/fYB+cTIc6A1+r4W35RSPaU8RvlLu6jrvUNZdEQn+wVoUR6J/876 X-Received: from jonathantanmy0.svl.corp.google.com ([2620:15c:2d3:204:b708:8deb:d032:67cf]) (user=jonathantanmy job=sendgmr) by 2002:a25:5042:0:b0:d89:47d6:b4fa with SMTP id e63-20020a255042000000b00d8947d6b4famr79683ybb.13.1696022459473; Fri, 29 Sep 2023 14:20:59 -0700 (PDT) Date: Fri, 29 Sep 2023 14:20:48 -0700 In-Reply-To: Mime-Version: 1.0 References: <20230627195251.1973421-1-calvinwan@google.com> X-Mailer: git-send-email 2.42.0.582.g8ccd20d70d-goog Message-ID: <02ecc00e9c7226c9eeb960cc49c8c03dcb182a38.1696021277.git.jonathantanmy@google.com> Subject: [PATCH v4 1/4] hex-ll: separate out non-hash-algo functions From: Jonathan Tan To: git@vger.kernel.org Cc: Calvin Wan , phillip.wood123@gmail.com, Junio C Hamano , Jonathan Tan Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Calvin Wan In order to further reduce all-in-one headers, separate out functions in hex.h that do not operate on object hashes into its own file, hex-ll.h, and update the include directives in the .c files that need only such functions accordingly. Signed-off-by: Calvin Wan Signed-off-by: Jonathan Tan --- 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 5776309365..861e643708 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 b24b19566b..f663c06ac4 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 01f17fe5c9..d42262bdca 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 87abf66602..e0b83f776f 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 hash (specified by the_hash_algo) in hexadecimal @@ -34,13 +19,6 @@ int get_oid_hex(const char *hex, struct object_id *oid); /* 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 931505363c..a07d2da16d 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 4c9ac6dc5e..7827178d8e 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 1c45f23adf..1d0254abac 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 Fri Sep 29 21:20:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Tan X-Patchwork-Id: 13404850 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 8AC39E71D58 for ; Fri, 29 Sep 2023 21:21:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233749AbjI2VVK (ORCPT ); Fri, 29 Sep 2023 17:21:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233540AbjI2VVE (ORCPT ); Fri, 29 Sep 2023 17:21:04 -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 6C5A41AB for ; Fri, 29 Sep 2023 14:21:02 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a1f12cf1ddso22808497b3.0 for ; Fri, 29 Sep 2023 14:21:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696022461; x=1696627261; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=6rwYe4h6Z9Jg1gIRfLKp95ZvmJDBSMmUEcRxwVtc2KY=; b=Ojk7u9a3CjOuoa3HaRwB/28J27V0bYvjLKHh7AFHPvlOx6EaMLWEokU75LJEmDKXDq gDa8cIfz65vx0MdMjmcnLWgXVN86cr9lq3sHb33lcsCJwCBiP2OALB2F/V2ernNJsk4h PljOE36kqHobwHGoAe/91izrtbr0XW5a9yGsY00SAvcULtS6meF5pvROJzhiWttgheAf yQMBqeYbmgs4XydVy7Ew+XGdWYMAd11cXulCGdkF3xECXSITWbEmEcKlzV6n7hP6BYFj NFJ+VBP6zO/auy35+yd0KnLbTaT16w9xbmHnhXhqq+YoaEYYFmoy59KFzrOQRYeAri5X kYpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696022461; x=1696627261; 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=6rwYe4h6Z9Jg1gIRfLKp95ZvmJDBSMmUEcRxwVtc2KY=; b=Nl/bx6kQGL4yrUiC6v+2hWy5YRRa9m7xlqTHxRSIJxjZw/62lR4/6GobSPLCmSyDq3 J4VcMD19Zpmsym7FJ7SlsoaM6fKAkj4uasDOpz7IUKCnbMeJXOK/FtzxWp/3poIiiM7z 0/jr/88LH6CYfsTTtjaoNdjMg5bXA/3z9LsAO8VWbEw/9LGRB9sA2AnPx4l1aESdslXm mQ8BR/k0YISbGUbMxd4GlSteKTzaaBNMp85bkCrpQRkk2wj6h9Og3QPAMUw/D9mLz+sh D//VjH/ZZAeDJyr7l0I8LTx2uDC4w7W6qgktetTclv0JYofT7bmQTcBln4vzQDXt0m1j cZDA== X-Gm-Message-State: AOJu0YxOOpNnNj6LjWpTQJ+SNJ4LMe6BhCLhI0fIuTbawmiAwMH6g6C3 HeQxZfA5DKw36IcBt/oGD/EVz0TQS2BLFOMPfH1wfPsZ+WobNUAvS1QBjhKxEQmVeDLsVexiBXq CaVG0AtwSbL39MttM5IqEV5Etj3S2IpoErYF9vKt5D0RQr5c56zO/1T5NI2bK5tqstuA2Nqq6yB no X-Google-Smtp-Source: AGHT+IFghsh+N2dPVLjrHT65YwSaScv++Fp0CI9NtgjpMf/3nFFuE+rET8ADOKxaFzmp9ZyU7FLTUmOkYydTv3E7W8cq X-Received: from jonathantanmy0.svl.corp.google.com ([2620:15c:2d3:204:b708:8deb:d032:67cf]) (user=jonathantanmy job=sendgmr) by 2002:a81:9982:0:b0:59f:4c14:ac5e with SMTP id q124-20020a819982000000b0059f4c14ac5emr120874ywg.2.1696022461415; Fri, 29 Sep 2023 14:21:01 -0700 (PDT) Date: Fri, 29 Sep 2023 14:20:49 -0700 In-Reply-To: Mime-Version: 1.0 References: <20230627195251.1973421-1-calvinwan@google.com> X-Mailer: git-send-email 2.42.0.582.g8ccd20d70d-goog Message-ID: Subject: [PATCH v4 2/4] wrapper: reduce scope of remove_or_warn() From: Jonathan Tan To: git@vger.kernel.org Cc: Calvin Wan , phillip.wood123@gmail.com, Junio C Hamano , Jonathan Tan Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Calvin Wan remove_or_warn() is only used by entry.c and apply.c, but it is currently declared and defined in wrapper.{h,c}, so it has a scope much greater than it needs. This needlessly large scope also causes wrapper.c to need to include object.h, when this file is largely unconcerned with Git objects. Move remove_or_warn() to entry.{h,c}. The file apply.c still has access to it, since it already includes entry.h for another reason. Signed-off-by: Calvin Wan Signed-off-by: Jonathan Tan --- entry.c | 5 +++++ entry.h | 6 ++++++ wrapper.c | 6 ------ wrapper.h | 5 ----- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/entry.c b/entry.c index 43767f9043..076e97eb89 100644 --- a/entry.c +++ b/entry.c @@ -581,3 +581,8 @@ void unlink_entry(const struct cache_entry *ce, const char *super_prefix) return; schedule_dir_for_removal(ce->name, ce_namelen(ce)); } + +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/entry.h b/entry.h index 7329f918a9..ca3ed35bc0 100644 --- a/entry.h +++ b/entry.h @@ -62,4 +62,10 @@ int fstat_checkout_output(int fd, const struct checkout *state, struct stat *st) void update_ce_after_write(const struct checkout *state, struct cache_entry *ce, struct stat *st); +/* + * 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 /* ENTRY_H */ diff --git a/wrapper.c b/wrapper.c index 48065c4f53..453a20ed99 100644 --- a/wrapper.c +++ b/wrapper.c @@ -5,7 +5,6 @@ #include "abspath.h" #include "config.h" #include "gettext.h" -#include "object.h" #include "repository.h" #include "strbuf.h" #include "trace2.h" @@ -632,11 +631,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 79c7321bb3..1b2b047ea0 100644 --- a/wrapper.h +++ b/wrapper.h @@ -106,11 +106,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 Fri Sep 29 21:20:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Tan X-Patchwork-Id: 13404851 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 05E81E743FF for ; Fri, 29 Sep 2023 21:21:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233838AbjI2VVM (ORCPT ); Fri, 29 Sep 2023 17:21:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233602AbjI2VVF (ORCPT ); Fri, 29 Sep 2023 17:21:05 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 158B21AE for ; Fri, 29 Sep 2023 14:21:04 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d86766bba9fso18340722276.1 for ; Fri, 29 Sep 2023 14:21:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696022463; x=1696627263; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=kckG2mDj4W61beRJrtNjk81pTQaRrpVtXZrSgI57tD0=; b=aOrx9q+v4LLjyoK04/tQ2O2wNxCfG3i/UyMoMpB0trAGrLDraL8PXvSvw2EY2IZiQN CuIdofBWUv7nd6nTM8c30RO5j55pJFS7ICXMeKJkepkI/g3Y9sPqPvNvIq2nvQ7xN7VI 4VfSxfjT+q9nxYqVbWaI4scYSL61h401QBcZJF702JW+9GohkhBjsf3lVNd19gIs3gJ1 pLEszAVrSr4Kk+D4MaEqsRV8loQ8r2F3Afz8KTaqF8CIR1kT6KKb5F65h1dJe91NAdhB oYuWc8nCUdVPLu8YgtHXZ6ZVJas75ZWdgU3g01/e48b3qJ0BFxtxVjO9V8hoSK10vOvK SiUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696022463; x=1696627263; 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=kckG2mDj4W61beRJrtNjk81pTQaRrpVtXZrSgI57tD0=; b=hwDz3mIkvp01au5SnjrWeFoLFWFVk+GT0XcJpaI7MP54M9hnMws97LYy3X1xOebn3v c/1sdyUzXZIagFzxziPUOUVI76V6Xal5DQnQMel11B8v2wNiaSF+0P4nVruCG/5nRD+n uwI6G2QDqBAefGCO2PQSk01CWMKSUjcVsLf8JfpUT07uFLzQBTdhXwm+8p2K2vKVzfZh gKHXXcrMnNk4HOXO1UyzlqitwmkT4VAQ0mbmOyL2TgmFAzx+P/7yJFxUOY1g0UpqjnR/ W2seV2A2wO6RY9/gEG6Pl/1gQG4k+6ffIs8/hPS1jGxSbgHTbkgyo+1ltywPcQzPRpwB O0JQ== X-Gm-Message-State: AOJu0Yw06Yz1MIoR8UMyCMwIuKKMAmvgQ7J5fB4fSsX35kAF1EzkMqGp C3looeMQxmdKf8KAAoS7h8CGk/2yKdhKlEdOZ/jZUliDG0AGtl+z0KDiKA0YTxjy/npMHb8np0J tH66hiugpEFKUBE5lin+VxK3S2N1C7LzEZLbNb5wBNwQJ9Y7lX5yHEEDNl0eOn71U+QpVvGQu+F eA X-Google-Smtp-Source: AGHT+IF7lVWJwyUXsNqxAovppY3HdVfg5vQ05xDN7ON4srTYW5e9mpUSicUeWL9PiclG6CIjG0ZfukGioWCJmBbjckQa X-Received: from jonathantanmy0.svl.corp.google.com ([2620:15c:2d3:204:b708:8deb:d032:67cf]) (user=jonathantanmy job=sendgmr) by 2002:a25:dd8:0:b0:d86:5500:7dba with SMTP id 207-20020a250dd8000000b00d8655007dbamr83203ybn.13.1696022463250; Fri, 29 Sep 2023 14:21:03 -0700 (PDT) Date: Fri, 29 Sep 2023 14:20:50 -0700 In-Reply-To: Mime-Version: 1.0 References: <20230627195251.1973421-1-calvinwan@google.com> X-Mailer: git-send-email 2.42.0.582.g8ccd20d70d-goog Message-ID: Subject: [PATCH v4 3/4] config: correct bad boolean env value error message From: Jonathan Tan To: git@vger.kernel.org Cc: Calvin Wan , phillip.wood123@gmail.com, Junio C Hamano , Jonathan Tan Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Calvin Wan 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 Signed-off-by: Junio C Hamano --- config.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/config.c b/config.c index 3846a37be9..7dde0aaa02 100644 --- a/config.c +++ b/config.c @@ -2133,7 +2133,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 Fri Sep 29 21:20:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Tan X-Patchwork-Id: 13404852 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 48B72E71D58 for ; Fri, 29 Sep 2023 21:21:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233812AbjI2VVN (ORCPT ); Fri, 29 Sep 2023 17:21:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54070 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233824AbjI2VVJ (ORCPT ); Fri, 29 Sep 2023 17:21:09 -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 0D7BE1B2 for ; Fri, 29 Sep 2023 14:21:06 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a23ad271d7so18047867b3.1 for ; Fri, 29 Sep 2023 14:21:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696022465; x=1696627265; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=mEffPqyvowslcO6VHdJaUVRI4J1WS8XMbMC+rLlH2eM=; b=niF865BphCe5DL9WBdtCJb+Q/WTYxGsdKoftWJSMpefxpXvbWKEBk9N5nRVK49IOKX nOaZAYP33ZMOs3tmq9tKAcS3pRTAoVFITI0hapKeo4Wa2RX67MJlyQJR2I7CQKyWO6gI pBRcgxBtCtUmPqAGO6w/wOPRgCIYkZbGM7dhpdxCw3799QGf9Turcv2/RzVcQRX/wTT/ vuLrmTYw5adjwojaWtYGhf1FvgV20uaXEA2/Q/IjrhojQOSrvgFBM1WPXPGEoE56urSH ojQJwvGavOJ1Vs9R6bJpgCSftTvJLuVIZYp2IwIhEcS6aoAce2C5I45QR8MjPTecq8QS Rcfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696022465; x=1696627265; 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=mEffPqyvowslcO6VHdJaUVRI4J1WS8XMbMC+rLlH2eM=; b=IRJpGTX/hxHvJvWP8X4MQjIBwwVAOdlTP5T6YmhxqOMF4ts3Vf1lLFGtBPbiRcAjmc OTWJlHa8KkvQiixvWgB6z/KI6kKAX4cmW3L/7seAXJ/zxEqDdXaqo8E6D1gkwjaMz3by UF1mzJmpdBXdnlKjqgg4o/ERAMXoSKUA8twS7lnysLl0GiHqSMd/O6Dh+aj2iWRymHdy ZUCteGmA58HS4veh6mKd9YUhhBZGH81k2iywsZI93gvu+wjg6j3Cu+f0bMjPswfW6vPX KSTNkZejGotnFfqVM0+/twHggZfYdVKRcOoGZwjACELIxLSGfyyYF+Yi8L8kNIBX2mOS xHFA== X-Gm-Message-State: AOJu0Yy8kOgn6TjnkjZhSWZb1404FsvW4ZswIevexPa23wiGJgBCxAux T42JTLJ4TZ5tf/UAsKBHY2Bt+oDJwKwtZWUGP1BgVlNB11szJVOEtAu49EHPve9NRZ2JSZflFJn UHd2oDnobixZO4K2WxTp/TdZmSIBQmHMs0CdgAViQRlnAtHVbVUGkZoJUFcOcpfcywlGQbPkB9G rz X-Google-Smtp-Source: AGHT+IFDjdhKF8G67vh5fmxqFfyNlIHFkasQ+WvIlAuN+eRUFC7P2261anFJhVeR4fRYDsEy5jdaLw75nYQ7AI6MnMLm X-Received: from jonathantanmy0.svl.corp.google.com ([2620:15c:2d3:204:b708:8deb:d032:67cf]) (user=jonathantanmy job=sendgmr) by 2002:a81:4004:0:b0:595:5cf0:a9b0 with SMTP id l4-20020a814004000000b005955cf0a9b0mr88456ywn.9.1696022465205; Fri, 29 Sep 2023 14:21:05 -0700 (PDT) Date: Fri, 29 Sep 2023 14:20:51 -0700 In-Reply-To: Mime-Version: 1.0 References: <20230627195251.1973421-1-calvinwan@google.com> X-Mailer: git-send-email 2.42.0.582.g8ccd20d70d-goog Message-ID: <5d9f0b3de08ab8541482b9b640db06b6d3000b86.1696021277.git.jonathantanmy@google.com> Subject: [PATCH v4 4/4] parse: separate out parsing functions from config.h From: Jonathan Tan To: git@vger.kernel.org Cc: Calvin Wan , phillip.wood123@gmail.com, Junio C Hamano , Jonathan Tan Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Calvin Wan The files config.{h,c} contain functions that have to do with parsing, but not config. In order to further reduce all-in-one headers, separate out functions in config.c that do not operate on config into its own file, parse.h, and update the include directives in the .c files that need only such functions accordingly. Signed-off-by: Calvin Wan Signed-off-by: Jonathan Tan --- 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 861e643708..9226c719a0 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 71c84fbcf8..3c0b4fb3d9 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 7dde0aaa02..c7bc21a25d 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" @@ -1165,129 +1166,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; -} - NORETURN static void die_bad_number(const char *name, const char *value, const struct key_value_info *kvi) @@ -1363,23 +1241,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; @@ -1454,16 +1315,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, const struct key_value_info *kvi, int *is_bool) { @@ -2126,35 +1977,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 6332d74904..14f881ecfa 100644 --- a/config.h +++ b/config.h @@ -4,7 +4,7 @@ #include "hashmap.h" #include "string-list.h" #include "repository.h" - +#include "parse.h" /** * The config API gives callers a way to access Git configuration files @@ -243,16 +243,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. @@ -385,8 +375,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 e8e076c3a6..093eaf2db8 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 3a3a5724c4..7f88f1c02b 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 453a20ed99..7da15a56da 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 "repository.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"