| 46 | | hg st -a -r -m | grep '' > /dev/null && { echo "Working directory not clean - exiting" > /dev/stderr; exit 1; } |
| 47 | | |
| 48 | | SHORTBRANCH="$1" |
| 49 | | BRANCH="releases/$SHORTBRANCH" |
| 50 | | if [ "$2" = "--svnrev" ] |
| 51 | | then |
| 52 | | SVNREV="$3" |
| 53 | | HGREV=`hg log -r "svnrev($SVNREV)" --template '{rev}'` |
| 54 | | if [ "x$HGREV" = "x" ] |
| 55 | | then |
| 56 | | echo "Can't find hg rev" |
| 57 | | exit 1; |
| 58 | | fi |
| 59 | | else |
| 60 | | HGREV="$2" |
| 61 | | SVNREV=`hg svn info -r $HGREV | egrep '^Revision' | cut -f 2 -d ' '` |
| 62 | | if [ "x$SVNREV" = "x" ] |
| 63 | | then |
| 64 | | echo "Can't find svn rev"; |
| 65 | | exit 1; |
| 66 | | fi |
| 67 | | fi |
| 68 | | |
| 69 | | echo "Backporting Subversion revision $SVNREV" |
| 70 | | hg update $BRANCH || exit 1; |
| 71 | | # Make a commit message first in case the transplant fails |
| 72 | | hg log -r $HGREV --template "[$SHORTBRANCH] {desc}\n\nBackport of [$SVNREV] from trunk.\n" > hg-commit-message.txt || exit 1 |
| 73 | | hg transplant $HGREV || exit 1; |
| 74 | | # Modify the commit message |
| 75 | | # We need to get added files, since after rollback this is forgotten |
| 76 | | ADDED=$(hg log -r tip --template '{file_adds}') |
| 77 | | REMOVED=$(hg log -r tip --template '{file_dels}') |
| 78 | | hg rollback > /dev/null || exit 1 |
| 79 | | if [ "x$ADDED" != "x" ] |
| 80 | | then |
| 81 | | hg add $ADDED || exit 1 |
| 82 | | fi |
| 83 | | if [ "x$REMOVED" != "x" ] |
| 84 | | then |
| 85 | | hg remove $REMOVED || exit 1 |
| 86 | | fi |
| 87 | | hg commit -l hg-commit-message.txt || exit 1 |
| 88 | | echo "Backport committed." |
| 89 | | }}} |
| 90 | | The command line is like one of the following: |
| 91 | | {{{ |
| 92 | | #!sh |
| 93 | | hg_backport 1.2.X 13643 |
| 94 | | hg_backport 1.2.X --svnrev 13237 |
| 95 | | }}} |
| 96 | | where 1.2.X is a directory under 'releases/' in the Subversion repo, 13643 is a hg revision ID, and 13237 is a Subversion revision ID. The change is already committed to the local repo, but not 'pushed', so you can still rollback if changes need to be made. |
| 97 | | |
| 98 | | === An alternate approach === |
| 99 | | |
| 100 | | 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: |
| 101 | | {{{ |
| 102 | | #!python |
| 103 | | #!/usr/bin/python |
| 104 | | import os |
| 105 | | import sys |
| 106 | | |
| 107 | | msg = sys.argv[1] |
| 108 | | patch = sys.argv[2] |
| 109 | | |
| 110 | | print |
| 111 | | branch = os.environ.get('DJANGO_BRANCH',None) |
| 112 | | if branch is None: |
| 113 | | branch = raw_input('Backport branch: ') |
| 114 | | else: |
| 115 | | print "Backport branch:", branch |
| 116 | | |
| 117 | | msg_file = open(msg, 'r') |
| 118 | | outlines = [] |
| 119 | | firstline = True |
| 120 | | for raw_line in msg_file: |
| 121 | | line = raw_line.strip() |
| 122 | | if firstline: |
| 123 | | if line.startswith('#'): |
| 124 | | outlines.append(line) |
| 125 | | else: |
| 126 | | firstline = False |
| 127 | | print 'MERGING: %s' % line |
| 128 | | outlines.append('[%s] %s' % (branch, line)) |
| 129 | | else: |
| 130 | | outlines.append(line) |
| 131 | | msg_file.close() |
| 132 | | |
| 133 | | svn_revision = os.environ.get('SVNMERGE_REVISION',None) |
| 134 | | if svn_revision is None: |
| 135 | | svn_revision = raw_input('SVN Revision: ') |
| 136 | | else: |
| 137 | | print "SVN Revision:", svn_revision |
| 138 | | outlines.append('') |
| 139 | | outlines.append('Backport of r%s from trunk.' % svn_revision) |
| 140 | | |
| 141 | | # And write the updated message file. |
| 142 | | msg_file = open(msg,'w') |
| 143 | | for line in outlines: |
| 144 | | print >> msg_file, line |
| 145 | | msg_file.close() |
| 146 | | |
| 147 | | # In case of a clash, write the filename of the message file |
| 148 | | print "Log message written to",msg |
| | 46 | sed -nie '1 p;2 p;3 p' "$1" |
| | 47 | hg log -r $HGREVISION --template "[$CURRENT_BRANCH] {desc}\n\nBackport of [$SVN_REV] from $SOURCE_BRANCH.\n" >> "$1" |