235
244
return cmd_arg.replace('^', '^^')
238
def shell_quote(*strings):
240
Quote strings so that they can be suitably martialled
241
off to the shell. This method supports POSIX sh syntax.
242
This is crucial to properly handle command line arguments
243
with spaces, quotes, double-quotes, etc. on darwin/win32...
246
regex = re.compile('[^\w!%+,\-./:@^]')
247
quote_regex = re.compile("((?:'\\''){2,})")
255
raise ValueError('No way to quote strings '
256
'containing null(\\000) bytes')
258
# = does need quoting else in command position it's a
259
# program-local environment setting
260
match = regex.search(s)
261
if match and '=' not in s:
263
s = s.replace("'", "'\\''")
265
# make multiple ' in a row look simpler
266
# '\'''\'''\'' -> '"'''"'
267
quote_match = quote_regex.match(s)
269
quotes = match.group(1)
270
s.replace(quotes, ("'" *(len(quotes)/4)) + "\"'")
273
if s.startswith("''"):
249
Subclass Git to provide search-for-git-dir
254
self.load_worktree(os.getcwd())
256
def load_worktree(self, path):
258
self._worktree = None
263
return self._worktree
266
curdir = self._git_dir
270
if self._is_git_dir(os.path.join(curdir, '.git')):
273
# Handle bare repositories
274
if (len(os.path.basename(curdir)) > 4
275
and curdir.endswith('.git')):
277
if 'GIT_WORK_TREE' in os.environ:
278
self._worktree = os.getenv('GIT_WORK_TREE')
279
if not self._worktree or not os.path.isdir(self._worktree):
281
gitparent = os.path.join(os.path.abspath(self._git_dir), '..')
282
self._worktree = os.path.abspath(gitparent)
283
self.set_cwd(self._worktree)
284
return self._worktree
287
return self._git_dir and self._is_git_dir(self._git_dir)
289
def git_path(self, *paths):
290
return os.path.join(self.git_dir(), *paths)
295
if 'GIT_DIR' in os.environ:
296
self._git_dir = os.getenv('GIT_DIR')
298
curpath = os.path.abspath(self._git_dir)
300
curpath = os.path.abspath(os.getcwd())
301
# Search for a .git directory
303
if self._is_git_dir(curpath):
304
self._git_dir = curpath
306
gitpath = os.path.join(curpath, '.git')
307
if self._is_git_dir(gitpath):
308
self._git_dir = gitpath
310
curpath, dummy = os.path.split(curpath)
315
def _is_git_dir(self, d):
316
"""From git's setup.c:is_git_directory()."""
318
and os.path.isdir(os.path.join(d, 'objects'))
319
and os.path.isdir(os.path.join(d, 'refs'))):
320
headref = os.path.join(d, 'HEAD')
321
return (os.path.isfile(headref)
322
or (os.path.islink(headref)
323
and os.readlink(headref).startswith('refs')))