diff --git a/progit/lib/__init__.py b/progit/lib/__init__.py index 018f392..0131749 100644 --- a/progit/lib/__init__.py +++ b/progit/lib/__init__.py @@ -883,87 +883,6 @@ def update_user_ssh(session, user, ssh_key): return message -def update_git_ticket(issue, repo, ticketfolder): - """ Update the given issue in its git. - - This method forks the provided repo, add/edit the issue whose file name - is defined by the uid field of the issue and if there are additions/ - changes commit them and push them back to the original repo. - - """ - - # Get the fork - repopath = os.path.join(ticketfolder, repo.path) - ticket_repo = pygit2.Repository(repopath) - - # Clone the repo into a temp folder - newpath = tempfile.mkdtemp() - new_repo = pygit2.clone_repository(repopath, newpath) - - file_path = os.path.join(newpath, issue.uid) - - # Get the current index - index = new_repo.index - - # Are we adding files - added = False - if not os.path.exists(file_path): - added = True - - # Write down what changed - with open(file_path, 'w') as stream: - stream.write(issue.to_json()) - - # Retrieve the list of files that changed - diff = new_repo.diff() - files = [patch.new_file_path for patch in diff] - - # Add the changes to the index - if added: - index.add(issue.uid) - for filename in files: - index.add(filename) - - # If not change, return - if not files and not added: - shutil.rmtree(newpath) - return - - # See if there is a parent to this commit - parent = None - try: - parent = new_repo.head.get_object().oid - except pygit2.GitError: - pass - - parents = [] - if parent: - parents.append(parent) - - # Author/commiter will always be this one - author = pygit2.Signature(name='progit', email='progit') - - # Actually commit - sha = new_repo.create_commit( - 'refs/heads/master', - author, - author, - 'Updated ticket %s: %s' % (issue.uid, issue.title), - new_repo.index.write_tree(), - parents) - index.write() - - # Push to origin - ori_remote = new_repo.remotes[0] - master_ref = new_repo.lookup_reference('HEAD').resolve() - refname = '%s:%s' % (master_ref.name, master_ref.name) - - ori_remote.push(refname) - - # Remove the clone - shutil.rmtree(newpath) - - def avatar_url(username, size=64, default='retro'): openid = "http://%s.id.fedoraproject.org/" % username return avatar_url_from_openid(openid, size, default) diff --git a/progit/lib/git.py b/progit/lib/git.py index f1b4b88..d2f0336 100644 --- a/progit/lib/git.py +++ b/progit/lib/git.py @@ -113,3 +113,84 @@ def write_gitolite_acls(session, configfile): with open(configfile, 'w') as stream: for row in config: stream.write(row + '\n') + + +def update_git_ticket(issue, repo, ticketfolder): + """ Update the given issue in its git. + + This method forks the provided repo, add/edit the issue whose file name + is defined by the uid field of the issue and if there are additions/ + changes commit them and push them back to the original repo. + + """ + + # Get the fork + repopath = os.path.join(ticketfolder, repo.path) + ticket_repo = pygit2.Repository(repopath) + + # Clone the repo into a temp folder + newpath = tempfile.mkdtemp() + new_repo = pygit2.clone_repository(repopath, newpath) + + file_path = os.path.join(newpath, issue.uid) + + # Get the current index + index = new_repo.index + + # Are we adding files + added = False + if not os.path.exists(file_path): + added = True + + # Write down what changed + with open(file_path, 'w') as stream: + stream.write(issue.to_json()) + + # Retrieve the list of files that changed + diff = new_repo.diff() + files = [patch.new_file_path for patch in diff] + + # Add the changes to the index + if added: + index.add(issue.uid) + for filename in files: + index.add(filename) + + # If not change, return + if not files and not added: + shutil.rmtree(newpath) + return + + # See if there is a parent to this commit + parent = None + try: + parent = new_repo.head.get_object().oid + except pygit2.GitError: + pass + + parents = [] + if parent: + parents.append(parent) + + # Author/commiter will always be this one + author = pygit2.Signature(name='progit', email='progit') + + # Actually commit + sha = new_repo.create_commit( + 'refs/heads/master', + author, + author, + 'Updated ticket %s: %s' % (issue.uid, issue.title), + new_repo.index.write_tree(), + parents) + index.write() + + # Push to origin + ori_remote = new_repo.remotes[0] + master_ref = new_repo.lookup_reference('HEAD').resolve() + refname = '%s:%s' % (master_ref.name, master_ref.name) + + ori_remote.push(refname) + + # Remove the clone + shutil.rmtree(newpath)