1#!/usr/bin/env python
2#
3#
4# Licensed to the Apache Software Foundation (ASF) under one
5# or more contributor license agreements.  See the NOTICE file
6# distributed with this work for additional information
7# regarding copyright ownership.  The ASF licenses this file
8# to you under the Apache License, Version 2.0 (the
9# "License"); you may not use this file except in compliance
10# with the License.  You may obtain a copy of the License at
11#
12#   http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing,
15# software distributed under the License is distributed on an
16# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17# KIND, either express or implied.  See the License for the
18# specific language governing permissions and limitations
19# under the License.
20#
21#
22
23import sys
24import os
25
26progname = os.path.basename(sys.argv[0])
27
28def usage():
29  print("Usage: %s SOURCEURL WCPATH [r]REVNUM[,] [...]" % progname)
30  print("Try '%s --help' for more information" % progname)
31
32def help():
33  val = """This script is meant to ease the pain of merging and
34reviewing revision(s) on a release branch (although it can be used to
35merge and review revisions from any line of development to another).
36
37To allow cutting and pasting from the STATUS file, revision numbers
38can be space or comma-separated, and may also include the prefix
39'r'.
40
41Lastly, a file (named 'rev1-rev2-rev3.log') is created for you.
42This file contains each merge command that was run, the log of the
43revision that was merged, and the diff from the previous revision.
44
45Examples:
46
47  %s http://svn.apache.org/repos/asf/subversion/trunk svn-1.2.x-branch \
48    r14041, r14149, r14186, r14194, r14238, r14273
49
50  %s http://svn.apache.org/repos/asf/subversion/trunk svn-1.2.x-branch \
51    14041 14149 14186 14194 14238 14273""" % (progname, progname)
52  print(val)
53
54
55if len(sys.argv) > 1 and sys.argv[1] == '--help':
56  help()
57  sys.exit(0)
58
59if len(sys.argv) < 4:
60  usage()
61  sys.exit(255)
62
63src_url = sys.argv[1]
64wc_path = sys.argv[2]
65
66# Tolerate comma separated lists of revs (e.g. "r234, r245, r251")
67revs = []
68for rev in sys.argv[3:]:
69  orig_rev = rev
70  if rev[-1:] == ',':
71    rev = rev[:-1]
72
73  if rev[:1] == 'r':
74    rev = rev[1:]
75
76  try:
77    rev = int(rev)
78  except ValueError:
79    print("Encountered non integer revision '%s'" % orig_rev)
80    usage()
81    sys.exit(254)
82  revs.append(rev)
83
84# Make an easily reviewable logfile
85logfile = "-".join([str(x) for x in revs]) + ".log"
86log = open(logfile, 'w')
87
88for rev in revs:
89  merge_cmd = ("svn merge -r%i:%i %s %s" % (rev - 1, rev, src_url, wc_path))
90  log_cmd = 'svn log -v -r%i %s' % (rev, src_url)
91  diff_cmd = 'svn diff -r%i:%i %s' % (rev -1, rev, src_url)
92
93  # Do the merge
94  os.system(merge_cmd)
95
96  # Write our header
97  log.write("=" * 72 + '\n')
98  log.write(merge_cmd + '\n')
99
100  # Get our log
101  fh = os.popen(log_cmd)
102  while True:
103    line = fh.readline()
104    if not line:
105      break
106    log.write(line)
107  fh.close()
108
109  # Get our diff
110  fh = os.popen(diff_cmd)
111  while True:
112    line = fh.readline()
113    if not line:
114      break
115    log.write(line)
116
117  # Write our footer
118  log.write("=" * 72 + '\n' * 10)
119
120
121log.close()
122print("\nYour logfile is '%s'" % logfile)
123