Пример #1
0
 def generate(args=('update', ), cwd=None, **streams):
     """Generate an output."""
     with capsys.disabled(), Isolation(cwd=cwd, **streams):
         try:
             cli.main(
                 args=args,
                 prog_name=runner.get_default_prog_name(cli),
             )
         except SystemExit as e:
             return 0 if e.code is None else e.code
         except Exception:
             raise
Пример #2
0
    def worktree(
            self,
            path=None,
            branch_name=None,
            commit=None,
            merge_args=('--ff-only', ),
    ):
        """Create new worktree."""
        from git import GitCommandError, NULL_TREE
        from renku.core.utils.contexts import Isolation

        delete = path is None
        path = path or tempfile.mkdtemp()
        branch_name = branch_name or 'renku/run/isolation/' + uuid.uuid4().hex

        # TODO sys.argv

        new_branch = False

        if commit is NULL_TREE:
            args = ['add', '--detach', path]
            self.repo.git.worktree(*args)
            client = attr.evolve(self, path=path)
            client.repo.git.checkout('--orphan', branch_name)
            client.repo.git.rm('-rf', '*')
        else:
            args = ['add', '-b', branch_name, path]
            if commit:
                args.append(commit)
            self.repo.git.worktree(*args)
            client = attr.evolve(self, path=path)
            new_branch = True

        client.repo.config_reader = self.repo.config_reader

        # Keep current directory relative to repository root.
        relative = Path('.').resolve().relative_to(self.path)

        # Reroute standard streams
        original_mapped_std = _mapped_std_streams(self.candidate_paths)
        mapped_std = {}
        for name, stream in original_mapped_std.items():
            stream_path = Path(path) / (Path(stream).relative_to(self.path))
            stream_path = stream_path.absolute()

            if not stream_path.exists():
                stream_path.parent.mkdir(parents=True, exist_ok=True)
                stream_path.touch()

            mapped_std[name] = stream_path

        _clean_streams(self.repo, original_mapped_std)

        new_cwd = Path(path) / relative
        new_cwd.mkdir(parents=True, exist_ok=True)

        with Isolation(cwd=str(new_cwd), **mapped_std):
            yield client

        try:
            self.repo.git.merge(branch_name, *merge_args)
        except GitCommandError:
            raise errors.FailedMerge(self.repo, branch_name, merge_args)

        if delete:
            self.repo.git.worktree('remove', path)

            if new_branch:
                # delete the created temporary branch
                self.repo.git.branch('-d', branch_name)

        self.checkout_paths_from_storage()