From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
To: Anders Kaseorg <andersk@mit.edu>
Cc: "Junio C Hamano" <gitster@pobox.com>,
git@vger.kernel.org, "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, 22 Nov 2021 14:13:51 +0100 (CET) [thread overview]
Message-ID: <nycvar.QRO.7.76.6.2111221358160.63@tvgsbejvaqbjf.bet> (raw)
In-Reply-To: <20211113033358.2179376-6-andersk@mit.edu>
[-- Attachment #1: Type: text/plain, Size: 6900 bytes --]
Hi Anders,
tl;dr the patch looks nice! A few remarks below, to prove that I did not
merely glance over the patch.
On Fri, 12 Nov 2021, Anders Kaseorg wrote:
> 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
These days, I think the lore.kernel.org/git/ links are slightly preferred.
>
> 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.
>
> 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,
As a reviewer, I prefer to spend my time reviewing the actual change. In
this case, the re-wrapping is a bit distracting. Not really important
here, but maybe for future contributions. The easier a contributor makes
it on reviewers, the better (it definitely helps avoid the grumpy judge
bias[*1*]).
> + 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;
>
> @@ -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)) {
> /*
> * 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;
> }
> @@ -1071,7 +1072,8 @@ N_("it took %.2f seconds to check forced updates; you can use\n"
> " to avoid this check\n");
>
> static int store_updated_refs(const char *raw_url, const char *remote_name,
> - int connectivity_checked, struct ref *ref_map)
> + int connectivity_checked, struct ref *ref_map,
> + struct worktree **worktrees)
> {
> struct fetch_head fetch_head;
> struct commit *commit;
> @@ -1188,7 +1190,8 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
> strbuf_reset(¬e);
> if (ref) {
> rc |= update_local_ref(ref, transaction, what,
> - rm, ¬e, summary_width);
> + rm, ¬e, summary_width,
> + worktrees);
> free(ref);
> } else if (write_fetch_head || dry_run) {
> /*
> @@ -1301,16 +1304,15 @@ static int fetch_refs(struct transport *transport, struct ref *ref_map)
> }
>
> /* Update local refs based on the ref values fetched from a remote */
> -static int consume_refs(struct transport *transport, struct ref *ref_map)
> +static int consume_refs(struct transport *transport, struct ref *ref_map,
> + struct worktree **worktrees)
> {
> int connectivity_checked = transport->smart_options
> ? transport->smart_options->connectivity_checked : 0;
> int ret;
> trace2_region_enter("fetch", "consume_refs", the_repository);
> - ret = store_updated_refs(transport->url,
> - transport->remote->name,
> - connectivity_checked,
> - ref_map);
> + ret = store_updated_refs(transport->url, transport->remote->name,
> + connectivity_checked, ref_map, worktrees);
Again, this rewrapping is slightly distracting to my brain.
> transport_unlock_pack(transport);
> trace2_region_leave("fetch", "consume_refs", the_repository);
> return ret;
> @@ -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:
Okay, this one is distracting _but also_ pleasing. I am only pointing this
out to prove that I did not go over your patch sloppily. :-)
> @@ -1653,11 +1657,12 @@ static int do_fetch(struct transport *transport,
> ref_map = NULL;
> find_non_local_tags(remote_refs, &ref_map, &tail);
> if (ref_map)
> - backfill_tags(transport, ref_map);
> + backfill_tags(transport, ref_map, worktrees);
> free_refs(ref_map);
> }
>
> - cleanup:
> +cleanup:
Same here.
> + free_worktrees(worktrees);
> return retcode;
> }
>
> 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
Reads nicely.
Thanks,
Dscho
Footnote: https://en.wikipedia.org/wiki/Hungry_judge_effect
next prev parent reply other threads:[~2021-11-22 13:14 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
2021-11-16 6:44 ` Anders Kaseorg
2021-11-22 13:13 ` Johannes Schindelin [this message]
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
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=nycvar.QRO.7.76.6.2111221358160.63@tvgsbejvaqbjf.bet \
--to=johannes.schindelin@gmx.de \
--cc=andersk@mit.edu \
--cc=andreas.heiduk@mathema.de \
--cc=avarab@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--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).