# EXDEV when re-init with --separate-git-dir option

3 messages from 2024-07-24 to 2024-07-24. Participants: Jin, Di, Junio C Hamano, Eric Sunshine.
Thread: https://gitlist.dev/t/61832

## Jin, Di, 2024-07-24 02:47

Subject: EXDEV when re-init with --separate-git-dir option
Message-ID: <CAKOkDnMGRfQoNygYLiAxPZB2q=VMYvw8kyu1dM=pM843-FH41Q@mail.gmail.com>
URL: https://gitlist.dev/e/CAKOkDnMGRfQoNygYLiAxPZB2q%3DVMYvw8kyu1dM%3DpM843-FH41Q%40mail.gmail.com

```
Dear maintainers,

We discovered that re-init with option --separate-git-dir will throw
an EXDEV when the target directory is not on the same file system as
the original directory.

An example is shown below:

$ mkdir -p fs1/gitdir fs2
$ ls
fs1  fs2
$ sudo mount -t tmpfs tmpfs fs2
$ cd fs1/gitdir
$ git init
Initialized empty Git repository in /home/user/fs1/gitdir/.git/
$ git init --separate-git-dir ../../fs2/surrealgitdir
fatal: unable to move /home/user/fs1/gitdir/.git to
/home/user/fs2/surrealgitdir: Invalid cross-device link

We tested this on version v2.45.2 and after reading the code a bit we
believe the issue still exist in the master branch on github
(https://github.com/git/git/blob/d19b6cd2dd72dc811f19df4b32c7ed223256c3ee/setup.c#L2280).

Best regards,
Di

```

## Junio C Hamano, 2024-07-24 15:39

Subject: Re: EXDEV when re-init with --separate-git-dir option
Message-ID: <xmqq1q3iztmi.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqq1q3iztmi.fsf%40gitster.g
In-Reply-To: <CAKOkDnMGRfQoNygYLiAxPZB2q=VMYvw8kyu1dM=pM843-FH41Q@mail.gmail.com>

```
"Jin, Di" <di_jin@brown.edu> writes:

> We discovered that re-init with option --separate-git-dir will throw
> an EXDEV when the target directory is not on the same file system as
> the original directory.

Yup, it is hitting the limitation of your filesystem.  The code
wants to move the original .git directory together with its contents
to a new place, and it makes a single rename() system call to do so.
When the system cannot do so, you'd get the error message you are
seeing ("fatal: unable to move X to Y").

The code path could probably borrow some code to recursively "copy"
directory from the local "git clone" code path, and then invent a
new code to recursively remove the original ".git", and trigger that
new code when rename() fails.

But at that point, only as a fall-back measure, it might be simpler
and much less error prone to spawn a "mv src dst" as a subprocess
using the run_command() API.

It would make a good bite-sized #leftoverbits project for aspiring
new Git contributors.  Any takers?  ;-)

Thanks.


```

## Eric Sunshine, 2024-07-24 17:14

Subject: Re: EXDEV when re-init with --separate-git-dir option
Message-ID: <CAPig+cSJKA5+iU6TfRZev_b+ePLJ=wRm-8PLw3wKqJYEEonOVA@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcSJKA5%2BiU6TfRZev_b%2BePLJ%3DwRm-8PLw3wKqJYEEonOVA%40mail.gmail.com
In-Reply-To: <xmqq1q3iztmi.fsf@gitster.g>

```
On Wed, Jul 24, 2024 at 11:40 AM Junio C Hamano <gitster@pobox.com> wrote:
> "Jin, Di" <di_jin@brown.edu> writes:
> > We discovered that re-init with option --separate-git-dir will throw
> > an EXDEV when the target directory is not on the same file system as
> > the original directory.
>
> Yup, it is hitting the limitation of your filesystem.  The code
> wants to move the original .git directory together with its contents
> to a new place, and it makes a single rename() system call to do so.
> [...]
> The code path could probably borrow some code to recursively "copy"
> directory from the local "git clone" code path, and then invent a
> new code to recursively remove the original ".git", and trigger that
> new code when rename() fails.

Re-init with --separate-git-dir isn't the only problem spot. `git
worktree move` also suffers the same problem for the same reason.

> But at that point, only as a fall-back measure, it might be simpler
> and much less error prone to spawn a "mv src dst" as a subprocess
> using the run_command() API.

This wouldn't help Windows users.

> It would make a good bite-sized #leftoverbits project for aspiring
> new Git contributors.  Any takers?  ;-)

It might be a bit more than bite-sized, though, considering the above
points about `git worktree move` also needing such a fix, and having
to deal with Microsoft Windows.

```
