From: Junio C Hamano <gitster@pobox.com>
To: Jeff King <peff@peff.net>
Cc: Anders Kaseorg <andersk@mit.edu>,
git@vger.kernel.org, Andreas Heiduk <andreas.heiduk@mathema.de>
Subject: Re: [PATCH v2] fetch: Protect branches checked out in all worktrees
Date: Fri, 05 Nov 2021 11:39:03 -0700 [thread overview]
Message-ID: <xmqqbl2yxxa0.fsf@gitster.g> (raw)
In-Reply-To: <YYUbQqyYQDD5QEAz@coredump.intra.peff.net> (Jeff King's message of "Fri, 5 Nov 2021 07:53:38 -0400")
Jeff King <peff@peff.net> writes:
> But all of that means we could actually drop check_not_current_branch()
> in favor of the update_local_ref() check. Doing this:
>
> diff --git a/builtin/fetch.c b/builtin/fetch.c
> index f7abbc31ff..c52c44684a 100644
> --- a/builtin/fetch.c
> +++ b/builtin/fetch.c
> @@ -869,7 +869,7 @@ static int update_local_ref(struct ref *ref,
> }
>
> if (current_branch &&
> - !strcmp(ref->name, current_branch->name) &&
> + !strcmp(ref->name, current_branch->refname) &&
> !(update_head_ok || is_bare_repository()) &&
> !is_null_oid(&ref->old_oid)) {
> /*
> @@ -1385,20 +1385,6 @@ static int prune_refs(struct refspec *rs, struct ref *ref_map,
> return result;
> }
>
> -static void check_not_current_branch(struct ref *ref_map)
> -{
> - struct branch *current_branch = branch_get(NULL);
> -
> - if (is_bare_repository() || !current_branch)
> - return;
> -
> - for (; ref_map; ref_map = ref_map->next)
> - if (ref_map->peer_ref && !strcmp(current_branch->refname,
> - ref_map->peer_ref->name))
> - die(_("Refusing to fetch into current branch %s "
> - "of non-bare repository"), current_branch->refname);
> -}
> -
> static int truncate_fetch_head(void)
> {
> const char *filename = git_path_fetch_head(the_repository);
> @@ -1587,8 +1573,6 @@ static int do_fetch(struct transport *transport,
>
> ref_map = get_ref_map(transport->remote, remote_refs, rs,
> tags, &autotags);
> - if (!update_head_ok)
> - check_not_current_branch(ref_map);
>
> if (tags == TAGS_DEFAULT && autotags)
> transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
>
> passes the entire test suite except for one test which expects:
>
> git fetch . side:main
>
> to fail. But that is only because "side" and "main" point to the same
> commit, and thus the fetch is a noop. The code in update_local_ref()
> covers that case before checking the HEAD case (which I would argue is
> a completely reasonable outcome).
So we can lose redundant code that was added there only because the
original safety was broken by actually fixing the original safety?
That is very nice.
> The reason I bring this up is that I think doing the check in
> update_local_ref() makes much more sense. We don't abort the whole
> fetch, but just treat it as a normal per-ref failure. That gives us the
> usual status-table output (I thought it might also avoid wasting some
> work of actually fetching objects, but I think the current check kicks
> in before we actually fetch anything).
Yes I agree with that reasoning.
Thanks for digging this to the bottom, both of you.
next prev parent reply other threads:[~2021-11-05 18:39 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-04 21:05 [PATCH] fetch: Protect branches checked out in all worktrees Anders Kaseorg
2021-11-04 21:10 ` [PATCH v2] " Anders Kaseorg
2021-11-05 8:38 ` Jeff King
2021-11-05 10:01 ` Anders Kaseorg
2021-11-05 11:53 ` Jeff King
2021-11-05 18:39 ` Junio C Hamano [this message]
2021-11-08 20:12 ` Anders Kaseorg
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=xmqqbl2yxxa0.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=andersk@mit.edu \
--cc=andreas.heiduk@mathema.de \
--cc=git@vger.kernel.org \
--cc=peff@peff.net \
/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).