From patchwork Thu Oct 28 18:30:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12590793 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5E240C433FE for ; Thu, 28 Oct 2021 18:31:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 44A5C610EA for ; Thu, 28 Oct 2021 18:31:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231140AbhJ1Sdo (ORCPT ); Thu, 28 Oct 2021 14:33:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229645AbhJ1Sdi (ORCPT ); Thu, 28 Oct 2021 14:33:38 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC41EC061767 for ; Thu, 28 Oct 2021 11:31:11 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id hg3-20020a17090b300300b001a3fd07578aso3506193pjb.6 for ; Thu, 28 Oct 2021 11:31:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=/jW7Z4NQq8oD4q7raYAzyIvk3oVztHUbLVm4scQCQSE=; b=DzAbKNSU1i3M51qXOrhDTUNBZEQnZOcsYB1CvigpY7XWwKYnLU4l0Yj/xPTwqaJE0s BldJNH36GUcQjlea4AOCBnqe4iOef8IqXHh8RwIWp90TLb7M4Wabu/cwNcE3OFKFoQJs rPc7l3s8tRrnx5kr/MvL/M4uPqiMK6dCp7LQoA/Us1QIpl7xcs9Ki7gNVLJkKTJrpaRZ jSUPF3GG5JLwpgMLs0q4awMwtwT2x6CHV2025ZDaJjQ2l86ZCKT9uum/7UPbCM1s/Ckb qURSAfyHP5IgXGdp5z5FMPgpZRRXYDLX6NUxBWMgOMactUVBtK8v/jAyNsgE0yl6vRN4 ZxyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=/jW7Z4NQq8oD4q7raYAzyIvk3oVztHUbLVm4scQCQSE=; b=DgmpXR9g1Jst7cm9knadeI+ZAYfpyqFDabZiTVvVyEZ5llOg/KiFDU2ZvfkDwXG+/V pRvhLl49hyq5o0J2paN0lsUvZFchOMJufhf/mzkqII2cRr2avv3P+fupUyPIrgz0BG2x loffBN++q8CptPZDsAfyg0meFZXO+1OdTk5qNWyvY8Ow3Jmm7IqjPpMmeT4tvxCpFZR2 YepMyepQSlpJKafb/D3bOJDRpuzCdDJLB+oblw6A2zx9BxQLZ+qRDq2vSI8yUelJgGGS 9j4tTj8KYzjtiOzsZsxOVjze6EGwu5XgbVjDLDuh11Bw1/tCjBtHG5lTqPRzludGTB86 GfoQ== X-Gm-Message-State: AOAM533QyTEbbUx/qKkG2tvTmRrMZm5RtC3aEJoBfIrnV2g6Jzwq5yOF 0bvFwvQ4EeUS/v9LR068OHf5jqymiIks0z4y8fqnS1sl1K/Bv+L3ZN8kawBdwvIDm6o1GdT/lJ/ bTrScH0kAJTNDEN4IWE7YNoEAAs1K0TKS/DLFXa8AKn3sqLQwZrNbHxQechrHC/M= X-Google-Smtp-Source: ABdhPJwv6uEaskIauKRAWsHCs2kkNgJ18DVWllpWp+5Lhx4+6USWPKdsjmc1EFjTe+8qbORAK9XP6KPWnVJP1Q== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a17:902:ea09:b0:13f:ac2:c5ae with SMTP id s9-20020a170902ea0900b0013f0ac2c5aemr5292139plg.3.1635445871194; Thu, 28 Oct 2021 11:31:11 -0700 (PDT) Date: Thu, 28 Oct 2021 11:30:56 -0700 In-Reply-To: <20211028183101.41013-1-chooglen@google.com> Message-Id: <20211028183101.41013-2-chooglen@google.com> Mime-Version: 1.0 References: <20211019224339.61881-1-chooglen@google.com> <20211028183101.41013-1-chooglen@google.com> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog Subject: [PATCH v4 1/6] t5516: add test case for pushing remote refspecs From: Glen Choo To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org In detached HEAD, "git push remote-name" should push the refspecs in remote.remote-name.push. Since there is no test case that checks this behavior, add one. Signed-off-by: Glen Choo --- t/t5516-fetch-push.sh | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh index 8212ca56dc..d4c2f1b68f 100755 --- a/t/t5516-fetch-push.sh +++ b/t/t5516-fetch-push.sh @@ -541,6 +541,15 @@ do done +test_expect_success "push to remote with detached HEAD and config remote.*.push = src:dest" ' + mk_test testrepo heads/main && + git checkout $the_first_commit && + test_config remote.there.url testrepo && + test_config remote.there.push refs/heads/main:refs/heads/main && + git push there && + check_push_result testrepo $the_commit heads/main +' + test_expect_success 'push with remote.pushdefault' ' mk_test up_repo heads/main && mk_test down_repo heads/main && From patchwork Thu Oct 28 18:30:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12590797 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C1C9DC433F5 for ; Thu, 28 Oct 2021 18:31:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9EC66610D2 for ; Thu, 28 Oct 2021 18:31:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231160AbhJ1Sdr (ORCPT ); Thu, 28 Oct 2021 14:33:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231124AbhJ1Sdk (ORCPT ); Thu, 28 Oct 2021 14:33:40 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F1EAC061570 for ; Thu, 28 Oct 2021 11:31:13 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id n9-20020a170902968900b0013f23b51142so3228613plp.8 for ; Thu, 28 Oct 2021 11:31:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=3KQ2sObVXYjj+dKwSYx0p6BoQGQt7qg8GvzWpJYqYAM=; b=Czg1KT6UEeVTvcw6nbD2EUqHTT4mfyHl/Z+spesRNxy+ycRtIbFmtW6rDd0th51lYX FES3PVw+oevnKTqH5ay8ZBU7oqRxmGCCRs5x5u5iKDoMlJeoCw/F5OZ3pe6irh45k9VH gVdiOE6PidxlPceugKbq+j7HvO+1mDnixZPg0GIld/nfX42y9/CL5yUsGKrBNQkgEtZS Ii+JEFgxAesyU16m6DoavA5klrOnnJIdZHIecs11qmmZbXcRvHb3oYhF+PTbu+4kdpBR jOO7ocG2RDqmMIbVYGiH42KVzCT2M7k7WqmYOmGCvSSFm8rRLomW/CNHmCj2OTKuEX0L pBcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=3KQ2sObVXYjj+dKwSYx0p6BoQGQt7qg8GvzWpJYqYAM=; b=ICuxCCK+GuhhL4iyjdmXHzopgxNVOmQ57KRtoanfLOMM5YUHdtSGPVwKYyew9upoI7 4wGT7kqvPuRF2gnSMlPnGgGKB+/j69dRvKMnxL8vegTTVsq7+oQFd5kQopaJFURDQ/rr lrsZm6pcwlOh7lBiK8tZZV1aLlGavcyWoSWlTGJ4FFrLcCYGz2lVizq1B10Gvjeg+CmN Rr8VRYHnMRubUs1BqC73AaFYea3KDqFz7fPZoz0T8nUpxJ2lKJYZM3IM2MjWc+POAPmJ ldbJB1B4G7iKXvaGGafu/uTQl5K1jXgIyYwvF13RH7+Jl3I7/iOhM5x+4VUVMVdO0PbU Kkvg== X-Gm-Message-State: AOAM531wwVHgzM9ZtuYivs2XcJkXTSIWl60+BewTLFC7uCWK4AHdq/kJ yb4mxlTeRTzWevcUkd2Yu3lwYoQJ4bM3YJfXdodmuHMKSF7d7BIJ4p8snn2JobtC75oUmgNjLZO yGIM/0jXtYjoIXRPdYkj0Ha7FoOZkf/7VtrmxTl4fnnUo+Z5IUXp//6jEysmM6Jo= X-Google-Smtp-Source: ABdhPJynEON2UC1QV4gj4E7aI42l2B9qhK63Dfs7VbuQgKDoCo63ZI6n0UYNDmonf+H1NJgxRLEoFyTrp8Mj9w== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a17:902:a983:b0:141:9407:1fb3 with SMTP id bh3-20020a170902a98300b0014194071fb3mr3669544plb.35.1635445873071; Thu, 28 Oct 2021 11:31:13 -0700 (PDT) Date: Thu, 28 Oct 2021 11:30:57 -0700 In-Reply-To: <20211028183101.41013-1-chooglen@google.com> Message-Id: <20211028183101.41013-3-chooglen@google.com> Mime-Version: 1.0 References: <20211019224339.61881-1-chooglen@google.com> <20211028183101.41013-1-chooglen@google.com> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog Subject: [PATCH v4 2/6] remote: move static variables into per-repository struct From: Glen Choo To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org remote.c does not work with non-the_repository because it stores its state as static variables. To support non-the_repository, we can use a per-repository struct for the remotes subsystem. Prepare for this change by defining a struct remote_state that holds the remotes subsystem state and move the static variables of remote.c into the_repository->remote_state. This introduces no behavioral or API changes. Signed-off-by: Glen Choo --- remote.c | 191 +++++++++++++++++++++++++++++++++------------------ remote.h | 34 +++++++++ repository.c | 8 +++ repository.h | 4 ++ 4 files changed, 170 insertions(+), 67 deletions(-) diff --git a/remote.c b/remote.c index f958543d70..c2bb11242b 100644 --- a/remote.c +++ b/remote.c @@ -21,33 +21,6 @@ struct counted_string { size_t len; const char *s; }; -struct rewrite { - const char *base; - size_t baselen; - struct counted_string *instead_of; - int instead_of_nr; - int instead_of_alloc; -}; -struct rewrites { - struct rewrite **rewrite; - int rewrite_alloc; - int rewrite_nr; -}; - -static struct remote **remotes; -static int remotes_alloc; -static int remotes_nr; -static struct hashmap remotes_hash; - -static struct branch **branches; -static int branches_alloc; -static int branches_nr; - -static struct branch *current_branch; -static const char *pushremote_name; - -static struct rewrites rewrites; -static struct rewrites rewrites_push; static int valid_remote(const struct remote *remote) { @@ -94,14 +67,16 @@ static void add_pushurl(struct remote *remote, const char *pushurl) static void add_pushurl_alias(struct remote *remote, const char *url) { - const char *pushurl = alias_url(url, &rewrites_push); + const char *pushurl = + alias_url(url, &the_repository->remote_state->rewrites_push); if (pushurl != url) add_pushurl(remote, pushurl); } static void add_url_alias(struct remote *remote, const char *url) { - add_url(remote, alias_url(url, &rewrites)); + add_url(remote, + alias_url(url, &the_repository->remote_state->rewrites)); add_pushurl_alias(remote, url); } @@ -127,12 +102,6 @@ static int remotes_hash_cmp(const void *unused_cmp_data, return strcmp(a->name, b->name); } -static inline void init_remotes_hash(void) -{ - if (!remotes_hash.cmpfn) - hashmap_init(&remotes_hash, remotes_hash_cmp, NULL, 0); -} - static struct remote *make_remote(const char *name, int len) { struct remote *ret; @@ -142,12 +111,12 @@ static struct remote *make_remote(const char *name, int len) if (!len) len = strlen(name); - init_remotes_hash(); lookup.str = name; lookup.len = len; hashmap_entry_init(&lookup_entry, memhash(name, len)); - e = hashmap_get(&remotes_hash, &lookup_entry, &lookup); + e = hashmap_get(&the_repository->remote_state->remotes_hash, + &lookup_entry, &lookup); if (e) return container_of(e, struct remote, ent); @@ -158,15 +127,41 @@ static struct remote *make_remote(const char *name, int len) refspec_init(&ret->push, REFSPEC_PUSH); refspec_init(&ret->fetch, REFSPEC_FETCH); - ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc); - remotes[remotes_nr++] = ret; + ALLOC_GROW(the_repository->remote_state->remotes, + the_repository->remote_state->remotes_nr + 1, + the_repository->remote_state->remotes_alloc); + the_repository->remote_state + ->remotes[the_repository->remote_state->remotes_nr++] = ret; hashmap_entry_init(&ret->ent, lookup_entry.hash); - if (hashmap_put_entry(&remotes_hash, ret, ent)) + if (hashmap_put_entry(&the_repository->remote_state->remotes_hash, ret, + ent)) BUG("hashmap_put overwrote entry after hashmap_get returned NULL"); return ret; } +static void remote_clear(struct remote *remote) +{ + int i; + + free((char *)remote->name); + free((char *)remote->foreign_vcs); + + for (i = 0; i < remote->url_nr; i++) { + free((char *)remote->url[i]); + } + FREE_AND_NULL(remote->pushurl); + + for (i = 0; i < remote->pushurl_nr; i++) { + free((char *)remote->pushurl[i]); + } + FREE_AND_NULL(remote->pushurl); + free((char *)remote->receivepack); + free((char *)remote->uploadpack); + FREE_AND_NULL(remote->http_proxy); + FREE_AND_NULL(remote->http_proxy_authmethod); +} + static void add_merge(struct branch *branch, const char *name) { ALLOC_GROW(branch->merge_name, branch->merge_nr + 1, @@ -179,15 +174,20 @@ static struct branch *make_branch(const char *name, size_t len) struct branch *ret; int i; - for (i = 0; i < branches_nr; i++) { - if (!strncmp(name, branches[i]->name, len) && - !branches[i]->name[len]) - return branches[i]; + for (i = 0; i < the_repository->remote_state->branches_nr; i++) { + if (!strncmp(name, + the_repository->remote_state->branches[i]->name, + len) && + !the_repository->remote_state->branches[i]->name[len]) + return the_repository->remote_state->branches[i]; } - ALLOC_GROW(branches, branches_nr + 1, branches_alloc); + ALLOC_GROW(the_repository->remote_state->branches, + the_repository->remote_state->branches_nr + 1, + the_repository->remote_state->branches_alloc); CALLOC_ARRAY(ret, 1); - branches[branches_nr++] = ret; + the_repository->remote_state + ->branches[the_repository->remote_state->branches_nr++] = ret; ret->name = xstrndup(name, len); ret->refname = xstrfmt("refs/heads/%s", ret->name); @@ -327,12 +327,16 @@ static int handle_config(const char *key, const char *value, void *cb) if (!strcmp(subkey, "insteadof")) { if (!value) return config_error_nonbool(key); - rewrite = make_rewrite(&rewrites, name, namelen); + rewrite = make_rewrite( + &the_repository->remote_state->rewrites, name, + namelen); add_instead_of(rewrite, xstrdup(value)); } else if (!strcmp(subkey, "pushinsteadof")) { if (!value) return config_error_nonbool(key); - rewrite = make_rewrite(&rewrites_push, name, namelen); + rewrite = make_rewrite( + &the_repository->remote_state->rewrites_push, + name, namelen); add_instead_of(rewrite, xstrdup(value)); } } @@ -342,7 +346,9 @@ static int handle_config(const char *key, const char *value, void *cb) /* Handle remote.* variables */ if (!name && !strcmp(subkey, "pushdefault")) - return git_config_string(&pushremote_name, key, value); + return git_config_string( + &the_repository->remote_state->pushremote_name, key, + value); if (!name) return 0; @@ -425,18 +431,34 @@ static int handle_config(const char *key, const char *value, void *cb) static void alias_all_urls(void) { int i, j; - for (i = 0; i < remotes_nr; i++) { + for (i = 0; i < the_repository->remote_state->remotes_nr; i++) { int add_pushurl_aliases; - if (!remotes[i]) + if (!the_repository->remote_state->remotes[i]) continue; - for (j = 0; j < remotes[i]->pushurl_nr; j++) { - remotes[i]->pushurl[j] = alias_url(remotes[i]->pushurl[j], &rewrites); + for (j = 0; + j < the_repository->remote_state->remotes[i]->pushurl_nr; + j++) { + the_repository->remote_state->remotes[i]->pushurl[j] = + alias_url( + the_repository->remote_state->remotes[i] + ->pushurl[j], + &the_repository->remote_state->rewrites); } - add_pushurl_aliases = remotes[i]->pushurl_nr == 0; - for (j = 0; j < remotes[i]->url_nr; j++) { + add_pushurl_aliases = + the_repository->remote_state->remotes[i]->pushurl_nr == + 0; + for (j = 0; + j < the_repository->remote_state->remotes[i]->url_nr; + j++) { if (add_pushurl_aliases) - add_pushurl_alias(remotes[i], remotes[i]->url[j]); - remotes[i]->url[j] = alias_url(remotes[i]->url[j], &rewrites); + add_pushurl_alias( + the_repository->remote_state->remotes[i], + the_repository->remote_state->remotes[i] + ->url[j]); + the_repository->remote_state->remotes[i] + ->url[j] = alias_url( + the_repository->remote_state->remotes[i]->url[j], + &the_repository->remote_state->rewrites); } } } @@ -450,12 +472,13 @@ static void read_config(void) return; loaded = 1; - current_branch = NULL; + the_repository->remote_state->current_branch = NULL; if (startup_info->have_repository) { const char *head_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flag); if (head_ref && (flag & REF_ISSYMREF) && skip_prefix(head_ref, "refs/heads/", &head_ref)) { - current_branch = make_branch(head_ref, strlen(head_ref)); + the_repository->remote_state->current_branch = + make_branch(head_ref, strlen(head_ref)); } } git_config(handle_config, NULL); @@ -493,10 +516,10 @@ const char *pushremote_for_branch(struct branch *branch, int *explicit) *explicit = 1; return branch->pushremote_name; } - if (pushremote_name) { + if (the_repository->remote_state->pushremote_name) { if (explicit) *explicit = 1; - return pushremote_name; + return the_repository->remote_state->pushremote_name; } return remote_for_branch(branch, explicit); } @@ -534,7 +557,8 @@ static struct remote *remote_get_1(const char *name, if (name) name_given = 1; else - name = get_default(current_branch, &name_given); + name = get_default(the_repository->remote_state->current_branch, + &name_given); ret = make_remote(name, 0); if (valid_remote_nick(name) && have_git_dir()) { @@ -573,11 +597,13 @@ int for_each_remote(each_remote_fn fn, void *priv) { int i, result = 0; read_config(); - for (i = 0; i < remotes_nr && !result; i++) { - struct remote *r = remotes[i]; - if (!r) + for (i = 0; i < the_repository->remote_state->remotes_nr && !result; + i++) { + struct remote *remote = + the_repository->remote_state->remotes[i]; + if (!remote) continue; - result = fn(r, priv); + result = fn(remote, priv); } return result; } @@ -1685,7 +1711,7 @@ struct branch *branch_get(const char *name) read_config(); if (!name || !*name || !strcmp(name, "HEAD")) - ret = current_branch; + ret = the_repository->remote_state->current_branch; else ret = make_branch(name, strlen(name)); set_merge(ret); @@ -2585,3 +2611,34 @@ void apply_push_cas(struct push_cas_option *cas, check_if_includes_upstream(ref); } } + +struct remote_state *remote_state_new(void) +{ + struct remote_state *r = xmalloc(sizeof(*r)); + + memset(r, 0, sizeof(*r)); + + hashmap_init(&r->remotes_hash, remotes_hash_cmp, NULL, 0); + return r; +} + +void remote_state_clear(struct remote_state *remote_state) +{ + int i; + + for (i = 0; i < remote_state->remotes_nr; i++) { + remote_clear(remote_state->remotes[i]); + } + FREE_AND_NULL(remote_state->remotes); + remote_state->remotes_alloc = 0; + remote_state->remotes_nr = 0; + + hashmap_clear_and_free(&remote_state->remotes_hash, struct remote, ent); + + for (i = 0; i < remote_state->branches_nr; i++) { + FREE_AND_NULL(remote_state->branches[i]); + } + FREE_AND_NULL(remote_state->branches); + remote_state->branches_alloc = 0; + remote_state->branches_nr = 0; +} diff --git a/remote.h b/remote.h index 5a59198252..d21c035f1b 100644 --- a/remote.h +++ b/remote.h @@ -23,6 +23,40 @@ enum { REMOTE_BRANCHES }; +struct rewrite { + const char *base; + size_t baselen; + struct counted_string *instead_of; + int instead_of_nr; + int instead_of_alloc; +}; + +struct rewrites { + struct rewrite **rewrite; + int rewrite_alloc; + int rewrite_nr; +}; + +struct remote_state { + struct remote **remotes; + int remotes_alloc; + int remotes_nr; + struct hashmap remotes_hash; + + struct branch **branches; + int branches_alloc; + int branches_nr; + + struct branch *current_branch; + const char *pushremote_name; + + struct rewrites rewrites; + struct rewrites rewrites_push; +}; + +void remote_state_clear(struct remote_state *remote_state); +struct remote_state *remote_state_new(void); + struct remote { struct hashmap_entry ent; diff --git a/repository.c b/repository.c index c5b90ba93e..c7ea706c20 100644 --- a/repository.c +++ b/repository.c @@ -9,6 +9,7 @@ #include "config.h" #include "object.h" #include "lockfile.h" +#include "remote.h" #include "submodule-config.h" #include "sparse-index.h" #include "promisor-remote.h" @@ -24,6 +25,7 @@ void initialize_the_repository(void) the_repo.index = &the_index; the_repo.objects = raw_object_store_new(); + the_repo.remote_state = remote_state_new(); the_repo.parsed_objects = parsed_object_pool_new(); repo_set_hash_algo(&the_repo, GIT_HASH_SHA1); @@ -164,6 +166,7 @@ int repo_init(struct repository *repo, repo->objects = raw_object_store_new(); repo->parsed_objects = parsed_object_pool_new(); + repo->remote_state = remote_state_new(); if (repo_init_gitdir(repo, gitdir)) goto error; @@ -270,6 +273,11 @@ void repo_clear(struct repository *repo) promisor_remote_clear(repo->promisor_remote_config); FREE_AND_NULL(repo->promisor_remote_config); } + + if (repo->remote_state) { + remote_state_clear(repo->remote_state); + FREE_AND_NULL(repo->remote_state); + } } int repo_read_index(struct repository *repo) diff --git a/repository.h b/repository.h index a057653981..98f9583470 100644 --- a/repository.h +++ b/repository.h @@ -11,6 +11,7 @@ struct pathspec; struct raw_object_store; struct submodule_cache; struct promisor_remote_config; +struct remote_state; enum untracked_cache_setting { UNTRACKED_CACHE_KEEP, @@ -127,6 +128,9 @@ struct repository { */ struct index_state *index; + /* Repository's remotes and associated structures. */ + struct remote_state *remote_state; + /* Repository's current hash algorithm, as serialized on disk. */ const struct git_hash_algo *hash_algo; From patchwork Thu Oct 28 18:30:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12590799 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D99A3C433EF for ; Thu, 28 Oct 2021 18:31:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BECCE610C8 for ; Thu, 28 Oct 2021 18:31:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231175AbhJ1Sdr (ORCPT ); Thu, 28 Oct 2021 14:33:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231132AbhJ1Sdm (ORCPT ); Thu, 28 Oct 2021 14:33:42 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 957E2C061745 for ; Thu, 28 Oct 2021 11:31:15 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id a191-20020a621ac8000000b0047c1b9c2a4bso3711729pfa.19 for ; Thu, 28 Oct 2021 11:31:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=w9nd/yTJfBRkjUwg1AqgIGDRGZdH94hqIqwJjWK8730=; b=tNbBgysO8rFF3gY5gFp48A2SRUj+ieCTAuRbQtjuwfrf/Ph5YUiBn8QGW+WSmWiy0Q yn+KkGzfiCQdkZC9pgdhV/MJl4LsfmlUw8hKXeXt3Mn33gdOWJE+dI6GyMgaow+RtFCa 1weSmEmwtWF8v1mswyHCzNtxTj/hDaCWHdNfuSfZxlNZT2k48lfO8UqhWmYpkr1IveCI S9uKOYLE9z+guDOObAEVfx4keS8M57BUZzS5AsCw8PTdVZdXZOlHLgrSitP5D4gjZEIL omc6Slf4xOZsyErDvedgFdHpcTXrfqVtzKV073eJLqbIdvdrgqoNSKSiQp1Urrk7tlVn Xnlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=w9nd/yTJfBRkjUwg1AqgIGDRGZdH94hqIqwJjWK8730=; b=xopUb1MLf20iNeeVuTnkPqtftdqY0iy4YoDwoY+9D7xo77Z7lg8PgPzMymF6KrxT1c iGRw8JNLtTPhXi/jcjoQgeqcahAYHN7LQWBlbPwn0/JbLW04pKlR/3Ld1JEcQtWSgH4w osEK2ELnltNO3LwZiIdN4MUC5bBfthShkEmbxL52EKzI3WDNDT4Q8AmTVITo22TdnzlH rQ+HSKrmaphQyUwiM7Tdq8ZIuvxQFTT+7Y9qFwh1V84+U26b2Xs62PWcI9+9amtADEte 5lLbgceL23ICntWe/De4fgCSKX334BXyG9SgJbyeqaMV1X+lNt5vOeG7k+kDhgcz+hkV D5Zg== X-Gm-Message-State: AOAM533zy2jkElhKQKVotcbXRsp/VqxmEtcu63ZgpI/FIZlNiKEBjgmX uoXw84QwBqBrFde2UTZh83Nk6ABKfE1cYK5KWxdnXBRyiZiE8oRHEjZYBfIKkw03KLr1sl8g8de 9ZWTD0/9kqX3LZob2UMlUcN7EAx5BFe6N/SjKxWuPxVVT97a4iYnPkNl/39fxqBc= X-Google-Smtp-Source: ABdhPJzFhy010FDpSHEvXG9/TiNS0hpCwY0D0n2BtTnMolO8b8liQbR+1UsUiJ70eej4PpSUSvWZBWxUu+RQiQ== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a05:6a00:1995:b0:47c:1f59:a27a with SMTP id d21-20020a056a00199500b0047c1f59a27amr5894131pfl.36.1635445874947; Thu, 28 Oct 2021 11:31:14 -0700 (PDT) Date: Thu, 28 Oct 2021 11:30:58 -0700 In-Reply-To: <20211028183101.41013-1-chooglen@google.com> Message-Id: <20211028183101.41013-4-chooglen@google.com> Mime-Version: 1.0 References: <20211019224339.61881-1-chooglen@google.com> <20211028183101.41013-1-chooglen@google.com> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog Subject: [PATCH v4 3/6] remote: use remote_state parameter internally From: Glen Choo To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Without changing external-facing functions, replace the_repository->remote_state internally by adding a struct remote_state parameter. As a result, external-facing functions are still tied to the_repository, but most static functions no longer reference the_repository->remote_state. The exceptions are those that are used in a way that depends on external-facing functions e.g. the callbacks to remote_get_1(). Signed-off-by: Glen Choo --- remote.c | 159 +++++++++++++++++++++++++------------------------------ remote.h | 2 + 2 files changed, 75 insertions(+), 86 deletions(-) diff --git a/remote.c b/remote.c index c2bb11242b..48374cc0e2 100644 --- a/remote.c +++ b/remote.c @@ -65,19 +65,19 @@ static void add_pushurl(struct remote *remote, const char *pushurl) remote->pushurl[remote->pushurl_nr++] = pushurl; } -static void add_pushurl_alias(struct remote *remote, const char *url) +static void add_pushurl_alias(struct remote_state *remote_state, + struct remote *remote, const char *url) { - const char *pushurl = - alias_url(url, &the_repository->remote_state->rewrites_push); + const char *pushurl = alias_url(url, &remote_state->rewrites_push); if (pushurl != url) add_pushurl(remote, pushurl); } -static void add_url_alias(struct remote *remote, const char *url) +static void add_url_alias(struct remote_state *remote_state, + struct remote *remote, const char *url) { - add_url(remote, - alias_url(url, &the_repository->remote_state->rewrites)); - add_pushurl_alias(remote, url); + add_url(remote, alias_url(url, &remote_state->rewrites)); + add_pushurl_alias(remote_state, remote, url); } struct remotes_hash_key { @@ -102,7 +102,8 @@ static int remotes_hash_cmp(const void *unused_cmp_data, return strcmp(a->name, b->name); } -static struct remote *make_remote(const char *name, int len) +static struct remote *make_remote(struct remote_state *remote_state, + const char *name, int len) { struct remote *ret; struct remotes_hash_key lookup; @@ -115,8 +116,7 @@ static struct remote *make_remote(const char *name, int len) lookup.len = len; hashmap_entry_init(&lookup_entry, memhash(name, len)); - e = hashmap_get(&the_repository->remote_state->remotes_hash, - &lookup_entry, &lookup); + e = hashmap_get(&remote_state->remotes_hash, &lookup_entry, &lookup); if (e) return container_of(e, struct remote, ent); @@ -127,15 +127,12 @@ static struct remote *make_remote(const char *name, int len) refspec_init(&ret->push, REFSPEC_PUSH); refspec_init(&ret->fetch, REFSPEC_FETCH); - ALLOC_GROW(the_repository->remote_state->remotes, - the_repository->remote_state->remotes_nr + 1, - the_repository->remote_state->remotes_alloc); - the_repository->remote_state - ->remotes[the_repository->remote_state->remotes_nr++] = ret; + ALLOC_GROW(remote_state->remotes, remote_state->remotes_nr + 1, + remote_state->remotes_alloc); + remote_state->remotes[remote_state->remotes_nr++] = ret; hashmap_entry_init(&ret->ent, lookup_entry.hash); - if (hashmap_put_entry(&the_repository->remote_state->remotes_hash, ret, - ent)) + if (hashmap_put_entry(&remote_state->remotes_hash, ret, ent)) BUG("hashmap_put overwrote entry after hashmap_get returned NULL"); return ret; } @@ -169,25 +166,22 @@ static void add_merge(struct branch *branch, const char *name) branch->merge_name[branch->merge_nr++] = name; } -static struct branch *make_branch(const char *name, size_t len) +static struct branch *make_branch(struct remote_state *remote_state, + const char *name, size_t len) { struct branch *ret; int i; - for (i = 0; i < the_repository->remote_state->branches_nr; i++) { - if (!strncmp(name, - the_repository->remote_state->branches[i]->name, - len) && - !the_repository->remote_state->branches[i]->name[len]) - return the_repository->remote_state->branches[i]; + for (i = 0; i < remote_state->branches_nr; i++) { + if (!strncmp(name, remote_state->branches[i]->name, len) && + !remote_state->branches[i]->name[len]) + return remote_state->branches[i]; } - ALLOC_GROW(the_repository->remote_state->branches, - the_repository->remote_state->branches_nr + 1, - the_repository->remote_state->branches_alloc); + ALLOC_GROW(remote_state->branches, remote_state->branches_nr + 1, + remote_state->branches_alloc); CALLOC_ARRAY(ret, 1); - the_repository->remote_state - ->branches[the_repository->remote_state->branches_nr++] = ret; + remote_state->branches[remote_state->branches_nr++] = ret; ret->name = xstrndup(name, len); ret->refname = xstrfmt("refs/heads/%s", ret->name); @@ -229,7 +223,8 @@ static const char *skip_spaces(const char *s) return s; } -static void read_remotes_file(struct remote *remote) +static void read_remotes_file(struct remote_state *remote_state, + struct remote *remote) { struct strbuf buf = STRBUF_INIT; FILE *f = fopen_or_warn(git_path("remotes/%s", remote->name), "r"); @@ -244,7 +239,8 @@ static void read_remotes_file(struct remote *remote) strbuf_rtrim(&buf); if (skip_prefix(buf.buf, "URL:", &v)) - add_url_alias(remote, xstrdup(skip_spaces(v))); + add_url_alias(remote_state, remote, + xstrdup(skip_spaces(v))); else if (skip_prefix(buf.buf, "Push:", &v)) refspec_append(&remote->push, skip_spaces(v)); else if (skip_prefix(buf.buf, "Pull:", &v)) @@ -254,7 +250,8 @@ static void read_remotes_file(struct remote *remote) fclose(f); } -static void read_branches_file(struct remote *remote) +static void read_branches_file(struct remote_state *remote_state, + struct remote *remote) { char *frag; struct strbuf buf = STRBUF_INIT; @@ -286,7 +283,7 @@ static void read_branches_file(struct remote *remote) else frag = (char *)git_default_branch_name(0); - add_url_alias(remote, strbuf_detach(&buf, NULL)); + add_url_alias(remote_state, remote, strbuf_detach(&buf, NULL)); refspec_appendf(&remote->fetch, "refs/heads/%s:refs/heads/%s", frag, remote->name); @@ -305,10 +302,12 @@ static int handle_config(const char *key, const char *value, void *cb) const char *subkey; struct remote *remote; struct branch *branch; + struct remote_state *remote_state = cb; + if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) { if (!name) return 0; - branch = make_branch(name, namelen); + branch = make_branch(remote_state, name, namelen); if (!strcmp(subkey, "remote")) { return git_config_string(&branch->remote_name, key, value); } else if (!strcmp(subkey, "pushremote")) { @@ -327,16 +326,14 @@ static int handle_config(const char *key, const char *value, void *cb) if (!strcmp(subkey, "insteadof")) { if (!value) return config_error_nonbool(key); - rewrite = make_rewrite( - &the_repository->remote_state->rewrites, name, - namelen); + rewrite = make_rewrite(&remote_state->rewrites, name, + namelen); add_instead_of(rewrite, xstrdup(value)); } else if (!strcmp(subkey, "pushinsteadof")) { if (!value) return config_error_nonbool(key); - rewrite = make_rewrite( - &the_repository->remote_state->rewrites_push, - name, namelen); + rewrite = make_rewrite(&remote_state->rewrites_push, + name, namelen); add_instead_of(rewrite, xstrdup(value)); } } @@ -346,9 +343,8 @@ static int handle_config(const char *key, const char *value, void *cb) /* Handle remote.* variables */ if (!name && !strcmp(subkey, "pushdefault")) - return git_config_string( - &the_repository->remote_state->pushremote_name, key, - value); + return git_config_string(&remote_state->pushremote_name, key, + value); if (!name) return 0; @@ -358,7 +354,7 @@ static int handle_config(const char *key, const char *value, void *cb) name); return 0; } - remote = make_remote(name, namelen); + remote = make_remote(remote_state, name, namelen); remote->origin = REMOTE_CONFIG; if (current_config_scope() == CONFIG_SCOPE_LOCAL || current_config_scope() == CONFIG_SCOPE_WORKTREE) @@ -428,61 +424,51 @@ static int handle_config(const char *key, const char *value, void *cb) return 0; } -static void alias_all_urls(void) +static void alias_all_urls(struct remote_state *remote_state) { int i, j; - for (i = 0; i < the_repository->remote_state->remotes_nr; i++) { + for (i = 0; i < remote_state->remotes_nr; i++) { int add_pushurl_aliases; - if (!the_repository->remote_state->remotes[i]) + if (!remote_state->remotes[i]) continue; - for (j = 0; - j < the_repository->remote_state->remotes[i]->pushurl_nr; - j++) { - the_repository->remote_state->remotes[i]->pushurl[j] = - alias_url( - the_repository->remote_state->remotes[i] - ->pushurl[j], - &the_repository->remote_state->rewrites); + for (j = 0; j < remote_state->remotes[i]->pushurl_nr; j++) { + remote_state->remotes[i]->pushurl[j] = + alias_url(remote_state->remotes[i]->pushurl[j], + &remote_state->rewrites); } - add_pushurl_aliases = - the_repository->remote_state->remotes[i]->pushurl_nr == - 0; - for (j = 0; - j < the_repository->remote_state->remotes[i]->url_nr; - j++) { + add_pushurl_aliases = remote_state->remotes[i]->pushurl_nr == 0; + for (j = 0; j < remote_state->remotes[i]->url_nr; j++) { if (add_pushurl_aliases) add_pushurl_alias( - the_repository->remote_state->remotes[i], - the_repository->remote_state->remotes[i] - ->url[j]); - the_repository->remote_state->remotes[i] - ->url[j] = alias_url( - the_repository->remote_state->remotes[i]->url[j], - &the_repository->remote_state->rewrites); + remote_state, remote_state->remotes[i], + remote_state->remotes[i]->url[j]); + remote_state->remotes[i]->url[j] = + alias_url(remote_state->remotes[i]->url[j], + &remote_state->rewrites); } } } -static void read_config(void) +static void read_config(struct repository *repo) { - static int loaded; int flag; - if (loaded) + if (repo->remote_state->initialized) return; - loaded = 1; + repo->remote_state->initialized = 1; - the_repository->remote_state->current_branch = NULL; + repo->remote_state->current_branch = NULL; if (startup_info->have_repository) { - const char *head_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flag); + const char *head_ref = refs_resolve_ref_unsafe( + get_main_ref_store(repo), "HEAD", 0, NULL, &flag); if (head_ref && (flag & REF_ISSYMREF) && skip_prefix(head_ref, "refs/heads/", &head_ref)) { - the_repository->remote_state->current_branch = - make_branch(head_ref, strlen(head_ref)); + repo->remote_state->current_branch = make_branch( + repo->remote_state, head_ref, strlen(head_ref)); } } - git_config(handle_config, NULL); - alias_all_urls(); + repo_config(repo, handle_config, repo->remote_state); + alias_all_urls(repo->remote_state); } static int valid_remote_nick(const char *name) @@ -552,7 +538,7 @@ static struct remote *remote_get_1(const char *name, struct remote *ret; int name_given = 0; - read_config(); + read_config(the_repository); if (name) name_given = 1; @@ -560,15 +546,15 @@ static struct remote *remote_get_1(const char *name, name = get_default(the_repository->remote_state->current_branch, &name_given); - ret = make_remote(name, 0); + ret = make_remote(the_repository->remote_state, name, 0); if (valid_remote_nick(name) && have_git_dir()) { if (!valid_remote(ret)) - read_remotes_file(ret); + read_remotes_file(the_repository->remote_state, ret); if (!valid_remote(ret)) - read_branches_file(ret); + read_branches_file(the_repository->remote_state, ret); } if (name_given && !valid_remote(ret)) - add_url_alias(ret, name); + add_url_alias(the_repository->remote_state, ret, name); if (!valid_remote(ret)) return NULL; return ret; @@ -596,7 +582,7 @@ int remote_is_configured(struct remote *remote, int in_repo) int for_each_remote(each_remote_fn fn, void *priv) { int i, result = 0; - read_config(); + read_config(the_repository); for (i = 0; i < the_repository->remote_state->remotes_nr && !result; i++) { struct remote *remote = @@ -1709,11 +1695,12 @@ struct branch *branch_get(const char *name) { struct branch *ret; - read_config(); + read_config(the_repository); if (!name || !*name || !strcmp(name, "HEAD")) ret = the_repository->remote_state->current_branch; else - ret = make_branch(name, strlen(name)); + ret = make_branch(the_repository->remote_state, name, + strlen(name)); set_merge(ret); return ret; } diff --git a/remote.h b/remote.h index d21c035f1b..85a730b8ef 100644 --- a/remote.h +++ b/remote.h @@ -52,6 +52,8 @@ struct remote_state { struct rewrites rewrites; struct rewrites rewrites_push; + + int initialized; }; void remote_state_clear(struct remote_state *remote_state); From patchwork Thu Oct 28 18:30:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12590801 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 09FE2C433EF for ; Thu, 28 Oct 2021 18:31:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E6752610D2 for ; Thu, 28 Oct 2021 18:31:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231169AbhJ1Sdv (ORCPT ); Thu, 28 Oct 2021 14:33:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59324 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230517AbhJ1Sdo (ORCPT ); Thu, 28 Oct 2021 14:33:44 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5CFC1C061570 for ; Thu, 28 Oct 2021 11:31:17 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id kn11-20020a17090b480b00b001a4053c7510so3021371pjb.8 for ; Thu, 28 Oct 2021 11:31:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=LgFgP+WD92/iEa0tJxZQ4c9HWKaXoDGfMblEnM6LJUA=; b=sjPjRFGkoIdWR8uJvv4dYT0k6VlJM+ROcOeQLKtfxTiUY8qaaVE9Gf6na9+E3/mfye Vre8velhdCaABDOORwv+C1igBtYgOqtK3JtLxdVeZZI7nc3zI8S4CwrzEn3eV+vH5kT7 FQIpPrn4I30fGE9m6G8co1XTuahExyvEsrNLg3S1vF+/P/C5S1RewvLFs9nmlv4zzqFB dxTU+bu09Jf0Zg3F23xe0bmiulmjpokvFzno+wsbl7YOPcM6YQ9xaRE/LWqlxN/haOT5 hqStv9BGglYfdIvsefhB0iC8Y+T9Tx/9YEAjVHaVHhH9jg38EEPTnm0eTE2ByX58UWX4 aANA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=LgFgP+WD92/iEa0tJxZQ4c9HWKaXoDGfMblEnM6LJUA=; b=Wh3zVGgQXTmehGFqiEC29ni//pZij1VFVbpywpGu8So9DqV5u55Lnl1zuQAI/Mi30U F9sKKInUKmq1E3aujtTyxc0WT7CZIa/scVBN0twtDHkaRXWdatM54M96FDIs2YDkHivu EYHMfa8E0gwyDblV8qd/s8wIYatSlr3/4zlmakAlxmcozhVvvY1PE/RNV1XcCs8rzAXz VgLt8/lzEHgOD9HhUOuQs7yhf+x/mx1x41AP90055MUPRMY7eVz25x3yPBeTD4xwApk0 cHw4mlIPFmsWjRUQ4NcLo+AyPCxMZWKmxf9lClyqJ5rCk433skb7PFFAjoVxnKwTmxoH ZnHw== X-Gm-Message-State: AOAM533Rp+/ZmAkUUgWVU9WzTcwMoAYrNfDHuPIXsnGFNENrIPQ9gDGY EZKFfhF0iJjnQ4EvxAuhe1V5LU7faE0WbRwwSy9AGOKut81F5ACJ/UOcwoBch67msA1ot4eaJBE LLTJTqxqEsu+HjEWFnYL/KoxU/4+nH9wUg6LSJf6ueBp74c87ORWssavUhKsrZNo= X-Google-Smtp-Source: ABdhPJwyiGZOxmKB3UJCT/FXRLJvnXiu8HkClffUneSiPfM01ek1AbRy3MQRDfTQCaAXWqdq/Lsm6NDDqMHpHA== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a17:902:aa08:b0:13f:eb2e:8ce8 with SMTP id be8-20020a170902aa0800b0013feb2e8ce8mr5417975plb.0.1635445876809; Thu, 28 Oct 2021 11:31:16 -0700 (PDT) Date: Thu, 28 Oct 2021 11:30:59 -0700 In-Reply-To: <20211028183101.41013-1-chooglen@google.com> Message-Id: <20211028183101.41013-5-chooglen@google.com> Mime-Version: 1.0 References: <20211019224339.61881-1-chooglen@google.com> <20211028183101.41013-1-chooglen@google.com> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog Subject: [PATCH v4 4/6] remote: remove the_repository->remote_state from static methods From: Glen Choo To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Replace all remaining references of the_repository->remote_state in static functions with a struct remote_state parameter. To do so, move read_config() calls to non-static functions and create a family of static functions, "remotes_*", that behave like "repo_*", but accept struct remote_state instead of struct repository. In the case where a static function calls a non-static function, replace the non-static function with its "remotes_*" equivalent. Signed-off-by: Glen Choo --- remote.c | 96 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 71 insertions(+), 25 deletions(-) diff --git a/remote.c b/remote.c index 48374cc0e2..88697e7e6d 100644 --- a/remote.c +++ b/remote.c @@ -483,7 +483,9 @@ static int valid_remote_nick(const char *name) return 1; } -const char *remote_for_branch(struct branch *branch, int *explicit) +static const char *remotes_remote_for_branch(struct remote_state *remote_state, + struct branch *branch, + int *explicit) { if (branch && branch->remote_name) { if (explicit) @@ -495,32 +497,55 @@ const char *remote_for_branch(struct branch *branch, int *explicit) return "origin"; } -const char *pushremote_for_branch(struct branch *branch, int *explicit) +const char *remote_for_branch(struct branch *branch, int *explicit) +{ + read_config(the_repository); + return remotes_remote_for_branch(the_repository->remote_state, branch, + explicit); +} + +static const char * +remotes_pushremote_for_branch(struct remote_state *remote_state, + struct branch *branch, int *explicit) { if (branch && branch->pushremote_name) { if (explicit) *explicit = 1; return branch->pushremote_name; } - if (the_repository->remote_state->pushremote_name) { + if (remote_state->pushremote_name) { if (explicit) *explicit = 1; - return the_repository->remote_state->pushremote_name; + return remote_state->pushremote_name; } - return remote_for_branch(branch, explicit); + return remotes_remote_for_branch(remote_state, branch, explicit); } +const char *pushremote_for_branch(struct branch *branch, int *explicit) +{ + read_config(the_repository); + return remotes_pushremote_for_branch(the_repository->remote_state, + branch, explicit); +} + +static struct remote *remotes_remote_get(struct remote_state *remote_state, + const char *name); + const char *remote_ref_for_branch(struct branch *branch, int for_push) { + read_config(the_repository); if (branch) { if (!for_push) { if (branch->merge_nr) { return branch->merge_name[0]; } } else { - const char *dst, *remote_name = - pushremote_for_branch(branch, NULL); - struct remote *remote = remote_get(remote_name); + const char *dst, + *remote_name = remotes_pushremote_for_branch( + the_repository->remote_state, branch, + NULL); + struct remote *remote = remotes_remote_get( + the_repository->remote_state, remote_name); if (remote && remote->push.nr && (dst = apply_refspecs(&remote->push, @@ -532,42 +557,58 @@ const char *remote_ref_for_branch(struct branch *branch, int for_push) return NULL; } -static struct remote *remote_get_1(const char *name, - const char *(*get_default)(struct branch *, int *)) +static struct remote * +remotes_remote_get_1(struct remote_state *remote_state, const char *name, + const char *(*get_default)(struct remote_state *, + struct branch *, int *)) { struct remote *ret; int name_given = 0; - read_config(the_repository); - if (name) name_given = 1; else - name = get_default(the_repository->remote_state->current_branch, + name = get_default(remote_state, remote_state->current_branch, &name_given); - ret = make_remote(the_repository->remote_state, name, 0); + ret = make_remote(remote_state, name, 0); if (valid_remote_nick(name) && have_git_dir()) { if (!valid_remote(ret)) - read_remotes_file(the_repository->remote_state, ret); + read_remotes_file(remote_state, ret); if (!valid_remote(ret)) - read_branches_file(the_repository->remote_state, ret); + read_branches_file(remote_state, ret); } if (name_given && !valid_remote(ret)) - add_url_alias(the_repository->remote_state, ret, name); + add_url_alias(remote_state, ret, name); if (!valid_remote(ret)) return NULL; return ret; } +static inline struct remote * +remotes_remote_get(struct remote_state *remote_state, const char *name) +{ + return remotes_remote_get_1(remote_state, name, + remotes_remote_for_branch); +} + struct remote *remote_get(const char *name) { - return remote_get_1(name, remote_for_branch); + read_config(the_repository); + return remotes_remote_get(the_repository->remote_state, name); +} + +static inline struct remote * +remotes_pushremote_get(struct remote_state *remote_state, const char *name) +{ + return remotes_remote_get_1(remote_state, name, + remotes_pushremote_for_branch); } struct remote *pushremote_get(const char *name) { - return remote_get_1(name, pushremote_for_branch); + read_config(the_repository); + return remotes_pushremote_get(the_repository->remote_state, name); } int remote_is_configured(struct remote *remote, int in_repo) @@ -1654,7 +1695,7 @@ void set_ref_status_for_push(struct ref *remote_refs, int send_mirror, } } -static void set_merge(struct branch *ret) +static void set_merge(struct remote_state *remote_state, struct branch *ret) { struct remote *remote; char *ref; @@ -1674,7 +1715,7 @@ static void set_merge(struct branch *ret) return; } - remote = remote_get(ret->remote_name); + remote = remotes_remote_get(remote_state, ret->remote_name); CALLOC_ARRAY(ret->merge, ret->merge_nr); for (i = 0; i < ret->merge_nr; i++) { @@ -1701,7 +1742,7 @@ struct branch *branch_get(const char *name) else ret = make_branch(the_repository->remote_state, name, strlen(name)); - set_merge(ret); + set_merge(the_repository->remote_state, ret); return ret; } @@ -1772,11 +1813,14 @@ static const char *tracking_for_push_dest(struct remote *remote, return ret; } -static const char *branch_get_push_1(struct branch *branch, struct strbuf *err) +static const char *branch_get_push_1(struct remote_state *remote_state, + struct branch *branch, struct strbuf *err) { struct remote *remote; - remote = remote_get(pushremote_for_branch(branch, NULL)); + remote = remotes_remote_get( + remote_state, + remotes_pushremote_for_branch(remote_state, branch, NULL)); if (!remote) return error_buf(err, _("branch '%s' has no remote for pushing"), @@ -1834,11 +1878,13 @@ static const char *branch_get_push_1(struct branch *branch, struct strbuf *err) const char *branch_get_push(struct branch *branch, struct strbuf *err) { + read_config(the_repository); if (!branch) return error_buf(err, _("HEAD does not point to a branch")); if (!branch->push_tracking_ref) - branch->push_tracking_ref = branch_get_push_1(branch, err); + branch->push_tracking_ref = branch_get_push_1( + the_repository->remote_state, branch, err); return branch->push_tracking_ref; } From patchwork Thu Oct 28 18:31:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12590803 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6A111C433FE for ; Thu, 28 Oct 2021 18:31:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4EDE8610C8 for ; Thu, 28 Oct 2021 18:31:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231208AbhJ1Sdw (ORCPT ); Thu, 28 Oct 2021 14:33:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231151AbhJ1Sdq (ORCPT ); Thu, 28 Oct 2021 14:33:46 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C70FDC061745 for ; Thu, 28 Oct 2021 11:31:19 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id c2-20020a63d5020000b029023ae853b72cso3688776pgg.18 for ; Thu, 28 Oct 2021 11:31:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=EPlHpmpm6K/ELW8suEF1UN7ZtD11QnXri6Lmaof06IQ=; b=Fpm4CcAcEPzH3gbSOAAIsl91LBZSkkceTw90+USWUKVnbKPBPn6G00z6TVM/dzLnmh MLCOF7L4A+2ya9f0Gfvf8KOhGhMjfxrAa7AZG+QI6+Bt7yDTnrcWs6Rk32jFpllHlXPT zSiCa53VCNlyuSzedeqx1MR9A8yoo4QqmffvAUFIQMAc+R+mjSOAM1sbinAu41YfxGy0 upFHcomeHejpOTHtjeFCwBKt73q8dNe5TDOG5kQ1kOnZtiEdZZi8jma2UfZeNOVuAs6u jHSkck6IbuyiEDvalBge4JJp0SXE8fVgF3qH59RtrTJJyVFde5WXh8Yso8FX8zihAuNa R8Ng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=EPlHpmpm6K/ELW8suEF1UN7ZtD11QnXri6Lmaof06IQ=; b=BfsaC9U97h1kT4WZF+nV2B4nobE/ZyIo+hWPXAIuJQow8JkyMiHXHFRQABJAGnOhx4 WBldwF2s8UL4obas47PoNpFqFCPuXkQFW1sgwz/ZARfto6NdcHPBuzCJWwi977Iu+ViV PaXQ+hDx5Cmh7NrLVsoTFU0vG6BmlRpz4wVtxgzNN0j8n4btUfYI3hZXEp6gpXWJvC2V 5gnNaAe89SHQDUe/d5sJ74SfgAuKG5wh3lUtrGotxvgooJM7oSsKg8YJZnV0Tudx0bWx C80WhTpKy6G55pKusWWhOjNf4yP5X7MtuwOR64L6t7Ogh+vm3P+wYVxRNLBB+86HDeXK VqjQ== X-Gm-Message-State: AOAM532J/wvnkFyAAdt4IhjO974V4gO2bMaRj4X3+HsBuFgzBolwnIB8 A+PwHD+hVtov2eZ3KizZ4fNtdklVkafm8KX+DHhxDRV5qWX9SIT756ZIPwglUzmIJWQo8EScCBA iyLSTeoukYKRJvevL2JsEDKGbMMxjxaosD5uI6BXuJvFR9jDoQhF+KRnts8MslZc= X-Google-Smtp-Source: ABdhPJyTrlIeBfscXFQZe77rPCL/Lj66jiEc4MsOBI2yMoupF+4IJPD0OvyUYWkVYgCbwpbv9yLGxpRN0ZJhrg== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a05:6a00:1348:b0:47c:e8f1:69a3 with SMTP id k8-20020a056a00134800b0047ce8f169a3mr6024593pfu.86.1635445878746; Thu, 28 Oct 2021 11:31:18 -0700 (PDT) Date: Thu, 28 Oct 2021 11:31:00 -0700 In-Reply-To: <20211028183101.41013-1-chooglen@google.com> Message-Id: <20211028183101.41013-6-chooglen@google.com> Mime-Version: 1.0 References: <20211019224339.61881-1-chooglen@google.com> <20211028183101.41013-1-chooglen@google.com> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog Subject: [PATCH v4 5/6] remote: die if branch is not found in repository From: Glen Choo To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org In a subsequent commit, we would like external-facing functions to be able to accept "struct repository" and "struct branch" as a pair. This is useful for functions like pushremote_for_branch(), which need to take values from the remote_state and branch, even if branch == NULL. However, a caller may supply an unrelated repository and branch, which is not supported behavior. To prevent misuse, add a die_on_missing_branch() helper function that dies if a given branch is not from a given repository. Speed up the existence check by using a new branches_hash hashmap to remote_state, and use the hashmap to remove the branch array iteration in make_branch(). Like read_config(), die_on_missing_branch() is only called from non-static functions; static functions are less prone to misuse because they have strong conventions for keeping remote_state and branch in sync. Signed-off-by: Glen Choo --- remote.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++----- remote.h | 2 ++ 2 files changed, 69 insertions(+), 6 deletions(-) diff --git a/remote.c b/remote.c index 88697e7e6d..2b5166dee0 100644 --- a/remote.c +++ b/remote.c @@ -166,17 +166,66 @@ static void add_merge(struct branch *branch, const char *name) branch->merge_name[branch->merge_nr++] = name; } +struct branches_hash_key { + const char *str; + int len; +}; + +static int branches_hash_cmp(const void *unused_cmp_data, + const struct hashmap_entry *eptr, + const struct hashmap_entry *entry_or_key, + const void *keydata) +{ + const struct branch *a, *b; + const struct branches_hash_key *key = keydata; + + a = container_of(eptr, const struct branch, ent); + b = container_of(entry_or_key, const struct branch, ent); + + if (key) + return strncmp(a->name, key->str, key->len) || + a->name[key->len]; + else + return strcmp(a->name, b->name); +} + +static struct branch *find_branch(struct remote_state *remote_state, + const char *name, size_t len) +{ + struct branches_hash_key lookup; + struct hashmap_entry lookup_entry, *e; + + if (!len) + len = strlen(name); + + lookup.str = name; + lookup.len = len; + hashmap_entry_init(&lookup_entry, memhash(name, len)); + + e = hashmap_get(&remote_state->branches_hash, &lookup_entry, &lookup); + if (e) + return container_of(e, struct branch, ent); + + return NULL; +} + +static void die_on_missing_branch(struct repository *repo, + struct branch *branch) +{ + /* branch == NULL is always valid because it represents detached HEAD. */ + if (branch && + branch != find_branch(repo->remote_state, branch->name, 0)) + die("branch %s was not found in the repository", branch->name); +} + static struct branch *make_branch(struct remote_state *remote_state, const char *name, size_t len) { struct branch *ret; - int i; - for (i = 0; i < remote_state->branches_nr; i++) { - if (!strncmp(name, remote_state->branches[i]->name, len) && - !remote_state->branches[i]->name[len]) - return remote_state->branches[i]; - } + ret = find_branch(remote_state, name, len); + if (ret) + return ret; ALLOC_GROW(remote_state->branches, remote_state->branches_nr + 1, remote_state->branches_alloc); @@ -185,6 +234,9 @@ static struct branch *make_branch(struct remote_state *remote_state, ret->name = xstrndup(name, len); ret->refname = xstrfmt("refs/heads/%s", ret->name); + hashmap_entry_init(&ret->ent, memhash(name, len)); + if (hashmap_put_entry(&remote_state->branches_hash, ret, ent)) + BUG("hashmap_put overwrote entry after hashmap_get returned NULL"); return ret; } @@ -500,6 +552,8 @@ static const char *remotes_remote_for_branch(struct remote_state *remote_state, const char *remote_for_branch(struct branch *branch, int *explicit) { read_config(the_repository); + die_on_missing_branch(the_repository, branch); + return remotes_remote_for_branch(the_repository->remote_state, branch, explicit); } @@ -524,6 +578,8 @@ remotes_pushremote_for_branch(struct remote_state *remote_state, const char *pushremote_for_branch(struct branch *branch, int *explicit) { read_config(the_repository); + die_on_missing_branch(the_repository, branch); + return remotes_pushremote_for_branch(the_repository->remote_state, branch, explicit); } @@ -534,6 +590,8 @@ static struct remote *remotes_remote_get(struct remote_state *remote_state, const char *remote_ref_for_branch(struct branch *branch, int for_push) { read_config(the_repository); + die_on_missing_branch(the_repository, branch); + if (branch) { if (!for_push) { if (branch->merge_nr) { @@ -1879,6 +1937,8 @@ static const char *branch_get_push_1(struct remote_state *remote_state, const char *branch_get_push(struct branch *branch, struct strbuf *err) { read_config(the_repository); + die_on_missing_branch(the_repository, branch); + if (!branch) return error_buf(err, _("HEAD does not point to a branch")); @@ -2652,6 +2712,7 @@ struct remote_state *remote_state_new(void) memset(r, 0, sizeof(*r)); hashmap_init(&r->remotes_hash, remotes_hash_cmp, NULL, 0); + hashmap_init(&r->branches_hash, branches_hash_cmp, NULL, 0); return r; } diff --git a/remote.h b/remote.h index 85a730b8ef..b205830ac3 100644 --- a/remote.h +++ b/remote.h @@ -46,6 +46,7 @@ struct remote_state { struct branch **branches; int branches_alloc; int branches_nr; + struct hashmap branches_hash; struct branch *current_branch; const char *pushremote_name; @@ -292,6 +293,7 @@ int remote_find_tracking(struct remote *remote, struct refspec_item *refspec); * branch_get(name) for "refs/heads/{name}", or with branch_get(NULL) for HEAD. */ struct branch { + struct hashmap_entry ent; /* The short name of the branch. */ const char *name; From patchwork Thu Oct 28 18:31:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12590805 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B3828C433EF for ; Thu, 28 Oct 2021 18:31:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9B38D610E5 for ; Thu, 28 Oct 2021 18:31:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231244AbhJ1Sdy (ORCPT ); Thu, 28 Oct 2021 14:33:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231178AbhJ1Sds (ORCPT ); Thu, 28 Oct 2021 14:33:48 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17D6AC061745 for ; Thu, 28 Oct 2021 11:31:21 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id nv1-20020a17090b1b4100b001a04861d474so3750356pjb.5 for ; Thu, 28 Oct 2021 11:31:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=yeaFnwYPSV2ktOrGS5VHYnqJVzUhpAO17V/BsDi/u2A=; b=HPzra/gl87d3qrHwuw13V9G5QAjzMOsV5nFDAOWUjeDC/pmE9Os1f7Zeu/EHSiQrrU rrKX+NeM1YoAevygB1ozCypwHDWKisV+XBnl7WFemj6HYoQEJGVaIZBCLmDNzwG6qKWe fHQuWfLhTwW1/lLCcwM6vXBTAPOt4nUSzLYEXXR1MCjQ203iHXYdZLHSis+fzR83MfU8 nc6za8HtZtGt5FjnNB2az41c3EVoTQJ4HgnI72E6MSp5wAylyJc3qOuA1I9RP4bLYQSR IMUiT67YkcmUuf/lp1f20TIG+vebQZ8GXWv2JeifluaBaPFx+v/4yM45zko34diKEo0G MMbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=yeaFnwYPSV2ktOrGS5VHYnqJVzUhpAO17V/BsDi/u2A=; b=KkCbEhUMb4ytBvJecDMe9+E8GSp+Vm0KbfZAhLMa/bCgu92yADiUmNdtY7RElUr/8K rDEpUQuC/XZEFz1TkxfrL8K48IcLbhxSyp/LiaYpHfUY+C2hX38D34YJU+erqXrmCWTa i4ftCWnfju+V1odV9YG94qi3iVKpzeqzM4YP93yJGUWgaEf4tTVm62A6oS6BCT0gLsdw DBFRxtOWQeo5uMKmsoKImy6LXHM4lQq2F+FhM8l4OcIceVl7+33hY08vzJ4P0wcReSAE hhT/eVObvnxPSm8RGQ0XbXtRfJ7iCg+xkQfkRmuMHY4xZfLPJS0yDs2/TwF0sUOVTTCQ UMQw== X-Gm-Message-State: AOAM532hxb9I/XMsZ6JkDXQurV5FmSvATCJQCGt4qWi8yKE6mCToJui+ R1WqblwstDgEQhOQAl7O9DDdzpLTq3mTWbT2Hc9n5dPE/oANuYaP1naPtt94dpZKQ+1JSfiSsGn d3AfH2RcpjHiPqjFRj+FFv+PZ5M6DH2DxEpnzl5Q5i2t/7kiuQqlRdWMVqRpSnmM= X-Google-Smtp-Source: ABdhPJwnGraNSr0TdcUklkWrCJTUwYU6/L+KU2P6hiAcsq8h97JxSES7/3mlbbK6eACmwb2275oIXPvBqIAx3w== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a17:902:9a91:b0:138:efd5:7302 with SMTP id w17-20020a1709029a9100b00138efd57302mr5253438plp.35.1635445880536; Thu, 28 Oct 2021 11:31:20 -0700 (PDT) Date: Thu, 28 Oct 2021 11:31:01 -0700 In-Reply-To: <20211028183101.41013-1-chooglen@google.com> Message-Id: <20211028183101.41013-7-chooglen@google.com> Mime-Version: 1.0 References: <20211019224339.61881-1-chooglen@google.com> <20211028183101.41013-1-chooglen@google.com> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog Subject: [PATCH v4 6/6] remote: add struct repository parameter to external functions From: Glen Choo To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Finish plumbing remote_state by adding a struct repository parameter to repo_* functions. This removes all references to the_repository->remote_state in remote.c - explicit references in the function bodies and implicit references via calls to non-repo_* functions. However, certain functions still use the_repository to parse oids. Where a non-static function has the option to call either the static "remotes_*" variant or the non-static "repo_*" variant, use "remotes_*" in order to be consistent with the static functions. A desirable side-effect of this is that it minimizes the number of calls to read_config(). Signed-off-by: Glen Choo --- remote.c | 85 ++++++++++++++++++++++++++++---------------------------- remote.h | 80 ++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 109 insertions(+), 56 deletions(-) diff --git a/remote.c b/remote.c index 2b5166dee0..f7d9a47faa 100644 --- a/remote.c +++ b/remote.c @@ -549,13 +549,13 @@ static const char *remotes_remote_for_branch(struct remote_state *remote_state, return "origin"; } -const char *remote_for_branch(struct branch *branch, int *explicit) +const char *repo_remote_for_branch(struct repository *repo, struct branch *branch, int *explicit) { - read_config(the_repository); - die_on_missing_branch(the_repository, branch); + read_config(repo); + die_on_missing_branch(repo, branch); - return remotes_remote_for_branch(the_repository->remote_state, branch, - explicit); + return remotes_remote_for_branch(repo->remote_state, branch, + explicit); } static const char * @@ -575,22 +575,22 @@ remotes_pushremote_for_branch(struct remote_state *remote_state, return remotes_remote_for_branch(remote_state, branch, explicit); } -const char *pushremote_for_branch(struct branch *branch, int *explicit) +const char *repo_pushremote_for_branch(struct repository *repo, struct branch *branch, int *explicit) { - read_config(the_repository); - die_on_missing_branch(the_repository, branch); + read_config(repo); + die_on_missing_branch(repo, branch); - return remotes_pushremote_for_branch(the_repository->remote_state, + return remotes_pushremote_for_branch(repo->remote_state, branch, explicit); } static struct remote *remotes_remote_get(struct remote_state *remote_state, const char *name); -const char *remote_ref_for_branch(struct branch *branch, int for_push) +const char *repo_remote_ref_for_branch(struct repository *repo, struct branch *branch, int for_push) { - read_config(the_repository); - die_on_missing_branch(the_repository, branch); + read_config(repo); + die_on_missing_branch(repo, branch); if (branch) { if (!for_push) { @@ -600,10 +600,10 @@ const char *remote_ref_for_branch(struct branch *branch, int for_push) } else { const char *dst, *remote_name = remotes_pushremote_for_branch( - the_repository->remote_state, branch, + repo->remote_state, branch, NULL); struct remote *remote = remotes_remote_get( - the_repository->remote_state, remote_name); + repo->remote_state, remote_name); if (remote && remote->push.nr && (dst = apply_refspecs(&remote->push, @@ -650,10 +650,10 @@ remotes_remote_get(struct remote_state *remote_state, const char *name) remotes_remote_for_branch); } -struct remote *remote_get(const char *name) +struct remote *repo_remote_get(struct repository *repo, const char *name) { - read_config(the_repository); - return remotes_remote_get(the_repository->remote_state, name); + read_config(repo); + return remotes_remote_get(repo->remote_state, name); } static inline struct remote * @@ -663,10 +663,10 @@ remotes_pushremote_get(struct remote_state *remote_state, const char *name) remotes_pushremote_for_branch); } -struct remote *pushremote_get(const char *name) +struct remote *repo_pushremote_get(struct repository *repo, const char *name) { - read_config(the_repository); - return remotes_pushremote_get(the_repository->remote_state, name); + read_config(repo); + return remotes_pushremote_get(repo->remote_state, name); } int remote_is_configured(struct remote *remote, int in_repo) @@ -678,14 +678,14 @@ int remote_is_configured(struct remote *remote, int in_repo) return !!remote->origin; } -int for_each_remote(each_remote_fn fn, void *priv) +int repo_for_each_remote(struct repository *repo, each_remote_fn fn, void *priv) { int i, result = 0; - read_config(the_repository); - for (i = 0; i < the_repository->remote_state->remotes_nr && !result; + read_config(repo); + for (i = 0; i < repo->remote_state->remotes_nr && !result; i++) { struct remote *remote = - the_repository->remote_state->remotes[i]; + repo->remote_state->remotes[i]; if (!remote) continue; result = fn(remote, priv); @@ -1790,17 +1790,16 @@ static void set_merge(struct remote_state *remote_state, struct branch *ret) } } -struct branch *branch_get(const char *name) +struct branch *repo_branch_get(struct repository *repo, const char *name) { struct branch *ret; - read_config(the_repository); + read_config(repo); if (!name || !*name || !strcmp(name, "HEAD")) - ret = the_repository->remote_state->current_branch; + ret = repo->remote_state->current_branch; else - ret = make_branch(the_repository->remote_state, name, - strlen(name)); - set_merge(the_repository->remote_state, ret); + ret = make_branch(repo->remote_state, name, strlen(name)); + set_merge(repo->remote_state, ret); return ret; } @@ -1934,17 +1933,17 @@ static const char *branch_get_push_1(struct remote_state *remote_state, BUG("unhandled push situation"); } -const char *branch_get_push(struct branch *branch, struct strbuf *err) +const char *repo_branch_get_push(struct repository *repo, struct branch *branch, struct strbuf *err) { - read_config(the_repository); - die_on_missing_branch(the_repository, branch); + read_config(repo); + die_on_missing_branch(repo, branch); if (!branch) return error_buf(err, _("HEAD does not point to a branch")); if (!branch->push_tracking_ref) branch->push_tracking_ref = branch_get_push_1( - the_repository->remote_state, branch, err); + repo->remote_state, branch, err); return branch->push_tracking_ref; } @@ -2197,15 +2196,16 @@ static int stat_branch_pair(const char *branch_name, const char *base, * upstream defined, or ref does not exist). Returns 0 if the commits are * identical. Returns 1 if commits are different. */ -int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs, - const char **tracking_name, int for_push, - enum ahead_behind_flags abf) +int repo_stat_tracking_info(struct repository *repo, struct branch *branch, + int *num_ours, int *num_theirs, + const char **tracking_name, int for_push, + enum ahead_behind_flags abf) { const char *base; /* Cannot stat unless we are marked to build on top of somebody else. */ - base = for_push ? branch_get_push(branch, NULL) : - branch_get_upstream(branch, NULL); + base = for_push ? repo_branch_get_push(repo, branch, NULL) : + branch_get_upstream(branch, NULL); if (tracking_name) *tracking_name = base; if (!base) @@ -2217,15 +2217,16 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs, /* * Return true when there is anything to report, otherwise false. */ -int format_tracking_info(struct branch *branch, struct strbuf *sb, - enum ahead_behind_flags abf) +int repo_format_tracking_info(struct repository *repo, struct branch *branch, + struct strbuf *sb, enum ahead_behind_flags abf) { int ours, theirs, sti; const char *full_base; char *base; int upstream_is_gone = 0; - sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf); + sti = repo_stat_tracking_info(repo, branch, &ours, &theirs, &full_base, + 0, abf); if (sti < 0) { if (!full_base) return 0; diff --git a/remote.h b/remote.h index b205830ac3..9b06afd285 100644 --- a/remote.h +++ b/remote.h @@ -120,15 +120,28 @@ struct remote { * remote_get(NULL) will return the default remote, given the current branch * and configuration. */ -struct remote *remote_get(const char *name); - -struct remote *pushremote_get(const char *name); +struct remote *repo_remote_get(struct repository *repo, const char *name); +static inline struct remote *remote_get(const char *name) +{ + return repo_remote_get(the_repository, name); +} + +struct remote *repo_pushremote_get(struct repository *repo, const char *name); +static inline struct remote *pushremote_get(const char *name) +{ + return repo_pushremote_get(the_repository, name); +} int remote_is_configured(struct remote *remote, int in_repo); typedef int each_remote_fn(struct remote *remote, void *priv); /* iterate through struct remotes */ -int for_each_remote(each_remote_fn fn, void *priv); +int repo_for_each_remote(struct repository *repo, each_remote_fn fn, + void *priv); +static inline int for_each_remote(each_remote_fn fn, void *priv) +{ + return repo_for_each_remote(the_repository, fn, priv); +} int remote_has_url(struct remote *remote, const char *url); @@ -324,10 +337,29 @@ struct branch { const char *push_tracking_ref; }; -struct branch *branch_get(const char *name); -const char *remote_for_branch(struct branch *branch, int *explicit); -const char *pushremote_for_branch(struct branch *branch, int *explicit); -const char *remote_ref_for_branch(struct branch *branch, int for_push); +struct branch *repo_branch_get(struct repository *repo, const char *name); +static inline struct branch *branch_get(const char *name) +{ + return repo_branch_get(the_repository, name); +} + +const char *repo_remote_for_branch(struct repository *repo, struct branch *branch, int *explicit); +static inline const char *remote_for_branch(struct branch *branch, int *explicit) +{ + return repo_remote_for_branch(the_repository, branch, explicit); +} + +const char *repo_pushremote_for_branch(struct repository *repo, struct branch *branch, int *explicit); +static inline const char *pushremote_for_branch(struct branch *branch, int *explicit) +{ + return repo_pushremote_for_branch(the_repository, branch, explicit); +} + +const char *repo_remote_ref_for_branch(struct repository *repo, struct branch *branch, int for_push); +static inline const char *remote_ref_for_branch(struct branch *branch, int for_push) +{ + return repo_remote_ref_for_branch(the_repository, branch, for_push); +} /* returns true if the given branch has merge configuration given. */ int branch_has_merge_config(struct branch *branch); @@ -351,7 +383,11 @@ const char *branch_get_upstream(struct branch *branch, struct strbuf *err); * * The return value and `err` conventions match those of `branch_get_upstream`. */ -const char *branch_get_push(struct branch *branch, struct strbuf *err); +const char *repo_branch_get_push(struct repository *repo, struct branch *branch, struct strbuf *err); +static inline const char *branch_get_push(struct branch *branch, struct strbuf *err) +{ + return repo_branch_get_push(the_repository, branch, err); +} /* Flags to match_refs. */ enum match_refs_flags { @@ -370,11 +406,27 @@ enum ahead_behind_flags { }; /* Reporting of tracking info */ -int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs, - const char **upstream_name, int for_push, - enum ahead_behind_flags abf); -int format_tracking_info(struct branch *branch, struct strbuf *sb, - enum ahead_behind_flags abf); +int repo_stat_tracking_info(struct repository *repo, struct branch *branch, + int *num_ours, int *num_theirs, + const char **upstream_name, int for_push, + enum ahead_behind_flags abf); +static inline int stat_tracking_info(struct branch *branch, int *num_ours, + int *num_theirs, + const char **upstream_name, int for_push, + enum ahead_behind_flags abf) +{ + return repo_stat_tracking_info(the_repository, branch, num_ours, + num_theirs, upstream_name, for_push, + abf); +} + +int repo_format_tracking_info(struct repository *repo, struct branch *branch, + struct strbuf *sb, enum ahead_behind_flags abf); +static inline int format_tracking_info(struct branch *branch, struct strbuf *sb, + enum ahead_behind_flags abf) +{ + return repo_format_tracking_info(the_repository, branch, sb, abf); +} struct ref *get_local_heads(void); /*