示例#1
0
def RunPager(globalConfig):
  if not os.isatty(0) or not os.isatty(1):
    return
  pager = _SelectPager(globalConfig)
  if pager == '' or pager == 'cat':
    return

  if platform_utils.isWindows():
    _PipePager(pager)
  else:
    _ForkPager(pager)
示例#2
0
def RunPager(globalConfig):
  if not os.isatty(0) or not os.isatty(1):
    return
  pager = _SelectPager(globalConfig)
  if pager == '' or pager == 'cat':
    return

  if platform_utils.isWindows():
    _PipePager(pager);
  else:
    _ForkPager(pager)
示例#3
0
    def EditString(cls, data):
        """Opens an editor to edit the given content.

    Args:
      data: The text to edit.

    Returns:
      New value of edited text.

    Raises:
      EditorError: The editor failed to run.
    """
        editor = cls._GetEditor()
        if editor == ":":
            return data

        fd, path = tempfile.mkstemp()
        try:
            os.write(fd, data.encode("utf-8"))
            os.close(fd)
            fd = None

            if platform_utils.isWindows():
                # Split on spaces, respecting quoted strings
                import shlex

                args = shlex.split(editor)
                shell = False
            elif re.compile("^.*[$ \t'].*$").match(editor):
                args = [editor + ' "$@"', "sh"]
                shell = True
            else:
                args = [editor]
                shell = False
            args.append(path)

            try:
                rc = subprocess.Popen(args, shell=shell).wait()
            except OSError as e:
                raise EditorError("editor failed, %s: %s %s" %
                                  (str(e), editor, path))
            if rc != 0:
                raise EditorError("editor failed with exit status %d: %s %s" %
                                  (rc, editor, path))

            with open(path, mode="rb") as fd2:
                return fd2.read().decode("utf-8")
        finally:
            if fd:
                os.close(fd)
            platform_utils.remove(path)
示例#4
0
    def EditString(cls, data):
        """Opens an editor to edit the given content.

       Args:
         data        : the text to edit

      Returns:
        new value of edited text; None if editing did not succeed
    """
        editor = cls._GetEditor()
        if editor == ':':
            return data

        fd, path = tempfile.mkstemp()
        try:
            os.write(fd, data)
            os.close(fd)
            fd = None

            if platform_utils.isWindows():
                # Split on spaces, respecting quoted strings
                import shlex
                args = shlex.split(editor)
                shell = False
            elif re.compile("^.*[$ \t'].*$").match(editor):
                args = [editor + ' "$@"', 'sh']
                shell = True
            else:
                args = [editor]
                shell = False
            args.append(path)

            try:
                rc = subprocess.Popen(args, shell=shell).wait()
            except OSError as e:
                raise EditorError('editor failed, %s: %s %s' %
                                  (str(e), editor, path))
            if rc != 0:
                raise EditorError('editor failed with exit status %d: %s %s' %
                                  (rc, editor, path))

            fd2 = open(path)
            try:
                return fd2.read()
            finally:
                fd2.close()
        finally:
            if fd:
                os.close(fd)
            platform_utils.remove(path)
示例#5
0
  def EditString(cls, data):
    """Opens an editor to edit the given content.

       Args:
         data        : the text to edit

      Returns:
        new value of edited text; None if editing did not succeed
    """
    editor = cls._GetEditor()
    if editor == ':':
      return data

    fd, path = tempfile.mkstemp()
    try:
      os.write(fd, data)
      os.close(fd)
      fd = None

      if platform_utils.isWindows():
        # Split on spaces, respecting quoted strings
        import shlex
        args = shlex.split(editor)
        shell = False
      elif re.compile("^.*[$ \t'].*$").match(editor):
        args = [editor + ' "$@"', 'sh']
        shell = True
      else:
        args = [editor]
        shell = False
      args.append(path)

      try:
        rc = subprocess.Popen(args, shell=shell).wait()
      except OSError as e:
        raise EditorError('editor failed, %s: %s %s'
          % (str(e), editor, path))
      if rc != 0:
        raise EditorError('editor failed with exit status %d: %s %s'
          % (rc, editor, path))

      fd2 = open(path)
      try:
        return fd2.read()
      finally:
        fd2.close()
    finally:
      if fd:
        os.close(fd)
      platform_utils.remove(path)
