From 2b86d4d53e3d2d1ae364b543bf4edfc22b9f89f6 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jun 18 2015 08:41:38 +0000 Subject: [PATCH 1/6] Adjust the update_file_in_git method to allow committing to a new branch The idea being that you can edit a file to an existing or a new branch. Committing to a new branch is a desired feature to allow making the contribution workflow for new-comers easier. --- diff --git a/pagure/lib/git.py b/pagure/lib/git.py index 7dd9a1d..ec73818 100644 --- a/pagure/lib/git.py +++ b/pagure/lib/git.py @@ -656,7 +656,8 @@ def add_file_to_git(repo, issue, ticketfolder, user, filename, filestream): return os.path.join('files', filename) -def update_file_in_git(repo, branch, filename, content, message, user, email): +def update_file_in_git( + repo, branch, branchto, filename, content, message, user, email): ''' Update a specific file in the specified repository with the content given and commit the change under the user's name. @@ -673,7 +674,8 @@ def update_file_in_git(repo, branch, filename, content, message, user, email): # Clone the repo into a temp folder newpath = tempfile.mkdtemp(prefix='pagure-') - new_repo = pygit2.clone_repository(repopath, newpath) + new_repo = pygit2.clone_repository( + repopath, newpath, checkout_branch=branch) file_path = os.path.join(newpath, filename) @@ -703,6 +705,11 @@ def update_file_in_git(repo, branch, filename, content, message, user, email): branch_ref = get_branch_ref(new_repo, branch) parent = branch_ref.get_object() + # See if we need to create the branch + nbranch_ref = None + if branchto not in new_repo.listall_branches(): + nbranch_ref = new_repo.create_branch(branchto, parent) + parents = [] if parent: parents.append(parent.hex) @@ -715,7 +722,7 @@ def update_file_in_git(repo, branch, filename, content, message, user, email): # Actually commit new_repo.create_commit( - branch_ref.name, + nbranch_ref.name if nbranch_ref else branch_ref.name, author, author, message.strip(), @@ -725,9 +732,16 @@ def update_file_in_git(repo, branch, filename, content, message, user, email): # Push to origin ori_remote = new_repo.remotes[0] - refname = '%s:refs/heads/%s' % (branch_ref.name, branch) + refname = '%s:refs/heads/%s' % ( + nbranch_ref.name if nbranch_ref else branch_ref.name, + branchto) - ori_remote.push(refname) + try: + ori_remote.push(refname) + except pygit2.GitError as err: # pragma: no cover + shutil.rmtree(newpath) + raise pagure.exceptions.PagureException( + 'Commit could not be done: %s' % err) # Remove the clone shutil.rmtree(newpath) From 6862d5ab71bfaffa43052c49ad5b65abbd0195a5 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jun 18 2015 08:44:15 +0000 Subject: [PATCH 2/6] Add a branch entry in the EditFileForm --- diff --git a/pagure/forms.py b/pagure/forms.py index def581f..fab1c1f 100644 --- a/pagure/forms.py +++ b/pagure/forms.py @@ -310,6 +310,8 @@ class EditFileForm(wtf.Form): 'Email', [wtforms.validators.Required()], choices=[(item, item) for item in []] ) + branch = wtforms.TextField( + 'Branch', [wtforms.validators.Required()]) def __init__(self, *args, **kwargs): """ Calls the default constructor with the normal argument but From d3f55d4c557d0a2f2378c94342fcd42a3bd34892 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jun 18 2015 08:44:44 +0000 Subject: [PATCH 3/6] Adjust the edit_file endpoint to support editing to a new branch --- diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index b5ea443..01a35be 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -1250,6 +1250,8 @@ def edit_file(repo, branchname, filename, username=None): if repo_obj.is_empty: flask.abort(404, 'Empty repo cannot have a file') + form = pagure.forms.EditFileForm(emails=user.emails) + branch = None if branchname in repo_obj.listall_branches(): branch = repo_obj.lookup_branch(branchname) @@ -1257,12 +1259,12 @@ def edit_file(repo, branchname, filename, username=None): else: flask.abort(400, 'Invalid branch specified') - form = pagure.forms.EditFileForm(emails=user.emails) if form.validate_on_submit(): try: pagure.lib.git.update_file_in_git( repo, branch=branchname, + branchto=form.branch.data, filename=filename, content=form.content.data, message='%s\n\n%s' % ( @@ -1303,4 +1305,5 @@ def edit_file(repo, branchname, filename, username=None): filename=filename, form=form, user=user, + branches=repo_obj.listall_branches(), ) From b76d2d57d295e334cb1dd6c7a7fb03afb665431a Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jun 18 2015 08:45:43 +0000 Subject: [PATCH 4/6] Adjust the edit_file template to support editing to a new branch --- diff --git a/pagure/templates/edit_file.html b/pagure/templates/edit_file.html index 75a3eb8..ef51d29 100644 --- a/pagure/templates/edit_file.html +++ b/pagure/templates/edit_file.html @@ -41,21 +41,49 @@
- {{ user.username | avatar(24) | safe }} - {{ user.fullname }} - + + + + + + + + + + + + +
+ {{ user.username | avatar(24) | safe }} + + {{ user.fullname }} + + +
+ Branch: + + + + Existing branch
+ New branch +
- + value="{{ form.commit_title.data if form.commit_title.data }}" />