Skip to content

Commit 1d50986

Browse files
Allen-WebbLUCI
authored andcommitted
worktree: Do not try to fix relative paths
--worktree was broken with incorrect paths in the .git files whenever the local copy of git populated gitdir with relative paths instead of absoulte paths. Bug: 376251410 Change-Id: Id32dc1576315218967de2a9bfe43bf7a5a0e7aa6 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/440801 Commit-Queue: Allen Webb <[email protected]> Reviewed-by: Josip Sokcevic <[email protected]> Tested-by: Allen Webb <[email protected]>
1 parent e219c78 commit 1d50986

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

project.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3375,24 +3375,29 @@ def _InitGitWorktree(self):
33753375
setting = fp.read()
33763376
assert setting.startswith("gitdir:")
33773377
git_worktree_path = setting.split(":", 1)[1].strip()
3378-
# Some platforms (e.g. Windows) won't let us update dotgit in situ
3379-
# because of file permissions. Delete it and recreate it from scratch
3380-
# to avoid.
3381-
platform_utils.remove(dotgit)
3382-
# Use relative path from checkout->worktree & maintain Unix line endings
3383-
# on all OS's to match git behavior.
3384-
with open(dotgit, "w", newline="\n") as fp:
3385-
print(
3386-
"gitdir:",
3387-
os.path.relpath(git_worktree_path, self.worktree),
3388-
file=fp,
3389-
)
3390-
# Use relative path from worktree->checkout & maintain Unix line endings
3391-
# on all OS's to match git behavior.
3392-
with open(
3393-
os.path.join(git_worktree_path, "gitdir"), "w", newline="\n"
3394-
) as fp:
3395-
print(os.path.relpath(dotgit, git_worktree_path), file=fp)
3378+
3379+
# `gitdir` maybe be either relative or absolute depending on the
3380+
# behavior of the local copy of git, so only convert the path to
3381+
# relative if it needs to be converted.
3382+
if os.path.isabs(git_worktree_path):
3383+
# Some platforms (e.g. Windows) won't let us update dotgit in situ
3384+
# because of file permissions. Delete it and recreate it from
3385+
# scratch to avoid.
3386+
platform_utils.remove(dotgit)
3387+
# Use relative path from checkout->worktree & maintain Unix line
3388+
# endings on all OS's to match git behavior.
3389+
with open(dotgit, "w", newline="\n") as fp:
3390+
print(
3391+
"gitdir:",
3392+
os.path.relpath(git_worktree_path, self.worktree),
3393+
file=fp,
3394+
)
3395+
# Use relative path from worktree->checkout & maintain Unix line
3396+
# endings on all OS's to match git behavior.
3397+
with open(
3398+
os.path.join(git_worktree_path, "gitdir"), "w", newline="\n"
3399+
) as fp:
3400+
print(os.path.relpath(dotgit, git_worktree_path), file=fp)
33963401

33973402
self._InitMRef()
33983403

0 commit comments

Comments
 (0)