| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -162,7 +162,7 @@ class Git(LazyMixin): | |||
| 162 | 162 | Set its value to 'full' to see details about the returned values. | |
| 163 | 163 | """ | |
| 164 | 164 | __slots__ = ("_working_dir", "cat_file_all", "cat_file_header", "_version_info", | |
| 165 | - "_git_options", "_environment") | ||
| 165 | + "_git_options", "_persistent_git_options", "_environment") | ||
| 166 | 166 | ||
| 167 | 167 | _excluded_ = ('cat_file_all', 'cat_file_header', '_version_info') | |
| 168 | 168 | ||
@@ -405,6 +405,7 @@ def __init__(self, working_dir=None): | |||
| 405 | 405 | super(Git, self).__init__() | |
| 406 | 406 | self._working_dir = working_dir | |
| 407 | 407 | self._git_options = () | |
| 408 | + self._persistent_git_options = [] | ||
| 408 | 409 | ||
| 409 | 410 | # Extra environment variables to pass to git commands | |
| 410 | 411 | self._environment = {} | |
@@ -421,6 +422,20 @@ def __getattr__(self, name): | |||
| 421 | 422 | return LazyMixin.__getattr__(self, name) | |
| 422 | 423 | return lambda *args, **kwargs: self._call_process(name, *args, **kwargs) | |
| 423 | 424 | ||
| 425 | + def set_persistent_git_options(self, **kwargs): | ||
| 426 | + """Specify command line options to the git executable | ||
| 427 | + for subsequent subcommand calls | ||
| 428 | + | ||
| 429 | + :param kwargs: | ||
| 430 | + is a dict of keyword arguments. | ||
| 431 | + these arguments are passed as in _call_process | ||
| 432 | + but will be passed to the git command rather than | ||
| 433 | + the subcommand. | ||
| 434 | + """ | ||
| 435 | + | ||
| 436 | + self._persistent_git_options = self.transform_kwargs( | ||
| 437 | + split_single_char_options=True, **kwargs) | ||
| 438 | + | ||
| 424 | 439 | def _set_cache_(self, attr): | |
| 425 | 440 | if attr == '_version_info': | |
| 426 | 441 | # We only use the first 4 numbers, as everthing else could be strings in fact (on windows) | |
@@ -839,7 +854,10 @@ def _call_process(self, method, *args, **kwargs): | |||
| 839 | 854 | ||
| 840 | 855 | call = [self.GIT_PYTHON_GIT_EXECUTABLE] | |
| 841 | 856 | ||
| 842 | - # add the git options, the reset to empty | ||
| 857 | + # add persistent git options | ||
| 858 | + call.extend(self._persistent_git_options) | ||
| 859 | + | ||
| 860 | + # add the git options, then reset to empty | ||
| 843 | 861 | # to avoid side_effects | |
| 844 | 862 | call.extend(self._git_options) | |
| 845 | 863 | self._git_options = () | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,10 +30,9 @@ | |||
| 30 | 30 | from git.refs import HEAD, Head, Reference, TagReference | |
| 31 | 31 | from git.remote import Remote, add_progress, to_progress_instance | |
| 32 | 32 | from git.util import Actor, finalize_process, decygpath, hex_to_bin | |
| 33 | - | ||
| 34 | 33 | import os.path as osp | |
| 35 | 34 | ||
| 36 | - from .fun import rev_parse, is_git_dir, find_git_dir, touch | ||
| 35 | + from .fun import rev_parse, is_git_dir, find_submodule_git_dir, touch | ||
| 37 | 36 | ||
| 38 | 37 | ||
| 39 | 38 | log = logging.getLogger(__name__) | |
@@ -50,7 +49,7 @@ | |||
| 50 | 49 | ||
| 51 | 50 | ||
| 52 | 51 | def _expand_path(p): | |
| 53 | - return osp.abspath(osp.expandvars(osp.expanduser(p))) | ||
| 52 | + return osp.normpath(osp.abspath(osp.expandvars(osp.expanduser(p)))) | ||
| 54 | 53 | ||
| 55 | 54 | ||
| 56 | 55 | class Repo(object): | |
@@ -69,6 +68,11 @@ class Repo(object): | |||
| 69 | 68 | 'git_dir' is the .git repository directory, which is always set.""" | |
| 70 | 69 | DAEMON_EXPORT_FILE = 'git-daemon-export-ok' | |
| 71 | 70 | ||
| 71 | + git = None # Must exist, or __del__ will fail in case we raise on `__init__()` | ||
| 72 | + working_dir = None | ||
| 73 | + _working_tree_dir = None | ||
| 74 | + git_dir = None | ||
| 75 | + | ||
| 72 | 76 | # precompiled regex | |
| 73 | 77 | re_whitespace = re.compile(r'\s+') | |
| 74 | 78 | re_hexsha_only = re.compile('^[0-9A-Fa-f]{40}$') | |
@@ -95,8 +99,9 @@ def __init__(self, path=None, odbt=DefaultDBType, search_parent_directories=Fals | |||
| 95 | 99 | repo = Repo("~/Development/git-python.git") | |
| 96 | 100 | repo = Repo("$REPOSITORIES/Development/git-python.git") | |
| 97 | 101 | ||
| 98 | - In *Cygwin*, path may be a `'cygdrive/...'` prefixed path. | ||
| 99 | - | ||
| 102 | + - In *Cygwin*, path may be a `'cygdrive/...'` prefixed path. | ||
| 103 | + - If `None, current-directory is used. | ||
| 104 | + - The :envvar:`GIT_DIR` if set and not empty takes precendance over this parameter. | ||
| 100 | 105 | :param odbt: | |
| 101 | 106 | Object DataBase type - a type which is constructed by providing | |
| 102 | 107 | the directory containing the database objects, i.e. .git/objects. It will | |
@@ -109,40 +114,39 @@ def __init__(self, path=None, odbt=DefaultDBType, search_parent_directories=Fals | |||
| 109 | 114 | :raise InvalidGitRepositoryError: | |
| 110 | 115 | :raise NoSuchPathError: | |
| 111 | 116 | :return: git.Repo """ | |
| 117 | + epath = os.getenv('GIT_DIR') or path or os.getcwd() | ||
| 112 | 118 | if Git.is_cygwin(): | |
| 113 | - path = decygpath(path) | ||
| 114 | - | ||
| 115 | - epath = _expand_path(path or os.getcwd()) | ||
| 116 | - self.git = None # should be set for __del__ not to fail in case we raise | ||
| 117 | - if not osp.exists(epath): | ||
| 119 | + epath = decygpath(epath) | ||
| 120 | + epath = _expand_path(epath or path or os.getcwd()) | ||
| 121 | + if not os.path.exists(epath): | ||
| 118 | 122 | raise NoSuchPathError(epath) | |
| 119 | 123 | ||
| 120 | - self.working_dir = None | ||
| 121 | - self._working_tree_dir = None | ||
| 122 | - self.git_dir = None | ||
| 123 | - curpath = os.getenv('GIT_DIR', epath) | ||
| 124 | - | ||
| 125 | - # walk up the path to find the .git dir | ||
| 124 | + ## Walk up the path to find the `.git` dir. | ||
| 125 | + # | ||
| 126 | + curpath = epath | ||
| 126 | 127 | while curpath: | |
| 127 | 128 | # ABOUT osp.NORMPATH | |
| 128 | 129 | # It's important to normalize the paths, as submodules will otherwise initialize their | |
| 129 | 130 | # repo instances with paths that depend on path-portions that will not exist after being | |
| 130 | 131 | # removed. It's just cleaner. | |
| 131 | 132 | if is_git_dir(curpath): | |
| 132 | - self.git_dir = osp.normpath(curpath) | ||
| 133 | - self._working_tree_dir = osp.dirname(self.git_dir) | ||
| 133 | + self.git_dir = curpath | ||
| 134 | + self._working_tree_dir = os.path.dirname(self.git_dir) | ||
| 134 | 135 | break | |
| 135 | 136 | ||
| 136 | - gitpath = find_git_dir(osp.join(curpath, '.git')) | ||
| 137 | - if gitpath is not None: | ||
| 138 | - self.git_dir = osp.normpath(gitpath) | ||
| 137 | + sm_gitpath = find_submodule_git_dir(osp.join(curpath, '.git')) | ||
| 138 | + if sm_gitpath is not None: | ||
| 139 | + self.git_dir = osp.normpath(sm_gitpath) | ||
| 140 | + sm_gitpath = find_submodule_git_dir(osp.join(curpath, '.git')) | ||
| 141 | + if sm_gitpath is not None: | ||
| 142 | + self.git_dir = _expand_path(sm_gitpath) | ||
| 139 | 143 | self._working_tree_dir = curpath | |
| 140 | 144 | break | |
| 141 | 145 | ||
| 142 | 146 | if not search_parent_directories: | |
| 143 | 147 | break | |
| 144 | - curpath, dummy = osp.split(curpath) | ||
| 145 | - if not dummy: | ||
| 148 | + curpath, tail = osp.split(curpath) | ||
| 149 | + if not tail: | ||
| 146 | 150 | break | |
| 147 | 151 | # END while curpath | |
| 148 | 152 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,7 +16,7 @@ | |||
| 16 | 16 | from git.cmd import Git | |
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | - __all__ = ('rev_parse', 'is_git_dir', 'touch', 'find_git_dir', 'name_to_object', 'short_to_long', 'deref_tag', | ||
| 19 | + __all__ = ('rev_parse', 'is_git_dir', 'touch', 'find_submodule_git_dir', 'name_to_object', 'short_to_long', 'deref_tag', | ||
| 20 | 20 | 'to_commit') | |
| 21 | 21 | ||
| 22 | 22 | ||
@@ -47,7 +47,8 @@ def is_git_dir(d): | |||
| 47 | 47 | return False | |
| 48 | 48 | ||
| 49 | 49 | ||
| 50 | - def find_git_dir(d): | ||
| 50 | + def find_submodule_git_dir(d): | ||
| 51 | + """Search for a submodule repo.""" | ||
| 51 | 52 | if is_git_dir(d): | |
| 52 | 53 | return d | |
| 53 | 54 | ||
@@ -60,12 +61,13 @@ def find_git_dir(d): | |||
| 60 | 61 | else: | |
| 61 | 62 | if content.startswith('gitdir: '): | |
| 62 | 63 | path = content[8:] | |
| 64 | + | ||
| 63 | 65 | if Git.is_cygwin(): | |
| 64 | 66 | ## Cygwin creates submodules prefixed with `/cygdrive/...` suffixes. | |
| 65 | 67 | path = decygpath(path) | |
| 66 | 68 | if not osp.isabs(path): | |
| 67 | 69 | path = osp.join(osp.dirname(d), path) | |
| 68 | - return find_git_dir(path) | ||
| 70 | + return find_submodule_git_dir(path) | ||
| 69 | 71 | # end handle exception | |
| 70 | 72 | return None | |
| 71 | 73 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -162,6 +162,20 @@ def test_options_are_passed_to_git(self): | |||
| 162 | 162 | git_command_version = self.git.version() | |
| 163 | 163 | self.assertEquals(git_version, git_command_version) | |
| 164 | 164 | ||
| 165 | + def test_persistent_options(self): | ||
| 166 | + git_command_version = self.git.version() | ||
| 167 | + # analog to test_options_are_passed_to_git | ||
| 168 | + self.git.set_persistent_git_options(version=True) | ||
| 169 | + git_version = self.git.NoOp() | ||
| 170 | + self.assertEquals(git_version, git_command_version) | ||
| 171 | + # subsequent calls keep this option: | ||
| 172 | + git_version_2 = self.git.NoOp() | ||
| 173 | + self.assertEquals(git_version_2, git_command_version) | ||
| 174 | + | ||
| 175 | + # reset to empty: | ||
| 176 | + self.git.set_persistent_git_options() | ||
| 177 | + self.assertRaises(GitCommandError, self.git.NoOp) | ||
| 178 | + | ||
| 165 | 179 | def test_single_char_git_options_are_passed_to_git(self): | |
| 166 | 180 | input_value = 'TestValue' | |
| 167 | 181 | output_value = self.git(c='user.name=%s' % input_value).config('--get', 'user.name') | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,34 +7,24 @@ | |||
| 7 | 7 | ||
| 8 | 8 | import git | |
| 9 | 9 | from git.cmd import Git | |
| 10 | - from git.compat import ( | ||
| 11 | - string_types, | ||
| 12 | - is_win, | ||
| 13 | - ) | ||
| 10 | + from git.compat import string_types, is_win | ||
| 14 | 11 | from git.exc import ( | |
| 15 | 12 | InvalidGitRepositoryError, | |
| 16 | 13 | RepositoryDirtyError | |
| 17 | 14 | ) | |
| 18 | 15 | from git.objects.submodule.base import Submodule | |
| 19 | - from git.objects.submodule.root import ( | ||
| 20 | - RootModule, | ||
| 21 | - RootUpdateProgress, | ||
| 22 | - ) | ||
| 16 | + from git.objects.submodule.root import RootModule, RootUpdateProgress | ||
| 23 | 17 | from git.repo.fun import ( | |
| 24 | - find_git_dir, | ||
| 25 | - touch, | ||
| 18 | + find_submodule_git_dir, | ||
| 19 | + touch | ||
| 26 | 20 | ) | |
| 27 | 21 | from git.test.lib import ( | |
| 28 | 22 | TestBase, | |
| 29 | - with_rw_repo, | ||
| 23 | + with_rw_repo | ||
| 30 | 24 | ) | |
| 31 | 25 | from git.test.lib import with_rw_directory | |
| 32 | - from git.util import ( | ||
| 33 | - to_native_path_linux, | ||
| 34 | - join_path_native, | ||
| 35 | - HIDE_WINDOWS_KNOWN_ERRORS, | ||
| 36 | - ) | ||
| 37 | - | ||
| 26 | + from git.util import HIDE_WINDOWS_KNOWN_ERRORS | ||
| 27 | + from git.util import to_native_path_linux, join_path_native | ||
| 38 | 28 | import os.path as osp | |
| 39 | 29 | ||
| 40 | 30 | ||
@@ -775,7 +765,7 @@ def assert_exists(sm, value=True): | |||
| 775 | 765 | else: | |
| 776 | 766 | assert osp.isfile(module_repo_path) | |
| 777 | 767 | assert sm.module().has_separate_working_tree() | |
| 778 | - assert find_git_dir(module_repo_path) is not None, "module pointed to by .git file must be valid" | ||
| 768 | + assert find_submodule_git_dir(module_repo_path) is not None, "module pointed to by .git file must be valid" | ||
| 779 | 769 | # end verify submodule 'style' | |
| 780 | 770 | ||
| 781 | 771 | # test move | |
| Back | FazBrowse Home | New Git URL |
0 commit comments