/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/generate_ids.py

  • Committer: Vincent Ladeuil
  • Date: 2010-07-07 11:21:19 UTC
  • mto: (5193.7.1 unify-confs)
  • mto: This revision was merged to the branch mainline in revision 5349.
  • Revision ID: v.ladeuil+lp@free.fr-20100707112119-jwyh312df41w6l0o
Revert previous change as I can't reproduce the related problem anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007, 2009, 2010, 2011 Canonical Ltd
 
1
# Copyright (C) 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
"""Common code for generating file or revision ids."""
18
18
 
19
 
from ..lazy_import import lazy_import
 
19
from bzrlib.lazy_import import lazy_import
20
20
lazy_import(globals(), """
21
21
import time
 
22
import unicodedata
22
23
 
23
 
from breezy import (
 
24
from bzrlib import (
24
25
    config,
 
26
    errors,
25
27
    osutils,
26
28
    )
27
29
""")
28
30
 
29
 
from .. import (
 
31
from bzrlib import (
30
32
    lazy_regex,
31
33
    )
32
34
 
33
35
# the regex removes any weird characters; we don't escape them
34
36
# but rather just pull them out
35
 
_file_id_chars_re = lazy_regex.lazy_compile(b'[^\\w.]')
36
 
_rev_id_chars_re = lazy_regex.lazy_compile(b'[^-\\w.+@]')
 
37
_file_id_chars_re = lazy_regex.lazy_compile(r'[^\w.]')
 
38
_rev_id_chars_re = lazy_regex.lazy_compile(r'[^-\w.+@]')
37
39
_gen_file_id_suffix = None
38
40
_gen_file_id_serial = 0
39
41
 
45
47
    give a highly probably globally unique number. Then each call in the same
46
48
    process adds 1 to a serial number we append to that unique value.
47
49
    """
48
 
    # XXX TODO: change breezy.add.smart_add_tree to call workingtree.add() rather
 
50
    # XXX TODO: change bzrlib.add.smart_add_tree to call workingtree.add() rather
49
51
    # than having to move the id randomness out of the inner loop like this.
50
52
    # XXX TODO: for the global randomness this uses we should add the thread-id
51
53
    # before the serial #.
56
58
    #           suffix forever.
57
59
    global _gen_file_id_suffix, _gen_file_id_serial
58
60
    if _gen_file_id_suffix is None:
59
 
        _gen_file_id_suffix = ("-%s-%s-" % (
60
 
            osutils.compact_date(time.time()), osutils.rand_chars(16))
61
 
            ).encode("ascii")
 
61
        _gen_file_id_suffix = "-%s-%s-" % (osutils.compact_date(time.time()),
 
62
                                           osutils.rand_chars(16))
62
63
    _gen_file_id_serial += 1
63
 
    return b"%s%d" % (_gen_file_id_suffix, _gen_file_id_serial)
 
64
    return _gen_file_id_suffix + str(_gen_file_id_serial)
64
65
 
65
66
 
66
67
def gen_file_id(name):
68
69
 
69
70
    The uniqueness is supplied from _next_id_suffix.
70
71
    """
71
 
    if isinstance(name, str):
72
 
        name = name.encode('ascii', 'replace')
73
72
    # The real randomness is in the _next_id_suffix, the
74
73
    # rest of the identifier is just to be nice.
75
74
    # So we:
81
80
    #    filesystems
82
81
    # 4) Removing starting '.' characters to prevent the file ids from
83
82
    #    being considered hidden.
84
 
    ascii_word_only = _file_id_chars_re.sub(b'', name.lower())
85
 
    short_no_dots = ascii_word_only.lstrip(b'.')[:20]
 
83
    ascii_word_only = str(_file_id_chars_re.sub('', name.lower()))
 
84
    short_no_dots = ascii_word_only.lstrip('.')[:20]
86
85
    return short_no_dots + _next_id_suffix()
87
86
 
88
87
 
94
93
def gen_revision_id(username, timestamp=None):
95
94
    """Return new revision-id.
96
95
 
97
 
    :param username: The username of the committer, in the format returned by
98
 
        config.username().  This is typically a real name, followed by an
99
 
        email address. If found, we will use just the email address portion.
100
 
        Otherwise we flatten the real name, and use that.
 
96
    :param username: This is the value returned by config.username(), which is
 
97
        typically a real name, followed by an email address. If found, we will
 
98
        use just the email address portion. Otherwise we flatten the real name,
 
99
        and use that.
101
100
    :return: A new revision id.
102
101
    """
103
102
    try:
104
103
        user_or_email = config.extract_email_address(username)
105
 
    except config.NoEmailInUsername:
 
104
    except errors.NoEmailInUsername:
106
105
        user_or_email = username
107
106
 
108
107
    user_or_email = user_or_email.lower()
109
108
    user_or_email = user_or_email.replace(' ', '_')
110
 
    user_or_email = _rev_id_chars_re.sub(b'', user_or_email.encode('utf-8'))
 
109
    user_or_email = _rev_id_chars_re.sub('', user_or_email)
111
110
 
112
111
    # This gives 36^16 ~= 2^82.7 ~= 83 bits of entropy
113
 
    unique_chunk = osutils.rand_chars(16).encode('utf-8')
 
112
    unique_chunk = osutils.rand_chars(16)
114
113
 
115
114
    if timestamp is None:
116
115
        timestamp = time.time()
117
116
 
118
 
    rev_id = b'-'.join((user_or_email,
119
 
                        osutils.compact_date(timestamp).encode('utf-8'),
 
117
    rev_id = u'-'.join((user_or_email,
 
118
                        osutils.compact_date(timestamp),
120
119
                        unique_chunk))
121
 
    return rev_id
 
120
    return rev_id.encode('utf8')