Beispiel #1
0
    def test_parse(self):
        self.assertEqual(Commit.parse('123@main'),
                         Commit(identifier=123, branch='main'))
        self.assertEqual(Commit.parse('123'), Commit(identifier=123))

        self.assertEqual(Commit.parse('r123'), Commit(revision=123))

        self.assertEqual(
            Commit.parse('c3bd784f8b88bd03f64467ddd3304ed8be28acbe'),
            Commit(hash='c3bd784f8b88bd03f64467ddd3304ed8be28acbe'),
        )
Beispiel #2
0
    def checkout(self, argument):
        if not isinstance(argument, six.string_types):
            raise ValueError(
                "Expected 'argument' to be a string, not '{}'".format(
                    type(argument)))

        self._branch = None

        if log.level > logging.WARNING:
            log_arg = ['-q']
        elif log.level < logging.WARNING:
            log_arg = ['--progress']
        else:
            log_arg = []

        parsed_commit = Commit.parse(argument, do_assert=False)
        if parsed_commit:
            commit = self.commit(
                hash=parsed_commit.hash,
                revision=parsed_commit.revision,
                identifier=parsed_commit.identifier,
                branch=parsed_commit.branch,
            )
            return None if run(
                [self.executable(), 'checkout'] + [commit.hash] + log_arg,
                cwd=self.root_path,
            ).returncode else commit

        return None if run(
            [self.executable(), 'checkout'] + [argument] + log_arg,
            cwd=self.root_path,
        ).returncode else self.commit()
Beispiel #3
0
    def find(self, argument, include_log=True, include_identifier=True):
        if not isinstance(argument, six.string_types):
            raise ValueError("Expected 'argument' to be a string, not '{}'".format(type(argument)))

        if argument in self.DEFAULT_BRANCHES:
            argument = self.default_branch

        parsed_commit = Commit.parse(argument, do_assert=False)
        if parsed_commit:
            if parsed_commit.branch in self.DEFAULT_BRANCHES:
                parsed_commit.branch = self.default_branch

            return self.commit(
                hash=parsed_commit.hash,
                revision=parsed_commit.revision,
                identifier=parsed_commit.identifier,
                branch=parsed_commit.branch,
                include_log=include_log,
                include_identifier=include_identifier,
            )

        commit_data = self.request('commits/{}'.format(argument))
        if not commit_data:
            raise ValueError("'{}' is not an argument recognized by git".format(argument))
        return self.commit(hash=commit_data['sha'], include_log=include_log, include_identifier=include_identifier)
Beispiel #4
0
    def find(self, argument, include_log=True):
        if not isinstance(argument, six.string_types):
            raise ValueError(
                "Expected 'argument' to be a string, not '{}'".format(
                    type(argument)))

        parsed_commit = Commit.parse(argument, do_assert=False)
        if parsed_commit:
            return self.commit(
                hash=parsed_commit.hash,
                revision=parsed_commit.revision,
                identifier=parsed_commit.identifier,
                branch=parsed_commit.branch,
                include_log=include_log,
            )

        output = run(
            [self.executable(), 'rev-parse', argument],
            cwd=self.root_path,
            capture_output=True,
            encoding='utf-8',
        )
        if output.returncode:
            raise ValueError(
                "'{}' is not an argument recognized by git".format(argument))
        return self.commit(hash=output.stdout.rstrip(),
                           include_log=include_log)
Beispiel #5
0
    def find(self, argument, include_log=True, include_identifier=True):
        if not isinstance(argument, six.string_types):
            raise ValueError("Expected 'argument' to be a string, not '{}'".format(type(argument)))

        offset = 0
        if '~' in argument:
            for s in argument.split('~')[1:]:
                if s and not s.isdigit():
                    raise ValueError("'{}' is not a valid argument to Scm.find()".format(argument))
                offset += int(s) if s else 1
            argument = argument.split('~')[0]

        if argument in self.DEFAULT_BRANCHES:
            argument = self.default_branch

        if argument == 'HEAD':
            result = self.commit(include_log=include_log, include_identifier=include_identifier)

        elif argument in self.branches:
            result = self.commit(branch=argument, include_log=include_log, include_identifier=include_identifier)

        elif argument in self.tags:
            result = self.commit(tag=argument, include_log=include_log, include_identifier=include_identifier)

        else:
            if offset:
                raise ValueError("'~' offsets are not supported for revisions and identifiers")

            parsed_commit = Commit.parse(argument)
            if parsed_commit.branch in self.DEFAULT_BRANCHES:
                parsed_commit.branch = self.default_branch

            return self.commit(
                hash=parsed_commit.hash,
                revision=parsed_commit.revision,
                identifier=parsed_commit.identifier,
                branch=parsed_commit.branch,
                include_log=include_log,
                include_identifier=include_identifier,
            )

        if not offset:
            return result

        return self.commit(
            identifier=result.identifier - offset,
            branch=result.branch,
            include_log=include_log,
            include_identifier=include_identifier,
        )
Beispiel #6
0
    def find(self, argument, include_log=True, include_identifier=True):
        if not isinstance(argument, six.string_types):
            raise ValueError(
                "Expected 'argument' to be a string, not '{}'".format(
                    type(argument)))

        # Map any candidate default branch to the one used by this repository
        if argument in self.DEFAULT_BRANCHES:
            argument = self.default_branch

        # See if the argument the user specified is a recognized commit format
        parsed_commit = Commit.parse(argument, do_assert=False)
        if parsed_commit:
            if parsed_commit.branch in self.DEFAULT_BRANCHES:
                parsed_commit.branch = self.default_branch

            return self.commit(
                hash=parsed_commit.hash,
                revision=parsed_commit.revision,
                identifier=parsed_commit.identifier,
                branch=parsed_commit.branch,
                include_log=include_log,
                include_identifier=include_identifier,
            )

        # The argument isn't a recognized commit format, hopefully it is a valid git ref of some form
        output = run(
            [self.executable(), 'rev-parse', argument],
            cwd=self.root_path,
            capture_output=True,
            encoding='utf-8',
        )
        if output.returncode:
            raise ValueError(
                "'{}' is not an argument recognized by git".format(argument))
        return self.commit(hash=output.stdout.rstrip(),
                           include_log=include_log,
                           include_identifier=include_identifier)