diff options
author | Peter Jones <pjones@redhat.com> | 2017-08-03 17:33:07 -0400 |
---|---|---|
committer | Peter Jones <pjones@redhat.com> | 2017-08-22 17:12:54 -0400 |
commit | d1c40f46bd547be663b4cd97a80704279708ea8a (patch) | |
tree | 2a580fb08efbba0c9923fb0c93ee0a91fc179369 /git/refs | |
parent | cf8dc259fcc9c1397ea67cec3a6a4cb5816e3e68 (diff) | |
download | gitpython-d1c40f46bd547be663b4cd97a80704279708ea8a.tar.gz |
worktrees: make non-packed refs also work correctly.
Turns out aec58a9 did the right thing for /packed/ refs, but didn't work
correctly on /unpacked/ refs. So this patch gives unpacked refs the
same treatment.
Without the fix here, the test added will cause this traceback:
======================================================================
ERROR: Check that we find .git as a worktree file and find the worktree
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/pjones/devel/github.com/GitPython/git/test/lib/helper.py", line 92, in wrapper
return func(self, path)
File "/home/pjones/devel/github.com/GitPython/git/test/test_repo.py", line 938, in test_git_work_tree_dotgit
self.assertIsInstance(repo.heads['aaaaaaaa'], Head)
File "/home/pjones/devel/github.com/GitPython/git/util.py", line 893, in __getitem__
raise IndexError("No item found with id %r" % (self._prefix + index))
IndexError: No item found with id 'aaaaaaaa'
Woops.
Things I've learned:
- test_remote doesn't work currently if you start on a branch. I think
it never did?
- Because of 346424da, all *sorts* of stuff in the test suite doesn't
work if you name your development branch "packed-refs"
(This seems like a bug...)
Signed-off-by: Peter Jones <pjones@redhat.com>
Diffstat (limited to 'git/refs')
-rw-r--r-- | git/refs/remote.py | 4 | ||||
-rw-r--r-- | git/refs/symbolic.py | 44 |
2 files changed, 24 insertions, 24 deletions
diff --git a/git/refs/remote.py b/git/refs/remote.py index ef69b5db..0164e110 100644 --- a/git/refs/remote.py +++ b/git/refs/remote.py @@ -37,6 +37,10 @@ class RemoteReference(Head): # and delete remainders manually for ref in refs: try: + os.remove(osp.join(repo.common_dir, ref.path)) + except OSError: + pass + try: os.remove(osp.join(repo.git_dir, ref.path)) except OSError: pass diff --git a/git/refs/symbolic.py b/git/refs/symbolic.py index 90ecb62c..bef6ba3c 100644 --- a/git/refs/symbolic.py +++ b/git/refs/symbolic.py @@ -26,6 +26,14 @@ from .log import RefLog __all__ = ["SymbolicReference"] +def _git_dir(repo, path): + """ Find the git dir that's appropriate for the path""" + name = "%s" % (path,) + if name in ['HEAD', 'ORIG_HEAD', 'FETCH_HEAD', 'index', 'logs']: + return repo.git_dir + return repo.common_dir + + class SymbolicReference(object): """Represents a special case of a reference such that this reference is symbolic. @@ -71,16 +79,11 @@ class SymbolicReference(object): @property def abspath(self): - return join_path_native(self.repo.git_dir, self.path) + return join_path_native(_git_dir(self.repo, self.path), self.path) @classmethod def _get_packed_refs_path(cls, repo): - try: - commondir = open(osp.join(repo.git_dir, 'commondir'), 'rt').readlines()[0].strip() - except (OSError, IOError): - commondir = '.' - repodir = osp.join(repo.git_dir, commondir) - return osp.join(repodir, 'packed-refs') + return osp.join(repo.common_dir, 'packed-refs') @classmethod def _iter_packed_refs(cls, repo): @@ -127,11 +130,12 @@ class SymbolicReference(object): # END recursive dereferencing @classmethod - def _get_ref_info_helper(cls, repo, repodir, ref_path): + def _get_ref_info_helper(cls, repo, ref_path): """Return: (str(sha), str(target_ref_path)) if available, the sha the file at rela_path points to, or None. target_ref_path is the reference we point to, or None""" tokens = None + repodir = _git_dir(repo, ref_path) try: with open(osp.join(repodir, ref_path), 'rt') as fp: value = fp.read().rstrip() @@ -169,16 +173,7 @@ class SymbolicReference(object): """Return: (str(sha), str(target_ref_path)) if available, the sha the file at rela_path points to, or None. target_ref_path is the reference we point to, or None""" - try: - return cls._get_ref_info_helper(repo, repo.git_dir, ref_path) - except ValueError: - try: - commondir = open(osp.join(repo.git_dir, 'commondir'), 'rt').readlines()[0].strip() - except (OSError, IOError): - commondir = '.' - - repodir = osp.join(repo.git_dir, commondir) - return cls._get_ref_info_helper(repo, repodir, ref_path) + return cls._get_ref_info_helper(repo, ref_path) def _get_object(self): """ @@ -433,7 +428,7 @@ class SymbolicReference(object): or just "myreference", hence 'refs/' is implied. Alternatively the symbolic reference to be deleted""" full_ref_path = cls.to_full_path(path) - abs_path = osp.join(repo.git_dir, full_ref_path) + abs_path = osp.join(repo.common_dir, full_ref_path) if osp.exists(abs_path): os.remove(abs_path) else: @@ -484,8 +479,9 @@ class SymbolicReference(object): a proper symbolic reference. Otherwise it will be resolved to the corresponding object and a detached symbolic reference will be created instead""" + git_dir = _git_dir(repo, path) full_ref_path = cls.to_full_path(path) - abs_ref_path = osp.join(repo.git_dir, full_ref_path) + abs_ref_path = osp.join(git_dir, full_ref_path) # figure out target data target = reference @@ -559,8 +555,8 @@ class SymbolicReference(object): if self.path == new_path: return self - new_abs_path = osp.join(self.repo.git_dir, new_path) - cur_abs_path = osp.join(self.repo.git_dir, self.path) + new_abs_path = osp.join(_git_dir(self.repo, new_path), new_path) + cur_abs_path = osp.join(_git_dir(self.repo, self.path), self.path) if osp.isfile(new_abs_path): if not force: # if they point to the same file, its not an error @@ -594,7 +590,7 @@ class SymbolicReference(object): # walk loose refs # Currently we do not follow links - for root, dirs, files in os.walk(join_path_native(repo.git_dir, common_path)): + for root, dirs, files in os.walk(join_path_native(repo.common_dir, common_path)): if 'refs' not in root.split(os.sep): # skip non-refs subfolders refs_id = [d for d in dirs if d == 'refs'] if refs_id: @@ -605,7 +601,7 @@ class SymbolicReference(object): if f == 'packed-refs': continue abs_path = to_native_path_linux(join_path(root, f)) - rela_paths.add(abs_path.replace(to_native_path_linux(repo.git_dir) + '/', "")) + rela_paths.add(abs_path.replace(to_native_path_linux(repo.common_dir) + '/', "")) # END for each file in root directory # END for each directory to walk |