From patchwork Thu Sep 21 21:17:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 13394676 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 6BE0BE7D0AE for ; Thu, 21 Sep 2023 21:40:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229643AbjIUVkW (ORCPT ); Thu, 21 Sep 2023 17:40:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232905AbjIUVjz (ORCPT ); Thu, 21 Sep 2023 17:39:55 -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 E2C84659C for ; Thu, 21 Sep 2023 14:17:29 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d81503de9c9so1830928276.3 for ; Thu, 21 Sep 2023 14:17:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695331048; x=1695935848; 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=xVuWW6EObRI/np9K8ufVTzo2sCqTpDUmXNFLbqi639U=; b=hrCDZ2I4Z/hcxO/rZy9oLRxYYj8mcfOmkqSiXA1+W6HVWGyMGwGW5jHrUYtGiWdMg1 g41DnohNDB7sg444FrBgU4NNRzcVI4MBToz8xNpG/fXNdS3i/BCC8g/7c5zolgVEBRhx 5UNmjlff9jJCU7j3QkfO8/6A/+XGy12Qqbncd53ObFE4+xwVnEMJcnoKuiByjFujkxUv 2H2ue+2NnNtm9yTpLc1Cw850Ldb1zpbOTOCwXZwfdulB5uJCwJdkDd6b5h1li5Sre3gm 3g1Prmpg7ZQQBPxwObc9zZy7ZlF8Je+Ay3lj0IR+W+A+/R171GyFShS/ytozt/+U6tJm 7ovg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695331048; x=1695935848; 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=xVuWW6EObRI/np9K8ufVTzo2sCqTpDUmXNFLbqi639U=; b=Rl//qXj028G8+49qWrR7BAXMQGrQ2X8abbdOl19otbaIGaFfaM8Q4lASFvKAhE9amR BWLgiR+To5GcG6snZqUM+4vpgaZeBtAwuYjRxVlhQBm1wP0Sr1qDa/cPjRnMn3GExP6t ywyRvp2NmFH0nF9S3F+35e/lULPXbtx5DN9iE83tU2tVIxBreurlNhBg47DQLS6+h9RH osA80kCLHQM9mdVP8YTshFX9kMNCxuccPepJn/qrNuB+RI6vsV1ej5XpqBPtQIVKFBYo u401GJ6sNtmdsXG2tgPhvy0uHInapsKtQ8JJwGKPq2UXngYvCnlUcWHnMOPZNKcne1V3 kZxg== X-Gm-Message-State: AOJu0YzRB7E7BtFtpAN/hPoHaSzHrSyaM/Sp/Hby37OXu1odS0vPos/q I2sKMd3AlKrkfe5wvJHTpxRO0NIpLYTc2EsYN5Jo22GyeCTnNR8fGYuxDksz+3QHG8n6mBi4ylT iAZnQUsI/4DgrXrhS0JtyGej/fcd6qkNJCgwmBko1YG60eGmFxmJNX9JSFwKElcE= X-Google-Smtp-Source: AGHT+IEqoheuVCbDmKUNBngwCGhYpNrlJKUJxn8N4qwsQ9a9mJ3u83xsFB34OJjEhPdQ1HT+SSN7ySlBaYkPoA== X-Received: from lunarfall.svl.corp.google.com ([2620:15c:2d3:204:828c:91e1:20c5:c8f]) (user=steadmon job=sendgmr) by 2002:a25:5053:0:b0:d7f:8e0a:4b3f with SMTP id e80-20020a255053000000b00d7f8e0a4b3fmr86900ybb.3.1695331048677; Thu, 21 Sep 2023 14:17:28 -0700 (PDT) Date: Thu, 21 Sep 2023 14:17:20 -0700 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.42.0.515.g380fc7ccd1-goog Message-ID: Subject: [PATCH v3 1/5] config: split out config_parse_options From: Josh Steadmon To: git@vger.kernel.org Cc: jonathantanmy@google.com, calvinwan@google.com, glencbz@gmail.com, gitster@pobox.com Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Glen Choo "struct config_options" is a disjoint set of options used by the config parser (e.g. event listeners) and options used by config_with_options() (e.g. to handle includes, choose which config files to parse). Split parser-only options into config_parse_options. Signed-off-by: Glen Choo Signed-off-by: Josh Steadmon --- bundle-uri.c | 2 +- config.c | 14 +++++++------- config.h | 37 ++++++++++++++++++++----------------- fsck.c | 2 +- 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/bundle-uri.c b/bundle-uri.c index 4b5c49b93d..f93ca6a486 100644 --- a/bundle-uri.c +++ b/bundle-uri.c @@ -237,7 +237,7 @@ int bundle_uri_parse_config_format(const char *uri, struct bundle_list *list) { int result; - struct config_options opts = { + struct config_parse_options opts = { .error_action = CONFIG_ERROR_ERROR, }; diff --git a/config.c b/config.c index 85c5f35132..1518f70fc2 100644 --- a/config.c +++ b/config.c @@ -982,7 +982,7 @@ static int get_base_var(struct config_source *cs, struct strbuf *name) struct parse_event_data { enum config_event_t previous_type; size_t previous_offset; - const struct config_options *opts; + const struct config_parse_options *opts; }; static int do_event(struct config_source *cs, enum config_event_t type, @@ -1030,7 +1030,7 @@ static void kvi_from_source(struct config_source *cs, static int git_parse_source(struct config_source *cs, config_fn_t fn, struct key_value_info *kvi, void *data, - const struct config_options *opts) + const struct config_parse_options *opts) { int comment = 0; size_t baselen = 0; @@ -1967,7 +1967,7 @@ int git_default_config(const char *var, const char *value, */ static int do_config_from(struct config_source *top, config_fn_t fn, void *data, enum config_scope scope, - const struct config_options *opts) + const struct config_parse_options *opts) { struct key_value_info kvi = KVI_INIT; int ret; @@ -1992,7 +1992,7 @@ static int do_config_from_file(config_fn_t fn, const enum config_origin_type origin_type, const char *name, const char *path, FILE *f, void *data, enum config_scope scope, - const struct config_options *opts) + const struct config_parse_options *opts) { struct config_source top = CONFIG_SOURCE_INIT; int ret; @@ -2021,7 +2021,7 @@ static int git_config_from_stdin(config_fn_t fn, void *data, int git_config_from_file_with_options(config_fn_t fn, const char *filename, void *data, enum config_scope scope, - const struct config_options *opts) + const struct config_parse_options *opts) { int ret = -1; FILE *f; @@ -2047,7 +2047,7 @@ int git_config_from_mem(config_fn_t fn, const enum config_origin_type origin_type, const char *name, const char *buf, size_t len, void *data, enum config_scope scope, - const struct config_options *opts) + const struct config_parse_options *opts) { struct config_source top = CONFIG_SOURCE_INIT; @@ -3380,7 +3380,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename, struct stat st; size_t copy_begin, copy_end; int i, new_line = 0; - struct config_options opts; + struct config_parse_options opts; if (!value_pattern) store.value_pattern = NULL; diff --git a/config.h b/config.h index 6332d74904..2537516446 100644 --- a/config.h +++ b/config.h @@ -85,6 +85,21 @@ typedef int (*config_parser_event_fn_t)(enum config_event_t type, struct config_source *cs, void *event_fn_data); +struct config_parse_options { + enum config_error_action { + CONFIG_ERROR_UNSET = 0, /* use source-specific default */ + CONFIG_ERROR_DIE, /* die() on error */ + CONFIG_ERROR_ERROR, /* error() on error, return -1 */ + CONFIG_ERROR_SILENT, /* return -1 */ + } error_action; + /* + * event_fn and event_fn_data are for internal use only. Handles events + * emitted by the config parser. + */ + config_parser_event_fn_t event_fn; + void *event_fn_data; +}; + struct config_options { unsigned int respect_includes : 1; unsigned int ignore_repo : 1; @@ -92,6 +107,9 @@ struct config_options { unsigned int ignore_cmdline : 1; unsigned int system_gently : 1; + const char *commondir; + const char *git_dir; + struct config_parse_options parse_options; /* * For internal use. Include all includeif.hasremoteurl paths without * checking if the repo has that remote URL, and when doing so, verify @@ -99,21 +117,6 @@ struct config_options { * themselves. */ unsigned int unconditional_remote_url : 1; - - const char *commondir; - const char *git_dir; - /* - * event_fn and event_fn_data are for internal use only. Handles events - * emitted by the config parser. - */ - config_parser_event_fn_t event_fn; - void *event_fn_data; - enum config_error_action { - CONFIG_ERROR_UNSET = 0, /* use source-specific default */ - CONFIG_ERROR_DIE, /* die() on error */ - CONFIG_ERROR_ERROR, /* error() on error, return -1 */ - CONFIG_ERROR_SILENT, /* return -1 */ - } error_action; }; /* Config source metadata for a given config key-value pair */ @@ -178,13 +181,13 @@ int git_config_from_file(config_fn_t fn, const char *, void *); int git_config_from_file_with_options(config_fn_t fn, const char *, void *, enum config_scope, - const struct config_options *); + const struct config_parse_options *); int git_config_from_mem(config_fn_t fn, const enum config_origin_type, const char *name, const char *buf, size_t len, void *data, enum config_scope scope, - const struct config_options *opts); + const struct config_parse_options *opts); int git_config_from_blob_oid(config_fn_t fn, const char *name, struct repository *repo, const struct object_id *oid, void *data, diff --git a/fsck.c b/fsck.c index 3be86616c5..522ee1c18a 100644 --- a/fsck.c +++ b/fsck.c @@ -1219,7 +1219,7 @@ static int fsck_blob(const struct object_id *oid, const char *buf, return 0; if (oidset_contains(&options->gitmodules_found, oid)) { - struct config_options config_opts = { 0 }; + struct config_parse_options config_opts = { 0 }; struct fsck_gitmodules_data data; oidset_insert(&options->gitmodules_done, oid); From patchwork Thu Sep 21 21:17:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 13394677 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 81D3BE7D0AE for ; Thu, 21 Sep 2023 21:40:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229826AbjIUVk0 (ORCPT ); Thu, 21 Sep 2023 17:40:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232895AbjIUVjy (ORCPT ); Thu, 21 Sep 2023 17:39:54 -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 112461710 for ; Thu, 21 Sep 2023 14:17:32 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-59e8ebc0376so19560867b3.2 for ; Thu, 21 Sep 2023 14:17:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695331051; x=1695935851; 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=6aHOle8N+he/kHuUZwZ5XD2ABLllxD2ea+pqnpp9t2c=; b=qkKPtMqGFJeP87Cpdl+yEmZdKgn7jVTXk3wLHSAAKJNH1GFo0pIiCHZEbQS3yJ0ObU 4CdAs7aHpSlTje73xXheUGxUSXXdykk733RtZlqui74Fjw8uJjETRoci7UWK099Z2eHi Q8TeRi0H2fvElB+95+R586gHfTSYBj2lU3u0qc4i1gJsJHdT2vAJvDyzHOZE/L/ZJrqd nzhrnUS1rgqrA37xa4AC66mFPlNzWJFssUCMGSmEambzAmDuTgqUGpUqT8+LTqBIl+Ve +UwUfDj9PNFw6a3wQ66pGwiZ75NPzHAocpwD5riU+PSt5VVxcjJNYrHggZBXD/3nUhFY zaYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695331051; x=1695935851; 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=6aHOle8N+he/kHuUZwZ5XD2ABLllxD2ea+pqnpp9t2c=; b=E9zzf4Xa6LMmIMsA+Mpl1FplrqDjDT+o3Hk+Oe/MD5RMIVQ9rbFPTLMSYLh3u3LbAw dgBGCejpWyf63IAI8C9Yy4TbG0qDOM1JJgcgNqwYzNeEFGgoPHHgR+V4O4xm8732o4VQ ZTn2+KgqK1/4dgNMGZLQP69syJVb3Hskt3pzVN42tRHTcC5iiLe38wZZeNq++SvU+F6N kJPe5P/0v7Tsy8t6CYAq8tImCXQW8Hfa9DWJmw+ZforAcO4qzDAUvYRHIE0cfiCyQsfy Z+w1mmIOpdwLeKBEfZZVd4hwWcPD51tpXFz7NqKkLw/ekn/H0OD5AxKOJCB7NWe0ndXW hpQQ== X-Gm-Message-State: AOJu0YzD1EAqZQKm1R3V5TeOPAnRFdZ+zw1F2jzO0CeVmOBSDCEp6GLk VJPdLNUKURDtA4lM4cwx/EHzymMfLbe92hBBt754J/1khse5tU2KlQl8NIfsnAGcdD6XRuSB7xT urmnrqWoyx7FXLuI6ft41l4VBavh9cwRdUI+18FSMVNLv2SNPKO0wz7EtThsi/Ac= X-Google-Smtp-Source: AGHT+IH1DJ90q+ylBjL9+WYagZFUDIy0Qq4Cnx4mZoD9Ipq/IY/LzAXKqvhkq4sXX21jY25n924dJozMSddP4g== X-Received: from lunarfall.svl.corp.google.com ([2620:15c:2d3:204:828c:91e1:20c5:c8f]) (user=steadmon job=sendgmr) by 2002:a81:a94a:0:b0:59b:ccba:1249 with SMTP id g71-20020a81a94a000000b0059bccba1249mr114618ywh.10.1695331051152; Thu, 21 Sep 2023 14:17:31 -0700 (PDT) Date: Thu, 21 Sep 2023 14:17:21 -0700 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.42.0.515.g380fc7ccd1-goog Message-ID: <8a1463c223497fca2fd3f11a54db5d7e52d1d08a.1695330852.git.steadmon@google.com> Subject: [PATCH v3 2/5] config: split do_event() into start and flush operations From: Josh Steadmon To: git@vger.kernel.org Cc: jonathantanmy@google.com, calvinwan@google.com, glencbz@gmail.com, gitster@pobox.com Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org When handling config-parsing events, the current do_event() handler is a bit confusing; calling it with a specific event type records the initial offset where the event occurred, and runs the supplied callback against the previous event (whose end offset is now known). Split this operation into "start_event" and "flush_event" functions. Then reimplement "do_event" (preserving the original behavior) using the newly split functions. In a later change, we can use these building blocks to also handle "immediate" events, where we want to run the callback without having to calculate an end offset for the event. Signed-off-by: Josh Steadmon --- config.c | 50 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/config.c b/config.c index 1518f70fc2..ff138500a2 100644 --- a/config.c +++ b/config.c @@ -985,19 +985,11 @@ struct parse_event_data { const struct config_parse_options *opts; }; -static int do_event(struct config_source *cs, enum config_event_t type, - struct parse_event_data *data) +static size_t get_corrected_offset(struct config_source *cs, + enum config_event_t type) { - size_t offset; - - if (!data->opts || !data->opts->event_fn) - return 0; - - if (type == CONFIG_EVENT_WHITESPACE && - data->previous_type == type) - return 0; + size_t offset = cs->do_ftell(cs); - offset = cs->do_ftell(cs); /* * At EOF, the parser always "inserts" an extra '\n', therefore * the end offset of the event is the current file position, otherwise @@ -1005,14 +997,44 @@ static int do_event(struct config_source *cs, enum config_event_t type, */ if (type != CONFIG_EVENT_EOF) offset--; + return offset; +} + +static void start_event(struct config_source *cs, enum config_event_t type, + struct parse_event_data *data) +{ + data->previous_type = type; + data->previous_offset = get_corrected_offset(cs, type); +} + +static int flush_event(struct config_source *cs, enum config_event_t type, + struct parse_event_data *data) +{ + if (!data->opts || !data->opts->event_fn) + return 0; + + if (type == CONFIG_EVENT_WHITESPACE && + data->previous_type == type) + return 0; if (data->previous_type != CONFIG_EVENT_EOF && data->opts->event_fn(data->previous_type, data->previous_offset, - offset, cs, data->opts->event_fn_data) < 0) + get_corrected_offset(cs, type), cs, + data->opts->event_fn_data) < 0) return -1; - data->previous_type = type; - data->previous_offset = offset; + return 1; +} + +static int do_event(struct config_source *cs, enum config_event_t type, + struct parse_event_data *data) +{ + int maybe_ret; + + if ((maybe_ret = flush_event(cs, type, data)) < 1) + return maybe_ret; + + start_event(cs, type, data); return 0; } From patchwork Thu Sep 21 21:17:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 13394680 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 567FBE7D0AB for ; Thu, 21 Sep 2023 21:41:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232307AbjIUVlQ (ORCPT ); Thu, 21 Sep 2023 17:41:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55024 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232321AbjIUVlA (ORCPT ); Thu, 21 Sep 2023 17:41:00 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0C8DA40047 for ; Thu, 21 Sep 2023 14:17:33 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-59e758d6236so20844317b3.1 for ; Thu, 21 Sep 2023 14:17:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695331053; x=1695935853; 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=HL2MfxBAE9fvcU/jCASbb6dWUCXfEdwvjgutSJQqP1k=; b=XL0cj24n86a3RXNPjw/4GSw32NI0shyEi9oKGPCXCtprAEg0HapMmvP/smDweTW+PS q0zpk1OyLTZtXHhQsDC+UGkGy0SfbriAfE5RjwTGbGnuy0DfKMl95uqYvNDV9m+134jy FVOzgg+ewHiN93x2de1riZ35o5fiuaTUAeCtFr3mBCJXEQyqpKKHN0JDGCG0WTiLzMRZ aVeyFcXWHDXmnyDUk/OXndfwKyLYSl70RQzQJ3oHVlISmt4vSP/tDoCwzmnJT++bO2Ut qc9KFfBfBnwd5aPfT3uvnBjhQAXJlNummAnQOX9LlgBLU8kPmhYe+ZE/sjoSJjKmZ+B+ UXaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695331053; x=1695935853; 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=HL2MfxBAE9fvcU/jCASbb6dWUCXfEdwvjgutSJQqP1k=; b=CMIOt0DlfS0PT6iFzpdE3eWWVZRuIf+CpNkh0XnXnUHfWXezz+Nfl0ihp/l5Pl1mk8 49JIU4ILVNwalZY61bvUHIqGg0foI7bkFB2rGGRSqnvLgd/o4J9Ja4IFelSZgyw6aKOp 9NlaaFvylxH1SqHAaHeU+GjZssixYBIYpkglKaWbg5GtsVfEivSLxEBdyvpD7vXY0n3Z tq9VDmaumSkoCfZAJZSdUycSw1KB7LNn71lJKWI4nvUVyze+w8MYHzvbJsNq22uYvHM+ rzv2cq0b2Ir/CowaqTwZCyEeHPOpxrwpZe+HbkBO5eJdSAQQZ0Sz3O9zIMKweB9T4/WI x9tg== X-Gm-Message-State: AOJu0Yzjl01O3WFcsaO8DAIBjxJbjAEG0gxzQqNnDZVh7zJGprLno6Hz f2R08AHvK1oj3aQLkAPiLT4M3NkFC4c7omqEu0RHnV3c3OiPYmRsIbKEsUSVoUALeOoKmhAEIpz EIHL8OWZSkcE6QjJJY/m0sn0Md3kekbdfSG+QOKG9/eK6X9lNdu0H+MlnHF7FSp0= X-Google-Smtp-Source: AGHT+IGBiCRn5sqaiuQQ5OJjTwt7mTjGnwpLifBGCJi0A9VfPK4fXjOwXVp9jHKzsIgueU3tHn6QC2J/3c79vQ== X-Received: from lunarfall.svl.corp.google.com ([2620:15c:2d3:204:828c:91e1:20c5:c8f]) (user=steadmon job=sendgmr) by 2002:a25:25c6:0:b0:d81:7617:a397 with SMTP id l189-20020a2525c6000000b00d817617a397mr93104ybl.9.1695331052865; Thu, 21 Sep 2023 14:17:32 -0700 (PDT) Date: Thu, 21 Sep 2023 14:17:22 -0700 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.42.0.515.g380fc7ccd1-goog Message-ID: Subject: [PATCH v3 3/5] config: report config parse errors using cb From: Josh Steadmon To: git@vger.kernel.org Cc: jonathantanmy@google.com, calvinwan@google.com, glencbz@gmail.com, gitster@pobox.com Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Glen Choo In a subsequent commit, config parsing will become its own library, and it's likely that the caller will want flexibility in handling errors (instead of being limited to the error handling we have in-tree). Move the Git-specific error handling into a config_parser_event_fn_t that responds to config errors, and make git_parse_source() always return -1 (careful inspection shows that it was always returning -1 already). This makes CONFIG_ERROR_SILENT obsolete since that is equivalent to not specifying an error event listener. Also, remove CONFIG_ERROR_UNSET and the config_source 'default', since all callers are now expected to specify the error handling they want. Add a new "do_event_and_flush" function for running event callbacks immediately, where the event does not need to calculate an end offset. Signed-off-by: Glen Choo Signed-off-by: Josh Steadmon --- builtin/config.c | 4 +- bundle-uri.c | 4 +- config.c | 195 ++++++++++++++++++++++++++++----------------- config.h | 20 +++-- fsck.c | 4 +- submodule-config.c | 9 ++- 6 files changed, 147 insertions(+), 89 deletions(-) diff --git a/builtin/config.c b/builtin/config.c index 1c75cbc43d..e2cf49de7a 100644 --- a/builtin/config.c +++ b/builtin/config.c @@ -42,7 +42,9 @@ static int actions, type; static char *default_value; static int end_nul; static int respect_includes_opt = -1; -static struct config_options config_options; +static struct config_options config_options = { + .parse_options = CP_OPTS_INIT(CONFIG_ERROR_DIE) +}; static int show_origin; static int show_scope; static int fixed_value; diff --git a/bundle-uri.c b/bundle-uri.c index f93ca6a486..856bffdcad 100644 --- a/bundle-uri.c +++ b/bundle-uri.c @@ -237,9 +237,7 @@ int bundle_uri_parse_config_format(const char *uri, struct bundle_list *list) { int result; - struct config_parse_options opts = { - .error_action = CONFIG_ERROR_ERROR, - }; + struct config_parse_options opts = CP_OPTS_INIT(CONFIG_ERROR_ERROR); if (!list->baseURI) { struct strbuf baseURI = STRBUF_INIT; diff --git a/config.c b/config.c index ff138500a2..0c4f1a2874 100644 --- a/config.c +++ b/config.c @@ -55,7 +55,6 @@ struct config_source { enum config_origin_type origin_type; const char *name; const char *path; - enum config_error_action default_error_action; int linenr; int eof; size_t total_len; @@ -185,13 +184,15 @@ static int handle_path_include(const struct key_value_info *kvi, } if (!access_or_die(path, R_OK, 0)) { + struct config_parse_options config_opts = CP_OPTS_INIT(CONFIG_ERROR_DIE); + if (++inc->depth > MAX_INCLUDE_DEPTH) die(_(include_depth_advice), MAX_INCLUDE_DEPTH, path, !kvi ? "" : kvi->filename ? kvi->filename : "the command line"); ret = git_config_from_file_with_options(git_config_include, path, inc, - kvi->scope, NULL); + kvi->scope, &config_opts); inc->depth--; } cleanup: @@ -339,7 +340,9 @@ static int add_remote_url(const char *var, const char *value, static void populate_remote_urls(struct config_include_data *inc) { - struct config_options opts; + struct config_options opts = { + .parse_options = CP_OPTS_INIT(CONFIG_ERROR_DIE), + }; opts = *inc->opts; opts.unconditional_remote_url = 1; @@ -1039,6 +1042,29 @@ static int do_event(struct config_source *cs, enum config_event_t type, return 0; } +static int do_event_and_flush(struct config_source *cs, + enum config_event_t type, + struct parse_event_data *data) +{ + int maybe_ret; + + if ((maybe_ret = flush_event(cs, type, data)) < 1) + return maybe_ret; + + start_event(cs, type, data); + + if ((maybe_ret = flush_event(cs, type, data)) < 1) + return maybe_ret; + + /* + * Not actually EOF, but this indicates we don't have a valid event + * to flush next time around. + */ + data->previous_type = CONFIG_EVENT_EOF; + + return 0; +} + static void kvi_from_source(struct config_source *cs, enum config_scope scope, struct key_value_info *out) @@ -1050,6 +1076,56 @@ static void kvi_from_source(struct config_source *cs, out->path = cs->path; } +int git_config_err_fn(enum config_event_t type, size_t begin_offset UNUSED, + size_t end_offset UNUSED, struct config_source *cs, + void *data) +{ + char *error_msg = NULL; + int error_return = 0; + enum config_error_action *action = data; + + if (type != CONFIG_EVENT_ERROR) + return 0; + + switch (cs->origin_type) { + case CONFIG_ORIGIN_BLOB: + error_msg = xstrfmt(_("bad config line %d in blob %s"), + cs->linenr, cs->name); + break; + case CONFIG_ORIGIN_FILE: + error_msg = xstrfmt(_("bad config line %d in file %s"), + cs->linenr, cs->name); + break; + case CONFIG_ORIGIN_STDIN: + error_msg = xstrfmt(_("bad config line %d in standard input"), + cs->linenr); + break; + case CONFIG_ORIGIN_SUBMODULE_BLOB: + error_msg = xstrfmt(_("bad config line %d in submodule-blob %s"), + cs->linenr, cs->name); + break; + case CONFIG_ORIGIN_CMDLINE: + error_msg = xstrfmt(_("bad config line %d in command line %s"), + cs->linenr, cs->name); + break; + default: + error_msg = xstrfmt(_("bad config line %d in %s"), + cs->linenr, cs->name); + } + + switch (*action) { + case CONFIG_ERROR_DIE: + die("%s", error_msg); + break; + case CONFIG_ERROR_ERROR: + error_return = error("%s", error_msg); + break; + } + + free(error_msg); + return error_return; +} + static int git_parse_source(struct config_source *cs, config_fn_t fn, struct key_value_info *kvi, void *data, const struct config_parse_options *opts) @@ -1057,8 +1133,6 @@ static int git_parse_source(struct config_source *cs, config_fn_t fn, int comment = 0; size_t baselen = 0; struct strbuf *var = &cs->var; - int error_return = 0; - char *error_msg = NULL; /* U+FEFF Byte Order Mark in UTF8 */ const char *bomptr = utf8_bom; @@ -1140,53 +1214,8 @@ static int git_parse_source(struct config_source *cs, config_fn_t fn, break; } - if (do_event(cs, CONFIG_EVENT_ERROR, &event_data) < 0) - return -1; - - switch (cs->origin_type) { - case CONFIG_ORIGIN_BLOB: - error_msg = xstrfmt(_("bad config line %d in blob %s"), - cs->linenr, cs->name); - break; - case CONFIG_ORIGIN_FILE: - error_msg = xstrfmt(_("bad config line %d in file %s"), - cs->linenr, cs->name); - break; - case CONFIG_ORIGIN_STDIN: - error_msg = xstrfmt(_("bad config line %d in standard input"), - cs->linenr); - break; - case CONFIG_ORIGIN_SUBMODULE_BLOB: - error_msg = xstrfmt(_("bad config line %d in submodule-blob %s"), - cs->linenr, cs->name); - break; - case CONFIG_ORIGIN_CMDLINE: - error_msg = xstrfmt(_("bad config line %d in command line %s"), - cs->linenr, cs->name); - break; - default: - error_msg = xstrfmt(_("bad config line %d in %s"), - cs->linenr, cs->name); - } - - switch (opts && opts->error_action ? - opts->error_action : - cs->default_error_action) { - case CONFIG_ERROR_DIE: - die("%s", error_msg); - break; - case CONFIG_ERROR_ERROR: - error_return = error("%s", error_msg); - break; - case CONFIG_ERROR_SILENT: - error_return = -1; - break; - case CONFIG_ERROR_UNSET: - BUG("config error action unset"); - } - - free(error_msg); - return error_return; + do_event_and_flush(cs, CONFIG_EVENT_ERROR, &event_data); + return -1; } static uintmax_t get_unit_factor(const char *end) @@ -2023,7 +2052,6 @@ static int do_config_from_file(config_fn_t fn, top.origin_type = origin_type; top.name = name; top.path = path; - top.default_error_action = CONFIG_ERROR_DIE; top.do_fgetc = config_file_fgetc; top.do_ungetc = config_file_ungetc; top.do_ftell = config_file_ftell; @@ -2037,8 +2065,10 @@ static int do_config_from_file(config_fn_t fn, static int git_config_from_stdin(config_fn_t fn, void *data, enum config_scope scope) { + struct config_parse_options config_opts = CP_OPTS_INIT(CONFIG_ERROR_DIE); + return do_config_from_file(fn, CONFIG_ORIGIN_STDIN, "", NULL, stdin, - data, scope, NULL); + data, scope, &config_opts); } int git_config_from_file_with_options(config_fn_t fn, const char *filename, @@ -2061,8 +2091,10 @@ int git_config_from_file_with_options(config_fn_t fn, const char *filename, int git_config_from_file(config_fn_t fn, const char *filename, void *data) { + struct config_parse_options config_opts = CP_OPTS_INIT(CONFIG_ERROR_DIE); + return git_config_from_file_with_options(fn, filename, data, - CONFIG_SCOPE_UNKNOWN, NULL); + CONFIG_SCOPE_UNKNOWN, &config_opts); } int git_config_from_mem(config_fn_t fn, @@ -2079,7 +2111,6 @@ int git_config_from_mem(config_fn_t fn, top.origin_type = origin_type; top.name = name; top.path = NULL; - top.default_error_action = CONFIG_ERROR_ERROR; top.do_fgetc = config_buf_fgetc; top.do_ungetc = config_buf_ungetc; top.do_ftell = config_buf_ftell; @@ -2098,6 +2129,7 @@ int git_config_from_blob_oid(config_fn_t fn, char *buf; unsigned long size; int ret; + struct config_parse_options config_opts = CP_OPTS_INIT(CONFIG_ERROR_ERROR); buf = repo_read_object_file(repo, oid, &type, &size); if (!buf) @@ -2108,7 +2140,7 @@ int git_config_from_blob_oid(config_fn_t fn, } ret = git_config_from_mem(fn, CONFIG_ORIGIN_BLOB, name, buf, size, - data, scope, NULL); + data, scope, &config_opts); free(buf); return ret; @@ -2209,29 +2241,32 @@ static int do_git_config_sequence(const struct config_options *opts, opts->system_gently ? ACCESS_EACCES_OK : 0)) ret += git_config_from_file_with_options(fn, system_config, data, CONFIG_SCOPE_SYSTEM, - NULL); + &opts->parse_options); git_global_config(&user_config, &xdg_config); if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK)) ret += git_config_from_file_with_options(fn, xdg_config, data, - CONFIG_SCOPE_GLOBAL, NULL); + CONFIG_SCOPE_GLOBAL, + &opts->parse_options); if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK)) ret += git_config_from_file_with_options(fn, user_config, data, - CONFIG_SCOPE_GLOBAL, NULL); + CONFIG_SCOPE_GLOBAL, + &opts->parse_options); if (!opts->ignore_repo && repo_config && !access_or_die(repo_config, R_OK, 0)) ret += git_config_from_file_with_options(fn, repo_config, data, - CONFIG_SCOPE_LOCAL, NULL); + CONFIG_SCOPE_LOCAL, + &opts->parse_options); if (!opts->ignore_worktree && worktree_config && repo && repo->repository_format_worktree_config && !access_or_die(worktree_config, R_OK, 0)) { ret += git_config_from_file_with_options(fn, worktree_config, data, CONFIG_SCOPE_WORKTREE, - NULL); + &opts->parse_options); } if (!opts->ignore_cmdline && git_config_from_parameters(fn, data) < 0) @@ -2272,7 +2307,7 @@ int config_with_options(config_fn_t fn, void *data, } else if (config_source && config_source->file) { ret = git_config_from_file_with_options(fn, config_source->file, data, config_source->scope, - NULL); + &opts->parse_options); } else if (config_source && config_source->blob) { ret = git_config_from_blob_ref(fn, repo, config_source->blob, data, config_source->scope); @@ -2310,9 +2345,11 @@ static void configset_iter(struct config_set *set, config_fn_t fn, void *data) void read_early_config(config_fn_t cb, void *data) { - struct config_options opts = {0}; struct strbuf commondir = STRBUF_INIT; struct strbuf gitdir = STRBUF_INIT; + struct config_options opts = { + .parse_options = CP_OPTS_INIT(CONFIG_ERROR_DIE), + }; opts.respect_includes = 1; @@ -2344,13 +2381,14 @@ void read_early_config(config_fn_t cb, void *data) */ void read_very_early_config(config_fn_t cb, void *data) { - struct config_options opts = { 0 }; - - opts.respect_includes = 1; - opts.ignore_repo = 1; - opts.ignore_worktree = 1; - opts.ignore_cmdline = 1; - opts.system_gently = 1; + struct config_options opts = { + .respect_includes = 1, + .ignore_repo = 1, + .ignore_worktree = 1, + .ignore_cmdline = 1, + .system_gently = 1, + .parse_options = CP_OPTS_INIT(CONFIG_ERROR_DIE), + }; config_with_options(cb, data, NULL, NULL, &opts); } @@ -2635,7 +2673,9 @@ int git_configset_get_pathname(struct config_set *set, const char *key, const ch /* Functions use to read configuration from a repository */ static void repo_read_config(struct repository *repo) { - struct config_options opts = { 0 }; + struct config_options opts = { + .parse_options = CP_OPTS_INIT(CONFIG_ERROR_DIE), + }; opts.respect_includes = 1; opts.commondir = repo->commondir; @@ -2786,8 +2826,10 @@ static void read_protected_config(void) .ignore_repo = 1, .ignore_worktree = 1, .system_gently = 1, + .parse_options = CP_OPTS_INIT(CONFIG_ERROR_DIE), }; + git_configset_init(&protected_config); config_with_options(config_set_callback, &protected_config, NULL, NULL, &opts); @@ -2998,6 +3040,7 @@ struct config_store_data { enum config_event_t type; int is_keys_section; } *parsed; + enum config_error_action error_action; unsigned int parsed_nr, parsed_alloc, *seen, seen_nr, seen_alloc; unsigned int key_seen:1, section_seen:1, is_keys_section:1; }; @@ -3065,6 +3108,10 @@ static int store_aux_event(enum config_event_t type, size_t begin, size_t end, store->seen[store->seen_nr] = store->parsed_nr; } } + if (type == CONFIG_EVENT_ERROR) { + return git_config_err_fn(type, begin, end, cs, + &store->error_action); + } store->parsed_nr++; @@ -3402,7 +3449,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename, struct stat st; size_t copy_begin, copy_end; int i, new_line = 0; - struct config_parse_options opts; + struct config_parse_options opts = CP_OPTS_INIT(CONFIG_ERROR_DIE); if (!value_pattern) store.value_pattern = NULL; @@ -3429,8 +3476,8 @@ int git_config_set_multivar_in_file_gently(const char *config_filename, ALLOC_GROW(store.parsed, 1, store.parsed_alloc); store.parsed[0].end = 0; + store.error_action = CONFIG_ERROR_DIE; - memset(&opts, 0, sizeof(opts)); opts.event_fn = store_aux_event; opts.event_fn_data = &store; diff --git a/config.h b/config.h index 2537516446..8ad399580f 100644 --- a/config.h +++ b/config.h @@ -86,12 +86,6 @@ typedef int (*config_parser_event_fn_t)(enum config_event_t type, void *event_fn_data); struct config_parse_options { - enum config_error_action { - CONFIG_ERROR_UNSET = 0, /* use source-specific default */ - CONFIG_ERROR_DIE, /* die() on error */ - CONFIG_ERROR_ERROR, /* error() on error, return -1 */ - CONFIG_ERROR_SILENT, /* return -1 */ - } error_action; /* * event_fn and event_fn_data are for internal use only. Handles events * emitted by the config parser. @@ -100,6 +94,11 @@ struct config_parse_options { void *event_fn_data; }; +#define CP_OPTS_INIT(error_action) { \ + .event_fn = git_config_err_fn, \ + .event_fn_data = (enum config_error_action []){(error_action)}, \ +} + struct config_options { unsigned int respect_includes : 1; unsigned int ignore_repo : 1; @@ -119,6 +118,15 @@ struct config_options { unsigned int unconditional_remote_url : 1; }; +enum config_error_action { + CONFIG_ERROR_DIE, /* die() on error */ + CONFIG_ERROR_ERROR, /* error() on error, return -1 */ +}; + +int git_config_err_fn(enum config_event_t type, size_t begin_offset, + size_t end_offset, struct config_source *cs, + void *event_fn_data); + /* Config source metadata for a given config key-value pair */ struct key_value_info { const char *filename; diff --git a/fsck.c b/fsck.c index 522ee1c18a..bc0ca11421 100644 --- a/fsck.c +++ b/fsck.c @@ -1219,7 +1219,6 @@ static int fsck_blob(const struct object_id *oid, const char *buf, return 0; if (oidset_contains(&options->gitmodules_found, oid)) { - struct config_parse_options config_opts = { 0 }; struct fsck_gitmodules_data data; oidset_insert(&options->gitmodules_done, oid); @@ -1238,10 +1237,9 @@ static int fsck_blob(const struct object_id *oid, const char *buf, data.oid = oid; data.options = options; data.ret = 0; - config_opts.error_action = CONFIG_ERROR_SILENT; if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB, ".gitmodules", buf, size, &data, - CONFIG_SCOPE_UNKNOWN, &config_opts)) + CONFIG_SCOPE_UNKNOWN, NULL)) data.ret |= report(options, oid, OBJ_BLOB, FSCK_MSG_GITMODULES_PARSE, "could not parse gitmodules blob"); diff --git a/submodule-config.c b/submodule-config.c index b6908e295f..d97135c917 100644 --- a/submodule-config.c +++ b/submodule-config.c @@ -565,6 +565,8 @@ static const struct submodule *config_from(struct submodule_cache *cache, enum object_type type; const struct submodule *submodule = NULL; struct parse_config_parameter parameter; + struct config_parse_options config_opts = CP_OPTS_INIT(CONFIG_ERROR_ERROR); + /* * If any parameter except the cache is a NULL pointer just @@ -608,7 +610,8 @@ static const struct submodule *config_from(struct submodule_cache *cache, parameter.gitmodules_oid = &oid; parameter.overwrite = 0; git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf, - config, config_size, ¶meter, CONFIG_SCOPE_UNKNOWN, NULL); + config, config_size, ¶meter, + CONFIG_SCOPE_UNKNOWN, &config_opts); strbuf_release(&rev); free(config); @@ -652,7 +655,9 @@ static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void struct git_config_source config_source = { 0, .scope = CONFIG_SCOPE_SUBMODULE }; - const struct config_options opts = { 0 }; + struct config_options opts = { + .parse_options = CP_OPTS_INIT(CONFIG_ERROR_DIE), + }; struct object_id oid; char *file; char *oidstr = NULL; From patchwork Thu Sep 21 21:17:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 13394747 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 61F93E7D0AF for ; Thu, 21 Sep 2023 21:58:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230445AbjIUV6s (ORCPT ); Thu, 21 Sep 2023 17:58:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50536 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232917AbjIUVj4 (ORCPT ); Thu, 21 Sep 2023 17:39:56 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6F29400E4 for ; Thu, 21 Sep 2023 14:17:35 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-597f461adc5so21234717b3.1 for ; Thu, 21 Sep 2023 14:17:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695331055; x=1695935855; 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=rvVec6cbm7k9iBEgTWmnNMO71JWPvgA81uYJ7EX831A=; b=GaHYdh5bQs9SnxOxIAj+l2+ielRqNcfvG0reSwz+paFri0brpPYUSHyiy2FpDJFoZ6 B+WUwjyZZOc7+jzOqedzdRV5bCKyk/NJNWPQvOKnYaktNFHaaW85eG3V4ldS2yARCI5B WqCvmXESCZldWVeyFiijhKxWV0bYN6SgNlssUjyQN8s+sKGSbs8HrcRq44zM9MwhotWy JkdW1y5xfHtPiza28Z0Q7nTZK7H6sPj6VEhozofql1Ufv2lC8MgPiEXbrG3Kh9X+gMSB B/OAWerDhEA4ye7QLLyy8UMpxxp1R2i5f60FDON9vnrhLPQbh/llsS0jGgdzJPallIJE hRRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695331055; x=1695935855; 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=rvVec6cbm7k9iBEgTWmnNMO71JWPvgA81uYJ7EX831A=; b=hMP+Ml+TKYa1ltGauMpi0zJzJhFqBC3JhcECkM2gWcI2a6Q/thTurkrVV5uOMmwCML +ohMs5gZh/D/EfDSa36pc4lQ9RJc1mKvfclXldaD0kIfawNat0eSORnmx9FKppj8yfrg VAY0g+pJoFBrMToCEZFyfMlIQphZpH8PFEE70hnTqjMBIe+DTdGV/vxnGR7AK4O0NNuO Ck5GKm3xztmq/0Xid4eP7h/nD3y4OzsxzBBqGT7Rj3mHmLdGirOjPsaasuWPz02uLcu6 4iONjxk81XYncgud39LTH0+LjNWgH5FbcBZqqKlZhUPf+q+CqnqDtCZYF41MtJoZ+GKE WPsg== X-Gm-Message-State: AOJu0YzpxZ7Da0+eO5py84hJ0MFFbi57m7slZfBQShNt8xDKjmGSlEyZ qXxTqt+Z0cuQY+Ku176xTjML7AjzfjK0Duc9o27gimRTgRJwSsj2NbB9rTegA3CPzOHCwgwvger JkaCyFotBBZGchsuV6bErwPUxrTC/3702v+CJqw1VziJCEcq535qogb6jop6QVnA= X-Google-Smtp-Source: AGHT+IF851MqmTK9CO/smWr91n0HwIdZ3Si0jUrwHND8BAKGuigbNSnhrvGQrzsq5Pku/YbGVnelZI6xonErMg== X-Received: from lunarfall.svl.corp.google.com ([2620:15c:2d3:204:828c:91e1:20c5:c8f]) (user=steadmon job=sendgmr) by 2002:a81:b726:0:b0:59b:ccba:124f with SMTP id v38-20020a81b726000000b0059bccba124fmr94547ywh.9.1695331054767; Thu, 21 Sep 2023 14:17:34 -0700 (PDT) Date: Thu, 21 Sep 2023 14:17:23 -0700 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.42.0.515.g380fc7ccd1-goog Message-ID: <49d4b649919e5661daa2113c2f5d674c5cd8dd0e.1695330852.git.steadmon@google.com> Subject: [PATCH v3 4/5] config.c: accept config_parse_options in git_config_from_stdin From: Josh Steadmon To: git@vger.kernel.org Cc: jonathantanmy@google.com, calvinwan@google.com, glencbz@gmail.com, gitster@pobox.com Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Glen Choo A later commit will move git_config_from_stdin() to a library, so it will need to accept event listeners. Signed-off-by: Glen Choo Signed-off-by: Josh Steadmon --- config.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/config.c b/config.c index 0c4f1a2874..50188f469a 100644 --- a/config.c +++ b/config.c @@ -2063,12 +2063,11 @@ static int do_config_from_file(config_fn_t fn, } static int git_config_from_stdin(config_fn_t fn, void *data, - enum config_scope scope) + enum config_scope scope, + const struct config_parse_options *config_opts) { - struct config_parse_options config_opts = CP_OPTS_INIT(CONFIG_ERROR_DIE); - return do_config_from_file(fn, CONFIG_ORIGIN_STDIN, "", NULL, stdin, - data, scope, &config_opts); + data, scope, config_opts); } int git_config_from_file_with_options(config_fn_t fn, const char *filename, @@ -2303,7 +2302,8 @@ int config_with_options(config_fn_t fn, void *data, * regular lookup sequence. */ if (config_source && config_source->use_stdin) { - ret = git_config_from_stdin(fn, data, config_source->scope); + ret = git_config_from_stdin(fn, data, config_source->scope, + &opts->parse_options); } else if (config_source && config_source->file) { ret = git_config_from_file_with_options(fn, config_source->file, data, config_source->scope, From patchwork Thu Sep 21 21:17:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 13394678 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 EDFA1E7D0AB for ; Thu, 21 Sep 2023 21:40:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231285AbjIUVkY (ORCPT ); Thu, 21 Sep 2023 17:40:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232963AbjIUVkB (ORCPT ); Thu, 21 Sep 2023 17:40:01 -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 3515242C10 for ; Thu, 21 Sep 2023 14:17:38 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d818fb959f4so2018565276.1 for ; Thu, 21 Sep 2023 14:17:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695331057; x=1695935857; 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=fGPZSFX56sDJcXrL9rdO2K/MOl6wfMRtoPsQvPeDja0=; b=NNF58hlTtWnOsaqE8wFdrb+y6PGdSJvfRegUlxBxDteHpLlwl+oy0lNbrXh5ocdQjO YyxmUEE47499ds2XRmEP0xqrhL+uCstri6rv7Sb6dzoS+KZJTFEBcO7p4Xw4K5uO+JBo mwx4J6reex2roRVLlQwwViHVASqxUReWDb84b9U0SNPijB7kOalk0XJb1Zhim9glOHVO +t+DjRQXl0vIMVgJB/FxzymrWld+DEZOC4GRJwKwObBK8mLM5kqLYRaS/V8Px/sIbd92 zGxS8dqfxq2etmeOO/whMg2tQc2kvYrZzS4TzCUifLWz7Lurt9YhGI+Df/A5GLN9tpX8 lVjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695331057; x=1695935857; 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=fGPZSFX56sDJcXrL9rdO2K/MOl6wfMRtoPsQvPeDja0=; b=CFjtm6cmaLjarniykizWW2Im6YcGDnUt2yWfI/fB1L26syxSWe06L4GT78+y15k53/ bw9mxexWuZ9GbtQLZLV2bU9WpECZ17yUpJzrC1tNcSjZ0sVr7+rSqzT/y6ztR5jlhqVM 49mAlQMd5Bov76jFI+doThlOtxzjxRfolkYjnNN6ElzAlRV0fY1PxhtQA4Q1EiUpQdKm a/o2TklLXpBNZcXRkp9MxebTmpyvKEsjZEwX0dhWlEn7Mh1TYNF4ghNr50qRWMLH7HS0 j2k3Gu0OVw7WjPLJf3EYDLq9rL5PoqCUBdqoL6Psjys0x9HRinyaCfLpcUz5ufIRF/3z HDGQ== X-Gm-Message-State: AOJu0YzuFPhJaMiHf2AHay8J0YsZulEMMh3uLyhmPWcXm9eEzuWPAbAu TokPRmQUuD+Ex8cNKy+/IPgqG4Ltbq/8gnzgF5jY5BzvrtJpCwUNCkKnkKSOvfzyqAWMlc1HM0A e0KhmtkWtAn2weQUwtwFnhAy2zGhN60daKaxLfFwdYNOcFG9V3L00KcetHtB62OU= X-Google-Smtp-Source: AGHT+IE57gzXpAKCWrIgwYEMTnkXSApzIdP4at1C3Ut4U0xVSwTOfCC7vrUIPYilpebyhguA8PyOFdy2Ejk71g== X-Received: from lunarfall.svl.corp.google.com ([2620:15c:2d3:204:828c:91e1:20c5:c8f]) (user=steadmon job=sendgmr) by 2002:a25:408f:0:b0:d7f:8774:dfd4 with SMTP id n137-20020a25408f000000b00d7f8774dfd4mr88408yba.12.1695331057245; Thu, 21 Sep 2023 14:17:37 -0700 (PDT) Date: Thu, 21 Sep 2023 14:17:24 -0700 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.42.0.515.g380fc7ccd1-goog Message-ID: Subject: [PATCH v3 5/5] config-parse: split library out of config.[c|h] From: Josh Steadmon To: git@vger.kernel.org Cc: jonathantanmy@google.com, calvinwan@google.com, glencbz@gmail.com, gitster@pobox.com Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Glen Choo The config parsing machinery (besides "include" directives) is usable by programs other than Git - it works with any file written in Git config syntax (IOW it doesn't rely on 'core' Git features like a repository), and as of the series ending at 6e8e7981eb (config: pass source to config_parser_event_fn_t, 2023-06-28), it no longer relies on global state. Thus, we can and should start turning it into a library other programs can use. Begin this process by splitting the config parsing code out of config.[c|h] and into config-parse.[c|h]. Do not change interfaces or function bodies, but tweak visibility and includes where appropriate, namely: - git_config_from_stdin() is now non-static so that it can be seen by config.c. - "struct config_source" is now defined in the .h file so that it can be seen by config.c. And as a result, config-lib.h needs to "#include strbuf.h". In theory, this makes it possible for in-tree files to decide whether they only need all of the config functionality or only config parsing, and bring in the smallest bit of functionality needed. But for now, there are no in-tree files that can swap "#include config.h" for "#include config-parse.h". E.g. Bundle URIs would only need config parsing to parse bundle lists, but bundle-uri.c uses other config.h functionality like key parsing and reading repo settings. The resulting library is usable, though it is unergonomic to do so, e.g. the caller needs to "#include git-compat-util.h" and other dependencies, and we don't have an easy way of linking in the required objects. This isn't the end state we want for our libraries, but at least we have _some_ library whose usability we can improve in future series. Signed-off-by: Glen Choo Signed-off-by: Josh Steadmon --- Makefile | 1 + config-parse.c | 601 +++++++++++++++++++++++++++++++++++++++++++++++ config-parse.h | 155 ++++++++++++ config.c | 621 ------------------------------------------------- config.h | 119 +--------- 5 files changed, 758 insertions(+), 739 deletions(-) create mode 100644 config-parse.c create mode 100644 config-parse.h diff --git a/Makefile b/Makefile index fb541dedc9..67e05bcee5 100644 --- a/Makefile +++ b/Makefile @@ -992,6 +992,7 @@ LIB_OBJS += compat/obstack.o LIB_OBJS += compat/terminal.o LIB_OBJS += compat/zlib-uncompress2.o LIB_OBJS += config.o +LIB_OBJS += config-parse.o LIB_OBJS += connect.o LIB_OBJS += connected.o LIB_OBJS += convert.o diff --git a/config-parse.c b/config-parse.c new file mode 100644 index 0000000000..66e5953e29 --- /dev/null +++ b/config-parse.c @@ -0,0 +1,601 @@ +#include "git-compat-util.h" +#include "strbuf.h" +#include "gettext.h" +#include "hashmap.h" +#include "utf8.h" +#include "config-parse.h" + +static int config_file_fgetc(struct config_source *conf) +{ + return getc_unlocked(conf->u.file); +} + +static int config_file_ungetc(int c, struct config_source *conf) +{ + return ungetc(c, conf->u.file); +} + +static long config_file_ftell(struct config_source *conf) +{ + return ftell(conf->u.file); +} + + +static int config_buf_fgetc(struct config_source *conf) +{ + if (conf->u.buf.pos < conf->u.buf.len) + return conf->u.buf.buf[conf->u.buf.pos++]; + + return EOF; +} + +static int config_buf_ungetc(int c, struct config_source *conf) +{ + if (conf->u.buf.pos > 0) { + conf->u.buf.pos--; + if (conf->u.buf.buf[conf->u.buf.pos] != c) + BUG("config_buf can only ungetc the same character"); + return c; + } + + return EOF; +} + +static long config_buf_ftell(struct config_source *conf) +{ + return conf->u.buf.pos; +} + +static inline int iskeychar(int c) +{ + return isalnum(c) || c == '-'; +} + +/* + * Auxiliary function to sanity-check and split the key into the section + * identifier and variable name. + * + * Returns 0 on success, -CONFIG_INVALID_KEY when there is an invalid character + * in the key and -CONFIG_NO_SECTION_OR_NAME if there is no section name in the + * key. + * + * store_key - pointer to char* which will hold a copy of the key with + * lowercase section and variable name + * baselen - pointer to size_t which will hold the length of the + * section + subsection part, can be NULL + */ +int git_config_parse_key(const char *key, char **store_key, size_t *baselen_) +{ + size_t i, baselen; + int dot; + const char *last_dot = strrchr(key, '.'); + + /* + * Since "key" actually contains the section name and the real + * key name separated by a dot, we have to know where the dot is. + */ + + if (last_dot == NULL || last_dot == key) { + error(_("key does not contain a section: %s"), key); + return -CONFIG_NO_SECTION_OR_NAME; + } + + if (!last_dot[1]) { + error(_("key does not contain variable name: %s"), key); + return -CONFIG_NO_SECTION_OR_NAME; + } + + baselen = last_dot - key; + if (baselen_) + *baselen_ = baselen; + + /* + * Validate the key and while at it, lower case it for matching. + */ + *store_key = xmallocz(strlen(key)); + + dot = 0; + for (i = 0; key[i]; i++) { + unsigned char c = key[i]; + if (c == '.') + dot = 1; + /* Leave the extended basename untouched.. */ + if (!dot || i > baselen) { + if (!iskeychar(c) || + (i == baselen + 1 && !isalpha(c))) { + error(_("invalid key: %s"), key); + goto out_free_ret_1; + } + c = tolower(c); + } else if (c == '\n') { + error(_("invalid key (newline): %s"), key); + goto out_free_ret_1; + } + (*store_key)[i] = c; + } + + return 0; + +out_free_ret_1: + FREE_AND_NULL(*store_key); + return -CONFIG_INVALID_KEY; +} + +static int get_next_char(struct config_source *cs) +{ + int c = cs->do_fgetc(cs); + + if (c == '\r') { + /* DOS like systems */ + c = cs->do_fgetc(cs); + if (c != '\n') { + if (c != EOF) + cs->do_ungetc(c, cs); + c = '\r'; + } + } + + if (c != EOF && ++cs->total_len > INT_MAX) { + /* + * This is an absurdly long config file; refuse to parse + * further in order to protect downstream code from integer + * overflows. Note that we can't return an error specifically, + * but we can mark EOF and put trash in the return value, + * which will trigger a parse error. + */ + cs->eof = 1; + return 0; + } + + if (c == '\n') + cs->linenr++; + if (c == EOF) { + cs->eof = 1; + cs->linenr++; + c = '\n'; + } + return c; +} + +static char *parse_value(struct config_source *cs) +{ + int quote = 0, comment = 0, space = 0; + + strbuf_reset(&cs->value); + for (;;) { + int c = get_next_char(cs); + if (c == '\n') { + if (quote) { + cs->linenr--; + return NULL; + } + return cs->value.buf; + } + if (comment) + continue; + if (isspace(c) && !quote) { + if (cs->value.len) + space++; + continue; + } + if (!quote) { + if (c == ';' || c == '#') { + comment = 1; + continue; + } + } + for (; space; space--) + strbuf_addch(&cs->value, ' '); + if (c == '\\') { + c = get_next_char(cs); + switch (c) { + case '\n': + continue; + case 't': + c = '\t'; + break; + case 'b': + c = '\b'; + break; + case 'n': + c = '\n'; + break; + /* Some characters escape as themselves */ + case '\\': case '"': + break; + /* Reject unknown escape sequences */ + default: + return NULL; + } + strbuf_addch(&cs->value, c); + continue; + } + if (c == '"') { + quote = 1-quote; + continue; + } + strbuf_addch(&cs->value, c); + } +} + +static int get_value(struct config_source *cs, struct key_value_info *kvi, + config_fn_t fn, void *data, struct strbuf *name) +{ + int c; + char *value; + int ret; + struct config_context ctx = { + .kvi = kvi, + }; + + /* Get the full name */ + for (;;) { + c = get_next_char(cs); + if (cs->eof) + break; + if (!iskeychar(c)) + break; + strbuf_addch(name, tolower(c)); + } + + while (c == ' ' || c == '\t') + c = get_next_char(cs); + + value = NULL; + if (c != '\n') { + if (c != '=') + return -1; + value = parse_value(cs); + if (!value) + return -1; + } + /* + * We already consumed the \n, but we need linenr to point to + * the line we just parsed during the call to fn to get + * accurate line number in error messages. + */ + cs->linenr--; + kvi->linenr = cs->linenr; + ret = fn(name->buf, value, &ctx, data); + if (ret >= 0) + cs->linenr++; + return ret; +} + +static int get_extended_base_var(struct config_source *cs, struct strbuf *name, + int c) +{ + cs->subsection_case_sensitive = 0; + do { + if (c == '\n') + goto error_incomplete_line; + c = get_next_char(cs); + } while (isspace(c)); + + /* We require the format to be '[base "extension"]' */ + if (c != '"') + return -1; + strbuf_addch(name, '.'); + + for (;;) { + int c = get_next_char(cs); + if (c == '\n') + goto error_incomplete_line; + if (c == '"') + break; + if (c == '\\') { + c = get_next_char(cs); + if (c == '\n') + goto error_incomplete_line; + } + strbuf_addch(name, c); + } + + /* Final ']' */ + if (get_next_char(cs) != ']') + return -1; + return 0; +error_incomplete_line: + cs->linenr--; + return -1; +} + +static int get_base_var(struct config_source *cs, struct strbuf *name) +{ + cs->subsection_case_sensitive = 1; + for (;;) { + int c = get_next_char(cs); + if (cs->eof) + return -1; + if (c == ']') + return 0; + if (isspace(c)) + return get_extended_base_var(cs, name, c); + if (!iskeychar(c) && c != '.') + return -1; + strbuf_addch(name, tolower(c)); + } +} + +struct parse_event_data { + enum config_event_t previous_type; + size_t previous_offset; + const struct config_parse_options *opts; +}; + +static size_t get_corrected_offset(struct config_source *cs, + enum config_event_t type) +{ + size_t offset = cs->do_ftell(cs); + + /* + * At EOF, the parser always "inserts" an extra '\n', therefore + * the end offset of the event is the current file position, otherwise + * we will already have advanced to the next event. + */ + if (type != CONFIG_EVENT_EOF) + offset--; + return offset; +} + +static void start_event(struct config_source *cs, enum config_event_t type, + struct parse_event_data *data) +{ + data->previous_type = type; + data->previous_offset = get_corrected_offset(cs, type); +} + +static int flush_event(struct config_source *cs, enum config_event_t type, + struct parse_event_data *data) +{ + if (!data->opts || !data->opts->event_fn) + return 0; + + if (type == CONFIG_EVENT_WHITESPACE && + data->previous_type == type) + return 0; + + if (data->previous_type != CONFIG_EVENT_EOF && + data->opts->event_fn(data->previous_type, data->previous_offset, + get_corrected_offset(cs, type), cs, + data->opts->event_fn_data) < 0) + return -1; + + return 1; +} + +static int do_event(struct config_source *cs, enum config_event_t type, + struct parse_event_data *data) +{ + int maybe_ret; + + if ((maybe_ret = flush_event(cs, type, data)) < 1) + return maybe_ret; + + start_event(cs, type, data); + + return 0; +} + +static int do_event_and_flush(struct config_source *cs, + enum config_event_t type, + struct parse_event_data *data) +{ + int maybe_ret; + + if ((maybe_ret = flush_event(cs, type, data)) < 1) + return maybe_ret; + + start_event(cs, type, data); + + if ((maybe_ret = flush_event(cs, type, data)) < 1) + return maybe_ret; + + /* + * Not actually EOF, but this indicates we don't have a valid event + * to flush next time around. + */ + data->previous_type = CONFIG_EVENT_EOF; + + return 0; +} + +static void kvi_from_source(struct config_source *cs, + enum config_scope scope, + struct key_value_info *out) +{ + out->filename = strintern(cs->name); + out->origin_type = cs->origin_type; + out->linenr = cs->linenr; + out->scope = scope; + out->path = cs->path; +} + +static int git_parse_source(struct config_source *cs, config_fn_t fn, + struct key_value_info *kvi, void *data, + const struct config_parse_options *opts) +{ + int comment = 0; + size_t baselen = 0; + struct strbuf *var = &cs->var; + + /* U+FEFF Byte Order Mark in UTF8 */ + const char *bomptr = utf8_bom; + + /* For the parser event callback */ + struct parse_event_data event_data = { + CONFIG_EVENT_EOF, 0, opts + }; + + for (;;) { + int c; + + c = get_next_char(cs); + if (bomptr && *bomptr) { + /* We are at the file beginning; skip UTF8-encoded BOM + * if present. Sane editors won't put this in on their + * own, but e.g. Windows Notepad will do it happily. */ + if (c == (*bomptr & 0377)) { + bomptr++; + continue; + } else { + /* Do not tolerate partial BOM. */ + if (bomptr != utf8_bom) + break; + /* No BOM at file beginning. Cool. */ + bomptr = NULL; + } + } + if (c == '\n') { + if (cs->eof) { + if (do_event(cs, CONFIG_EVENT_EOF, &event_data) < 0) + return -1; + return 0; + } + if (do_event(cs, CONFIG_EVENT_WHITESPACE, &event_data) < 0) + return -1; + comment = 0; + continue; + } + if (comment) + continue; + if (isspace(c)) { + if (do_event(cs, CONFIG_EVENT_WHITESPACE, &event_data) < 0) + return -1; + continue; + } + if (c == '#' || c == ';') { + if (do_event(cs, CONFIG_EVENT_COMMENT, &event_data) < 0) + return -1; + comment = 1; + continue; + } + if (c == '[') { + if (do_event(cs, CONFIG_EVENT_SECTION, &event_data) < 0) + return -1; + + /* Reset prior to determining a new stem */ + strbuf_reset(var); + if (get_base_var(cs, var) < 0 || var->len < 1) + break; + strbuf_addch(var, '.'); + baselen = var->len; + continue; + } + if (!isalpha(c)) + break; + + if (do_event(cs, CONFIG_EVENT_ENTRY, &event_data) < 0) + return -1; + + /* + * Truncate the var name back to the section header + * stem prior to grabbing the suffix part of the name + * and the value. + */ + strbuf_setlen(var, baselen); + strbuf_addch(var, tolower(c)); + if (get_value(cs, kvi, fn, data, var) < 0) + break; + } + + do_event_and_flush(cs, CONFIG_EVENT_ERROR, &event_data); + return -1; +} + +/* + * All source specific fields in the union, die_on_error, name and the callbacks + * fgetc, ungetc, ftell of top need to be initialized before calling + * this function. + */ +static int do_config_from(struct config_source *top, config_fn_t fn, + void *data, enum config_scope scope, + const struct config_parse_options *opts) +{ + struct key_value_info kvi = KVI_INIT; + int ret; + + /* push config-file parsing state stack */ + top->linenr = 1; + top->eof = 0; + top->total_len = 0; + strbuf_init(&top->value, 1024); + strbuf_init(&top->var, 1024); + kvi_from_source(top, scope, &kvi); + + ret = git_parse_source(top, fn, &kvi, data, opts); + + strbuf_release(&top->value); + strbuf_release(&top->var); + + return ret; +} + +static int do_config_from_file(config_fn_t fn, + const enum config_origin_type origin_type, + const char *name, const char *path, FILE *f, + void *data, enum config_scope scope, + const struct config_parse_options *opts) +{ + struct config_source top = CONFIG_SOURCE_INIT; + int ret; + + top.u.file = f; + top.origin_type = origin_type; + top.name = name; + top.path = path; + top.do_fgetc = config_file_fgetc; + top.do_ungetc = config_file_ungetc; + top.do_ftell = config_file_ftell; + + flockfile(f); + ret = do_config_from(&top, fn, data, scope, opts); + funlockfile(f); + return ret; +} + +int git_config_from_stdin(config_fn_t fn, void *data, enum config_scope scope, + const struct config_parse_options *config_opts) +{ + return do_config_from_file(fn, CONFIG_ORIGIN_STDIN, "", NULL, stdin, + data, scope, config_opts); +} + +int git_config_from_file_with_options(config_fn_t fn, const char *filename, + void *data, enum config_scope scope, + const struct config_parse_options *opts) +{ + int ret = -1; + FILE *f; + + if (!filename) + BUG("filename cannot be NULL"); + f = fopen_or_warn(filename, "r"); + if (f) { + ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename, + filename, f, data, scope, opts); + fclose(f); + } + return ret; +} + +int git_config_from_mem(config_fn_t fn, + const enum config_origin_type origin_type, + const char *name, const char *buf, size_t len, + void *data, enum config_scope scope, + const struct config_parse_options *opts) +{ + struct config_source top = CONFIG_SOURCE_INIT; + + top.u.buf.buf = buf; + top.u.buf.len = len; + top.u.buf.pos = 0; + top.origin_type = origin_type; + top.name = name; + top.path = NULL; + top.do_fgetc = config_buf_fgetc; + top.do_ungetc = config_buf_ungetc; + top.do_ftell = config_buf_ftell; + + return do_config_from(&top, fn, data, scope, opts); +} diff --git a/config-parse.h b/config-parse.h new file mode 100644 index 0000000000..ac73a826d9 --- /dev/null +++ b/config-parse.h @@ -0,0 +1,155 @@ +/* + * Low level config parsing. + */ +#ifndef CONFIG_PARSE_H +#define CONFIG_PARSE_H + +#include "strbuf.h" + +/* git_config_parse_key() returns these negated: */ +#define CONFIG_INVALID_KEY 1 +#define CONFIG_NO_SECTION_OR_NAME 2 + +int git_config_parse_key(const char *, char **, size_t *); + +enum config_scope { + CONFIG_SCOPE_UNKNOWN = 0, + CONFIG_SCOPE_SYSTEM, + CONFIG_SCOPE_GLOBAL, + CONFIG_SCOPE_LOCAL, + CONFIG_SCOPE_WORKTREE, + CONFIG_SCOPE_COMMAND, + CONFIG_SCOPE_SUBMODULE, +}; +const char *config_scope_name(enum config_scope scope); + +enum config_origin_type { + CONFIG_ORIGIN_UNKNOWN = 0, + CONFIG_ORIGIN_BLOB, + CONFIG_ORIGIN_FILE, + CONFIG_ORIGIN_STDIN, + CONFIG_ORIGIN_SUBMODULE_BLOB, + CONFIG_ORIGIN_CMDLINE +}; + +enum config_event_t { + CONFIG_EVENT_SECTION, + CONFIG_EVENT_ENTRY, + CONFIG_EVENT_WHITESPACE, + CONFIG_EVENT_COMMENT, + CONFIG_EVENT_EOF, + CONFIG_EVENT_ERROR +}; + +struct config_source; +/* + * The parser event function (if not NULL) is called with the event type and + * the begin/end offsets of the parsed elements. + * + * Note: for CONFIG_EVENT_ENTRY (i.e. config variables), the trailing newline + * character is considered part of the element. + */ +typedef int (*config_parser_event_fn_t)(enum config_event_t type, + size_t begin_offset, size_t end_offset, + struct config_source *cs, + void *event_fn_data); + +struct config_parse_options { + /* + * event_fn and event_fn_data are for internal use only. Handles events + * emitted by the config parser. + */ + config_parser_event_fn_t event_fn; + void *event_fn_data; +}; + +struct config_source { + struct config_source *prev; + union { + FILE *file; + struct config_buf { + const char *buf; + size_t len; + size_t pos; + } buf; + } u; + enum config_origin_type origin_type; + const char *name; + const char *path; + int linenr; + int eof; + size_t total_len; + struct strbuf value; + struct strbuf var; + unsigned subsection_case_sensitive : 1; + + int (*do_fgetc)(struct config_source *c); + int (*do_ungetc)(int c, struct config_source *conf); + long (*do_ftell)(struct config_source *c); +}; +#define CONFIG_SOURCE_INIT { 0 } + +/* Config source metadata for a given config key-value pair */ +struct key_value_info { + const char *filename; + int linenr; + enum config_origin_type origin_type; + enum config_scope scope; + const char *path; +}; +#define KVI_INIT { \ + .filename = NULL, \ + .linenr = -1, \ + .origin_type = CONFIG_ORIGIN_UNKNOWN, \ + .scope = CONFIG_SCOPE_UNKNOWN, \ + .path = NULL, \ +} + +/* Captures additional information that a config callback can use. */ +struct config_context { + /* Config source metadata for key and value. */ + const struct key_value_info *kvi; +}; +#define CONFIG_CONTEXT_INIT { 0 } + +/** + * A config callback function takes four parameters: + * + * - the name of the parsed variable. This is in canonical "flat" form: the + * section, subsection, and variable segments will be separated by dots, + * and the section and variable segments will be all lowercase. E.g., + * `core.ignorecase`, `diff.SomeType.textconv`. + * + * - the value of the found variable, as a string. If the variable had no + * value specified, the value will be NULL (typically this means it + * should be interpreted as boolean true). + * + * - the 'config context', that is, additional information about the config + * iteration operation provided by the config machinery. For example, this + * includes information about the config source being parsed (e.g. the + * filename). + * + * - a void pointer passed in by the caller of the config API; this can + * contain callback-specific data + * + * A config callback should return 0 for success, or -1 if the variable + * could not be parsed properly. + */ +typedef int (*config_fn_t)(const char *, const char *, + const struct config_context *, void *); + +int git_config_from_file_with_options(config_fn_t fn, const char *, + void *, enum config_scope, + const struct config_parse_options *); + +int git_config_from_mem(config_fn_t fn, + const enum config_origin_type, + const char *name, + const char *buf, size_t len, + void *data, enum config_scope scope, + const struct config_parse_options *opts); + +int git_config_from_stdin(config_fn_t fn, void *data, enum config_scope scope, + const struct config_parse_options *config_opts); + +#endif /* CONFIG_PARSE_H */ diff --git a/config.c b/config.c index 50188f469a..e10901514a 100644 --- a/config.c +++ b/config.c @@ -42,32 +42,6 @@ #include "wrapper.h" #include "write-or-die.h" -struct config_source { - struct config_source *prev; - union { - FILE *file; - struct config_buf { - const char *buf; - size_t len; - size_t pos; - } buf; - } u; - enum config_origin_type origin_type; - const char *name; - const char *path; - int linenr; - int eof; - size_t total_len; - struct strbuf value; - struct strbuf var; - unsigned subsection_case_sensitive : 1; - - int (*do_fgetc)(struct config_source *c); - int (*do_ungetc)(int c, struct config_source *conf); - long (*do_ftell)(struct config_source *c); -}; -#define CONFIG_SOURCE_INIT { 0 } - static int pack_compression_seen; static int zlib_compression_seen; @@ -82,47 +56,6 @@ static int zlib_compression_seen; */ static struct config_set protected_config; -static int config_file_fgetc(struct config_source *conf) -{ - return getc_unlocked(conf->u.file); -} - -static int config_file_ungetc(int c, struct config_source *conf) -{ - return ungetc(c, conf->u.file); -} - -static long config_file_ftell(struct config_source *conf) -{ - return ftell(conf->u.file); -} - - -static int config_buf_fgetc(struct config_source *conf) -{ - if (conf->u.buf.pos < conf->u.buf.len) - return conf->u.buf.buf[conf->u.buf.pos++]; - - return EOF; -} - -static int config_buf_ungetc(int c, struct config_source *conf) -{ - if (conf->u.buf.pos > 0) { - conf->u.buf.pos--; - if (conf->u.buf.buf[conf->u.buf.pos] != c) - BUG("config_buf can only ungetc the same character"); - return c; - } - - return EOF; -} - -static long config_buf_ftell(struct config_source *conf) -{ - return conf->u.buf.pos; -} - struct config_include_data { int depth; config_fn_t fn; @@ -528,80 +461,6 @@ void git_config_push_env(const char *spec) free(key); } -static inline int iskeychar(int c) -{ - return isalnum(c) || c == '-'; -} - -/* - * Auxiliary function to sanity-check and split the key into the section - * identifier and variable name. - * - * Returns 0 on success, -1 when there is an invalid character in the key and - * -2 if there is no section name in the key. - * - * store_key - pointer to char* which will hold a copy of the key with - * lowercase section and variable name - * baselen - pointer to size_t which will hold the length of the - * section + subsection part, can be NULL - */ -int git_config_parse_key(const char *key, char **store_key, size_t *baselen_) -{ - size_t i, baselen; - int dot; - const char *last_dot = strrchr(key, '.'); - - /* - * Since "key" actually contains the section name and the real - * key name separated by a dot, we have to know where the dot is. - */ - - if (last_dot == NULL || last_dot == key) { - error(_("key does not contain a section: %s"), key); - return -CONFIG_NO_SECTION_OR_NAME; - } - - if (!last_dot[1]) { - error(_("key does not contain variable name: %s"), key); - return -CONFIG_NO_SECTION_OR_NAME; - } - - baselen = last_dot - key; - if (baselen_) - *baselen_ = baselen; - - /* - * Validate the key and while at it, lower case it for matching. - */ - *store_key = xmallocz(strlen(key)); - - dot = 0; - for (i = 0; key[i]; i++) { - unsigned char c = key[i]; - if (c == '.') - dot = 1; - /* Leave the extended basename untouched.. */ - if (!dot || i > baselen) { - if (!iskeychar(c) || - (i == baselen + 1 && !isalpha(c))) { - error(_("invalid key: %s"), key); - goto out_free_ret_1; - } - c = tolower(c); - } else if (c == '\n') { - error(_("invalid key (newline): %s"), key); - goto out_free_ret_1; - } - (*store_key)[i] = c; - } - - return 0; - -out_free_ret_1: - FREE_AND_NULL(*store_key); - return -CONFIG_INVALID_KEY; -} - static int config_parse_pair(const char *key, const char *value, struct key_value_info *kvi, config_fn_t fn, void *data) @@ -786,296 +645,6 @@ int git_config_from_parameters(config_fn_t fn, void *data) return ret; } -static int get_next_char(struct config_source *cs) -{ - int c = cs->do_fgetc(cs); - - if (c == '\r') { - /* DOS like systems */ - c = cs->do_fgetc(cs); - if (c != '\n') { - if (c != EOF) - cs->do_ungetc(c, cs); - c = '\r'; - } - } - - if (c != EOF && ++cs->total_len > INT_MAX) { - /* - * This is an absurdly long config file; refuse to parse - * further in order to protect downstream code from integer - * overflows. Note that we can't return an error specifically, - * but we can mark EOF and put trash in the return value, - * which will trigger a parse error. - */ - cs->eof = 1; - return 0; - } - - if (c == '\n') - cs->linenr++; - if (c == EOF) { - cs->eof = 1; - cs->linenr++; - c = '\n'; - } - return c; -} - -static char *parse_value(struct config_source *cs) -{ - int quote = 0, comment = 0, space = 0; - - strbuf_reset(&cs->value); - for (;;) { - int c = get_next_char(cs); - if (c == '\n') { - if (quote) { - cs->linenr--; - return NULL; - } - return cs->value.buf; - } - if (comment) - continue; - if (isspace(c) && !quote) { - if (cs->value.len) - space++; - continue; - } - if (!quote) { - if (c == ';' || c == '#') { - comment = 1; - continue; - } - } - for (; space; space--) - strbuf_addch(&cs->value, ' '); - if (c == '\\') { - c = get_next_char(cs); - switch (c) { - case '\n': - continue; - case 't': - c = '\t'; - break; - case 'b': - c = '\b'; - break; - case 'n': - c = '\n'; - break; - /* Some characters escape as themselves */ - case '\\': case '"': - break; - /* Reject unknown escape sequences */ - default: - return NULL; - } - strbuf_addch(&cs->value, c); - continue; - } - if (c == '"') { - quote = 1-quote; - continue; - } - strbuf_addch(&cs->value, c); - } -} - -static int get_value(struct config_source *cs, struct key_value_info *kvi, - config_fn_t fn, void *data, struct strbuf *name) -{ - int c; - char *value; - int ret; - struct config_context ctx = { - .kvi = kvi, - }; - - /* Get the full name */ - for (;;) { - c = get_next_char(cs); - if (cs->eof) - break; - if (!iskeychar(c)) - break; - strbuf_addch(name, tolower(c)); - } - - while (c == ' ' || c == '\t') - c = get_next_char(cs); - - value = NULL; - if (c != '\n') { - if (c != '=') - return -1; - value = parse_value(cs); - if (!value) - return -1; - } - /* - * We already consumed the \n, but we need linenr to point to - * the line we just parsed during the call to fn to get - * accurate line number in error messages. - */ - cs->linenr--; - kvi->linenr = cs->linenr; - ret = fn(name->buf, value, &ctx, data); - if (ret >= 0) - cs->linenr++; - return ret; -} - -static int get_extended_base_var(struct config_source *cs, struct strbuf *name, - int c) -{ - cs->subsection_case_sensitive = 0; - do { - if (c == '\n') - goto error_incomplete_line; - c = get_next_char(cs); - } while (isspace(c)); - - /* We require the format to be '[base "extension"]' */ - if (c != '"') - return -1; - strbuf_addch(name, '.'); - - for (;;) { - int c = get_next_char(cs); - if (c == '\n') - goto error_incomplete_line; - if (c == '"') - break; - if (c == '\\') { - c = get_next_char(cs); - if (c == '\n') - goto error_incomplete_line; - } - strbuf_addch(name, c); - } - - /* Final ']' */ - if (get_next_char(cs) != ']') - return -1; - return 0; -error_incomplete_line: - cs->linenr--; - return -1; -} - -static int get_base_var(struct config_source *cs, struct strbuf *name) -{ - cs->subsection_case_sensitive = 1; - for (;;) { - int c = get_next_char(cs); - if (cs->eof) - return -1; - if (c == ']') - return 0; - if (isspace(c)) - return get_extended_base_var(cs, name, c); - if (!iskeychar(c) && c != '.') - return -1; - strbuf_addch(name, tolower(c)); - } -} - -struct parse_event_data { - enum config_event_t previous_type; - size_t previous_offset; - const struct config_parse_options *opts; -}; - -static size_t get_corrected_offset(struct config_source *cs, - enum config_event_t type) -{ - size_t offset = cs->do_ftell(cs); - - /* - * At EOF, the parser always "inserts" an extra '\n', therefore - * the end offset of the event is the current file position, otherwise - * we will already have advanced to the next event. - */ - if (type != CONFIG_EVENT_EOF) - offset--; - return offset; -} - -static void start_event(struct config_source *cs, enum config_event_t type, - struct parse_event_data *data) -{ - data->previous_type = type; - data->previous_offset = get_corrected_offset(cs, type); -} - -static int flush_event(struct config_source *cs, enum config_event_t type, - struct parse_event_data *data) -{ - if (!data->opts || !data->opts->event_fn) - return 0; - - if (type == CONFIG_EVENT_WHITESPACE && - data->previous_type == type) - return 0; - - if (data->previous_type != CONFIG_EVENT_EOF && - data->opts->event_fn(data->previous_type, data->previous_offset, - get_corrected_offset(cs, type), cs, - data->opts->event_fn_data) < 0) - return -1; - - return 1; -} - -static int do_event(struct config_source *cs, enum config_event_t type, - struct parse_event_data *data) -{ - int maybe_ret; - - if ((maybe_ret = flush_event(cs, type, data)) < 1) - return maybe_ret; - - start_event(cs, type, data); - - return 0; -} - -static int do_event_and_flush(struct config_source *cs, - enum config_event_t type, - struct parse_event_data *data) -{ - int maybe_ret; - - if ((maybe_ret = flush_event(cs, type, data)) < 1) - return maybe_ret; - - start_event(cs, type, data); - - if ((maybe_ret = flush_event(cs, type, data)) < 1) - return maybe_ret; - - /* - * Not actually EOF, but this indicates we don't have a valid event - * to flush next time around. - */ - data->previous_type = CONFIG_EVENT_EOF; - - return 0; -} - -static void kvi_from_source(struct config_source *cs, - enum config_scope scope, - struct key_value_info *out) -{ - out->filename = strintern(cs->name); - out->origin_type = cs->origin_type; - out->linenr = cs->linenr; - out->scope = scope; - out->path = cs->path; -} - int git_config_err_fn(enum config_event_t type, size_t begin_offset UNUSED, size_t end_offset UNUSED, struct config_source *cs, void *data) @@ -1126,98 +695,6 @@ int git_config_err_fn(enum config_event_t type, size_t begin_offset UNUSED, return error_return; } -static int git_parse_source(struct config_source *cs, config_fn_t fn, - struct key_value_info *kvi, void *data, - const struct config_parse_options *opts) -{ - int comment = 0; - size_t baselen = 0; - struct strbuf *var = &cs->var; - - /* U+FEFF Byte Order Mark in UTF8 */ - const char *bomptr = utf8_bom; - - /* For the parser event callback */ - struct parse_event_data event_data = { - CONFIG_EVENT_EOF, 0, opts - }; - - for (;;) { - int c; - - c = get_next_char(cs); - if (bomptr && *bomptr) { - /* We are at the file beginning; skip UTF8-encoded BOM - * if present. Sane editors won't put this in on their - * own, but e.g. Windows Notepad will do it happily. */ - if (c == (*bomptr & 0377)) { - bomptr++; - continue; - } else { - /* Do not tolerate partial BOM. */ - if (bomptr != utf8_bom) - break; - /* No BOM at file beginning. Cool. */ - bomptr = NULL; - } - } - if (c == '\n') { - if (cs->eof) { - if (do_event(cs, CONFIG_EVENT_EOF, &event_data) < 0) - return -1; - return 0; - } - if (do_event(cs, CONFIG_EVENT_WHITESPACE, &event_data) < 0) - return -1; - comment = 0; - continue; - } - if (comment) - continue; - if (isspace(c)) { - if (do_event(cs, CONFIG_EVENT_WHITESPACE, &event_data) < 0) - return -1; - continue; - } - if (c == '#' || c == ';') { - if (do_event(cs, CONFIG_EVENT_COMMENT, &event_data) < 0) - return -1; - comment = 1; - continue; - } - if (c == '[') { - if (do_event(cs, CONFIG_EVENT_SECTION, &event_data) < 0) - return -1; - - /* Reset prior to determining a new stem */ - strbuf_reset(var); - if (get_base_var(cs, var) < 0 || var->len < 1) - break; - strbuf_addch(var, '.'); - baselen = var->len; - continue; - } - if (!isalpha(c)) - break; - - if (do_event(cs, CONFIG_EVENT_ENTRY, &event_data) < 0) - return -1; - - /* - * Truncate the var name back to the section header - * stem prior to grabbing the suffix part of the name - * and the value. - */ - strbuf_setlen(var, baselen); - strbuf_addch(var, tolower(c)); - if (get_value(cs, kvi, fn, data, var) < 0) - break; - } - - do_event_and_flush(cs, CONFIG_EVENT_ERROR, &event_data); - return -1; -} - static uintmax_t get_unit_factor(const char *end) { if (!*end) @@ -2011,83 +1488,6 @@ int git_default_config(const char *var, const char *value, return 0; } -/* - * All source specific fields in the union, die_on_error, name and the callbacks - * fgetc, ungetc, ftell of top need to be initialized before calling - * this function. - */ -static int do_config_from(struct config_source *top, config_fn_t fn, - void *data, enum config_scope scope, - const struct config_parse_options *opts) -{ - struct key_value_info kvi = KVI_INIT; - int ret; - - /* push config-file parsing state stack */ - top->linenr = 1; - top->eof = 0; - top->total_len = 0; - strbuf_init(&top->value, 1024); - strbuf_init(&top->var, 1024); - kvi_from_source(top, scope, &kvi); - - ret = git_parse_source(top, fn, &kvi, data, opts); - - strbuf_release(&top->value); - strbuf_release(&top->var); - - return ret; -} - -static int do_config_from_file(config_fn_t fn, - const enum config_origin_type origin_type, - const char *name, const char *path, FILE *f, - void *data, enum config_scope scope, - const struct config_parse_options *opts) -{ - struct config_source top = CONFIG_SOURCE_INIT; - int ret; - - top.u.file = f; - top.origin_type = origin_type; - top.name = name; - top.path = path; - top.do_fgetc = config_file_fgetc; - top.do_ungetc = config_file_ungetc; - top.do_ftell = config_file_ftell; - - flockfile(f); - ret = do_config_from(&top, fn, data, scope, opts); - funlockfile(f); - return ret; -} - -static int git_config_from_stdin(config_fn_t fn, void *data, - enum config_scope scope, - const struct config_parse_options *config_opts) -{ - return do_config_from_file(fn, CONFIG_ORIGIN_STDIN, "", NULL, stdin, - data, scope, config_opts); -} - -int git_config_from_file_with_options(config_fn_t fn, const char *filename, - void *data, enum config_scope scope, - const struct config_parse_options *opts) -{ - int ret = -1; - FILE *f; - - if (!filename) - BUG("filename cannot be NULL"); - f = fopen_or_warn(filename, "r"); - if (f) { - ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename, - filename, f, data, scope, opts); - fclose(f); - } - return ret; -} - int git_config_from_file(config_fn_t fn, const char *filename, void *data) { struct config_parse_options config_opts = CP_OPTS_INIT(CONFIG_ERROR_DIE); @@ -2096,27 +1496,6 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data) CONFIG_SCOPE_UNKNOWN, &config_opts); } -int git_config_from_mem(config_fn_t fn, - const enum config_origin_type origin_type, - const char *name, const char *buf, size_t len, - void *data, enum config_scope scope, - const struct config_parse_options *opts) -{ - struct config_source top = CONFIG_SOURCE_INIT; - - top.u.buf.buf = buf; - top.u.buf.len = len; - top.u.buf.pos = 0; - top.origin_type = origin_type; - top.name = name; - top.path = NULL; - top.do_fgetc = config_buf_fgetc; - top.do_ungetc = config_buf_ungetc; - top.do_ftell = config_buf_ftell; - - return do_config_from(&top, fn, data, scope, opts); -} - int git_config_from_blob_oid(config_fn_t fn, const char *name, struct repository *repo, diff --git a/config.h b/config.h index 8ad399580f..3bad5e1c32 100644 --- a/config.h +++ b/config.h @@ -4,7 +4,7 @@ #include "hashmap.h" #include "string-list.h" #include "repository.h" - +#include "config-parse.h" /** * The config API gives callers a way to access Git configuration files @@ -23,9 +23,6 @@ struct object_id; -/* git_config_parse_key() returns these negated: */ -#define CONFIG_INVALID_KEY 1 -#define CONFIG_NO_SECTION_OR_NAME 2 /* git_config_set_gently(), git_config_set_multivar_gently() return the above or these: */ #define CONFIG_NO_LOCK -1 #define CONFIG_INVALID_FILE 3 @@ -36,17 +33,6 @@ struct object_id; #define CONFIG_REGEX_NONE ((void *)1) -enum config_scope { - CONFIG_SCOPE_UNKNOWN = 0, - CONFIG_SCOPE_SYSTEM, - CONFIG_SCOPE_GLOBAL, - CONFIG_SCOPE_LOCAL, - CONFIG_SCOPE_WORKTREE, - CONFIG_SCOPE_COMMAND, - CONFIG_SCOPE_SUBMODULE, -}; -const char *config_scope_name(enum config_scope scope); - struct git_config_source { unsigned int use_stdin:1; const char *file; @@ -54,46 +40,6 @@ struct git_config_source { enum config_scope scope; }; -enum config_origin_type { - CONFIG_ORIGIN_UNKNOWN = 0, - CONFIG_ORIGIN_BLOB, - CONFIG_ORIGIN_FILE, - CONFIG_ORIGIN_STDIN, - CONFIG_ORIGIN_SUBMODULE_BLOB, - CONFIG_ORIGIN_CMDLINE -}; - -enum config_event_t { - CONFIG_EVENT_SECTION, - CONFIG_EVENT_ENTRY, - CONFIG_EVENT_WHITESPACE, - CONFIG_EVENT_COMMENT, - CONFIG_EVENT_EOF, - CONFIG_EVENT_ERROR -}; - -struct config_source; -/* - * The parser event function (if not NULL) is called with the event type and - * the begin/end offsets of the parsed elements. - * - * Note: for CONFIG_EVENT_ENTRY (i.e. config variables), the trailing newline - * character is considered part of the element. - */ -typedef int (*config_parser_event_fn_t)(enum config_event_t type, - size_t begin_offset, size_t end_offset, - struct config_source *cs, - void *event_fn_data); - -struct config_parse_options { - /* - * event_fn and event_fn_data are for internal use only. Handles events - * emitted by the config parser. - */ - config_parser_event_fn_t event_fn; - void *event_fn_data; -}; - #define CP_OPTS_INIT(error_action) { \ .event_fn = git_config_err_fn, \ .event_fn_data = (enum config_error_action []){(error_action)}, \ @@ -126,59 +72,8 @@ enum config_error_action { int git_config_err_fn(enum config_event_t type, size_t begin_offset, size_t end_offset, struct config_source *cs, void *event_fn_data); - -/* Config source metadata for a given config key-value pair */ -struct key_value_info { - const char *filename; - int linenr; - enum config_origin_type origin_type; - enum config_scope scope; - const char *path; -}; -#define KVI_INIT { \ - .filename = NULL, \ - .linenr = -1, \ - .origin_type = CONFIG_ORIGIN_UNKNOWN, \ - .scope = CONFIG_SCOPE_UNKNOWN, \ - .path = NULL, \ -} - -/* Captures additional information that a config callback can use. */ -struct config_context { - /* Config source metadata for key and value. */ - const struct key_value_info *kvi; -}; -#define CONFIG_CONTEXT_INIT { 0 } - -/** - * A config callback function takes four parameters: - * - * - the name of the parsed variable. This is in canonical "flat" form: the - * section, subsection, and variable segments will be separated by dots, - * and the section and variable segments will be all lowercase. E.g., - * `core.ignorecase`, `diff.SomeType.textconv`. - * - * - the value of the found variable, as a string. If the variable had no - * value specified, the value will be NULL (typically this means it - * should be interpreted as boolean true). - * - * - the 'config context', that is, additional information about the config - * iteration operation provided by the config machinery. For example, this - * includes information about the config source being parsed (e.g. the - * filename). - * - * - a void pointer passed in by the caller of the config API; this can - * contain callback-specific data - * - * A config callback should return 0 for success, or -1 if the variable - * could not be parsed properly. - */ -typedef int (*config_fn_t)(const char *, const char *, - const struct config_context *, void *); - int git_default_config(const char *, const char *, const struct config_context *, void *); - /** * Read a specific file in git-config format. * This function takes the same callback and data parameters as `git_config`. @@ -186,16 +81,6 @@ int git_default_config(const char *, const char *, * Unlike git_config(), this function does not respect includes. */ int git_config_from_file(config_fn_t fn, const char *, void *); - -int git_config_from_file_with_options(config_fn_t fn, const char *, - void *, enum config_scope, - const struct config_parse_options *); -int git_config_from_mem(config_fn_t fn, - const enum config_origin_type, - const char *name, - const char *buf, size_t len, - void *data, enum config_scope scope, - const struct config_parse_options *opts); int git_config_from_blob_oid(config_fn_t fn, const char *name, struct repository *repo, const struct object_id *oid, void *data, @@ -333,8 +218,6 @@ int repo_config_set_worktree_gently(struct repository *, const char *, const cha */ void git_config_set(const char *, const char *); -int git_config_parse_key(const char *, char **, size_t *); - /* * The following macros specify flag bits that alter the behavior * of the git_config_set_multivar*() methods.