Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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')
)
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)
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)
# 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")
# 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')
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)
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")
)
def commit(self, message, author_name, author_email, **kwargs):
if self.dirty:
raise IndexError('Index already commited')
ref = kwargs.pop('ref', 'HEAD')
commiter_name = kwargs.pop('commiter_name', author_name)
commiter_email = kwargs.pop('commiter_email', author_email)
parents = kwargs.pop(
'parents', [self.revision.id] if self.revision else [])
author = pygit2.Signature(author_name, author_email)
commiter = pygit2.Signature(commiter_name, commiter_email)
# Create tree
tree = IndexTree(self)
for path, (oid, mode) in self.stash.items():
if oid is None:
tree.remove(path)
else:
tree.add(path, oid, mode)
oid = tree.write()
self.dirty = True
branch = re.sub("refs/heads/", "", ref)
if not self.repository.is_bare and (branch == self.repository.current_head or
self.repository.current_head is None):
def write_commit(self, message: typing.Optional[str] = ""):
now = int(time.time())
parents: typing.List[git.Oid]
if self._commit:
parents = [ self._commit ]
else:
parents = []
sig = git.Signature(self._name, self._email, time=now)
self._commit = self._repo.create_commit(
"HEAD",
sig,
sig,
message,
self._tree,
parents,
)
def get_author(self, user=None):
if user is not None:
author_name = f'User {user.id}'
author_email = user.email
else:
author_name = 'Mozilla Add-ons Robot'
author_email = 'addons-dev-automation+github@mozilla.com'
return pygit2.Signature(name=author_name, email=author_email)