45
45
                os.mkdir( self.dir )
 
48
 
                p = Popen( [ 'bzr', 'init', '.' ], cwd = self.dir,
 
49
 
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
 
50
 
                output = p.communicate()[ 0 ]
 
 
49
                        self.run( [ 'bzr', 'init', '.' ] )
 
 
50
                except self.VcsError as e:
 
53
52
                        # attempt to clean-up dir
 
 
69
68
                os.mkdir( self.dir )
 
72
 
                p = Popen( [ 'bzr', 'co', url, '.' ], cwd = self.dir,
 
73
 
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
 
74
 
                output = p.communicate()[ 0 ]
 
 
72
                        self.run( [ 'bzr', 'checkout', url, '.' ] )
 
 
73
                except self.VcsError as e:
 
77
75
                        # attempt to clean-up dir
 
 
83
 
                        raise self.VcsError( 'bzr checkout failed', output )
 
 
84
        def get_revno( self ):
 
 
85
                """Obtain some sort of revision identifier
 
 
89
                output = self.run( [ 'bzr', 'revno', '--tree' ] )
 
 
92
                buf = StringIO.StringIO( output )
 
 
93
                return buf.readline().rstrip()
 
 
96
        def revert( self, revno = None ):
 
87
97
                """Revert the branch so that there are no outstanding changes or unknown files.
 
 
98
                If a revno is supplied, then the repository is reverted to that
 
91
 
                p = Popen( [ 'bzr', 'revert', '--no-backup' ], cwd = self.dir,
 
92
 
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
 
93
 
                output = p.communicate()[ 0 ]
 
95
 
                        raise self.VcsError( 'bzr revert failed', output )
 
 
102
                # bzr revert (run twice to handle a bug in bzr where reverting a
 
 
103
                # directory from a symlink can cause conflicts during initial revert)
 
 
104
                self.run( [ 'bzr', 'revert', '--no-backup' ] )
 
 
105
                self.run( [ 'bzr', 'revert', '--no-backup' ] )
 
98
 
                p = Popen( [ 'bzr', 'st' ], cwd = self.dir,
 
99
 
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
 
100
 
                output = p.communicate()[ 0 ]
 
102
 
                        raise self.VcsError( 'bzr status failed', output )
 
 
108
                output = self.run( [ 'bzr', 'status' ] )
 
103
109
                files = self.parse_file_blocks( output )
 
105
111
                # remove unknown files
 
106
112
                if 'unknown' in files:
 
107
113
                        for file in files[ 'unknown' ]:
 
 
114
                                if the.verbose > 1: print "removing unknown: " + file
 
108
115
                                full_file = os.path.join( self.dir, file )
 
109
116
                                if os.path.isfile( full_file ):
 
110
117
                                        os.unlink( full_file )
 
111
 
                                elif os.full_file.isdir( full_file ):
 
 
118
                                elif os.path.isdir( full_file ):
 
112
119
                                        shutil.rmtree( full_file )
 
114
121
                                        raise RuntimeError( 'exotic file in repo: %s' % file )
 
 
123
                # if a revision identifyer has been given, update to that
 
 
124
                if revno is not None:
 
 
127
                        self.run( [ 'bzr', 'update', '-r', revno ] )
 
117
130
        def update( self ):
 
118
131
                """Update the branch, pulling down any upstream changes and merging them.  This
 
 
124
 
                p = Popen( [ 'bzr', 'update' ], cwd = self.dir,
 
125
 
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
 
126
 
                output = p.communicate()[ 0 ]
 
128
 
                        raise self.VcsError( 'bzr update failed', output )
 
 
136
#               WARNING: the following might cause bzr to ask for your ssh password more than
 
 
137
#               once during an update!!!
 
 
140
#               revno = self.get_revno()
 
 
142
#               # update to current revision (pull in history without updating tree)
 
 
143
#               self.run( [ 'bzr', 'update', '-r', revno ] )
 
 
146
#               next_revno = str( int( revno ) + 1 )
 
 
147
#               output = self.run( [ 'bzr', 'log', '-r', next_revno + '..' ] )
 
 
150
#               keep_files = list()
 
 
151
#               buf = StringIO.StringIO( output )
 
 
154
#                       line = line.rstrip( '\n' )
 
 
155
#                       if line.lower() == 'message:':
 
 
158
#                               if line[ : 2 ] != '  ':
 
 
163
#                                       # process directives
 
 
164
#                                       if line[ : 6 ].lower() == 'keep: ':
 
 
166
#                                               if file in rename_files: file = rename_files[ file ]
 
 
167
#                                               keep_files.append( file )
 
 
168
#                                       elif line[ : 8 ].lower() == 'rename: ':
 
 
169
#                                               rename_from = line[ 8 : ]
 
 
170
#                                       elif line[ : 4 ].lower() == 'to: ':
 
 
171
#                                               if rename_from in rename_files:
 
 
172
#                                                       rename_from = rename_files[ rename_from ]
 
 
173
#                                               rename_files[ line[ 4 : ] ] = rename_from
 
 
175
                # bzr update properly
 
 
176
                output = self.run( [ 'bzr', 'update' ] )
 
130
178
                # parse output (see logic in report() in bzrlib/delta.py)
 
 
134
182
                        if not re.search( '^[-R+ ?][K NMD!][* ] ', line ): continue
 
135
183
                        line = line.rstrip()
 
136
 
                        if the.verbose: print '  %s' % line
 
 
184
                        if the.verbose > 1: print '  %s' % line
 
138
186
                        # renames show before and after file names
 
139
187
                        matches = re.search( '^R.. (.*?)[/@+]? => (.*?)[/@+]?$', line )
 
 
168
 
                p = Popen( [ 'bzr', 'status', '--no-pending' ], cwd = self.dir,
 
169
 
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
 
170
 
                output = p.communicate()[ 0 ]
 
172
 
                        raise self.VcsError( 'bzr status failed', output )
 
 
216
                output = self.run( [ 'bzr', 'status', '--no-pending' ] )
 
173
219
                files = self.parse_file_blocks( output )
 
174
220
                return True if len( files ) else False
 
 
182
 
                p = Popen( [ 'bzr', 'status', '--no-pending' ], cwd = self.dir,
 
183
 
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
 
184
 
                output = p.communicate()[ 0 ]
 
186
 
                        raise self.VcsError( 'bzr status failed', output )
 
 
228
                output = self.run( [ 'bzr', 'status', '--no-pending' ] )
 
187
231
                files = self.parse_file_blocks( output )
 
188
232
                return files['conflicts'] if 'conflicts' in files else None
 
 
235
        def run( self, cmd ):
 
 
236
                if the.verbose > 1: print 'exec: %s' % ' '.join( cmd )
 
 
237
                p = Popen( cmd, cwd = self.dir,
 
 
238
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
 
 
239
                output = p.communicate()[ 0 ]
 
 
241
                        raise self.VcsError( ' '.join( cmd[ : 2 ] ), output )
 
191
245
        def parse_file_blocks( self, output ):