| |
@@ -59,6 +59,7 @@
|
| |
from pagure.api.ci import jenkins
|
| |
import pagure.flask_app
|
| |
import pagure.lib
|
| |
+ import pagure.lib.git
|
| |
import pagure.lib.model
|
| |
import pagure.lib.tasks_mirror
|
| |
import pagure.perfrepo as perfrepo
|
| |
@@ -695,8 +696,13 @@
|
| |
session.commit()
|
| |
|
| |
|
| |
- def add_content_git_repo(folder, branch='master'):
|
| |
- """ Create some content for the specified git repo. """
|
| |
+ 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)
|
| |
@@ -704,29 +710,52 @@
|
| |
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()
|
| |
+ 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:
|
| |
- commit = repo.revparse_single(
|
| |
- 'HEAD' if branch == 'master' else branch)
|
| |
+ if branch_ref_obj:
|
| |
+ commit = repo[branch_ref_obj.get_object().hex]
|
| |
+ else:
|
| |
+ commit = repo.revparse_single('HEAD')
|
| |
except KeyError:
|
| |
pass
|
| |
if commit:
|
| |
parents = [commit.oid.hex]
|
| |
|
| |
+ return (repo, newfolder, parents)
|
| |
+
|
| |
+
|
| |
+ def add_content_git_repo(folder, branch='master', append=None):
|
| |
+ """ Create some content for the specified git repo. """
|
| |
+ repo, newfolder, parents = _clone_and_top_commits(folder, branch)
|
| |
+
|
| |
+ # Create a file in that git repo
|
| |
+ filename = os.path.join(newfolder, 'sources')
|
| |
+ content = 'foo\n bar'
|
| |
+ if os.path.exists(filename):
|
| |
+ content = 'foo\n bar\nbaz'
|
| |
+ if append:
|
| |
+ content += append
|
| |
+ with open(filename, 'w') as stream:
|
| |
+ stream.write(content)
|
| |
+ 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(
|
| |
+ commit = repo.create_commit(
|
| |
'refs/heads/%s' % branch, # the name of the reference to update
|
| |
author,
|
| |
committer,
|
| |
@@ -737,15 +766,8 @@
|
| |
parents,
|
| |
)
|
| |
|
| |
- parents = []
|
| |
- commit = None
|
| |
- try:
|
| |
- commit = repo.revparse_single(
|
| |
- 'HEAD' if branch == 'master' else branch)
|
| |
- except KeyError:
|
| |
- pass
|
| |
if commit:
|
| |
- parents = [commit.oid.hex]
|
| |
+ parents = [commit.hex]
|
| |
|
| |
subfolder = os.path.join('folder1', 'folder2')
|
| |
if not os.path.exists(os.path.join(newfolder, subfolder)):
|
| |
@@ -765,7 +787,7 @@
|
| |
'Alice Author', 'alice@authors.tld')
|
| |
committer = pygit2.Signature(
|
| |
'Cecil Committer', 'cecil@committers.tld')
|
| |
- repo.create_commit(
|
| |
+ commit =repo.create_commit(
|
| |
'refs/heads/%s' % branch, # the name of the reference to update
|
| |
author,
|
| |
committer,
|
| |
@@ -787,14 +809,9 @@
|
| |
shutil.rmtree(newfolder)
|
| |
|
| |
|
| |
- def add_readme_git_repo(folder, readme_name='README.rst'):
|
| |
+ def add_readme_git_repo(folder, readme_name='README.rst', branch='master'):
|
| |
""" Create a README file 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)
|
| |
+ repo, newfolder, parents = _clone_and_top_commits(folder, branch)
|
| |
|
| |
if readme_name == 'README.rst':
|
| |
content = """Pagure
|
| |
@@ -822,15 +839,6 @@
|
| |
that should never get displayed on the website if there is a README.rst in the repo.
|
| |
"""
|
| |
|
| |
- parents = []
|
| |
- commit = None
|
| |
- try:
|
| |
- commit = repo.revparse_single('HEAD')
|
| |
- except KeyError:
|
| |
- pass
|
| |
- if commit:
|
| |
- parents = [commit.oid.hex]
|
| |
-
|
| |
# Create a file in that git repo
|
| |
with open(os.path.join(newfolder, readme_name), 'w') as stream:
|
| |
stream.write(content)
|
| |
@@ -843,8 +851,9 @@
|
| |
'Alice Author', 'alice@authors.tld')
|
| |
committer = pygit2.Signature(
|
| |
'Cecil Committer', 'cecil@committers.tld')
|
| |
+ branch_ref = "refs/heads/%s" % branch
|
| |
repo.create_commit(
|
| |
- 'refs/heads/master', # the name of the reference to update
|
| |
+ branch_ref, # the name of the reference to update
|
| |
author,
|
| |
committer,
|
| |
'Add a README file',
|
| |
@@ -856,10 +865,8 @@
|
| |
|
| |
# Push to origin
|
| |
ori_remote = repo.remotes[0]
|
| |
- master_ref = repo.lookup_reference('HEAD').resolve()
|
| |
- refname = '%s:%s' % (master_ref.name, master_ref.name)
|
| |
|
| |
- PagureRepo.push(ori_remote, refname)
|
| |
+ PagureRepo.push(ori_remote, '%s:%s' % (branch_ref, branch_ref))
|
| |
|
| |
shutil.rmtree(newfolder)
|
| |
|
| |
@@ -867,12 +874,8 @@
|
| |
def add_commit_git_repo(folder, ncommits=10, filename='sources',
|
| |
branch='master'):
|
| |
""" Create some more commits for the specified git repo. """
|
| |
- if not os.path.exists(folder):
|
| |
- os.makedirs(folder)
|
| |
- pygit2.init_repository(folder, bare=True)
|
| |
-
|
| |
- newfolder = tempfile.mkdtemp(prefix='pagure-tests')
|
| |
- repo = pygit2.clone_repository(folder, newfolder)
|
| |
+ repo, newfolder, branch_ref_obj = _clone_and_top_commits(
|
| |
+ folder, branch, branch_ref=True)
|
| |
|
| |
for index in range(ncommits):
|
| |
# Create a file in that git repo
|
| |
@@ -884,8 +887,11 @@
|
| |
parents = []
|
| |
commit = None
|
| |
try:
|
| |
- commit = repo.revparse_single('HEAD')
|
| |
- except KeyError:
|
| |
+ if branch_ref_obj:
|
| |
+ commit = repo[branch_ref_obj.get_object().hex]
|
| |
+ else:
|
| |
+ commit = repo.revparse_single('HEAD')
|
| |
+ except (KeyError, AttributeError):
|
| |
pass
|
| |
if commit:
|
| |
parents = [commit.oid.hex]
|
| |
@@ -896,8 +902,9 @@
|
| |
'Alice Author', 'alice@authors.tld')
|
| |
committer = pygit2.Signature(
|
| |
'Cecil Committer', 'cecil@committers.tld')
|
| |
+ branch_ref = "refs/heads/%s" % branch
|
| |
repo.create_commit(
|
| |
- 'refs/heads/master',
|
| |
+ branch_ref,
|
| |
author,
|
| |
committer,
|
| |
'Add row %s to %s file' % (index, filename),
|
| |
@@ -906,22 +913,18 @@
|
| |
# list of binary strings representing parents of the new commit
|
| |
parents,
|
| |
)
|
| |
+ branch_ref_obj = pagure.lib.git.get_branch_ref(repo, branch)
|
| |
|
| |
# Push to origin
|
| |
ori_remote = repo.remotes[0]
|
| |
- PagureRepo.push(ori_remote, 'HEAD:refs/heads/%s' % branch)
|
| |
+ PagureRepo.push(ori_remote, '%s:%s' % (branch_ref, branch_ref))
|
| |
|
| |
shutil.rmtree(newfolder)
|
| |
|
| |
|
| |
def add_content_to_git(folder, filename='sources', content='foo'):
|
| |
""" Create some more commits 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)
|
| |
+ repo, newfolder, parents = _clone_and_top_commits(folder, 'master')
|
| |
|
| |
# Create a file in that git repo
|
| |
with open(os.path.join(newfolder, filename), 'a', encoding="utf-8") as stream:
|
| |
@@ -929,15 +932,6 @@
|
| |
repo.index.add(filename)
|
| |
repo.index.write()
|
| |
|
| |
- parents = []
|
| |
- commit = None
|
| |
- try:
|
| |
- commit = repo.revparse_single('HEAD')
|
| |
- except KeyError:
|
| |
- pass
|
| |
- if commit:
|
| |
- parents = [commit.oid.hex]
|
| |
-
|
| |
# Commits the files added
|
| |
tree = repo.index.write_tree()
|
| |
author = pygit2.Signature(
|
| |
@@ -967,12 +961,7 @@
|
| |
|
| |
def add_binary_git_repo(folder, filename):
|
| |
""" Create a fake image file 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)
|
| |
+ repo, newfolder, parents = _clone_and_top_commits(folder, 'master')
|
| |
|
| |
content = b"""\x00\x00\x01\x00\x01\x00\x18\x18\x00\x00\x01\x00 \x00\x88
|
| |
\t\x00\x00\x16\x00\x00\x00(\x00\x00\x00\x18\x00x00\x00\x01\x00 \x00\x00\x00
|
| |
@@ -981,15 +970,6 @@
|
| |
\xa4fF\x04\xa2dE\x95\xa2cD8\xa1a
|
| |
"""
|
| |
|
| |
- parents = []
|
| |
- commit = None
|
| |
- try:
|
| |
- commit = repo.revparse_single('HEAD')
|
| |
- except KeyError:
|
| |
- pass
|
| |
- if commit:
|
| |
- parents = [commit.oid.hex]
|
| |
-
|
| |
# Create a file in that git repo
|
| |
with open(os.path.join(newfolder, filename), 'wb') as stream:
|
| |
stream.write(content)
|
| |
@@ -1023,6 +1003,40 @@
|
| |
shutil.rmtree(newfolder)
|
| |
|
| |
|
| |
+ def remove_file_git_repo(folder, filename, branch='master'):
|
| |
+ """ Delete the specified file on the give git repo and branch. """
|
| |
+ repo, newfolder, parents = _clone_and_top_commits(folder, branch)
|
| |
+
|
| |
+ # Remove file
|
| |
+ repo.index.remove(filename)
|
| |
+
|
| |
+ # Write the change and commit it
|
| |
+ tree = repo.index.write_tree()
|
| |
+
|
| |
+ author = pygit2.Signature(
|
| |
+ 'Alice Author', 'alice@authors.tld')
|
| |
+ committer = pygit2.Signature(
|
| |
+ 'Cecil Committer', 'cecil@committers.tld')
|
| |
+ branch_ref = "refs/heads/%s" % branch
|
| |
+ repo.create_commit(
|
| |
+ branch_ref, # the name of the reference to update
|
| |
+ author,
|
| |
+ committer,
|
| |
+ 'Remove file %s' % filename,
|
| |
+ # binary string representing the tree object ID
|
| |
+ tree,
|
| |
+ # list of binary strings representing parents of the new commit
|
| |
+ parents
|
| |
+ )
|
| |
+
|
| |
+ # Push to origin
|
| |
+ ori_remote = repo.remotes[0]
|
| |
+
|
| |
+ PagureRepo.push(ori_remote, '%s:%s' % (branch_ref, branch_ref))
|
| |
+
|
| |
+ shutil.rmtree(newfolder)
|
| |
+
|
| |
+
|
| |
@contextmanager
|
| |
def capture_output(merge_stderr=True):
|
| |
oldout, olderr = sys.stdout, sys.stderr
|
| |
This new API endpoint allows to retrieve the list of files changed as
well as the number of lines added/removed and whether the file was added
removed, modified or renamed (cf the status field).
Fixes https://pagure.io/pagure/issue/3686
Signed-off-by: Pierre-Yves Chibon pingou@pingoured.fr