15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22
from bzrlib.trace import mutter, note
22
from bzrlib.osutils import isdir, quotefn, compact_date, rand_bytes, splitpath, \
23
from bzrlib.osutils import isdir, quotefn, compact_date, rand_bytes, \
23
25
sha_file, appendpath, file_kind
24
from bzrlib.errors import BzrError
26
from bzrlib.errors import BzrError, InvalidRevisionNumber, InvalidRevisionId
28
from bzrlib.textui import show_status
29
from bzrlib.revision import Revision
30
from bzrlib.xml import unpack_xml
31
from bzrlib.delta import compare_trees
32
from bzrlib.tree import EmptyTree, RevisionTree
26
34
BZR_BRANCH_FORMAT = "Bazaar-NG branch, format 0.0.4\n"
27
35
## TODO: Maybe include checks for common corruption of newlines, etc?
38
# TODO: Some operations like log might retrieve the same revisions
39
# repeatedly to calculate deltas. We could perhaps have a weakref
40
# cache in memory to make this faster.
42
# TODO: please move the revision-string syntax stuff out of the branch
43
# object; it's clutter
31
46
def find_branch(f, **args):
32
47
if f and (f.startswith('http://') or f.startswith('https://')):
597
def get_revision_xml(self, revision_id):
598
"""Return XML file object for revision object."""
599
if not revision_id or not isinstance(revision_id, basestring):
600
raise InvalidRevisionId(revision_id)
605
return self.revision_store[revision_id]
607
raise bzrlib.errors.NoSuchRevision(self, revision_id)
562
612
def get_revision(self, revision_id):
563
613
"""Return the Revision object for a named revision"""
564
from bzrlib.revision import Revision
565
from bzrlib.xml import unpack_xml
614
xml_file = self.get_revision_xml(revision_id)
569
if not revision_id or not isinstance(revision_id, basestring):
570
raise ValueError('invalid revision-id: %r' % revision_id)
571
r = unpack_xml(Revision, self.revision_store[revision_id])
617
r = unpack_xml(Revision, xml_file)
618
except SyntaxError, e:
619
raise bzrlib.errors.BzrError('failed to unpack revision_xml',
575
623
assert r.revision_id == revision_id
627
def get_revision_delta(self, revno):
628
"""Return the delta for one revision.
630
The delta is relative to its mainline predecessor, or the
631
empty tree for revision 1.
633
assert isinstance(revno, int)
634
rh = self.revision_history()
635
if not (1 <= revno <= len(rh)):
636
raise InvalidRevisionNumber(revno)
638
# revno is 1-based; list is 0-based
640
new_tree = self.revision_tree(rh[revno-1])
642
old_tree = EmptyTree()
644
old_tree = self.revision_tree(rh[revno-2])
646
return compare_trees(old_tree, new_tree)
579
650
def get_revision_sha1(self, revision_id):
836
887
commit(self, *args, **kw)
839
def lookup_revision(self, revno):
840
"""Return revision hash for revision number."""
845
# list is 0-based; revisions are 1-based
846
return self.revision_history()[revno-1]
848
raise BzrError("no such revision %s" % revno)
890
def lookup_revision(self, revision):
891
"""Return the revision identifier for a given revision information."""
892
revno, info = self.get_revision_info(revision)
895
def get_revision_info(self, revision):
896
"""Return (revno, revision id) for revision identifier.
898
revision can be an integer, in which case it is assumed to be revno (though
899
this will translate negative values into positive ones)
900
revision can also be a string, in which case it is parsed for something like
901
'date:' or 'revid:' etc.
906
try:# Convert to int if possible
907
revision = int(revision)
910
revs = self.revision_history()
911
if isinstance(revision, int):
914
# Mabye we should do this first, but we don't need it if revision == 0
916
revno = len(revs) + revision + 1
919
elif isinstance(revision, basestring):
920
for prefix, func in Branch.REVISION_NAMESPACES.iteritems():
921
if revision.startswith(prefix):
922
revno = func(self, revs, revision)
925
raise BzrError('No namespace registered for string: %r' % revision)
927
if revno is None or revno <= 0 or revno > len(revs):
928
raise BzrError("no such revision %s" % revision)
929
return revno, revs[revno-1]
931
def _namespace_revno(self, revs, revision):
932
"""Lookup a revision by revision number"""
933
assert revision.startswith('revno:')
935
return int(revision[6:])
938
REVISION_NAMESPACES['revno:'] = _namespace_revno
940
def _namespace_revid(self, revs, revision):
941
assert revision.startswith('revid:')
943
return revs.index(revision[6:]) + 1
946
REVISION_NAMESPACES['revid:'] = _namespace_revid
948
def _namespace_last(self, revs, revision):
949
assert revision.startswith('last:')
951
offset = int(revision[5:])
956
raise BzrError('You must supply a positive value for --revision last:XXX')
957
return len(revs) - offset + 1
958
REVISION_NAMESPACES['last:'] = _namespace_last
960
def _namespace_tag(self, revs, revision):
961
assert revision.startswith('tag:')
962
raise BzrError('tag: namespace registered, but not implemented.')
963
REVISION_NAMESPACES['tag:'] = _namespace_tag
965
def _namespace_date(self, revs, revision):
966
assert revision.startswith('date:')
968
# Spec for date revisions:
970
# value can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
971
# it can also start with a '+/-/='. '+' says match the first
972
# entry after the given date. '-' is match the first entry before the date
973
# '=' is match the first entry after, but still on the given date.
975
# +2005-05-12 says find the first matching entry after May 12th, 2005 at 0:00
976
# -2005-05-12 says find the first matching entry before May 12th, 2005 at 0:00
977
# =2005-05-12 says find the first match after May 12th, 2005 at 0:00 but before
978
# May 13th, 2005 at 0:00
980
# So the proper way of saying 'give me all entries for today' is:
981
# -r {date:+today}:{date:-tomorrow}
982
# The default is '=' when not supplied
985
if val[:1] in ('+', '-', '='):
986
match_style = val[:1]
989
today = datetime.datetime.today().replace(hour=0,minute=0,second=0,microsecond=0)
990
if val.lower() == 'yesterday':
991
dt = today - datetime.timedelta(days=1)
992
elif val.lower() == 'today':
994
elif val.lower() == 'tomorrow':
995
dt = today + datetime.timedelta(days=1)
998
# This should be done outside the function to avoid recompiling it.
999
_date_re = re.compile(
1000
r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
1002
r'(?P<time>(?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d))?)?'
1004
m = _date_re.match(val)
1005
if not m or (not m.group('date') and not m.group('time')):
1006
raise BzrError('Invalid revision date %r' % revision)
1009
year, month, day = int(m.group('year')), int(m.group('month')), int(m.group('day'))
1011
year, month, day = today.year, today.month, today.day
1013
hour = int(m.group('hour'))
1014
minute = int(m.group('minute'))
1015
if m.group('second'):
1016
second = int(m.group('second'))
1020
hour, minute, second = 0,0,0
1022
dt = datetime.datetime(year=year, month=month, day=day,
1023
hour=hour, minute=minute, second=second)
1027
if match_style == '-':
1029
elif match_style == '=':
1030
last = dt + datetime.timedelta(days=1)
1033
for i in range(len(revs)-1, -1, -1):
1034
r = self.get_revision(revs[i])
1035
# TODO: Handle timezone.
1036
dt = datetime.datetime.fromtimestamp(r.timestamp)
1037
if first >= dt and (last is None or dt >= last):
1040
for i in range(len(revs)):
1041
r = self.get_revision(revs[i])
1042
# TODO: Handle timezone.
1043
dt = datetime.datetime.fromtimestamp(r.timestamp)
1044
if first <= dt and (last is None or dt <= last):
1046
REVISION_NAMESPACES['date:'] = _namespace_date
851
1048
def revision_tree(self, revision_id):
852
1049
"""Return Tree for a revision on this branch.
854
1051
`revision_id` may be None for the null revision, in which case
855
1052
an `EmptyTree` is returned."""
856
from bzrlib.tree import EmptyTree, RevisionTree
857
1053
# TODO: refactor this to use an existing revision object
858
1054
# so we don't need to read it in twice.
859
1055
if revision_id == None: