git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* "git branch -f" corrupt other worktree
@ 2019-05-02 11:58 frank kulow
  2019-05-02 12:29 ` Duy Nguyen
  0 siblings, 1 reply; 6+ messages in thread
From: frank kulow @ 2019-05-02 11:58 UTC (permalink / raw)
  To: git

git version 2.21.0.windows.1

> /c/tmp/gt (Branch_702091a0)
$ git worktree add ../wt master
Preparing worktree (checking out 'master')
HEAD is now at f534c32 4

> /c/tmp/gt (Branch_702091a0)
$ git branch -D master
error: Cannot delete branch 'master' checked out at 'C:/tmp/wt'

#but this is possible:

> /c/tmp/gt (Branch_702091a0)
$ git branch -f master HEAD

#and the other worktree is now corrupted:

> /c/tmp/wt (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   txt.txt
        deleted:    txtb.txt



greetings f.kulow

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: "git branch -f" corrupt other worktree
  2019-05-02 11:58 "git branch -f" corrupt other worktree frank kulow
@ 2019-05-02 12:29 ` Duy Nguyen
  2019-05-02 12:51   ` Ævar Arnfjörð Bjarmason
  0 siblings, 1 reply; 6+ messages in thread
From: Duy Nguyen @ 2019-05-02 12:29 UTC (permalink / raw)
  To: frank kulow; +Cc: Git Mailing List

On Thu, May 2, 2019 at 6:59 PM frank kulow <kulow.f@googlemail.com> wrote:
>
> git version 2.21.0.windows.1
>
> > /c/tmp/gt (Branch_702091a0)
> $ git worktree add ../wt master
> Preparing worktree (checking out 'master')
> HEAD is now at f534c32 4
>
> > /c/tmp/gt (Branch_702091a0)
> $ git branch -D master
> error: Cannot delete branch 'master' checked out at 'C:/tmp/wt'
>
> #but this is possible:
>
> > /c/tmp/gt (Branch_702091a0)
> $ git branch -f master HEAD

I admit I didn't see this. But I don't know how far we would go
protecting other worktrees. You give --force and that usually means
"Yes I know what I'm doing, don't stop me". If --force rejects in this
case, what would be the real force, --force --force maybe, or fall
back to "git update-ref"?

>
> #and the other worktree is now corrupted:
>
> > /c/tmp/wt (master)
> $ git status
> On branch master
> Changes to be committed:
>   (use "git reset HEAD <file>..." to unstage)
>
>         modified:   txt.txt
>         deleted:    txtb.txt
>
>
>
> greetings f.kulow



-- 
Duy

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: "git branch -f" corrupt other worktree
  2019-05-02 12:29 ` Duy Nguyen
@ 2019-05-02 12:51   ` Ævar Arnfjörð Bjarmason
  2019-05-02 13:05     ` Duy Nguyen
  0 siblings, 1 reply; 6+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2019-05-02 12:51 UTC (permalink / raw)
  To: Duy Nguyen; +Cc: frank kulow, Git Mailing List


On Thu, May 02 2019, Duy Nguyen wrote:

> On Thu, May 2, 2019 at 6:59 PM frank kulow <kulow.f@googlemail.com> wrote:
>>
>> git version 2.21.0.windows.1
>>
>> > /c/tmp/gt (Branch_702091a0)
>> $ git worktree add ../wt master
>> Preparing worktree (checking out 'master')
>> HEAD is now at f534c32 4
>>
>> > /c/tmp/gt (Branch_702091a0)
>> $ git branch -D master
>> error: Cannot delete branch 'master' checked out at 'C:/tmp/wt'
>>
>> #but this is possible:
>>
>> > /c/tmp/gt (Branch_702091a0)
>> $ git branch -f master HEAD
>
> I admit I didn't see this. But I don't know how far we would go
> protecting other worktrees. You give --force and that usually means
> "Yes I know what I'm doing, don't stop me". If --force rejects in this
> case, what would be the real force, --force --force maybe, or fall
> back to "git update-ref"?
>
>>
>> #and the other worktree is now corrupted:
>>
>> > /c/tmp/wt (master)
>> $ git status
>> On branch master
>> Changes to be committed:
>>   (use "git reset HEAD <file>..." to unstage)
>>
>>         modified:   txt.txt
>>         deleted:    txtb.txt
>>
>>
>>
>> greetings f.kulow

