From: Glen Choo <chooglen@google.com>
To: git@vger.kernel.org
Cc: Glen Choo <chooglen@google.com>
Subject: [PATCH v1 3/3] branch: remove implicit the_repository from create_branch()
Date: Thu, 11 Nov 2021 09:16:43 -0800 [thread overview]
Message-ID: <20211111171643.13805-4-chooglen@google.com> (raw)
In-Reply-To: <20211111171643.13805-1-chooglen@google.com>
create_branch() takes a struct repository parameter, which implies that
it behaves as expected when called on any repository. However, this is
not true because it depends on functions that ultimately use
the_repository.
Remove all implicit references to the_repository in create_branch().
This is achieved by replacing a function with a variant that accepts
struct repository or struct ref_store (typically named repo_* or
refs_*). For functions defined in branch.c, add a struct repository
parameter where necessary.
validate_new_branchname() still has an implicit reference to
the_repository via is_bare_repository(), which uses environment.c's
global state. Instead of pretending that we can tell if an arbitrary
repository is bare, validate_new_branchname() will simply die if r !=
the_repository. This shortcoming will only show itself if the user works
across multiple repositories in-process and some of them are bare -
which is probably rare in practice.
Signed-off-by: Glen Choo <chooglen@google.com>
---
branch.c | 69 +++++++++++++++++++++++++++++++---------------
branch.h | 9 ++++--
builtin/branch.c | 5 ++--
builtin/checkout.c | 7 +++--
4 files changed, 61 insertions(+), 29 deletions(-)
diff --git a/branch.c b/branch.c
index 5cc105ff8d..688da607cd 100644
--- a/branch.c
+++ b/branch.c
@@ -5,6 +5,7 @@
#include "refs.h"
#include "refspec.h"
#include "remote.h"
+#include "repository.h"
#include "sequencer.h"
#include "commit.h"
#include "worktree.h"
@@ -55,7 +56,9 @@ N_("\n"
"the remote tracking information by invoking\n"
"\"git branch --set-upstream-to=%s%s%s\".");
-int install_branch_config(int flag, const char *local, const char *origin, const char *remote)
+int repo_install_branch_config(struct repository *r, int flag,
+ const char *local, const char *origin,
+ const char *remote)
{
const char *shortname = NULL;
struct strbuf key = STRBUF_INIT;
@@ -70,18 +73,18 @@ int install_branch_config(int flag, const char *local, const char *origin, const
}
strbuf_addf(&key, "branch.%s.remote", local);
- if (git_config_set_gently(key.buf, origin ? origin : ".") < 0)
+ if (repo_config_set_gently(r, key.buf, origin ? origin : ".") < 0)
goto out_err;
strbuf_reset(&key);
strbuf_addf(&key, "branch.%s.merge", local);
- if (git_config_set_gently(key.buf, remote) < 0)
+ if (repo_config_set_gently(r, key.buf, remote) < 0)
goto out_err;
if (rebasing) {
strbuf_reset(&key);
strbuf_addf(&key, "branch.%s.rebase", local);
- if (git_config_set_gently(key.buf, "true") < 0)
+ if (repo_config_set_gently(r, key.buf, "true") < 0)
goto out_err;
}
strbuf_release(&key);
@@ -126,6 +129,13 @@ int install_branch_config(int flag, const char *local, const char *origin, const
return -1;
}
+int install_branch_config(int flag, const char *local, const char *origin,
+ const char *remote)
+{
+ return repo_install_branch_config(the_repository, flag, local, origin,
+ remote);
+}
+
static int inherit_tracking(struct tracking *tracking, const char *orig_ref)
{
struct strbuf key = STRBUF_INIT;
@@ -164,8 +174,9 @@ static int inherit_tracking(struct tracking *tracking, const char *orig_ref)
* to infer the settings for branch.<new_ref>.{remote,merge} from the
* config.
*/
-static void setup_tracking(const char *new_ref, const char *orig_ref,
- enum branch_track track, int quiet)
+static void setup_tracking(struct repository *r, const char *new_ref,
+ const char *orig_ref, enum branch_track track,
+ int quiet)
{
struct tracking tracking;
int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
@@ -173,7 +184,7 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
memset(&tracking, 0, sizeof(tracking));
tracking.spec.dst = (char *)orig_ref;
if (track != BRANCH_TRACK_INHERIT) {
- for_each_remote(find_tracked_branch, &tracking);
+ repo_for_each_remote(r, find_tracked_branch, &tracking);
} else if (inherit_tracking(&tracking, orig_ref))
return;
@@ -191,8 +202,9 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,
die(_("Not tracking: ambiguous information for ref %s"),
orig_ref);
- if (install_branch_config(config_flags, new_ref, tracking.remote,
- tracking.src ? tracking.src : orig_ref) < 0)
+ if (repo_install_branch_config(
+ r, config_flags, new_ref, tracking.remote,
+ tracking.src ? tracking.src : orig_ref) < 0)
exit(-1);
free(tracking.src);
@@ -218,12 +230,13 @@ int read_branch_desc(struct strbuf *buf, const char *branch_name)
* Return 1 if the named branch already exists; return 0 otherwise.
* Fill ref with the full refname for the branch.
*/
-int validate_branchname(const char *name, struct strbuf *ref)
+int validate_branchname(struct repository *r, const char *name,
+ struct strbuf *ref)
{
if (strbuf_check_branch_ref(ref, name))
die(_("'%s' is not a valid branch name."), name);
- return ref_exists(ref->buf);
+ return refs_ref_exists(get_main_ref_store(r), ref->buf);
}
/*
@@ -232,19 +245,31 @@ int validate_branchname(const char *name, struct strbuf *ref)
* Return 1 if the named branch already exists; return 0 otherwise.
* Fill ref with the full refname for the branch.
*/
-int validate_new_branchname(const char *name, struct strbuf *ref, int force)
+int validate_new_branchname(struct repository *r, const char *name,
+ struct strbuf *ref, int force)
{
const char *head;
+ int ignore_errno;
- if (!validate_branchname(name, ref))
+ if (!validate_branchname(r, name, ref))
return 0;
if (!force)
die(_("A branch named '%s' already exists."),
ref->buf + strlen("refs/heads/"));
- head = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
- if (!is_bare_repository() && head && !strcmp(head, ref->buf))
+ head = refs_resolve_ref_unsafe(get_main_ref_store(r), "HEAD", 0, NULL,
+ NULL, &ignore_errno);
+
+ /*
+ * If we would move the repository head, allow the move only if
+ * the repository is bare.
+ *
+ * NEEDSWORK because we don't have an easy way to check if a
+ * non-the_repository is bare, fail if r is not the_repository.
+ */
+ if (head && !strcmp(head, ref->buf) &&
+ (r != the_repository || !is_bare_repository()))
die(_("Cannot force update the current branch."));
return 1;
@@ -294,9 +319,9 @@ void create_branch(struct repository *r,
if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
explicit_tracking = 1;
- if ((track == BRANCH_TRACK_OVERRIDE || clobber_head_ok)
- ? validate_branchname(name, &ref)
- : validate_new_branchname(name, &ref, force)) {
+ if ((track == BRANCH_TRACK_OVERRIDE || clobber_head_ok) ?
+ validate_branchname(r, name, &ref) :
+ validate_new_branchname(r, name, &ref, force)) {
if (!force)
dont_change_ref = 1;
else
@@ -304,7 +329,7 @@ void create_branch(struct repository *r,
}
real_ref = NULL;
- if (get_oid_mb(start_name, &oid)) {
+ if (repo_get_oid_mb(r, start_name, &oid)) {
if (explicit_tracking) {
if (advice_enabled(ADVICE_SET_UPSTREAM_FAILURE)) {
error(_(upstream_missing), start_name);
@@ -316,7 +341,7 @@ void create_branch(struct repository *r,
die(_("Not a valid object name: '%s'."), start_name);
}
- switch (dwim_ref(start_name, strlen(start_name), &oid, &real_ref, 0)) {
+ switch (repo_dwim_ref(r, start_name, strlen(start_name), &oid, &real_ref, 0)) {
case 0:
/* Not branching from any existing branch */
if (explicit_tracking)
@@ -354,7 +379,7 @@ void create_branch(struct repository *r,
else
msg = xstrfmt("branch: Created from %s", start_name);
- transaction = ref_transaction_begin(&err);
+ transaction = ref_store_transaction_begin(get_main_ref_store(r), &err);
if (!transaction ||
ref_transaction_update(transaction, ref.buf,
&oid, forcing ? NULL : null_oid(),
@@ -367,7 +392,7 @@ void create_branch(struct repository *r,
}
if (real_ref && track)
- setup_tracking(ref.buf + 11, real_ref, track, quiet);
+ setup_tracking(r, ref.buf + 11, real_ref, track, quiet);
strbuf_release(&ref);
free(real_ref);
diff --git a/branch.h b/branch.h
index 6484bda8a2..0177528304 100644
--- a/branch.h
+++ b/branch.h
@@ -51,7 +51,8 @@ void create_branch(struct repository *r,
* Return 1 if the named branch already exists; return 0 otherwise.
* Fill ref with the full refname for the branch.
*/
-int validate_branchname(const char *name, struct strbuf *ref);
+int validate_branchname(struct repository *r, const char *name,
+ struct strbuf *ref);
/*
* Check if a branch 'name' can be created as a new branch; die otherwise.
@@ -59,7 +60,8 @@ int validate_branchname(const char *name, struct strbuf *ref);
* Return 1 if the named branch already exists; return 0 otherwise.
* Fill ref with the full refname for the branch.
*/
-int validate_new_branchname(const char *name, struct strbuf *ref, int force);
+int validate_new_branchname(struct repository *r, const char *name,
+ struct strbuf *ref, int force);
/*
* Remove information about the merge state on the current
@@ -79,6 +81,9 @@ void remove_branch_state(struct repository *r, int verbose);
* Returns 0 on success.
*/
#define BRANCH_CONFIG_VERBOSE 01
+int repo_install_branch_config(struct repository *r, int flag,
+ const char *local, const char *origin,
+ const char *remote);
int install_branch_config(int flag, const char *local, const char *origin, const char *remote);
/*
diff --git a/builtin/branch.c b/builtin/branch.c
index 1fb4b57ca9..46fe7cb634 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -531,9 +531,10 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
* cause the worktree to become inconsistent with HEAD, so allow it.
*/
if (!strcmp(oldname, newname))
- validate_branchname(newname, &newref);
+ validate_branchname(the_repository, newname, &newref);
else
- validate_new_branchname(newname, &newref, force);
+ validate_new_branchname(the_repository, newname, &newref,
+ force);
reject_rebase_or_bisect_branch(oldref.buf);
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 2b9501520c..c98ba384dd 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1762,10 +1762,11 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
struct strbuf buf = STRBUF_INIT;
if (opts->new_branch_force)
- opts->branch_exists = validate_branchname(opts->new_branch, &buf);
+ opts->branch_exists = validate_branchname(
+ the_repository, opts->new_branch, &buf);
else
- opts->branch_exists =
- validate_new_branchname(opts->new_branch, &buf, 0);
+ opts->branch_exists = validate_new_branchname(
+ the_repository, opts->new_branch, &buf, 0);
strbuf_release(&buf);
}
--
2.33.GIT
prev parent reply other threads:[~2021-11-11 17:17 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-11 17:16 [PATCH v1 0/3] make create_branch() accept any repository Glen Choo
2021-11-11 17:16 ` [PATCH v1 1/3] refs/files-backend: remove the_repository Glen Choo
2021-11-11 17:16 ` [PATCH v1 2/3] config: introduce repo_config_set* functions Glen Choo
2021-11-11 20:24 ` Junio C Hamano
2021-11-12 0:45 ` Glen Choo
2021-11-15 22:17 ` Glen Choo
2021-11-11 17:16 ` Glen Choo [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211111171643.13805-4-chooglen@google.com \
--to=chooglen@google.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).