~bzr-upload-devs/bzr-upload/trunk

« back to all changes in this revision

Viewing changes to __init__.py

  • Committer: Vincent Ladeuil
  • Date: 2010-01-07 15:46:30 UTC
  • mfrom: (68.1.5 499525-ignore)
  • Revision ID: v.ladeuil+lp@free.fr-20100107154630-2th06jzq919iahm2
(vila, beuno) Implement .bzrignore-upload

Show diffs side-by-side

added added

removed removed

Lines of Context:
164
164
from bzrlib import (
165
165
    bzrdir,
166
166
    errors,
 
167
    globbing,
167
168
    ignores,
168
169
    revisionspec,
169
170
    transport,
262
263
        self._pending_deletions = []
263
264
        self._pending_renames = []
264
265
        self._uploaded_revid = None
 
266
        self._ignored = None
265
267
 
266
268
    def set_uploaded_revid(self, rev_id):
267
269
        # XXX: Add tests for concurrent updates, etc.
279
281
                self._uploaded_revid = revision.NULL_REVISION
280
282
        return self._uploaded_revid
281
283
 
282
 
    def get_ignored(self):
283
 
        """Get upload-specific ignored files from the current branch"""
284
 
        try:
285
 
            ignore_file = self.tree.get_file_by_path('.bzrignore-upload')
286
 
            return ignores.parse_ignore_file(ignore_file)
287
 
        except errors.NoSuchId:
288
 
            return []
 
284
    def _get_ignored(self):
 
285
        if self._ignored is None:
 
286
            try:
 
287
                ignore_file = self.tree.get_file_by_path('.bzrignore-upload')
 
288
                ignored_patterns = ignores.parse_ignore_file(ignore_file)
 
289
            except errors.NoSuchId:
 
290
                ignored_patterns = []
 
291
            self._ignored = globbing.Globster(ignored_patterns)
 
292
        return self._ignored
 
293
 
 
294
    def is_ignored(self, relpath):
 
295
        glob = self._get_ignored()
 
296
        ignored = glob.match(relpath)
 
297
        import os
 
298
        if not ignored:
 
299
            # We still need to check that all parents are not ignored
 
300
            dir = os.path.dirname(relpath)
 
301
            while dir and not ignored:
 
302
                ignored = glob.match(dir)
 
303
                if not ignored:
 
304
                    dir = os.path.dirname(dir)
 
305
        return ignored
289
306
 
290
307
    def upload_file(self, relpath, id, mode=None):
291
 
        ignored_files = self.get_ignored()
292
 
        if relpath not in ignored_files:
293
 
            if mode is None:
294
 
                if self.tree.is_executable(id):
295
 
                    mode = 0775
296
 
                else:
297
 
                    mode = 0664
298
 
            if not self.quiet:
299
 
                self.outf.write('Uploading %s\n' % relpath)
300
 
            self.to_transport.put_bytes(relpath, self.tree.get_file_text(id), mode)
301
 
        else:
302
 
            if not self.quiet:
303
 
                self.outf.write('Ignoring %s\n' % relpath)
 
308
        if mode is None:
 
309
            if self.tree.is_executable(id):
 
310
                mode = 0775
 
311
            else:
 
312
                mode = 0664
 
313
        if not self.quiet:
 
314
            self.outf.write('Uploading %s\n' % relpath)
 
315
        self.to_transport.put_bytes(relpath, self.tree.get_file_text(id),
 
316
                                    mode)
304
317
 
305
318
    def upload_file_robustly(self, relpath, id, mode=None):
306
319
        """Upload a file, clearing the way on the remote side.
321
334
        self.upload_file(relpath, id, mode)
322
335
 
323
336
    def make_remote_dir(self, relpath, mode=None):
324
 
        ignored_files = self.get_ignored()
325
 
        if relpath not in ignored_files:
326
 
            if mode is None:
327
 
                mode = 0775
328
 
            self.to_transport.mkdir(relpath, mode)
329
 
        else:
330
 
            if not self.quiet:
331
 
                self.outf.write('Ignoring %s\n' % relpath)
 
337
        if mode is None:
 
338
            mode = 0775
 
339
        self.to_transport.mkdir(relpath, mode)
332
340
 
333
341
    def make_remote_dir_robustly(self, relpath, mode=None):
334
342
        """Create a remote directory, clearing the way on the remote side.
359
367
        if not self.quiet:
360
368
            self.outf.write('Deleting %s\n' % relpath)
361
369
        self.to_transport.rmdir(relpath)
 
370
        # XXX: Add a test where a subdir is ignored but we still want to
 
371
        # delete the dir -- vila 100106
362
372
 
363
373
    def delete_remote_dir_maybe(self, relpath):
364
374
        """Try to delete relpath, keeping failures to retry later."""
416
426
            for relpath, ie in self.tree.inventory.iter_entries():
417
427
                if relpath in ('', '.bzrignore', '.bzrignore-upload'):
418
428
                    # skip root ('')
419
 
                    # .bzrignore has no meaning outside of a working tree
420
 
                    # so do not upload it
 
429
                    # .bzrignore and .bzrignore-upload have no meaning outside
 
430
                    # a working tree so do not upload them
 
431
                    continue
 
432
                if self.is_ignored(relpath):
 
433
                    if not self.quiet:
 
434
                        self.outf.write('Ignoring %s\n' % relpath)
421
435
                    continue
422
436
                if ie.kind == 'file':
423
437
                    self.upload_file_robustly(relpath, ie.file_id)
456
470
        self.tree.lock_read()
457
471
        try:
458
472
            for (path, id, kind) in changes.removed:
 
473
                if self.is_ignored(path):
 
474
                    if not self.quiet:
 
475
                        self.outf.write('Ignoring %s\n' % path)
 
476
                    continue
459
477
                if kind is 'file':
460
478
                    self.delete_remote_file(path)
461
479
                elif kind is  'directory':
465
483
 
466
484
            for (old_path, new_path, id, kind,
467
485
                 content_change, exec_change) in changes.renamed:
 
486
                if self.is_ignored(old_path) and self.is_ignored(new_path):
 
487
                    if not self.quiet:
 
488
                        self.outf.write('Ignoring %s\n' % old_path)
 
489
                        self.outf.write('Ignoring %s\n' % new_path)
 
490
                    continue
468
491
                if content_change:
469
492
                    # We update the old_path content because renames and
470
493
                    # deletions are differed.
474
497
            self.finish_deletions()
475
498
 
476
499
            for (path, id, old_kind, new_kind) in changes.kind_changed:
 
500
                if self.is_ignored(path):
 
501
                    if not self.quiet:
 
502
                        self.outf.write('Ignoring %s\n' % path)
 
503
                    continue
477
504
                if old_kind is 'file':
478
505
                    self.delete_remote_file(path)
479
506
                elif old_kind is  'directory':
489
516
                    raise NotImplementedError
490
517
 
491
518
            for (path, id, kind) in changes.added:
 
519
                if self.is_ignored(path):
 
520
                    if not self.quiet:
 
521
                        self.outf.write('Ignoring %s\n' % path)
 
522
                    continue
492
523
                if kind is 'file':
493
524
                    self.upload_file(path, id)
494
525
                elif kind is 'directory':
499
530
            # XXX: Add a test for exec_change
500
531
            for (path, id, kind,
501
532
                 content_change, exec_change) in changes.modified:
 
533
                if self.is_ignored(path):
 
534
                    if not self.quiet:
 
535
                        self.outf.write('Ignoring %s\n' % path)
 
536
                    continue
502
537
                if kind is 'file':
503
538
                    self.upload_file(path, id)
504
539
                else: