82
raise the.program.FatalError( '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 ):
86
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
103
output = self.run( [ 'bzr', 'status' ] )
104
files = self.parse_file_blocks( output )
106
# remove kind changed files (or they can cause `bzr revert` to break in
107
# strange situations, like when a directory has been replaced with a
108
# symlink to a non-existant file)
109
if 'kind changed' in files:
110
for file in files[ 'kind changed' ]:
111
matches = re.match( r'(.+?)[/@+]? \([^)]+\)', file )
114
'failed to parse bzr kind change: %s' % file )
115
file = matches.group( 1 )
116
if the.verbose >= 2: print "removing (kind changed): " + file
117
full_file = os.path.join( self.dir, file )
118
if os.path.isfile( full_file ) or os.path.islink( full_file ):
119
os.unlink( full_file )
120
elif os.path.isdir( full_file ):
121
shutil.rmtree( full_file )
123
raise RuntimeError( 'exotic file in repo: %s' % file )
90
p = Popen( [ 'bzr', 'revert', '--no-backup' ], cwd = self.dir,
91
stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
92
output = p.communicate()[ 0 ]
94
raise the.program.FatalError( 'bzr revert failed', output )
126
self.run( [ 'bzr', 'revert', '--no-backup' ] )
97
p = Popen( [ 'bzr', 'st' ], cwd = self.dir,
98
stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
99
output = p.communicate()[ 0 ]
101
raise the.program.FatalError( 'bzr status failed', output )
129
output = self.run( [ 'bzr', 'status' ] )
102
130
files = self.parse_file_blocks( output )
104
132
# remove unknown files
105
133
if 'unknown' in files:
106
134
for file in files[ 'unknown' ]:
135
if the.verbose >= 2: print "removing (unknown): " + file
107
136
full_file = os.path.join( self.dir, file )
108
137
if os.path.isfile( full_file ):
109
138
os.unlink( full_file )
110
elif os.full_file.isdir( full_file ):
139
elif os.path.isdir( full_file ):
111
140
shutil.rmtree( full_file )
113
142
raise RuntimeError( 'exotic file in repo: %s' % file )
144
# if a revision identifyer has been given, update to that
145
if revno is not None:
148
self.run( [ 'bzr', 'update', '-r', revno ] )
116
151
def update( self ):
117
"""Update the branch, pulling down any upstream changes and merging them.
152
"""Update the branch, pulling down any upstream changes and merging them. This
153
method returns a list of the files that were modified as part of this
121
p = Popen( [ 'bzr', 'update' ], cwd = self.dir,
122
stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
123
output = p.communicate()[ 0 ]
125
raise the.program.FatalError( 'bzr update failed', output )
157
# WARNING: the following might cause bzr to ask for your ssh password more than
158
# once during an update!!!
161
# revno = self.get_revno()
163
# # update to current revision (pull in history without updating tree)
164
# self.run( [ 'bzr', 'update', '-r', revno ] )
167
# next_revno = str( int( revno ) + 1 )
168
# output = self.run( [ 'bzr', 'log', '-r', next_revno + '..' ] )
171
# keep_files = list()
172
# buf = StringIO.StringIO( output )
175
# line = line.rstrip( '\n' )
176
# if line.lower() == 'message:':
179
# if line[ : 2 ] != ' ':
184
# # process directives
185
# if line[ : 6 ].lower() == 'keep: ':
187
# if file in rename_files: file = rename_files[ file ]
188
# keep_files.append( file )
189
# elif line[ : 8 ].lower() == 'rename: ':
190
# rename_from = line[ 8 : ]
191
# elif line[ : 4 ].lower() == 'to: ':
192
# if rename_from in rename_files:
193
# rename_from = rename_files[ rename_from ]
194
# rename_files[ line[ 4 : ] ] = rename_from
196
# bzr update properly
197
output = self.run( [ 'bzr', 'update' ] )
199
# parse output (see logic in report() in bzrlib/delta.py)
201
buf = StringIO.StringIO( output )
203
if not re.search( '^[-R+ ?][K NMD!][* ] ', line ): continue
205
if the.verbose >= 2: print ' %s' % line
207
# renames show before and after file names
208
matches = re.search( '^R.. (.*?)[/@+]? => (.*?)[/@+]?$', line )
210
files.append( matches.group( 1 ) )
211
files.append( matches.group( 2 ) )
214
# kind changes shows the same name twice
215
matches = re.search( '^.K. (.*?)[/@+]? => (.*?)[/@+]?$', line )
217
files.append( matches.group( 1 ) )
220
# other entries have only one filename
221
matches = re.search( '^... (.*?)[/@+]?$', line )
223
files.append( matches.group( 1 ) )
227
'failed to parse bzr update output line:\n%s' % line )
128
232
def has_changes( self ):
147
p = Popen( [ 'bzr', 'status', '--no-pending' ], cwd = self.dir,
148
stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
149
output = p.communicate()[ 0 ]
151
raise the.program.FatalError( 'bzr status failed', output )
249
output = self.run( [ 'bzr', 'status', '--no-pending' ] )
152
252
files = self.parse_file_blocks( output )
153
253
return files['conflicts'] if 'conflicts' in files else None
256
def add( self, files ):
257
"""Make sure files are added to version control.
258
@param files a list of relative filenames
262
self.run( [ 'bzr', 'add' ] + files )
266
"""Commit changes to the repo.
270
self.run( [ 'bzr', 'commit', '-m', '' ] )
273
def run( self, cmd ):
274
if the.verbose >= 2: print 'exec: %s' % ' '.join( cmd )
275
p = Popen( cmd, cwd = self.dir,
276
stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
277
output = p.communicate()[ 0 ]
279
raise self.VcsError( ' '.join( cmd[ : 2 ] ), output )
156
283
def parse_file_blocks( self, output ):