git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Eric Sunshine <sunshine@sunshineco.com>
To: Ingo Wolf <ingo.wolf@gmx.de>
Cc: Git List <git@vger.kernel.org>
Subject: Re: worktree add already exists
Date: Sun, 2 Jun 2019 03:07:45 -0400	[thread overview]
Message-ID: <CAPig+cSsNQDe-3GLnmvA+EPior=RSPETJDjkCc7X1qi6gWSGDQ@mail.gmail.com> (raw)
In-Reply-To: <ad2ff81a-d75e-1ac4-07d0-c807a2ed9e36@gmx.de>

On Mon, May 27, 2019 at 11:32 AM Ingo Wolf <ingo.wolf@gmx.de> wrote:
> $ ls -a barework
> ./  ../  test.txt
> $ git -C bare worktree add --no-checkout ../barework
> Preparing worktree (new branch 'barework')
> fatal: '../barework' already exists
> $ git -C bare branch
>    barework
> * master
> Why this doesn't just work and if not why is barework branch made then,
> why at all ?

It is by design that "git worktree add" (in general) fails if the
target directory already exists and is non-empty. This is consistent
with how "git clone" behaves. As for why your particular use-case
isn't directly supported, it's likely that nobody has yet asked for
it, and nobody thought about this particular case when --no-checkout
was added (which came some time after basic "git worktree add" itself
was implemented).

It is an accident of implementation that the new branch gets created
before "git worktree add" errors out due to the existing non-empty
directory (and, likely, nobody complained about it, so it went
unnoticed). This particular issue probably can be easily fixed now
that the logic for checking if the target directory can be a valid
worktree has been factored out of the code which actually creates the
new directory[1].

[1]: 45059e6468 (worktree: prepare for more checks of whether path can
become worktree, 2018-08-28)

> I would like to attach an existing dir to git (make it a workdir) and
> then update the index with git reset and checkin the differences.

I haven't thought through the possible ramifications, but the actual
implementation might be as simple as changing this code in
builtin/worktree.c:validate_worktree_add():

    if (file_exists(path) && !is_empty_dir(path))
        die(_("'%s' already exists"), path);

to:

    if (opts->checkout && file_exists(path) && !is_empty_dir(path))
        die(_("'%s' already exists"), path);

or something.

  reply	other threads:[~2019-06-02  7:07 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-27 15:32 worktree add already exists Ingo Wolf
2019-06-02  7:07 ` Eric Sunshine [this message]
2019-06-03  9:46   ` Duy Nguyen
2019-06-03 18:32     ` Eric Sunshine
2019-06-05 10:17       ` Duy Nguyen
2019-06-05 15:30         ` Ingo Wolf
2019-06-06  9:34           ` Duy Nguyen

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='CAPig+cSsNQDe-3GLnmvA+EPior=RSPETJDjkCc7X1qi6gWSGDQ@mail.gmail.com' \
    --to=sunshine@sunshineco.com \
    --cc=git@vger.kernel.org \
    --cc=ingo.wolf@gmx.de \
    /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).