From patchwork Thu Nov 18 00:53:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12625781 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 EB87BC433FE for ; Thu, 18 Nov 2021 00:53:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CE3AE61251 for ; Thu, 18 Nov 2021 00:53:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242033AbhKRA4p (ORCPT ); Wed, 17 Nov 2021 19:56:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242028AbhKRA4l (ORCPT ); Wed, 17 Nov 2021 19:56:41 -0500 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C82F5C061570 for ; Wed, 17 Nov 2021 16:53:42 -0800 (PST) Received: by mail-pf1-x449.google.com with SMTP id c21-20020a62e815000000b004a29ebf0aa7so2596358pfi.2 for ; Wed, 17 Nov 2021 16:53:42 -0800 (PST) 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=AU5Bk/32zjduelByQqyJLtOSDrQw4PqLJqEko5EiDOs=; b=tYcxQ/XB+70VJBQpCCODxNykzWCrOwA5KqiM50rb9wR7tkRGi/znbG70SUxf1xFkcH GQCuf8Xtm1GwN75990U4K+iJDbvzlqJrahxorJC6tnhxARbQTF/GdjcBcGk186FLCn+/ UrVFNmirS5nU7fqJc0HAKczCEmDOADVnFdLQDy7FhVsfHIM8kyETjGqEdLJJB9wvLdHT N9AbZm8ng0G+0C56tJGbODN67HJldavsMZpk8JrkosFjwYSGxqaSBYziXupq9LipPA7W qF+bkwh/xHt2PciTFWsthUafDcflRQnEfmsB+43MWfhnQ4dNYQGucHOoEhdBlosxiusM XWrQ== 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=AU5Bk/32zjduelByQqyJLtOSDrQw4PqLJqEko5EiDOs=; b=JntcYoP5NB7kJECVc3ef5hX/MrqGPOlFEHZfR7N894pUZ5KKP8trZuBboYTODD3aH+ U2SAj/IoXYv2PN1NvX+WmarekaZJ2x2NHd2ogdfl338V0azb42yRyS9Bi4u7aWYa8L1E QGN00UhTlV/B710NMYBsOwx16roCDOwU8NpGC06K2oCBLihhl0WZxob1q8iNDG4aMGZl f6jVcNhF2XaP9RrSfJvJVFXntPPR6EDU+dnHwxtUZz8P8QliDfrYXiiK4md1wtR0bPyc ByJgK4ShUFe7EeB82y0TJc6rl6qM2kNb0lswF2Pks5Qf60bFWYloCNiw8iqsPOdND3af oNEQ== X-Gm-Message-State: AOAM530Pe+igorzc91fQnQbAlnVIgbF1ZDg6SlxV46Z6dcUmyRvF5XNL vU8rb0ubhMIx/YDpvG3rvQGwpJmS6Iq9+/IuwXnu3yD7tMaLzjs9mBdiQ6Xfv6bkb2hwTZepeVM KY/XH2Pqa6q6SYzBX0UrnB9UgkWCd3mO3ionSIl0uJcDZwdEn+EELxnNmUl0qmPw= X-Google-Smtp-Source: ABdhPJyGgzm01ORn58Fw4ibgj4NbxVIiEaaoi+ZB6tIEeJt80X3PWdJzfxnINh7ox7o9undjK8BPQrrgCmnzzg== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a65:5882:: with SMTP id d2mr742333pgu.120.1637196822109; Wed, 17 Nov 2021 16:53:42 -0800 (PST) Date: Wed, 17 Nov 2021 16:53:21 -0800 In-Reply-To: <20211118005325.64971-1-chooglen@google.com> Message-Id: <20211118005325.64971-2-chooglen@google.com> Mime-Version: 1.0 References: <20211118005325.64971-1-chooglen@google.com> X-Mailer: git-send-email 2.34.0.rc1.387.gb447b232ab-goog Subject: [PATCH v5 1/5] t5516: add test case for pushing remote refspecs From: Glen Choo To: git@vger.kernel.org Cc: Jonathan Tan , Junio C Hamano , Glen Choo Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org "git push remote-name" (that is, with no refspec given on the command line) should push the refspecs in remote.remote-name.push. There is no test case that checks this behavior in detached HEAD, so 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..7831a38dde 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 no explicit refspec 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 Nov 18 00:53:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12625783 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 62B45C433F5 for ; Thu, 18 Nov 2021 00:53:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3A67A61251 for ; Thu, 18 Nov 2021 00:53:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242025AbhKRA4r (ORCPT ); Wed, 17 Nov 2021 19:56:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242024AbhKRA4o (ORCPT ); Wed, 17 Nov 2021 19:56:44 -0500 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 437A8C061570 for ; Wed, 17 Nov 2021 16:53:45 -0800 (PST) Received: by mail-pg1-x54a.google.com with SMTP id k22-20020a635a56000000b002df9863aa74so1851143pgm.19 for ; Wed, 17 Nov 2021 16:53:45 -0800 (PST) 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=heJ4+AzvpKXHuaOC+hx+WWWDm4V9yt7HXM1QatVJtDM=; b=IVT7MKYAvWjKDYlaCKFxN8w4/t2njIouO3ilqxab3V5TYKxN6FLNTT4ZWion+fytR1 6zQsUlZOZ25e2XyaEMFLXTGLdcsbKApjy5gZALnvA3Pbf4eERXRcq7uhnWGMoyalH/mC KlTq+/FrYtROriykrmPDWKb0wP/BN4JM+ywBxpDAZVyDja5aDFCHkbbUwqmNy/JQtdfy jGS/g+oKdvzjHQU3ZnPQ7z5v865VAzyjQIoxbnXcsUr4C8T/xH21yHYMCESYQvbgvflK C1hEJq45TBOzBVZw5mNY6HJciHFSQ9g/XGkr/ud5ARiJNAT06ug6/tKqaNlWFMtPGWqT 56WQ== 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=heJ4+AzvpKXHuaOC+hx+WWWDm4V9yt7HXM1QatVJtDM=; b=OdTvN1VdmH25e8kXK5AS+2gSDVugeP28roXPyjkc/RBQv5UttdI4gt2kVG5xJjBOlz k+f8Bg/FtZs8Uvfs8f3XsJIXskWvm8bXUvHE/LXcZehXRLtXYnlcaKjyyRhxIcofMaad 1ZR6WXLLjvzfenn/fmgDRclRwgr7p0q/1szJPfsnvstw9z2RJ06a+x/4atw6VwqIKts2 bs7lk0P/WuDx5gU+CsP9QZGnM96q5p2r+ZIXnEdx0hQnZUWOU4eLkJKiME7JEFbLlF9I Z347OajupjJ5tCkbOVU5A4ZziSUq4JMW4posu2ROsyeMT2e874M+U1SsyDzojVb6H3fj omgQ== X-Gm-Message-State: AOAM530WPdyx3Cwf4mR/h37lFLI0oJBIdUjchLn/uC8tRPf4mX1ZZPI/ pPUoKRKnteLy9L1GEKXaoVZZHaARF8UFn8dZ6USO+RwAwMe56p0Tu3JWqdKtjgcaREibZyqa01v yUta3coiBd5a+o69nETLz2HWJ+/wUQNEqm7UFLN3bIPYr3NU5Q4DyQEL2lk73PMk= X-Google-Smtp-Source: ABdhPJzaic7wjqoWmgu97zYP+3Ybmt7IxCcUKiwCROXzaGrIJPZre6BpLPvlWoJD8fFD+Fem8wXGSPIHo4paJw== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a17:90a:390c:: with SMTP id y12mr780690pjb.0.1637196824156; Wed, 17 Nov 2021 16:53:44 -0800 (PST) Date: Wed, 17 Nov 2021 16:53:22 -0800 In-Reply-To: <20211118005325.64971-1-chooglen@google.com> Message-Id: <20211118005325.64971-3-chooglen@google.com> Mime-Version: 1.0 References: <20211118005325.64971-1-chooglen@google.com> X-Mailer: git-send-email 2.34.0.rc1.387.gb447b232ab-goog Subject: [PATCH v5 2/5] remote: move static variables into per-repository struct From: Glen Choo To: git@vger.kernel.org Cc: Jonathan Tan , Junio C Hamano , Glen Choo Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org remote.c does not works 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 Nov 18 00:53:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12625785 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 F1BA1C433EF for ; Thu, 18 Nov 2021 00:53:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D9DAE61B3E for ; Thu, 18 Nov 2021 00:53:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242049AbhKRA4u (ORCPT ); Wed, 17 Nov 2021 19:56:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242028AbhKRA4q (ORCPT ); Wed, 17 Nov 2021 19:56:46 -0500 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 1AECCC061570 for ; Wed, 17 Nov 2021 16:53:47 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id n6-20020a17090a670600b001a9647fd1aaso4030683pjj.1 for ; Wed, 17 Nov 2021 16:53:47 -0800 (PST) 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=MS4CsYWAST8YkiPNlOnHdGMIUqQ5+uMcQj9aStHlYE+lFmM89c3+J/c5IsRICHJjP0 hDwK1Sas83Bds3XNIfMlyO6zGX6qoP56ur75w72avAhOzfcQ46Dn2xz55Mm+9UWAphiz XvZvuGumPQbqF4OeVnTLv+S5mCUl1Th/gR58b9AHQeWB54N0txow7cLc9SiTcpQW8qQ1 bgPKHjP80+ZQhirvoFzslo39fgyIBvqYr31c8JNggC/hy1eBgkAy7XlHZRlVtJfLeQ9R A8ENCXZIl+s1pLy/jmWrCDIakgjSOvC42crK8R9xI8lLe+Gl/SBtSaqhMb5+uBQPY840 SEJw== 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=S1s8jBIPKHMat8bKIhqy5ngZsKZWztaONBYyQ+OWyrJOcTnA5L1VRdhErr3d6XtwbD SaH2Pxdy37OM/26CBsX1o+yCaN42Vc+SehnU9qEJR4wvZidVIPJK9EFk9ysQvGdvOFFo E4MVlBND6qo/vkn8XXCvoYFHxEuopS5xCaWP2rV4LMlEjbvg6BKRYqAmhMywYAYT9mJo snZUbOm0MXjw7aSNlq1HFeLBq+1wgrdZZqf/hFh7r+4i54GrUJ6mx+/fwFf/AZZQ1wvH bkyIdZg3VI5E2cYcROlsdDqS0yhvUPTVbC3ptFjUwkacsCSOz070fXhokJVK94v83Ple O93A== X-Gm-Message-State: AOAM530u1yYHZUc39aRVQf8R+mZ5cGDKCqf2upqxrsjnRPSuMmXu1wgK jbLTIFif0XS6kdvRsivsrWS5R20iaezxP2izwV2yV9GjyP2Uj2WT4UBp5nE1VdTZsIW6QsaC6o9 GpFM5tWLdtTx8ZSwstsvegDcLvJaC21w34dkm5Ayr0nTfHDbd0tys+ljOsEPlC+Y= X-Google-Smtp-Source: ABdhPJzIvXCEgYZtUxTbWTzDD9+0iOTezNO+n2P3GJy+JpQvOISmgdxfNGUfctTSSlcSQqs1rSTJz/6EN9+yhA== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a17:90a:e7c4:: with SMTP id kb4mr5162604pjb.237.1637196826540; Wed, 17 Nov 2021 16:53:46 -0800 (PST) Date: Wed, 17 Nov 2021 16:53:23 -0800 In-Reply-To: <20211118005325.64971-1-chooglen@google.com> Message-Id: <20211118005325.64971-4-chooglen@google.com> Mime-Version: 1.0 References: <20211118005325.64971-1-chooglen@google.com> X-Mailer: git-send-email 2.34.0.rc1.387.gb447b232ab-goog Subject: [PATCH v5 3/5] remote: use remote_state parameter internally From: Glen Choo To: git@vger.kernel.org Cc: Jonathan Tan , 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 Nov 18 00:53:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12625787 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 6CE11C433EF for ; Thu, 18 Nov 2021 00:53:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 510AC61B31 for ; Thu, 18 Nov 2021 00:53:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242050AbhKRA4x (ORCPT ); Wed, 17 Nov 2021 19:56:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242041AbhKRA4s (ORCPT ); Wed, 17 Nov 2021 19:56:48 -0500 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 3EF44C061767 for ; Wed, 17 Nov 2021 16:53:49 -0800 (PST) Received: by mail-pg1-x549.google.com with SMTP id a26-20020a63bd1a000000b002fab31bc2d9so963139pgf.7 for ; Wed, 17 Nov 2021 16:53:49 -0800 (PST) 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=e+HTANyg93h5zVjnNlKLX616IWLJ71SWdG/BnOEyspOCYUjs67A8hYmlp/sWb0I5vJ 5TQ1/zeiuw6BjLhePvUzsbtXIAhX58GNrWqFWTDTL2ioTlQHpe9Do3K5aSNDU+8V7n5S S1fhOvVxMyaHbU2ymSQ/dyN0xNhBQlOokNP9xJJsj+mPNcDNULq7ty4zce5a5k/GWJEp 2CbFbGqfVwHepHKR9cAtCTVDUkrKm/xqQB6shAxk1ygS30ClDRf7uQqHCOuIXx59FCU7 TmXAZqaR/YYCEzX/BylrL9NjL9Oh2LXYXL5Vwqehwg7SaXYri1EHaSs1Cw1QyzN0tjDe A7Mg== 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=MWJMhnNvXA4dpzAZ69v/mQpFRtuXG3h/gLkUsxEJ9J3fl+nQ2t9yHg2L1OzMqr8+HH 4EtR7f5M/VpMzVwfGDqE1bA9gn2IcaR+lspOQu5cwzSFGs4MpUwKJpBCfEBs7xVOiIF/ xxsLlPdG/PHsqHb3Lj0LywOLgMPwzUJ3Ctl0xBRx2jLIId8703Zng+osiMd0CjRv863h DKABk8L/XfhtsqIhwWoPt53u2YhyyDJwPOkF0e+lIw98XfbPTN4eU2GI0skui1T7ZL0M N/JYnRW82pEDPyCSVws9Ewx5BsituQ7Pvex8Tapb4NI+3kWUoe9XMDnE5YTkhiGEdzYB pMXQ== X-Gm-Message-State: AOAM5322DJQogdDcwb7l9MQOfEUTp6c/EzSIzXrkeeqXIBS71WkhJXjR RVJ+Sf+NI3AokY5nc4MMKXkwtQN76kgLk44o5qE1sRyWDvk9nH46HTVzmp3FHHkC85liZHcU1rT T5X82CTWoCV3MGAAA0I9nhQIe+I12Z0DpA8M0hty9lpZMPowRe7zmBPkxrzzxnVI= X-Google-Smtp-Source: ABdhPJy87J7HvYuYipaPio7mONrMDVxu88zDS81rPiNNYs1pyO/0t2RW1+obP8OUUhKzOawThePu3a1vZ/A4Ew== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a05:6a00:b83:b0:49f:b555:1183 with SMTP id g3-20020a056a000b8300b0049fb5551183mr53039958pfj.32.1637196828574; Wed, 17 Nov 2021 16:53:48 -0800 (PST) Date: Wed, 17 Nov 2021 16:53:24 -0800 In-Reply-To: <20211118005325.64971-1-chooglen@google.com> Message-Id: <20211118005325.64971-5-chooglen@google.com> Mime-Version: 1.0 References: <20211118005325.64971-1-chooglen@google.com> X-Mailer: git-send-email 2.34.0.rc1.387.gb447b232ab-goog Subject: [PATCH v5 4/5] remote: remove the_repository->remote_state from static methods From: Glen Choo To: git@vger.kernel.org Cc: Jonathan Tan , 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 Nov 18 00:53:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12625789 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 4F6A1C433F5 for ; Thu, 18 Nov 2021 00:53:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 316E061251 for ; Thu, 18 Nov 2021 00:53:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242037AbhKRA4y (ORCPT ); Wed, 17 Nov 2021 19:56:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242051AbhKRA4w (ORCPT ); Wed, 17 Nov 2021 19:56:52 -0500 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2FCAAC061206 for ; Wed, 17 Nov 2021 16:53:51 -0800 (PST) Received: by mail-pg1-x54a.google.com with SMTP id s8-20020a63af48000000b002e6c10ac245so1855988pgo.21 for ; Wed, 17 Nov 2021 16:53:51 -0800 (PST) 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=T+ETImNGszr/UXUnjr823Jp3cUJFSOV67dSZT8WrSAQ=; b=Xx638QLIbHBEjoBOrBcpE9N3v5Gs4UOWqp8Pzg7mFzKgOFLe8JPIwKCccVceAfyCRi 8SA0qBGT3gJKAArh9T9bpse4FkMmsSxHb7kjh95Vg8JIMP421pjfN08lzf3/Oe7cKvmz edmIXBn5PNLxmiM+Z79UfaAycHI9tp1rnlp0+yJsQndQGBuQoLDHO7hx+aTnwy29NxKt IuvvjYpdrGtEEIph4LXq1PkRO8dIssZkO56ZvYtu6mhh/KtiP74ywtpp6CsEeoOQSExh VZ6hRzfwaxtltOZ7AFt1Zw2QYj+sUBRL2XiRx/KZnMfubqUWkICZpyYe25X6FiMotn6e TH4w== 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=T+ETImNGszr/UXUnjr823Jp3cUJFSOV67dSZT8WrSAQ=; b=XXC0yuAqJFVKFqy41ciDkwV/gTB8u4PsXE0K/mfBqpVSEvUL29xaoc2rCKrlQfH2ih wJSRD/8ATsMqEGGdQ6yoiuoMORpHonpbWVOrKdUvrQ+ESAt4MGADEE8H34+na0ie2S5V iZq00mPn1lFY1ZAAGiGC5paCgJEVDNd64bsmoL9/p/zy0zbuzj5jj7lR81yrpwwmPiUm DwFd4fxmstqsgGFWhhl68kFUPNywdBAo3DQLpeVEBGUMl6DYmR6xBnBAvF5Lry1RVQ/q 2kjYAkXCUEwyUpbHXT7IGUvAFtQ2RMIFaek5lVSgVPKtNXIFCb7MIrq9E+7CIWAWdOYn X/ig== X-Gm-Message-State: AOAM530UUwnvQ75QSRELtSzzJvUcAbn7M2C4UnqmkjVOLlbZdqITsAsI I9Jhq6MHeqwFrHeYaJAFsjMXWjqzAydZwcfs4q9H+kgWmBjVZPEFPzl0rPnv/UfwulS/vHMtXsQ J0N13tRvbeBrpD1YTaYv3T9NhVgJxnSWLEgeu2bcdW/qwvCiVL5AevYcMo3KUB44= X-Google-Smtp-Source: ABdhPJxkjLq9Dx50SYSrgCivVULyC3y8/wLmvMzyUv5msIzKxlDdRfDu5S++fzacuuvfpWnedYEgBLYlDJ6QjA== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a05:6a00:230d:b0:49f:b8ad:ae23 with SMTP id h13-20020a056a00230d00b0049fb8adae23mr11205563pfh.80.1637196830557; Wed, 17 Nov 2021 16:53:50 -0800 (PST) Date: Wed, 17 Nov 2021 16:53:25 -0800 In-Reply-To: <20211118005325.64971-1-chooglen@google.com> Message-Id: <20211118005325.64971-6-chooglen@google.com> Mime-Version: 1.0 References: <20211118005325.64971-1-chooglen@google.com> X-Mailer: git-send-email 2.34.0.rc1.387.gb447b232ab-goog Subject: [PATCH v5 5/5] remote: die if branch is not found in repository From: Glen Choo To: git@vger.kernel.org Cc: Jonathan Tan , 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 replacing the branches list with a branches_hash hashmap. 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 | 84 +++++++++++++++++++++++++++++++++++++++++++++----------- remote.h | 5 ++-- 2 files changed, 70 insertions(+), 19 deletions(-) diff --git a/remote.c b/remote.c index 88697e7e6d..c97c626eaa 100644 --- a/remote.c +++ b/remote.c @@ -166,25 +166,74 @@ 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); CALLOC_ARRAY(ret, 1); - remote_state->branches[remote_state->branches_nr++] = ret; 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 +549,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 +575,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 +587,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 +1934,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 +2709,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; } @@ -2667,11 +2725,5 @@ void remote_state_clear(struct remote_state *remote_state) 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; + hashmap_clear_and_free(&remote_state->branches_hash, struct remote, ent); } diff --git a/remote.h b/remote.h index 85a730b8ef..4a1209ae2c 100644 --- a/remote.h +++ b/remote.h @@ -43,9 +43,7 @@ struct remote_state { int remotes_nr; struct hashmap remotes_hash; - struct branch **branches; - int branches_alloc; - int branches_nr; + struct hashmap branches_hash; struct branch *current_branch; const char *pushremote_name; @@ -292,6 +290,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;