root/trunk/svnutils/merge-safe.py

Revision 291, 5.7 KB (checked in by tyler, 2 years ago)

[git @ 0e6f213e17fc8eeb17325ae8cea76f5da056a661]
OMFG TESTING

Original author: R. Tyler Ballance <tyler@…>
Date: 2008-08-12 16:46:01-07:00

Line 
1'''
2Copyright (c) 2007, Slide, Inc.
3
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7
8        * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9        * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10        * Neither the name of the Slide, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11
12THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
16CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
17EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
18PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
19PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
20LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
24
25For questions and patches, contact <tyler@slide.com>
26
27'''
28import os
29import sys
30import string
31from optparse import OptionParser
32
33class MergeMaster(object):
34        '''
35                MergeMaster handles a lot of the specific merge operations that are
36                needed for merging file-by-file from one branch to another
37        '''     
38        def __init__(self, src, dst, src_rev, dst_rev):
39                self.src = src
40                self.dst = dst
41                self.src_rev = src_rev
42                self.dst_rev = dst_rev
43                self.dry_run = False
44                self.diff_cmd = 'svn diff %(dst)s@%(dst_rev)s %(src)s@%(dst_rev)s'
45                self.merge_cmd = 'svn merge -r %(src_rev)s:%(dst_rev)s %(src)s/%(file)s ./%(file)s'
46                self.copy_cmd = 'svn copy -r %(dst_rev)s %(src)s/%(file)s ./%(file)s'
47       
48        def __del__(self):
49                pass
50
51       
52        def branch_diff(self):
53                cmd = self.diff_cmd % {'src_rev' : self.src_rev, 'dst_rev' : self.dst_rev, 'src' : self.src,
54                                                        'dst' : self.dst}
55                print 'Figuring out which files have changed between the two branches at r%s' % (self.dst_rev)
56                files = os.popen('%s | grep "Index: " | sed \'s/Index: //\'' % cmd)
57                files = files.readlines()
58
59                for index, file in enumerate(files):
60                        files[index] = string.strip(file)
61       
62                return files
63
64        def merge_files(self, files):
65                appendage = ''
66                new_files = []
67                if self.dry_run:
68                        appendage = ' --dry-run'       
69                for file in files:
70                        cmd = self.merge_cmd % {'src_rev' : self.src_rev, 'dst_rev' : self.dst_rev, 'src' : self.src,
71                                                                'dst' : self.dst, 'file' : file}
72                        cmd += appendage                                       
73                        cont = True
74                       
75                        if self.interactive:
76                                print 'Would you like to merge %(file)s?' % {'file' : file}
77                                sys.stdout.write('[y/n]: ')
78                                while True:
79                                        val = raw_input()
80                                        if val == 'y':
81                                                break
82                                        elif val ==     'n':
83                                                cont = False
84                                                break
85                                        else:
86                                                sys.stdout.write('Please enter \'y\' or \'n\': ')
87                                                continue
88
89                        if not cont:
90                                continue
91
92                        print '==> %s' % cmd
93                        #       Using popen3 to make sure we capture stderr
94                        stdin, out, err = os.popen3(cmd)
95                        if out:
96                                out = out.readlines()
97                                out = string.join(out, ' ')
98                                print string.strip(out)
99                        if err:
100                                err = err.readlines()
101                                err = string.join(err, ' ')
102
103                                if string.find(err, 'is not under version control') >= 0:
104                                        print 'Looks like "%s" is a new file, we\'ll add it later' % (file)
105                                        print
106                                        new_files.append(file)
107
108                if not self.dry_run:
109                        for file in new_files:
110                                cmd = self.copy_cmd % {'file' : file, 'src' : self.src, 'dst_rev' : self.dst_rev}
111                                print '==> %s' % cmd
112                                os.system(cmd)
113
114
115def main():
116        options = OptionParser()
117        options.usage = '''
118
119The merge-safe script should help you, the lowly startup employee
120more effectively merge one branch to another by examining which files
121have changed, and merge/copy those to the destination branch.
122       
123Examples:
124        Do a dry-run of merging from $SRC to $DST where r1002 is the starting branch of
125        $SRC and r1050 is the last revision to merge from $SRC
126          %> python some/dir/merge-safe.py -s $SRC -d $DST -r 1002:1050 --dry-run
127               
128        Do an interactive merge from $SRC to $DST
129          %> python some/dir/merge-safe.py -s $SRC -d $DST -r 1002:1050 -i
130
131        Usage: $prog [options]
132                        '''     
133        options.add_option('-s' , '--source', dest='source', help='The source branch to merge from')
134        options.add_option('-d' , '--dest', dest='dest', help='The destination branch to merge to')
135        options.add_option('-i', '--interactive', action='store_true', dest='interactive', help='Enable merging interactively on each file')
136        options.add_option('--dry-run', action='store_true', dest='dry_run', help='Run with --dry-run enabled')
137        options.add_option('-r', '--revision', dest='revision', help='Specify the revisions separated by a colon (i.e. -r 100:104)')
138        opts, args = options.parse_args()
139
140        if not opts.source or not opts.dest:
141                print 'Please enter a valid source and destination branch!'
142                return
143        if not opts.revision:
144                print 'Please enter a valid set of revisions'
145                return
146
147        revision = string.split(opts.revision, ':')
148        source_revision = revision[0]
149        dest_revision = revision[1]
150        if not source_revision or not dest_revision:
151                print 'Please enter a valid set of revisions'
152                return
153
154        master = MergeMaster(opts.source, opts.dest, source_revision, dest_revision)
155        master.dry_run = opts.dry_run
156        master.interactive = opts.interactive
157        diff_files = master.branch_diff()
158       
159        if len(diff_files) > 0:
160                master.merge_files(diff_files)
161       
162
163if __name__ == '__main__':
164        main()
165
Note: See TracBrowser for help on using the browser.