From a2735670dd553cd8179a7d6341b32c62a1280de0 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Feb 27 2015 08:34:00 +0000 Subject: Move the commit_to_patch method into progit.lib.git --- diff --git a/progit/lib/__init__.py b/progit/lib/__init__.py index 873e551..b39169e 100644 --- a/progit/lib/__init__.py +++ b/progit/lib/__init__.py @@ -86,58 +86,6 @@ def get_next_id(session, projectid): return nid + 1 -def commit_to_patch(repo_obj, commits): - ''' For a given commit (PyGit2 commit object) of a specified git repo, - returns a string representation of the changes the commit did in a - format that allows it to be used as patch. - ''' - if not isinstance(commits, list): - commits = [commits] - - patch = "" - for cnt, commit in enumerate(commits): - if commit.parents: - diff = commit.tree.diff_to_tree() - - parent = repo_obj.revparse_single('%s^' % commit.oid.hex) - diff = repo_obj.diff(parent, commit) - else: - # First commit in the repo - diff = commit.tree.diff_to_tree(swap=True) - - subject = message = '' - if '\n' in commit.message: - subject, message = commit.message.split('\n', 1) - else: - subject = commit.message - - if len(commits) > 1: - subject = '[PATCH %s/%s] %s' % (cnt + 1, len(commits), subject) - - patch += """From %(commit)s Mon Sep 17 00:00:00 2001 -From: %(author_name)s <%(author_email)s> -Date: %(date)s -Subject: %(subject)s - -%(msg)s ---- - -%(patch)s -""" % ( - { - 'commit': commit.oid.hex, - 'author_name': commit.author.name, - 'author_email': commit.author.email, - 'date': datetime.datetime.utcfromtimestamp( - commit.commit_time).strftime('%b %d %Y %H:%M:%S +0000'), - 'subject': subject, - 'msg': message, - 'patch': diff.patch, - } - ) - return patch - - def get_user(session, username): ''' Return the user corresponding to this username, or None. ''' user = session.query( diff --git a/progit/lib/git.py b/progit/lib/git.py new file mode 100644 index 0000000..b9d36d2 --- /dev/null +++ b/progit/lib/git.py @@ -0,0 +1,77 @@ +#-*- coding: utf-8 -*- + +""" + (c) 2015 - Copyright Red Hat Inc + + Authors: + Pierre-Yves Chibon + +""" + + +import datetime +import json +import os +import random +import shutil +import string +import tempfile +import uuid + +import pygit2 + +import progit.exceptions +import progit.notify +from progit.lib import model + + +def commit_to_patch(repo_obj, commits): + ''' For a given commit (PyGit2 commit object) of a specified git repo, + returns a string representation of the changes the commit did in a + format that allows it to be used as patch. + ''' + if not isinstance(commits, list): + commits = [commits] + + patch = "" + for cnt, commit in enumerate(commits): + if commit.parents: + diff = commit.tree.diff_to_tree() + + parent = repo_obj.revparse_single('%s^' % commit.oid.hex) + diff = repo_obj.diff(parent, commit) + else: + # First commit in the repo + diff = commit.tree.diff_to_tree(swap=True) + + subject = message = '' + if '\n' in commit.message: + subject, message = commit.message.split('\n', 1) + else: + subject = commit.message + + if len(commits) > 1: + subject = '[PATCH %s/%s] %s' % (cnt + 1, len(commits), subject) + + patch += """From %(commit)s Mon Sep 17 00:00:00 2001 +From: %(author_name)s <%(author_email)s> +Date: %(date)s +Subject: %(subject)s + +%(msg)s +--- + +%(patch)s +""" % ( + { + 'commit': commit.oid.hex, + 'author_name': commit.author.name, + 'author_email': commit.author.email, + 'date': datetime.datetime.utcfromtimestamp( + commit.commit_time).strftime('%b %d %Y %H:%M:%S +0000'), + 'subject': subject, + 'msg': message, + 'patch': diff.patch, + } + ) + return patch