Python pygit2.clone_repository() Examples

The following are 10 code examples of pygit2.clone_repository(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pygit2 , or try the search function .
Example #1
Source File: test_pagure_flask_ui_fork.py    From pagure with GNU General Public License v2.0 5 votes vote down vote up
def test_new_request_pull_empty_fork(self, send_email):
        """ Test the new_request_pull endpoint against an empty repo. """
        send_email.return_value = True

        self.test_fork_project()

        tests.create_projects_git(
            os.path.join(self.path, "requests"), bare=True
        )

        repo = pagure.lib.query.get_authorized_project(self.session, "test")
        fork = pagure.lib.query.get_authorized_project(
            self.session, "test", user="foo"
        )

        # Create a git repo to play with
        gitrepo = os.path.join(self.path, "repos", "test.git")
        repo = pygit2.init_repository(gitrepo, bare=True)

        # Create a fork of this repo
        newpath = tempfile.mkdtemp(prefix="pagure-fork-test")
        gitrepo = os.path.join(self.path, "repos", "forks", "foo", "test.git")
        new_repo = pygit2.clone_repository(gitrepo, newpath)

        user = tests.FakeUser()
        user.username = "foo"
        with tests.user_set(self.app.application, user):
            output = self.app.get(
                "/fork/foo/test/diff/master..master", follow_redirects=True
            )
            self.assertEqual(output.status_code, 400)
            self.assertIn(
                "<p>Fork is empty, there are no commits to create a pull "
                "request with</p>",
                output.get_data(as_text=True),
            )

        shutil.rmtree(newpath) 
Example #2
Source File: __init__.py    From pagure with GNU General Public License v2.0 5 votes vote down vote up
def _clone_and_top_commits(folder, branch, branch_ref=False):
    """ Clone the repository, checkout the specified branch and return
    the top commit of that branch if there is one.
    Returns the repo, the path to the clone and the top commit(s) in a tuple
    or the repo, the path to the clone and the reference to the branch
    object if branch_ref is True.
    """
    if not os.path.exists(folder):
        os.makedirs(folder)
    brepo = pygit2.init_repository(folder, bare=True)

    newfolder = tempfile.mkdtemp(prefix="pagure-tests")
    repo = pygit2.clone_repository(folder, newfolder)

    branch_ref_obj = None
    if "origin/%s" % branch in repo.listall_branches(pygit2.GIT_BRANCH_ALL):
        branch_ref_obj = pagure.lib.git.get_branch_ref(repo, branch)
        repo.checkout(branch_ref_obj)

    if branch_ref:
        return (repo, newfolder, branch_ref_obj)

    parents = []
    commit = None
    try:
        if branch_ref_obj:
            commit = repo[branch_ref_obj.peel().hex]
        else:
            commit = repo.revparse_single("HEAD")
    except KeyError:
        pass
    if commit:
        parents = [commit.oid.hex]

    return (repo, newfolder, parents) 
Example #3
Source File: test_pagure_flask_api_project.py    From pagure with GNU General Public License v2.0 5 votes vote down vote up
def test_api_git_branches(self):
        """ Test the api_git_branches method of the flask api. """
        # Create a git repo to add branches to
        tests.create_projects(self.session)
        repo_path = os.path.join(self.path, "repos", "test.git")
        tests.add_content_git_repo(repo_path)
        new_repo_path = tempfile.mkdtemp(prefix="pagure-api-git-branches-test")
        clone_repo = pygit2.clone_repository(repo_path, new_repo_path)

        # Create two other branches based on master
        for branch in ["pats-win-49", "pats-win-51"]:
            clone_repo.create_branch(branch, clone_repo.head.peel())
            refname = "refs/heads/{0}:refs/heads/{0}".format(branch)
            PagureRepo.push(clone_repo.remotes[0], refname)

        # Check that the branches show up on the API
        output = self.app.get("/api/0/test/git/branches")
        # Delete the cloned git repo after the API call
        shutil.rmtree(new_repo_path)

        # Verify the API data
        self.assertEqual(output.status_code, 200)
        data = json.loads(output.get_data(as_text=True))
        self.assertDictEqual(
            data,
            {
                "branches": ["master", "pats-win-49", "pats-win-51"],
                "total_branches": 3,
            },
        ) 
Example #4
Source File: test_pagure_flask_ui_repo_slash_name.py    From pagure with GNU General Public License v2.0 5 votes vote down vote up
def set_up_git_repo(self, name="test"):
        """ Set up the git repo to play with. """

        # Create a git repo to play with
        gitrepo = os.path.join(self.path, "repos", "%s.git" % name)
        repo = pygit2.init_repository(gitrepo, bare=True)

        newpath = tempfile.mkdtemp(prefix="pagure-other-test")
        repopath = os.path.join(newpath, "test")
        clone_repo = pygit2.clone_repository(gitrepo, repopath)

        # Create a file in that git repo
        with open(os.path.join(repopath, "sources"), "w") as stream:
            stream.write("foo\n bar")
        clone_repo.index.add("sources")
        clone_repo.index.write()

        # Commits the files added
        tree = clone_repo.index.write_tree()
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
        clone_repo.create_commit(
            "refs/heads/master",  # the name of the reference to update
            author,
            committer,
            "Add sources file for testing",
            # binary string representing the tree object ID
            tree,
            # list of binary strings representing parents of the new commit
            [],
        )
        refname = "refs/heads/master"
        ori_remote = clone_repo.remotes[0]
        PagureRepo.push(ori_remote, refname) 
Example #5
Source File: repo.py    From pagure with GNU General Public License v2.0 5 votes vote down vote up
def clone(path_from, path_to, checkout_branch=None, bare=False):
        """ Clone the git repo at the specified path to the specified location.

        This method is meant to replace pygit2.clone_repository which for us
        leaks file descriptors on large project leading to "Too many open files
        error" which then prevent some tasks from completing.

        :arg path_from: the path or url of the git repository to clone
        :type path_from: str
        :arg path_to: the path where the git repository should be cloned
        :type path_to: str
        :
        """
        cmd = ["git", "clone", path_from, path_to]
        if checkout_branch:
            cmd.extend(["-b", checkout_branch])
        if bare:
            cmd.append("--bare")

        run_command(cmd) 
Example #6
Source File: OCA_Downloader.py    From odoo-development with GNU Affero General Public License v3.0 5 votes vote down vote up
def clone_project(self, repo_url, repo_path, branch):
        repo = False

        try:
            repo = clone_repository(
                repo_url, repo_path, checkout_branch=branch)

            assert bool(repo), \
                'Unknown error when downloading {}'.format(repo_url)
        except Exception as e:
            error_event = ErrorEvent(e.message)
            self.onError.fire(error_event)

        return repo 
Example #7
Source File: dgroc.py    From dgroc with GNU General Public License v3.0 5 votes vote down vote up
def clone(cls, url, folder):
        '''Clone the repository'''
        pygit2.clone_repository(url, folder) 
Example #8
Source File: lambda_function.py    From quickstart-git2s3 with Apache License 2.0 5 votes vote down vote up
def create_repo(repo_path, remote_url, creds):
    if os.path.exists(repo_path):
        logger.info('Cleaning up repo path...')
        shutil.rmtree(repo_path)
    repo = clone_repository(remote_url, repo_path, callbacks=creds)

    return repo 
Example #9
Source File: test_pagure_flask_ui_fork.py    From pagure with GNU General Public License v2.0 4 votes vote down vote up
def test_new_request_pull_empty_repo(self, send_email):
        """ Test the new_request_pull endpoint against an empty repo. """
        send_email.return_value = True

        self.test_fork_project()

        tests.create_projects_git(
            os.path.join(self.path, "requests"), bare=True
        )

        repo = pagure.lib.query.get_authorized_project(self.session, "test")
        fork = pagure.lib.query.get_authorized_project(
            self.session, "test", user="foo"
        )

        # Create a git repo to play with
        gitrepo = os.path.join(self.path, "repos", "test.git")
        repo = pygit2.init_repository(gitrepo, bare=True)

        # Create a fork of this repo
        newpath = tempfile.mkdtemp(prefix="pagure-fork-test")
        gitrepo = os.path.join(self.path, "repos", "forks", "foo", "test.git")
        new_repo = pygit2.clone_repository(gitrepo, newpath)

        user = tests.FakeUser()
        user.username = "foo"
        with tests.user_set(self.app.application, user):
            output = self.app.get(
                "/fork/foo/test/diff/master..feature", follow_redirects=True
            )
            self.assertEqual(output.status_code, 400)
            self.assertIn(
                "<p>Fork is empty, there are no commits to create a pull "
                "request with</p>",
                output.get_data(as_text=True),
            )

            output = self.app.get("/test/new_issue")
            csrf_token = self.get_csrf(output=output)

            data = {"csrf_token": csrf_token, "title": "foo bar PR"}

            output = self.app.post(
                "/test/diff/master..feature", data=data, follow_redirects=True
            )
            self.assertEqual(output.status_code, 400)
            self.assertIn(
                "<p>Fork is empty, there are no commits to create a pull "
                "request with</p>",
                output.get_data(as_text=True),
            )

        shutil.rmtree(newpath) 
Example #10
Source File: test_pagure_flask_ui_no_master_branch.py    From pagure with GNU General Public License v2.0 4 votes vote down vote up
def set_up_git_repo(self):
        """ Set up the git repo to play with. """

        # Create a git repo to play with
        gitrepo = os.path.join(self.path, "repos", "test.git")
        repo = pygit2.init_repository(gitrepo, bare=True)

        newpath = tempfile.mkdtemp(prefix="pagure-other-test")
        repopath = os.path.join(newpath, "test")
        clone_repo = pygit2.clone_repository(gitrepo, repopath)

        # Create a file in that git repo
        with open(os.path.join(repopath, "sources"), "w") as stream:
            stream.write("foo\n bar")
        clone_repo.index.add("sources")
        clone_repo.index.write()

        # Commits the files added
        tree = clone_repo.index.write_tree()
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
        clone_repo.create_commit(
            "refs/heads/feature",  # the name of the reference to update
            author,
            committer,
            "Add sources file for testing",
            # binary string representing the tree object ID
            tree,
            # list of binary strings representing parents of the new commit
            [],
        )

        feature_branch = clone_repo.lookup_branch("feature")
        first_commit = feature_branch.peel().hex

        # Second commit
        with open(os.path.join(repopath, ".gitignore"), "w") as stream:
            stream.write("*~")
        clone_repo.index.add(".gitignore")
        clone_repo.index.write()

        tree = clone_repo.index.write_tree()
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
        clone_repo.create_commit(
            "refs/heads/feature",
            author,
            committer,
            "Add .gitignore file for testing",
            # binary string representing the tree object ID
            tree,
            # list of binary strings representing parents of the new commit
            [first_commit],
        )

        refname = "refs/heads/feature"
        ori_remote = clone_repo.remotes[0]
        PagureRepo.push(ori_remote, refname)

        shutil.rmtree(newpath)