| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -36,6 +36,10 @@ def delete(cls, repo, *refs, **kwargs): | |||
| 36 | 36 | # are generally ignored in the refs/ folder. We don't though | |
| 37 | 37 | # and delete remainders manually | |
| 38 | 38 | for ref in refs: | |
| 39 | + try: | ||
| 40 | + os.remove(osp.join(repo.common_dir, ref.path)) | ||
| 41 | + except OSError: | ||
| 42 | + pass | ||
| 39 | 43 | try: | |
| 40 | 44 | os.remove(osp.join(repo.git_dir, ref.path)) | |
| 41 | 45 | except OSError: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,6 +26,14 @@ | |||
| 26 | 26 | __all__ = ["SymbolicReference"] | |
| 27 | 27 | ||
| 28 | 28 | ||
| 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 | + | ||
| 29 | 37 | class SymbolicReference(object): | |
| 30 | 38 | ||
| 31 | 39 | """Represents a special case of a reference such that this reference is symbolic. | |
@@ -71,16 +79,11 @@ def name(self): | |||
| 71 | 79 | ||
| 72 | 80 | @property | |
| 73 | 81 | 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) | ||
| 75 | 83 | ||
| 76 | 84 | @classmethod | |
| 77 | 85 | 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') | ||
| 84 | 87 | ||
| 85 | 88 | @classmethod | |
| 86 | 89 | def _iter_packed_refs(cls, repo): | |
@@ -127,11 +130,12 @@ def dereference_recursive(cls, repo, ref_path): | |||
| 127 | 130 | # END recursive dereferencing | |
| 128 | 131 | ||
| 129 | 132 | @classmethod | |
| 130 | - def _get_ref_info_helper(cls, repo, repodir, ref_path): | ||
| 133 | + def _get_ref_info_helper(cls, repo, ref_path): | ||
| 131 | 134 | """Return: (str(sha), str(target_ref_path)) if available, the sha the file at | |
| 132 | 135 | rela_path points to, or None. target_ref_path is the reference we | |
| 133 | 136 | point to, or None""" | |
| 134 | 137 | tokens = None | |
| 138 | + repodir = _git_dir(repo, ref_path) | ||
| 135 | 139 | try: | |
| 136 | 140 | with open(osp.join(repodir, ref_path), 'rt') as fp: | |
| 137 | 141 | value = fp.read().rstrip() | |
@@ -169,16 +173,7 @@ def _get_ref_info(cls, repo, ref_path): | |||
| 169 | 173 | """Return: (str(sha), str(target_ref_path)) if available, the sha the file at | |
| 170 | 174 | rela_path points to, or None. target_ref_path is the reference we | |
| 171 | 175 | 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) | ||
| 182 | 177 | ||
| 183 | 178 | def _get_object(self): | |
| 184 | 179 | """ | |
@@ -433,7 +428,7 @@ def delete(cls, repo, path): | |||
| 433 | 428 | or just "myreference", hence 'refs/' is implied. | |
| 434 | 429 | Alternatively the symbolic reference to be deleted""" | |
| 435 | 430 | 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) | ||
| 437 | 432 | if osp.exists(abs_path): | |
| 438 | 433 | os.remove(abs_path) | |
| 439 | 434 | else: | |
@@ -484,8 +479,9 @@ def _create(cls, repo, path, resolve, reference, force, logmsg=None): | |||
| 484 | 479 | a proper symbolic reference. Otherwise it will be resolved to the | |
| 485 | 480 | corresponding object and a detached symbolic reference will be created | |
| 486 | 481 | instead""" | |
| 482 | + git_dir = _git_dir(repo, path) | ||
| 487 | 483 | 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) | ||
| 489 | 485 | ||
| 490 | 486 | # figure out target data | |
| 491 | 487 | target = reference | |
@@ -559,8 +555,8 @@ def rename(self, new_path, force=False): | |||
| 559 | 555 | if self.path == new_path: | |
| 560 | 556 | return self | |
| 561 | 557 | ||
| 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) | ||
| 564 | 560 | if osp.isfile(new_abs_path): | |
| 565 | 561 | if not force: | |
| 566 | 562 | # if they point to the same file, its not an error | |
@@ -594,7 +590,7 @@ def _iter_items(cls, repo, common_path=None): | |||
| 594 | 590 | ||
| 595 | 591 | # walk loose refs | |
| 596 | 592 | # 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)): | ||
| 598 | 594 | if 'refs' not in root.split(os.sep): # skip non-refs subfolders | |
| 599 | 595 | refs_id = [d for d in dirs if d == 'refs'] | |
| 600 | 596 | if refs_id: | |
@@ -605,7 +601,7 @@ def _iter_items(cls, repo, common_path=None): | |||
| 605 | 601 | if f == 'packed-refs': | |
| 606 | 602 | continue | |
| 607 | 603 | 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) + '/', "")) | ||
| 609 | 605 | # END for each file in root directory | |
| 610 | 606 | # END for each directory to walk | |
| 611 | 607 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -653,7 +653,7 @@ def _get_fetch_info_from_stderr(self, proc, progress): | |||
| 653 | 653 | continue | |
| 654 | 654 | ||
| 655 | 655 | # read head information | |
| 656 | - with open(osp.join(self.repo.git_dir, 'FETCH_HEAD'), 'rb') as fp: | ||
| 656 | + with open(osp.join(self.repo.common_dir, 'FETCH_HEAD'), 'rb') as fp: | ||
| 657 | 657 | fetch_head_info = [l.decode(defenc) for l in fp.readlines()] | |
| 658 | 658 | ||
| 659 | 659 | l_fil = len(fetch_info_lines) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -71,6 +71,7 @@ class Repo(object): | |||
| 71 | 71 | working_dir = None | |
| 72 | 72 | _working_tree_dir = None | |
| 73 | 73 | git_dir = None | |
| 74 | + _common_dir = None | ||
| 74 | 75 | ||
| 75 | 76 | # precompiled regex | |
| 76 | 77 | re_whitespace = re.compile(r'\s+') | |
@@ -172,17 +173,23 @@ def __init__(self, path=None, odbt=DefaultDBType, search_parent_directories=Fals | |||
| 172 | 173 | # lets not assume the option exists, although it should | |
| 173 | 174 | pass | |
| 174 | 175 | ||
| 176 | + try: | ||
| 177 | + common_dir = open(osp.join(self.git_dir, 'commondir'), 'rt').readlines()[0].strip() | ||
| 178 | + self._common_dir = osp.join(self.git_dir, common_dir) | ||
| 179 | + except (OSError, IOError): | ||
| 180 | + self._common_dir = None | ||
| 181 | + | ||
| 175 | 182 | # adjust the wd in case we are actually bare - we didn't know that | |
| 176 | 183 | # in the first place | |
| 177 | 184 | if self._bare: | |
| 178 | 185 | self._working_tree_dir = None | |
| 179 | 186 | # END working dir handling | |
| 180 | 187 | ||
| 181 | - self.working_dir = self._working_tree_dir or self.git_dir | ||
| 188 | + self.working_dir = self._working_tree_dir or self.common_dir | ||
| 182 | 189 | self.git = self.GitCommandWrapperType(self.working_dir) | |
| 183 | 190 | ||
| 184 | 191 | # special handling, in special times | |
| 185 | - args = [osp.join(self.git_dir, 'objects')] | ||
| 192 | + args = [osp.join(self.common_dir, 'objects')] | ||
| 186 | 193 | if issubclass(odbt, GitCmdObjectDB): | |
| 187 | 194 | args.append(self.git) | |
| 188 | 195 | self.odb = odbt(*args) | |
@@ -239,6 +246,13 @@ def working_tree_dir(self): | |||
| 239 | 246 | """ | |
| 240 | 247 | return self._working_tree_dir | |
| 241 | 248 | ||
| 249 | + @property | ||
| 250 | + def common_dir(self): | ||
| 251 | + """:return: The git dir that holds everything except possibly HEAD, | ||
| 252 | + FETCH_HEAD, ORIG_HEAD, COMMIT_EDITMSG, index, and logs/ . | ||
| 253 | + """ | ||
| 254 | + return self._common_dir or self.git_dir | ||
| 255 | + | ||
| 242 | 256 | @property | |
| 243 | 257 | def bare(self): | |
| 244 | 258 | """:return: True if the repository is bare""" | |
@@ -577,7 +591,7 @@ def _set_alternates(self, alts): | |||
| 577 | 591 | :note: | |
| 578 | 592 | The method does not check for the existence of the paths in alts | |
| 579 | 593 | as the caller is responsible.""" | |
| 580 | - alternates_path = osp.join(self.git_dir, 'objects', 'info', 'alternates') | ||
| 594 | + alternates_path = osp.join(self.common_dir, 'objects', 'info', 'alternates') | ||
| 581 | 595 | if not alts: | |
| 582 | 596 | if osp.isfile(alternates_path): | |
| 583 | 597 | os.remove(alternates_path) | |
@@ -940,7 +954,7 @@ def clone(self, path, progress=None, **kwargs): | |||
| 940 | 954 | * All remaining keyword arguments are given to the git-clone command | |
| 941 | 955 | ||
| 942 | 956 | :return: ``git.Repo`` (the newly cloned repo)""" | |
| 943 | - return self._clone(self.git, self.git_dir, path, type(self.odb), progress, **kwargs) | ||
| 957 | + return self._clone(self.git, self.common_dir, path, type(self.odb), progress, **kwargs) | ||
| 944 | 958 | ||
| 945 | 959 | @classmethod | |
| 946 | 960 | def clone_from(cls, url, to_path, progress=None, env=None, **kwargs): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -935,6 +935,8 @@ def test_git_work_tree_dotgit(self, rw_dir): | |||
| 935 | 935 | commit = repo.head.commit | |
| 936 | 936 | self.assertIsInstance(commit, Object) | |
| 937 | 937 | ||
| 938 | + self.assertIsInstance(repo.heads['aaaaaaaa'], Head) | ||
| 939 | + | ||
| 938 | 940 | @with_rw_directory | |
| 939 | 941 | def test_git_work_tree_env(self, rw_dir): | |
| 940 | 942 | """Check that we yield to GIT_WORK_TREE""" | |
| Back | FazBrowse Home | New Git URL |
0 commit comments