Part of this is "doctor, it hurts when I stab my eye" :) but I wonder in
general whether users are more likely to expect different worktrees to
have different views of the refstore, since they way they're created is
"I want just this branch over there".

I.e. whether they want something closer to another directory with
"alternates" pointing to the "main" repo, and whether that should be
promoted to UI that's easier to set up than it is now.

Or maybe something in-between, where they'd expect remote tracking refs
to update for everything, but a worktree's "master" branch not to be
touchable by a worktree on "topic".


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: "git branch -f" corrupt other worktree
  2019-05-02 12:51   ` Ævar Arnfjörð Bjarmason
@ 2019-05-02 13:05     ` Duy Nguyen
  2019-05-02 13:49       ` Duy Nguyen
  2019-05-02 17:16       ` Duy Nguyen
  0 siblings, 2 replies; 6+ messages in thread
From: Duy Nguyen @ 2019-05-02 13:05 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason; +Cc: frank kulow, Git Mailing List

On Thu, May 2, 2019 at 7:51 PM Ævar Arnfjörð Bjarmason <avarab@gmail.com> wrote:
>
>
> On Thu, May 02 2019, Duy Nguyen wrote:
>
> > On Thu, May 2, 2019 at 6:59 PM frank kulow <kulow.f@googlemail.com> wrote:
> >>
> >> git version 2.21.0.windows.1
> >>
> >> > /c/tmp/gt (Branch_702091a0)
> >> $ git worktree add ../wt master
> >> Preparing worktree (checking out 'master')
> >> HEAD is now at f534c32 4
> >>
> >> > /c/tmp/gt (Branch_702091a0)
> >> $ git branch -D master
> >> error: Cannot delete branch 'master' checked out at 'C:/tmp/wt'
> >>
> >> #but this is possible:
> >>
> >> > /c/tmp/gt (Branch_702091a0)
> >> $ git branch -f master HEAD
> >
> > I admit I didn't see this. But I don't know how far we would go
> > protecting other worktrees. You give --force and that usually means
> > "Yes I know what I'm doing, don't stop me". If --force rejects in this
> > case, what would be the real force, --force --force maybe, or fall
> > back to "git update-ref"?
> >
> >>
> >> #and the other worktree is now corrupted:
> >>
> >> > /c/tmp/wt (master)
> >> $ git status
> >> On branch master
> >> Changes to be committed:
> >>   (use "git reset HEAD <file>..." to unstage)
> >>
> >>         modified:   txt.txt
> >>         deleted:    txtb.txt
> >>
> >>
> >>
> >> greetings f.kulow
>
> Part of this is "doctor, it hurts when I stab my eye" :) but I wonder in
> general whether users are more likely to expect different worktrees to
> have different views of the refstore, since they way they're created is
> "I want just this branch over there".
>
> I.e. whether they want something closer to another directory with
> "alternates" pointing to the "main" repo, and whether that should be
> promoted to UI that's easier to set up than it is now.
>
> Or maybe something in-between, where they'd expect remote tracking refs
> to update for everything, but a worktree's "master" branch not to be
> touchable by a worktree on "topic".

I think it's a minefield to go with different views on refs. They can
already have per-worktree refs now. Granted it's long to type (e.g.
worktrees/foo/something) but it does help remind it's per-worktree.
And we can probably improve the ref resolution rules to resolve
"foo/something" (or "something" even) to worktrees/foo/something for
example. But going totally custom, I think it's hard to even manage as
a user.

