git/builtin
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
..
add.c add: pass in repo variable instead of global the_repository 2024-09-13 14:33:30 -07:00
am.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
annotate.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
apply.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
archive.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
bisect.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
blame.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
branch.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
bugreport.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
bundle.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
cat-file.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
check-attr.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
check-ignore.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
check-mailmap.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
check-ref-format.c builtin: remove USE_THE_REPOSITORY for those without the_repository 2024-09-13 14:33:30 -07:00
checkout--worker.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
checkout-index.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
checkout.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
clean.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
clone.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
column.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
commit-graph.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
commit-tree.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
commit.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
config.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
count-objects.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
credential-cache--daemon.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
credential-cache.c builtin: remove USE_THE_REPOSITORY for those without the_repository 2024-09-13 14:33:30 -07:00
credential-store.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
credential.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
describe.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
diagnose.c builtin: remove USE_THE_REPOSITORY for those without the_repository 2024-09-13 14:33:30 -07:00
diff-files.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
diff-index.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
diff-tree.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
diff.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
difftool.c Merge branch 'ps/leakfixes-part-7' 2024-10-02 07:46:26 -07:00
fast-export.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
fast-import.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
fetch-pack.c fetch-pack: clear pack lockfiles list 2024-09-25 10:24:54 -07:00
fetch.c fetch: free "raw" string when shrinking refspec 2024-09-25 10:24:54 -07:00
fmt-merge-msg.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
for-each-ref.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
for-each-repo.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
fsck.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
fsmonitor--daemon.c fsmonitor OSX: fix hangs for submodules 2024-10-04 08:01:27 -07:00
gc.c Merge branch 'ps/leakfixes-part-7' 2024-10-02 07:46:26 -07:00
get-tar-commit-id.c builtin: remove USE_THE_REPOSITORY for those without the_repository 2024-09-13 14:33:30 -07:00
grep.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
hash-object.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
help.c Merge branch 'ps/leakfixes-part-7' 2024-10-02 07:46:26 -07:00
hook.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
index-pack.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
init-db.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
interpret-trailers.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
log.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
ls-files.c builtin: fix typos 2024-09-24 10:54:39 -07:00
ls-remote.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
ls-tree.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
mailinfo.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
mailsplit.c builtin: remove USE_THE_REPOSITORY for those without the_repository 2024-09-13 14:33:30 -07:00
merge-base.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
merge-file.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
merge-index.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
merge-ours.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
merge-recursive.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
merge-tree.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
merge.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
mktag.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
mktree.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
multi-pack-index.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
mv.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
name-rev.c builtin: fix typos 2024-09-24 10:54:39 -07:00
notes.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
pack-objects.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
pack-redundant.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
pack-refs.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
patch-id.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
prune-packed.c builtin: remove USE_THE_REPOSITORY for those without the_repository 2024-09-13 14:33:30 -07:00
prune.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
pull.c Merge branch 'ps/leakfixes-part-7' 2024-10-02 07:46:26 -07:00
push.c send-pack: free cas options before exit 2024-09-25 10:24:53 -07:00
range-diff.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
read-tree.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
rebase.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
receive-pack.c Merge branch 'ak/typofix-builtins' 2024-09-25 18:24:50 -07:00
reflog.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
refs.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
remote-ext.c builtin: remove USE_THE_REPOSITORY for those without the_repository 2024-09-13 14:33:30 -07:00
remote-fd.c builtin: remove USE_THE_REPOSITORY for those without the_repository 2024-09-13 14:33:30 -07:00
remote.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
repack.c Merge branch 'ps/leakfixes-part-7' 2024-10-02 07:46:26 -07:00
replace.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
replay.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
rerere.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
reset.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
rev-list.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
rev-parse.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
revert.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
rm.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
send-pack.c send-pack: free cas options before exit 2024-09-25 10:24:53 -07:00
shortlog.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
show-branch.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
show-index.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
show-ref.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
sparse-checkout.c Merge branch 'ds/sparse-checkout-expansion-advice' 2024-10-02 07:46:25 -07:00
stash.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
stripspace.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
submodule--helper.c Merge branch 'ps/leakfixes-part-7' 2024-10-02 07:46:26 -07:00
symbolic-ref.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
tag.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
unpack-file.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
unpack-objects.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
update-index.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
update-ref.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
update-server-info.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
upload-archive.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
upload-pack.c builtin: remove USE_THE_REPOSITORY for those without the_repository 2024-09-13 14:33:30 -07:00
var.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
verify-commit.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
verify-pack.c builtin: remove USE_THE_REPOSITORY_VARIABLE from builtin.h 2024-09-13 14:32:24 -07:00
verify-tag.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00
worktree.c worktree: link worktrees with relative paths 2024-10-08 11:49:22 -07:00
write-tree.c Merge branch 'jc/pass-repo-to-builtins' 2024-09-23 10:35:09 -07:00