How to use the pygit2.init_repository function in pygit2

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
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)
github Pagure / pagure / tests / test_pagure_flask_api_ui_private_repo.py View on Github external
user_id=1,  # pingou
            name="test4",
            description="test project description",
            hook_token="aaabbbeeeceee",
            private=True,
        )
        self.session.add(item)
        self.session.commit()

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

        user = tests.FakeUser(username="pingou")
        with tests.user_set(self.app.application, user):
            tests.create_projects(self.session)
            tests.create_projects_git(pagure.config.config.get("GIT_FOLDER"))

            output = self.app.get("/test/settings")

            # Check for a public repo
            self.assertEqual(output.status_code, 200)
            self.assertNotIn(
                '
github Pagure / pagure / tests / test_pagure_flask_ui_fork.py View on Github external
def set_up_git_repo(
            self, new_project=None, branch_from='feature', mtype='FF',
            prid=1, name_from='test'):
        """ Set up the git repo and create the corresponding PullRequest
        object.
        """

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

        newpath = tempfile.mkdtemp(prefix='pagure-fork-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()

        try:
            com = repo.revparse_single('HEAD')
            prev_commit = [com.oid.hex]
        except:
            prev_commit = []
github Pagure / pagure / tests / test_pagure_lib_git.py View on Github external
def test_commit_to_patch_two_commits_diff_separated(self):
        """ Test the commit_to_patch function of pagure.lib.git. """
        repo = pygit2.init_repository(self.gitrepo)

        patches = pagure.lib.git.commit_to_patch(
            repo,
            [self.first_commit, self.second_commit],
            diff_view=True,
            separated=True,
        )
        exp = [
            r"""diff --git a/sources b/sources
new file mode 100644
index 0000000..9f44358
--- /dev/null
+++ b/sources
@@ -0,0 +1,2 @@
+foo
+ bar
github libgit2 / pygit2 / test / test_repository.py View on Github external
def test_keyword_arg_true(self):
        repo = init_repository(self._temp_dir, bare=True)
        assert repo.is_bare
github Pagure / pagure / tests / test_pagure_flask_docs.py View on Github external
def _set_up_doc(self):
        # forked doc repo
        docrepo = os.path.join(self.path, "repos", "docs", "test", "test.git")
        repo = pygit2.init_repository(docrepo)

        # Create files in that git repo
        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()
github Pagure / pagure / dev-data.py View on Github external
def add_content_git_repo(folder, branch="master"):
    """ Create some content for the specified git repo. """
    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)

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

    parents = []
    commit = None
    try:
        commit = repo.revparse_single("HEAD" if branch == "master" else branch)
    except KeyError:
        pass
github Pagure / pagure / pagure / lib / git.py View on Github external
"The %s repo %s already exists" % (repotype, project.path)
                )
            else:
                return None

        if repotype == "main":
            pygit2.init_repository(repodir, bare=True, template_path=templ)

            if not project.private:
                # Make the repo exportable via apache
                http_clone_file = os.path.join(repodir, "git-daemon-export-ok")
                if not os.path.exists(http_clone_file):
                    with open(http_clone_file, "w"):
                        pass
        else:
            pygit2.init_repository(
                repodir,
                bare=True,
                mode=pygit2.C.GIT_REPOSITORY_INIT_SHARED_GROUP,
            )

        return repodir