git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Anders Kaseorg <andersk@mit.edu>
Cc: git@vger.kernel.org,
	"Johannes Schindelin" <Johannes.Schindelin@gmx.de>,
	"Jeff King" <peff@peff.net>,
	"Andreas Heiduk" <andreas.heiduk@mathema.de>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Subject: Re: [PATCH v6 5/8] fetch: protect branches checked out in all worktrees
Date: Mon, 15 Nov 2021 21:49:08 -0800	[thread overview]
Message-ID: <xmqq1r3gd50r.fsf@gitster.g> (raw)
In-Reply-To: 20211113033358.2179376-6-andersk@mit.edu

Anders Kaseorg <andersk@mit.edu> writes:

> Refuse to fetch into the currently checked out branch of any working
> tree, not just the current one.
>
> Fixes this previously reported bug:
>
> https://public-inbox.org/git/cb957174-5e9a-5603-ea9e-ac9b58a2eaad@mathema.de
>
> As a side effect of using find_shared_symref, we’ll also refuse the
> fetch when we’re on a detached HEAD because we’re rebasing or bisecting
> on the branch in question. This seems like a sensible change.

Indeed.

> Signed-off-by: Anders Kaseorg <andersk@mit.edu>
> ---
>  builtin/fetch.c       | 75 +++++++++++++++++++++++--------------------
>  t/t5516-fetch-push.sh | 18 +++++++++++
>  2 files changed, 58 insertions(+), 35 deletions(-)
>
> diff --git a/builtin/fetch.c b/builtin/fetch.c
> index e5971fa6e5..f373252490 100644
> --- a/builtin/fetch.c
> +++ b/builtin/fetch.c
> @@ -28,6 +28,7 @@
>  #include "promisor-remote.h"
>  #include "commit-graph.h"
>  #include "shallow.h"
> +#include "worktree.h"
>  
>  #define FORCED_UPDATES_DELAY_WARNING_IN_MS (10 * 1000)
>  
> @@ -840,14 +841,13 @@ static void format_display(struct strbuf *display, char code,
>  
>  static int update_local_ref(struct ref *ref,
>  			    struct ref_transaction *transaction,
> -			    const char *remote,
> -			    const struct ref *remote_ref,
> -			    struct strbuf *display,
> -			    int summary_width)
> +			    const char *remote, const struct ref *remote_ref,
> +			    struct strbuf *display, int summary_width,
> +			    struct worktree **worktrees)
>  {
>  	struct commit *current = NULL, *updated;
>  	enum object_type type;
> -	struct branch *current_branch = branch_get(NULL);
> +	const struct worktree *wt;
>  	const char *pretty_ref = prettify_refname(ref->name);
>  	int fast_forward = 0;

Having to pass the parameter down to here through the

    ->do_fetch()
      ->backfill_tags() (or do_fetch() itself)
        ->consume_refs()
          ->store_updated_refs()
            ->update_local_ref()

callchain makes the "damage to the code" by the patch look larger
than it actually is.  The real change is ...

> @@ -862,16 +862,17 @@ static int update_local_ref(struct ref *ref,
>  		return 0;
>  	}
>  
> -	if (current_branch &&
> -	    !strcmp(ref->name, current_branch->name) &&
> -	    !(update_head_ok || is_bare_repository()) &&
> -	    !is_null_oid(&ref->old_oid)) {
> +	if (!update_head_ok &&
> +	    (wt = find_shared_symref(worktrees, "HEAD", ref->name)) &&
> +	    !wt->is_bare && !is_null_oid(&ref->old_oid)) {

... this part, which looks very sensible.

>  		 * If this is the head, and it's not okay to update
>  		 * the head, and the old value of the head isn't empty...
>  		 */
>  		format_display(display, '!', _("[rejected]"),
> -			       _("can't fetch in current branch"),
> +			       wt->is_current ?
> +				       _("can't fetch in current branch") :
> +				       _("checked out in another worktree"),
>  			       remote, pretty_ref, summary_width);
>  		return 1;
>  	}

> @@ -1643,7 +1647,7 @@ static int do_fetch(struct transport *transport,
>  				  "you need to specify exactly one branch with the --set-upstream option"));
>  		}
>  	}
> - skip:
> +skip:
>  	free_refs(ref_map);

