git/t/t2408-worktree-relative.sh
Caleb White 717af916cd worktree: link worktrees with relative paths
Git currently stores absolute paths to both the main repository and
linked worktrees. However, this causes problems when moving repositories
or working in containerized environments where absolute paths differ
between systems. The worktree links break, and users are required to
manually execute `worktree repair` to repair them, leading to workflow
disruptions. Additionally, mapping repositories inside of containerized
environments renders the repository unusable inside the containers, and
this is not repairable as repairing the worktrees inside the containers
will result in them being broken outside the containers.

To address this, this patch makes Git always write relative paths when
linking worktrees. Relative paths increase the resilience of the
worktree links across various systems and environments, particularly
when the worktrees are self-contained inside the main repository (such
as when using a bare repository with worktrees). This improves
portability, workflow efficiency, and reduces overall breakages.

Although Git now writes relative paths, existing repositories with
absolute paths are still supported. There are no breaking changes
to workflows based on absolute paths, ensuring backward compatibility.

At a low level, the changes involve modifying functions in `worktree.c`
and `builtin/worktree.c` to use `relative_path()` when writing the
worktree’s `.git` file and the main repository’s `gitdir` reference.
Instead of hardcoding absolute paths, Git now computes the relative path
between the worktree and the repository, ensuring that these links are
portable. Locations where these respective file are read have also been
updated to properly handle both absolute and relative paths. Generally,
relative paths are always resolved into absolute paths before any
operations or comparisons are performed.

Additionally, `repair_worktrees_after_gitdir_move()` has been introduced
to address the case where both the `<worktree>/.git` and
`<repo>/worktrees/<id>/gitdir` links are broken after the gitdir is
moved (such as during a re-initialization). This function repairs both
sides of the worktree link using the old gitdir path to reestablish the
correct paths after a move.

The `worktree.path` struct member has also been updated to always store
the absolute path of a worktree. This ensures that worktree consumers
never have to worry about trying to resolve the absolute path themselves.

Signed-off-by: Caleb White <cdwhite3@pm.me>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-10-08 11:49:22 -07:00

40 lines
886 B
Bash
Executable File

#!/bin/sh
test_description='test worktrees linked with relative paths'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
test_expect_success 'links worktrees with relative paths' '
test_when_finished rm -rf repo &&
git init repo &&
(
cd repo &&
test_commit initial &&
git worktree add wt1 &&
echo "../../../wt1/.git" >expected_gitdir &&
cat .git/worktrees/wt1/gitdir >actual_gitdir &&
echo "gitdir: ../.git/worktrees/wt1" >expected_git &&
cat wt1/.git >actual_git &&
test_cmp expected_gitdir actual_gitdir &&
test_cmp expected_git actual_git
)
'
test_expect_success 'move repo without breaking relative internal links' '
test_when_finished rm -rf repo moved &&
git init repo &&
(
cd repo &&
test_commit initial &&
git worktree add wt1 &&
cd .. &&
mv repo moved &&
cd moved/wt1 &&
git status >out 2>err &&
test_must_be_empty err
)
'
test_done