示例#6
0
def _SelectPager(globalConfig):
    try:
        return os.environ['GIT_PAGER']
    except KeyError:
        pass

    pager = globalConfig.GetString('core.pager')
    if pager:
        return pager

    try:
        return os.environ['PAGER']
    except KeyError:
        pass

    return 'less' if not platform_utils.isWindows() else ''
示例#7
0
    def __init__(self,
                 project,
                 cmdv,
                 bare=False,
                 input=None,
                 capture_stdout=False,
                 capture_stderr=False,
                 merge_output=False,
                 disable_editor=False,
                 ssh_proxy=None,
                 cwd=None,
                 gitdir=None):
        env = self._GetBasicEnv()

        if disable_editor:
            env['GIT_EDITOR'] = ':'
        if ssh_proxy:
            env['REPO_SSH_SOCK'] = ssh_proxy.sock()
            env['GIT_SSH'] = ssh_proxy.proxy
            env['GIT_SSH_VARIANT'] = 'ssh'
        if 'http_proxy' in env and 'darwin' == sys.platform:
            s = "'http.proxy=%s'" % (env['http_proxy'], )
            p = env.get('GIT_CONFIG_PARAMETERS')
            if p is not None:
                s = p + ' ' + s
            env['GIT_CONFIG_PARAMETERS'] = s
        if 'GIT_ALLOW_PROTOCOL' not in env:
            env['GIT_ALLOW_PROTOCOL'] = (
                'file:git:http:https:ssh:persistent-http:persistent-https:sso:rpc'
            )
        env['GIT_HTTP_USER_AGENT'] = user_agent.git

        if project:
            if not cwd:
                cwd = project.worktree
            if not gitdir:
                gitdir = project.gitdir

        command = [GIT]
        if bare:
            if gitdir:
                # Git on Windows wants its paths only using / for reliability.
                if platform_utils.isWindows():
                    gitdir = gitdir.replace('\\', '/')
                env[GIT_DIR] = gitdir
            cwd = None
        command.append(cmdv[0])
        # Need to use the --progress flag for fetch/clone so output will be
        # displayed as by default git only does progress output if stderr is a TTY.
        if sys.stderr.isatty() and cmdv[0] in ('fetch', 'clone'):
            if '--progress' not in cmdv and '--quiet' not in cmdv:
                command.append('--progress')
        command.extend(cmdv[1:])

        stdin = subprocess.PIPE if input else None
        stdout = subprocess.PIPE if capture_stdout else None
        stderr = (subprocess.STDOUT if merge_output else
                  (subprocess.PIPE if capture_stderr else None))

        if IsTrace():
            global LAST_CWD
            global LAST_GITDIR

            dbg = ''

            if cwd and LAST_CWD != cwd:
                if LAST_GITDIR or LAST_CWD:
                    dbg += '\n'
                dbg += ': cd %s\n' % cwd
                LAST_CWD = cwd

            if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]:
                if LAST_GITDIR or LAST_CWD:
                    dbg += '\n'
                dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR]
                LAST_GITDIR = env[GIT_DIR]

            dbg += ': '
            dbg += ' '.join(command)
            if stdin == subprocess.PIPE:
                dbg += ' 0<|'
            if stdout == subprocess.PIPE:
                dbg += ' 1>|'
            if stderr == subprocess.PIPE:
                dbg += ' 2>|'
            elif stderr == subprocess.STDOUT:
                dbg += ' 2>&1'
            Trace('%s', dbg)

        try:
            p = subprocess.Popen(command,
                                 cwd=cwd,
                                 env=env,
                                 encoding='utf-8',
                                 errors='backslashreplace',
                                 stdin=stdin,
                                 stdout=stdout,
                                 stderr=stderr)
        except Exception as e:
            raise GitError('%s: %s' % (command[1], e))

        if ssh_proxy:
            ssh_proxy.add_client(p)

        self.process = p
        if input:
            if isinstance(input, str):
                input = input.encode('utf-8')
            p.stdin.write(input)
            p.stdin.close()

        try:
            self.stdout, self.stderr = p.communicate()
        finally:
            if ssh_proxy:
                ssh_proxy.remove_client(p)
        self.rc = p.wait()