Esempio n. 1
0
    def test_saving(self):
        mapping_a = Contributor.Mapping()
        mapping_a.create('Jonathan Bedard', '*****@*****.**')
        mapping_a.create('Stephanie Lewis', '*****@*****.**')
        mapping_a['JonWBedard'] = mapping_a['*****@*****.**']

        file = StringIO()
        mapping_a.save(file)
        file.seek(0)

        mapping_b = Contributor.Mapping.load(file)
        self.assertEqual(mapping_a['*****@*****.**'],
                         mapping_b['*****@*****.**'])
        self.assertEqual(mapping_a['*****@*****.**'],
                         mapping_b['JonWBedard'])
        self.assertEqual(mapping_a['*****@*****.**'],
                         mapping_b['*****@*****.**'])
Esempio n. 2
0
    def filter_branch(self,
                      range,
                      identifier_template=None,
                      environment_shell=None):
        # We can't effectively mock the bash script in the command, but we can mock the python code that
        # script calls, which is where the program logic is.
        head, start = range.split('...')
        head = self.find(head)
        start = self.find(start)

        commits_to_edit = []
        for commit in reversed(self.commits[head.branch]):
            if commit.branch == start.branch and commit.identifier <= start.identifier:
                break
            commits_to_edit.insert(0, commit)
        if head.branch != self.default_branch:
            for commit in reversed(
                    self.commits[self.default_branch][:head.branch_point]):
                if commit.identifier <= start.identifier:
                    break
                commits_to_edit.insert(0, commit)

        stdout = StringIO()
        original_env = {
            key: os.environ.get('OLDPWD')
            for key in [
                'OLDPWD',
                'GIT_COMMIT',
                'GIT_AUTHOR_NAME',
                'GIT_AUTHOR_EMAIL',
                'GIT_COMMITTER_NAME',
                'GIT_COMMITTER_EMAIL',
            ]
        }

        try:
            count = 0
            os.environ['OLDPWD'] = self.path
            for commit in commits_to_edit:
                count += 1
                os.environ['GIT_COMMIT'] = commit.hash
                os.environ['GIT_AUTHOR_NAME'] = commit.author.name
                os.environ['GIT_AUTHOR_EMAIL'] = commit.author.email
                os.environ['GIT_COMMITTER_NAME'] = commit.author.name
                os.environ['GIT_COMMITTER_EMAIL'] = commit.author.email

                stdout.write(
                    'Rewrite {hash} ({count}/{total}) (--- seconds passed, remaining --- predicted)\n'
                    .format(
                        hash=commit.hash,
                        count=count,
                        total=len(commits_to_edit),
                    ))

                if identifier_template:
                    messagefile = StringIO()
                    messagefile.write(commit.message)
                    messagefile.seek(0)
                    with OutputCapture() as captured:
                        message_main(messagefile, identifier_template)
                    lines = captured.stdout.getvalue().splitlines()
                    if lines[-1].startswith('git-svn-id: https://svn'):
                        lines.pop(-1)
                    commit.message = '\n'.join(lines)

                if not environment_shell:
                    continue
                if re.search(r'echo "Overwriting', environment_shell):
                    stdout.write('Overwriting {}\n'.format(commit.hash))

                match = re.search(r'(?P<json>\S+\.json)', environment_shell)
                if match:
                    with OutputCapture() as captured:
                        committer_main(match.group('json'))
                    captured.stdout.seek(0)
                    for line in captured.stdout.readlines():
                        line = line.rstrip()
                        os.environ[line.split(' ')[0]] = ' '.join(
                            line.split(' ')[1:])

                commit.author = Contributor(
                    name=os.environ['GIT_AUTHOR_NAME'],
                    emails=[os.environ['GIT_AUTHOR_EMAIL']])

                if re.search(r'echo "\s+', environment_shell):
                    for key in [
                            'GIT_AUTHOR_NAME', 'GIT_AUTHOR_EMAIL',
                            'GIT_COMMITTER_NAME', 'GIT_COMMITTER_EMAIL'
                    ]:
                        stdout.write('    {}={}\n'.format(
                            key, os.environ[key]))

        finally:
            for key, value in original_env.items():
                if value is not None:
                    os.environ[key] = value
                else:
                    del os.environ[key]

        return mocks.ProcessCompletion(
            returncode=0,
            stdout=stdout.getvalue(),
        )