How to use pygit2 - 10 common examples

To help you get started, we’ve selected a few pygit2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Pagure / pagure / tests / test_pagure_flask_ui_fork.py View on Github external
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)

        # Edit the sources file again
        with open(os.path.join(newpath, 'sources'), 'w') as stream:
            stream.write('foo\n bar\nbaz\n boose')
        new_repo.index.add('sources')
        new_repo.index.write()

        # Commits the files added
        tree = new_repo.index.write_tree()
        author = pygit2.Signature(
            'Alice Author', 'alice@authors.tld')
        committer = pygit2.Signature(
            'Cecil Committer', 'cecil@committers.tld')
        new_repo.create_commit(
            'refs/heads/feature',
            author,
            committer,
            'A commit on branch feature',
            tree,
            []
        )
        refname = 'refs/heads/feature:refs/heads/feature'
        ori_remote = new_repo.remotes[0]
        PagureRepo.push(ori_remote, refname)

        # Create a PR for these "changes" (there are none, both repos are
        # empty)
        project = pagure.get_authorized_project(self.session, 'test')
github Pagure / pagure / tests / test_pagure_flask_api_ui_private_repo.py View on Github external
)
        refname = "refs/heads/master:refs/heads/master"
        ori_remote = clone_repo.remotes[0]
        PagureRepo.push(ori_remote, refname)

        first_commit = repo.revparse_single("HEAD")

        if mtype == "merge":
            with open(os.path.join(repopath, ".gitignore"), "w") as stream:
                stream.write("*~")
            clone_repo.index.add(".gitignore")
            clone_repo.index.write()

            # Commits the files added
            tree = clone_repo.index.write_tree()
            author = pygit2.Signature("Alice Äuthòr", "alice@äuthòrs.tld")
            committer = pygit2.Signature(
                "Cecil Cõmmîttër", "cecil@cõmmîttërs.tld"
            )
            clone_repo.create_commit(
                "refs/heads/master",
                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.oid.hex],
            )
            refname = "refs/heads/master:refs/heads/master"
            ori_remote = clone_repo.remotes[0]
            PagureRepo.push(ori_remote, refname)
github Pagure / pagure / tests / test_pagure_flask_ui_repo_slash_name.py View on Github external
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)
github Pagure / pagure / tests / test_pagure_lib_git.py View on Github external
# Create a git repo to play with
        self.gitrepo = os.path.join(self.path, "repos", "test_repo.git")
        os.makedirs(self.gitrepo)
        repo = pygit2.init_repository(self.gitrepo)

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

        # Commits the files added
        tree = repo.index.write_tree()
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
        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
            [],
        )

        self.first_commit = repo.revparse_single("HEAD")

        # Edit the sources file again
        with open(os.path.join(self.gitrepo, "sources"), "w") as stream:
            stream.write("foo\n bar\nbaz\n boose")
github Pagure / pagure / tests / test_pagure_flask_ui_fork.py View on Github external
# list of binary strings representing parents of the new commit
            [last_commit.oid.hex]
        )

        # Second commit
        with open(os.path.join(repopath, 'testfile'), 'a') as stream:
            stream.write('\nfoo2\n bar2')
        clone_repo.index.add('testfile')
        clone_repo.index.write()

        # Commits the files added
        last_commit = clone_repo.revparse_single('HEAD')
        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 a second commit to testfile for testing',
            # binary string representing the tree object ID
            tree,
            # list of binary strings representing parents of the new commit
            [last_commit.oid.hex]
        )

        # Third commit
        with open(os.path.join(repopath, 'testfile'), 'a') as stream:
            stream.write('\nfoo3\n bar3')
        clone_repo.index.add('testfile')
github Pagure / pagure / tests / test_pagure_flask_ui_slash_branch_name.py View on Github external
refname = "refs/heads/master"
        ori_remote = clone_repo.remotes[0]
        PagureRepo.push(ori_remote, refname)

        master_branch = clone_repo.lookup_branch("master")
        first_commit = master_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/maxamilion/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/maxamilion/feature"
        ori_remote = clone_repo.remotes[0]
        PagureRepo.push(ori_remote, refname)

        shutil.rmtree(newpath)
github Pagure / pagure / tests / test_pagure_flask_docs.py View on Github external
with open(os.path.join(docrepo, "sources"), "w") as stream:
            stream.write("foo\n bar")
        repo.index.add("sources")
        repo.index.write()

        folderpart = os.path.join(docrepo, "folder1", "folder2")
        os.makedirs(folderpart)
        with open(os.path.join(folderpart, "test_file"), "w") as stream:
            stream.write("row1\nrow2\nrow3")
        repo.index.add(os.path.join("folder1", "folder2", "test_file"))
        repo.index.write()

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

        # Push the changes to the bare repo
        remote = repo.create_remote(
            "origin", os.path.join(self.path, "repos", "docs", "test.git")
        )
github Pagure / pagure / tests / test_pagure_flask_ui_fork.py View on Github external
is_fork=True,
            parent_id=1,
        )
        self.session.add(item)
        self.session.commit()

        tests.create_projects_git(
            os.path.join(self.path, 'requests'), bare=True)
        tests.create_projects_git(
            os.path.join(self.path, 'repos', 'forks', 'foo'), bare=True)

        # Create a git repo to play with
        gitrepo = os.path.join(self.path, 'repos', 'test.git')
        self.assertFalse(os.path.exists(gitrepo))
        os.makedirs(gitrepo)
        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)

        # Edit the sources file again
        with open(os.path.join(newpath, 'sources'), 'w') as stream:
            stream.write('foo\n bar\nbaz\n boose')
        new_repo.index.add('sources')
        new_repo.index.write()

        # Commits the files added
        tree = new_repo.index.write_tree()
        author = pygit2.Signature(
github Pagure / pagure / tests / test_pagure_flask_ui_slash_branch_name.py View on Github external
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(
github Pagure / pagure / tests / test_pagure_flask_api_ui_private_repo.py View on Github external
# Add private repo
        item = pagure.lib.model.Project(
            user_id=1,  # pingou
            name="test4",
            description="test project description",
            hook_token="aaabbbeeeceee",
            private=False,
        )
        self.session.add(item)
        self.session.commit()

        # Add a git repo
        repo_path = os.path.join(
            pagure.config.config.get("GIT_FOLDER"), "test4.git"
        )
        pygit2.init_repository(repo_path)

        user = tests.FakeUser(username="pingou")
        with tests.user_set(self.app.application, user):

            # Check for public repo
            output = self.app.get("/test4/settings")
            self.assertEqual(output.status_code, 200)
            self.assertNotIn(
                '<input checked="" name="private" value="private" type="checkbox">',
                output.get_data(as_text=True),
            )

            self.session.commit()
            repo = pagure.lib.query._get_project(self.session, "test4")
            self.assertFalse(repo.private)