diff options
Diffstat (limited to 'git/cmd.py')
-rw-r--r-- | git/cmd.py | 53 |
1 files changed, 46 insertions, 7 deletions
@@ -42,7 +42,8 @@ class Git(LazyMixin): of the command to stdout. Set its value to 'full' to see details about the returned values. """ - __slots__ = ("_working_dir", "cat_file_all", "cat_file_header", "_version_info") + __slots__ = ("_working_dir", "cat_file_all", "cat_file_header", "_version_info", + "_git_options") # CONFIGURATION # The size in bytes read from stdout when copying git's output to another stream @@ -224,7 +225,8 @@ class Git(LazyMixin): .git directory in case of bare repositories.""" super(Git, self).__init__() self._working_dir = working_dir - + self._git_options = () + # cached command slots self.cat_file_header = None self.cat_file_all = None @@ -241,7 +243,7 @@ class Git(LazyMixin): if attr == '_version_info': # We only use the first 4 numbers, as everthing else could be strings in fact (on windows) version_numbers = self._call_process('version').split(' ')[2] - self._version_info = tuple(int(n) for n in version_numbers.split('.')[:4]) + self._version_info = tuple(int(n) for n in version_numbers.split('.')[:4] if n.isdigit()) else: super(Git, self)._set_cache_(attr) #END handle version info @@ -321,6 +323,9 @@ class Git(LazyMixin): if ouput_stream is True, the stdout value will be your output stream: * output_stream if extended_output = False * tuple(int(status), output_stream, str(stderr)) if extended_output = True + + Note git is executed with LC_MESSAGES="C" to ensure consitent + output regardless of system language. :raise GitCommandError: @@ -338,6 +343,7 @@ class Git(LazyMixin): # Start the process proc = Popen(command, + env={"LC_MESSAGES": "C"}, cwd=cwd, stdin=istream, stderr=PIPE, @@ -385,7 +391,10 @@ class Git(LazyMixin): # END handle debug printing if with_exceptions and status != 0: - raise GitCommandError(command, status, stderr_value) + if with_extended_output: + raise GitCommandError(command, status, stderr_value, stdout_value) + else: + raise GitCommandError(command, status, stderr_value) # Allow access to the command's status code if with_extended_output: @@ -393,7 +402,7 @@ class Git(LazyMixin): else: return stdout_value - def transform_kwargs(self, **kwargs): + def transform_kwargs(self, split_single_char_options=False, **kwargs): """Transforms Python style kwargs into git command line options.""" args = list() for k, v in kwargs.items(): @@ -401,7 +410,10 @@ class Git(LazyMixin): if v is True: args.append("-%s" % k) elif type(v) is not bool: - args.append("-%s%s" % (k, v)) + if split_single_char_options: + args.extend(["-%s" % k, "%s" % v]) + else: + args.append("-%s%s" % (k, v)) else: if v is True: args.append("--%s" % dashify(k)) @@ -412,18 +424,38 @@ class Git(LazyMixin): @classmethod def __unpack_args(cls, arg_list): if not isinstance(arg_list, (list,tuple)): + if isinstance(arg_list, unicode): + return [arg_list.encode('utf-8')] return [ str(arg_list) ] outlist = list() for arg in arg_list: if isinstance(arg_list, (list, tuple)): outlist.extend(cls.__unpack_args( arg )) + elif isinstance(arg_list, unicode): + outlist.append(arg_list.encode('utf-8')) # END recursion else: outlist.append(str(arg)) # END for each arg return outlist + def __call__(self, **kwargs): + """Specify command line options to the git executable + for a subcommand call + + :param kwargs: + is a dict of keyword arguments. + these arguments are passed as in _call_process + but will be passed to the git command rather than + the subcommand. + + ``Examples``:: + git(work_tree='/tmp').difftool()""" + self._git_options = self.transform_kwargs( + split_single_char_options=True, **kwargs) + return self + def _call_process(self, method, *args, **kwargs): """Run the given git command with the specified arguments and return the result as a String @@ -462,7 +494,14 @@ class Git(LazyMixin): args = opt_args + ext_args def make_call(): - call = [self.GIT_PYTHON_GIT_EXECUTABLE, dashify(method)] + call = [self.GIT_PYTHON_GIT_EXECUTABLE] + + # add the git options, the reset to empty + # to avoid side_effects + call.extend(self._git_options) + self._git_options = () + + call.extend([dashify(method)]) call.extend(args) return call #END utility to recreate call after changes |