Python pygit2.Repository() Examples
The following are 30
code examples of pygit2.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_archives.py From pagure with GNU General Public License v2.0 | 6 votes |
def setUp(self): """ Set up the environnment, ran before every tests. """ super(PagureFlaskUiArchivesTest, self).setUp() tests.create_projects(self.session) tests.create_projects_git(os.path.join(self.path, "repos"), bare=True) project = pagure.lib.query._get_project(self.session, "test") # test has both commits and tags repopath = os.path.join(self.path, "repos", "test.git") tests.add_readme_git_repo(repopath) repo = pygit2.Repository(repopath) add_repo_tag(self.path, repo, ["v1.0", "v1.1"], "test.git") # test2 has only commits tests.add_readme_git_repo( os.path.join(self.path, "repos", "test2.git") ) # somenamespace/test3 has neither commits nor tags # Create the archive folder: self.archive_path = os.path.join(self.path, "archives") os.mkdir(self.archive_path)
Example #2
Source File: git.py From filesystem_spec with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, path=None, ref=None, **kwargs): """ Parameters ---------- path: str (optional) Local location of the repo (uses current directory if not given) ref: str (optional) Reference to work with, could be a hash, tag or branch name. Defaults to current working tree. Note that ``ls`` and ``open`` also take hash, so this becomes the default for those operations kwargs """ super().__init__(**kwargs) self.repo = pygit2.Repository(path or os.getcwd()) self.ref = ref or "master"
Example #3
Source File: test_pagure_flask_api_project.py From pagure with GNU General Public License v2.0 | 6 votes |
def test_api_new_git_branch_from_commit(self): """ Test the api_new_branch method of the flask api """ tests.create_projects(self.session) repos_path = os.path.join(self.path, "repos") tests.create_projects_git(repos_path, bare=True) git_path = os.path.join(repos_path, "test.git") tests.add_content_git_repo(git_path) tests.create_tokens(self.session, project_id=None) tests.create_tokens_acl(self.session, "aaabbbcccddd", "create_branch") repo_obj = pygit2.Repository(git_path) from_commit = repo_obj.revparse_single("HEAD").oid.hex headers = {"Authorization": "token aaabbbcccddd"} args = {"branch": "test123", "from_commit": from_commit} output = self.app.post( "/api/0/test/git/branch", headers=headers, data=args ) self.assertEqual(output.status_code, 200) data = json.loads(output.get_data(as_text=True)) expected_output = {"message": "Project branch was created"} self.assertEqual(data, expected_output) self.assertIn("test123", repo_obj.listall_branches())
Example #4
Source File: test_pagure_flask_api_project.py From pagure with GNU General Public License v2.0 | 6 votes |
def test_api_new_git_branch_from_branch(self): """ Test the api_new_branch method of the flask api """ tests.create_projects(self.session) repo_path = os.path.join(self.path, "repos") tests.create_projects_git(repo_path, bare=True) tests.add_content_git_repo(os.path.join(repo_path, "test.git")) tests.create_tokens(self.session, project_id=None) tests.create_tokens_acl(self.session, "aaabbbcccddd", "create_branch") git_path = os.path.join(self.path, "repos", "test.git") repo_obj = pygit2.Repository(git_path) parent = pagure.lib.git.get_branch_ref(repo_obj, "master").peel() repo_obj.create_branch("dev123", parent) headers = {"Authorization": "token aaabbbcccddd"} args = {"branch": "test123", "from_branch": "dev123"} output = self.app.post( "/api/0/test/git/branch", headers=headers, data=args ) self.assertEqual(output.status_code, 200) data = json.loads(output.get_data(as_text=True)) expected_output = {"message": "Project branch was created"} self.assertEqual(data, expected_output) self.assertIn("test123", repo_obj.listall_branches())
Example #5
Source File: test_pagure_flask_api_project.py From pagure with GNU General Public License v2.0 | 6 votes |
def test_api_new_git_branch_json(self): """ Test the api_new_branch method of the flask api """ tests.create_projects(self.session) repo_path = os.path.join(self.path, "repos") tests.create_projects_git(repo_path, bare=True) tests.add_content_git_repo(os.path.join(repo_path, "test.git")) tests.create_tokens(self.session, project_id=None) tests.create_tokens_acl(self.session, "aaabbbcccddd", "create_branch") headers = { "Authorization": "token aaabbbcccddd", "Content-Type": "application/json", } args = {"branch": "test123"} output = self.app.post( "/api/0/test/git/branch", headers=headers, data=json.dumps(args) ) self.assertEqual(output.status_code, 200) data = json.loads(output.get_data(as_text=True)) expected_output = {"message": "Project branch was created"} self.assertEqual(data, expected_output) git_path = os.path.join(self.path, "repos", "test.git") repo_obj = pygit2.Repository(git_path) self.assertIn("test123", repo_obj.listall_branches())
Example #6
Source File: tasks.py From pagure with GNU General Public License v2.0 | 6 votes |
def commits_history_stats(self, session, repopath): """ Returns the evolution of the commits made against the specified git repository. """ if not os.path.exists(repopath): raise ValueError("Git repository not found.") repo_obj = pygit2.Repository(repopath) dates = collections.defaultdict(int) for commit in repo_obj.walk( repo_obj.head.peel().oid.hex, pygit2.GIT_SORT_NONE ): delta = ( datetime.datetime.utcnow() - arrow.get(commit.commit_time).naive ) if delta.days > 365: break dates[arrow.get(commit.commit_time).date().isoformat()] += 1 return [(key, dates[key]) for key in sorted(dates)]
Example #7
Source File: repo.py From git-annex-adapter with GNU General Public License v3.0 | 6 votes |
def __init__(self, repo): # Must have run git-annex init if repo.lookup_branch('git-annex') is None: fmt = 'Repository {} is not a git-annex repo.' msg = fmt.format(repo) raise NotAGitAnnexRepoError(msg) self.repo = repo self.processes = types.SimpleNamespace() self.processes.metadata = \ GitAnnexMetadataBatchJsonProcess(self.repo.workdir) self.processes.contentlocation = \ GitAnnexContentlocationBatchProcess(self.repo.workdir) self.runners = types.SimpleNamespace() self.runners.find = \ GitAnnexFindJsonRunner(self.repo.workdir)
Example #8
Source File: tasks.py From pagure with GNU General Public License v2.0 | 6 votes |
def delete_branch(self, session, name, namespace, user, branchname): """ Delete a branch from a git repo. """ project = pagure.lib.query._get_project( session, namespace=namespace, name=name, user=user ) with project.lock("WORKER"): repo_obj = pygit2.Repository(pagure.utils.get_repo_path(project)) try: branch = repo_obj.lookup_branch(branchname) branch.delete() except pygit2.GitError as err: _log.exception(err) return ret( "ui_ns.view_branches", repo=name, namespace=namespace, username=user )
Example #9
Source File: test_pagure_flask_api_project_view_file.py From pagure with GNU General Public License v2.0 | 6 votes |
def test_view_file_from_commit(self): repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git")) commit = repo.revparse_single("HEAD") output = self.app.get("/api/0/test/tree/%s" % commit.oid.hex) self.assertEqual(output.status_code, 200) data = json.loads(output.get_data(as_text=True)) self.assertDictEqual( data, { "content": [ { "content_url": "http://localhost/test/raw/" "%s/f/README.rst" % commit.oid.hex, "name": "README.rst", "path": "README.rst", "type": "file", } ], "name": None, "type": "folder", }, )
Example #10
Source File: test_pagure_admin.py From pagure with GNU General Public License v2.0 | 6 votes |
def test_set_default_branch(self): """ Test the set-default-branch funcion of pagure-admin. """ gitrepo_path = os.path.join(self.path, "repos", "test.git") tests.add_content_git_repo(gitrepo_path) tests.add_commit_git_repo(gitrepo_path, branch="dev") # Check default branch before: gitrepo = pygit2.Repository(gitrepo_path) self.assertEqual(gitrepo.head.shorthand, "master") args = munch.Munch({"project": "test", "user": None, "branch": "dev"}) with tests.capture_output() as output: pagure.cli.admin.do_set_default_branch(args) output = output.getvalue() self.assertEqual("Branch dev set as default\n", output) # Check default branch after: self.assertEqual(gitrepo.head.shorthand, "dev")
Example #11
Source File: irc.py From pagure with GNU General Public License v2.0 | 6 votes |
def install(cls, project, dbobj): """ Method called to install the hook for a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed """ repopaths = [get_repo_path(project)] repo_obj = pygit2.Repository(repopaths[0]) # noqa # Configure the hook # repo_obj.config.set_multivar() # Install the hook itself # cls.base_install(repopaths, dbobj, 'irc', 'git_irc.py')
Example #12
Source File: test_pagure_flask_ui_repo_view_blame.py From pagure with GNU General Public License v2.0 | 6 votes |
def test_view_blame_file_unborn_head_no_identifier(self): repo_obj = pygit2.Repository( os.path.join(self.path, "repos", "test.git") ) repo_obj.set_head("refs/heads/unexistent") output = self.app.get("/test/blame/sources") self.assertEqual(output.status_code, 404) output_text = output.get_data(as_text=True) self.assertIn( "<title>Page not found :'( - Pagure</title>", output_text ) self.assertIn("<h2>Page not found (404)</h2>", output_text) self.assertIn( "<p>Identifier is mandatory on unborn HEAD repos</p>", output_text )
Example #13
Source File: test_pagure_flask_ui_repo_view_blame.py From pagure with GNU General Public License v2.0 | 6 votes |
def test_view_blame_file_default_branch_non_master(self): """ Test the view_blame_file endpoint """ repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git")) reference = repo.lookup_reference("refs/heads/feature").resolve() repo.set_head(reference.name) output = self.app.get("/test/blame/sources") self.assertEqual(output.status_code, 200) output_text = output.get_data(as_text=True) self.assertIn('<table class="code_table">', output_text) self.assertTrue( '<tr><td class="cell1"><a id="1" href="#1" ' 'data-line-number="1"></a></td>' in output_text or '<tr><td class="cell1"><a data-line-number="1" ' 'href="#1" id="1"></a></td>' in output_text ) self.assertIn( '<td class="cell2"><pre><code>bar</code></pre></td>', output_text ) self.assertIn('<td class="cell_user">Aritz Author</td>', output_text) data = self.regex.findall(output_text) self.assertEqual(len(data), 3)
Example #14
Source File: test_pagure_flask_ui_repo_view_history.py From pagure with GNU General Public License v2.0 | 6 votes |
def test_view_history_file_on_tag(self): """ Test the view_history_file endpoint """ # set a tag on the head's parent commit repo_obj = pygit2.Repository( os.path.join(self.path, "repos", "test.git") ) commit = repo_obj[repo_obj.head.target] parent = commit.parents[0].oid.hex tagger = pygit2.Signature("Alice Doe", "adoe@example.com", 12347, 0) repo_obj.create_tag( "v1.0", parent, pygit2.GIT_OBJ_COMMIT, tagger, "Release v1.0" ) output = self.app.get("/test/history/sources?identifier=v1.0") self.assertEqual(output.status_code, 200) output_text = output.get_data(as_text=True) self.assertIn("<strong>initial commit</strong>", output_text) data = self.regex.findall(output_text) self.assertEqual(len(data), 1)
Example #15
Source File: test_gitdata.py From repostat with GNU General Public License v3.0 | 6 votes |
def test_incomplete_signature_does_not_crash_gitdata_classes(self): import tempfile with tempfile.TemporaryDirectory(prefix="tmprepo_") as tmp_repo_location: # change working directory os.chdir(tmp_repo_location) subprocess.run(['git', 'init'], cwd=tmp_repo_location) # create a file a single line in it filename = 'file.txt' with open(os.path.join(tmp_repo_location, filename), "w") as f: f.write("A single line of code\n") subprocess.run(['git', 'add', 'file.txt'], cwd=tmp_repo_location) # apparently it is not possible to create pygit.Signature with empty author's email (and name) # but commits with no author's email can be created via git subprocess.run(['git', 'commit', '-m "No-email author" --author "Author NoEmail <>"'], cwd=tmp_repo_location) try: # Commit without author's email does not crash data fetch git_repository = Repository(tmp_repo_location) WholeHistory(git_repository) BlameData(git_repository) except Exception as e: self.fail(str(e))
Example #16
Source File: test_pagure_flask_ui_fork.py From pagure with GNU General Public License v2.0 | 6 votes |
def test_request_pull_reference(self): """ Test if there is a reference created for a new PR. """ tests.create_projects(self.session) tests.create_projects_git( os.path.join(self.path, "requests"), bare=True ) set_up_git_repo( self.session, self.path, new_project=None, branch_from="feature" ) project = pagure.lib.query.get_authorized_project(self.session, "test") self.assertEqual(len(project.requests), 1) # View the pull-request output = self.app.get("/test/pull-request/1") self.assertEqual(output.status_code, 200) gitrepo = os.path.join(self.path, "repos", "test.git") repo = pygit2.Repository(gitrepo) self.assertEqual( list(repo.listall_references()), ["refs/heads/feature", "refs/heads/master", "refs/pull/1/head"], )
Example #17
Source File: test_pagure_flask_ui_archives.py From pagure with GNU General Public License v2.0 | 6 votes |
def test_project_w_commit_tar_gz(self): """ Test getting the archive from a commit. """ repopath = os.path.join(self.path, "repos", "test.git") repo = pygit2.Repository(repopath) commit = repo.head.target.hex with mock.patch.dict( "pagure.config.config", {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")}, ): output = self.app.get( "/test/archive/%s/test-v1.0.tar.gz" % commit, follow_redirects=True, ) self.assertEqual(output.status_code, 200) self.assertEqual(os.listdir(self.archive_path), ["test"]) self.assertEqual( os.listdir(os.path.join(self.archive_path, "test")), [commit] ) self.assertEqual( os.listdir(os.path.join(self.archive_path, "test", commit)), ["test-v1.0.tar.gz"], )
Example #18
Source File: gitutils.py From git-deps with GNU General Public License v2.0 | 5 votes |
def get_repo(cls, path='.'): try: repo_path = pygit2.discover_repository(path) except KeyError: abort("Couldn't find a repository in the current directory.") return pygit2.Repository(repo_path)
Example #19
Source File: gitdata.py From repostat with GNU General Public License v3.0 | 5 votes |
def __init__(self, repository: git.Repository, branch: str = "master"): self.repo = repository self.branch = branch self.cache = None self.mailmap = git.Mailmap.from_repository(self.repo)
Example #20
Source File: gitrepository.py From repostat with GNU General Public License v3.0 | 5 votes |
def __init__(self, path: str): """ :param path: path to a repository """ self.repo = git.Repository(path) self.branch = self.repo.head.shorthand self.whole_history_df = GitWholeHistory(self.repo).as_dataframe() self.linear_history_df = GitLinearHistory(self.repo).as_dataframe() self._head_revision = None self._tags = None self._name = None
Example #21
Source File: git.py From Gitmails with MIT License | 5 votes |
def get_authors(self, repo_path): try: if self.args.verbose: Helpers.print_success("Collecting authors in {}".format(repo_path)) authors_set = set() repo = Repository(repo_path) for commit in repo.walk(repo.head.target, GIT_SORT_TOPOLOGICAL): authors_set.add(Author(commit.author.name, commit.author.email)) return authors_set except Exception as e: Helpers.print_error("{}: Could not collect authors".format(repo_path)) return None
Example #22
Source File: gitdata.py From repostat with GNU General Public License v3.0 | 5 votes |
def __init__(self, repository: git.Repository, revision: str = None): self.repo = repository self.mailmap = git.Mailmap.from_repository(self.repo) self.revision_commit = self.repo.revparse_single(revision) if revision else self.repo.head.peel()
Example #23
Source File: test_pagure_flask_ui_app.py From pagure with GNU General Public License v2.0 | 5 votes |
def test_markdown_preview_valid_commit(self): """ Test the markdown_preview endpoint with an existing commit. """ user = tests.FakeUser() user.username = "foo" with tests.user_set(self.app.application, user): output = self.app.get("/settings/") self.assertEqual(output.status_code, 200) output_text = output.get_data(as_text=True) self.assertIn( "<title>foo's settings - Pagure</title>", output_text ) csrf_token = self.get_csrf(output=output) tests.create_projects(self.session) tests.create_projects_git(os.path.join(self.path, "repos"), bare=True) repopath = os.path.join(self.path, "repos", "test.git") tests.add_content_git_repo(repopath) repo = pygit2.Repository(repopath) first_commit = repo.revparse_single("HEAD") text = "Cf commit %s" % first_commit.oid.hex exp = ( '<div class="markdown"><p>Cf commit <a href="/test/c/{0}" title="Commit {0}">{1}' "</a></p></div>".format( first_commit.oid.hex, first_commit.oid.hex[:7] ) ) with self.app.application.app_context(): data = {"content": text, "csrf_token": csrf_token} output = self.app.post("/markdown/?repo=test", data=data) self.assertEqual(output.status_code, 200) self.assertEqual(exp, output.get_data(as_text=True))
Example #24
Source File: admin.py From pagure with GNU General Public License v2.0 | 5 votes |
def do_set_default_branch(args): """ Sets the specified git branch as default Args: args (argparse.Namespace): Parsed arguments """ _log.debug("project: %s", args.project) _log.debug("user: %s", args.user) _log.debug("branch: %s", args.branch) # Get the project project = _get_project(args.project, user=args.user) _check_project(project, project=args.project, user=args.user) repo_path = get_repo_path(project) repo_obj = pygit2.Repository(repo_path) if args.branch not in repo_obj.listall_branches(): raise pagure.exceptions.PagureException( "No %s branch found on project: %s" % (args.branch, args.project) ) pagure.lib.git.git_set_ref_head(project, args.branch) print("Branch %s set as default" % (args.branch))
Example #25
Source File: tasks.py From pagure with GNU General Public License v2.0 | 5 votes |
def sync_pull_ref(self, session, name, namespace, user, requestid): """ Synchronize a pull/ reference from the content in the forked repo, allowing local checkout of the pull-request. """ project = pagure.lib.query._get_project( session, namespace=namespace, name=name, user=user ) with project.lock("WORKER"): request = pagure.lib.query.search_pull_requests( session, project_id=project.id, requestid=requestid ) _log.debug( "Update pull refs of: %s#%s", request.project.fullname, request.id ) if request.remote: # Get the fork repopath = pagure.utils.get_remote_repo_path( request.remote_git, request.branch_from ) elif request.project_from: # Get the fork repopath = pagure.utils.get_repo_path(request.project_from) else: return _log.debug(" working on the repo in: %s", repopath) repo_obj = pygit2.Repository(repopath) pagure.lib.git.update_pull_ref(request, repo_obj)
Example #26
Source File: gitrevision.py From repostat with GNU General Public License v3.0 | 5 votes |
def __init__(self, repository: git.Repository, revision: str = 'HEAD'): self.blame_data = BlameData(repository, revision) self.files_data = FilesData(repository, revision).as_dataframe()
Example #27
Source File: test_pagure_flask_ui_old_commit.py From pagure with GNU General Public License v2.0 | 5 votes |
def test_view_commit_old_with_bogus_url(self): """ Test the view_commit_old endpoint. """ tests.create_projects(self.session) tests.create_projects_git(os.path.join(self.path, "repos"), bare=True) # Add a README to the git repo - First commit tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git")) pygit2.Repository(os.path.join(self.path, "repos", "test.git")) # View first commit output = self.app.get("/apple-touch-icon-152x152-precomposed.png") self.assertEqual(output.status_code, 404)
Example #28
Source File: fork.py From pagure with GNU General Public License v2.0 | 5 votes |
def _get_parent_request_repo_path(repo): """ Return the path of the parent git repository corresponding to the provided Repository object from the DB. """ if repo.parent: return repo.parent.repopath("requests") else: return repo.repopath("requests")
Example #29
Source File: pfmarkdown.py From pagure with GNU General Public License v2.0 | 5 votes |
def _commit_exists(user, namespace, repo, githash): """ Utility method checking if a given commit exists. """ repo_obj = pagure.lib.query.get_authorized_project( flask.g.session, project_name=repo, user=user, namespace=namespace ) if not repo_obj: return False reponame = pagure.utils.get_repo_path(repo_obj) git_repo = pygit2.Repository(reponame) return githash in git_repo
Example #30
Source File: pagure_hook.py From pagure with GNU General Public License v2.0 | 5 votes |
def post_receive(session, username, project, repotype, repodir, changes): """ Run the default post-receive hook. For args, see BaseRunner.runhook. """ if repotype != "main": print("The pagure hook only runs on the main git repo.") return for refname in changes: (oldrev, newrev) = changes[refname] # Retrieve the default branch repo_obj = pygit2.Repository(repodir) default_branch = None if not repo_obj.is_empty and not repo_obj.head_is_unborn: default_branch = repo_obj.head.shorthand # Skip all branch but the default one refname = refname.replace("refs/heads/", "") if refname != default_branch: continue if set(newrev) == set(["0"]): print( "Deleting a reference/branch, so we won't run the " "pagure hook" ) return generate_revision_change_log( session, project, username, repodir, pagure.lib.git.get_revs_between( oldrev, newrev, repodir, refname ), ) session.close()