reviewboard: allow fake ids file path to be passed in; r=dminor
So we can eventually re-use this function from Git.
MozReview-Commit-ID: Ljspt79gOA8
--- a/hgext/reviewboard/hgrb/util.py
+++ b/hgext/reviewboard/hgrb/util.py
@@ -1,11 +1,12 @@
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
+import errno
import random
import time
from mercurial import util
from mercurial.i18n import _
class ReviewID(object):
"""Represents a parsed review identifier."""
@@ -58,17 +59,17 @@ class ReviewID(object):
return s
BASE62_CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
# (datetime.datetime(2000, 1, 1, 0, 0, 0, 0) - datetime.datetime.utcfromtimestamp(0)).total_seconds()
EPOCH = 946684800
-def genid(repo):
+def genid(repo=None, fakeidpath=None):
"""Generate a unique identifier.
Unique identifiers are treated as a black box. But under the hood, they
consist of a time component and a random component.
Each identifier is up to 64 bits. The first 32 bits are random. The
final 32 bits are integer seconds since midnight UTC on January 1, 2000. We
don't use UNIX epoch because dates from the 70's aren't interesting to us.
@@ -81,26 +82,38 @@ def genid(repo):
Base62 is used as the encoding mechanism because it is safe for both
URLs and revsets. We could get base66 for URLs, but the characters
-~. could conflict with revsets.
"""
# Provide a backdoor to generate deterministic IDs. This is used for
# testing purposes because tests want constant output. And since
# commit IDs go into the commit and are part of the SHA-1, they need
# to be deterministic.
- if repo.ui.configbool('reviewboard', 'fakeids'):
- data = repo.vfs.tryread('genid')
+ if repo and repo.ui.configbool('reviewboard', 'fakeids'):
+ fakeidpath = repo.vfs.join('genid')
+
+ if fakeidpath:
+ try:
+ with open(fakeidpath, 'rb') as fh:
+ data = fh.read()
+ except IOError as e:
+ if e.errno != errno.ENOENT:
+ raise
+
+ data = None
+
if data:
n = int(data)
else:
n = 0
seconds = EPOCH
rnd = n
- repo.vfs.write('genid', str(n + 1))
+ with open(fakeidpath, 'wb') as fh:
+ fh.write(str(n + 1))
else:
now = int(time.time())
# May 5, 2015 sometime.
if now < 1430860700:
raise util.Abort('your system clock is wrong; fix your system '
'clock')
seconds = now - EPOCH
rnd = random.SystemRandom().getrandbits(32)