3
# Copyright (C) 2014 Tim Marston <tim@edm.am>
5
# This file is part of stdhome (hereafter referred to as "this program").
6
# See http://ed.am/dev/stdhome for more information.
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
22
import subprocess, os, re, shutil
23
from subprocess import Popen
26
from stdhome import the
32
def __init__( self, dir ):
35
@param dir the fully-qualified directory to work in.
41
"""Create a new, empty branch
44
# the directory shouldn't exist
49
self.run( [ 'bzr', 'init', '.' ] )
50
except self.VcsError as e:
52
# attempt to clean-up dir
54
shutil.rmtree( self.dir )
61
def checkout( self, url ):
62
"""Checkout a new copy of a remote branch.
64
@param url the remote repository URL
67
# the directory shouldn't exist
72
self.run( [ 'bzr', 'checkout', url, '.' ] )
73
except self.VcsError as e:
75
# attempt to clean-up dir
77
shutil.rmtree( self.dir )
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 ):
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
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' ] )
108
output = self.run( [ 'bzr', 'status' ] )
109
files = self.parse_file_blocks( output )
111
# remove unknown files
112
if 'unknown' in files:
113
for file in files[ 'unknown' ]:
114
if the.verbose > 1: print "removing unknown: " + file
115
full_file = os.path.join( self.dir, file )
116
if os.path.isfile( full_file ):
117
os.unlink( full_file )
118
elif os.path.isdir( full_file ):
119
shutil.rmtree( full_file )
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 ] )
131
"""Update the branch, pulling down any upstream changes and merging them. This
132
method returns a list of the files that were modified as part of this
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' ] )
178
# parse output (see logic in report() in bzrlib/delta.py)
180
buf = StringIO.StringIO( output )
182
if not re.search( '^[-R+ ?][K NMD!][* ] ', line ): continue
184
if the.verbose > 1: print ' %s' % line
186
# renames show before and after file names
187
matches = re.search( '^R.. (.*?)[/@+]? => (.*?)[/@+]?$', line )
189
files.append( matches.group( 1 ) )
190
files.append( matches.group( 2 ) )
193
# kind changes shows the same name twice
194
matches = re.search( '^.K. (.*?)[/@+]? => (.*?)[/@+]?$', line )
196
files.append( matches.group( 1 ) )
199
# other entries have only one filename
200
matches = re.search( '^... (.*?)[/@+]?$', line )
202
files.append( matches.group( 1 ) )
206
'failed to parse bzr update output line:\n%' % line )
211
def has_changes( self ):
212
"""Check if the branch has any local modifications.
216
output = self.run( [ 'bzr', 'status', '--no-pending' ] )
219
files = self.parse_file_blocks( output )
220
return True if len( files ) else False
223
def get_conflicts( self ):
224
"""Return a list of files that have conflicts.
228
output = self.run( [ 'bzr', 'status', '--no-pending' ] )
231
files = self.parse_file_blocks( output )
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 )
245
def parse_file_blocks( self, output ):
248
buf = StringIO.StringIO( output )
250
matches = re.search( '^([a-z ]+):$', line, re.I )
252
current = matches.group( 1 )
255
matches = re.search( '^ ([^ ].*)$', line )
257
if not current in res:
258
res[ current ] = list()
259
res[ current ].append( matches.group( 1 ) )
261
if re.search( '^[0-9]+ shelf exists', line ): continue
262
if re.search( '^working tree is out of date', line ): continue
263
raise self.ParseError( "unrecognised line: %s" % line )
267
class ParseError( Exception ):