/stdhome

To get this branch, use:
bzr branch http://bzr.ed.am/stdhome

« back to all changes in this revision

Viewing changes to lib/stdhome/vcs/bzr.py

  • Committer: Tim Marston
  • Date: 2016-04-10 20:23:56 UTC
  • Revision ID: tim@ed.am-20160410202356-3rl6p8m4nef0nx3b
updated todo

Show diffs side-by-side

added added

removed removed

34
34
 
35
35
                @param dir the fully-qualified directory to work in.
36
36
                """
 
37
 
37
38
                self.dir = dir
38
39
 
39
40
 
 
41
        def has_authority( self ):
 
42
                """Check that the directory is under this VCS's control.
 
43
                """
 
44
 
 
45
                return os.path.exists( os.path.join( self.dir, '.bzr' ) )
 
46
 
 
47
 
 
48
        def expand_repo_url( self, url ):
 
49
                """Convert a simple hostname in to an URL that the VCS can use.
 
50
                """
 
51
 
 
52
                return 'bzr+ssh://%s/%s/%s' % ( url, the.dir, the.repo.name )
 
53
 
 
54
 
40
55
        def init( self ):
41
56
                """Create a new, empty branch
42
57
                """
85
100
                """Obtain some sort of revision identifier
86
101
                """
87
102
 
88
 
                # bzr revert
 
103
                # bzr revno
89
104
                output = self.run( [ 'bzr', 'revno', '--tree' ] )
90
105
 
91
106
                # parse revno
99
114
                revision.
100
115
                """
101
116
 
 
117
                # bzr st
 
118
                output = self.run( [ 'bzr', 'status' ] )
 
119
                files = self.parse_file_blocks( output )
 
120
 
 
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 )
 
127
                                if not matches:
 
128
                                        raise RunTimeError(
 
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 )
 
137
                                else:
 
138
                                        raise RuntimeError( 'exotic file in repo: %s' % file )
 
139
 
102
140
                # bzr revert
103
141
                self.run( [ 'bzr', 'revert', '--no-backup' ] )
104
142
 
109
147
                # remove unknown files
110
148
                if 'unknown' in files:
111
149
                        for file in files[ 'unknown' ]:
 
150
                                matches = re.search( r'^(.+?)[/@+]?$', file )
 
151
                                if not matches:
 
152
                                        raise RunTimeError(
 
153
                                                'failed to parse bzr unknowns: %s' % file )
 
154
                                file = matches.group( 1 )
 
155
                                if the.verbose >= 2: print "removing (unknown): " + file
112
156
                                full_file = os.path.join( self.dir, file )
113
 
                                if os.path.isfile( full_file ):
 
157
                                if os.path.isfile( full_file ) or os.path.islink( full_file ):
114
158
                                        os.unlink( full_file )
115
 
                                elif os.full_file.isdir( full_file ):
 
159
                                elif os.path.isdir( full_file ):
116
160
                                        shutil.rmtree( full_file )
117
161
                                else:
118
162
                                        raise RuntimeError( 'exotic file in repo: %s' % file )
119
163
 
120
 
                # if a revision identifyer has been given, update to that
121
 
                if revno is not None:
 
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:
122
166
 
123
167
                        # bzr update
124
168
                        self.run( [ 'bzr', 'update', '-r', revno ] )
178
222
                for line in buf:
179
223
                        if not re.search( '^[-R+ ?][K NMD!][* ] ', line ): continue
180
224
                        line = line.rstrip()
181
 
                        if the.verbose > 1: print '  %s' % line
182
225
 
183
226
                        # renames show before and after file names
184
227
                        matches = re.search( '^R.. (.*?)[/@+]? => (.*?)[/@+]?$', line )
200
243
                                continue
201
244
 
202
245
                        raise RuntimeError(
203
 
                                'failed to parse bzr update output line:\n%' % line )
 
246
                                'failed to parse bzr update output line:\n%s' % line )
204
247
 
205
248
                return files
206
249
 
229
272
                return files['conflicts'] if 'conflicts' in files else None
230
273
 
231
274
 
 
275
        def add( self, files ):
 
276
                """Make sure files are added to version control.
 
277
                @param files a list of relative filenames
 
278
                """
 
279
 
 
280
                # bzr add
 
281
                self.run( [ 'bzr', 'add', '-N' ] + files )
 
282
 
 
283
 
 
284
        def commit( self ):
 
285
                """Commit changes to the repo.
 
286
                """
 
287
 
 
288
                # bzr commit
 
289
                self.run( [ 'bzr', 'commit', '-m', '' ] )
 
290
 
 
291
 
232
292
        def run( self, cmd ):
233
 
                if the.verbose > 1: print 'exec: %s' % ' '.join( cmd )
 
293
                if the.verbose >= 2: print 'exec: %s' % ' '.join( cmd )
234
294
                p = Popen( cmd, cwd = self.dir,
235
295
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
236
296
                output = p.communicate()[ 0 ]
237
297
                if p.returncode > 0:
238
298
                        raise self.VcsError( ' '.join( cmd[ : 2 ] ), output )
 
299
                if the.verbose >= 2:
 
300
                        verbose_output = output.rstrip()
 
301
                        if len( verbose_output ):
 
302
                                print re.sub( '(^|\n)', '\\1  : ', verbose_output )
239
303
                return output
240
304
 
241
305
 
255
319
                                                res[ current ] = list()
256
320
                                        res[ current ].append( matches.group( 1 ) )
257
321
                                        continue
258
 
                        if re.search( '^[0-9]+ shelf exists', line ): continue
 
322
                        if re.search( '^[0-9]+ shel(?:f|ves) exists?', line ): continue
259
323
                        if re.search( '^working tree is out of date', line ): continue
260
324
                        raise self.ParseError( "unrecognised line: %s" % line )
261
325
                return res