annotate hgsubversion/maps.py @ 1558:ae572c9be4e6

cleanup: remove lots of dead imports and code found by pyflakes
author Augie Fackler <raf@durin42.com>
date Sat, 24 Mar 2018 17:04:19 -0400
parents cff81f35b31e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
307
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
1 ''' Module for self-contained maps. '''
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
2
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
3 import collections
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
4 import contextlib
889
7a98fbadcae9 revsets: huge speedups for fromsvn and svnrev
Bryan O'Sullivan <bryano@fb.com>
parents: 847
diff changeset
5 import errno
307
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
6 import os
1394
c4055968f030 maps: use regex for better comment handling
Sean Farley <sean.michael.farley@gmail.com>
parents: 1393
diff changeset
7 import re
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
8 import sqlite3
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
9 import sys
1439
ab15749252b0 TagMap: stop automagically running 'hg svn rebuildmeta'
Augie Fackler <raf@durin42.com>
parents: 1438
diff changeset
10 from mercurial import error
307
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
11 from mercurial import util as hgutil
1253
c54214bb6c4e maps: avoid O(n) property lookups on the node module
Siddharth Agarwal <sid0@fb.com>
parents: 1252
diff changeset
12 from mercurial.node import bin, hex, nullid
307
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
13
1374
a17d8874a099 Added dynamic author mapping.
Jerome M. BERGER <jeberger@free.fr>
parents: 1356
diff changeset
14 import subprocess
829
5061640fe5bc revmap: load/save _youngest using new load_string and save_string API
Yonggang Luo <luoyonggang@gmail.com>
parents: 826
diff changeset
15 import util
453
bb612e625be6 tags: handle copyfrom old versions of tags more correctly
Augie Fackler <durin42@gmail.com>
parents: 448
diff changeset
16
1383
73c76f99ca08 maps: add a basemap class
Sean Farley <sean.michael.farley@gmail.com>
parents: 1382
diff changeset
17 class BaseMap(dict):
73c76f99ca08 maps: add a basemap class
Sean Farley <sean.michael.farley@gmail.com>
parents: 1382
diff changeset
18 '''A base class for the different type of mappings: author, branch, and
73c76f99ca08 maps: add a basemap class
Sean Farley <sean.michael.farley@gmail.com>
parents: 1382
diff changeset
19 tags.'''
1441
e79ff1a85938 BaseMap: no longer take a meta as an argument
Augie Fackler <raf@durin42.com>
parents: 1440
diff changeset
20 def __init__(self, ui, filepath):
1383
73c76f99ca08 maps: add a basemap class
Sean Farley <sean.michael.farley@gmail.com>
parents: 1382
diff changeset
21 super(BaseMap, self).__init__()
1441
e79ff1a85938 BaseMap: no longer take a meta as an argument
Augie Fackler <raf@durin42.com>
parents: 1440
diff changeset
22 self._ui = ui
1383
73c76f99ca08 maps: add a basemap class
Sean Farley <sean.michael.farley@gmail.com>
parents: 1382
diff changeset
23
1394
c4055968f030 maps: use regex for better comment handling
Sean Farley <sean.michael.farley@gmail.com>
parents: 1393
diff changeset
24 self._commentre = re.compile(r'((^|[^\\])(\\\\)*)#.*')
1401
70cb6ba038fa maps: add ability to parse a regex or glob
Sean Farley <sean.michael.farley@gmail.com>
parents: 1400
diff changeset
25 self.syntaxes = ('re', 'glob')
1394
c4055968f030 maps: use regex for better comment handling
Sean Farley <sean.michael.farley@gmail.com>
parents: 1393
diff changeset
26
1441
e79ff1a85938 BaseMap: no longer take a meta as an argument
Augie Fackler <raf@durin42.com>
parents: 1440
diff changeset
27 self._filepath = filepath
e79ff1a85938 BaseMap: no longer take a meta as an argument
Augie Fackler <raf@durin42.com>
parents: 1440
diff changeset
28 self.load(filepath)
1383
73c76f99ca08 maps: add a basemap class
Sean Farley <sean.michael.farley@gmail.com>
parents: 1382
diff changeset
29
1440
4d3a51e82147 BaseMap: extract filename attribute magic into a classmethod
Augie Fackler <raf@durin42.com>
parents: 1439
diff changeset
30 # Append mappings specified from the commandline. A little
4d3a51e82147 BaseMap: extract filename attribute magic into a classmethod
Augie Fackler <raf@durin42.com>
parents: 1439
diff changeset
31 # magic here: our name in the config mapping is the same as
4d3a51e82147 BaseMap: extract filename attribute magic into a classmethod
Augie Fackler <raf@durin42.com>
parents: 1439
diff changeset
32 # the class name lowercased.
4d3a51e82147 BaseMap: extract filename attribute magic into a classmethod
Augie Fackler <raf@durin42.com>
parents: 1439
diff changeset
33 clmap = util.configpath(self._ui, self.mapname())
1383
73c76f99ca08 maps: add a basemap class
Sean Farley <sean.michael.farley@gmail.com>
parents: 1382
diff changeset
34 if clmap:
73c76f99ca08 maps: add a basemap class
Sean Farley <sean.michael.farley@gmail.com>
parents: 1382
diff changeset
35 self.load(clmap)
73c76f99ca08 maps: add a basemap class
Sean Farley <sean.michael.farley@gmail.com>
parents: 1382
diff changeset
36
1440
4d3a51e82147 BaseMap: extract filename attribute magic into a classmethod
Augie Fackler <raf@durin42.com>
parents: 1439
diff changeset
37 @classmethod
4d3a51e82147 BaseMap: extract filename attribute magic into a classmethod
Augie Fackler <raf@durin42.com>
parents: 1439
diff changeset
38 def mapname(cls):
4d3a51e82147 BaseMap: extract filename attribute magic into a classmethod
Augie Fackler <raf@durin42.com>
parents: 1439
diff changeset
39 return cls.__name__.lower()
4d3a51e82147 BaseMap: extract filename attribute magic into a classmethod
Augie Fackler <raf@durin42.com>
parents: 1439
diff changeset
40
1395
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
41 def _findkey(self, key):
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
42 '''Takes a string and finds the first corresponding key that matches
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
43 via regex'''
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
44 if not key:
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
45 return None
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
46
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
47 # compile a new regex key if we're given a string; can't use
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
48 # hgutil.compilere since we need regex.sub
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
49 k = key
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
50 if isinstance(key, str):
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
51 k = re.compile(re.escape(key))
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
52
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
53 # preference goes to matching the exact pattern, i.e. 'foo' should
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
54 # first match 'foo' before trying regexes
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
55 for regex in self:
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
56 if regex.pattern == k.pattern:
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
57 return regex
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
58
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
59 # if key isn't a string, then we are done; nothing matches
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
60 if not isinstance(key, str):
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
61 return None
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
62
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
63 # now we test the regex; the above loop will be faster and is
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
64 # equivalent to not having regexes (i.e. just doing string compares)
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
65 for regex in self:
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
66 if regex.search(key):
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
67 return regex
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
68 return None
53184be1b1fd maps: add _findkey method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1394
diff changeset
69
1396
77594c88d91f maps: add custom get method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1395
diff changeset
70 def get(self, key, default=None):
77594c88d91f maps: add custom get method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1395
diff changeset
71 '''Similar to dict.get, except we use our own matcher, _findkey.'''
77594c88d91f maps: add custom get method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1395
diff changeset
72 if self._findkey(key):
77594c88d91f maps: add custom get method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1395
diff changeset
73 return self[key]
77594c88d91f maps: add custom get method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1395
diff changeset
74 return default
77594c88d91f maps: add custom get method
Sean Farley <sean.michael.farley@gmail.com>
parents: 1395
diff changeset
75
1397
304fdb9810a6 maps: add custom __getitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1396
diff changeset
76 def __getitem__(self, key):
304fdb9810a6 maps: add custom __getitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1396
diff changeset
77 '''Similar to dict.get, except we use our own matcher, _findkey. If the key is
304fdb9810a6 maps: add custom __getitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1396
diff changeset
78 a string, then we can use our regex matching to map its value.
304fdb9810a6 maps: add custom __getitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1396
diff changeset
79 '''
304fdb9810a6 maps: add custom __getitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1396
diff changeset
80 k = self._findkey(key)
304fdb9810a6 maps: add custom __getitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1396
diff changeset
81 val = super(BaseMap, self).__getitem__(k)
304fdb9810a6 maps: add custom __getitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1396
diff changeset
82
304fdb9810a6 maps: add custom __getitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1396
diff changeset
83 # if key is a string then we can transform it using our regex, else we
304fdb9810a6 maps: add custom __getitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1396
diff changeset
84 # don't have enough information, so we just return the val
304fdb9810a6 maps: add custom __getitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1396
diff changeset
85 if isinstance(key, str):
304fdb9810a6 maps: add custom __getitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1396
diff changeset
86 val = k.sub(val, key)
304fdb9810a6 maps: add custom __getitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1396
diff changeset
87
304fdb9810a6 maps: add custom __getitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1396
diff changeset
88 return val
304fdb9810a6 maps: add custom __getitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1396
diff changeset
89
1398
75745298d99d maps: add custom __setitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1397
diff changeset
90 def __setitem__(self, key, value):
75745298d99d maps: add custom __setitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1397
diff changeset
91 '''Similar to dict.__setitem__, except we compile the string into a regex, if
75745298d99d maps: add custom __setitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1397
diff changeset
92 need be.
75745298d99d maps: add custom __setitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1397
diff changeset
93 '''
75745298d99d maps: add custom __setitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1397
diff changeset
94 # try to find the regex already in the map
75745298d99d maps: add custom __setitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1397
diff changeset
95 k = self._findkey(key)
75745298d99d maps: add custom __setitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1397
diff changeset
96 # if we found one, then use it
75745298d99d maps: add custom __setitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1397
diff changeset
97 if k:
75745298d99d maps: add custom __setitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1397
diff changeset
98 key = k
75745298d99d maps: add custom __setitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1397
diff changeset
99 # else make a new regex
75745298d99d maps: add custom __setitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1397
diff changeset
100 if isinstance(key, str):
75745298d99d maps: add custom __setitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1397
diff changeset
101 key = re.compile(re.escape(key))
75745298d99d maps: add custom __setitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1397
diff changeset
102 super(BaseMap, self).__setitem__(key, value)
75745298d99d maps: add custom __setitem__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1397
diff changeset
103
1399
3b96075bffa7 maps: add custom __contains__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1398
diff changeset
104 def __contains__(self, key):
3b96075bffa7 maps: add custom __contains__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1398
diff changeset
105 '''Similar to dict.get, except we use our own matcher, _findkey.'''
3b96075bffa7 maps: add custom __contains__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1398
diff changeset
106 return self._findkey(key) is not None
3b96075bffa7 maps: add custom __contains__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1398
diff changeset
107
1384
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
108 def load(self, path):
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
109 '''Load mappings from a file at the specified path.'''
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
110 path = os.path.expandvars(path)
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
111 if not os.path.exists(path):
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
112 return
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
113
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
114 writing = False
1431
066c08918060 BaseMap: record filename on self._filename
Augie Fackler <raf@durin42.com>
parents: 1430
diff changeset
115 mapfile = self._filepath
1384
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
116 if path != mapfile:
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
117 writing = open(mapfile, 'a')
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
118
1440
4d3a51e82147 BaseMap: extract filename attribute magic into a classmethod
Augie Fackler <raf@durin42.com>
parents: 1439
diff changeset
119 self._ui.debug('reading %s from %s\n' % (self.mapname() , path))
1384
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
120 f = open(path, 'r')
1401
70cb6ba038fa maps: add ability to parse a regex or glob
Sean Farley <sean.michael.farley@gmail.com>
parents: 1400
diff changeset
121 syntax = ''
1384
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
122 for number, line in enumerate(f):
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
123
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
124 if writing:
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
125 writing.write(line)
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
126
1394
c4055968f030 maps: use regex for better comment handling
Sean Farley <sean.michael.farley@gmail.com>
parents: 1393
diff changeset
127 # strip out comments
c4055968f030 maps: use regex for better comment handling
Sean Farley <sean.michael.farley@gmail.com>
parents: 1393
diff changeset
128 if "#" in line:
c4055968f030 maps: use regex for better comment handling
Sean Farley <sean.michael.farley@gmail.com>
parents: 1393
diff changeset
129 # remove comments prefixed by an even number of escapes
c4055968f030 maps: use regex for better comment handling
Sean Farley <sean.michael.farley@gmail.com>
parents: 1393
diff changeset
130 line = self._commentre.sub(r'\1', line)
c4055968f030 maps: use regex for better comment handling
Sean Farley <sean.michael.farley@gmail.com>
parents: 1393
diff changeset
131 # fixup properly escaped comments that survived the above
c4055968f030 maps: use regex for better comment handling
Sean Farley <sean.michael.farley@gmail.com>
parents: 1393
diff changeset
132 line = line.replace("\\#", "#")
c4055968f030 maps: use regex for better comment handling
Sean Farley <sean.michael.farley@gmail.com>
parents: 1393
diff changeset
133 line = line.rstrip()
c4055968f030 maps: use regex for better comment handling
Sean Farley <sean.michael.farley@gmail.com>
parents: 1393
diff changeset
134 if not line:
1384
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
135 continue
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
136
1401
70cb6ba038fa maps: add ability to parse a regex or glob
Sean Farley <sean.michael.farley@gmail.com>
parents: 1400
diff changeset
137 if line.startswith('syntax:'):
70cb6ba038fa maps: add ability to parse a regex or glob
Sean Farley <sean.michael.farley@gmail.com>
parents: 1400
diff changeset
138 s = line[7:].strip()
70cb6ba038fa maps: add ability to parse a regex or glob
Sean Farley <sean.michael.farley@gmail.com>
parents: 1400
diff changeset
139 syntax = ''
70cb6ba038fa maps: add ability to parse a regex or glob
Sean Farley <sean.michael.farley@gmail.com>
parents: 1400
diff changeset
140 if s in self.syntaxes:
70cb6ba038fa maps: add ability to parse a regex or glob
Sean Farley <sean.michael.farley@gmail.com>
parents: 1400
diff changeset
141 syntax = s
70cb6ba038fa maps: add ability to parse a regex or glob
Sean Farley <sean.michael.farley@gmail.com>
parents: 1400
diff changeset
142 continue
70cb6ba038fa maps: add ability to parse a regex or glob
Sean Farley <sean.michael.farley@gmail.com>
parents: 1400
diff changeset
143 pat = syntax
70cb6ba038fa maps: add ability to parse a regex or glob
Sean Farley <sean.michael.farley@gmail.com>
parents: 1400
diff changeset
144 for s in self.syntaxes:
70cb6ba038fa maps: add ability to parse a regex or glob
Sean Farley <sean.michael.farley@gmail.com>
parents: 1400
diff changeset
145 if line.startswith(s + ':'):
70cb6ba038fa maps: add ability to parse a regex or glob
Sean Farley <sean.michael.farley@gmail.com>
parents: 1400
diff changeset
146 pat = s
70cb6ba038fa maps: add ability to parse a regex or glob
Sean Farley <sean.michael.farley@gmail.com>
parents: 1400
diff changeset
147 line = line[len(s) + 1:]
70cb6ba038fa maps: add ability to parse a regex or glob
Sean Farley <sean.michael.farley@gmail.com>
parents: 1400
diff changeset
148 break
70cb6ba038fa maps: add ability to parse a regex or glob
Sean Farley <sean.michael.farley@gmail.com>
parents: 1400
diff changeset
149
70cb6ba038fa maps: add ability to parse a regex or glob
Sean Farley <sean.michael.farley@gmail.com>
parents: 1400
diff changeset
150 # split on the first '='
1384
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
151 try:
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
152 src, dst = line.split('=', 1)
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
153 except (IndexError, ValueError):
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
154 msg = 'ignoring line %i in %s %s: %s\n'
1440
4d3a51e82147 BaseMap: extract filename attribute magic into a classmethod
Augie Fackler <raf@durin42.com>
parents: 1439
diff changeset
155 self._ui.status(msg % (number, self.mapname(), path,
1384
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
156 line.rstrip()))
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
157 continue
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
158
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
159 src = src.strip()
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
160 dst = dst.strip()
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
161
1401
70cb6ba038fa maps: add ability to parse a regex or glob
Sean Farley <sean.michael.farley@gmail.com>
parents: 1400
diff changeset
162 if pat != 're':
70cb6ba038fa maps: add ability to parse a regex or glob
Sean Farley <sean.michael.farley@gmail.com>
parents: 1400
diff changeset
163 src = re.escape(src)
70cb6ba038fa maps: add ability to parse a regex or glob
Sean Farley <sean.michael.farley@gmail.com>
parents: 1400
diff changeset
164 if pat == 'glob':
70cb6ba038fa maps: add ability to parse a regex or glob
Sean Farley <sean.michael.farley@gmail.com>
parents: 1400
diff changeset
165 src = src.replace('\\*', '.*')
70cb6ba038fa maps: add ability to parse a regex or glob
Sean Farley <sean.michael.farley@gmail.com>
parents: 1400
diff changeset
166 src = re.compile(src)
70cb6ba038fa maps: add ability to parse a regex or glob
Sean Farley <sean.michael.farley@gmail.com>
parents: 1400
diff changeset
167
1384
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
168 if src not in self:
1440
4d3a51e82147 BaseMap: extract filename attribute magic into a classmethod
Augie Fackler <raf@durin42.com>
parents: 1439
diff changeset
169 self._ui.debug('adding %s to %s\n' % (src, self.mapname()))
1384
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
170 elif dst != self[src]:
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
171 msg = 'overriding %s: "%s" to "%s" (%s)\n'
1440
4d3a51e82147 BaseMap: extract filename attribute magic into a classmethod
Augie Fackler <raf@durin42.com>
parents: 1439
diff changeset
172 self._ui.status(msg % (self.mapname(), self[src], dst, src))
1384
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
173 self[src] = dst
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
174
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
175 f.close()
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
176 if writing:
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
177 writing.close()
2d1d05e6e46c maps: add a load method to base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1383
diff changeset
178
1385
9139d9295a36 maps: make author map inherit from base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1384
diff changeset
179 class AuthorMap(BaseMap):
307
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
180 '''A mapping from Subversion-style authors to Mercurial-style
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
181 authors, and back. The data is stored persistently on disk.
322
05cd4a5138bf Move some .warn() calls to noisy levels instead.
Augie Fackler <durin42@gmail.com>
parents: 310
diff changeset
182
307
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
183 If the 'hgsubversion.defaultauthors' configuration option is set to false,
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
184 attempting to obtain an unknown author will fail with an Abort.
1188
38dd8721fb0d maps: remove trailing whitespace
Sean Farley <sean.michael.farley@gmail.com>
parents: 1187
diff changeset
185
1097
e015cd34168d authormap: allow case-insensitive authormaps for easier conversions
maugustin
parents: 976
diff changeset
186 If the 'hgsubversion.caseignoreauthors' configuration option is set to true,
e015cd34168d authormap: allow case-insensitive authormaps for easier conversions
maugustin
parents: 976
diff changeset
187 the userid from Subversion is always compared lowercase.
307
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
188 '''
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
189
1442
a0ba38def79b AuthorMap: no longer take a meta
Augie Fackler <raf@durin42.com>
parents: 1441
diff changeset
190 def __init__(self, ui, filepath, defaulthost, caseignoreauthors,
1429
3a723188051e AuthorMap: make local implementation concerns stop using self.meta
Augie Fackler <raf@durin42.com>
parents: 1428
diff changeset
191 mapauthorscmd, defaultauthors):
307
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
192 '''Initialise a new AuthorMap.
322
05cd4a5138bf Move some .warn() calls to noisy levels instead.
Augie Fackler <durin42@gmail.com>
parents: 310
diff changeset
193
307
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
194 The ui argument is used to print diagnostic messages.
322
05cd4a5138bf Move some .warn() calls to noisy levels instead.
Augie Fackler <durin42@gmail.com>
parents: 310
diff changeset
195
307
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
196 The path argument is the location of the backing store,
846
7ca3d1b08d67 Save filemap into .hg/svn/filemap just like other maps
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 829
diff changeset
197 typically .hg/svn/authors.
307
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
198 '''
1429
3a723188051e AuthorMap: make local implementation concerns stop using self.meta
Augie Fackler <raf@durin42.com>
parents: 1428
diff changeset
199 if defaulthost:
3a723188051e AuthorMap: make local implementation concerns stop using self.meta
Augie Fackler <raf@durin42.com>
parents: 1428
diff changeset
200 self.defaulthost = '@%s' % defaulthost.lstrip('@')
3a723188051e AuthorMap: make local implementation concerns stop using self.meta
Augie Fackler <raf@durin42.com>
parents: 1428
diff changeset
201 else:
3a723188051e AuthorMap: make local implementation concerns stop using self.meta
Augie Fackler <raf@durin42.com>
parents: 1428
diff changeset
202 self.defaulthost = ''
3a723188051e AuthorMap: make local implementation concerns stop using self.meta
Augie Fackler <raf@durin42.com>
parents: 1428
diff changeset
203 self._caseignoreauthors = caseignoreauthors
3a723188051e AuthorMap: make local implementation concerns stop using self.meta
Augie Fackler <raf@durin42.com>
parents: 1428
diff changeset
204 self._mapauthorscmd = mapauthorscmd
3a723188051e AuthorMap: make local implementation concerns stop using self.meta
Augie Fackler <raf@durin42.com>
parents: 1428
diff changeset
205 self._defaulthost = defaulthost
3a723188051e AuthorMap: make local implementation concerns stop using self.meta
Augie Fackler <raf@durin42.com>
parents: 1428
diff changeset
206 self._defaultauthors = defaultauthors
1194
49791c40a8a5 maps: change authormap to initialize with an svnmeta object
Sean Farley <sean.michael.farley@gmail.com>
parents: 1193
diff changeset
207
1442
a0ba38def79b AuthorMap: no longer take a meta
Augie Fackler <raf@durin42.com>
parents: 1441
diff changeset
208 super(AuthorMap, self).__init__(ui, filepath)
1193
a55339d35066 maps: load commandline authormap in __init__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1188
diff changeset
209
1400
3e264851f879 maps: protect author map functions from regexes
Sean Farley <sean.michael.farley@gmail.com>
parents: 1399
diff changeset
210 def _lowercase(self, key):
3e264851f879 maps: protect author map functions from regexes
Sean Farley <sean.michael.farley@gmail.com>
parents: 1399
diff changeset
211 '''Determine whether or not to lowercase a str or regex using the
3e264851f879 maps: protect author map functions from regexes
Sean Farley <sean.michael.farley@gmail.com>
parents: 1399
diff changeset
212 meta.caseignoreauthors.'''
3e264851f879 maps: protect author map functions from regexes
Sean Farley <sean.michael.farley@gmail.com>
parents: 1399
diff changeset
213 k = key
1429
3a723188051e AuthorMap: make local implementation concerns stop using self.meta
Augie Fackler <raf@durin42.com>
parents: 1428
diff changeset
214 if self._caseignoreauthors:
1400
3e264851f879 maps: protect author map functions from regexes
Sean Farley <sean.michael.farley@gmail.com>
parents: 1399
diff changeset
215 if isinstance(key, str):
3e264851f879 maps: protect author map functions from regexes
Sean Farley <sean.michael.farley@gmail.com>
parents: 1399
diff changeset
216 k = key.lower()
3e264851f879 maps: protect author map functions from regexes
Sean Farley <sean.michael.farley@gmail.com>
parents: 1399
diff changeset
217 else:
3e264851f879 maps: protect author map functions from regexes
Sean Farley <sean.michael.farley@gmail.com>
parents: 1399
diff changeset
218 k = re.compile(key.pattern.lower())
3e264851f879 maps: protect author map functions from regexes
Sean Farley <sean.michael.farley@gmail.com>
parents: 1399
diff changeset
219 return k
3e264851f879 maps: protect author map functions from regexes
Sean Farley <sean.michael.farley@gmail.com>
parents: 1399
diff changeset
220
1379
367e65989b41 maps: add custom __setitem__ to author map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1375
diff changeset
221 def __setitem__(self, key, value):
367e65989b41 maps: add custom __setitem__ to author map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1375
diff changeset
222 '''Similar to dict.__setitem__, except we check caseignoreauthors to
367e65989b41 maps: add custom __setitem__ to author map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1375
diff changeset
223 use lowercase string or not
367e65989b41 maps: add custom __setitem__ to author map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1375
diff changeset
224 '''
1400
3e264851f879 maps: protect author map functions from regexes
Sean Farley <sean.michael.farley@gmail.com>
parents: 1399
diff changeset
225 super(AuthorMap, self).__setitem__(self._lowercase(key), value)
1379
367e65989b41 maps: add custom __setitem__ to author map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1375
diff changeset
226
1380
332ad9ea579b maps: add custom __contains__ to author map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1379
diff changeset
227 def __contains__(self, key):
332ad9ea579b maps: add custom __contains__ to author map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1379
diff changeset
228 '''Similar to dict.__contains__, except we check caseignoreauthors to
332ad9ea579b maps: add custom __contains__ to author map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1379
diff changeset
229 use lowercase string or not
332ad9ea579b maps: add custom __contains__ to author map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1379
diff changeset
230 '''
1400
3e264851f879 maps: protect author map functions from regexes
Sean Farley <sean.michael.farley@gmail.com>
parents: 1399
diff changeset
231 return super(AuthorMap, self).__contains__(self._lowercase(key))
1380
332ad9ea579b maps: add custom __contains__ to author map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1379
diff changeset
232
307
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
233 def __getitem__(self, author):
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
234 ''' Similar to dict.__getitem__, except in case of an unknown author.
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
235 In such cases, a new value is generated and added to the dictionary
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
236 as well as the backing store. '''
735
c2b9e08ecf10 maps: map a missing author to '(no author)'
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 729
diff changeset
237 if author is None:
c2b9e08ecf10 maps: map a missing author to '(no author)'
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 729
diff changeset
238 author = '(no author)'
1097
e015cd34168d authormap: allow case-insensitive authormaps for easier conversions
maugustin
parents: 976
diff changeset
239
1400
3e264851f879 maps: protect author map functions from regexes
Sean Farley <sean.michael.farley@gmail.com>
parents: 1399
diff changeset
240 if not isinstance(author, str):
3e264851f879 maps: protect author map functions from regexes
Sean Farley <sean.michael.farley@gmail.com>
parents: 1399
diff changeset
241 return super(AuthorMap, self).__getitem__(author)
3e264851f879 maps: protect author map functions from regexes
Sean Farley <sean.michael.farley@gmail.com>
parents: 1399
diff changeset
242
1196
878372849175 maps: use meta.caseignoreauthors intead of accessing ui directly
Sean Farley <sean.michael.farley@gmail.com>
parents: 1195
diff changeset
243 search_author = author
1429
3a723188051e AuthorMap: make local implementation concerns stop using self.meta
Augie Fackler <raf@durin42.com>
parents: 1428
diff changeset
244 if self._caseignoreauthors:
1097
e015cd34168d authormap: allow case-insensitive authormaps for easier conversions
maugustin
parents: 976
diff changeset
245 search_author = author.lower()
e015cd34168d authormap: allow case-insensitive authormaps for easier conversions
maugustin
parents: 976
diff changeset
246
1374
a17d8874a099 Added dynamic author mapping.
Jerome M. BERGER <jeberger@free.fr>
parents: 1356
diff changeset
247 result = None
1097
e015cd34168d authormap: allow case-insensitive authormaps for easier conversions
maugustin
parents: 976
diff changeset
248 if search_author in self:
1382
d996850ac4e8 maps: call super directly instead of self.super
Sean Farley <sean.michael.farley@gmail.com>
parents: 1381
diff changeset
249 result = super(AuthorMap, self).__getitem__(search_author)
1429
3a723188051e AuthorMap: make local implementation concerns stop using self.meta
Augie Fackler <raf@durin42.com>
parents: 1428
diff changeset
250 elif self._mapauthorscmd:
3a723188051e AuthorMap: make local implementation concerns stop using self.meta
Augie Fackler <raf@durin42.com>
parents: 1428
diff changeset
251 cmd = self._mapauthorscmd % author
1375
abc87a62ff51 maps: remove python2.7ism from dynamic author mapping
Mateusz Kwapich <mitrandir@fb.com>
parents: 1374
diff changeset
252 process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
abc87a62ff51 maps: remove python2.7ism from dynamic author mapping
Mateusz Kwapich <mitrandir@fb.com>
parents: 1374
diff changeset
253 output, err = process.communicate()
abc87a62ff51 maps: remove python2.7ism from dynamic author mapping
Mateusz Kwapich <mitrandir@fb.com>
parents: 1374
diff changeset
254 retcode = process.poll()
abc87a62ff51 maps: remove python2.7ism from dynamic author mapping
Mateusz Kwapich <mitrandir@fb.com>
parents: 1374
diff changeset
255 if retcode:
abc87a62ff51 maps: remove python2.7ism from dynamic author mapping
Mateusz Kwapich <mitrandir@fb.com>
parents: 1374
diff changeset
256 msg = 'map author command "%s" exited with error'
1555
cff81f35b31e cleanup: reference Abort from mercurial.error instead of mercurial.util
Augie Fackler <raf@durin42.com>
parents: 1550
diff changeset
257 raise error.Abort(msg % cmd)
1375
abc87a62ff51 maps: remove python2.7ism from dynamic author mapping
Mateusz Kwapich <mitrandir@fb.com>
parents: 1374
diff changeset
258 self[author] = result = output.strip()
1374
a17d8874a099 Added dynamic author mapping.
Jerome M. BERGER <jeberger@free.fr>
parents: 1356
diff changeset
259 if not result:
1429
3a723188051e AuthorMap: make local implementation concerns stop using self.meta
Augie Fackler <raf@durin42.com>
parents: 1428
diff changeset
260 if self._defaultauthors:
1374
a17d8874a099 Added dynamic author mapping.
Jerome M. BERGER <jeberger@free.fr>
parents: 1356
diff changeset
261 self[author] = result = '%s%s' % (author, self.defaulthost)
a17d8874a099 Added dynamic author mapping.
Jerome M. BERGER <jeberger@free.fr>
parents: 1356
diff changeset
262 msg = 'substituting author "%s" for default "%s"\n'
1428
da272633997f maps: store a direct reference to ui
Augie Fackler <raf@durin42.com>
parents: 1427
diff changeset
263 self._ui.debug(msg % (author, result))
1374
a17d8874a099 Added dynamic author mapping.
Jerome M. BERGER <jeberger@free.fr>
parents: 1356
diff changeset
264 else:
a17d8874a099 Added dynamic author mapping.
Jerome M. BERGER <jeberger@free.fr>
parents: 1356
diff changeset
265 msg = 'author %s has no entry in the author map!'
1555
cff81f35b31e cleanup: reference Abort from mercurial.error instead of mercurial.util
Augie Fackler <raf@durin42.com>
parents: 1550
diff changeset
266 raise error.Abort(msg % author)
1428
da272633997f maps: store a direct reference to ui
Augie Fackler <raf@durin42.com>
parents: 1427
diff changeset
267 self._ui.debug('mapping author "%s" to "%s"\n' % (author, result))
307
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
268 return result
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
269
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
270 def reverselookup(self, author):
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
271 for svnauthor, hgauthor in self.iteritems():
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
272 if author == hgauthor:
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
273 return svnauthor
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
274 else:
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
275 # Mercurial incorrectly splits at e.g. '.', so we roll our own.
1d48d9a34c19 Put authormap into a separate file, and make it much better too.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
diff changeset
276 return author.rsplit('@', 1)[0]
408
f137231f9d30 extract the revmap support into a separate dict-like class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 360
diff changeset
277
f137231f9d30 extract the revmap support into a separate dict-like class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 360
diff changeset
278
728
cfefeefad199 rename TagMap to Tags, to free up the TagMap name
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 725
diff changeset
279 class Tags(dict):
519
247110c633f7 maps: TagMap tags are non-empty strings
Patrick Mezard <pmezard@gmail.com>
parents: 460
diff changeset
280 """Map tags to converted node identifier.
448
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
281
729
467b95348e6a implement tag renames
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 728
diff changeset
282 tag names are non-empty strings. Tags are saved in a file
467b95348e6a implement tag renames
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 728
diff changeset
283 called tagmap, for backwards compatibility reasons.
519
247110c633f7 maps: TagMap tags are non-empty strings
Patrick Mezard <pmezard@gmail.com>
parents: 460
diff changeset
284 """
453
bb612e625be6 tags: handle copyfrom old versions of tags more correctly
Augie Fackler <durin42@gmail.com>
parents: 448
diff changeset
285 VERSION = 2
448
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
286
1445
5dbc6356a0d3 Tags: no longer take a meta
Augie Fackler <raf@durin42.com>
parents: 1444
diff changeset
287 def __init__(self, ui, filepath, endrev=None):
448
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
288 dict.__init__(self)
1445
5dbc6356a0d3 Tags: no longer take a meta
Augie Fackler <raf@durin42.com>
parents: 1444
diff changeset
289 self._filepath = filepath
5dbc6356a0d3 Tags: no longer take a meta
Augie Fackler <raf@durin42.com>
parents: 1444
diff changeset
290 self._ui = ui
725
c787147fa3b7 fix some style nits
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 647
diff changeset
291 self.endrev = endrev
1432
a2ef2c1e3644 TagMap: record filepath explicitly
Augie Fackler <raf@durin42.com>
parents: 1431
diff changeset
292 if os.path.isfile(self._filepath):
1187
30b2139c3931 maps: change tags init to accept svnmeta not a repo
Sean Farley <sean.michael.farley@gmail.com>
parents: 1186
diff changeset
293 self._load()
448
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
294 else:
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
295 self._write()
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
296
1187
30b2139c3931 maps: change tags init to accept svnmeta not a repo
Sean Farley <sean.michael.farley@gmail.com>
parents: 1186
diff changeset
297 def _load(self):
1432
a2ef2c1e3644 TagMap: record filepath explicitly
Augie Fackler <raf@durin42.com>
parents: 1431
diff changeset
298 f = open(self._filepath)
448
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
299 ver = int(f.readline())
453
bb612e625be6 tags: handle copyfrom old versions of tags more correctly
Augie Fackler <durin42@gmail.com>
parents: 448
diff changeset
300 if ver < self.VERSION:
1439
ab15749252b0 TagMap: stop automagically running 'hg svn rebuildmeta'
Augie Fackler <raf@durin42.com>
parents: 1438
diff changeset
301 raise error.Abort(
ab15749252b0 TagMap: stop automagically running 'hg svn rebuildmeta'
Augie Fackler <raf@durin42.com>
parents: 1438
diff changeset
302 'tag map outdated, please run `hg svn rebuildmeta`')
453
bb612e625be6 tags: handle copyfrom old versions of tags more correctly
Augie Fackler <durin42@gmail.com>
parents: 448
diff changeset
303 elif ver != self.VERSION:
1555
cff81f35b31e cleanup: reference Abort from mercurial.error instead of mercurial.util
Augie Fackler <raf@durin42.com>
parents: 1550
diff changeset
304 raise error.Abort('tagmap too new -- please upgrade')
448
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
305 for l in f:
826
8794302f3614 maps: s/hash/ha/ and s/map/m/ to avoid hiding Python builtins
Yonggang Luo <luoyonggang@gmail.com>
parents: 822
diff changeset
306 ha, revision, tag = l.split(' ', 2)
453
bb612e625be6 tags: handle copyfrom old versions of tags more correctly
Augie Fackler <durin42@gmail.com>
parents: 448
diff changeset
307 revision = int(revision)
448
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
308 tag = tag[:-1]
453
bb612e625be6 tags: handle copyfrom old versions of tags more correctly
Augie Fackler <durin42@gmail.com>
parents: 448
diff changeset
309 if self.endrev is not None and revision > self.endrev:
bb612e625be6 tags: handle copyfrom old versions of tags more correctly
Augie Fackler <durin42@gmail.com>
parents: 448
diff changeset
310 break
519
247110c633f7 maps: TagMap tags are non-empty strings
Patrick Mezard <pmezard@gmail.com>
parents: 460
diff changeset
311 if not tag:
247110c633f7 maps: TagMap tags are non-empty strings
Patrick Mezard <pmezard@gmail.com>
parents: 460
diff changeset
312 continue
1253
c54214bb6c4e maps: avoid O(n) property lookups on the node module
Siddharth Agarwal <sid0@fb.com>
parents: 1252
diff changeset
313 dict.__setitem__(self, tag, bin(ha))
448
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
314 f.close()
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
315
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
316 def _write(self):
453
bb612e625be6 tags: handle copyfrom old versions of tags more correctly
Augie Fackler <durin42@gmail.com>
parents: 448
diff changeset
317 assert self.endrev is None
1432
a2ef2c1e3644 TagMap: record filepath explicitly
Augie Fackler <raf@durin42.com>
parents: 1431
diff changeset
318 f = open(self._filepath, 'w')
448
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
319 f.write('%s\n' % self.VERSION)
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
320 f.close()
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
321
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
322 def update(self, other):
725
c787147fa3b7 fix some style nits
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 647
diff changeset
323 for k, v in other.iteritems():
448
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
324 self[k] = v
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
325
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
326 def __contains__(self, tag):
593
eb16630bceb1 maps: fix a % formatting bug
Augie Fackler <durin42@gmail.com>
parents: 579
diff changeset
327 return (tag and dict.__contains__(self, tag)
1253
c54214bb6c4e maps: avoid O(n) property lookups on the node module
Siddharth Agarwal <sid0@fb.com>
parents: 1252
diff changeset
328 and dict.__getitem__(self, tag) != nullid)
448
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
329
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
330 def __getitem__(self, tag):
519
247110c633f7 maps: TagMap tags are non-empty strings
Patrick Mezard <pmezard@gmail.com>
parents: 460
diff changeset
331 if tag and tag in self:
448
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
332 return dict.__getitem__(self, tag)
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
333 raise KeyError()
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
334
453
bb612e625be6 tags: handle copyfrom old versions of tags more correctly
Augie Fackler <durin42@gmail.com>
parents: 448
diff changeset
335 def __setitem__(self, tag, info):
519
247110c633f7 maps: TagMap tags are non-empty strings
Patrick Mezard <pmezard@gmail.com>
parents: 460
diff changeset
336 if not tag:
1555
cff81f35b31e cleanup: reference Abort from mercurial.error instead of mercurial.util
Augie Fackler <raf@durin42.com>
parents: 1550
diff changeset
337 raise error.Abort('tag cannot be empty')
826
8794302f3614 maps: s/hash/ha/ and s/map/m/ to avoid hiding Python builtins
Yonggang Luo <luoyonggang@gmail.com>
parents: 822
diff changeset
338 ha, revision = info
1432
a2ef2c1e3644 TagMap: record filepath explicitly
Augie Fackler <raf@durin42.com>
parents: 1431
diff changeset
339 f = open(self._filepath, 'a')
1253
c54214bb6c4e maps: avoid O(n) property lookups on the node module
Siddharth Agarwal <sid0@fb.com>
parents: 1252
diff changeset
340 f.write('%s %s %s\n' % (hex(ha), revision, tag))
448
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
341 f.close()
826
8794302f3614 maps: s/hash/ha/ and s/map/m/ to avoid hiding Python builtins
Yonggang Luo <luoyonggang@gmail.com>
parents: 822
diff changeset
342 dict.__setitem__(self, tag, ha)
448
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
343
fbc7cf4fd701 tags: reinstate a tag map file in a better way
Augie Fackler <durin42@gmail.com>
parents: 430
diff changeset
344
408
f137231f9d30 extract the revmap support into a separate dict-like class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 360
diff changeset
345 class RevMap(dict):
f137231f9d30 extract the revmap support into a separate dict-like class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 360
diff changeset
346
f137231f9d30 extract the revmap support into a separate dict-like class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 360
diff changeset
347 VERSION = 1
f137231f9d30 extract the revmap support into a separate dict-like class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 360
diff changeset
348
1455
8cfe074cd463 maps: use util.fileproperty for RevMap.lastpulled
Jun Wu <quark@fb.com>
parents: 1449
diff changeset
349 lastpulled = util.fileproperty('_lastpulled', lambda x: x._lastpulled_file,
8cfe074cd463 maps: use util.fileproperty for RevMap.lastpulled
Jun Wu <quark@fb.com>
parents: 1449
diff changeset
350 default=0, deserializer=int)
8cfe074cd463 maps: use util.fileproperty for RevMap.lastpulled
Jun Wu <quark@fb.com>
parents: 1449
diff changeset
351
1446
2eba84031a78 RevMap: no longer take a meta
Augie Fackler <raf@durin42.com>
parents: 1445
diff changeset
352 def __init__(self, revmap_path, lastpulled_path):
408
f137231f9d30 extract the revmap support into a separate dict-like class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 360
diff changeset
353 dict.__init__(self)
1446
2eba84031a78 RevMap: no longer take a meta
Augie Fackler <raf@durin42.com>
parents: 1445
diff changeset
354 self._filepath = revmap_path
2eba84031a78 RevMap: no longer take a meta
Augie Fackler <raf@durin42.com>
parents: 1445
diff changeset
355 self._lastpulled_file = lastpulled_path
1294
9a722b5246df maps: cache hashes() for the revmap
Mateusz Kwapich <mitrandir@fb.com>
parents: 1254
diff changeset
356 self._hashes = None
1468
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
357 # disable iteration to have a consistent interface with SqliteRevMap
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
358 # it's less about performance since RevMap needs iteration internally
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
359 self._allowiter = False
1183
09b20039192c maps: change revmap init to accept svnmeta not a repo
Sean Farley <sean.michael.farley@gmail.com>
parents: 1182
diff changeset
360
1434
0a6b3da6d34c RevMap: move lastpulled from SVNMeta down into RevMap
Augie Fackler <raf@durin42.com>
parents: 1432
diff changeset
361 self.firstpulled = 0
1430
48beb467b2e5 RevMap: use self._filepath instead of using meta
Augie Fackler <raf@durin42.com>
parents: 1429
diff changeset
362 if os.path.isfile(self._filepath):
408
f137231f9d30 extract the revmap support into a separate dict-like class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 360
diff changeset
363 self._load()
f137231f9d30 extract the revmap support into a separate dict-like class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 360
diff changeset
364 else:
f137231f9d30 extract the revmap support into a separate dict-like class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 360
diff changeset
365 self._write()
f137231f9d30 extract the revmap support into a separate dict-like class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 360
diff changeset
366
415
b17b2969861c svnmeta: move revmap methods, make last_known_revision() more efficient
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 409
diff changeset
367 def hashes(self):
1294
9a722b5246df maps: cache hashes() for the revmap
Mateusz Kwapich <mitrandir@fb.com>
parents: 1254
diff changeset
368 if self._hashes is None:
1468
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
369 self._hashes = dict((v, k) for (k, v) in self._origiteritems())
1294
9a722b5246df maps: cache hashes() for the revmap
Mateusz Kwapich <mitrandir@fb.com>
parents: 1254
diff changeset
370 return self._hashes
415
b17b2969861c svnmeta: move revmap methods, make last_known_revision() more efficient
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 409
diff changeset
371
1472
cf79525f507c maps: change branchedits to accept revnum directly
Jun Wu <quark@fb.com>
parents: 1470
diff changeset
372 def branchedits(self, branch, revnum):
cf79525f507c maps: change branchedits to accept revnum directly
Jun Wu <quark@fb.com>
parents: 1470
diff changeset
373 check = lambda x: x[0][1] == branch and x[0][0] < revnum
1468
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
374 return sorted(filter(check, self._origiteritems()), reverse=True)
415
b17b2969861c svnmeta: move revmap methods, make last_known_revision() more efficient
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 409
diff changeset
375
1422
372afb75f465 maps: add the "branchmaxrevnum" method to RevMap
Jun Wu <quark@fb.com>
parents: 1421
diff changeset
376 def branchmaxrevnum(self, branch, maxrevnum):
372afb75f465 maps: add the "branchmaxrevnum" method to RevMap
Jun Wu <quark@fb.com>
parents: 1421
diff changeset
377 result = 0
1468
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
378 for num, br in self._origiterkeys():
1422
372afb75f465 maps: add the "branchmaxrevnum" method to RevMap
Jun Wu <quark@fb.com>
parents: 1421
diff changeset
379 if br == branch and num <= maxrevnum and num > result:
372afb75f465 maps: add the "branchmaxrevnum" method to RevMap
Jun Wu <quark@fb.com>
parents: 1421
diff changeset
380 result = num
372afb75f465 maps: add the "branchmaxrevnum" method to RevMap
Jun Wu <quark@fb.com>
parents: 1421
diff changeset
381 return result
372afb75f465 maps: add the "branchmaxrevnum" method to RevMap
Jun Wu <quark@fb.com>
parents: 1421
diff changeset
382
1419
2e4145e452cd maps: add "lasthash" property to RevMap
Jun Wu <quark@fb.com>
parents: 1414
diff changeset
383 @property
2e4145e452cd maps: add "lasthash" property to RevMap
Jun Wu <quark@fb.com>
parents: 1414
diff changeset
384 def lasthash(self):
1421
0094f222c5dc maps: make readmapfile of RevMap a private instance method
Jun Wu <quark@fb.com>
parents: 1419
diff changeset
385 lines = list(self._readmapfile())
1419
2e4145e452cd maps: add "lasthash" property to RevMap
Jun Wu <quark@fb.com>
parents: 1414
diff changeset
386 if not lines:
2e4145e452cd maps: add "lasthash" property to RevMap
Jun Wu <quark@fb.com>
parents: 1414
diff changeset
387 return None
2e4145e452cd maps: add "lasthash" property to RevMap
Jun Wu <quark@fb.com>
parents: 1414
diff changeset
388 return bin(lines[-1].split(' ', 2)[1])
2e4145e452cd maps: add "lasthash" property to RevMap
Jun Wu <quark@fb.com>
parents: 1414
diff changeset
389
1413
951a87f2f2bd maps: add the "revhashes" method to RevMap
Jun Wu <quark@fb.com>
parents: 1412
diff changeset
390 def revhashes(self, revnum):
1468
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
391 for key, value in self._origiteritems():
1413
951a87f2f2bd maps: add the "revhashes" method to RevMap
Jun Wu <quark@fb.com>
parents: 1412
diff changeset
392 if key[0] == revnum:
951a87f2f2bd maps: add the "revhashes" method to RevMap
Jun Wu <quark@fb.com>
parents: 1412
diff changeset
393 yield value
951a87f2f2bd maps: add the "revhashes" method to RevMap
Jun Wu <quark@fb.com>
parents: 1412
diff changeset
394
1411
025e849d22f0 maps: add the "clear" method to RevMap
Jun Wu <quark@fb.com>
parents: 1401
diff changeset
395 def clear(self):
025e849d22f0 maps: add the "clear" method to RevMap
Jun Wu <quark@fb.com>
parents: 1401
diff changeset
396 self._write()
025e849d22f0 maps: add the "clear" method to RevMap
Jun Wu <quark@fb.com>
parents: 1401
diff changeset
397 dict.clear(self)
025e849d22f0 maps: add the "clear" method to RevMap
Jun Wu <quark@fb.com>
parents: 1401
diff changeset
398 self._hashes = None
025e849d22f0 maps: add the "clear" method to RevMap
Jun Wu <quark@fb.com>
parents: 1401
diff changeset
399
1434
0a6b3da6d34c RevMap: move lastpulled from SVNMeta down into RevMap
Augie Fackler <raf@durin42.com>
parents: 1432
diff changeset
400 def batchset(self, items, lastpulled):
1412
7e98352a37db maps: add the "batchset" method to RevMap
Jun Wu <quark@fb.com>
parents: 1411
diff changeset
401 '''Set items in batches
7e98352a37db maps: add the "batchset" method to RevMap
Jun Wu <quark@fb.com>
parents: 1411
diff changeset
402
7e98352a37db maps: add the "batchset" method to RevMap
Jun Wu <quark@fb.com>
parents: 1411
diff changeset
403 items is an array of (rev num, branch, binary hash)
7e98352a37db maps: add the "batchset" method to RevMap
Jun Wu <quark@fb.com>
parents: 1411
diff changeset
404
1424
a794cbc174a9 maps: document RevMap.batchset will not update internal state
Jun Wu <quark@fb.com>
parents: 1422
diff changeset
405 For performance reason, internal in-memory state is not updated.
a794cbc174a9 maps: document RevMap.batchset will not update internal state
Jun Wu <quark@fb.com>
parents: 1422
diff changeset
406 To get an up-to-date RevMap, reconstruct the object.
1412
7e98352a37db maps: add the "batchset" method to RevMap
Jun Wu <quark@fb.com>
parents: 1411
diff changeset
407 '''
1435
18a961672a72 maps: switch many file opens to using the with statement
Augie Fackler <raf@durin42.com>
parents: 1434
diff changeset
408 with open(self._filepath, 'a') as f:
18a961672a72 maps: switch many file opens to using the with statement
Augie Fackler <raf@durin42.com>
parents: 1434
diff changeset
409 f.write(''.join('%s %s %s\n' % (revnum, hex(binhash), br or '')
18a961672a72 maps: switch many file opens to using the with statement
Augie Fackler <raf@durin42.com>
parents: 1434
diff changeset
410 for revnum, br, binhash in items))
1455
8cfe074cd463 maps: use util.fileproperty for RevMap.lastpulled
Jun Wu <quark@fb.com>
parents: 1449
diff changeset
411 self.lastpulled = lastpulled
1412
7e98352a37db maps: add the "batchset" method to RevMap
Jun Wu <quark@fb.com>
parents: 1411
diff changeset
412
1421
0094f222c5dc maps: make readmapfile of RevMap a private instance method
Jun Wu <quark@fb.com>
parents: 1419
diff changeset
413 def _readmapfile(self):
1430
48beb467b2e5 RevMap: use self._filepath instead of using meta
Augie Fackler <raf@durin42.com>
parents: 1429
diff changeset
414 path = self._filepath
889
7a98fbadcae9 revsets: huge speedups for fromsvn and svnrev
Bryan O'Sullivan <bryano@fb.com>
parents: 847
diff changeset
415 try:
1182
8f9619a67565 maps: change readmapfile to take a path instead of repo
Sean Farley <sean.michael.farley@gmail.com>
parents: 1144
diff changeset
416 f = open(path)
889
7a98fbadcae9 revsets: huge speedups for fromsvn and svnrev
Bryan O'Sullivan <bryano@fb.com>
parents: 847
diff changeset
417 except IOError, err:
1421
0094f222c5dc maps: make readmapfile of RevMap a private instance method
Jun Wu <quark@fb.com>
parents: 1419
diff changeset
418 if err.errno != errno.ENOENT:
889
7a98fbadcae9 revsets: huge speedups for fromsvn and svnrev
Bryan O'Sullivan <bryano@fb.com>
parents: 847
diff changeset
419 raise
7a98fbadcae9 revsets: huge speedups for fromsvn and svnrev
Bryan O'Sullivan <bryano@fb.com>
parents: 847
diff changeset
420 return iter([])
408
f137231f9d30 extract the revmap support into a separate dict-like class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 360
diff changeset
421 ver = int(f.readline())
1469
7bb2c6ca4d24 maps: implement migration from SqliteRevMap to RevMap
Jun Wu <quark@fb.com>
parents: 1468
diff changeset
422 if ver == SqliteRevMap.VERSION:
7bb2c6ca4d24 maps: implement migration from SqliteRevMap to RevMap
Jun Wu <quark@fb.com>
parents: 1468
diff changeset
423 revmap = SqliteRevMap(self._filepath, self._lastpulled_file)
7bb2c6ca4d24 maps: implement migration from SqliteRevMap to RevMap
Jun Wu <quark@fb.com>
parents: 1468
diff changeset
424 tmppath = '%s.tmp' % self._filepath
7bb2c6ca4d24 maps: implement migration from SqliteRevMap to RevMap
Jun Wu <quark@fb.com>
parents: 1468
diff changeset
425 revmap.exportrevmapv1(tmppath)
7bb2c6ca4d24 maps: implement migration from SqliteRevMap to RevMap
Jun Wu <quark@fb.com>
parents: 1468
diff changeset
426 os.rename(tmppath, self._filepath)
7bb2c6ca4d24 maps: implement migration from SqliteRevMap to RevMap
Jun Wu <quark@fb.com>
parents: 1468
diff changeset
427 hgutil.unlinkpath(revmap._dbpath)
1475
ea4d6142c6d9 maps: do not ask sqlite for row count
Jun Wu <quark@fb.com>
parents: 1472
diff changeset
428 hgutil.unlinkpath(revmap._rowcountpath, ignoremissing=True)
1469
7bb2c6ca4d24 maps: implement migration from SqliteRevMap to RevMap
Jun Wu <quark@fb.com>
parents: 1468
diff changeset
429 return self._readmapfile()
1421
0094f222c5dc maps: make readmapfile of RevMap a private instance method
Jun Wu <quark@fb.com>
parents: 1419
diff changeset
430 if ver != self.VERSION:
1555
cff81f35b31e cleanup: reference Abort from mercurial.error instead of mercurial.util
Augie Fackler <raf@durin42.com>
parents: 1550
diff changeset
431 raise error.Abort('revmap too new -- please upgrade')
889
7a98fbadcae9 revsets: huge speedups for fromsvn and svnrev
Bryan O'Sullivan <bryano@fb.com>
parents: 847
diff changeset
432 return f
7a98fbadcae9 revsets: huge speedups for fromsvn and svnrev
Bryan O'Sullivan <bryano@fb.com>
parents: 847
diff changeset
433
1251
46cec117dda2 maps.RevMap: disable GC while loading the revmap
Siddharth Agarwal <sid0@fb.com>
parents: 1217
diff changeset
434 @util.gcdisable
889
7a98fbadcae9 revsets: huge speedups for fromsvn and svnrev
Bryan O'Sullivan <bryano@fb.com>
parents: 847
diff changeset
435 def _load(self):
1434
0a6b3da6d34c RevMap: move lastpulled from SVNMeta down into RevMap
Augie Fackler <raf@durin42.com>
parents: 1432
diff changeset
436 lastpulled = self.lastpulled
0a6b3da6d34c RevMap: move lastpulled from SVNMeta down into RevMap
Augie Fackler <raf@durin42.com>
parents: 1432
diff changeset
437 firstpulled = self.firstpulled
1254
d07ccad28b1a maps.RevMap: avoid O(revs) property lookups on dict
Siddharth Agarwal <sid0@fb.com>
parents: 1253
diff changeset
438 setitem = dict.__setitem__
1421
0094f222c5dc maps: make readmapfile of RevMap a private instance method
Jun Wu <quark@fb.com>
parents: 1419
diff changeset
439 for l in self._readmapfile():
826
8794302f3614 maps: s/hash/ha/ and s/map/m/ to avoid hiding Python builtins
Yonggang Luo <luoyonggang@gmail.com>
parents: 822
diff changeset
440 revnum, ha, branch = l.split(' ', 2)
408
f137231f9d30 extract the revmap support into a separate dict-like class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 360
diff changeset
441 if branch == '\n':
f137231f9d30 extract the revmap support into a separate dict-like class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 360
diff changeset
442 branch = None
f137231f9d30 extract the revmap support into a separate dict-like class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 360
diff changeset
443 else:
f137231f9d30 extract the revmap support into a separate dict-like class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 360
diff changeset
444 branch = branch[:-1]
415
b17b2969861c svnmeta: move revmap methods, make last_known_revision() more efficient
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 409
diff changeset
445 revnum = int(revnum)
1252
a321afbc3479 maps.RevMap: while loading, read lastpulled and firstpulled once
Siddharth Agarwal <sid0@fb.com>
parents: 1251
diff changeset
446 if revnum > lastpulled or not lastpulled:
a321afbc3479 maps.RevMap: while loading, read lastpulled and firstpulled once
Siddharth Agarwal <sid0@fb.com>
parents: 1251
diff changeset
447 lastpulled = revnum
a321afbc3479 maps.RevMap: while loading, read lastpulled and firstpulled once
Siddharth Agarwal <sid0@fb.com>
parents: 1251
diff changeset
448 if revnum < firstpulled or not firstpulled:
a321afbc3479 maps.RevMap: while loading, read lastpulled and firstpulled once
Siddharth Agarwal <sid0@fb.com>
parents: 1251
diff changeset
449 firstpulled = revnum
1254
d07ccad28b1a maps.RevMap: avoid O(revs) property lookups on dict
Siddharth Agarwal <sid0@fb.com>
parents: 1253
diff changeset
450 setitem(self, (revnum, branch), bin(ha))
1434
0a6b3da6d34c RevMap: move lastpulled from SVNMeta down into RevMap
Augie Fackler <raf@durin42.com>
parents: 1432
diff changeset
451 if self.lastpulled != lastpulled:
0a6b3da6d34c RevMap: move lastpulled from SVNMeta down into RevMap
Augie Fackler <raf@durin42.com>
parents: 1432
diff changeset
452 self.lastpulled = lastpulled
0a6b3da6d34c RevMap: move lastpulled from SVNMeta down into RevMap
Augie Fackler <raf@durin42.com>
parents: 1432
diff changeset
453 self.firstpulled = firstpulled
408
f137231f9d30 extract the revmap support into a separate dict-like class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 360
diff changeset
454
f137231f9d30 extract the revmap support into a separate dict-like class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 360
diff changeset
455 def _write(self):
1435
18a961672a72 maps: switch many file opens to using the with statement
Augie Fackler <raf@durin42.com>
parents: 1434
diff changeset
456 with open(self._filepath, 'w') as f:
18a961672a72 maps: switch many file opens to using the with statement
Augie Fackler <raf@durin42.com>
parents: 1434
diff changeset
457 f.write('%s\n' % self.VERSION)
408
f137231f9d30 extract the revmap support into a separate dict-like class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 360
diff changeset
458
826
8794302f3614 maps: s/hash/ha/ and s/map/m/ to avoid hiding Python builtins
Yonggang Luo <luoyonggang@gmail.com>
parents: 822
diff changeset
459 def __setitem__(self, key, ha):
408
f137231f9d30 extract the revmap support into a separate dict-like class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 360
diff changeset
460 revnum, branch = key
f137231f9d30 extract the revmap support into a separate dict-like class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 360
diff changeset
461 b = branch or ''
1435
18a961672a72 maps: switch many file opens to using the with statement
Augie Fackler <raf@durin42.com>
parents: 1434
diff changeset
462 with open(self._filepath, 'a') as f:
18a961672a72 maps: switch many file opens to using the with statement
Augie Fackler <raf@durin42.com>
parents: 1434
diff changeset
463 f.write(str(revnum) + ' ' + hex(ha) + ' ' + b + '\n')
1434
0a6b3da6d34c RevMap: move lastpulled from SVNMeta down into RevMap
Augie Fackler <raf@durin42.com>
parents: 1432
diff changeset
464 if revnum > self.lastpulled or not self.lastpulled:
0a6b3da6d34c RevMap: move lastpulled from SVNMeta down into RevMap
Augie Fackler <raf@durin42.com>
parents: 1432
diff changeset
465 self.lastpulled = revnum
0a6b3da6d34c RevMap: move lastpulled from SVNMeta down into RevMap
Augie Fackler <raf@durin42.com>
parents: 1432
diff changeset
466 if revnum < self.firstpulled or not self.firstpulled:
0a6b3da6d34c RevMap: move lastpulled from SVNMeta down into RevMap
Augie Fackler <raf@durin42.com>
parents: 1432
diff changeset
467 self.firstpulled = revnum
826
8794302f3614 maps: s/hash/ha/ and s/map/m/ to avoid hiding Python builtins
Yonggang Luo <luoyonggang@gmail.com>
parents: 822
diff changeset
468 dict.__setitem__(self, (revnum, branch), ha)
1294
9a722b5246df maps: cache hashes() for the revmap
Mateusz Kwapich <mitrandir@fb.com>
parents: 1254
diff changeset
469 if self._hashes is not None:
9a722b5246df maps: cache hashes() for the revmap
Mateusz Kwapich <mitrandir@fb.com>
parents: 1254
diff changeset
470 self._hashes[ha] = (revnum, branch)
409
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
471
1468
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
472 @classmethod
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
473 def _wrapitermethods(cls):
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
474 def wrap(orig):
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
475 def wrapper(self, *args, **kwds):
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
476 if not self._allowiter:
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
477 raise NotImplementedError(
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
478 'Iteration methods on RevMap are disabled ' +
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
479 'to avoid performance issues on SqliteRevMap')
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
480 return orig(self, *args, **kwds)
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
481 return wrapper
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
482 methodre = re.compile(r'^_*(?:iter|view)?(?:keys|items|values)?_*$')
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
483 for name in filter(methodre.match, dir(cls)):
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
484 orig = getattr(cls, name)
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
485 setattr(cls, '_orig%s' % name, orig)
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
486 setattr(cls, name, wrap(orig))
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
487
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
488 RevMap._wrapitermethods()
b98ff95b5861 maps: disable iterating methods of RevMap
Jun Wu <quark@fb.com>
parents: 1467
diff changeset
489
409
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
490
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
491 class SqliteRevMap(collections.MutableMapping):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
492 """RevMap backed by sqlite3.
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
493
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
494 It tries to address performance issues for a very large rev map.
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
495 As such iteration is unavailable for both the map itself and the
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
496 reverse map (self.hashes).
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
497
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
498 It migrates from the old RevMap upon first use. Then it will bump the
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
499 version of revmap so RevMap no longer works. The real database is a
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
500 separated file which has a ".db" suffix.
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
501 """
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
502
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
503 VERSION = 2
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
504
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
505 TABLESCHEMA = [
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
506 '''CREATE TABLE IF NOT EXISTS revmap (
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
507 rev INTEGER NOT NULL,
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
508 branch TEXT NOT NULL DEFAULT '',
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
509 hash BLOB NOT NULL)''',
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
510 ]
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
511
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
512 INDEXSCHEMA = [
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
513 'CREATE UNIQUE INDEX IF NOT EXISTS revbranch ON revmap (rev,branch);',
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
514 'CREATE INDEX IF NOT EXISTS hash ON revmap (hash);',
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
515 ]
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
516
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
517 # "bytes" in Python 2 will get truncated at '\0' when storing as sqlite
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
518 # blobs. "buffer" does not have this issue. Python 3 does not have "buffer"
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
519 # but "bytes" won't get truncated.
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
520 sqlblobtype = bytes if sys.version_info >= (3, 0) else buffer
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
521
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
522 class ReverseRevMap(object):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
523 # collections.Mapping is not suitable since we don't want 2/3 of
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
524 # its required interfaces: __iter__, __len__.
1550
67b28d657f62 sqliterevmap: break ".hashes()" cycle in a safer way
Jun Wu <quark@fb.com>
parents: 1518
diff changeset
525 def __init__(self, revmap, cache):
67b28d657f62 sqliterevmap: break ".hashes()" cycle in a safer way
Jun Wu <quark@fb.com>
parents: 1518
diff changeset
526 self.revmap = revmap
67b28d657f62 sqliterevmap: break ".hashes()" cycle in a safer way
Jun Wu <quark@fb.com>
parents: 1518
diff changeset
527 self._cache = cache
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
528
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
529 def get(self, key, default=None):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
530 if key not in self._cache:
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
531 result = None
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
532 for row in self.revmap._query(
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
533 'SELECT rev, branch FROM revmap WHERE hash=?',
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
534 (SqliteRevMap.sqlblobtype(key),)):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
535 result = (row[0], row[1] or None)
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
536 break
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
537 self._cache[key] = result
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
538 return self._cache[key] or default
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
539
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
540 def __contains__(self, key):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
541 return self.get(key) != None
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
542
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
543 def __getitem__(self, key):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
544 dummy = self._cache
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
545 item = self.get(key, dummy)
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
546 if item == dummy:
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
547 raise KeyError(key)
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
548 else:
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
549 return item
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
550
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
551 def keys(self):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
552 for row in self.revmap._query('SELECT hash FROM revmap'):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
553 yield bytes(row[0])
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
554
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
555 lastpulled = util.fileproperty('_lastpulled', lambda x: x._lastpulledpath,
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
556 default=0, deserializer=int)
1475
ea4d6142c6d9 maps: do not ask sqlite for row count
Jun Wu <quark@fb.com>
parents: 1472
diff changeset
557 rowcount = util.fileproperty('_rowcount', lambda x: x._rowcountpath,
ea4d6142c6d9 maps: do not ask sqlite for row count
Jun Wu <quark@fb.com>
parents: 1472
diff changeset
558 default=0, deserializer=int)
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
559
1478
797c7b58a735 maps: add a config option to tweak sqlite
Jun Wu <quark@fb.com>
parents: 1477
diff changeset
560 def __init__(self, revmap_path, lastpulled_path, sqlitepragmas=None):
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
561 self._filepath = revmap_path
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
562 self._dbpath = revmap_path + '.db'
1475
ea4d6142c6d9 maps: do not ask sqlite for row count
Jun Wu <quark@fb.com>
parents: 1472
diff changeset
563 self._rowcountpath = self._dbpath + '.rowcount'
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
564 self._lastpulledpath = lastpulled_path
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
565
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
566 self._db = None
1478
797c7b58a735 maps: add a config option to tweak sqlite
Jun Wu <quark@fb.com>
parents: 1477
diff changeset
567 self._sqlitepragmas = sqlitepragmas
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
568 self.firstpulled = 0
1476
581f72f9478b maps: do not ask sqlite for min(rev), max(rev) together
Jun Wu <quark@fb.com>
parents: 1475
diff changeset
569 self._updatefirstlastpulled()
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
570 # __iter__ is expensive and thus disabled by default
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
571 # it should only be enabled for testing
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
572 self._allowiter = False
1550
67b28d657f62 sqliterevmap: break ".hashes()" cycle in a safer way
Jun Wu <quark@fb.com>
parents: 1518
diff changeset
573 # used by self.hashes(), {hash: (rev, branch)}
67b28d657f62 sqliterevmap: break ".hashes()" cycle in a safer way
Jun Wu <quark@fb.com>
parents: 1518
diff changeset
574 self._hashescache = {}
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
575
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
576 def hashes(self):
1550
67b28d657f62 sqliterevmap: break ".hashes()" cycle in a safer way
Jun Wu <quark@fb.com>
parents: 1518
diff changeset
577 return self.ReverseRevMap(self, self._hashescache)
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
578
1472
cf79525f507c maps: change branchedits to accept revnum directly
Jun Wu <quark@fb.com>
parents: 1470
diff changeset
579 def branchedits(self, branch, revnum):
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
580 return [((r[0], r[1] or None), bytes(r[2])) for r in
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
581 self._query('SELECT rev, branch, hash FROM revmap ' +
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
582 'WHERE rev < ? AND branch = ? ' +
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
583 'ORDER BY rev DESC, branch DESC',
1472
cf79525f507c maps: change branchedits to accept revnum directly
Jun Wu <quark@fb.com>
parents: 1470
diff changeset
584 (revnum, branch or ''))]
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
585
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
586 def branchmaxrevnum(self, branch, maxrev):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
587 for row in self._query('SELECT rev FROM revmap ' +
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
588 'WHERE rev <= ? AND branch = ? ' +
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
589 'ORDER By rev DESC LIMIT 1',
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
590 (maxrev, branch or '')):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
591 return row[0]
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
592 return 0
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
593
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
594 @property
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
595 def lasthash(self):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
596 for row in self._query('SELECT hash FROM revmap ORDER BY rev DESC'):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
597 return bytes(row[0])
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
598 return None
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
599
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
600 def revhashes(self, revnum):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
601 for row in self._query('SELECT hash FROM revmap WHERE rev = ?',
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
602 (revnum,)):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
603 yield bytes(row[0])
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
604
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
605 def clear(self):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
606 hgutil.unlinkpath(self._filepath, ignoremissing=True)
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
607 hgutil.unlinkpath(self._dbpath, ignoremissing=True)
1475
ea4d6142c6d9 maps: do not ask sqlite for row count
Jun Wu <quark@fb.com>
parents: 1472
diff changeset
608 hgutil.unlinkpath(self._rowcountpath, ignoremissing=True)
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
609 self._db = None
1550
67b28d657f62 sqliterevmap: break ".hashes()" cycle in a safer way
Jun Wu <quark@fb.com>
parents: 1518
diff changeset
610 self._hashescache = {}
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
611 self._firstpull = None
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
612 self._lastpull = None
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
613
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
614 def batchset(self, items, lastpulled):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
615 with self._transaction():
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
616 self._insert(items)
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
617 self.lastpulled = lastpulled
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
618
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
619 def __getitem__(self, key):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
620 for row in self._querybykey('SELECT hash', key):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
621 return bytes(row[0])
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
622 raise KeyError(key)
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
623
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
624 def __iter__(self):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
625 if not self._allowiter:
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
626 raise NotImplementedError(
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
627 'SqliteRevMap.__iter__ is not implemented intentionally ' +
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
628 'to avoid performance issues')
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
629 # collect result to avoid nested transaction issues
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
630 rows = []
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
631 for row in self._query('SELECT rev, branch FROM revmap'):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
632 rows.append((row[0], row[1] or None))
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
633 return iter(rows)
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
634
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
635 def __len__(self):
1475
ea4d6142c6d9 maps: do not ask sqlite for row count
Jun Wu <quark@fb.com>
parents: 1472
diff changeset
636 # rowcount is faster than "SELECT COUNT(1)". the latter is not O(1)
ea4d6142c6d9 maps: do not ask sqlite for row count
Jun Wu <quark@fb.com>
parents: 1472
diff changeset
637 return self.rowcount
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
638
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
639 def __setitem__(self, key, binha):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
640 revnum, branch = key
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
641 with self._transaction():
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
642 self._insert([(revnum, branch, binha)])
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
643 if revnum < self.firstpulled or not self.firstpulled:
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
644 self.firstpulled = revnum
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
645 if revnum > self.lastpulled or not self.lastpulled:
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
646 self.lastpulled = revnum
1550
67b28d657f62 sqliterevmap: break ".hashes()" cycle in a safer way
Jun Wu <quark@fb.com>
parents: 1518
diff changeset
647 if self._hashescache:
67b28d657f62 sqliterevmap: break ".hashes()" cycle in a safer way
Jun Wu <quark@fb.com>
parents: 1518
diff changeset
648 self._hashescache[binha] = key
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
649
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
650 def __delitem__(self, key):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
651 for row in self._querybykey('DELETE', key):
1475
ea4d6142c6d9 maps: do not ask sqlite for row count
Jun Wu <quark@fb.com>
parents: 1472
diff changeset
652 if self.rowcount > 0:
ea4d6142c6d9 maps: do not ask sqlite for row count
Jun Wu <quark@fb.com>
parents: 1472
diff changeset
653 self.rowcount -= 1
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
654 return
1550
67b28d657f62 sqliterevmap: break ".hashes()" cycle in a safer way
Jun Wu <quark@fb.com>
parents: 1518
diff changeset
655 # For performance reason, self._hashescache is not updated
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
656 raise KeyError(key)
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
657
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
658 @contextlib.contextmanager
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
659 def _transaction(self, mode='IMMEDIATE'):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
660 if self._db is None:
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
661 self._opendb()
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
662 with self._db as db:
1490
bc73b80baf98 SqliteRevMap: wait indefinitely for database lock
Jun Wu <quark@fb.com>
parents: 1478
diff changeset
663 # wait indefinitely for database lock
bc73b80baf98 SqliteRevMap: wait indefinitely for database lock
Jun Wu <quark@fb.com>
parents: 1478
diff changeset
664 while True:
bc73b80baf98 SqliteRevMap: wait indefinitely for database lock
Jun Wu <quark@fb.com>
parents: 1478
diff changeset
665 try:
bc73b80baf98 SqliteRevMap: wait indefinitely for database lock
Jun Wu <quark@fb.com>
parents: 1478
diff changeset
666 db.execute('BEGIN %s' % mode)
bc73b80baf98 SqliteRevMap: wait indefinitely for database lock
Jun Wu <quark@fb.com>
parents: 1478
diff changeset
667 break
bc73b80baf98 SqliteRevMap: wait indefinitely for database lock
Jun Wu <quark@fb.com>
parents: 1478
diff changeset
668 except sqlite3.OperationalError as ex:
bc73b80baf98 SqliteRevMap: wait indefinitely for database lock
Jun Wu <quark@fb.com>
parents: 1478
diff changeset
669 if str(ex) != 'database is locked':
bc73b80baf98 SqliteRevMap: wait indefinitely for database lock
Jun Wu <quark@fb.com>
parents: 1478
diff changeset
670 raise
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
671 yield db
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
672
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
673 def _query(self, sql, params=()):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
674 with self._transaction() as db:
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
675 cur = db.execute(sql, params)
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
676 try:
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
677 for row in cur:
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
678 yield row
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
679 finally:
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
680 cur.close()
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
681
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
682 def _querybykey(self, prefix, key):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
683 revnum, branch = key
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
684 return self._query(
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
685 '%s FROM revmap WHERE rev=? AND branch=?'
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
686 % prefix, (revnum, branch or ''))
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
687
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
688 def _insert(self, rows):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
689 # convert to a safe type so '\0' does not truncate the blob
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
690 if rows and type(rows[0][-1]) is not self.sqlblobtype:
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
691 rows = [(r, b, self.sqlblobtype(h)) for r, b, h in rows]
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
692 self._db.executemany(
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
693 'INSERT OR REPLACE INTO revmap (rev, branch, hash) ' +
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
694 'VALUES (?, ?, ?)', rows)
1475
ea4d6142c6d9 maps: do not ask sqlite for row count
Jun Wu <quark@fb.com>
parents: 1472
diff changeset
695 # If REPLACE happens, rowcount can be wrong. But it is only used to
ea4d6142c6d9 maps: do not ask sqlite for row count
Jun Wu <quark@fb.com>
parents: 1472
diff changeset
696 # calculate how many revisions pulled, and during pull we don't
ea4d6142c6d9 maps: do not ask sqlite for row count
Jun Wu <quark@fb.com>
parents: 1472
diff changeset
697 # replace rows. So it is fine.
ea4d6142c6d9 maps: do not ask sqlite for row count
Jun Wu <quark@fb.com>
parents: 1472
diff changeset
698 self.rowcount += len(rows)
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
699
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
700 def _opendb(self):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
701 '''Open the database and make sure the table is created on demand.'''
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
702 version = None
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
703 try:
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
704 version = int(open(self._filepath).read(2))
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
705 except (ValueError, IOError):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
706 pass
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
707 if version and version not in [RevMap.VERSION, self.VERSION]:
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
708 raise error.Abort('revmap too new -- please upgrade')
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
709
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
710 if self._db:
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
711 self._db.close()
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
712
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
713 # if version mismatch, the database is considered invalid
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
714 if version != self.VERSION:
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
715 hgutil.unlinkpath(self._dbpath, ignoremissing=True)
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
716
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
717 self._db = sqlite3.connect(self._dbpath)
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
718 self._db.text_factory = bytes
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
719
1470
b6e2bc962536 maps: increase sqlite cache size automatically
Jun Wu <quark@fb.com>
parents: 1469
diff changeset
720 # cache size affects random accessing (e.g. index building)
b6e2bc962536 maps: increase sqlite cache size automatically
Jun Wu <quark@fb.com>
parents: 1469
diff changeset
721 # performance greatly. default is 2MB (2000 KB), we want to have
b6e2bc962536 maps: increase sqlite cache size automatically
Jun Wu <quark@fb.com>
parents: 1469
diff changeset
722 # a big enough cache that can hold the entire map.
b6e2bc962536 maps: increase sqlite cache size automatically
Jun Wu <quark@fb.com>
parents: 1469
diff changeset
723 cachesize = 2000
b6e2bc962536 maps: increase sqlite cache size automatically
Jun Wu <quark@fb.com>
parents: 1469
diff changeset
724 for path, ratio in [(self._filepath, 1.7), (self._dbpath, 1)]:
b6e2bc962536 maps: increase sqlite cache size automatically
Jun Wu <quark@fb.com>
parents: 1469
diff changeset
725 if os.path.exists(path):
b6e2bc962536 maps: increase sqlite cache size automatically
Jun Wu <quark@fb.com>
parents: 1469
diff changeset
726 cachesize += os.stat(path).st_size * ratio // 1000
1478
797c7b58a735 maps: add a config option to tweak sqlite
Jun Wu <quark@fb.com>
parents: 1477
diff changeset
727
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
728 # disable auto-commit. everything is inside a transaction
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
729 self._db.isolation_level = 'DEFERRED'
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
730
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
731 with self._transaction('EXCLUSIVE'):
1518
09476d758b59 maps: handle sqlite lock error triggered by PRAGMA statements
Jun Wu <quark@fb.com>
parents: 1490
diff changeset
732 self._db.execute('PRAGMA cache_size=%d' % (-cachesize))
09476d758b59 maps: handle sqlite lock error triggered by PRAGMA statements
Jun Wu <quark@fb.com>
parents: 1490
diff changeset
733
09476d758b59 maps: handle sqlite lock error triggered by PRAGMA statements
Jun Wu <quark@fb.com>
parents: 1490
diff changeset
734 # PRAGMA statements provided by the user
09476d758b59 maps: handle sqlite lock error triggered by PRAGMA statements
Jun Wu <quark@fb.com>
parents: 1490
diff changeset
735 for pragma in (self._sqlitepragmas or []):
09476d758b59 maps: handle sqlite lock error triggered by PRAGMA statements
Jun Wu <quark@fb.com>
parents: 1490
diff changeset
736 # drop malicious ones
09476d758b59 maps: handle sqlite lock error triggered by PRAGMA statements
Jun Wu <quark@fb.com>
parents: 1490
diff changeset
737 if re.match(r'\A\w+=\w+\Z', pragma):
09476d758b59 maps: handle sqlite lock error triggered by PRAGMA statements
Jun Wu <quark@fb.com>
parents: 1490
diff changeset
738 self._db.execute('PRAGMA %s' % pragma)
09476d758b59 maps: handle sqlite lock error triggered by PRAGMA statements
Jun Wu <quark@fb.com>
parents: 1490
diff changeset
739
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
740 map(self._db.execute, self.TABLESCHEMA)
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
741 if version == RevMap.VERSION:
1475
ea4d6142c6d9 maps: do not ask sqlite for row count
Jun Wu <quark@fb.com>
parents: 1472
diff changeset
742 self.rowcount = 0
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
743 self._importrevmapv1()
1475
ea4d6142c6d9 maps: do not ask sqlite for row count
Jun Wu <quark@fb.com>
parents: 1472
diff changeset
744 elif not self.rowcount:
ea4d6142c6d9 maps: do not ask sqlite for row count
Jun Wu <quark@fb.com>
parents: 1472
diff changeset
745 self.rowcount = self._db.execute(
ea4d6142c6d9 maps: do not ask sqlite for row count
Jun Wu <quark@fb.com>
parents: 1472
diff changeset
746 'SELECT COUNT(1) FROM revmap').fetchone()[0]
ea4d6142c6d9 maps: do not ask sqlite for row count
Jun Wu <quark@fb.com>
parents: 1472
diff changeset
747
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
748 # "bulk insert; then create index" is about 2.4x as fast as
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
749 # "create index; then bulk insert" on a large repo
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
750 map(self._db.execute, self.INDEXSCHEMA)
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
751
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
752 # write a dummy rev map file with just the revision number
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
753 if version != self.VERSION:
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
754 f = open(self._filepath, 'w')
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
755 f.write('%s\n' % self.VERSION)
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
756 f.close()
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
757
1476
581f72f9478b maps: do not ask sqlite for min(rev), max(rev) together
Jun Wu <quark@fb.com>
parents: 1475
diff changeset
758 def _updatefirstlastpulled(self):
581f72f9478b maps: do not ask sqlite for min(rev), max(rev) together
Jun Wu <quark@fb.com>
parents: 1475
diff changeset
759 sql = 'SELECT rev FROM revmap ORDER BY rev %s LIMIT 1'
581f72f9478b maps: do not ask sqlite for min(rev), max(rev) together
Jun Wu <quark@fb.com>
parents: 1475
diff changeset
760 for row in self._query(sql % 'ASC'):
581f72f9478b maps: do not ask sqlite for min(rev), max(rev) together
Jun Wu <quark@fb.com>
parents: 1475
diff changeset
761 self.firstpulled = row[0]
581f72f9478b maps: do not ask sqlite for min(rev), max(rev) together
Jun Wu <quark@fb.com>
parents: 1475
diff changeset
762 for row in self._query(sql % 'DESC'):
581f72f9478b maps: do not ask sqlite for min(rev), max(rev) together
Jun Wu <quark@fb.com>
parents: 1475
diff changeset
763 if row[0] > self.lastpulled:
581f72f9478b maps: do not ask sqlite for min(rev), max(rev) together
Jun Wu <quark@fb.com>
parents: 1475
diff changeset
764 self.lastpulled = row[0]
581f72f9478b maps: do not ask sqlite for min(rev), max(rev) together
Jun Wu <quark@fb.com>
parents: 1475
diff changeset
765
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
766 @util.gcdisable
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
767 def _importrevmapv1(self):
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
768 with open(self._filepath, 'r') as f:
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
769 # 1st line is version
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
770 assert(int(f.readline())) == RevMap.VERSION
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
771 data = {}
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
772 for line in f:
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
773 revnum, ha, branch = line[:-1].split(' ', 2)
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
774 # ignore malicious lines
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
775 if len(ha) != 40:
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
776 continue
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
777 data[revnum, branch or None] = bin(ha)
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
778 self._insert([(r, b, h) for (r, b), h in data.iteritems()])
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
779
1469
7bb2c6ca4d24 maps: implement migration from SqliteRevMap to RevMap
Jun Wu <quark@fb.com>
parents: 1468
diff changeset
780 @util.gcdisable
7bb2c6ca4d24 maps: implement migration from SqliteRevMap to RevMap
Jun Wu <quark@fb.com>
parents: 1468
diff changeset
781 def exportrevmapv1(self, path):
7bb2c6ca4d24 maps: implement migration from SqliteRevMap to RevMap
Jun Wu <quark@fb.com>
parents: 1468
diff changeset
782 with open(path, 'w') as f:
7bb2c6ca4d24 maps: implement migration from SqliteRevMap to RevMap
Jun Wu <quark@fb.com>
parents: 1468
diff changeset
783 f.write('%s\n' % RevMap.VERSION)
7bb2c6ca4d24 maps: implement migration from SqliteRevMap to RevMap
Jun Wu <quark@fb.com>
parents: 1468
diff changeset
784 for row in self._query('SELECT rev, branch, hash FROM revmap'):
7bb2c6ca4d24 maps: implement migration from SqliteRevMap to RevMap
Jun Wu <quark@fb.com>
parents: 1468
diff changeset
785 rev, br, ha = row
7bb2c6ca4d24 maps: implement migration from SqliteRevMap to RevMap
Jun Wu <quark@fb.com>
parents: 1468
diff changeset
786 f.write('%s %s %s\n' % (rev, hex(ha), br))
7bb2c6ca4d24 maps: implement migration from SqliteRevMap to RevMap
Jun Wu <quark@fb.com>
parents: 1468
diff changeset
787
1467
53e306a6086b maps: implement sqlite revmap
Jun Wu <quark@fb.com>
parents: 1455
diff changeset
788
409
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
789 class FileMap(object):
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
790
846
7ca3d1b08d67 Save filemap into .hg/svn/filemap just like other maps
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 829
diff changeset
791 VERSION = 1
7ca3d1b08d67 Save filemap into .hg/svn/filemap just like other maps
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 829
diff changeset
792
1447
a6fa4f3aa826 FileMap: no longer take a meta
Augie Fackler <raf@durin42.com>
parents: 1446
diff changeset
793 def __init__(self, ui, filepath):
846
7ca3d1b08d67 Save filemap into .hg/svn/filemap just like other maps
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 829
diff changeset
794 '''Initialise a new FileMap.
7ca3d1b08d67 Save filemap into .hg/svn/filemap just like other maps
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 829
diff changeset
795
7ca3d1b08d67 Save filemap into .hg/svn/filemap just like other maps
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 829
diff changeset
796 The ui argument is used to print diagnostic messages.
7ca3d1b08d67 Save filemap into .hg/svn/filemap just like other maps
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 829
diff changeset
797
7ca3d1b08d67 Save filemap into .hg/svn/filemap just like other maps
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 829
diff changeset
798 The path argument is the location of the backing store,
7ca3d1b08d67 Save filemap into .hg/svn/filemap just like other maps
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 829
diff changeset
799 typically .hg/svn/filemap.
7ca3d1b08d67 Save filemap into .hg/svn/filemap just like other maps
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 829
diff changeset
800 '''
1447
a6fa4f3aa826 FileMap: no longer take a meta
Augie Fackler <raf@durin42.com>
parents: 1446
diff changeset
801 self._filename = filepath
a6fa4f3aa826 FileMap: no longer take a meta
Augie Fackler <raf@durin42.com>
parents: 1446
diff changeset
802 self._ui = ui
409
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
803 self.include = {}
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
804 self.exclude = {}
1437
43df01d36f22 FileMap: store filename locally
Augie Fackler <raf@durin42.com>
parents: 1435
diff changeset
805 if os.path.isfile(self._filename):
846
7ca3d1b08d67 Save filemap into .hg/svn/filemap just like other maps
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 829
diff changeset
806 self._load()
7ca3d1b08d67 Save filemap into .hg/svn/filemap just like other maps
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 829
diff changeset
807 else:
7ca3d1b08d67 Save filemap into .hg/svn/filemap just like other maps
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 829
diff changeset
808 self._write()
409
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
809
1214
2c793092862b maps: load commandline filemap in __init__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1213
diff changeset
810 # append file mapping specified from the commandline
1428
da272633997f maps: store a direct reference to ui
Augie Fackler <raf@durin42.com>
parents: 1427
diff changeset
811 clmap = util.configpath(self._ui, 'filemap')
1214
2c793092862b maps: load commandline filemap in __init__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1213
diff changeset
812 if clmap:
2c793092862b maps: load commandline filemap in __init__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1213
diff changeset
813 self.load(clmap)
2c793092862b maps: load commandline filemap in __init__
Sean Farley <sean.michael.farley@gmail.com>
parents: 1213
diff changeset
814
409
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
815 def _rpairs(self, name):
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
816 e = len(name)
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
817 while e != -1:
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
818 yield name[:e], name[e+1:]
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
819 e = name.rfind('/', 0, e)
847
0de18c5c2e35 Respect filemap rule order (rules that come first are overridden by rules that come later)
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 846
diff changeset
820 yield '.', name
409
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
821
826
8794302f3614 maps: s/hash/ha/ and s/map/m/ to avoid hiding Python builtins
Yonggang Luo <luoyonggang@gmail.com>
parents: 822
diff changeset
822 def check(self, m, path):
8794302f3614 maps: s/hash/ha/ and s/map/m/ to avoid hiding Python builtins
Yonggang Luo <luoyonggang@gmail.com>
parents: 822
diff changeset
823 m = getattr(self, m)
8794302f3614 maps: s/hash/ha/ and s/map/m/ to avoid hiding Python builtins
Yonggang Luo <luoyonggang@gmail.com>
parents: 822
diff changeset
824 for pre, _suf in self._rpairs(path):
847
0de18c5c2e35 Respect filemap rule order (rules that come first are overridden by rules that come later)
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 846
diff changeset
825 if pre in m:
0de18c5c2e35 Respect filemap rule order (rules that come first are overridden by rules that come later)
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 846
diff changeset
826 return m[pre]
0de18c5c2e35 Respect filemap rule order (rules that come first are overridden by rules that come later)
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 846
diff changeset
827 return -1
409
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
828
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
829 def __contains__(self, path):
847
0de18c5c2e35 Respect filemap rule order (rules that come first are overridden by rules that come later)
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 846
diff changeset
830 if not len(path):
0de18c5c2e35 Respect filemap rule order (rules that come first are overridden by rules that come later)
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 846
diff changeset
831 return True
0de18c5c2e35 Respect filemap rule order (rules that come first are overridden by rules that come later)
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 846
diff changeset
832 if len(self.include):
409
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
833 inc = self.check('include', path)
847
0de18c5c2e35 Respect filemap rule order (rules that come first are overridden by rules that come later)
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 846
diff changeset
834 elif not len(self.exclude):
0de18c5c2e35 Respect filemap rule order (rules that come first are overridden by rules that come later)
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 846
diff changeset
835 return True
409
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
836 else:
847
0de18c5c2e35 Respect filemap rule order (rules that come first are overridden by rules that come later)
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 846
diff changeset
837 inc = 0
0de18c5c2e35 Respect filemap rule order (rules that come first are overridden by rules that come later)
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 846
diff changeset
838 if len(self.exclude):
409
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
839 exc = self.check('exclude', path)
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
840 else:
847
0de18c5c2e35 Respect filemap rule order (rules that come first are overridden by rules that come later)
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 846
diff changeset
841 exc = -1
0de18c5c2e35 Respect filemap rule order (rules that come first are overridden by rules that come later)
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 846
diff changeset
842 # respect rule order: newer rules override older
0de18c5c2e35 Respect filemap rule order (rules that come first are overridden by rules that come later)
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 846
diff changeset
843 return inc > exc
409
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
844
822
033b86e0f56d stupid/filemap: disable this since it doesn't currently work
Augie Fackler <durin42@gmail.com>
parents: 821
diff changeset
845 # Needed so empty filemaps are false
033b86e0f56d stupid/filemap: disable this since it doesn't currently work
Augie Fackler <durin42@gmail.com>
parents: 821
diff changeset
846 def __len__(self):
033b86e0f56d stupid/filemap: disable this since it doesn't currently work
Augie Fackler <durin42@gmail.com>
parents: 821
diff changeset
847 return len(self.include) + len(self.exclude)
033b86e0f56d stupid/filemap: disable this since it doesn't currently work
Augie Fackler <durin42@gmail.com>
parents: 821
diff changeset
848
826
8794302f3614 maps: s/hash/ha/ and s/map/m/ to avoid hiding Python builtins
Yonggang Luo <luoyonggang@gmail.com>
parents: 822
diff changeset
849 def add(self, fn, m, path):
8794302f3614 maps: s/hash/ha/ and s/map/m/ to avoid hiding Python builtins
Yonggang Luo <luoyonggang@gmail.com>
parents: 822
diff changeset
850 mapping = getattr(self, m)
409
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
851 if path in mapping:
593
eb16630bceb1 maps: fix a % formatting bug
Augie Fackler <durin42@gmail.com>
parents: 579
diff changeset
852 msg = 'duplicate %s entry in %s: "%s"\n'
1428
da272633997f maps: store a direct reference to ui
Augie Fackler <raf@durin42.com>
parents: 1427
diff changeset
853 self._ui.status(msg % (m, fn, path))
409
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
854 return
956
24fbba02cb8f maps: fix filemap loading --verbose message
Patrick Mezard <patrick@mezard.eu>
parents: 891
diff changeset
855 bits = m.rstrip('e'), path
1428
da272633997f maps: store a direct reference to ui
Augie Fackler <raf@durin42.com>
parents: 1427
diff changeset
856 self._ui.debug('%sing %s\n' % bits)
847
0de18c5c2e35 Respect filemap rule order (rules that come first are overridden by rules that come later)
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 846
diff changeset
857 # respect rule order
0de18c5c2e35 Respect filemap rule order (rules that come first are overridden by rules that come later)
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 846
diff changeset
858 mapping[path] = len(self)
1437
43df01d36f22 FileMap: store filename locally
Augie Fackler <raf@durin42.com>
parents: 1435
diff changeset
859 if fn != self._filename:
43df01d36f22 FileMap: store filename locally
Augie Fackler <raf@durin42.com>
parents: 1435
diff changeset
860 with open(self._filename, 'a') as f:
1435
18a961672a72 maps: switch many file opens to using the with statement
Augie Fackler <raf@durin42.com>
parents: 1434
diff changeset
861 f.write(m + ' ' + path + '\n')
409
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
862
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
863 def load(self, fn):
1428
da272633997f maps: store a direct reference to ui
Augie Fackler <raf@durin42.com>
parents: 1427
diff changeset
864 self._ui.debug('reading file map from %s\n' % fn)
1435
18a961672a72 maps: switch many file opens to using the with statement
Augie Fackler <raf@durin42.com>
parents: 1434
diff changeset
865 with open(fn, 'r') as f:
18a961672a72 maps: switch many file opens to using the with statement
Augie Fackler <raf@durin42.com>
parents: 1434
diff changeset
866 self.load_fd(f, fn)
846
7ca3d1b08d67 Save filemap into .hg/svn/filemap just like other maps
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 829
diff changeset
867
7ca3d1b08d67 Save filemap into .hg/svn/filemap just like other maps
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 829
diff changeset
868 def load_fd(self, f, fn):
409
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
869 for line in f:
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
870 if line.strip() == '' or line.strip()[0] == '#':
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
871 continue
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
872 try:
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
873 cmd, path = line.split(' ', 1)
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
874 cmd = cmd.strip()
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
875 path = path.strip()
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
876 if cmd in ('include', 'exclude'):
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
877 self.add(fn, cmd, path)
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
878 continue
1428
da272633997f maps: store a direct reference to ui
Augie Fackler <raf@durin42.com>
parents: 1427
diff changeset
879 self._ui.warn('unknown filemap command %s\n' % cmd)
409
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
880 except IndexError:
d4615986e1db extract the filemap support into a separate class
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 408
diff changeset
881 msg = 'ignoring bad line in filemap %s: %s\n'
1428
da272633997f maps: store a direct reference to ui
Augie Fackler <raf@durin42.com>
parents: 1427
diff changeset
882 self._ui.warn(msg % (fn, line.rstrip()))
846
7ca3d1b08d67 Save filemap into .hg/svn/filemap just like other maps
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 829
diff changeset
883
7ca3d1b08d67 Save filemap into .hg/svn/filemap just like other maps
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 829
diff changeset
884 def _load(self):
1437
43df01d36f22 FileMap: store filename locally
Augie Fackler <raf@durin42.com>
parents: 1435
diff changeset
885 self._ui.debug('reading in-repo file map from %s\n' % self._filename)
43df01d36f22 FileMap: store filename locally
Augie Fackler <raf@durin42.com>
parents: 1435
diff changeset
886 with open(self._filename) as f:
1435
18a961672a72 maps: switch many file opens to using the with statement
Augie Fackler <raf@durin42.com>
parents: 1434
diff changeset
887 ver = int(f.readline())
18a961672a72 maps: switch many file opens to using the with statement
Augie Fackler <raf@durin42.com>
parents: 1434
diff changeset
888 if ver != self.VERSION:
1555
cff81f35b31e cleanup: reference Abort from mercurial.error instead of mercurial.util
Augie Fackler <raf@durin42.com>
parents: 1550
diff changeset
889 raise error.Abort('filemap too new -- please upgrade')
1437
43df01d36f22 FileMap: store filename locally
Augie Fackler <raf@durin42.com>
parents: 1435
diff changeset
890 self.load_fd(f, self._filename)
846
7ca3d1b08d67 Save filemap into .hg/svn/filemap just like other maps
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 829
diff changeset
891
7ca3d1b08d67 Save filemap into .hg/svn/filemap just like other maps
Vitaliy Filippov <vitalif@yourcmc.ru>
parents: 829
diff changeset
892 def _write(self):
1437
43df01d36f22 FileMap: store filename locally
Augie Fackler <raf@durin42.com>
parents: 1435
diff changeset
893 with open(self._filename, 'w') as f:
1435
18a961672a72 maps: switch many file opens to using the with statement
Augie Fackler <raf@durin42.com>
parents: 1434
diff changeset
894 f.write('%s\n' % self.VERSION)
574
8e025a6f0db4 add basic branchmap functionality, to rename branches
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 573
diff changeset
895
1388
130ced9e371d maps: make branch map inherit from base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1387
diff changeset
896 class BranchMap(BaseMap):
574
8e025a6f0db4 add basic branchmap functionality, to rename branches
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 573
diff changeset
897 '''Facility for controlled renaming of branch names. Example:
8e025a6f0db4 add basic branchmap functionality, to rename branches
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 573
diff changeset
898
8e025a6f0db4 add basic branchmap functionality, to rename branches
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 573
diff changeset
899 oldname = newname
8e025a6f0db4 add basic branchmap functionality, to rename branches
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 573
diff changeset
900 other = default
8e025a6f0db4 add basic branchmap functionality, to rename branches
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 573
diff changeset
901
8e025a6f0db4 add basic branchmap functionality, to rename branches
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 573
diff changeset
902 All changes on the oldname branch will now be on the newname branch; all
8e025a6f0db4 add basic branchmap functionality, to rename branches
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 573
diff changeset
903 changes on other will now be on default (have no branch name set).
8e025a6f0db4 add basic branchmap functionality, to rename branches
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 573
diff changeset
904 '''
8e025a6f0db4 add basic branchmap functionality, to rename branches
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 573
diff changeset
905
1391
7a866bca15de maps: make tag map inherit from base map
Sean Farley <sean.michael.farley@gmail.com>
parents: 1390
diff changeset
906 class TagMap(BaseMap):
729
467b95348e6a implement tag renames
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 728
diff changeset
907 '''Facility for controlled renaming of tags. Example:
467b95348e6a implement tag renames
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 728
diff changeset
908
467b95348e6a implement tag renames
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 728
diff changeset
909 oldname = newname
467b95348e6a implement tag renames
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 728
diff changeset
910 other =
467b95348e6a implement tag renames
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 728
diff changeset
911
809
ab372e38fb6c maps: clean up whitespace
Augie Fackler <durin42@gmail.com>
parents: 742
diff changeset
912 The oldname tag from SVN will be represented as newname in the hg tags;
ab372e38fb6c maps: clean up whitespace
Augie Fackler <durin42@gmail.com>
parents: 742
diff changeset
913 the other tag will not be reflected in the hg repository.
729
467b95348e6a implement tag renames
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 728
diff changeset
914 '''