Exemplo n.º 1
0
def get_commit(sha1):
    """
    Retrieve the text of a Git commit given its SHA1.
    Returns an empty string if the commit file cannot be found.
    """
    blob = cat_file(sha1)
    blob = blob[blob.index(b'\x00') + 1:]
    return p4gf_char.decode(blob)
Exemplo n.º 2
0
 def get_data(self):
     """Read a git style string: <size> SP <string> [LF]."""
     self.get_token(SP)
     count = int(self.get_token(LF))
     string = p4gf_char.decode(self.text[self.offset:self.offset + count])
     self.offset += count
     self.skip_optional_lf()
     return string
Exemplo n.º 3
0
def _popen_no_throw_internal(cmd_, expect_error, stdin=None, env=None):
    """
    Internal Popen() wrapper that records command and result to log.
    The standard output and error results are converted to text using
    the p4gf_char.decode() function.
    """
    if _validate_popen(cmd_) is None:
        return None
    cmd = translate_git_cmd(cmd_)
    result = ChildProc.popen(cmd, stdin, env)
    result['cmd'] = ' '.join(cmd_)  # use the untranslated cmd_ for logging
    if 'out' in result:
        result['out'] = p4gf_char.decode(result['out'])
    if 'err' in result:
        result['err'] = p4gf_char.decode(result['err'])
    _log_cmd_result(result, expect_error)
    return result
Exemplo n.º 4
0
def _popen_no_throw_internal(cmd_, expect_error, stdin=None, env=None):
    """
    Internal Popen() wrapper that records command and result to log.
    The standard output and error results are converted to text using
    the p4gf_char.decode() function.
    """
    if _validate_popen(cmd_) is None:
        return None
    cmd = translate_git_cmd(cmd_)
    result = ChildProc.popen(cmd, stdin, env)
    result['cmd'] = ' '.join(cmd_)   # use the untranslated cmd_ for logging
    if 'out' in result:
        result['out'] = p4gf_char.decode(result['out'])
    if 'err' in result:
        result['err'] = p4gf_char.decode(result['err'])
    _log_cmd_result(result, expect_error)
    return result
Exemplo n.º 5
0
 def get_data(self):
     """read a git style string: <size> SP <string> [LF]"""
     self.get_token(SP)
     count = int(self.get_token(LF))
     string = p4gf_char.decode(self.text[self.offset:self.offset + count])
     self.offset += count
     self.skip_optional_lf()
     return string
Exemplo n.º 6
0
def get_commit(sha1, view_repo):
    """Retrieve the text of a Git commit given its SHA1.

    Returns an empty string if the commit file cannot be found.

    """
    blob = cat_file(sha1, view_repo)
    blob = blob[blob.index(b'\x00') + 1:]
    return p4gf_char.decode(blob)
Exemplo n.º 7
0
    def get_token(self, separator):
        """Return the next token, advancing position.

        If no token available, raises error

        If separator is more than one char, first char is the actual
        separator and rest is lookahead, so offset will be left pointing
        at second char of 'separator'.
        """
        sep = self.text.find(separator, self.offset)
        if sep == -1:
            raise RuntimeError(_("error parsing git-fast-export: expected '{separator}'")
                               .format(separator=separator.decode()))
        token = p4gf_char.decode(self.text[self.offset:sep])
        self.offset = sep + 1
        return token
Exemplo n.º 8
0
    def get_token(self, separator):
        """return the next token, advancing position

        If no token available, raises error

        If separator is more than one char, first char is the actual
        separator and rest is lookahead, so offset will be left pointing
        at second char of 'separator'.
        """
        sep = self.text.find(separator, self.offset)
        if sep == -1:
            raise RuntimeError(_("error parsing git-fast-export: expected '{}'")
                               .format(separator.decode()))
        token = p4gf_char.decode(self.text[self.offset:sep])
        self.offset = sep + 1
        return token
Exemplo n.º 9
0
def remove_backslash_escapes(ba):
    """given an bytearray with a path escaped by git-fast-export
    return an unescaped string

    quotes are escaped as \\"
    control characters are escaped as \\\\a and similar
    unicode chars are escaped utf8, with \\ooo for each byte
    """
    # pylint: disable=anomalous-backslash-in-string
    ba = re.sub(b'\\\\\d{3}', unescape_unicode, ba)
    ba = re.sub(b'\\\\a', b'\\a', ba)
    ba = re.sub(b'\\\\b', b'\\b', ba)
    ba = re.sub(b'\\\\f', b'\\f', ba)
    ba = re.sub(b'\\\\n', b'\\n', ba)
    ba = re.sub(b'\\\\r', b'\\r', ba)
    ba = re.sub(b'\\\\t', b'\\t', ba)
    ba = re.sub(b'\\\\v', b'\\v', ba)
    ba = ba.replace(b'\\"', b'"')
    ba = ba.replace(b'\\\\', b'\\')
    return p4gf_char.decode(ba)
Exemplo n.º 10
0
def string_from_print(d):
    """Create a string from p4 print dict.

    This is a noop for unicode servers, because p4python returns strings.

    But for non-unicode servers, when running 'p4 print' we use "raw" encoding
    with p4python to avoid mangling file content, so we get back bytes from
    p4python, which need to be decoded. Usually those bytes are utf8,
    but that's not guaranteed. Let p4gf_char.decode() find a match.
    """
    if type(d) == str:
        return sys.intern(d)
    try:
        s = p4gf_char.decode(d)
        #return sys.intern(d.decode(locale.nl_langinfo(locale.CODESET)))
        return sys.intern(s)
    except UnicodeDecodeError:
        replaced = d.decode(locale.nl_langinfo(locale.CODESET),
                            'replace').replace('\ufffd', '?')
        raise RuntimeError(
            _('Error decoding file path: {path}').format(path=replaced))
Exemplo n.º 11
0
def remove_backslash_escapes(ba):
    """
    given an bytearray with a path escaped by git-fast-export
    return an unescaped string

    quotes are escaped as \\"
    control characters are escaped as \\\\a and similar
    unicode chars are escaped utf8, with \\ooo for each byte
    """
    # pylint: disable=W1401
    # Anomalous backslash in string
    ba = re.sub(b'\\\\\d{3}', unescape_unicode, ba)
    ba = re.sub(b'\\\\a', b'\\a', ba)
    ba = re.sub(b'\\\\b', b'\\b', ba)
    ba = re.sub(b'\\\\f', b'\\f', ba)
    ba = re.sub(b'\\\\n', b'\\n', ba)
    ba = re.sub(b'\\\\r', b'\\r', ba)
    ba = re.sub(b'\\\\t', b'\\t', ba)
    ba = re.sub(b'\\\\v', b'\\v', ba)
    ba = ba.replace(b'\\"', b'"')
    return p4gf_char.decode(ba)
Exemplo n.º 12
0
 def peek_token(self, separator):
     """Return the next token or None, without advancing position."""
     sep = self.text.find(separator, self.offset)
     if sep == -1:
         return None
     return p4gf_char.decode(self.text[self.offset:sep])
Exemplo n.º 13
0
 def peek_token(self, separator):
     """return the next token or None, without advancing position"""
     sep = self.text.find(separator, self.offset)
     if sep == -1:
         return None
     return p4gf_char.decode(self.text[self.offset:sep])