| 75 | === An alternate approach === |
| 76 | |
| 77 | Another way of doing the same thing: Use the --filter option to the transplant extension. This requires the use of a Python script. Put the following in backport.py, and stick it in a tools directory somewhere: |
| 78 | {{{ |
| 79 | #!python |
| 80 | #!/usr/bin/python |
| 81 | import os |
| 82 | import sys |
| 83 | |
| 84 | msg = sys.argv[1] |
| 85 | patch = sys.argv[2] |
| 86 | |
| 87 | print |
| 88 | msg_file = open(msg, 'r') |
| 89 | outlines = [] |
| 90 | firstline = True |
| 91 | for raw_line in msg_file: |
| 92 | line = raw_line.strip() |
| 93 | if firstline: |
| 94 | if line.startswith('#'): |
| 95 | outlines.append(line) |
| 96 | else: |
| 97 | firstline = False |
| 98 | print 'MERGING: %s' % line |
| 99 | outlines.append('[1.2.X] %s' % line) |
| 100 | else: |
| 101 | outlines.append(line) |
| 102 | msg_file.close() |
| 103 | |
| 104 | svn_revision = os.environ.get('SVNMERGE_REVISION',None) |
| 105 | if svn_revision is None: |
| 106 | svn_revision = raw_input('SVN Revision: ') |
| 107 | else: |
| 108 | print "SVN Revision:", svn_revision |
| 109 | outlines.append('') |
| 110 | outlines.append('Backport of r%s from trunk.' % svn_revision) |
| 111 | |
| 112 | # And write the updated message file. |
| 113 | msg_file = open(msg,'w') |
| 114 | for line in outlines: |
| 115 | print >> msg_file, line |
| 116 | msg_file.close() |
| 117 | |
| 118 | # In case of a clash, write the filename of the message file |
| 119 | print "Log message written to",msg |
| 120 | }}} |
| 121 | |
| 122 | Then, use the following shell script to do the actual backport: |
| 123 | {{{ |
| 124 | #!sh |
| 125 | #!/bin/bash |
| 126 | export SVNMERGE_REVISION=`hg svn info -r $1 | egrep '^Revision' | cut -f 2 -d ' '` |
| 127 | hg transplant $1 --filter /path/to/backport.py |
| 128 | unset SVNMERGE_REVISION |
| 129 | }}} |
| 130 | |
| 131 | If you call this script 'hg_backport', then |
| 132 | |
| 133 | The command line is like: |
| 134 | {{{ |
| 135 | #!sh |
| 136 | hg_backport 13643 |
| 137 | }}} |
| 138 | Any hg revision ID can be used as the argument; since the most common usage pattern is to backport the most recent svn commit on trunk, you can use 'tip': |
| 139 | {{{ |
| 140 | #!sh |
| 141 | hg_backport tip |
| 142 | }}} |
| 143 | |
| 144 | This script needs to be manually updated after every release, because the [1.2.X] string is hardcoded. This is mostly due to my (Russell's) own laziness -- it would be trivial to update the script to pull the branch identifier from the output of `hg branch`. |
| 145 | |