1
# Copyright (C) 2006 Canonical Ltd
1
# Copyright (C) 2006, 2007, 2009, 2010, 2011 Canonical Ltd
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
17
17
"""Common code for generating file or revision ids."""
19
from bzrlib.lazy_import import lazy_import
19
from __future__ import absolute_import
21
from .lazy_import import lazy_import
20
22
lazy_import(globals(), """
34
from .sixish import text_type
35
36
# the regex removes any weird characters; we don't escape them
36
37
# but rather just pull them out
37
_file_id_chars_re = lazy_regex.lazy_compile(r'[^\w.]')
38
_rev_id_chars_re = lazy_regex.lazy_compile(r'[^-\w.+@]')
38
_file_id_chars_re = lazy_regex.lazy_compile(b'[^\\w.]')
39
_rev_id_chars_re = lazy_regex.lazy_compile(b'[^-\\w.+@]')
39
40
_gen_file_id_suffix = None
40
41
_gen_file_id_serial = 0
47
48
give a highly probably globally unique number. Then each call in the same
48
49
process adds 1 to a serial number we append to that unique value.
50
# XXX TODO: change bzrlib.add.smart_add_tree to call workingtree.add() rather
51
# XXX TODO: change breezy.add.smart_add_tree to call workingtree.add() rather
51
52
# than having to move the id randomness out of the inner loop like this.
52
53
# XXX TODO: for the global randomness this uses we should add the thread-id
53
54
# before the serial #.
59
60
global _gen_file_id_suffix, _gen_file_id_serial
60
61
if _gen_file_id_suffix is None:
61
_gen_file_id_suffix = "-%s-%s-" % (osutils.compact_date(time.time()),
62
osutils.rand_chars(16))
62
_gen_file_id_suffix = ("-%s-%s-" % (
63
osutils.compact_date(time.time()), osutils.rand_chars(16))
63
65
_gen_file_id_serial += 1
64
return _gen_file_id_suffix + str(_gen_file_id_serial)
66
return b"%s%d" % (_gen_file_id_suffix, _gen_file_id_serial)
67
69
def gen_file_id(name):
70
72
The uniqueness is supplied from _next_id_suffix.
74
if isinstance(name, text_type):
75
name = name.encode('ascii', 'replace')
72
76
# The real randomness is in the _next_id_suffix, the
73
77
# rest of the identifier is just to be nice.
81
85
# 4) Removing starting '.' characters to prevent the file ids from
82
86
# being considered hidden.
83
ascii_word_only = str(_file_id_chars_re.sub('', name.lower()))
84
short_no_dots = ascii_word_only.lstrip('.')[:20]
87
ascii_word_only = _file_id_chars_re.sub(b'', name.lower())
88
short_no_dots = ascii_word_only.lstrip(b'.')[:20]
85
89
return short_no_dots + _next_id_suffix()
93
97
def gen_revision_id(username, timestamp=None):
94
98
"""Return new revision-id.
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,
100
:param username: The username of the committer, in the format returned by
101
config.username(). This is typically a real name, followed by an
102
email address. If found, we will use just the email address portion.
103
Otherwise we flatten the real name, and use that.
100
104
:return: A new revision id.
103
107
user_or_email = config.extract_email_address(username)
104
except errors.NoEmailInUsername:
108
except config.NoEmailInUsername:
105
109
user_or_email = username
107
111
user_or_email = user_or_email.lower()
108
112
user_or_email = user_or_email.replace(' ', '_')
109
user_or_email = _rev_id_chars_re.sub('', user_or_email)
113
user_or_email = _rev_id_chars_re.sub(b'', user_or_email.encode('utf-8'))
111
115
# This gives 36^16 ~= 2^82.7 ~= 83 bits of entropy
112
unique_chunk = osutils.rand_chars(16)
116
unique_chunk = osutils.rand_chars(16).encode('utf-8')
114
118
if timestamp is None:
115
119
timestamp = time.time()
117
rev_id = u'-'.join((user_or_email,
118
osutils.compact_date(timestamp),
121
rev_id = b'-'.join((user_or_email,
122
osutils.compact_date(timestamp).encode('utf-8'),
120
return rev_id.encode('utf8')