With that said, I think we can technically support per-worktree refs
even outside worktrees/, or the whole refeference space per-worktree,
now. The difficulty will be coming up with some sane UI that can
handle that and not leave too many traps behind. I can't see that UI.
-- 
Duy

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: "git branch -f" corrupt other worktree
  2019-05-02 13:05     ` Duy Nguyen
@ 2019-05-02 13:49       ` Duy Nguyen
  2019-05-02 17:16       ` Duy Nguyen
  1 sibling, 0 replies; 6+ messages in thread
From: Duy Nguyen @ 2019-05-02 13:49 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason; +Cc: frank kulow, Git Mailing List

On Thu, May 2, 2019 at 8:05 PM Duy Nguyen <pclouds@gmail.com> wrote:
> > Part of this is "doctor, it hurts when I stab my eye" :) but I wonder in
> > general whether users are more likely to expect different worktrees to
> > have different views of the refstore, since they way they're created is
> > "I want just this branch over there".
> >
> > I.e. whether they want something closer to another directory with
> > "alternates" pointing to the "main" repo, and whether that should be
> > promoted to UI that's easier to set up than it is now.
> >
> > Or maybe something in-between, where they'd expect remote tracking refs
> > to update for everything, but a worktree's "master" branch not to be
> > touchable by a worktree on "topic".
>
> I think it's a minefield to go with different views on refs. They can
> already have per-worktree refs now. Granted it's long to type (e.g.
> worktrees/foo/something) but it does help remind it's per-worktree.

Correction (apparently I'm not using per-worktree refs enough to remember).

s,worktrees/foo/something,worktree/something,

worktrees/<name>/<ref> is to access per-worktree refs of other
worktrees. worktree/<ref> is about the current one.

> And we can probably improve the ref resolution rules to resolve
> "foo/something" (or "something" even) to worktrees/foo/something for
> example. But going totally custom, I think it's hard to even manage as
> a user.
>
> With that said, I think we can technically support per-worktree refs
> even outside worktrees/, or the whole refeference space per-worktree,
> now. The difficulty will be coming up with some sane UI that can
> handle that and not leave too many traps behind. I can't see that UI.
> --
> Duy



-- 
Duy

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: "git branch -f" corrupt other worktree
  2019-05-02 13:05     ` Duy Nguyen
  2019-05-02 13:49       ` Duy Nguyen
@ 2019-05-02 17:16       ` Duy Nguyen
  1 sibling, 0 replies; 6+ messages in thread
From: Duy Nguyen @ 2019-05-02 17:16 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason; +Cc: frank kulow, Git Mailing List

On Thu, May 02, 2019 at 08:05:57PM +0700, Duy Nguyen wrote:
> The difficulty will be coming up with some sane UI that can
> handle that and not leave too many traps behind. I can't see that UI.

Well, I'm still thinking about it. And perhaps a good UI is not that
far away. The following hacky patch does three things:

1. it resolves "foo" to "refs/worktree/foo" if neither refs/heads/foo
   or refs/tags/foo exists.

2. it pretty prints refs/worktree/foo as worktree/foo

3. "git branch" has a new option --per-worktree that creates
   refs/worktree/foo instead of refs/heads/foo.

The idea here is we manage/decide per-worktree or shared refs at
branch/tag creation time. After that we use it like a normal
branch/tag. Point #1 helps the "normal" part.

Point #2 could even pretty print it to "foo" to make it even more
normal, but I feel that the distinction between per-worktree and
shared should still be more visible, hence "worktree/foo".

--per-worktree is pretty much a placeholder name. If we could find a
good name (and --worktre is already taken), then it could be added in
more places where a branch may be created.

It's far from complete. For example, refs/worktree/foo is not
considered a branch by any command, Only those inside refs/heads are
at the moment.

When I added refs/worktree/ namespace I didn't think that far ahead,
what kind of refs should be in there. But maybe we can still have
branches in refs/worktree/heads/ and tags in refs/worktree/tags/. Or
perhaps all refs/worktree/ should only contain branches, tags are
always shared (it's shared with remotes now even, at least in common
case)...

With more details worked out, perhaps we can make it work.

