#!/usr/bin/python
#
# git-changelog - Output a rpm changelog
#
# Copyright (C) 2009-2010  Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Author: David Cantrell <dcantrell@redhat.com>
# Author: Brian C. Lane <bcl@redhat.com>

import os
import re
import subprocess
import sys
import textwrap
import datetime
from optparse import OptionParser



class ChangeLog:
    def __init__(self, tag):
        self.tag = tag
        self.ignore = None
	self.merges = {} 

    def _getCommitDetail(self, commit, field):
        proc = subprocess.Popen(['git', 'log', '-1',
                                 "--pretty=format:%s" % field, commit],
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE).communicate()

        ret = proc[0].strip('\n').split('\n')

        if len(ret) == 1 and ret[0].find('@') != -1:
            ret = ret[0].split('@')[0]
        elif len(ret) == 1:
            ret = ret[0]
        else:
            ret = filter(lambda x: x != '', ret)

        return ret

    def _get_orabug(self, commit, is_merge = 0):
	if is_merge == 1:
		m_option = "--merges"
	else:
		m_option = "--no-merges"

    	proc = subprocess.Popen(['git', 'log', m_option, '-1',
			    "--pretty=format:%b", commit],
			    stdout=subprocess.PIPE,
			    stderr=subprocess.PIPE).communicate()
	bugz = []
	m = re.search("Orabug:.*$", proc[0], re.MULTILINE)
	if m:
		bugz.append(m.group())
	ret = ""
	if len(bugz) > 0:
		for bug in bugz:
			ret += "%s " % bug
	bugz = []
	m = re.search("Oracle bug:.*$", proc[0], re.MULTILINE)
	if m:
		bugz.append(m.group())
	if len(bugz) > 0:
		for bug in bugz:
			ret += "%s " % bug
	m = re.search("Bugdb:.*$", proc[0], re.MULTILINE)
	if m:
		bugz.append(m.group())
	if len(bugz) > 0:
		for bug in bugz:
			ret += "%s " % bug
	m = re.search("Bug-db:.*$", proc[0], re.MULTILINE)
	if m:
		bugz.append(m.group())
	#	print m.group()
	#if len(bugz) > 0:
	#	for bug in bugz:
	#		ret += "%s " % bug
	m = re.search("Oracle-bug:.*$", proc[0], re.MULTILINE)
	if m:
		bugz.append(m.group())
	if len(bugz) > 0:
		for bug in bugz:
			ret += "%s " % bug

	if len(ret) > 0:
		return "[%s]" % ret.rstrip()

	#If commit has merge commit check if there is Bug #
	if is_merge == 1:
		return ret

	for key in self.merges:
		if commit in self.merges[key]:
			return self._get_orabug(key, is_merge = 1)
	return ret

    def _get_cve(self, commit):
    	proc = subprocess.Popen(['git', 'log', '-1',
			    "--pretty=format:%b", commit],
			    stdout=subprocess.PIPE,
			    stderr=subprocess.PIPE).communicate()

	cves = [] 
	m = re.search("CVE-[0-9]+-[0-9]+", proc[0], re.MULTILINE)
	if m:
		cves.append(m.group())
	ret = ""
	if len(cves) > 0:
		for cve in cves:
			ret += "%s " % cve
		return "{%s}" %	ret.rstrip()
	else:
		return ret

    def getMerges(self):
        range = "%s.." % (self.tag)
        proc = subprocess.Popen(['git', 'log', '--merges', '--pretty=oneline', range],
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE).communicate()
        lines = filter(lambda x: x.find('l10n: ') != 41, 
                       proc[0].strip('\n').split('\n'))

        for line in lines:
            fields = line.split(' ')
            commit = fields[0]
	    #print "Processing merge %s" % commit
	    show_merges = "%s^..%s" % (commit, commit)
	    proc2 = subprocess.Popen(['git', 'log', '--no-merges', '--pretty=%H', show_merges],
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE).communicate()
	    mlist = []
	    for l in proc2[0].split('\n'):
	    	mlist.append(l)
	    self.merges[commit] = mlist

    def getLog(self):
        range = "%s.." % (self.tag)
        proc = subprocess.Popen(['git', 'log', '--no-merges', '--pretty=oneline', range],
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE).communicate()
        lines = filter(lambda x: x.find('l10n: ') != 41 and \
                                 x.find('Merge commit') != 41 and \
                                 x.find('Merge branch') != 41,
                       proc[0].strip('\n').split('\n'))

        if self.ignore and self.ignore != '':
            for commit in self.ignore.split(','):
                lines = filter(lambda x: not x.startswith(commit), lines)

        log = []
        for line in lines:
            fields = line.split(' ')
            commit = fields[0]
	    #print commit
		
            summary = self._getCommitDetail(commit, "%s")
            long = self._getCommitDetail(commit, "%b")
            author = self._getCommitDetail(commit, "%aN")
            orabug = self._get_orabug(commit)
            cve = self._get_cve(commit)

            log.append(("%s (%s) %s %s" % (summary.strip(), author, orabug, cve)))

        return log

    def get_date(self):
    	proc = subprocess.Popen(['git', 'config', '--get-all',
			    "user.name"],
			    stdout=subprocess.PIPE,
			    stderr=subprocess.PIPE).communicate()
	name = proc[0].rstrip()

	proc = subprocess.Popen(['git', 'config', '--get-all',
			    "user.email"],
			    stdout=subprocess.PIPE,
			    stderr=subprocess.PIPE).communicate()
	email = proc[0].rstrip()

	date = datetime.datetime.now().strftime("%a %b %d %Y")

	ret = "* %s %s <%s>" % (date, name, email)
	return ret

    def formatLog(self):
        s = ""
        for msg in self.getLog():
            sublines = textwrap.wrap(msg, 77)
            s = s + "- %s\n" % sublines[0]

            if len(sublines) > 1:
                for subline in sublines[1:]:
                    s = s + "  %s\n" % subline

        return s

def main():
    parser = OptionParser()
    parser.add_option("-t", "--tag", dest="tag",
                      help="Last tag, changelog is commits after this tag")
    (options, args) = parser.parse_args()

    cl = ChangeLog(options.tag)
    cl.getMerges()

    print cl.get_date()
    print cl.formatLog()

if __name__ == "__main__":
    main()

