From patchwork Tue Oct 19 22:43:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12571275 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 E37C9C433F5 for ; Tue, 19 Oct 2021 22:43:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B7001610A1 for ; Tue, 19 Oct 2021 22:43:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229809AbhJSWqE (ORCPT ); Tue, 19 Oct 2021 18:46:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229548AbhJSWqC (ORCPT ); Tue, 19 Oct 2021 18:46:02 -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 67CFFC06161C for ; Tue, 19 Oct 2021 15:43:49 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id e6-20020a637446000000b002993ba24bbaso12346110pgn.12 for ; Tue, 19 Oct 2021 15:43:49 -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=JJ6Stf2FOm8TfSZmvbwtPeD+WJ1VJKl3MJehYwP9Zmk=; b=cVJvitm4rSJYf1i1l7gyPCP1sT/l7kwt+kH413lxOGoxS0v+ETlCQW8ZQL7moCQOvd VLKiEMjt7Ng0vmGO/MsEpRqbqkZF54Pl/5nwpD4Vp7UHsnMO7IZTyLRHHBVyXbmFaZeE k+cz9tAr6bMM8J1liQyEFpT+QJ5YOSTYUs7LxyqeLI0YLHFI+Vh3TFIDh9hDR0ZX8hg5 Mo0EYI5iWjSrnD2FvImpFRmCOne1f/PcCUawi/7TxW012H4QSm/XatPotFY8c6S7KCds Ggq2zWx1uteLOOKh8mxF7s9G5mxRBeerCH5Etu65iuQs2JN0XdmMoBORyd/Rw/heOAoA ht6w== 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=JJ6Stf2FOm8TfSZmvbwtPeD+WJ1VJKl3MJehYwP9Zmk=; b=IZWAHisxMUi84UX+xBrKttLyOp+CEi6XleCBr009wpLgrwUuS7OUIQtFalsIZLEPVl poyYkwtGD0LEP89pb243UovlNg+9KZtfOQmumja04FZsHUpOLd7JMr4cMW1hZ40OlTnu WgvwVhJcWDjksRJZkabMwGRMG5ReTaOc9WvdAyK8SNpUo8KJV9xw0zq2OO2tHyJ+kOKn D+CEe9MxFPJhBRv97NOjNPA4ojWiPTkN+/uWPkXFTjXZ1BENOSyiccuguOKnSzo9sY2N ue9sdFH6TZr5uTQBxhNKxKBCNQIM2gVzZMlUNaKNhYYf8pjhrVHmMyTWOZMRvlZKj1Wr QNDA== X-Gm-Message-State: AOAM5314P8wTBQfjByZkUEf6Oq/7E5MXoTk5GQB8AVfVJi9QqmsKrlXX KGPV/aK/4DuQsOXO/I4eqmSlpJuB+YR5Aoa5mYK86WUW99/VOPnvQv21OrXCwPxik8ji+MHRYv2 7zELUHOV5xpKB99+iH73jjI+cKPynhvTJjzspPnTmD3p8MV4JeXVz/rv96tvnMyE= X-Google-Smtp-Source: ABdhPJyWYSA/lBqGOXK1ZgN1eekexICNOc24ARF0yczKU28QcLcAcqWt0LWHoZFs02yRqKJaPCTdQGVjuQeA8w== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a17:902:968a:b0:133:e2c5:4908 with SMTP id n10-20020a170902968a00b00133e2c54908mr35802634plp.2.1634683427921; Tue, 19 Oct 2021 15:43:47 -0700 (PDT) Date: Tue, 19 Oct 2021 15:43:36 -0700 In-Reply-To: <20211019224339.61881-1-chooglen@google.com> Message-Id: <20211019224339.61881-2-chooglen@google.com> Mime-Version: 1.0 References: <20211013193127.76537-1-chooglen@google.com> <20211019224339.61881-1-chooglen@google.com> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog Subject: [PATCH v3 1/4] 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 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 | 187 ++++++++++++++++++++++++++++++++++----------------- remote.h | 34 ++++++++++ repository.c | 8 +++ repository.h | 4 ++ 4 files changed, 171 insertions(+), 62 deletions(-) diff --git a/remote.c b/remote.c index f958543d70..29c29fcc3b 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); } @@ -129,8 +104,9 @@ static int remotes_hash_cmp(const void *unused_cmp_data, static inline void init_remotes_hash(void) { - if (!remotes_hash.cmpfn) - hashmap_init(&remotes_hash, remotes_hash_cmp, NULL, 0); + if (!the_repository->remote_state->remotes_hash.cmpfn) + hashmap_init(&the_repository->remote_state->remotes_hash, + remotes_hash_cmp, NULL, 0); } static struct remote *make_remote(const char *name, int len) @@ -147,7 +123,8 @@ static struct remote *make_remote(const char *name, int len) 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 +135,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 +182,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 +335,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 +354,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 +439,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 +480,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 +524,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 +565,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 +605,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 +1719,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 +2619,32 @@ 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)); + 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 Tue Oct 19 22:43:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12571277 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 051CAC433EF for ; Tue, 19 Oct 2021 22:43:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D047261154 for ; Tue, 19 Oct 2021 22:43:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229790AbhJSWqG (ORCPT ); Tue, 19 Oct 2021 18:46:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36756 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229706AbhJSWqE (ORCPT ); Tue, 19 Oct 2021 18:46:04 -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 98E10C061746 for ; Tue, 19 Oct 2021 15:43:50 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id n9-20020a63f809000000b0026930ed1b24so12314233pgh.23 for ; Tue, 19 Oct 2021 15:43:50 -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=gmxLhRZrORi4D9PP/yfXpBpmU3cLSKRAedRgN9mijcQ=; b=Dj9yotDPLOi6FjZ7tRab7Hv7cQGPqspQVl5esznHJ5u6ryIVyW40VRO7O1w8Jh3wag sSxEXnxsNA5Mmrd7FOBcm7mE9ShawVjeZWJ2VSTLS0ZL/fCzErd2Llp9USAoK4PS2O8o zyf+8WKPsjzFtgjEAfu/XcC0uq4scgPYzeO/uexRXdsIZF+daZy/bgAsD0rChdY5xgMq q6WXS9Stj+rqYCQqmZ3C9zgTN9kCh+L2N/MGYy7jIZJke7l7gROKH23X29M8rGvX1C2i xusBmr2aS+ZnuWtfugUEZEuRYHBASbMwqiyvCZsL/tYHqhYde5xLoPk0XQD7XAkHSlJm DcUQ== 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=gmxLhRZrORi4D9PP/yfXpBpmU3cLSKRAedRgN9mijcQ=; b=YH3Z/XkM2/JDOjLAmGgC0SflARi2eRyKPasr5d4X3ezv96YuBJnVn2mDQ/ht5Z4zrX iJIzFUYGZgYwvTy6bn+zbqBD76Zq+BCpIT3QfilV1HoD3zTBF2jmAiOVtXnlI6TVzhAS v3rT3zG+2Tf0/3/R0THKzw45nUb7uikj0fPNwgKEt3h/VLfDXEaKUbE6dDTuaSyW5KIs SyU1Gd2pz4W1aqIau8Rqv831CD+/Zub43SkE6ei+kaqxyrtNUHe2Q5phVbwYgPpWdv1b EELSIWuGpwISLIuw+nrbQ0SB1X8AYv0YYTWALOXteCmnICgckhQNDHUM6TtBmI/JIq3y 0HIQ== X-Gm-Message-State: AOAM532LeZXT85DdanLH0hxAwNfX8dlDbImzO1Gac9DiewhhR8mkBQz1 3hT9wFJRHcs68REuSzCikppqsfOrMH6kqRLil2NkRH8GLpEUv+DxV9/gG9EgFhoZJapP0E/DPXU or0KI4x1abBT6xKbPg0cgqEmeo2EMzhOmRZbgNkIe5OizzaijvOWHbz73I5hkGC4= X-Google-Smtp-Source: ABdhPJzf9wDAXoMeEcxZZqwqQffF2/CgUpSIWAE46U8Q/eymIpGDVA3nWy3JWkDQcmcLUiiiDqyYv/PaouFofQ== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a17:90b:4d0f:: with SMTP id mw15mr3019450pjb.207.1634683429988; Tue, 19 Oct 2021 15:43:49 -0700 (PDT) Date: Tue, 19 Oct 2021 15:43:37 -0700 In-Reply-To: <20211019224339.61881-1-chooglen@google.com> Message-Id: <20211019224339.61881-3-chooglen@google.com> Mime-Version: 1.0 References: <20211013193127.76537-1-chooglen@google.com> <20211019224339.61881-1-chooglen@google.com> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog Subject: [PATCH v3 2/4] 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 Introduce a struct remote_state member to structs that need to 'remember' their remote_state. Without changing external-facing functions, replace the_repository->remote_state internally by using the remote_state member where it is applicable i.e. when a function accepts a struct that depends on the remote_state. If it is not applicable, add a struct remote_state parameter instead. 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 | 153 ++++++++++++++++++++++++++----------------------------- remote.h | 10 ++++ 2 files changed, 81 insertions(+), 82 deletions(-) diff --git a/remote.c b/remote.c index 29c29fcc3b..e3ca44f735 100644 --- a/remote.c +++ b/remote.c @@ -68,15 +68,14 @@ 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, &the_repository->remote_state->rewrites_push); + alias_url(url, &remote->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, &the_repository->remote_state->rewrites)); + add_url(remote, alias_url(url, &remote->remote_state->rewrites)); add_pushurl_alias(remote, url); } @@ -102,14 +101,19 @@ static int remotes_hash_cmp(const void *unused_cmp_data, return strcmp(a->name, b->name); } -static inline void init_remotes_hash(void) +/** + * NEEDSWORK: Now that the hashmap is in a struct, this should probably + * just be moved into remote_state_new(). + */ +static inline void init_remotes_hash(struct remote_state *remote_state) { - if (!the_repository->remote_state->remotes_hash.cmpfn) - hashmap_init(&the_repository->remote_state->remotes_hash, - remotes_hash_cmp, NULL, 0); + if (!remote_state->remotes_hash.cmpfn) + hashmap_init(&remote_state->remotes_hash, remotes_hash_cmp, + NULL, 0); } -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; @@ -118,13 +122,12 @@ static struct remote *make_remote(const char *name, int len) if (!len) len = strlen(name); - init_remotes_hash(); + init_remotes_hash(remote_state); lookup.str = name; 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); @@ -132,18 +135,16 @@ static struct remote *make_remote(const char *name, int len) ret->prune = -1; /* unspecified */ ret->prune_tags = -1; /* unspecified */ ret->name = xstrndup(name, len); + ret->remote_state = remote_state; 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; } @@ -177,27 +178,25 @@ 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); + ret->remote_state = remote_state; return ret; } @@ -313,10 +312,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")) { @@ -335,16 +336,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)); } } @@ -354,9 +353,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; @@ -366,7 +364,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) @@ -436,61 +434,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->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) @@ -524,10 +512,10 @@ const char *pushremote_for_branch(struct branch *branch, int *explicit) *explicit = 1; return branch->pushremote_name; } - if (the_repository->remote_state->pushremote_name) { + if (branch->remote_state->pushremote_name) { if (explicit) *explicit = 1; - return the_repository->remote_state->pushremote_name; + return branch->remote_state->pushremote_name; } return remote_for_branch(branch, explicit); } @@ -560,7 +548,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; @@ -568,7 +556,7 @@ 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); @@ -604,7 +592,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 = @@ -1717,11 +1705,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..d268a92ebb 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); @@ -110,6 +112,10 @@ struct remote { /* The method used for authenticating against `http_proxy`. */ char *http_proxy_authmethod; + + /** The remote_state that this remote belongs to. This is only meant to + * be used by remote_* functions. */ + struct remote_state *remote_state; }; /** @@ -318,6 +324,10 @@ struct branch { int merge_alloc; const char *push_tracking_ref; + + /** The remote_state that this branch belongs to. This is only meant to + * be used by branch_* functions. */ + struct remote_state *remote_state; }; struct branch *branch_get(const char *name); From patchwork Tue Oct 19 22:43:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12571279 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 03E49C4332F for ; Tue, 19 Oct 2021 22:43:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C92A361154 for ; Tue, 19 Oct 2021 22:43:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229842AbhJSWqH (ORCPT ); Tue, 19 Oct 2021 18:46:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229784AbhJSWqF (ORCPT ); Tue, 19 Oct 2021 18:46:05 -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 80B6EC06161C for ; Tue, 19 Oct 2021 15:43:52 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id l10-20020a17090ac58a00b001a04b92a5d4so661170pjt.8 for ; Tue, 19 Oct 2021 15:43:52 -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=RK8CQc84tN6Wwg4rDYHsQ0Tw91F8B5aIaLcZEOgqg+M=; b=drgR94pXr7rRsBnV3VWnJsZzDfT82K8wpVxVrG671NoqyNG4nPNJgm2bsEiDHQ4ceQ mwxE8PiOX4Z606HhX0uymPi8mOcKzHfHLVfPSG1DyF3iJayryrFbRcjf91gfpUbDL971 eGy6wjeIouLgo5uH9lq080NWLXdk4A0HD3tkMZp6CFf3tUHhMITjyZh5C1Mg5gnN4Ozm av8dLxysmwqBO+kvxbefgUfw22U/TmMpMAvBP8rriVtADWSuPIo4WfBF0L2xUGCqezWH T7lV5daqcHDPT1eATe0GTUKNqFWFX/hTUDts0qfbrCwLPzZzBjiLQdhORF4T6nkhfwyK pd1g== 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=RK8CQc84tN6Wwg4rDYHsQ0Tw91F8B5aIaLcZEOgqg+M=; b=Z6y1F4AECi1GHdqt2za9akfKyPtJTjhCFJpd8+PU0u7mfY05hsALFbe1AMHpBikdvb uZLY493jbVAkWfa4gLbPDaJZ8zRUSVP1WxbyS+wBar9OwSyI5/CrAh87eRkSswJ0g6cl XuGH2PcIUTzucGfj8bTBzZVRipIZy2qeU8Lw2gmC+C9iWfbhWLXPwXfZxGV146317bPs nQyGZZ5e6vuPaHbDT46aCFzAVu2lIOcwzAh4UubGumwOWw7nVq2jFuG7q/rNyikh176G fWbOq53WjoWyLBfshSF5Gf3c+DB/jmaREFuTHh+9bRfxkmV2cfkOhV/uOmunb6phgd3L R+Og== X-Gm-Message-State: AOAM530KPElscElRJ236Tk6zMRiKYsUTNRx3A6gvjtCiH9N9Hn881oVY zoN7fUYpCG92M0R5ek2yfQzamwegA6wTV1UhpNgOUe/PSVKt//OLhUt4oHz+dpI+pkwc8IwxFkA bLkX2uc4IdmZVuM2QoeJwPsdmb8Me0jQEB+BD+5EYTdT0kCUXAfn2M0eFyZEACJM= X-Google-Smtp-Source: ABdhPJxrW9OVZMHGJ/vCX30iLjv7aaovAlKve10MYotR4Sznu8xucpC//IATUbRk1aSWoWeU+mSkaC17+T5TOg== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:a05:6a00:21c6:b0:44c:937:fbf3 with SMTP id t6-20020a056a0021c600b0044c0937fbf3mr2655472pfj.2.1634683431846; Tue, 19 Oct 2021 15:43:51 -0700 (PDT) Date: Tue, 19 Oct 2021 15:43:38 -0700 In-Reply-To: <20211019224339.61881-1-chooglen@google.com> Message-Id: <20211019224339.61881-4-chooglen@google.com> Mime-Version: 1.0 References: <20211013193127.76537-1-chooglen@google.com> <20211019224339.61881-1-chooglen@google.com> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog Subject: [PATCH v3 3/4] 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 methods with a struct remote_state parameter. To do so, introduce a family of helper functions, "remotes_*", that behave like "repo_*", but accept struct remote_state instead of struct repository, and move read_config() calls to non-static functions. Signed-off-by: Glen Choo --- remote.c | 94 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 41 deletions(-) diff --git a/remote.c b/remote.c index e3ca44f735..cf9cced5ed 100644 --- a/remote.c +++ b/remote.c @@ -505,6 +505,55 @@ const char *remote_for_branch(struct branch *branch, int *explicit) return "origin"; } +static struct remote *remotes_remote_get_1( + struct remote_state *remote_state, const char *name, + const char *(*get_default)(struct branch *, int *)) +{ + struct remote *ret; + int name_given = 0; + + if (name) + name_given = 1; + else + name = get_default(remote_state->current_branch, + &name_given); + + ret = make_remote(remote_state, name, 0); + if (valid_remote_nick(name) && have_git_dir()) { + if (!valid_remote(ret)) + read_remotes_file(ret); + if (!valid_remote(ret)) + read_branches_file(ret); + } + if (name_given && !valid_remote(ret)) + add_url_alias(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, remote_for_branch); +} + +static inline struct remote *remotes_pushremote_get(struct remote_state *remote_state, const char *name) +{ + return remotes_remote_get_1(remote_state, name, pushremote_for_branch); +} + +struct remote *remote_get(const char *name) +{ + read_config(the_repository); + return remotes_remote_get(the_repository->remote_state, name); +} + +struct remote *pushremote_get(const char *name) +{ + read_config(the_repository); + return remotes_pushremote_get(the_repository->remote_state, name); +} + const char *pushremote_for_branch(struct branch *branch, int *explicit) { if (branch && branch->pushremote_name) { @@ -530,7 +579,8 @@ const char *remote_ref_for_branch(struct branch *branch, int for_push) } else { const char *dst, *remote_name = pushremote_for_branch(branch, NULL); - struct remote *remote = remote_get(remote_name); + struct remote *remote = + remotes_remote_get(branch->remote_state, remote_name); if (remote && remote->push.nr && (dst = apply_refspecs(&remote->push, @@ -542,44 +592,6 @@ 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 *)) -{ - 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_given); - - 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); - if (!valid_remote(ret)) - read_branches_file(ret); - } - if (name_given && !valid_remote(ret)) - add_url_alias(ret, name); - if (!valid_remote(ret)) - return NULL; - return ret; -} - -struct remote *remote_get(const char *name) -{ - return remote_get_1(name, remote_for_branch); -} - -struct remote *pushremote_get(const char *name) -{ - return remote_get_1(name, pushremote_for_branch); -} - int remote_is_configured(struct remote *remote, int in_repo) { if (!remote) @@ -1684,7 +1696,7 @@ static void set_merge(struct branch *ret) return; } - remote = remote_get(ret->remote_name); + remote = remotes_remote_get(ret->remote_state, ret->remote_name); CALLOC_ARRAY(ret->merge, ret->merge_nr); for (i = 0; i < ret->merge_nr; i++) { @@ -1786,7 +1798,7 @@ static const char *branch_get_push_1(struct branch *branch, struct strbuf *err) { struct remote *remote; - remote = remote_get(pushremote_for_branch(branch, NULL)); + remote = remotes_remote_get(branch->remote_state, pushremote_for_branch(branch, NULL)); if (!remote) return error_buf(err, _("branch '%s' has no remote for pushing"), From patchwork Tue Oct 19 22:43:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 12571281 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 85275C43217 for ; Tue, 19 Oct 2021 22:43:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6C247610A1 for ; Tue, 19 Oct 2021 22:43:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229928AbhJSWqK (ORCPT ); Tue, 19 Oct 2021 18:46:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229784AbhJSWqH (ORCPT ); Tue, 19 Oct 2021 18:46:07 -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 1E1BFC06161C for ; Tue, 19 Oct 2021 15:43:54 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id f9-20020a056a001ac900b0044c4f04a6b1so698454pfv.23 for ; Tue, 19 Oct 2021 15:43:54 -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=0s4p9GfsGdlSLDH0e3+SR+Rhz32UmN8nIoPnEaE49O0=; b=CRpRAa98iBrYaJKRTHFLfBgcYldOCgIBVE5FXzB1+K0yk+ECOLgpa3LUkZCWFhw1R1 Redig6OODqFtcRFGfdLhUh6FXkC7G49c1JsegzOp4HsbOVuUtl0vHNGvNQejS9Ox5xox AboMrZ+OiVctzezBHzG15YZRC8UWlKsri/1L3EIXf+qEECaAdGT3Sd6rl7DQBWqGqyO6 F//buNsBSPHuFdXMmhmQANeMXL/pAnZDIktpRGepkBKOBsCzEAkn14lqI6SE9F7vl9gz s/FP5ivmvEgkQt8dQ/2nWqhtSWejgBwBbjoW2jlX/guqunxVW5qAEM1qyOUue/WcxWNF heLQ== 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=0s4p9GfsGdlSLDH0e3+SR+Rhz32UmN8nIoPnEaE49O0=; b=8HK9N2l7EqE+H4/iVsX0Y6MekXESysx/2liOkkTiWO+uqJL6nZ5zb4DIRuZfrdO4GE N3IA62sUTEgt13z6NL7A0uepRpTK0ZrE3o62J7XI253ik3xrVVBvPj5IT3mw5BLXmRnU yDLnDFITLboiUdaVsdrKjRrWEvIlJHbWXRrM3MHrGV7rhQYtiINAPZrTsQB2OCYfTAYg hAoX4RIZajmE2cb30DnXeO49z+rAuafycbG0qjS8DFPhNyXMM2b44oU9AIEYW2CfWh3a H5iP0hYlEIHmiJDCaO93XFi2i+P3DyCkC03u/pvIaXGSwG6gG5/F+LVvX31b0q3E2fKD IhZw== X-Gm-Message-State: AOAM530zQHqG5m7iCS4V2T+HtGjGehkMmkuNRvZ2RQcuqV0PMWUScC+A UWR7nOesA52rKFW/ZhCoeWygyukn0E5PZPbb1oLlRyNRyl6yRbACF/87b2faiS6rSCoygvhQo6B bw4++ygTBWsa12YAzZFdtyTb8ZnIv/tsIqk2W6XDLOV1kxF/KDkZi7+56QaIeG9w= X-Google-Smtp-Source: ABdhPJyyJCCODrulqjNfDUuvQh0h5DxaoJ2BZcLZB4yUWchAdJPS7lwR74ON1ZjeTQhkbWIm6niyQlIGQTGuaA== X-Received: from chooglen.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:26d9]) (user=chooglen job=sendgmr) by 2002:aa7:9789:0:b0:44d:3a4b:b2b1 with SMTP id o9-20020aa79789000000b0044d3a4bb2b1mr2647133pfp.66.1634683433467; Tue, 19 Oct 2021 15:43:53 -0700 (PDT) Date: Tue, 19 Oct 2021 15:43:39 -0700 In-Reply-To: <20211019224339.61881-1-chooglen@google.com> Message-Id: <20211019224339.61881-5-chooglen@google.com> Mime-Version: 1.0 References: <20211013193127.76537-1-chooglen@google.com> <20211019224339.61881-1-chooglen@google.com> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog Subject: [PATCH v3 4/4] 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. While this removes all references to the_repository->remote_state, certain functions still use the_repository to parse oids. Signed-off-by: Glen Choo --- remote.c | 29 ++++++++++++++--------------- remote.h | 28 +++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/remote.c b/remote.c index cf9cced5ed..179f0ff1ab 100644 --- a/remote.c +++ b/remote.c @@ -542,16 +542,16 @@ static inline struct remote *remotes_pushremote_get(struct remote_state *remote_ return remotes_remote_get_1(remote_state, name, pushremote_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); } -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); } const char *pushremote_for_branch(struct branch *branch, int *explicit) @@ -601,14 +601,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); @@ -1713,16 +1713,15 @@ static void set_merge(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)); + ret = make_branch(repo->remote_state, name, strlen(name)); set_merge(ret); return ret; } diff --git a/remote.h b/remote.h index d268a92ebb..e19cae2718 100644 --- a/remote.h +++ b/remote.h @@ -123,15 +123,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); @@ -330,7 +343,12 @@ struct branch { struct remote_state *remote_state; }; -struct branch *branch_get(const char *name); +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 *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);