1 from __future__
import division
, absolute_import
, unicode_literals
9 from os
.path
import join
12 from .compat
import int_types
13 from .compat
import ustr
14 from .decorators
import memoize
15 from .interaction
import Interaction
18 INDEX_LOCK
= threading
.Lock()
19 GIT_COLA_TRACE
= core
.getenv('GIT_COLA_TRACE', '')
26 return s
.replace('_', '-')
29 def is_git_dir(git_dir
):
30 """From git's setup.c:is_git_directory()."""
33 headref
= join(git_dir
, 'HEAD')
35 if (core
.isdir(git_dir
) and
36 (core
.isdir(join(git_dir
, 'objects')) and
37 core
.isdir(join(git_dir
, 'refs'))) or
38 (core
.isfile(join(git_dir
, 'gitdir')) and
39 core
.isfile(join(git_dir
, 'commondir')))):
41 result
= (core
.isfile(headref
) or
42 (core
.islink(headref
) and
43 core
.readlink(headref
).startswith('refs/')))
45 result
= is_git_file(git_dir
)
51 return core
.isfile(f
) and '.git' == os
.path
.basename(f
)
54 def is_git_worktree(d
):
55 return is_git_dir(join(d
, '.git'))
58 def read_git_file(path
):
59 """Read the path from a .git-file
61 `None` is returned when <path> is not a .git-file.
65 if path
and is_git_file(path
):
67 data
= core
.read(path
).strip()
68 if data
.startswith(header
):
69 result
= data
[len(header
):]
74 """Git repository paths of interest"""
76 def __init__(self
, git_dir
=None, git_file
=None, worktree
=None):
77 self
.git_dir
= git_dir
78 self
.git_file
= git_file
79 self
.worktree
= worktree
82 def find_git_directory(curpath
):
83 """Perform Git repository discovery
86 paths
= Paths(git_dir
=core
.getenv('GIT_DIR'),
87 worktree
=core
.getenv('GIT_WORK_TREE'),
91 ceiling
= core
.getenv('GIT_CEILING_DIRECTORIES')
93 ceiling_dirs
.update([x
for x
in ceiling
.split(':') if x
])
95 if not paths
.git_dir
or not paths
.worktree
:
97 curpath
= core
.abspath(curpath
)
99 # Search for a .git directory
101 if curpath
in ceiling_dirs
:
103 if is_git_dir(curpath
):
104 paths
.git_dir
= curpath
105 if os
.path
.basename(curpath
) == '.git':
106 paths
.worktree
= os
.path
.dirname(curpath
)
108 gitpath
= join(curpath
, '.git')
109 if is_git_dir(gitpath
):
110 paths
.git_dir
= gitpath
111 paths
.worktree
= curpath
113 curpath
, dummy
= os
.path
.split(curpath
)
117 git_dir_path
= read_git_file(paths
.git_dir
)
119 paths
.git_file
= paths
.git_dir
120 paths
.git_dir
= git_dir_path
127 The Git class manages communication with the Git binary
132 self
._git
_cwd
= None #: The working directory used by execute()
133 self
._valid
= {} #: Store the result of is_git_dir() for performance
134 self
.set_worktree(core
.getcwd())
139 def _find_git_directory(self
, path
):
141 self
.paths
= find_git_directory(path
)
143 # Update the current directory for executing commands
144 if self
.paths
.worktree
:
145 self
._git
_cwd
= self
.paths
.worktree
146 elif self
.paths
.git_dir
:
147 self
._git
_cwd
= self
.paths
.git_dir
149 def set_worktree(self
, path
):
150 path
= core
.decode(path
)
151 self
._find
_git
_directory
(path
)
152 return self
.paths
.worktree
155 if not self
.paths
.worktree
:
156 path
= core
.abspath(core
.getcwd())
157 self
._find
_git
_directory
(path
)
158 return self
.paths
.worktree
161 """Is this a valid git repository?
163 Cache the result to avoid hitting the filesystem.
166 git_dir
= self
.paths
.git_dir
168 valid
= bool(git_dir
) and self
._valid
[git_dir
]
170 valid
= self
._valid
[git_dir
] = is_git_dir(git_dir
)
174 def git_path(self
, *paths
):
175 if self
.paths
.git_dir
:
176 result
= join(self
.paths
.git_dir
, *paths
)
182 if not self
.paths
.git_dir
:
183 path
= core
.abspath(core
.getcwd())
184 self
._find
_git
_directory
(path
)
185 return self
.paths
.git_dir
187 def __getattr__(self
, name
):
188 git_cmd
= functools
.partial(self
.git
, name
)
189 setattr(self
, name
, git_cmd
)
199 _stderr
=subprocess
.PIPE
,
200 _stdout
=subprocess
.PIPE
,
202 _no_win32_startupinfo
=False):
204 Execute a command and returns its output
206 :param command: argument list to execute.
207 :param _cwd: working directory, defaults to the current directory.
208 :param _decode: whether to decode output, defaults to True.
209 :param _encoding: default encoding, defaults to None (utf-8).
210 :param _raw: do not strip trailing whitespace.
211 :param _stdin: optional stdin filehandle.
212 :returns (status, out, err): exit status, stdout, stderr
215 # Allow the user to have the command executed in their working dir.
221 if hasattr(os
, 'setsid'):
222 # SSH uses the SSH_ASKPASS variable only if the process is really
223 # detached from the TTY (stdin redirection and setting the
224 # SSH_ASKPASS environment variable is not enough). To detach a
225 # process from the console it should fork and call os.setsid().
226 extra
['preexec_fn'] = os
.setsid
229 # Guard against thread-unsafe .git/index.lock files
232 status
, out
, err
= core
.run_command(
233 command
, cwd
=_cwd
, encoding
=_encoding
,
234 stdin
=_stdin
, stdout
=_stdout
, stderr
=_stderr
,
235 no_win32_startupinfo
=_no_win32_startupinfo
, **extra
)
236 # Let the next thread in
240 if not _raw
and out
is not None:
241 out
= out
.rstrip('\n')
243 cola_trace
= GIT_COLA_TRACE
244 if cola_trace
== 'trace':
245 msg
= 'trace: ' + core
.list2cmdline(command
)
246 Interaction
.log_status(status
, msg
, '')
247 elif cola_trace
== 'full':
249 core
.stderr("%s -> %d: '%s' '%s'" %
250 (' '.join(command
), status
, out
, err
))
252 core
.stderr("%s -> %d" % (' '.join(command
), status
))
254 core
.stderr(' '.join(command
))
256 # Allow access to the command's status code
257 return (status
, out
, err
)
259 def transform_kwargs(self
, **kwargs
):
260 """Transform kwargs into git command line options
262 Callers can assume the following behavior:
264 Passing foo=None ignores foo, so that callers can
265 use default values of None that are ignored unless
268 Passing foo=False ignore foo, for the same reason.
270 Passing foo={string-or-number} results in ['--foo=<value>']
271 in the resulting arguments.
275 types_to_stringify
= set((ustr
, float, str) + int_types
)
277 for k
, v
in kwargs
.items():
284 type_of_value
= type(v
)
286 args
.append('%s%s' % (dashes
, dashify(k
)))
287 elif type_of_value
in types_to_stringify
:
288 args
.append('%s%s%s%s' % (dashes
, dashify(k
), join
, v
))
292 def git(self
, cmd
, *args
, **kwargs
):
293 # Handle optional arguments prior to calling transform_kwargs
294 # otherwise they'll end up in args, which is bad.
295 _kwargs
= dict(_cwd
=self
._git
_cwd
)
305 '_no_win32_startupinfo',
308 for kwarg
in execute_kwargs
:
310 _kwargs
[kwarg
] = kwargs
.pop(kwarg
)
312 # Prepare the argument list
313 git_args
= ['git', '-c', 'diff.suppressBlankEmpty=false', dashify(cmd
)]
314 opt_args
= self
.transform_kwargs(**kwargs
)
315 call
= git_args
+ opt_args
318 return self
.execute(call
, **_kwargs
)
320 if e
.errno
!= errno
.ENOENT
:
324 # see if git exists at all. on win32 it can fail with ENOENT in
325 # case of argv overflow. we should be safe from that but use
326 # defensive coding for the worst-case scenario. on other OS-en
327 # we have ENAMETOOLONG which doesn't exist in with32 API.
328 status
, out
, err
= self
.execute(['git', '--version'])
332 core
.stderr("error: unable to execute 'git'\n"
333 "error: please ensure that 'git' is in your $PATH")
334 if sys
.platform
== 'win32':
335 _print_win32_git_hint()
339 def _print_win32_git_hint():
341 'hint: If you have Git installed in a custom location, e.g.\n'
342 'hint: C:\\Tools\\Git, then you can create a file at\n'
343 'hint: ~/.config/git-cola/git-bindir with following text\n'
344 'hint: and git-cola will add the specified location to your $PATH\n'
345 'hint: automatically when starting cola:\n'
347 'hint: C:\\Tools\\Git\\bin\n')
353 """Return the Git singleton"""
359 Git command singleton
362 >>> 'git' == git.version()[0][:3].lower()