;-)

I count 30 hits of "^ [a-z0-9]*:" and 255 hits of "^[a-z0-9]*:" in
our codebase.  It must be some developers used to subscribe to
"don't place the label abut the left edge" school but no longer, or
something like that.

The code changes all look good to me.

Thanks.

> diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh
> index 4db8edd9c8..36fb90f4b0 100755
> --- a/t/t5516-fetch-push.sh
> +++ b/t/t5516-fetch-push.sh
> @@ -1770,4 +1770,22 @@ test_expect_success 'denyCurrentBranch and worktrees' '
>  	git -C cloned push origin HEAD:new-wt &&
>  	test_must_fail git -C cloned push --delete origin new-wt
>  '
> +
> +test_expect_success 'refuse fetch to current branch of worktree' '
> +	test_when_finished "git worktree remove --force wt && git branch -D wt" &&
> +	git worktree add wt &&
> +	test_commit apple &&
> +	test_must_fail git fetch . HEAD:wt &&
> +	git fetch -u . HEAD:wt
> +'
> +
> +test_expect_success 'refuse fetch to current branch of bare repository worktree' '
> +	test_when_finished "rm -fr bare.git" &&
> +	git clone --bare . bare.git &&
> +	git -C bare.git worktree add wt &&
> +	test_commit banana &&
> +	test_must_fail git -C bare.git fetch .. HEAD:wt &&
> +	git -C bare.git fetch -u .. HEAD:wt
> +'
> +
>  test_done

  reply	other threads:[~2021-11-16  5:49 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-13  3:33 [PATCH v6 0/8] protect branches checked out in all worktrees Anders Kaseorg
2021-11-13  3:33 ` [PATCH v6 1/8] fetch: lowercase error messages Anders Kaseorg
2021-11-16  5:19   ` Junio C Hamano
2021-11-16  7:10     ` Anders Kaseorg
2021-11-17  8:09       ` Junio C Hamano
2021-11-22  1:14   ` Jiang Xin
2021-11-13  3:33 ` [PATCH v6 2/8] receive-pack: " Anders Kaseorg
2021-11-18  4:53   ` Junio C Hamano
2021-11-13  3:33 ` [PATCH v6 3/8] branch: " Anders Kaseorg
2021-11-13  3:33 ` [PATCH v6 4/8] worktree: simplify find_shared_symref() memory ownership model Anders Kaseorg
2021-11-16  5:39   ` Junio C Hamano
2021-11-22 12:45   ` Johannes Schindelin
2021-11-13  3:33 ` [PATCH v6 5/8] fetch: protect branches checked out in all worktrees Anders Kaseorg
2021-11-16  5:49   ` Junio C Hamano [this message]
2021-11-16  6:44     ` Anders Kaseorg
2021-11-22 13:13   ` Johannes Schindelin
2021-11-13  3:33 ` [PATCH v6 6/8] receive-pack: clean dead code from update_worktree() Anders Kaseorg
2021-11-16  5:49   ` Junio C Hamano
2021-11-13  3:33 ` [PATCH v6 7/8] receive-pack: protect current branch for bare repository worktree Anders Kaseorg
2021-11-13  3:33 ` [PATCH v6 8/8] branch: protect branches checked out in all worktrees Anders Kaseorg
2021-11-22 13:21 ` [PATCH v6 0/8] " Johannes Schindelin

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqq1r3gd50r.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=andersk@mit.edu \
    --cc=andreas.heiduk@mathema.de \
    --cc=avarab@gmail.com \
    --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.
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 NNTP newsgroup(s).