/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5752.3.8 by John Arbash Meinel
Merge bzr.dev 5764 to resolve release-notes (aka NEWS) conflicts
1
# Copyright (C) 2006, 2007, 2009, 2010, 2011 Canonical Ltd
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
16
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
17
"""Common code for generating file or revision ids."""
18
6379.6.3 by Jelmer Vernooij
Use absolute_import.
19
from __future__ import absolute_import
20
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
21
from .lazy_import import lazy_import
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
22
lazy_import(globals(), """
23
import time
24
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
25
from breezy import (
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
26
    config,
27
    osutils,
28
    )
29
""")
30
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
31
from . import (
6737 by Jelmer Vernooij
Merge lp:~jelmer/brz/move-errors-config.
32
    errors,
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
33
    lazy_regex,
34
    )
7058.4.27 by Jelmer Vernooij
File id generation handling.
35
from .sixish import text_type
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
36
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
37
# the regex removes any weird characters; we don't escape them
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
38
# but rather just pull them out
7058.4.19 by Jelmer Vernooij
Fix generate_ids tests.
39
_file_id_chars_re = lazy_regex.lazy_compile(b'[^\\w.]')
40
_rev_id_chars_re = lazy_regex.lazy_compile(b'[^-\\w.+@]')
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
41
_gen_file_id_suffix = None
42
_gen_file_id_serial = 0
43
44
45
def _next_id_suffix():
46
    """Create a new file id suffix that is reasonably unique.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
47
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
48
    On the first call we combine the current time with 64 bits of randomness to
49
    give a highly probably globally unique number. Then each call in the same
50
    process adds 1 to a serial number we append to that unique value.
51
    """
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
52
    # XXX TODO: change breezy.add.smart_add_tree to call workingtree.add() rather
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
53
    # than having to move the id randomness out of the inner loop like this.
54
    # XXX TODO: for the global randomness this uses we should add the thread-id
55
    # before the serial #.
56
    # XXX TODO: jam 20061102 I think it would be good to reset every 100 or
57
    #           1000 calls, or perhaps if time.time() increases by a certain
58
    #           amount. time.time() shouldn't be terribly expensive to call,
59
    #           and it means that long-lived processes wouldn't use the same
60
    #           suffix forever.
61
    global _gen_file_id_suffix, _gen_file_id_serial
62
    if _gen_file_id_suffix is None:
6973.12.1 by Jelmer Vernooij
Fix more tests.
63
        _gen_file_id_suffix = ("-%s-%s-" % (
6677.1.1 by Martin
Go back to native str for urls and many other py3 changes
64
                osutils.compact_date(time.time()), osutils.rand_chars(16))
65
            ).encode("ascii")
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
66
    _gen_file_id_serial += 1
6677.1.1 by Martin
Go back to native str for urls and many other py3 changes
67
    return b"%s%d" % (_gen_file_id_suffix, _gen_file_id_serial)
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
68
69
70
def gen_file_id(name):
71
    """Return new file id for the basename 'name'.
72
73
    The uniqueness is supplied from _next_id_suffix.
74
    """
7058.4.27 by Jelmer Vernooij
File id generation handling.
75
    if isinstance(name, text_type):
76
        name = name.encode('ascii', 'replace')
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
77
    # The real randomness is in the _next_id_suffix, the
78
    # rest of the identifier is just to be nice.
79
    # So we:
80
    # 1) Remove non-ascii word characters to keep the ids portable
81
    # 2) squash to lowercase, so the file id doesn't have to
82
    #    be escaped (case insensitive filesystems would bork for ids
83
    #    that only differ in case without escaping).
84
    # 3) truncate the filename to 20 chars. Long filenames also bork on some
85
    #    filesystems
86
    # 4) Removing starting '.' characters to prevent the file ids from
87
    #    being considered hidden.
7058.4.27 by Jelmer Vernooij
File id generation handling.
88
    ascii_word_only = _file_id_chars_re.sub(b'', name.lower())
6677.1.1 by Martin
Go back to native str for urls and many other py3 changes
89
    short_no_dots = ascii_word_only.lstrip(b'.')[:20]
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
90
    return short_no_dots + _next_id_suffix()
91
92
93
def gen_root_id():
94
    """Return a new tree-root file id."""
95
    return gen_file_id('tree_root')
96
97
98
def gen_revision_id(username, timestamp=None):
99
    """Return new revision-id.
100
5050.18.1 by Aaron Bentley
CommitBuilder user committer, not username in revision-id.
101
    :param username: The username of the committer, in the format returned by
102
        config.username().  This is typically a real name, followed by an
103
        email address. If found, we will use just the email address portion.
104
        Otherwise we flatten the real name, and use that.
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
105
    :return: A new revision id.
106
    """
107
    try:
108
        user_or_email = config.extract_email_address(username)
6737 by Jelmer Vernooij
Merge lp:~jelmer/brz/move-errors-config.
109
    except config.NoEmailInUsername:
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
110
        user_or_email = username
111
112
    user_or_email = user_or_email.lower()
113
    user_or_email = user_or_email.replace(' ', '_')
7058.4.19 by Jelmer Vernooij
Fix generate_ids tests.
114
    user_or_email = _rev_id_chars_re.sub(b'', user_or_email.encode('utf-8'))
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
115
116
    # This gives 36^16 ~= 2^82.7 ~= 83 bits of entropy
7058.4.19 by Jelmer Vernooij
Fix generate_ids tests.
117
    unique_chunk = osutils.rand_chars(16).encode('utf-8')
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
118
119
    if timestamp is None:
120
        timestamp = time.time()
121
7058.4.19 by Jelmer Vernooij
Fix generate_ids tests.
122
    rev_id = b'-'.join((user_or_email,
123
                        osutils.compact_date(timestamp).encode('utf-8'),
2249.5.13 by John Arbash Meinel
Finish auditing Repository, and fix generate_ids to always generate utf8 ids.
124
                        unique_chunk))
7058.4.19 by Jelmer Vernooij
Fix generate_ids tests.
125
    return rev_id