#!/usr/bin/env python
"""
Generate release notes using git log starting from the given version.
"""
from __future__ import print_function
from argparse import ArgumentParser

import subprocess
from textwrap import wrap

def main():
    parser = ArgumentParser(description=__doc__.rstrip())
    parser.add_argument('version')
    options = parser.parse_args()

    print(options.version)

    cmd = """git log --pretty=format:"%%s%%n%%b%%n" --topo-order --reverse release_%s..HEAD""" % options.version

    print(cmd)

    raw = subprocess.check_output(cmd.split())

    msgs = raw.split('\n\n')

    merges = []
    ims = [0]
    for msg in msgs:
        msg = msg.replace('\n"', '')
        if 'Merge' in msg:
            merges.insert(ims[-1], msg)
            ims.append(len(merges))

        else:
            merges.append(msg)

    for ii in range(len(ims) - 1):
        merge = merges[ims[ii]:ims[ii+1]]

        msg = merge[0][1:].strip().lower()
        wmsg = '\n  '.join(wrap(msg, 77))

        print('- ' + wmsg)
        print()

        for msg in merge[1:]:
            wmsg = '\n    '.join(wrap(msg[1:], 75))
            print('  - ' + wmsg)
        print()

if __name__ == '__main__':
    main()

@@ -168,16 +168,27 @@
 Parser%0A%0A
+import sys%0A
 import s
@@ -197,16 +197,16 @@
 process%0A
-
 from tex
@@ -522,16 +522,134 @@
 t(cmd)%0A%0A
+    if sys.version_info %3E (3, 0):%0A        raw = subprocess.check_output(cmd.split(), encoding='utf-8')%0A%0A    else:%0A    
     raw 
@@ -687,16 +687,16 @@
 plit())%0A
-
 %0A    msg
@@ -1210,45 +1210,236 @@
     
-wmsg = '%5Cn    '.join(wrap(msg%5B1:%5D, 75
+submsgs = msg%5B1:%5D.split('%5Cn- ')%0A%0A            wmsg = '%5Cn    '.join(wrap(submsgs%5B0%5D.strip(), 75))%0A            print('  - ' + wmsg)%0A            for submsg in submsgs%5B1:%5D:%0A                wmsg = '%5Cn    '.join(wrap(submsg.strip(), 73
 ))%0A 
@@ -1441,39 +1441,45 @@
 3))%0A            
+    
 print('
+  
   - ' + wmsg)%0A  
@@ -1468,32 +1468,33 @@
     - ' + wmsg)%0A
+%0A
         print()%0A

fix for Python 3, improve formatting