~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/workingtree.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-05-19 05:46:31 UTC
  • mfrom: (1714.1.1 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20060519054631-77bbfb6756845888
Merge reductions in overhead during smart_add. (Robert Collins).

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
# At the moment they may alias the inventory and have old copies of it in
40
40
# memory.  (Now done? -- mbp 20060309)
41
41
 
 
42
from binascii import hexlify
42
43
from copy import deepcopy
43
44
from cStringIO import StringIO
44
45
import errno
45
46
import fnmatch
46
47
import os
 
48
import re
47
49
import stat
48
 
 
 
50
from time import time
49
51
 
50
52
from bzrlib.atomicfile import AtomicFile
51
53
from bzrlib.branch import (Branch,
80
82
                            pumpfile,
81
83
                            safe_unicode,
82
84
                            splitpath,
83
 
                            rand_bytes,
 
85
                            rand_chars,
84
86
                            normpath,
85
87
                            realpath,
86
88
                            relpath,
101
103
import bzrlib.xml5
102
104
 
103
105
 
 
106
# the regex here does the following:
 
107
# 1) remove any weird characters; we don't escape them but rather
 
108
# just pull them out
 
109
 # 2) match leading '.'s to make it not hidden
 
110
_gen_file_id_re = re.compile(r'[^\w.]|(^\.*)')
 
111
_gen_id_suffix = None
 
112
_gen_id_serial = 0
 
113
 
 
114
 
 
115
def _next_id_suffix():
 
116
    """Create a new file id suffix that is reasonably unique.
 
117
    
 
118
    On the first call we combine the current time with 64 bits of randomness
 
119
    to give a highly probably globally unique number. Then each call in the same
 
120
    process adds 1 to a serial number we append to that unique value.
 
121
    """
 
122
    # XXX TODO: change bzrlib.add.smart_add to call workingtree.add() rather 
 
123
    # than having to move the id randomness out of the inner loop like this.
 
124
    # XXX TODO: for the global randomness this uses we should add the thread-id
 
125
    # before the serial #.
 
126
    global _gen_id_suffix, _gen_id_serial
 
127
    if _gen_id_suffix is None:
 
128
        _gen_id_suffix = "-%s-%s-" % (compact_date(time()), rand_chars(16))
 
129
    _gen_id_serial += 1
 
130
    return _gen_id_suffix + str(_gen_id_serial)
 
131
 
 
132
 
104
133
def gen_file_id(name):
105
 
    """Return new file id.
106
 
 
107
 
    This should probably generate proper UUIDs, but for the moment we
108
 
    cope with just randomness because running uuidgen every time is
109
 
    slow."""
110
 
    import re
111
 
    from binascii import hexlify
112
 
    from time import time
113
 
 
114
 
    # get last component
115
 
    idx = name.rfind('/')
116
 
    if idx != -1:
117
 
        name = name[idx+1 : ]
118
 
    idx = name.rfind('\\')
119
 
    if idx != -1:
120
 
        name = name[idx+1 : ]
121
 
 
122
 
    # make it not a hidden file
123
 
    name = name.lstrip('.')
124
 
 
125
 
    # remove any wierd characters; we don't escape them but rather
126
 
    # just pull them out
127
 
    name = re.sub(r'[^\w.]', '', name)
128
 
 
129
 
    s = hexlify(rand_bytes(8))
130
 
    return '-'.join((name, compact_date(time()), s))
 
134
    """Return new file id for the basename 'name'.
 
135
 
 
136
    The uniqueness is supplied from _next_id_suffix.
 
137
    """
 
138
    # XXX TODO: squash the filename to lowercase.
 
139
    # XXX TODO: truncate the filename to something like 20 or 30 chars.
 
140
    # XXX TODO: consider what to do with ids that look like illegal filepaths
 
141
    # on platforms we support.
 
142
    return _gen_file_id_re.sub('', name) + _next_id_suffix()
131
143
 
132
144
 
133
145
def gen_root_id():
588
600
                               'i.e. regular file, symlink or directory): %s' % quotefn(f))
589
601
 
590
602
            if file_id is None:
591
 
                file_id = gen_file_id(f)
592
 
            inv.add_path(f, kind=kind, file_id=file_id)
 
603
                inv.add_path(f, kind=kind)
 
604
            else:
 
605
                inv.add_path(f, kind=kind, file_id=file_id)
593
606
 
594
 
            mutter("add file %s file_id:{%s} kind=%r" % (f, file_id, kind))
595
607
        self._write_inventory(inv)
596
608
 
597
609
    @needs_write_lock