35
35
@param dir the fully-qualified directory to work in.
41
def has_authority( self ):
42
"""Check that the directory is under this VCS's control.
45
return os.path.exists( os.path.join( self.dir, '.bzr' ) )
48
def expand_repo_url( self, url ):
49
"""Convert a simple hostname in to an URL that the VCS can use.
52
return 'bzr+ssh://%s/%s/%s' % ( url, the.dir, the.repo.name )
41
56
"""Create a new, empty branch
69
83
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 ]
87
self.run( [ 'bzr', 'checkout', url, '.' ] )
88
except self.VcsError as e:
77
90
# attempt to clean-up dir
83
raise self.VcsError( 'bzr checkout failed', output )
99
def get_revno( self ):
100
"""Obtain some sort of revision identifier
104
output = self.run( [ 'bzr', 'revno', '--tree' ] )
107
buf = StringIO.StringIO( output )
108
return buf.readline().rstrip()
111
def revert( self, revno = None ):
87
112
"""Revert the branch so that there are no outstanding changes or unknown files.
113
If a revno is supplied, then the repository is reverted to that
118
output = self.run( [ 'bzr', 'status' ] )
119
files = self.parse_file_blocks( output )
121
# remove kind changed files (or they can cause `bzr revert` to break in
122
# strange situations, like when a directory has been replaced with a
123
# symlink to a non-existant file)
124
if 'kind changed' in files:
125
for file in files[ 'kind changed' ]:
126
matches = re.search( '^(.+?)[/@+]? \([^)]+\)$', file )
129
'failed to parse bzr kind change: %s' % file )
130
file = matches.group( 1 )
131
if the.verbose >= 2: print "removing (kind changed): " + file
132
full_file = os.path.join( self.dir, file )
133
if os.path.isfile( full_file ) or os.path.islink( full_file ):
134
os.unlink( full_file )
135
elif os.path.isdir( full_file ):
136
shutil.rmtree( full_file )
138
raise RuntimeError( 'exotic file in repo: %s' % file )
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 )
141
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 )
144
output = self.run( [ 'bzr', 'status' ] )
103
145
files = self.parse_file_blocks( output )
105
147
# remove unknown files
106
148
if 'unknown' in files:
107
149
for file in files[ 'unknown' ]:
150
matches = re.search( r'^(.+?)[/@+]?$', file )
153
'failed to parse bzr unknowns: %s' % file )
154
file = matches.group( 1 )
155
if the.verbose >= 2: print "removing (unknown): " + file
108
156
full_file = os.path.join( self.dir, file )
109
if os.path.isfile( full_file ):
157
if os.path.isfile( full_file ) or os.path.islink( full_file ):
110
158
os.unlink( full_file )
111
elif os.full_file.isdir( full_file ):
159
elif os.path.isdir( full_file ):
112
160
shutil.rmtree( full_file )
114
162
raise RuntimeError( 'exotic file in repo: %s' % file )
164
# if a revision identifier has been given, ensure we're updated to that
165
if revno is not None and self.get_revno() != revno:
168
self.run( [ 'bzr', 'update', '-r', revno ] )
117
171
def update( self ):
118
172
"""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 )
177
# WARNING: the following might cause bzr to ask for your ssh password more than
178
# once during an update!!!
181
# revno = self.get_revno()
183
# # update to current revision (pull in history without updating tree)
184
# self.run( [ 'bzr', 'update', '-r', revno ] )
187
# next_revno = str( int( revno ) + 1 )
188
# output = self.run( [ 'bzr', 'log', '-r', next_revno + '..' ] )
191
# keep_files = list()
192
# buf = StringIO.StringIO( output )
195
# line = line.rstrip( '\n' )
196
# if line.lower() == 'message:':
199
# if line[ : 2 ] != ' ':
204
# # process directives
205
# if line[ : 6 ].lower() == 'keep: ':
207
# if file in rename_files: file = rename_files[ file ]
208
# keep_files.append( file )
209
# elif line[ : 8 ].lower() == 'rename: ':
210
# rename_from = line[ 8 : ]
211
# elif line[ : 4 ].lower() == 'to: ':
212
# if rename_from in rename_files:
213
# rename_from = rename_files[ rename_from ]
214
# rename_files[ line[ 4 : ] ] = rename_from
216
# bzr update properly
217
output = self.run( [ 'bzr', 'update' ] )
130
219
# parse output (see logic in report() in bzrlib/delta.py)
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 )
256
output = self.run( [ 'bzr', 'status', '--no-pending' ] )
173
259
files = self.parse_file_blocks( output )
174
260
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 )
268
output = self.run( [ 'bzr', 'status', '--no-pending' ] )
187
271
files = self.parse_file_blocks( output )
188
272
return files['conflicts'] if 'conflicts' in files else None
275
def add( self, files ):
276
"""Make sure files are added to version control.
277
@param files a list of relative filenames
281
self.run( [ 'bzr', 'add', '-N' ] + files )
285
"""Commit changes to the repo.
289
self.run( [ 'bzr', 'commit', '-m', '' ] )
292
def run( self, cmd ):
293
if the.verbose >= 2: print 'exec: %s' % ' '.join( cmd )
294
p = Popen( cmd, cwd = self.dir,
295
stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
296
output = p.communicate()[ 0 ]
298
raise self.VcsError( ' '.join( cmd[ : 2 ] ), output )
300
verbose_output = output.rstrip()
301
if len( verbose_output ):
302
print re.sub( '(^|\n)', '\\1 : ', verbose_output )
191
306
def parse_file_blocks( self, output ):