2626__all__ = ["SymbolicReference"]
2727
2828
29+ def _git_dir(repo, path):
30+ """ Find the git dir that's appropriate for the path"""
31+ name = "%s" % (path,)
32+ if name in ['HEAD', 'ORIG_HEAD', 'FETCH_HEAD', 'index', 'logs']:
33+ return repo.git_dir
34+ return repo.common_dir
35+
36+
2937class SymbolicReference(object):
3038
3139 """Represents a special case of a reference such that this reference is symbolic.
@@ -71,16 +79,11 @@ def name(self):
7179
7280 @property
7381 def abspath(self):
74- return join_path_native(self.repo.git_dir , self.path)
82+ return join_path_native(_git_dir( self.repo, self.path) , self.path)
7583
7684 @classmethod
7785 def _get_packed_refs_path(cls, repo):
78- try:
79- commondir = open(osp.join(repo.git_dir, 'commondir'), 'rt').readlines()[0].strip()
80- except (OSError, IOError):
81- commondir = '.'
82- repodir = osp.join(repo.git_dir, commondir)
83- return osp.join(repodir, 'packed-refs')
86+ return osp.join(repo.common_dir, 'packed-refs')
8487
8588 @classmethod
8689 def _iter_packed_refs(cls, repo):
@@ -127,11 +130,12 @@ def dereference_recursive(cls, repo, ref_path):
127130 # END recursive dereferencing
128131
129132 @classmethod
130- def _get_ref_info_helper(cls, repo, repodir, ref_path):
133+ def _get_ref_info_helper(cls, repo, ref_path):
131134 """Return: (str(sha), str(target_ref_path)) if available, the sha the file at
132135 rela_path points to, or None. target_ref_path is the reference we
133136 point to, or None"""
134137 tokens = None
138+ repodir = _git_dir(repo, ref_path)
135139 try:
136140 with open(osp.join(repodir, ref_path), 'rt') as fp:
137141 value = fp.read().rstrip()
@@ -169,16 +173,7 @@ def _get_ref_info(cls, repo, ref_path):
169173 """Return: (str(sha), str(target_ref_path)) if available, the sha the file at
170174 rela_path points to, or None. target_ref_path is the reference we
171175 point to, or None"""
172- try:
173- return cls._get_ref_info_helper(repo, repo.git_dir, ref_path)
174- except ValueError:
175- try:
176- commondir = open(osp.join(repo.git_dir, 'commondir'), 'rt').readlines()[0].strip()
177- except (OSError, IOError):
178- commondir = '.'
179-
180- repodir = osp.join(repo.git_dir, commondir)
181- return cls._get_ref_info_helper(repo, repodir, ref_path)
176+ return cls._get_ref_info_helper(repo, ref_path)
182177
183178 def _get_object(self):
184179 """
@@ -433,7 +428,7 @@ def delete(cls, repo, path):
433428 or just "myreference", hence 'refs/' is implied.
434429 Alternatively the symbolic reference to be deleted"""
435430 full_ref_path = cls.to_full_path(path)
436- abs_path = osp.join(repo.git_dir , full_ref_path)
431+ abs_path = osp.join(repo.common_dir , full_ref_path)
437432 if osp.exists(abs_path):
438433 os.remove(abs_path)
439434 else:
@@ -484,8 +479,9 @@ def _create(cls, repo, path, resolve, reference, force, logmsg=None):
484479 a proper symbolic reference. Otherwise it will be resolved to the
485480 corresponding object and a detached symbolic reference will be created
486481 instead"""
482+ git_dir = _git_dir(repo, path)
487483 full_ref_path = cls.to_full_path(path)
488- abs_ref_path = osp.join(repo. git_dir, full_ref_path)
484+ abs_ref_path = osp.join(git_dir, full_ref_path)
489485
490486 # figure out target data
491487 target = reference
@@ -559,8 +555,8 @@ def rename(self, new_path, force=False):
559555 if self.path == new_path:
560556 return self
561557
562- new_abs_path = osp.join(self.repo.git_dir , new_path)
563- cur_abs_path = osp.join(self.repo.git_dir , self.path)
558+ new_abs_path = osp.join(_git_dir( self.repo, new_path) , new_path)
559+ cur_abs_path = osp.join(_git_dir( self.repo, self.path) , self.path)
564560 if osp.isfile(new_abs_path):
565561 if not force:
566562 # if they point to the same file, its not an error
@@ -594,7 +590,7 @@ def _iter_items(cls, repo, common_path=None):
594590
595591 # walk loose refs
596592 # Currently we do not follow links
597- for root, dirs, files in os.walk(join_path_native(repo.git_dir , common_path)):
593+ for root, dirs, files in os.walk(join_path_native(repo.common_dir , common_path)):
598594 if 'refs' not in root.split(os.sep): # skip non-refs subfolders
599595 refs_id = [d for d in dirs if d == 'refs']
600596 if refs_id:
@@ -605,7 +601,7 @@ def _iter_items(cls, repo, common_path=None):
605601 if f == 'packed-refs':
606602 continue
607603 abs_path = to_native_path_linux(join_path(root, f))
608- rela_paths.add(abs_path.replace(to_native_path_linux(repo.git_dir ) + '/', ""))
604+ rela_paths.add(abs_path.replace(to_native_path_linux(repo.common_dir ) + '/', ""))
609605 # END for each file in root directory
610606 # END for each directory to walk
611607
0 commit comments