|
17 | 17 | import subprocess
|
18 | 18 | import sys
|
19 | 19 | import threading
|
| 20 | +from textwrap import dedent |
20 | 21 |
|
21 | 22 | from git.compat import (
|
22 | 23 | string_types,
|
@@ -182,16 +183,69 @@ def __setstate__(self, d):
|
182 | 183 | # Enables debugging of GitPython's git commands
|
183 | 184 | GIT_PYTHON_TRACE = os.environ.get("GIT_PYTHON_TRACE", False)
|
184 | 185 |
|
185 |
| - # Provide the full path to the git executable. Otherwise it assumes git is in the path |
186 |
| - _git_exec_env_var = "GIT_PYTHON_GIT_EXECUTABLE" |
187 |
| - GIT_PYTHON_GIT_EXECUTABLE = os.environ.get(_git_exec_env_var, git_exec_name) |
188 |
| - |
189 | 186 | # If True, a shell will be used when executing git commands.
|
190 | 187 | # This should only be desirable on Windows, see https://github.com/gitpython-developers/GitPython/pull/126
|
191 | 188 | # and check `git/test_repo.py:TestRepo.test_untracked_files()` TC for an example where it is required.
|
192 | 189 | # Override this value using `Git.USE_SHELL = True`
|
193 | 190 | USE_SHELL = False
|
194 | 191 |
|
| 192 | + # Provide the full path to the git executable. Otherwise it assumes git is in the path |
| 193 | + @classmethod |
| 194 | + def refresh(cls, path=None): |
| 195 | + """Convenience method for refreshing the git executable path.""" |
| 196 | + cls.setup(path=path) |
| 197 | + |
| 198 | + @classmethod |
| 199 | + def setup(cls, path=None): |
| 200 | + """Convenience method for setting the git executable path.""" |
| 201 | + if path is not None: |
| 202 | + # use the path the user gave |
| 203 | + os.environ[cls._git_exec_env_var] = path |
| 204 | + elif cls._git_exec_env_var in os.environ: |
| 205 | + # fall back to the environment variable that's already set |
| 206 | + pass |
| 207 | + else: |
| 208 | + # hope that git can be found on the user's $PATH |
| 209 | + pass |
| 210 | + |
| 211 | + old_git = cls.GIT_PYTHON_GIT_EXECUTABLE |
| 212 | + new_git = os.environ.get(cls._git_exec_env_var, cls.git_exec_name) |
| 213 | + cls.GIT_PYTHON_GIT_EXECUTABLE = new_git |
| 214 | + |
| 215 | + has_git = False |
| 216 | + try: |
| 217 | + cls().version() |
| 218 | + has_git = True |
| 219 | + except GitCommandNotFound: |
| 220 | + pass |
| 221 | + |
| 222 | + if not has_git: |
| 223 | + err = dedent("""\ |
| 224 | + Bad git executable. The git executable must be specified in one of the following ways: |
| 225 | + (1) be included in your $PATH, or |
| 226 | + (2) be set via $GIT_PYTHON_GIT_EXECUTABLE, or |
| 227 | + (3) explicitly call git.cmd.setup with the full path. |
| 228 | + """) |
| 229 | + |
| 230 | + if old_git is None: |
| 231 | + # on the first setup (when GIT_PYTHON_GIT_EXECUTABLE is |
| 232 | + # None) we only warn the user and simply set the default |
| 233 | + # executable |
| 234 | + cls.GIT_PYTHON_GIT_EXECUTABLE = cls.git_exec_name |
| 235 | + print("WARNING: %s" % err) |
| 236 | + else: |
| 237 | + # after the first setup (when GIT_PYTHON_GIT_EXECUTABLE |
| 238 | + # is no longer None) we raise an exception and reset the |
| 239 | + # GIT_PYTHON_GIT_EXECUTABLE to whatever the value was |
| 240 | + # previously |
| 241 | + cls.GIT_PYTHON_GIT_EXECUTABLE = old_name |
| 242 | + raise GitCommandNotFound("git", err) |
| 243 | + |
| 244 | + _git_exec_env_var = "GIT_PYTHON_GIT_EXECUTABLE" |
| 245 | + # immediately set with the default value ("git") |
| 246 | + GIT_PYTHON_GIT_EXECUTABLE = None |
| 247 | + # see the setup performed below |
| 248 | + |
195 | 249 | @classmethod
|
196 | 250 | def is_cygwin(cls):
|
197 | 251 | return is_cygwin_git(cls.GIT_PYTHON_GIT_EXECUTABLE)
|
@@ -828,13 +882,13 @@ def _call_process(self, method, *args, **kwargs):
|
828 | 882 | - "command options" to be converted by :meth:`transform_kwargs()`;
|
829 | 883 | - the `'insert_kwargs_after'` key which its value must match one of ``*args``,
|
830 | 884 | and any cmd-options will be appended after the matched arg.
|
831 |
| - |
| 885 | +
|
832 | 886 | Examples::
|
833 |
| - |
| 887 | +
|
834 | 888 | git.rev_list('master', max_count=10, header=True)
|
835 |
| - |
| 889 | +
|
836 | 890 | turns into::
|
837 |
| - |
| 891 | +
|
838 | 892 | git rev-list max-count 10 --header master
|
839 | 893 |
|
840 | 894 | :return: Same as ``execute``"""
|
@@ -970,3 +1024,16 @@ def clear_cache(self):
|
970 | 1024 | self.cat_file_all = None
|
971 | 1025 | self.cat_file_header = None
|
972 | 1026 | return self
|
| 1027 | + |
| 1028 | + |
| 1029 | + |
| 1030 | +# this is where the git executable is setup |
| 1031 | +def setup(path=None): |
| 1032 | + Git.setup(path=path) |
| 1033 | + |
| 1034 | + |
| 1035 | +def refresh(path=None): |
| 1036 | + Git.refresh(path=path) |
| 1037 | + |
| 1038 | + |
| 1039 | +setup() |
0 commit comments