def _detect_type(self, p):

        # check for SVN
        #  - header starts with Index:
        #  - next line is ===... delimiter
        #  - filename is followed by revision number
        # TODO add SVN revision
        if (len(p.header) > 1 and p.header[-2].startswith(b"Index: ")
            and p.header[-1].startswith(b"=" * 67)):
            return SVN

        # common checks for both HG and GIT
        DVCS = ((p.source.startswith(b'a/') or p.source == b'/dev/null')
                and (p.target.startswith(b'b/') or p.target == b'/dev/null'))

        # GIT type check
        #  - header[-2] is like "diff --git a/oldname b/newname"
        #  - header[-1] is like "index <hash>..<hash> <mode>"
        # TODO add git rename diffs and add/remove diffs
        #      add git diff with spaced filename
        # TODO http://www.kernel.org/pub/software/scm/git/docs/git-diff.html

        # Git patch header len is 2 min
        if len(p.header) > 1:
            # detect the start of diff header - there might be some comments before
            for idx in reversed(range(len(p.header))):
                if p.header[idx].startswith(b"diff --git"):
                    break
            if p.header[idx].startswith(b'diff --git a/'):
                if (idx + 1 < len(p.header)
                    and re.match(b'index \\w{7}..\\w{7} \\d{6}', p.header[idx + 1])):
                    if DVCS:
                        return GIT

        # HG check
        #
        #  - for plain HG format header is like "diff -r b2d9961ff1f5 filename"
        #  - for Git-style HG patches it is "diff --git a/oldname b/newname"
        #  - filename starts with a/, b/ or is equal to /dev/null
        #  - exported changesets also contain the header
        #    # HG changeset patch
        #    # User name@example.com
        #    ...
        # TODO add MQ
        # TODO add revision info
        if len(p.header) > 0:
            if DVCS and re.match(b'diff -r \\w{12} .*', p.header[-1]):
                return HG
            if DVCS and p.header[-1].startswith(b'diff --git a/'):
                if len(p.header) == 1:  # native Git patch header len is 2
                    return HG
                elif p.header[0].startswith(b'# HG changeset patch'):
                    return HG

        return PLAIN
I{getpwuid} raises L{KeyError} when passed a uid which does not exist
dataset/ETHPy150Open ensime/ensime-sublime/patch.py/PatchSet._detect_type--crossover with--dataset/ETHPy150Open kuri65536/python-for-android/python-modules/twisted/twisted/python/test/test_fakepwd.py/UserDatabaseTestsMixin.test_noSuchUID