From 0184dcc510969d6e7cb2525da8e7e2a87ed5f012 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Tue, 5 May 2015 00:24:54 -0700 Subject: [PATCH] Make linkfile symlinks relative The source (target) of the symlink is specified relative to a project within a tree, and the destination is specified relative to the top of the tree, so it should always be possible to create a relative symlink to the target file. Relative symlinks will allow moving an entire tree without breaking the symlink, and copying a tree (with -p) without leaving a symlink to the old tree. Change-Id: I16492a8b59a137d2abe43ca78e3b212e2c835599 --- project.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/project.py b/project.py index 00e41ada..86e2756d 100644 --- a/project.py +++ b/project.py @@ -233,14 +233,14 @@ class _CopyFile(object): _error('Cannot copy file %s to %s', src, dest) class _LinkFile(object): - def __init__(self, src, dest, abssrc, absdest): + def __init__(self, src, dest, relsrc, absdest): self.src = src self.dest = dest - self.abs_src = abssrc + self.src_rel_to_dest = relsrc self.abs_dest = absdest def _Link(self): - src = self.abs_src + src = self.src_rel_to_dest dest = self.abs_dest # link file if it does not exist or is out of date if not os.path.islink(dest) or os.readlink(dest) != src: @@ -1359,9 +1359,10 @@ class Project(object): def AddLinkFile(self, src, dest, absdest): # dest should already be an absolute path, but src is project relative - # make src an absolute path - abssrc = os.path.join(self.worktree, src) - self.linkfiles.append(_LinkFile(src, dest, abssrc, absdest)) + # make src relative path to dest + absdestdir = os.path.dirname(absdest) + relsrc = os.path.relpath(os.path.join(self.worktree, src), absdestdir) + self.linkfiles.append(_LinkFile(src, dest, relsrc, absdest)) def AddAnnotation(self, name, value, keep): self.annotations.append(_Annotation(name, value, keep))