-- 8< --
diff --git a/branch.c b/branch.c
index 28b81a7e02..e5d738efa7 100644
--- a/branch.c
+++ b/branch.c
@@ -242,6 +242,8 @@ N_("\n"
 "will track its remote counterpart, you may want to use\n"
 "\"git push -u\" to set the upstream config as you push.");
 
+int create_branch_per_worktree;
+
 void create_branch(struct repository *r,
 		   const char *name, const char *start_name,
 		   int force, int clobber_head_ok, int reflog,
@@ -308,6 +310,9 @@ void create_branch(struct repository *r,
 	if (reflog)
 		log_all_ref_updates = LOG_REFS_NORMAL;
 
+	if (create_branch_per_worktree)
+		strbuf_splice(&ref, 0, 11, "refs/worktree/", 14);
+
 	if (!dont_change_ref) {
 		struct ref_transaction *transaction;
 		struct strbuf err = STRBUF_INIT;
diff --git a/builtin/branch.c b/builtin/branch.c
index d4359b33ac..8839c6f33f 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -605,6 +605,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	int icase = 0;
 	static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
 	struct ref_format format = REF_FORMAT_INIT;
+	int per_worktree = 0;
 
 	struct option options[] = {
 		OPT_GROUP(N_("Generic options")),
@@ -613,6 +614,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		OPT__QUIET(&quiet, N_("suppress informational messages")),
 		OPT_SET_INT('t', "track",  &track, N_("set up tracking mode (see git-pull(1))"),
 			BRANCH_TRACK_EXPLICIT),
+		OPT_BOOL(0, "per-worktree",  &per_worktree, N_("create per-worktree branch")),
 		OPT_SET_INT_F(0, "set-upstream", &track, N_("do not use"),
 			BRANCH_TRACK_OVERRIDE, PARSE_OPT_HIDDEN),
 		OPT_STRING('u', "set-upstream-to", &new_upstream, N_("upstream"), N_("change the upstream info")),
@@ -829,12 +831,15 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		git_config_set_multivar(buf.buf, NULL, NULL, 1);
 		strbuf_release(&buf);
 	} else if (argc > 0 && argc <= 2) {
+		extern int create_branch_per_worktree;
+
 		if (filter.kind != FILTER_REFS_BRANCHES)
 			die(_("-a and -r options to 'git branch' do not make sense with a branch name"));
 
 		if (track == BRANCH_TRACK_OVERRIDE)
 			die(_("the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead."));
 
+		create_branch_per_worktree = per_worktree;
 		create_branch(the_repository,
 			      argv[0], (argc == 2) ? argv[1] : head,
 			      force, 0, reflog, quiet, track);
diff --git a/refs.c b/refs.c
index 142888a40a..8d01a80e07 100644
--- a/refs.c
+++ b/refs.c
@@ -479,7 +479,9 @@ const char *prettify_refname(const char *name)
 {
 	if (skip_prefix(name, "refs/heads/", &name) ||
 	    skip_prefix(name, "refs/tags/", &name) ||
-	    skip_prefix(name, "refs/remotes/", &name))
+	    skip_prefix(name, "refs/remotes/", &name) ||
+	    (starts_with(name, "refs/worktree/") &&
+	     skip_prefix(name, "refs/", &name)))
 		; /* nothing */
 	return name;
 }
@@ -491,6 +493,7 @@ static const char *ref_rev_parse_rules[] = {
 	"refs/heads/%.*s",
 	"refs/remotes/%.*s",
 	"refs/remotes/%.*s/HEAD",
+	"refs/worktree/%.*s",
 	NULL
 };
 
-- 8< --

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-05-02 17:16 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-02 11:58 "git branch -f" corrupt other worktree frank kulow
2019-05-02 12:29 ` Duy Nguyen
2019-05-02 12:51   ` Ævar Arnfjörð Bjarmason
2019-05-02 13:05     ` Duy Nguyen
2019-05-02 13:49       ` Duy Nguyen
2019-05-02 17:16       ` Duy Nguyen

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).