##// END OF EJS Templates
check-code: catch misspellings of descendant...
Matt Mackall -
r14549:48ec0763 default
parent child Browse files
Show More
@@ -1,379 +1,380 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 #
2 #
3 # check-code - a style and portability checker for Mercurial
3 # check-code - a style and portability checker for Mercurial
4 #
4 #
5 # Copyright 2010 Matt Mackall <mpm@selenic.com>
5 # Copyright 2010 Matt Mackall <mpm@selenic.com>
6 #
6 #
7 # This software may be used and distributed according to the terms of the
7 # This software may be used and distributed according to the terms of the
8 # GNU General Public License version 2 or any later version.
8 # GNU General Public License version 2 or any later version.
9
9
10 import re, glob, os, sys
10 import re, glob, os, sys
11 import keyword
11 import keyword
12 import optparse
12 import optparse
13
13
14 def repquote(m):
14 def repquote(m):
15 t = re.sub(r"\w", "x", m.group('text'))
15 t = re.sub(r"\w", "x", m.group('text'))
16 t = re.sub(r"[^\sx]", "o", t)
16 t = re.sub(r"[^\sx]", "o", t)
17 return m.group('quote') + t + m.group('quote')
17 return m.group('quote') + t + m.group('quote')
18
18
19 def reppython(m):
19 def reppython(m):
20 comment = m.group('comment')
20 comment = m.group('comment')
21 if comment:
21 if comment:
22 return "#" * len(comment)
22 return "#" * len(comment)
23 return repquote(m)
23 return repquote(m)
24
24
25 def repcomment(m):
25 def repcomment(m):
26 return m.group(1) + "#" * len(m.group(2))
26 return m.group(1) + "#" * len(m.group(2))
27
27
28 def repccomment(m):
28 def repccomment(m):
29 t = re.sub(r"((?<=\n) )|\S", "x", m.group(2))
29 t = re.sub(r"((?<=\n) )|\S", "x", m.group(2))
30 return m.group(1) + t + "*/"
30 return m.group(1) + t + "*/"
31
31
32 def repcallspaces(m):
32 def repcallspaces(m):
33 t = re.sub(r"\n\s+", "\n", m.group(2))
33 t = re.sub(r"\n\s+", "\n", m.group(2))
34 return m.group(1) + t
34 return m.group(1) + t
35
35
36 def repinclude(m):
36 def repinclude(m):
37 return m.group(1) + "<foo>"
37 return m.group(1) + "<foo>"
38
38
39 def rephere(m):
39 def rephere(m):
40 t = re.sub(r"\S", "x", m.group(2))
40 t = re.sub(r"\S", "x", m.group(2))
41 return m.group(1) + t
41 return m.group(1) + t
42
42
43
43
44 testpats = [
44 testpats = [
45 [
45 [
46 (r'(pushd|popd)', "don't use 'pushd' or 'popd', use 'cd'"),
46 (r'(pushd|popd)', "don't use 'pushd' or 'popd', use 'cd'"),
47 (r'\W\$?\(\([^\)]*\)\)', "don't use (()) or $(()), use 'expr'"),
47 (r'\W\$?\(\([^\)]*\)\)', "don't use (()) or $(()), use 'expr'"),
48 (r'^function', "don't use 'function', use old style"),
48 (r'^function', "don't use 'function', use old style"),
49 (r'grep.*-q', "don't use 'grep -q', redirect to /dev/null"),
49 (r'grep.*-q', "don't use 'grep -q', redirect to /dev/null"),
50 (r'echo.*\\n', "don't use 'echo \\n', use printf"),
50 (r'echo.*\\n', "don't use 'echo \\n', use printf"),
51 (r'echo -n', "don't use 'echo -n', use printf"),
51 (r'echo -n', "don't use 'echo -n', use printf"),
52 (r'^diff.*-\w*N', "don't use 'diff -N'"),
52 (r'^diff.*-\w*N', "don't use 'diff -N'"),
53 (r'(^| )wc[^|]*$', "filter wc output"),
53 (r'(^| )wc[^|]*$', "filter wc output"),
54 (r'head -c', "don't use 'head -c', use 'dd'"),
54 (r'head -c', "don't use 'head -c', use 'dd'"),
55 (r'ls.*-\w*R', "don't use 'ls -R', use 'find'"),
55 (r'ls.*-\w*R', "don't use 'ls -R', use 'find'"),
56 (r'printf.*\\\d\d\d', "don't use 'printf \NNN', use Python"),
56 (r'printf.*\\\d\d\d', "don't use 'printf \NNN', use Python"),
57 (r'printf.*\\x', "don't use printf \\x, use Python"),
57 (r'printf.*\\x', "don't use printf \\x, use Python"),
58 (r'\$\(.*\)', "don't use $(expr), use `expr`"),
58 (r'\$\(.*\)', "don't use $(expr), use `expr`"),
59 (r'rm -rf \*', "don't use naked rm -rf, target a directory"),
59 (r'rm -rf \*', "don't use naked rm -rf, target a directory"),
60 (r'(^|\|\s*)grep (-\w\s+)*[^|]*[(|]\w',
60 (r'(^|\|\s*)grep (-\w\s+)*[^|]*[(|]\w',
61 "use egrep for extended grep syntax"),
61 "use egrep for extended grep syntax"),
62 (r'/bin/', "don't use explicit paths for tools"),
62 (r'/bin/', "don't use explicit paths for tools"),
63 (r'\$PWD', "don't use $PWD, use `pwd`"),
63 (r'\$PWD', "don't use $PWD, use `pwd`"),
64 (r'[^\n]\Z', "no trailing newline"),
64 (r'[^\n]\Z', "no trailing newline"),
65 (r'export.*=', "don't export and assign at once"),
65 (r'export.*=', "don't export and assign at once"),
66 ('^([^"\']|("[^"]*")|(\'[^\']*\'))*\\^', "^ must be quoted"),
66 ('^([^"\']|("[^"]*")|(\'[^\']*\'))*\\^', "^ must be quoted"),
67 (r'^source\b', "don't use 'source', use '.'"),
67 (r'^source\b', "don't use 'source', use '.'"),
68 (r'touch -d', "don't use 'touch -d', use 'touch -t' instead"),
68 (r'touch -d', "don't use 'touch -d', use 'touch -t' instead"),
69 (r'ls\s+[^|-]+\s+-', "options to 'ls' must come before filenames"),
69 (r'ls\s+[^|-]+\s+-', "options to 'ls' must come before filenames"),
70 (r'[^>]>\s*\$HGRCPATH', "don't overwrite $HGRCPATH, append to it"),
70 (r'[^>]>\s*\$HGRCPATH', "don't overwrite $HGRCPATH, append to it"),
71 ],
71 ],
72 # warnings
72 # warnings
73 []
73 []
74 ]
74 ]
75
75
76 testfilters = [
76 testfilters = [
77 (r"( *)(#([^\n]*\S)?)", repcomment),
77 (r"( *)(#([^\n]*\S)?)", repcomment),
78 (r"<<(\S+)((.|\n)*?\n\1)", rephere),
78 (r"<<(\S+)((.|\n)*?\n\1)", rephere),
79 ]
79 ]
80
80
81 uprefix = r"^ \$ "
81 uprefix = r"^ \$ "
82 uprefixc = r"^ > "
82 uprefixc = r"^ > "
83 utestpats = [
83 utestpats = [
84 [
84 [
85 (r'^(\S| $ ).*(\S\s+|^\s+)\n', "trailing whitespace on non-output"),
85 (r'^(\S| $ ).*(\S\s+|^\s+)\n', "trailing whitespace on non-output"),
86 (uprefix + r'.*\|\s*sed', "use regex test output patterns instead of sed"),
86 (uprefix + r'.*\|\s*sed', "use regex test output patterns instead of sed"),
87 (uprefix + r'(true|exit 0)', "explicit zero exit unnecessary"),
87 (uprefix + r'(true|exit 0)', "explicit zero exit unnecessary"),
88 (uprefix + r'.*\$\?', "explicit exit code checks unnecessary"),
88 (uprefix + r'.*\$\?', "explicit exit code checks unnecessary"),
89 (uprefix + r'.*\|\| echo.*(fail|error)',
89 (uprefix + r'.*\|\| echo.*(fail|error)',
90 "explicit exit code checks unnecessary"),
90 "explicit exit code checks unnecessary"),
91 (uprefix + r'set -e', "don't use set -e"),
91 (uprefix + r'set -e', "don't use set -e"),
92 (uprefixc + r'( *)\t', "don't use tabs to indent"),
92 (uprefixc + r'( *)\t', "don't use tabs to indent"),
93 ],
93 ],
94 # warnings
94 # warnings
95 []
95 []
96 ]
96 ]
97
97
98 for i in [0, 1]:
98 for i in [0, 1]:
99 for p, m in testpats[i]:
99 for p, m in testpats[i]:
100 if p.startswith('^'):
100 if p.startswith('^'):
101 p = uprefix + p[1:]
101 p = uprefix + p[1:]
102 else:
102 else:
103 p = uprefix + p
103 p = uprefix + p
104 utestpats[i].append((p, m))
104 utestpats[i].append((p, m))
105
105
106 utestfilters = [
106 utestfilters = [
107 (r"( *)(#([^\n]*\S)?)", repcomment),
107 (r"( *)(#([^\n]*\S)?)", repcomment),
108 ]
108 ]
109
109
110 pypats = [
110 pypats = [
111 [
111 [
112 (r'^\s*def\s*\w+\s*\(.*,\s*\(',
112 (r'^\s*def\s*\w+\s*\(.*,\s*\(',
113 "tuple parameter unpacking not available in Python 3+"),
113 "tuple parameter unpacking not available in Python 3+"),
114 (r'lambda\s*\(.*,.*\)',
114 (r'lambda\s*\(.*,.*\)',
115 "tuple parameter unpacking not available in Python 3+"),
115 "tuple parameter unpacking not available in Python 3+"),
116 (r'(?<!def)\s+(cmp)\(', "cmp is not available in Python 3+"),
116 (r'(?<!def)\s+(cmp)\(', "cmp is not available in Python 3+"),
117 (r'\breduce\s*\(.*', "reduce is not available in Python 3+"),
117 (r'\breduce\s*\(.*', "reduce is not available in Python 3+"),
118 (r'\.has_key\b', "dict.has_key is not available in Python 3+"),
118 (r'\.has_key\b', "dict.has_key is not available in Python 3+"),
119 (r'^\s*\t', "don't use tabs"),
119 (r'^\s*\t', "don't use tabs"),
120 (r'\S;\s*\n', "semicolon"),
120 (r'\S;\s*\n', "semicolon"),
121 (r'\w,\w', "missing whitespace after ,"),
121 (r'\w,\w', "missing whitespace after ,"),
122 (r'\w[+/*\-<>]\w', "missing whitespace in expression"),
122 (r'\w[+/*\-<>]\w', "missing whitespace in expression"),
123 (r'^\s+\w+=\w+[^,)]$', "missing whitespace in assignment"),
123 (r'^\s+\w+=\w+[^,)]$', "missing whitespace in assignment"),
124 (r'.{85}', "line too long"),
124 (r'.{85}', "line too long"),
125 (r'[^\n]\Z', "no trailing newline"),
125 (r'[^\n]\Z', "no trailing newline"),
126 (r'(\S\s+|^\s+)\n', "trailing whitespace"),
126 (r'(\S\s+|^\s+)\n', "trailing whitespace"),
127 # (r'^\s+[^_ ][^_. ]+_[^_]+\s*=', "don't use underbars in identifiers"),
127 # (r'^\s+[^_ ][^_. ]+_[^_]+\s*=', "don't use underbars in identifiers"),
128 # (r'\w*[a-z][A-Z]\w*\s*=', "don't use camelcase in identifiers"),
128 # (r'\w*[a-z][A-Z]\w*\s*=', "don't use camelcase in identifiers"),
129 (r'^\s*(if|while|def|class|except|try)\s[^[]*:\s*[^\]#\s]+',
129 (r'^\s*(if|while|def|class|except|try)\s[^[]*:\s*[^\]#\s]+',
130 "linebreak after :"),
130 "linebreak after :"),
131 (r'class\s[^(]:', "old-style class, use class foo(object)"),
131 (r'class\s[^(]:', "old-style class, use class foo(object)"),
132 (r'\b(%s)\(' % '|'.join(keyword.kwlist),
132 (r'\b(%s)\(' % '|'.join(keyword.kwlist),
133 "Python keyword is not a function"),
133 "Python keyword is not a function"),
134 (r',]', "unneeded trailing ',' in list"),
134 (r',]', "unneeded trailing ',' in list"),
135 # (r'class\s[A-Z][^\(]*\((?!Exception)',
135 # (r'class\s[A-Z][^\(]*\((?!Exception)',
136 # "don't capitalize non-exception classes"),
136 # "don't capitalize non-exception classes"),
137 # (r'in range\(', "use xrange"),
137 # (r'in range\(', "use xrange"),
138 # (r'^\s*print\s+', "avoid using print in core and extensions"),
138 # (r'^\s*print\s+', "avoid using print in core and extensions"),
139 (r'[\x80-\xff]', "non-ASCII character literal"),
139 (r'[\x80-\xff]', "non-ASCII character literal"),
140 (r'("\')\.format\(', "str.format() not available in Python 2.4"),
140 (r'("\')\.format\(', "str.format() not available in Python 2.4"),
141 (r'^\s*with\s+', "with not available in Python 2.4"),
141 (r'^\s*with\s+', "with not available in Python 2.4"),
142 (r'\.isdisjoint\(', "set.isdisjoint not available in Python 2.4"),
142 (r'\.isdisjoint\(', "set.isdisjoint not available in Python 2.4"),
143 (r'^\s*except.* as .*:', "except as not available in Python 2.4"),
143 (r'^\s*except.* as .*:', "except as not available in Python 2.4"),
144 (r'^\s*os\.path\.relpath', "relpath not available in Python 2.4"),
144 (r'^\s*os\.path\.relpath', "relpath not available in Python 2.4"),
145 (r'(?<!def)\s+(any|all|format)\(',
145 (r'(?<!def)\s+(any|all|format)\(',
146 "any/all/format not available in Python 2.4"),
146 "any/all/format not available in Python 2.4"),
147 (r'(?<!def)\s+(callable)\(',
147 (r'(?<!def)\s+(callable)\(',
148 "callable not available in Python 3, use hasattr(f, '__call__')"),
148 "callable not available in Python 3, use hasattr(f, '__call__')"),
149 (r'if\s.*\selse', "if ... else form not available in Python 2.4"),
149 (r'if\s.*\selse', "if ... else form not available in Python 2.4"),
150 (r'^\s*(%s)\s\s' % '|'.join(keyword.kwlist),
150 (r'^\s*(%s)\s\s' % '|'.join(keyword.kwlist),
151 "gratuitous whitespace after Python keyword"),
151 "gratuitous whitespace after Python keyword"),
152 (r'([\(\[]\s\S)|(\S\s[\)\]])', "gratuitous whitespace in () or []"),
152 (r'([\(\[]\s\S)|(\S\s[\)\]])', "gratuitous whitespace in () or []"),
153 # (r'\s\s=', "gratuitous whitespace before ="),
153 # (r'\s\s=', "gratuitous whitespace before ="),
154 (r'[^>< ](\+=|-=|!=|<>|<=|>=|<<=|>>=)\S',
154 (r'[^>< ](\+=|-=|!=|<>|<=|>=|<<=|>>=)\S',
155 "missing whitespace around operator"),
155 "missing whitespace around operator"),
156 (r'[^>< ](\+=|-=|!=|<>|<=|>=|<<=|>>=)\s',
156 (r'[^>< ](\+=|-=|!=|<>|<=|>=|<<=|>>=)\s',
157 "missing whitespace around operator"),
157 "missing whitespace around operator"),
158 (r'\s(\+=|-=|!=|<>|<=|>=|<<=|>>=)\S',
158 (r'\s(\+=|-=|!=|<>|<=|>=|<<=|>>=)\S',
159 "missing whitespace around operator"),
159 "missing whitespace around operator"),
160 (r'[^+=*/!<>&| -](\s=|=\s)[^= ]',
160 (r'[^+=*/!<>&| -](\s=|=\s)[^= ]',
161 "wrong whitespace around ="),
161 "wrong whitespace around ="),
162 (r'raise Exception', "don't raise generic exceptions"),
162 (r'raise Exception', "don't raise generic exceptions"),
163 (r' is\s+(not\s+)?["\'0-9-]', "object comparison with literal"),
163 (r' is\s+(not\s+)?["\'0-9-]', "object comparison with literal"),
164 (r' [=!]=\s+(True|False|None)',
164 (r' [=!]=\s+(True|False|None)',
165 "comparison with singleton, use 'is' or 'is not' instead"),
165 "comparison with singleton, use 'is' or 'is not' instead"),
166 (r'^\s*(while|if) [01]:',
166 (r'^\s*(while|if) [01]:',
167 "use True/False for constant Boolean expression"),
167 "use True/False for constant Boolean expression"),
168 (r'opener\([^)]*\).read\(',
168 (r'opener\([^)]*\).read\(',
169 "use opener.read() instead"),
169 "use opener.read() instead"),
170 (r'opener\([^)]*\).write\(',
170 (r'opener\([^)]*\).write\(',
171 "use opener.write() instead"),
171 "use opener.write() instead"),
172 (r'[\s\(](open|file)\([^)]*\)\.read\(',
172 (r'[\s\(](open|file)\([^)]*\)\.read\(',
173 "use util.readfile() instead"),
173 "use util.readfile() instead"),
174 (r'[\s\(](open|file)\([^)]*\)\.write\(',
174 (r'[\s\(](open|file)\([^)]*\)\.write\(',
175 "use util.readfile() instead"),
175 "use util.readfile() instead"),
176 (r'^[\s\(]*(open(er)?|file)\([^)]*\)',
176 (r'^[\s\(]*(open(er)?|file)\([^)]*\)',
177 "always assign an opened file to a variable, and close it afterwards"),
177 "always assign an opened file to a variable, and close it afterwards"),
178 (r'[\s\(](open|file)\([^)]*\)\.',
178 (r'[\s\(](open|file)\([^)]*\)\.',
179 "always assign an opened file to a variable, and close it afterwards"),
179 "always assign an opened file to a variable, and close it afterwards"),
180 (r'(?i)descendent', "the proper spelling is descendAnt"),
180 ],
181 ],
181 # warnings
182 # warnings
182 [
183 [
183 (r'.{81}', "warning: line over 80 characters"),
184 (r'.{81}', "warning: line over 80 characters"),
184 (r'^\s*except:$', "warning: naked except clause"),
185 (r'^\s*except:$', "warning: naked except clause"),
185 (r'ui\.(status|progress|write|note|warn)\([\'\"]x',
186 (r'ui\.(status|progress|write|note|warn)\([\'\"]x',
186 "warning: unwrapped ui message"),
187 "warning: unwrapped ui message"),
187 ]
188 ]
188 ]
189 ]
189
190
190 pyfilters = [
191 pyfilters = [
191 (r"""(?msx)(?P<comment>\#.*?$)|
192 (r"""(?msx)(?P<comment>\#.*?$)|
192 ((?P<quote>('''|\"\"\"|(?<!')'(?!')|(?<!")"(?!")))
193 ((?P<quote>('''|\"\"\"|(?<!')'(?!')|(?<!")"(?!")))
193 (?P<text>(([^\\]|\\.)*?))
194 (?P<text>(([^\\]|\\.)*?))
194 (?P=quote))""", reppython),
195 (?P=quote))""", reppython),
195 ]
196 ]
196
197
197 cpats = [
198 cpats = [
198 [
199 [
199 (r'//', "don't use //-style comments"),
200 (r'//', "don't use //-style comments"),
200 (r'^ ', "don't use spaces to indent"),
201 (r'^ ', "don't use spaces to indent"),
201 (r'\S\t', "don't use tabs except for indent"),
202 (r'\S\t', "don't use tabs except for indent"),
202 (r'(\S\s+|^\s+)\n', "trailing whitespace"),
203 (r'(\S\s+|^\s+)\n', "trailing whitespace"),
203 (r'.{85}', "line too long"),
204 (r'.{85}', "line too long"),
204 (r'(while|if|do|for)\(', "use space after while/if/do/for"),
205 (r'(while|if|do|for)\(', "use space after while/if/do/for"),
205 (r'return\(', "return is not a function"),
206 (r'return\(', "return is not a function"),
206 (r' ;', "no space before ;"),
207 (r' ;', "no space before ;"),
207 (r'\w+\* \w+', "use int *foo, not int* foo"),
208 (r'\w+\* \w+', "use int *foo, not int* foo"),
208 (r'\([^\)]+\) \w+', "use (int)foo, not (int) foo"),
209 (r'\([^\)]+\) \w+', "use (int)foo, not (int) foo"),
209 (r'\S+ (\+\+|--)', "use foo++, not foo ++"),
210 (r'\S+ (\+\+|--)', "use foo++, not foo ++"),
210 (r'\w,\w', "missing whitespace after ,"),
211 (r'\w,\w', "missing whitespace after ,"),
211 (r'^[^#]\w[+/*]\w', "missing whitespace in expression"),
212 (r'^[^#]\w[+/*]\w', "missing whitespace in expression"),
212 (r'^#\s+\w', "use #foo, not # foo"),
213 (r'^#\s+\w', "use #foo, not # foo"),
213 (r'[^\n]\Z', "no trailing newline"),
214 (r'[^\n]\Z', "no trailing newline"),
214 (r'^\s*#import\b', "use only #include in standard C code"),
215 (r'^\s*#import\b', "use only #include in standard C code"),
215 ],
216 ],
216 # warnings
217 # warnings
217 []
218 []
218 ]
219 ]
219
220
220 cfilters = [
221 cfilters = [
221 (r'(/\*)(((\*(?!/))|[^*])*)\*/', repccomment),
222 (r'(/\*)(((\*(?!/))|[^*])*)\*/', repccomment),
222 (r'''(?P<quote>(?<!")")(?P<text>([^"]|\\")+)"(?!")''', repquote),
223 (r'''(?P<quote>(?<!")")(?P<text>([^"]|\\")+)"(?!")''', repquote),
223 (r'''(#\s*include\s+<)([^>]+)>''', repinclude),
224 (r'''(#\s*include\s+<)([^>]+)>''', repinclude),
224 (r'(\()([^)]+\))', repcallspaces),
225 (r'(\()([^)]+\))', repcallspaces),
225 ]
226 ]
226
227
227 inutilpats = [
228 inutilpats = [
228 [
229 [
229 (r'\bui\.', "don't use ui in util"),
230 (r'\bui\.', "don't use ui in util"),
230 ],
231 ],
231 # warnings
232 # warnings
232 []
233 []
233 ]
234 ]
234
235
235 inrevlogpats = [
236 inrevlogpats = [
236 [
237 [
237 (r'\brepo\.', "don't use repo in revlog"),
238 (r'\brepo\.', "don't use repo in revlog"),
238 ],
239 ],
239 # warnings
240 # warnings
240 []
241 []
241 ]
242 ]
242
243
243 checks = [
244 checks = [
244 ('python', r'.*\.(py|cgi)$', pyfilters, pypats),
245 ('python', r'.*\.(py|cgi)$', pyfilters, pypats),
245 ('test script', r'(.*/)?test-[^.~]*$', testfilters, testpats),
246 ('test script', r'(.*/)?test-[^.~]*$', testfilters, testpats),
246 ('c', r'.*\.c$', cfilters, cpats),
247 ('c', r'.*\.c$', cfilters, cpats),
247 ('unified test', r'.*\.t$', utestfilters, utestpats),
248 ('unified test', r'.*\.t$', utestfilters, utestpats),
248 ('layering violation repo in revlog', r'mercurial/revlog\.py', pyfilters,
249 ('layering violation repo in revlog', r'mercurial/revlog\.py', pyfilters,
249 inrevlogpats),
250 inrevlogpats),
250 ('layering violation ui in util', r'mercurial/util\.py', pyfilters,
251 ('layering violation ui in util', r'mercurial/util\.py', pyfilters,
251 inutilpats),
252 inutilpats),
252 ]
253 ]
253
254
254 class norepeatlogger(object):
255 class norepeatlogger(object):
255 def __init__(self):
256 def __init__(self):
256 self._lastseen = None
257 self._lastseen = None
257
258
258 def log(self, fname, lineno, line, msg, blame):
259 def log(self, fname, lineno, line, msg, blame):
259 """print error related a to given line of a given file.
260 """print error related a to given line of a given file.
260
261
261 The faulty line will also be printed but only once in the case
262 The faulty line will also be printed but only once in the case
262 of multiple errors.
263 of multiple errors.
263
264
264 :fname: filename
265 :fname: filename
265 :lineno: line number
266 :lineno: line number
266 :line: actual content of the line
267 :line: actual content of the line
267 :msg: error message
268 :msg: error message
268 """
269 """
269 msgid = fname, lineno, line
270 msgid = fname, lineno, line
270 if msgid != self._lastseen:
271 if msgid != self._lastseen:
271 if blame:
272 if blame:
272 print "%s:%d (%s):" % (fname, lineno, blame)
273 print "%s:%d (%s):" % (fname, lineno, blame)
273 else:
274 else:
274 print "%s:%d:" % (fname, lineno)
275 print "%s:%d:" % (fname, lineno)
275 print " > %s" % line
276 print " > %s" % line
276 self._lastseen = msgid
277 self._lastseen = msgid
277 print " " + msg
278 print " " + msg
278
279
279 _defaultlogger = norepeatlogger()
280 _defaultlogger = norepeatlogger()
280
281
281 def getblame(f):
282 def getblame(f):
282 lines = []
283 lines = []
283 for l in os.popen('hg annotate -un %s' % f):
284 for l in os.popen('hg annotate -un %s' % f):
284 start, line = l.split(':', 1)
285 start, line = l.split(':', 1)
285 user, rev = start.split()
286 user, rev = start.split()
286 lines.append((line[1:-1], user, rev))
287 lines.append((line[1:-1], user, rev))
287 return lines
288 return lines
288
289
289 def checkfile(f, logfunc=_defaultlogger.log, maxerr=None, warnings=False,
290 def checkfile(f, logfunc=_defaultlogger.log, maxerr=None, warnings=False,
290 blame=False, debug=False):
291 blame=False, debug=False):
291 """checks style and portability of a given file
292 """checks style and portability of a given file
292
293
293 :f: filepath
294 :f: filepath
294 :logfunc: function used to report error
295 :logfunc: function used to report error
295 logfunc(filename, linenumber, linecontent, errormessage)
296 logfunc(filename, linenumber, linecontent, errormessage)
296 :maxerr: number of error to display before arborting.
297 :maxerr: number of error to display before arborting.
297 Set to None (default) to report all errors
298 Set to None (default) to report all errors
298
299
299 return True if no error is found, False otherwise.
300 return True if no error is found, False otherwise.
300 """
301 """
301 blamecache = None
302 blamecache = None
302 result = True
303 result = True
303 for name, match, filters, pats in checks:
304 for name, match, filters, pats in checks:
304 if debug:
305 if debug:
305 print name, f
306 print name, f
306 fc = 0
307 fc = 0
307 if not re.match(match, f):
308 if not re.match(match, f):
308 if debug:
309 if debug:
309 print "Skipping %s for %s it doesn't match %s" % (
310 print "Skipping %s for %s it doesn't match %s" % (
310 name, match, f)
311 name, match, f)
311 continue
312 continue
312 fp = open(f)
313 fp = open(f)
313 pre = post = fp.read()
314 pre = post = fp.read()
314 fp.close()
315 fp.close()
315 if "no-" + "check-code" in pre:
316 if "no-" + "check-code" in pre:
316 if debug:
317 if debug:
317 print "Skipping %s for %s it has no- and check-code" % (
318 print "Skipping %s for %s it has no- and check-code" % (
318 name, f)
319 name, f)
319 break
320 break
320 for p, r in filters:
321 for p, r in filters:
321 post = re.sub(p, r, post)
322 post = re.sub(p, r, post)
322 if warnings:
323 if warnings:
323 pats = pats[0] + pats[1]
324 pats = pats[0] + pats[1]
324 else:
325 else:
325 pats = pats[0]
326 pats = pats[0]
326 # print post # uncomment to show filtered version
327 # print post # uncomment to show filtered version
327 z = enumerate(zip(pre.splitlines(), post.splitlines(True)))
328 z = enumerate(zip(pre.splitlines(), post.splitlines(True)))
328 if debug:
329 if debug:
329 print "Checking %s for %s" % (name, f)
330 print "Checking %s for %s" % (name, f)
330 for n, l in z:
331 for n, l in z:
331 if "check-code" + "-ignore" in l[0]:
332 if "check-code" + "-ignore" in l[0]:
332 if debug:
333 if debug:
333 print "Skipping %s for %s:%s (check-code -ignore)" % (
334 print "Skipping %s for %s:%s (check-code -ignore)" % (
334 name, f, n)
335 name, f, n)
335 continue
336 continue
336 for p, msg in pats:
337 for p, msg in pats:
337 if re.search(p, l[1]):
338 if re.search(p, l[1]):
338 bd = ""
339 bd = ""
339 if blame:
340 if blame:
340 bd = 'working directory'
341 bd = 'working directory'
341 if not blamecache:
342 if not blamecache:
342 blamecache = getblame(f)
343 blamecache = getblame(f)
343 if n < len(blamecache):
344 if n < len(blamecache):
344 bl, bu, br = blamecache[n]
345 bl, bu, br = blamecache[n]
345 if bl == l[0]:
346 if bl == l[0]:
346 bd = '%s@%s' % (bu, br)
347 bd = '%s@%s' % (bu, br)
347 logfunc(f, n + 1, l[0], msg, bd)
348 logfunc(f, n + 1, l[0], msg, bd)
348 fc += 1
349 fc += 1
349 result = False
350 result = False
350 if maxerr is not None and fc >= maxerr:
351 if maxerr is not None and fc >= maxerr:
351 print " (too many errors, giving up)"
352 print " (too many errors, giving up)"
352 break
353 break
353 return result
354 return result
354
355
355 if __name__ == "__main__":
356 if __name__ == "__main__":
356 parser = optparse.OptionParser("%prog [options] [files]")
357 parser = optparse.OptionParser("%prog [options] [files]")
357 parser.add_option("-w", "--warnings", action="store_true",
358 parser.add_option("-w", "--warnings", action="store_true",
358 help="include warning-level checks")
359 help="include warning-level checks")
359 parser.add_option("-p", "--per-file", type="int",
360 parser.add_option("-p", "--per-file", type="int",
360 help="max warnings per file")
361 help="max warnings per file")
361 parser.add_option("-b", "--blame", action="store_true",
362 parser.add_option("-b", "--blame", action="store_true",
362 help="use annotate to generate blame info")
363 help="use annotate to generate blame info")
363 parser.add_option("", "--debug", action="store_true",
364 parser.add_option("", "--debug", action="store_true",
364 help="show debug information")
365 help="show debug information")
365
366
366 parser.set_defaults(per_file=15, warnings=False, blame=False, debug=False)
367 parser.set_defaults(per_file=15, warnings=False, blame=False, debug=False)
367 (options, args) = parser.parse_args()
368 (options, args) = parser.parse_args()
368
369
369 if len(args) == 0:
370 if len(args) == 0:
370 check = glob.glob("*")
371 check = glob.glob("*")
371 else:
372 else:
372 check = args
373 check = args
373
374
374 for f in check:
375 for f in check:
375 ret = 0
376 ret = 0
376 if not checkfile(f, maxerr=options.per_file, warnings=options.warnings,
377 if not checkfile(f, maxerr=options.per_file, warnings=options.warnings,
377 blame=options.blame, debug=options.debug):
378 blame=options.blame, debug=options.debug):
378 ret = 1
379 ret = 1
379 sys.exit(ret)
380 sys.exit(ret)
@@ -1,1989 +1,1989 b''
1 # localrepo.py - read/write repository class for mercurial
1 # localrepo.py - read/write repository class for mercurial
2 #
2 #
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 from node import bin, hex, nullid, nullrev, short
8 from node import bin, hex, nullid, nullrev, short
9 from i18n import _
9 from i18n import _
10 import repo, changegroup, subrepo, discovery, pushkey
10 import repo, changegroup, subrepo, discovery, pushkey
11 import changelog, dirstate, filelog, manifest, context, bookmarks
11 import changelog, dirstate, filelog, manifest, context, bookmarks
12 import lock, transaction, store, encoding
12 import lock, transaction, store, encoding
13 import scmutil, util, extensions, hook, error
13 import scmutil, util, extensions, hook, error
14 import match as matchmod
14 import match as matchmod
15 import merge as mergemod
15 import merge as mergemod
16 import tags as tagsmod
16 import tags as tagsmod
17 from lock import release
17 from lock import release
18 import weakref, errno, os, time, inspect
18 import weakref, errno, os, time, inspect
19 propertycache = util.propertycache
19 propertycache = util.propertycache
20
20
21 class localrepository(repo.repository):
21 class localrepository(repo.repository):
22 capabilities = set(('lookup', 'changegroupsubset', 'branchmap', 'pushkey',
22 capabilities = set(('lookup', 'changegroupsubset', 'branchmap', 'pushkey',
23 'known', 'getbundle'))
23 'known', 'getbundle'))
24 supportedformats = set(('revlogv1', 'generaldelta'))
24 supportedformats = set(('revlogv1', 'generaldelta'))
25 supported = supportedformats | set(('store', 'fncache', 'shared',
25 supported = supportedformats | set(('store', 'fncache', 'shared',
26 'dotencode'))
26 'dotencode'))
27
27
28 def __init__(self, baseui, path=None, create=False):
28 def __init__(self, baseui, path=None, create=False):
29 repo.repository.__init__(self)
29 repo.repository.__init__(self)
30 self.root = os.path.realpath(util.expandpath(path))
30 self.root = os.path.realpath(util.expandpath(path))
31 self.path = os.path.join(self.root, ".hg")
31 self.path = os.path.join(self.root, ".hg")
32 self.origroot = path
32 self.origroot = path
33 self.auditor = scmutil.pathauditor(self.root, self._checknested)
33 self.auditor = scmutil.pathauditor(self.root, self._checknested)
34 self.opener = scmutil.opener(self.path)
34 self.opener = scmutil.opener(self.path)
35 self.wopener = scmutil.opener(self.root)
35 self.wopener = scmutil.opener(self.root)
36 self.baseui = baseui
36 self.baseui = baseui
37 self.ui = baseui.copy()
37 self.ui = baseui.copy()
38
38
39 try:
39 try:
40 self.ui.readconfig(self.join("hgrc"), self.root)
40 self.ui.readconfig(self.join("hgrc"), self.root)
41 extensions.loadall(self.ui)
41 extensions.loadall(self.ui)
42 except IOError:
42 except IOError:
43 pass
43 pass
44
44
45 if not os.path.isdir(self.path):
45 if not os.path.isdir(self.path):
46 if create:
46 if create:
47 if not os.path.exists(path):
47 if not os.path.exists(path):
48 util.makedirs(path)
48 util.makedirs(path)
49 util.makedir(self.path, notindexed=True)
49 util.makedir(self.path, notindexed=True)
50 requirements = ["revlogv1"]
50 requirements = ["revlogv1"]
51 if self.ui.configbool('format', 'usestore', True):
51 if self.ui.configbool('format', 'usestore', True):
52 os.mkdir(os.path.join(self.path, "store"))
52 os.mkdir(os.path.join(self.path, "store"))
53 requirements.append("store")
53 requirements.append("store")
54 if self.ui.configbool('format', 'usefncache', True):
54 if self.ui.configbool('format', 'usefncache', True):
55 requirements.append("fncache")
55 requirements.append("fncache")
56 if self.ui.configbool('format', 'dotencode', True):
56 if self.ui.configbool('format', 'dotencode', True):
57 requirements.append('dotencode')
57 requirements.append('dotencode')
58 # create an invalid changelog
58 # create an invalid changelog
59 self.opener.append(
59 self.opener.append(
60 "00changelog.i",
60 "00changelog.i",
61 '\0\0\0\2' # represents revlogv2
61 '\0\0\0\2' # represents revlogv2
62 ' dummy changelog to prevent using the old repo layout'
62 ' dummy changelog to prevent using the old repo layout'
63 )
63 )
64 if self.ui.configbool('format', 'generaldelta', False):
64 if self.ui.configbool('format', 'generaldelta', False):
65 requirements.append("generaldelta")
65 requirements.append("generaldelta")
66 else:
66 else:
67 raise error.RepoError(_("repository %s not found") % path)
67 raise error.RepoError(_("repository %s not found") % path)
68 elif create:
68 elif create:
69 raise error.RepoError(_("repository %s already exists") % path)
69 raise error.RepoError(_("repository %s already exists") % path)
70 else:
70 else:
71 try:
71 try:
72 requirements = scmutil.readrequires(self.opener, self.supported)
72 requirements = scmutil.readrequires(self.opener, self.supported)
73 except IOError, inst:
73 except IOError, inst:
74 if inst.errno != errno.ENOENT:
74 if inst.errno != errno.ENOENT:
75 raise
75 raise
76 requirements = set()
76 requirements = set()
77
77
78 self.sharedpath = self.path
78 self.sharedpath = self.path
79 try:
79 try:
80 s = os.path.realpath(self.opener.read("sharedpath"))
80 s = os.path.realpath(self.opener.read("sharedpath"))
81 if not os.path.exists(s):
81 if not os.path.exists(s):
82 raise error.RepoError(
82 raise error.RepoError(
83 _('.hg/sharedpath points to nonexistent directory %s') % s)
83 _('.hg/sharedpath points to nonexistent directory %s') % s)
84 self.sharedpath = s
84 self.sharedpath = s
85 except IOError, inst:
85 except IOError, inst:
86 if inst.errno != errno.ENOENT:
86 if inst.errno != errno.ENOENT:
87 raise
87 raise
88
88
89 self.store = store.store(requirements, self.sharedpath, scmutil.opener)
89 self.store = store.store(requirements, self.sharedpath, scmutil.opener)
90 self.spath = self.store.path
90 self.spath = self.store.path
91 self.sopener = self.store.opener
91 self.sopener = self.store.opener
92 self.sjoin = self.store.join
92 self.sjoin = self.store.join
93 self.opener.createmode = self.store.createmode
93 self.opener.createmode = self.store.createmode
94 self._applyrequirements(requirements)
94 self._applyrequirements(requirements)
95 if create:
95 if create:
96 self._writerequirements()
96 self._writerequirements()
97
97
98 # These two define the set of tags for this repository. _tags
98 # These two define the set of tags for this repository. _tags
99 # maps tag name to node; _tagtypes maps tag name to 'global' or
99 # maps tag name to node; _tagtypes maps tag name to 'global' or
100 # 'local'. (Global tags are defined by .hgtags across all
100 # 'local'. (Global tags are defined by .hgtags across all
101 # heads, and local tags are defined in .hg/localtags.) They
101 # heads, and local tags are defined in .hg/localtags.) They
102 # constitute the in-memory cache of tags.
102 # constitute the in-memory cache of tags.
103 self._tags = None
103 self._tags = None
104 self._tagtypes = None
104 self._tagtypes = None
105
105
106 self._branchcache = None
106 self._branchcache = None
107 self._branchcachetip = None
107 self._branchcachetip = None
108 self.nodetagscache = None
108 self.nodetagscache = None
109 self.filterpats = {}
109 self.filterpats = {}
110 self._datafilters = {}
110 self._datafilters = {}
111 self._transref = self._lockref = self._wlockref = None
111 self._transref = self._lockref = self._wlockref = None
112
112
113 def _applyrequirements(self, requirements):
113 def _applyrequirements(self, requirements):
114 self.requirements = requirements
114 self.requirements = requirements
115 openerreqs = set(('revlogv1', 'generaldelta'))
115 openerreqs = set(('revlogv1', 'generaldelta'))
116 self.sopener.options = dict((r, 1) for r in requirements
116 self.sopener.options = dict((r, 1) for r in requirements
117 if r in openerreqs)
117 if r in openerreqs)
118
118
119 def _writerequirements(self):
119 def _writerequirements(self):
120 reqfile = self.opener("requires", "w")
120 reqfile = self.opener("requires", "w")
121 for r in self.requirements:
121 for r in self.requirements:
122 reqfile.write("%s\n" % r)
122 reqfile.write("%s\n" % r)
123 reqfile.close()
123 reqfile.close()
124
124
125 def _checknested(self, path):
125 def _checknested(self, path):
126 """Determine if path is a legal nested repository."""
126 """Determine if path is a legal nested repository."""
127 if not path.startswith(self.root):
127 if not path.startswith(self.root):
128 return False
128 return False
129 subpath = path[len(self.root) + 1:]
129 subpath = path[len(self.root) + 1:]
130
130
131 # XXX: Checking against the current working copy is wrong in
131 # XXX: Checking against the current working copy is wrong in
132 # the sense that it can reject things like
132 # the sense that it can reject things like
133 #
133 #
134 # $ hg cat -r 10 sub/x.txt
134 # $ hg cat -r 10 sub/x.txt
135 #
135 #
136 # if sub/ is no longer a subrepository in the working copy
136 # if sub/ is no longer a subrepository in the working copy
137 # parent revision.
137 # parent revision.
138 #
138 #
139 # However, it can of course also allow things that would have
139 # However, it can of course also allow things that would have
140 # been rejected before, such as the above cat command if sub/
140 # been rejected before, such as the above cat command if sub/
141 # is a subrepository now, but was a normal directory before.
141 # is a subrepository now, but was a normal directory before.
142 # The old path auditor would have rejected by mistake since it
142 # The old path auditor would have rejected by mistake since it
143 # panics when it sees sub/.hg/.
143 # panics when it sees sub/.hg/.
144 #
144 #
145 # All in all, checking against the working copy seems sensible
145 # All in all, checking against the working copy seems sensible
146 # since we want to prevent access to nested repositories on
146 # since we want to prevent access to nested repositories on
147 # the filesystem *now*.
147 # the filesystem *now*.
148 ctx = self[None]
148 ctx = self[None]
149 parts = util.splitpath(subpath)
149 parts = util.splitpath(subpath)
150 while parts:
150 while parts:
151 prefix = os.sep.join(parts)
151 prefix = os.sep.join(parts)
152 if prefix in ctx.substate:
152 if prefix in ctx.substate:
153 if prefix == subpath:
153 if prefix == subpath:
154 return True
154 return True
155 else:
155 else:
156 sub = ctx.sub(prefix)
156 sub = ctx.sub(prefix)
157 return sub.checknested(subpath[len(prefix) + 1:])
157 return sub.checknested(subpath[len(prefix) + 1:])
158 else:
158 else:
159 parts.pop()
159 parts.pop()
160 return False
160 return False
161
161
162 @util.propertycache
162 @util.propertycache
163 def _bookmarks(self):
163 def _bookmarks(self):
164 return bookmarks.read(self)
164 return bookmarks.read(self)
165
165
166 @util.propertycache
166 @util.propertycache
167 def _bookmarkcurrent(self):
167 def _bookmarkcurrent(self):
168 return bookmarks.readcurrent(self)
168 return bookmarks.readcurrent(self)
169
169
170 @propertycache
170 @propertycache
171 def changelog(self):
171 def changelog(self):
172 c = changelog.changelog(self.sopener)
172 c = changelog.changelog(self.sopener)
173 if 'HG_PENDING' in os.environ:
173 if 'HG_PENDING' in os.environ:
174 p = os.environ['HG_PENDING']
174 p = os.environ['HG_PENDING']
175 if p.startswith(self.root):
175 if p.startswith(self.root):
176 c.readpending('00changelog.i.a')
176 c.readpending('00changelog.i.a')
177 return c
177 return c
178
178
179 @propertycache
179 @propertycache
180 def manifest(self):
180 def manifest(self):
181 return manifest.manifest(self.sopener)
181 return manifest.manifest(self.sopener)
182
182
183 @propertycache
183 @propertycache
184 def dirstate(self):
184 def dirstate(self):
185 warned = [0]
185 warned = [0]
186 def validate(node):
186 def validate(node):
187 try:
187 try:
188 self.changelog.rev(node)
188 self.changelog.rev(node)
189 return node
189 return node
190 except error.LookupError:
190 except error.LookupError:
191 if not warned[0]:
191 if not warned[0]:
192 warned[0] = True
192 warned[0] = True
193 self.ui.warn(_("warning: ignoring unknown"
193 self.ui.warn(_("warning: ignoring unknown"
194 " working parent %s!\n") % short(node))
194 " working parent %s!\n") % short(node))
195 return nullid
195 return nullid
196
196
197 return dirstate.dirstate(self.opener, self.ui, self.root, validate)
197 return dirstate.dirstate(self.opener, self.ui, self.root, validate)
198
198
199 def __getitem__(self, changeid):
199 def __getitem__(self, changeid):
200 if changeid is None:
200 if changeid is None:
201 return context.workingctx(self)
201 return context.workingctx(self)
202 return context.changectx(self, changeid)
202 return context.changectx(self, changeid)
203
203
204 def __contains__(self, changeid):
204 def __contains__(self, changeid):
205 try:
205 try:
206 return bool(self.lookup(changeid))
206 return bool(self.lookup(changeid))
207 except error.RepoLookupError:
207 except error.RepoLookupError:
208 return False
208 return False
209
209
210 def __nonzero__(self):
210 def __nonzero__(self):
211 return True
211 return True
212
212
213 def __len__(self):
213 def __len__(self):
214 return len(self.changelog)
214 return len(self.changelog)
215
215
216 def __iter__(self):
216 def __iter__(self):
217 for i in xrange(len(self)):
217 for i in xrange(len(self)):
218 yield i
218 yield i
219
219
220 def url(self):
220 def url(self):
221 return 'file:' + self.root
221 return 'file:' + self.root
222
222
223 def hook(self, name, throw=False, **args):
223 def hook(self, name, throw=False, **args):
224 return hook.hook(self.ui, self, name, throw, **args)
224 return hook.hook(self.ui, self, name, throw, **args)
225
225
226 tag_disallowed = ':\r\n'
226 tag_disallowed = ':\r\n'
227
227
228 def _tag(self, names, node, message, local, user, date, extra={}):
228 def _tag(self, names, node, message, local, user, date, extra={}):
229 if isinstance(names, str):
229 if isinstance(names, str):
230 allchars = names
230 allchars = names
231 names = (names,)
231 names = (names,)
232 else:
232 else:
233 allchars = ''.join(names)
233 allchars = ''.join(names)
234 for c in self.tag_disallowed:
234 for c in self.tag_disallowed:
235 if c in allchars:
235 if c in allchars:
236 raise util.Abort(_('%r cannot be used in a tag name') % c)
236 raise util.Abort(_('%r cannot be used in a tag name') % c)
237
237
238 branches = self.branchmap()
238 branches = self.branchmap()
239 for name in names:
239 for name in names:
240 self.hook('pretag', throw=True, node=hex(node), tag=name,
240 self.hook('pretag', throw=True, node=hex(node), tag=name,
241 local=local)
241 local=local)
242 if name in branches:
242 if name in branches:
243 self.ui.warn(_("warning: tag %s conflicts with existing"
243 self.ui.warn(_("warning: tag %s conflicts with existing"
244 " branch name\n") % name)
244 " branch name\n") % name)
245
245
246 def writetags(fp, names, munge, prevtags):
246 def writetags(fp, names, munge, prevtags):
247 fp.seek(0, 2)
247 fp.seek(0, 2)
248 if prevtags and prevtags[-1] != '\n':
248 if prevtags and prevtags[-1] != '\n':
249 fp.write('\n')
249 fp.write('\n')
250 for name in names:
250 for name in names:
251 m = munge and munge(name) or name
251 m = munge and munge(name) or name
252 if self._tagtypes and name in self._tagtypes:
252 if self._tagtypes and name in self._tagtypes:
253 old = self._tags.get(name, nullid)
253 old = self._tags.get(name, nullid)
254 fp.write('%s %s\n' % (hex(old), m))
254 fp.write('%s %s\n' % (hex(old), m))
255 fp.write('%s %s\n' % (hex(node), m))
255 fp.write('%s %s\n' % (hex(node), m))
256 fp.close()
256 fp.close()
257
257
258 prevtags = ''
258 prevtags = ''
259 if local:
259 if local:
260 try:
260 try:
261 fp = self.opener('localtags', 'r+')
261 fp = self.opener('localtags', 'r+')
262 except IOError:
262 except IOError:
263 fp = self.opener('localtags', 'a')
263 fp = self.opener('localtags', 'a')
264 else:
264 else:
265 prevtags = fp.read()
265 prevtags = fp.read()
266
266
267 # local tags are stored in the current charset
267 # local tags are stored in the current charset
268 writetags(fp, names, None, prevtags)
268 writetags(fp, names, None, prevtags)
269 for name in names:
269 for name in names:
270 self.hook('tag', node=hex(node), tag=name, local=local)
270 self.hook('tag', node=hex(node), tag=name, local=local)
271 return
271 return
272
272
273 try:
273 try:
274 fp = self.wfile('.hgtags', 'rb+')
274 fp = self.wfile('.hgtags', 'rb+')
275 except IOError:
275 except IOError:
276 fp = self.wfile('.hgtags', 'ab')
276 fp = self.wfile('.hgtags', 'ab')
277 else:
277 else:
278 prevtags = fp.read()
278 prevtags = fp.read()
279
279
280 # committed tags are stored in UTF-8
280 # committed tags are stored in UTF-8
281 writetags(fp, names, encoding.fromlocal, prevtags)
281 writetags(fp, names, encoding.fromlocal, prevtags)
282
282
283 fp.close()
283 fp.close()
284
284
285 if '.hgtags' not in self.dirstate:
285 if '.hgtags' not in self.dirstate:
286 self[None].add(['.hgtags'])
286 self[None].add(['.hgtags'])
287
287
288 m = matchmod.exact(self.root, '', ['.hgtags'])
288 m = matchmod.exact(self.root, '', ['.hgtags'])
289 tagnode = self.commit(message, user, date, extra=extra, match=m)
289 tagnode = self.commit(message, user, date, extra=extra, match=m)
290
290
291 for name in names:
291 for name in names:
292 self.hook('tag', node=hex(node), tag=name, local=local)
292 self.hook('tag', node=hex(node), tag=name, local=local)
293
293
294 return tagnode
294 return tagnode
295
295
296 def tag(self, names, node, message, local, user, date):
296 def tag(self, names, node, message, local, user, date):
297 '''tag a revision with one or more symbolic names.
297 '''tag a revision with one or more symbolic names.
298
298
299 names is a list of strings or, when adding a single tag, names may be a
299 names is a list of strings or, when adding a single tag, names may be a
300 string.
300 string.
301
301
302 if local is True, the tags are stored in a per-repository file.
302 if local is True, the tags are stored in a per-repository file.
303 otherwise, they are stored in the .hgtags file, and a new
303 otherwise, they are stored in the .hgtags file, and a new
304 changeset is committed with the change.
304 changeset is committed with the change.
305
305
306 keyword arguments:
306 keyword arguments:
307
307
308 local: whether to store tags in non-version-controlled file
308 local: whether to store tags in non-version-controlled file
309 (default False)
309 (default False)
310
310
311 message: commit message to use if committing
311 message: commit message to use if committing
312
312
313 user: name of user to use if committing
313 user: name of user to use if committing
314
314
315 date: date tuple to use if committing'''
315 date: date tuple to use if committing'''
316
316
317 if not local:
317 if not local:
318 for x in self.status()[:5]:
318 for x in self.status()[:5]:
319 if '.hgtags' in x:
319 if '.hgtags' in x:
320 raise util.Abort(_('working copy of .hgtags is changed '
320 raise util.Abort(_('working copy of .hgtags is changed '
321 '(please commit .hgtags manually)'))
321 '(please commit .hgtags manually)'))
322
322
323 self.tags() # instantiate the cache
323 self.tags() # instantiate the cache
324 self._tag(names, node, message, local, user, date)
324 self._tag(names, node, message, local, user, date)
325
325
326 def tags(self):
326 def tags(self):
327 '''return a mapping of tag to node'''
327 '''return a mapping of tag to node'''
328 if self._tags is None:
328 if self._tags is None:
329 (self._tags, self._tagtypes) = self._findtags()
329 (self._tags, self._tagtypes) = self._findtags()
330
330
331 return self._tags
331 return self._tags
332
332
333 def _findtags(self):
333 def _findtags(self):
334 '''Do the hard work of finding tags. Return a pair of dicts
334 '''Do the hard work of finding tags. Return a pair of dicts
335 (tags, tagtypes) where tags maps tag name to node, and tagtypes
335 (tags, tagtypes) where tags maps tag name to node, and tagtypes
336 maps tag name to a string like \'global\' or \'local\'.
336 maps tag name to a string like \'global\' or \'local\'.
337 Subclasses or extensions are free to add their own tags, but
337 Subclasses or extensions are free to add their own tags, but
338 should be aware that the returned dicts will be retained for the
338 should be aware that the returned dicts will be retained for the
339 duration of the localrepo object.'''
339 duration of the localrepo object.'''
340
340
341 # XXX what tagtype should subclasses/extensions use? Currently
341 # XXX what tagtype should subclasses/extensions use? Currently
342 # mq and bookmarks add tags, but do not set the tagtype at all.
342 # mq and bookmarks add tags, but do not set the tagtype at all.
343 # Should each extension invent its own tag type? Should there
343 # Should each extension invent its own tag type? Should there
344 # be one tagtype for all such "virtual" tags? Or is the status
344 # be one tagtype for all such "virtual" tags? Or is the status
345 # quo fine?
345 # quo fine?
346
346
347 alltags = {} # map tag name to (node, hist)
347 alltags = {} # map tag name to (node, hist)
348 tagtypes = {}
348 tagtypes = {}
349
349
350 tagsmod.findglobaltags(self.ui, self, alltags, tagtypes)
350 tagsmod.findglobaltags(self.ui, self, alltags, tagtypes)
351 tagsmod.readlocaltags(self.ui, self, alltags, tagtypes)
351 tagsmod.readlocaltags(self.ui, self, alltags, tagtypes)
352
352
353 # Build the return dicts. Have to re-encode tag names because
353 # Build the return dicts. Have to re-encode tag names because
354 # the tags module always uses UTF-8 (in order not to lose info
354 # the tags module always uses UTF-8 (in order not to lose info
355 # writing to the cache), but the rest of Mercurial wants them in
355 # writing to the cache), but the rest of Mercurial wants them in
356 # local encoding.
356 # local encoding.
357 tags = {}
357 tags = {}
358 for (name, (node, hist)) in alltags.iteritems():
358 for (name, (node, hist)) in alltags.iteritems():
359 if node != nullid:
359 if node != nullid:
360 try:
360 try:
361 # ignore tags to unknown nodes
361 # ignore tags to unknown nodes
362 self.changelog.lookup(node)
362 self.changelog.lookup(node)
363 tags[encoding.tolocal(name)] = node
363 tags[encoding.tolocal(name)] = node
364 except error.LookupError:
364 except error.LookupError:
365 pass
365 pass
366 tags['tip'] = self.changelog.tip()
366 tags['tip'] = self.changelog.tip()
367 tagtypes = dict([(encoding.tolocal(name), value)
367 tagtypes = dict([(encoding.tolocal(name), value)
368 for (name, value) in tagtypes.iteritems()])
368 for (name, value) in tagtypes.iteritems()])
369 return (tags, tagtypes)
369 return (tags, tagtypes)
370
370
371 def tagtype(self, tagname):
371 def tagtype(self, tagname):
372 '''
372 '''
373 return the type of the given tag. result can be:
373 return the type of the given tag. result can be:
374
374
375 'local' : a local tag
375 'local' : a local tag
376 'global' : a global tag
376 'global' : a global tag
377 None : tag does not exist
377 None : tag does not exist
378 '''
378 '''
379
379
380 self.tags()
380 self.tags()
381
381
382 return self._tagtypes.get(tagname)
382 return self._tagtypes.get(tagname)
383
383
384 def tagslist(self):
384 def tagslist(self):
385 '''return a list of tags ordered by revision'''
385 '''return a list of tags ordered by revision'''
386 l = []
386 l = []
387 for t, n in self.tags().iteritems():
387 for t, n in self.tags().iteritems():
388 r = self.changelog.rev(n)
388 r = self.changelog.rev(n)
389 l.append((r, t, n))
389 l.append((r, t, n))
390 return [(t, n) for r, t, n in sorted(l)]
390 return [(t, n) for r, t, n in sorted(l)]
391
391
392 def nodetags(self, node):
392 def nodetags(self, node):
393 '''return the tags associated with a node'''
393 '''return the tags associated with a node'''
394 if not self.nodetagscache:
394 if not self.nodetagscache:
395 self.nodetagscache = {}
395 self.nodetagscache = {}
396 for t, n in self.tags().iteritems():
396 for t, n in self.tags().iteritems():
397 self.nodetagscache.setdefault(n, []).append(t)
397 self.nodetagscache.setdefault(n, []).append(t)
398 for tags in self.nodetagscache.itervalues():
398 for tags in self.nodetagscache.itervalues():
399 tags.sort()
399 tags.sort()
400 return self.nodetagscache.get(node, [])
400 return self.nodetagscache.get(node, [])
401
401
402 def nodebookmarks(self, node):
402 def nodebookmarks(self, node):
403 marks = []
403 marks = []
404 for bookmark, n in self._bookmarks.iteritems():
404 for bookmark, n in self._bookmarks.iteritems():
405 if n == node:
405 if n == node:
406 marks.append(bookmark)
406 marks.append(bookmark)
407 return sorted(marks)
407 return sorted(marks)
408
408
409 def _branchtags(self, partial, lrev):
409 def _branchtags(self, partial, lrev):
410 # TODO: rename this function?
410 # TODO: rename this function?
411 tiprev = len(self) - 1
411 tiprev = len(self) - 1
412 if lrev != tiprev:
412 if lrev != tiprev:
413 ctxgen = (self[r] for r in xrange(lrev + 1, tiprev + 1))
413 ctxgen = (self[r] for r in xrange(lrev + 1, tiprev + 1))
414 self._updatebranchcache(partial, ctxgen)
414 self._updatebranchcache(partial, ctxgen)
415 self._writebranchcache(partial, self.changelog.tip(), tiprev)
415 self._writebranchcache(partial, self.changelog.tip(), tiprev)
416
416
417 return partial
417 return partial
418
418
419 def updatebranchcache(self):
419 def updatebranchcache(self):
420 tip = self.changelog.tip()
420 tip = self.changelog.tip()
421 if self._branchcache is not None and self._branchcachetip == tip:
421 if self._branchcache is not None and self._branchcachetip == tip:
422 return self._branchcache
422 return self._branchcache
423
423
424 oldtip = self._branchcachetip
424 oldtip = self._branchcachetip
425 self._branchcachetip = tip
425 self._branchcachetip = tip
426 if oldtip is None or oldtip not in self.changelog.nodemap:
426 if oldtip is None or oldtip not in self.changelog.nodemap:
427 partial, last, lrev = self._readbranchcache()
427 partial, last, lrev = self._readbranchcache()
428 else:
428 else:
429 lrev = self.changelog.rev(oldtip)
429 lrev = self.changelog.rev(oldtip)
430 partial = self._branchcache
430 partial = self._branchcache
431
431
432 self._branchtags(partial, lrev)
432 self._branchtags(partial, lrev)
433 # this private cache holds all heads (not just tips)
433 # this private cache holds all heads (not just tips)
434 self._branchcache = partial
434 self._branchcache = partial
435
435
436 def branchmap(self):
436 def branchmap(self):
437 '''returns a dictionary {branch: [branchheads]}'''
437 '''returns a dictionary {branch: [branchheads]}'''
438 self.updatebranchcache()
438 self.updatebranchcache()
439 return self._branchcache
439 return self._branchcache
440
440
441 def branchtags(self):
441 def branchtags(self):
442 '''return a dict where branch names map to the tipmost head of
442 '''return a dict where branch names map to the tipmost head of
443 the branch, open heads come before closed'''
443 the branch, open heads come before closed'''
444 bt = {}
444 bt = {}
445 for bn, heads in self.branchmap().iteritems():
445 for bn, heads in self.branchmap().iteritems():
446 tip = heads[-1]
446 tip = heads[-1]
447 for h in reversed(heads):
447 for h in reversed(heads):
448 if 'close' not in self.changelog.read(h)[5]:
448 if 'close' not in self.changelog.read(h)[5]:
449 tip = h
449 tip = h
450 break
450 break
451 bt[bn] = tip
451 bt[bn] = tip
452 return bt
452 return bt
453
453
454 def _readbranchcache(self):
454 def _readbranchcache(self):
455 partial = {}
455 partial = {}
456 try:
456 try:
457 f = self.opener("cache/branchheads")
457 f = self.opener("cache/branchheads")
458 lines = f.read().split('\n')
458 lines = f.read().split('\n')
459 f.close()
459 f.close()
460 except (IOError, OSError):
460 except (IOError, OSError):
461 return {}, nullid, nullrev
461 return {}, nullid, nullrev
462
462
463 try:
463 try:
464 last, lrev = lines.pop(0).split(" ", 1)
464 last, lrev = lines.pop(0).split(" ", 1)
465 last, lrev = bin(last), int(lrev)
465 last, lrev = bin(last), int(lrev)
466 if lrev >= len(self) or self[lrev].node() != last:
466 if lrev >= len(self) or self[lrev].node() != last:
467 # invalidate the cache
467 # invalidate the cache
468 raise ValueError('invalidating branch cache (tip differs)')
468 raise ValueError('invalidating branch cache (tip differs)')
469 for l in lines:
469 for l in lines:
470 if not l:
470 if not l:
471 continue
471 continue
472 node, label = l.split(" ", 1)
472 node, label = l.split(" ", 1)
473 label = encoding.tolocal(label.strip())
473 label = encoding.tolocal(label.strip())
474 partial.setdefault(label, []).append(bin(node))
474 partial.setdefault(label, []).append(bin(node))
475 except KeyboardInterrupt:
475 except KeyboardInterrupt:
476 raise
476 raise
477 except Exception, inst:
477 except Exception, inst:
478 if self.ui.debugflag:
478 if self.ui.debugflag:
479 self.ui.warn(str(inst), '\n')
479 self.ui.warn(str(inst), '\n')
480 partial, last, lrev = {}, nullid, nullrev
480 partial, last, lrev = {}, nullid, nullrev
481 return partial, last, lrev
481 return partial, last, lrev
482
482
483 def _writebranchcache(self, branches, tip, tiprev):
483 def _writebranchcache(self, branches, tip, tiprev):
484 try:
484 try:
485 f = self.opener("cache/branchheads", "w", atomictemp=True)
485 f = self.opener("cache/branchheads", "w", atomictemp=True)
486 f.write("%s %s\n" % (hex(tip), tiprev))
486 f.write("%s %s\n" % (hex(tip), tiprev))
487 for label, nodes in branches.iteritems():
487 for label, nodes in branches.iteritems():
488 for node in nodes:
488 for node in nodes:
489 f.write("%s %s\n" % (hex(node), encoding.fromlocal(label)))
489 f.write("%s %s\n" % (hex(node), encoding.fromlocal(label)))
490 f.rename()
490 f.rename()
491 except (IOError, OSError):
491 except (IOError, OSError):
492 pass
492 pass
493
493
494 def _updatebranchcache(self, partial, ctxgen):
494 def _updatebranchcache(self, partial, ctxgen):
495 # collect new branch entries
495 # collect new branch entries
496 newbranches = {}
496 newbranches = {}
497 for c in ctxgen:
497 for c in ctxgen:
498 newbranches.setdefault(c.branch(), []).append(c.node())
498 newbranches.setdefault(c.branch(), []).append(c.node())
499 # if older branchheads are reachable from new ones, they aren't
499 # if older branchheads are reachable from new ones, they aren't
500 # really branchheads. Note checking parents is insufficient:
500 # really branchheads. Note checking parents is insufficient:
501 # 1 (branch a) -> 2 (branch b) -> 3 (branch a)
501 # 1 (branch a) -> 2 (branch b) -> 3 (branch a)
502 for branch, newnodes in newbranches.iteritems():
502 for branch, newnodes in newbranches.iteritems():
503 bheads = partial.setdefault(branch, [])
503 bheads = partial.setdefault(branch, [])
504 bheads.extend(newnodes)
504 bheads.extend(newnodes)
505 if len(bheads) <= 1:
505 if len(bheads) <= 1:
506 continue
506 continue
507 bheads = sorted(bheads, key=lambda x: self[x].rev())
507 bheads = sorted(bheads, key=lambda x: self[x].rev())
508 # starting from tip means fewer passes over reachable
508 # starting from tip means fewer passes over reachable
509 while newnodes:
509 while newnodes:
510 latest = newnodes.pop()
510 latest = newnodes.pop()
511 if latest not in bheads:
511 if latest not in bheads:
512 continue
512 continue
513 minbhrev = self[bheads[0]].node()
513 minbhrev = self[bheads[0]].node()
514 reachable = self.changelog.reachable(latest, minbhrev)
514 reachable = self.changelog.reachable(latest, minbhrev)
515 reachable.remove(latest)
515 reachable.remove(latest)
516 if reachable:
516 if reachable:
517 bheads = [b for b in bheads if b not in reachable]
517 bheads = [b for b in bheads if b not in reachable]
518 partial[branch] = bheads
518 partial[branch] = bheads
519
519
520 def lookup(self, key):
520 def lookup(self, key):
521 if isinstance(key, int):
521 if isinstance(key, int):
522 return self.changelog.node(key)
522 return self.changelog.node(key)
523 elif key == '.':
523 elif key == '.':
524 return self.dirstate.p1()
524 return self.dirstate.p1()
525 elif key == 'null':
525 elif key == 'null':
526 return nullid
526 return nullid
527 elif key == 'tip':
527 elif key == 'tip':
528 return self.changelog.tip()
528 return self.changelog.tip()
529 n = self.changelog._match(key)
529 n = self.changelog._match(key)
530 if n:
530 if n:
531 return n
531 return n
532 if key in self._bookmarks:
532 if key in self._bookmarks:
533 return self._bookmarks[key]
533 return self._bookmarks[key]
534 if key in self.tags():
534 if key in self.tags():
535 return self.tags()[key]
535 return self.tags()[key]
536 if key in self.branchtags():
536 if key in self.branchtags():
537 return self.branchtags()[key]
537 return self.branchtags()[key]
538 n = self.changelog._partialmatch(key)
538 n = self.changelog._partialmatch(key)
539 if n:
539 if n:
540 return n
540 return n
541
541
542 # can't find key, check if it might have come from damaged dirstate
542 # can't find key, check if it might have come from damaged dirstate
543 if key in self.dirstate.parents():
543 if key in self.dirstate.parents():
544 raise error.Abort(_("working directory has unknown parent '%s'!")
544 raise error.Abort(_("working directory has unknown parent '%s'!")
545 % short(key))
545 % short(key))
546 try:
546 try:
547 if len(key) == 20:
547 if len(key) == 20:
548 key = hex(key)
548 key = hex(key)
549 except TypeError:
549 except TypeError:
550 pass
550 pass
551 raise error.RepoLookupError(_("unknown revision '%s'") % key)
551 raise error.RepoLookupError(_("unknown revision '%s'") % key)
552
552
553 def lookupbranch(self, key, remote=None):
553 def lookupbranch(self, key, remote=None):
554 repo = remote or self
554 repo = remote or self
555 if key in repo.branchmap():
555 if key in repo.branchmap():
556 return key
556 return key
557
557
558 repo = (remote and remote.local()) and remote or self
558 repo = (remote and remote.local()) and remote or self
559 return repo[key].branch()
559 return repo[key].branch()
560
560
561 def known(self, nodes):
561 def known(self, nodes):
562 nm = self.changelog.nodemap
562 nm = self.changelog.nodemap
563 return [(n in nm) for n in nodes]
563 return [(n in nm) for n in nodes]
564
564
565 def local(self):
565 def local(self):
566 return True
566 return True
567
567
568 def join(self, f):
568 def join(self, f):
569 return os.path.join(self.path, f)
569 return os.path.join(self.path, f)
570
570
571 def wjoin(self, f):
571 def wjoin(self, f):
572 return os.path.join(self.root, f)
572 return os.path.join(self.root, f)
573
573
574 def file(self, f):
574 def file(self, f):
575 if f[0] == '/':
575 if f[0] == '/':
576 f = f[1:]
576 f = f[1:]
577 return filelog.filelog(self.sopener, f)
577 return filelog.filelog(self.sopener, f)
578
578
579 def changectx(self, changeid):
579 def changectx(self, changeid):
580 return self[changeid]
580 return self[changeid]
581
581
582 def parents(self, changeid=None):
582 def parents(self, changeid=None):
583 '''get list of changectxs for parents of changeid'''
583 '''get list of changectxs for parents of changeid'''
584 return self[changeid].parents()
584 return self[changeid].parents()
585
585
586 def filectx(self, path, changeid=None, fileid=None):
586 def filectx(self, path, changeid=None, fileid=None):
587 """changeid can be a changeset revision, node, or tag.
587 """changeid can be a changeset revision, node, or tag.
588 fileid can be a file revision or node."""
588 fileid can be a file revision or node."""
589 return context.filectx(self, path, changeid, fileid)
589 return context.filectx(self, path, changeid, fileid)
590
590
591 def getcwd(self):
591 def getcwd(self):
592 return self.dirstate.getcwd()
592 return self.dirstate.getcwd()
593
593
594 def pathto(self, f, cwd=None):
594 def pathto(self, f, cwd=None):
595 return self.dirstate.pathto(f, cwd)
595 return self.dirstate.pathto(f, cwd)
596
596
597 def wfile(self, f, mode='r'):
597 def wfile(self, f, mode='r'):
598 return self.wopener(f, mode)
598 return self.wopener(f, mode)
599
599
600 def _link(self, f):
600 def _link(self, f):
601 return os.path.islink(self.wjoin(f))
601 return os.path.islink(self.wjoin(f))
602
602
603 def _loadfilter(self, filter):
603 def _loadfilter(self, filter):
604 if filter not in self.filterpats:
604 if filter not in self.filterpats:
605 l = []
605 l = []
606 for pat, cmd in self.ui.configitems(filter):
606 for pat, cmd in self.ui.configitems(filter):
607 if cmd == '!':
607 if cmd == '!':
608 continue
608 continue
609 mf = matchmod.match(self.root, '', [pat])
609 mf = matchmod.match(self.root, '', [pat])
610 fn = None
610 fn = None
611 params = cmd
611 params = cmd
612 for name, filterfn in self._datafilters.iteritems():
612 for name, filterfn in self._datafilters.iteritems():
613 if cmd.startswith(name):
613 if cmd.startswith(name):
614 fn = filterfn
614 fn = filterfn
615 params = cmd[len(name):].lstrip()
615 params = cmd[len(name):].lstrip()
616 break
616 break
617 if not fn:
617 if not fn:
618 fn = lambda s, c, **kwargs: util.filter(s, c)
618 fn = lambda s, c, **kwargs: util.filter(s, c)
619 # Wrap old filters not supporting keyword arguments
619 # Wrap old filters not supporting keyword arguments
620 if not inspect.getargspec(fn)[2]:
620 if not inspect.getargspec(fn)[2]:
621 oldfn = fn
621 oldfn = fn
622 fn = lambda s, c, **kwargs: oldfn(s, c)
622 fn = lambda s, c, **kwargs: oldfn(s, c)
623 l.append((mf, fn, params))
623 l.append((mf, fn, params))
624 self.filterpats[filter] = l
624 self.filterpats[filter] = l
625 return self.filterpats[filter]
625 return self.filterpats[filter]
626
626
627 def _filter(self, filterpats, filename, data):
627 def _filter(self, filterpats, filename, data):
628 for mf, fn, cmd in filterpats:
628 for mf, fn, cmd in filterpats:
629 if mf(filename):
629 if mf(filename):
630 self.ui.debug("filtering %s through %s\n" % (filename, cmd))
630 self.ui.debug("filtering %s through %s\n" % (filename, cmd))
631 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
631 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
632 break
632 break
633
633
634 return data
634 return data
635
635
636 @propertycache
636 @propertycache
637 def _encodefilterpats(self):
637 def _encodefilterpats(self):
638 return self._loadfilter('encode')
638 return self._loadfilter('encode')
639
639
640 @propertycache
640 @propertycache
641 def _decodefilterpats(self):
641 def _decodefilterpats(self):
642 return self._loadfilter('decode')
642 return self._loadfilter('decode')
643
643
644 def adddatafilter(self, name, filter):
644 def adddatafilter(self, name, filter):
645 self._datafilters[name] = filter
645 self._datafilters[name] = filter
646
646
647 def wread(self, filename):
647 def wread(self, filename):
648 if self._link(filename):
648 if self._link(filename):
649 data = os.readlink(self.wjoin(filename))
649 data = os.readlink(self.wjoin(filename))
650 else:
650 else:
651 data = self.wopener.read(filename)
651 data = self.wopener.read(filename)
652 return self._filter(self._encodefilterpats, filename, data)
652 return self._filter(self._encodefilterpats, filename, data)
653
653
654 def wwrite(self, filename, data, flags):
654 def wwrite(self, filename, data, flags):
655 data = self._filter(self._decodefilterpats, filename, data)
655 data = self._filter(self._decodefilterpats, filename, data)
656 if 'l' in flags:
656 if 'l' in flags:
657 self.wopener.symlink(data, filename)
657 self.wopener.symlink(data, filename)
658 else:
658 else:
659 self.wopener.write(filename, data)
659 self.wopener.write(filename, data)
660 if 'x' in flags:
660 if 'x' in flags:
661 util.setflags(self.wjoin(filename), False, True)
661 util.setflags(self.wjoin(filename), False, True)
662
662
663 def wwritedata(self, filename, data):
663 def wwritedata(self, filename, data):
664 return self._filter(self._decodefilterpats, filename, data)
664 return self._filter(self._decodefilterpats, filename, data)
665
665
666 def transaction(self, desc):
666 def transaction(self, desc):
667 tr = self._transref and self._transref() or None
667 tr = self._transref and self._transref() or None
668 if tr and tr.running():
668 if tr and tr.running():
669 return tr.nest()
669 return tr.nest()
670
670
671 # abort here if the journal already exists
671 # abort here if the journal already exists
672 if os.path.exists(self.sjoin("journal")):
672 if os.path.exists(self.sjoin("journal")):
673 raise error.RepoError(
673 raise error.RepoError(
674 _("abandoned transaction found - run hg recover"))
674 _("abandoned transaction found - run hg recover"))
675
675
676 journalfiles = self._writejournal(desc)
676 journalfiles = self._writejournal(desc)
677 renames = [(x, undoname(x)) for x in journalfiles]
677 renames = [(x, undoname(x)) for x in journalfiles]
678
678
679 tr = transaction.transaction(self.ui.warn, self.sopener,
679 tr = transaction.transaction(self.ui.warn, self.sopener,
680 self.sjoin("journal"),
680 self.sjoin("journal"),
681 aftertrans(renames),
681 aftertrans(renames),
682 self.store.createmode)
682 self.store.createmode)
683 self._transref = weakref.ref(tr)
683 self._transref = weakref.ref(tr)
684 return tr
684 return tr
685
685
686 def _writejournal(self, desc):
686 def _writejournal(self, desc):
687 # save dirstate for rollback
687 # save dirstate for rollback
688 try:
688 try:
689 ds = self.opener.read("dirstate")
689 ds = self.opener.read("dirstate")
690 except IOError:
690 except IOError:
691 ds = ""
691 ds = ""
692 self.opener.write("journal.dirstate", ds)
692 self.opener.write("journal.dirstate", ds)
693 self.opener.write("journal.branch",
693 self.opener.write("journal.branch",
694 encoding.fromlocal(self.dirstate.branch()))
694 encoding.fromlocal(self.dirstate.branch()))
695 self.opener.write("journal.desc",
695 self.opener.write("journal.desc",
696 "%d\n%s\n" % (len(self), desc))
696 "%d\n%s\n" % (len(self), desc))
697
697
698 bkname = self.join('bookmarks')
698 bkname = self.join('bookmarks')
699 if os.path.exists(bkname):
699 if os.path.exists(bkname):
700 util.copyfile(bkname, self.join('journal.bookmarks'))
700 util.copyfile(bkname, self.join('journal.bookmarks'))
701 else:
701 else:
702 self.opener.write('journal.bookmarks', '')
702 self.opener.write('journal.bookmarks', '')
703
703
704 return (self.sjoin('journal'), self.join('journal.dirstate'),
704 return (self.sjoin('journal'), self.join('journal.dirstate'),
705 self.join('journal.branch'), self.join('journal.desc'),
705 self.join('journal.branch'), self.join('journal.desc'),
706 self.join('journal.bookmarks'))
706 self.join('journal.bookmarks'))
707
707
708 def recover(self):
708 def recover(self):
709 lock = self.lock()
709 lock = self.lock()
710 try:
710 try:
711 if os.path.exists(self.sjoin("journal")):
711 if os.path.exists(self.sjoin("journal")):
712 self.ui.status(_("rolling back interrupted transaction\n"))
712 self.ui.status(_("rolling back interrupted transaction\n"))
713 transaction.rollback(self.sopener, self.sjoin("journal"),
713 transaction.rollback(self.sopener, self.sjoin("journal"),
714 self.ui.warn)
714 self.ui.warn)
715 self.invalidate()
715 self.invalidate()
716 return True
716 return True
717 else:
717 else:
718 self.ui.warn(_("no interrupted transaction available\n"))
718 self.ui.warn(_("no interrupted transaction available\n"))
719 return False
719 return False
720 finally:
720 finally:
721 lock.release()
721 lock.release()
722
722
723 def rollback(self, dryrun=False):
723 def rollback(self, dryrun=False):
724 wlock = lock = None
724 wlock = lock = None
725 try:
725 try:
726 wlock = self.wlock()
726 wlock = self.wlock()
727 lock = self.lock()
727 lock = self.lock()
728 if os.path.exists(self.sjoin("undo")):
728 if os.path.exists(self.sjoin("undo")):
729 try:
729 try:
730 args = self.opener.read("undo.desc").splitlines()
730 args = self.opener.read("undo.desc").splitlines()
731 if len(args) >= 3 and self.ui.verbose:
731 if len(args) >= 3 and self.ui.verbose:
732 desc = _("repository tip rolled back to revision %s"
732 desc = _("repository tip rolled back to revision %s"
733 " (undo %s: %s)\n") % (
733 " (undo %s: %s)\n") % (
734 int(args[0]) - 1, args[1], args[2])
734 int(args[0]) - 1, args[1], args[2])
735 elif len(args) >= 2:
735 elif len(args) >= 2:
736 desc = _("repository tip rolled back to revision %s"
736 desc = _("repository tip rolled back to revision %s"
737 " (undo %s)\n") % (
737 " (undo %s)\n") % (
738 int(args[0]) - 1, args[1])
738 int(args[0]) - 1, args[1])
739 except IOError:
739 except IOError:
740 desc = _("rolling back unknown transaction\n")
740 desc = _("rolling back unknown transaction\n")
741 self.ui.status(desc)
741 self.ui.status(desc)
742 if dryrun:
742 if dryrun:
743 return
743 return
744 transaction.rollback(self.sopener, self.sjoin("undo"),
744 transaction.rollback(self.sopener, self.sjoin("undo"),
745 self.ui.warn)
745 self.ui.warn)
746 util.rename(self.join("undo.dirstate"), self.join("dirstate"))
746 util.rename(self.join("undo.dirstate"), self.join("dirstate"))
747 if os.path.exists(self.join('undo.bookmarks')):
747 if os.path.exists(self.join('undo.bookmarks')):
748 util.rename(self.join('undo.bookmarks'),
748 util.rename(self.join('undo.bookmarks'),
749 self.join('bookmarks'))
749 self.join('bookmarks'))
750 try:
750 try:
751 branch = self.opener.read("undo.branch")
751 branch = self.opener.read("undo.branch")
752 self.dirstate.setbranch(branch)
752 self.dirstate.setbranch(branch)
753 except IOError:
753 except IOError:
754 self.ui.warn(_("named branch could not be reset, "
754 self.ui.warn(_("named branch could not be reset, "
755 "current branch is still: %s\n")
755 "current branch is still: %s\n")
756 % self.dirstate.branch())
756 % self.dirstate.branch())
757 self.invalidate()
757 self.invalidate()
758 self.dirstate.invalidate()
758 self.dirstate.invalidate()
759 self.destroyed()
759 self.destroyed()
760 parents = tuple([p.rev() for p in self.parents()])
760 parents = tuple([p.rev() for p in self.parents()])
761 if len(parents) > 1:
761 if len(parents) > 1:
762 self.ui.status(_("working directory now based on "
762 self.ui.status(_("working directory now based on "
763 "revisions %d and %d\n") % parents)
763 "revisions %d and %d\n") % parents)
764 else:
764 else:
765 self.ui.status(_("working directory now based on "
765 self.ui.status(_("working directory now based on "
766 "revision %d\n") % parents)
766 "revision %d\n") % parents)
767 else:
767 else:
768 self.ui.warn(_("no rollback information available\n"))
768 self.ui.warn(_("no rollback information available\n"))
769 return 1
769 return 1
770 finally:
770 finally:
771 release(lock, wlock)
771 release(lock, wlock)
772
772
773 def invalidatecaches(self):
773 def invalidatecaches(self):
774 self._tags = None
774 self._tags = None
775 self._tagtypes = None
775 self._tagtypes = None
776 self.nodetagscache = None
776 self.nodetagscache = None
777 self._branchcache = None # in UTF-8
777 self._branchcache = None # in UTF-8
778 self._branchcachetip = None
778 self._branchcachetip = None
779
779
780 def invalidate(self):
780 def invalidate(self):
781 for a in ("changelog", "manifest", "_bookmarks", "_bookmarkcurrent"):
781 for a in ("changelog", "manifest", "_bookmarks", "_bookmarkcurrent"):
782 if a in self.__dict__:
782 if a in self.__dict__:
783 delattr(self, a)
783 delattr(self, a)
784 self.invalidatecaches()
784 self.invalidatecaches()
785
785
786 def _lock(self, lockname, wait, releasefn, acquirefn, desc):
786 def _lock(self, lockname, wait, releasefn, acquirefn, desc):
787 try:
787 try:
788 l = lock.lock(lockname, 0, releasefn, desc=desc)
788 l = lock.lock(lockname, 0, releasefn, desc=desc)
789 except error.LockHeld, inst:
789 except error.LockHeld, inst:
790 if not wait:
790 if not wait:
791 raise
791 raise
792 self.ui.warn(_("waiting for lock on %s held by %r\n") %
792 self.ui.warn(_("waiting for lock on %s held by %r\n") %
793 (desc, inst.locker))
793 (desc, inst.locker))
794 # default to 600 seconds timeout
794 # default to 600 seconds timeout
795 l = lock.lock(lockname, int(self.ui.config("ui", "timeout", "600")),
795 l = lock.lock(lockname, int(self.ui.config("ui", "timeout", "600")),
796 releasefn, desc=desc)
796 releasefn, desc=desc)
797 if acquirefn:
797 if acquirefn:
798 acquirefn()
798 acquirefn()
799 return l
799 return l
800
800
801 def lock(self, wait=True):
801 def lock(self, wait=True):
802 '''Lock the repository store (.hg/store) and return a weak reference
802 '''Lock the repository store (.hg/store) and return a weak reference
803 to the lock. Use this before modifying the store (e.g. committing or
803 to the lock. Use this before modifying the store (e.g. committing or
804 stripping). If you are opening a transaction, get a lock as well.)'''
804 stripping). If you are opening a transaction, get a lock as well.)'''
805 l = self._lockref and self._lockref()
805 l = self._lockref and self._lockref()
806 if l is not None and l.held:
806 if l is not None and l.held:
807 l.lock()
807 l.lock()
808 return l
808 return l
809
809
810 l = self._lock(self.sjoin("lock"), wait, self.store.write,
810 l = self._lock(self.sjoin("lock"), wait, self.store.write,
811 self.invalidate, _('repository %s') % self.origroot)
811 self.invalidate, _('repository %s') % self.origroot)
812 self._lockref = weakref.ref(l)
812 self._lockref = weakref.ref(l)
813 return l
813 return l
814
814
815 def wlock(self, wait=True):
815 def wlock(self, wait=True):
816 '''Lock the non-store parts of the repository (everything under
816 '''Lock the non-store parts of the repository (everything under
817 .hg except .hg/store) and return a weak reference to the lock.
817 .hg except .hg/store) and return a weak reference to the lock.
818 Use this before modifying files in .hg.'''
818 Use this before modifying files in .hg.'''
819 l = self._wlockref and self._wlockref()
819 l = self._wlockref and self._wlockref()
820 if l is not None and l.held:
820 if l is not None and l.held:
821 l.lock()
821 l.lock()
822 return l
822 return l
823
823
824 l = self._lock(self.join("wlock"), wait, self.dirstate.write,
824 l = self._lock(self.join("wlock"), wait, self.dirstate.write,
825 self.dirstate.invalidate, _('working directory of %s') %
825 self.dirstate.invalidate, _('working directory of %s') %
826 self.origroot)
826 self.origroot)
827 self._wlockref = weakref.ref(l)
827 self._wlockref = weakref.ref(l)
828 return l
828 return l
829
829
830 def _filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist):
830 def _filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist):
831 """
831 """
832 commit an individual file as part of a larger transaction
832 commit an individual file as part of a larger transaction
833 """
833 """
834
834
835 fname = fctx.path()
835 fname = fctx.path()
836 text = fctx.data()
836 text = fctx.data()
837 flog = self.file(fname)
837 flog = self.file(fname)
838 fparent1 = manifest1.get(fname, nullid)
838 fparent1 = manifest1.get(fname, nullid)
839 fparent2 = fparent2o = manifest2.get(fname, nullid)
839 fparent2 = fparent2o = manifest2.get(fname, nullid)
840
840
841 meta = {}
841 meta = {}
842 copy = fctx.renamed()
842 copy = fctx.renamed()
843 if copy and copy[0] != fname:
843 if copy and copy[0] != fname:
844 # Mark the new revision of this file as a copy of another
844 # Mark the new revision of this file as a copy of another
845 # file. This copy data will effectively act as a parent
845 # file. This copy data will effectively act as a parent
846 # of this new revision. If this is a merge, the first
846 # of this new revision. If this is a merge, the first
847 # parent will be the nullid (meaning "look up the copy data")
847 # parent will be the nullid (meaning "look up the copy data")
848 # and the second one will be the other parent. For example:
848 # and the second one will be the other parent. For example:
849 #
849 #
850 # 0 --- 1 --- 3 rev1 changes file foo
850 # 0 --- 1 --- 3 rev1 changes file foo
851 # \ / rev2 renames foo to bar and changes it
851 # \ / rev2 renames foo to bar and changes it
852 # \- 2 -/ rev3 should have bar with all changes and
852 # \- 2 -/ rev3 should have bar with all changes and
853 # should record that bar descends from
853 # should record that bar descends from
854 # bar in rev2 and foo in rev1
854 # bar in rev2 and foo in rev1
855 #
855 #
856 # this allows this merge to succeed:
856 # this allows this merge to succeed:
857 #
857 #
858 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
858 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
859 # \ / merging rev3 and rev4 should use bar@rev2
859 # \ / merging rev3 and rev4 should use bar@rev2
860 # \- 2 --- 4 as the merge base
860 # \- 2 --- 4 as the merge base
861 #
861 #
862
862
863 cfname = copy[0]
863 cfname = copy[0]
864 crev = manifest1.get(cfname)
864 crev = manifest1.get(cfname)
865 newfparent = fparent2
865 newfparent = fparent2
866
866
867 if manifest2: # branch merge
867 if manifest2: # branch merge
868 if fparent2 == nullid or crev is None: # copied on remote side
868 if fparent2 == nullid or crev is None: # copied on remote side
869 if cfname in manifest2:
869 if cfname in manifest2:
870 crev = manifest2[cfname]
870 crev = manifest2[cfname]
871 newfparent = fparent1
871 newfparent = fparent1
872
872
873 # find source in nearest ancestor if we've lost track
873 # find source in nearest ancestor if we've lost track
874 if not crev:
874 if not crev:
875 self.ui.debug(" %s: searching for copy revision for %s\n" %
875 self.ui.debug(" %s: searching for copy revision for %s\n" %
876 (fname, cfname))
876 (fname, cfname))
877 for ancestor in self[None].ancestors():
877 for ancestor in self[None].ancestors():
878 if cfname in ancestor:
878 if cfname in ancestor:
879 crev = ancestor[cfname].filenode()
879 crev = ancestor[cfname].filenode()
880 break
880 break
881
881
882 if crev:
882 if crev:
883 self.ui.debug(" %s: copy %s:%s\n" % (fname, cfname, hex(crev)))
883 self.ui.debug(" %s: copy %s:%s\n" % (fname, cfname, hex(crev)))
884 meta["copy"] = cfname
884 meta["copy"] = cfname
885 meta["copyrev"] = hex(crev)
885 meta["copyrev"] = hex(crev)
886 fparent1, fparent2 = nullid, newfparent
886 fparent1, fparent2 = nullid, newfparent
887 else:
887 else:
888 self.ui.warn(_("warning: can't find ancestor for '%s' "
888 self.ui.warn(_("warning: can't find ancestor for '%s' "
889 "copied from '%s'!\n") % (fname, cfname))
889 "copied from '%s'!\n") % (fname, cfname))
890
890
891 elif fparent2 != nullid:
891 elif fparent2 != nullid:
892 # is one parent an ancestor of the other?
892 # is one parent an ancestor of the other?
893 fparentancestor = flog.ancestor(fparent1, fparent2)
893 fparentancestor = flog.ancestor(fparent1, fparent2)
894 if fparentancestor == fparent1:
894 if fparentancestor == fparent1:
895 fparent1, fparent2 = fparent2, nullid
895 fparent1, fparent2 = fparent2, nullid
896 elif fparentancestor == fparent2:
896 elif fparentancestor == fparent2:
897 fparent2 = nullid
897 fparent2 = nullid
898
898
899 # is the file changed?
899 # is the file changed?
900 if fparent2 != nullid or flog.cmp(fparent1, text) or meta:
900 if fparent2 != nullid or flog.cmp(fparent1, text) or meta:
901 changelist.append(fname)
901 changelist.append(fname)
902 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
902 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
903
903
904 # are just the flags changed during merge?
904 # are just the flags changed during merge?
905 if fparent1 != fparent2o and manifest1.flags(fname) != fctx.flags():
905 if fparent1 != fparent2o and manifest1.flags(fname) != fctx.flags():
906 changelist.append(fname)
906 changelist.append(fname)
907
907
908 return fparent1
908 return fparent1
909
909
910 def commit(self, text="", user=None, date=None, match=None, force=False,
910 def commit(self, text="", user=None, date=None, match=None, force=False,
911 editor=False, extra={}):
911 editor=False, extra={}):
912 """Add a new revision to current repository.
912 """Add a new revision to current repository.
913
913
914 Revision information is gathered from the working directory,
914 Revision information is gathered from the working directory,
915 match can be used to filter the committed files. If editor is
915 match can be used to filter the committed files. If editor is
916 supplied, it is called to get a commit message.
916 supplied, it is called to get a commit message.
917 """
917 """
918
918
919 def fail(f, msg):
919 def fail(f, msg):
920 raise util.Abort('%s: %s' % (f, msg))
920 raise util.Abort('%s: %s' % (f, msg))
921
921
922 if not match:
922 if not match:
923 match = matchmod.always(self.root, '')
923 match = matchmod.always(self.root, '')
924
924
925 if not force:
925 if not force:
926 vdirs = []
926 vdirs = []
927 match.dir = vdirs.append
927 match.dir = vdirs.append
928 match.bad = fail
928 match.bad = fail
929
929
930 wlock = self.wlock()
930 wlock = self.wlock()
931 try:
931 try:
932 wctx = self[None]
932 wctx = self[None]
933 merge = len(wctx.parents()) > 1
933 merge = len(wctx.parents()) > 1
934
934
935 if (not force and merge and match and
935 if (not force and merge and match and
936 (match.files() or match.anypats())):
936 (match.files() or match.anypats())):
937 raise util.Abort(_('cannot partially commit a merge '
937 raise util.Abort(_('cannot partially commit a merge '
938 '(do not specify files or patterns)'))
938 '(do not specify files or patterns)'))
939
939
940 changes = self.status(match=match, clean=force)
940 changes = self.status(match=match, clean=force)
941 if force:
941 if force:
942 changes[0].extend(changes[6]) # mq may commit unchanged files
942 changes[0].extend(changes[6]) # mq may commit unchanged files
943
943
944 # check subrepos
944 # check subrepos
945 subs = []
945 subs = []
946 removedsubs = set()
946 removedsubs = set()
947 if '.hgsub' in wctx:
947 if '.hgsub' in wctx:
948 # only manage subrepos and .hgsubstate if .hgsub is present
948 # only manage subrepos and .hgsubstate if .hgsub is present
949 for p in wctx.parents():
949 for p in wctx.parents():
950 removedsubs.update(s for s in p.substate if match(s))
950 removedsubs.update(s for s in p.substate if match(s))
951 for s in wctx.substate:
951 for s in wctx.substate:
952 removedsubs.discard(s)
952 removedsubs.discard(s)
953 if match(s) and wctx.sub(s).dirty():
953 if match(s) and wctx.sub(s).dirty():
954 subs.append(s)
954 subs.append(s)
955 if (subs or removedsubs):
955 if (subs or removedsubs):
956 if (not match('.hgsub') and
956 if (not match('.hgsub') and
957 '.hgsub' in (wctx.modified() + wctx.added())):
957 '.hgsub' in (wctx.modified() + wctx.added())):
958 raise util.Abort(
958 raise util.Abort(
959 _("can't commit subrepos without .hgsub"))
959 _("can't commit subrepos without .hgsub"))
960 if '.hgsubstate' not in changes[0]:
960 if '.hgsubstate' not in changes[0]:
961 changes[0].insert(0, '.hgsubstate')
961 changes[0].insert(0, '.hgsubstate')
962 if '.hgsubstate' in changes[2]:
962 if '.hgsubstate' in changes[2]:
963 changes[2].remove('.hgsubstate')
963 changes[2].remove('.hgsubstate')
964 elif '.hgsub' in changes[2]:
964 elif '.hgsub' in changes[2]:
965 # clean up .hgsubstate when .hgsub is removed
965 # clean up .hgsubstate when .hgsub is removed
966 if ('.hgsubstate' in wctx and
966 if ('.hgsubstate' in wctx and
967 '.hgsubstate' not in changes[0] + changes[1] + changes[2]):
967 '.hgsubstate' not in changes[0] + changes[1] + changes[2]):
968 changes[2].insert(0, '.hgsubstate')
968 changes[2].insert(0, '.hgsubstate')
969
969
970 if subs and not self.ui.configbool('ui', 'commitsubrepos', True):
970 if subs and not self.ui.configbool('ui', 'commitsubrepos', True):
971 changedsubs = [s for s in subs if wctx.sub(s).dirty(True)]
971 changedsubs = [s for s in subs if wctx.sub(s).dirty(True)]
972 if changedsubs:
972 if changedsubs:
973 raise util.Abort(_("uncommitted changes in subrepo %s")
973 raise util.Abort(_("uncommitted changes in subrepo %s")
974 % changedsubs[0])
974 % changedsubs[0])
975
975
976 # make sure all explicit patterns are matched
976 # make sure all explicit patterns are matched
977 if not force and match.files():
977 if not force and match.files():
978 matched = set(changes[0] + changes[1] + changes[2])
978 matched = set(changes[0] + changes[1] + changes[2])
979
979
980 for f in match.files():
980 for f in match.files():
981 if f == '.' or f in matched or f in wctx.substate:
981 if f == '.' or f in matched or f in wctx.substate:
982 continue
982 continue
983 if f in changes[3]: # missing
983 if f in changes[3]: # missing
984 fail(f, _('file not found!'))
984 fail(f, _('file not found!'))
985 if f in vdirs: # visited directory
985 if f in vdirs: # visited directory
986 d = f + '/'
986 d = f + '/'
987 for mf in matched:
987 for mf in matched:
988 if mf.startswith(d):
988 if mf.startswith(d):
989 break
989 break
990 else:
990 else:
991 fail(f, _("no match under directory!"))
991 fail(f, _("no match under directory!"))
992 elif f not in self.dirstate:
992 elif f not in self.dirstate:
993 fail(f, _("file not tracked!"))
993 fail(f, _("file not tracked!"))
994
994
995 if (not force and not extra.get("close") and not merge
995 if (not force and not extra.get("close") and not merge
996 and not (changes[0] or changes[1] or changes[2])
996 and not (changes[0] or changes[1] or changes[2])
997 and wctx.branch() == wctx.p1().branch()):
997 and wctx.branch() == wctx.p1().branch()):
998 return None
998 return None
999
999
1000 ms = mergemod.mergestate(self)
1000 ms = mergemod.mergestate(self)
1001 for f in changes[0]:
1001 for f in changes[0]:
1002 if f in ms and ms[f] == 'u':
1002 if f in ms and ms[f] == 'u':
1003 raise util.Abort(_("unresolved merge conflicts "
1003 raise util.Abort(_("unresolved merge conflicts "
1004 "(see hg help resolve)"))
1004 "(see hg help resolve)"))
1005
1005
1006 cctx = context.workingctx(self, text, user, date, extra, changes)
1006 cctx = context.workingctx(self, text, user, date, extra, changes)
1007 if editor:
1007 if editor:
1008 cctx._text = editor(self, cctx, subs)
1008 cctx._text = editor(self, cctx, subs)
1009 edited = (text != cctx._text)
1009 edited = (text != cctx._text)
1010
1010
1011 # commit subs
1011 # commit subs
1012 if subs or removedsubs:
1012 if subs or removedsubs:
1013 state = wctx.substate.copy()
1013 state = wctx.substate.copy()
1014 for s in sorted(subs):
1014 for s in sorted(subs):
1015 sub = wctx.sub(s)
1015 sub = wctx.sub(s)
1016 self.ui.status(_('committing subrepository %s\n') %
1016 self.ui.status(_('committing subrepository %s\n') %
1017 subrepo.subrelpath(sub))
1017 subrepo.subrelpath(sub))
1018 sr = sub.commit(cctx._text, user, date)
1018 sr = sub.commit(cctx._text, user, date)
1019 state[s] = (state[s][0], sr)
1019 state[s] = (state[s][0], sr)
1020 subrepo.writestate(self, state)
1020 subrepo.writestate(self, state)
1021
1021
1022 # Save commit message in case this transaction gets rolled back
1022 # Save commit message in case this transaction gets rolled back
1023 # (e.g. by a pretxncommit hook). Leave the content alone on
1023 # (e.g. by a pretxncommit hook). Leave the content alone on
1024 # the assumption that the user will use the same editor again.
1024 # the assumption that the user will use the same editor again.
1025 msgfn = self.savecommitmessage(cctx._text)
1025 msgfn = self.savecommitmessage(cctx._text)
1026
1026
1027 p1, p2 = self.dirstate.parents()
1027 p1, p2 = self.dirstate.parents()
1028 hookp1, hookp2 = hex(p1), (p2 != nullid and hex(p2) or '')
1028 hookp1, hookp2 = hex(p1), (p2 != nullid and hex(p2) or '')
1029 try:
1029 try:
1030 self.hook("precommit", throw=True, parent1=hookp1, parent2=hookp2)
1030 self.hook("precommit", throw=True, parent1=hookp1, parent2=hookp2)
1031 ret = self.commitctx(cctx, True)
1031 ret = self.commitctx(cctx, True)
1032 except:
1032 except:
1033 if edited:
1033 if edited:
1034 self.ui.write(
1034 self.ui.write(
1035 _('note: commit message saved in %s\n') % msgfn)
1035 _('note: commit message saved in %s\n') % msgfn)
1036 raise
1036 raise
1037
1037
1038 # update bookmarks, dirstate and mergestate
1038 # update bookmarks, dirstate and mergestate
1039 bookmarks.update(self, p1, ret)
1039 bookmarks.update(self, p1, ret)
1040 for f in changes[0] + changes[1]:
1040 for f in changes[0] + changes[1]:
1041 self.dirstate.normal(f)
1041 self.dirstate.normal(f)
1042 for f in changes[2]:
1042 for f in changes[2]:
1043 self.dirstate.drop(f)
1043 self.dirstate.drop(f)
1044 self.dirstate.setparents(ret)
1044 self.dirstate.setparents(ret)
1045 ms.reset()
1045 ms.reset()
1046 finally:
1046 finally:
1047 wlock.release()
1047 wlock.release()
1048
1048
1049 self.hook("commit", node=hex(ret), parent1=hookp1, parent2=hookp2)
1049 self.hook("commit", node=hex(ret), parent1=hookp1, parent2=hookp2)
1050 return ret
1050 return ret
1051
1051
1052 def commitctx(self, ctx, error=False):
1052 def commitctx(self, ctx, error=False):
1053 """Add a new revision to current repository.
1053 """Add a new revision to current repository.
1054 Revision information is passed via the context argument.
1054 Revision information is passed via the context argument.
1055 """
1055 """
1056
1056
1057 tr = lock = None
1057 tr = lock = None
1058 removed = list(ctx.removed())
1058 removed = list(ctx.removed())
1059 p1, p2 = ctx.p1(), ctx.p2()
1059 p1, p2 = ctx.p1(), ctx.p2()
1060 user = ctx.user()
1060 user = ctx.user()
1061
1061
1062 lock = self.lock()
1062 lock = self.lock()
1063 try:
1063 try:
1064 tr = self.transaction("commit")
1064 tr = self.transaction("commit")
1065 trp = weakref.proxy(tr)
1065 trp = weakref.proxy(tr)
1066
1066
1067 if ctx.files():
1067 if ctx.files():
1068 m1 = p1.manifest().copy()
1068 m1 = p1.manifest().copy()
1069 m2 = p2.manifest()
1069 m2 = p2.manifest()
1070
1070
1071 # check in files
1071 # check in files
1072 new = {}
1072 new = {}
1073 changed = []
1073 changed = []
1074 linkrev = len(self)
1074 linkrev = len(self)
1075 for f in sorted(ctx.modified() + ctx.added()):
1075 for f in sorted(ctx.modified() + ctx.added()):
1076 self.ui.note(f + "\n")
1076 self.ui.note(f + "\n")
1077 try:
1077 try:
1078 fctx = ctx[f]
1078 fctx = ctx[f]
1079 new[f] = self._filecommit(fctx, m1, m2, linkrev, trp,
1079 new[f] = self._filecommit(fctx, m1, m2, linkrev, trp,
1080 changed)
1080 changed)
1081 m1.set(f, fctx.flags())
1081 m1.set(f, fctx.flags())
1082 except OSError, inst:
1082 except OSError, inst:
1083 self.ui.warn(_("trouble committing %s!\n") % f)
1083 self.ui.warn(_("trouble committing %s!\n") % f)
1084 raise
1084 raise
1085 except IOError, inst:
1085 except IOError, inst:
1086 errcode = getattr(inst, 'errno', errno.ENOENT)
1086 errcode = getattr(inst, 'errno', errno.ENOENT)
1087 if error or errcode and errcode != errno.ENOENT:
1087 if error or errcode and errcode != errno.ENOENT:
1088 self.ui.warn(_("trouble committing %s!\n") % f)
1088 self.ui.warn(_("trouble committing %s!\n") % f)
1089 raise
1089 raise
1090 else:
1090 else:
1091 removed.append(f)
1091 removed.append(f)
1092
1092
1093 # update manifest
1093 # update manifest
1094 m1.update(new)
1094 m1.update(new)
1095 removed = [f for f in sorted(removed) if f in m1 or f in m2]
1095 removed = [f for f in sorted(removed) if f in m1 or f in m2]
1096 drop = [f for f in removed if f in m1]
1096 drop = [f for f in removed if f in m1]
1097 for f in drop:
1097 for f in drop:
1098 del m1[f]
1098 del m1[f]
1099 mn = self.manifest.add(m1, trp, linkrev, p1.manifestnode(),
1099 mn = self.manifest.add(m1, trp, linkrev, p1.manifestnode(),
1100 p2.manifestnode(), (new, drop))
1100 p2.manifestnode(), (new, drop))
1101 files = changed + removed
1101 files = changed + removed
1102 else:
1102 else:
1103 mn = p1.manifestnode()
1103 mn = p1.manifestnode()
1104 files = []
1104 files = []
1105
1105
1106 # update changelog
1106 # update changelog
1107 self.changelog.delayupdate()
1107 self.changelog.delayupdate()
1108 n = self.changelog.add(mn, files, ctx.description(),
1108 n = self.changelog.add(mn, files, ctx.description(),
1109 trp, p1.node(), p2.node(),
1109 trp, p1.node(), p2.node(),
1110 user, ctx.date(), ctx.extra().copy())
1110 user, ctx.date(), ctx.extra().copy())
1111 p = lambda: self.changelog.writepending() and self.root or ""
1111 p = lambda: self.changelog.writepending() and self.root or ""
1112 xp1, xp2 = p1.hex(), p2 and p2.hex() or ''
1112 xp1, xp2 = p1.hex(), p2 and p2.hex() or ''
1113 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
1113 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
1114 parent2=xp2, pending=p)
1114 parent2=xp2, pending=p)
1115 self.changelog.finalize(trp)
1115 self.changelog.finalize(trp)
1116 tr.close()
1116 tr.close()
1117
1117
1118 if self._branchcache:
1118 if self._branchcache:
1119 self.updatebranchcache()
1119 self.updatebranchcache()
1120 return n
1120 return n
1121 finally:
1121 finally:
1122 if tr:
1122 if tr:
1123 tr.release()
1123 tr.release()
1124 lock.release()
1124 lock.release()
1125
1125
1126 def destroyed(self):
1126 def destroyed(self):
1127 '''Inform the repository that nodes have been destroyed.
1127 '''Inform the repository that nodes have been destroyed.
1128 Intended for use by strip and rollback, so there's a common
1128 Intended for use by strip and rollback, so there's a common
1129 place for anything that has to be done after destroying history.'''
1129 place for anything that has to be done after destroying history.'''
1130 # XXX it might be nice if we could take the list of destroyed
1130 # XXX it might be nice if we could take the list of destroyed
1131 # nodes, but I don't see an easy way for rollback() to do that
1131 # nodes, but I don't see an easy way for rollback() to do that
1132
1132
1133 # Ensure the persistent tag cache is updated. Doing it now
1133 # Ensure the persistent tag cache is updated. Doing it now
1134 # means that the tag cache only has to worry about destroyed
1134 # means that the tag cache only has to worry about destroyed
1135 # heads immediately after a strip/rollback. That in turn
1135 # heads immediately after a strip/rollback. That in turn
1136 # guarantees that "cachetip == currenttip" (comparing both rev
1136 # guarantees that "cachetip == currenttip" (comparing both rev
1137 # and node) always means no nodes have been added or destroyed.
1137 # and node) always means no nodes have been added or destroyed.
1138
1138
1139 # XXX this is suboptimal when qrefresh'ing: we strip the current
1139 # XXX this is suboptimal when qrefresh'ing: we strip the current
1140 # head, refresh the tag cache, then immediately add a new head.
1140 # head, refresh the tag cache, then immediately add a new head.
1141 # But I think doing it this way is necessary for the "instant
1141 # But I think doing it this way is necessary for the "instant
1142 # tag cache retrieval" case to work.
1142 # tag cache retrieval" case to work.
1143 self.invalidatecaches()
1143 self.invalidatecaches()
1144
1144
1145 def walk(self, match, node=None):
1145 def walk(self, match, node=None):
1146 '''
1146 '''
1147 walk recursively through the directory tree or a given
1147 walk recursively through the directory tree or a given
1148 changeset, finding all files matched by the match
1148 changeset, finding all files matched by the match
1149 function
1149 function
1150 '''
1150 '''
1151 return self[node].walk(match)
1151 return self[node].walk(match)
1152
1152
1153 def status(self, node1='.', node2=None, match=None,
1153 def status(self, node1='.', node2=None, match=None,
1154 ignored=False, clean=False, unknown=False,
1154 ignored=False, clean=False, unknown=False,
1155 listsubrepos=False):
1155 listsubrepos=False):
1156 """return status of files between two nodes or node and working directory
1156 """return status of files between two nodes or node and working directory
1157
1157
1158 If node1 is None, use the first dirstate parent instead.
1158 If node1 is None, use the first dirstate parent instead.
1159 If node2 is None, compare node1 with working directory.
1159 If node2 is None, compare node1 with working directory.
1160 """
1160 """
1161
1161
1162 def mfmatches(ctx):
1162 def mfmatches(ctx):
1163 mf = ctx.manifest().copy()
1163 mf = ctx.manifest().copy()
1164 for fn in mf.keys():
1164 for fn in mf.keys():
1165 if not match(fn):
1165 if not match(fn):
1166 del mf[fn]
1166 del mf[fn]
1167 return mf
1167 return mf
1168
1168
1169 if isinstance(node1, context.changectx):
1169 if isinstance(node1, context.changectx):
1170 ctx1 = node1
1170 ctx1 = node1
1171 else:
1171 else:
1172 ctx1 = self[node1]
1172 ctx1 = self[node1]
1173 if isinstance(node2, context.changectx):
1173 if isinstance(node2, context.changectx):
1174 ctx2 = node2
1174 ctx2 = node2
1175 else:
1175 else:
1176 ctx2 = self[node2]
1176 ctx2 = self[node2]
1177
1177
1178 working = ctx2.rev() is None
1178 working = ctx2.rev() is None
1179 parentworking = working and ctx1 == self['.']
1179 parentworking = working and ctx1 == self['.']
1180 match = match or matchmod.always(self.root, self.getcwd())
1180 match = match or matchmod.always(self.root, self.getcwd())
1181 listignored, listclean, listunknown = ignored, clean, unknown
1181 listignored, listclean, listunknown = ignored, clean, unknown
1182
1182
1183 # load earliest manifest first for caching reasons
1183 # load earliest manifest first for caching reasons
1184 if not working and ctx2.rev() < ctx1.rev():
1184 if not working and ctx2.rev() < ctx1.rev():
1185 ctx2.manifest()
1185 ctx2.manifest()
1186
1186
1187 if not parentworking:
1187 if not parentworking:
1188 def bad(f, msg):
1188 def bad(f, msg):
1189 if f not in ctx1:
1189 if f not in ctx1:
1190 self.ui.warn('%s: %s\n' % (self.dirstate.pathto(f), msg))
1190 self.ui.warn('%s: %s\n' % (self.dirstate.pathto(f), msg))
1191 match.bad = bad
1191 match.bad = bad
1192
1192
1193 if working: # we need to scan the working dir
1193 if working: # we need to scan the working dir
1194 subrepos = []
1194 subrepos = []
1195 if '.hgsub' in self.dirstate:
1195 if '.hgsub' in self.dirstate:
1196 subrepos = ctx1.substate.keys()
1196 subrepos = ctx1.substate.keys()
1197 s = self.dirstate.status(match, subrepos, listignored,
1197 s = self.dirstate.status(match, subrepos, listignored,
1198 listclean, listunknown)
1198 listclean, listunknown)
1199 cmp, modified, added, removed, deleted, unknown, ignored, clean = s
1199 cmp, modified, added, removed, deleted, unknown, ignored, clean = s
1200
1200
1201 # check for any possibly clean files
1201 # check for any possibly clean files
1202 if parentworking and cmp:
1202 if parentworking and cmp:
1203 fixup = []
1203 fixup = []
1204 # do a full compare of any files that might have changed
1204 # do a full compare of any files that might have changed
1205 for f in sorted(cmp):
1205 for f in sorted(cmp):
1206 if (f not in ctx1 or ctx2.flags(f) != ctx1.flags(f)
1206 if (f not in ctx1 or ctx2.flags(f) != ctx1.flags(f)
1207 or ctx1[f].cmp(ctx2[f])):
1207 or ctx1[f].cmp(ctx2[f])):
1208 modified.append(f)
1208 modified.append(f)
1209 else:
1209 else:
1210 fixup.append(f)
1210 fixup.append(f)
1211
1211
1212 # update dirstate for files that are actually clean
1212 # update dirstate for files that are actually clean
1213 if fixup:
1213 if fixup:
1214 if listclean:
1214 if listclean:
1215 clean += fixup
1215 clean += fixup
1216
1216
1217 try:
1217 try:
1218 # updating the dirstate is optional
1218 # updating the dirstate is optional
1219 # so we don't wait on the lock
1219 # so we don't wait on the lock
1220 wlock = self.wlock(False)
1220 wlock = self.wlock(False)
1221 try:
1221 try:
1222 for f in fixup:
1222 for f in fixup:
1223 self.dirstate.normal(f)
1223 self.dirstate.normal(f)
1224 finally:
1224 finally:
1225 wlock.release()
1225 wlock.release()
1226 except error.LockError:
1226 except error.LockError:
1227 pass
1227 pass
1228
1228
1229 if not parentworking:
1229 if not parentworking:
1230 mf1 = mfmatches(ctx1)
1230 mf1 = mfmatches(ctx1)
1231 if working:
1231 if working:
1232 # we are comparing working dir against non-parent
1232 # we are comparing working dir against non-parent
1233 # generate a pseudo-manifest for the working dir
1233 # generate a pseudo-manifest for the working dir
1234 mf2 = mfmatches(self['.'])
1234 mf2 = mfmatches(self['.'])
1235 for f in cmp + modified + added:
1235 for f in cmp + modified + added:
1236 mf2[f] = None
1236 mf2[f] = None
1237 mf2.set(f, ctx2.flags(f))
1237 mf2.set(f, ctx2.flags(f))
1238 for f in removed:
1238 for f in removed:
1239 if f in mf2:
1239 if f in mf2:
1240 del mf2[f]
1240 del mf2[f]
1241 else:
1241 else:
1242 # we are comparing two revisions
1242 # we are comparing two revisions
1243 deleted, unknown, ignored = [], [], []
1243 deleted, unknown, ignored = [], [], []
1244 mf2 = mfmatches(ctx2)
1244 mf2 = mfmatches(ctx2)
1245
1245
1246 modified, added, clean = [], [], []
1246 modified, added, clean = [], [], []
1247 for fn in mf2:
1247 for fn in mf2:
1248 if fn in mf1:
1248 if fn in mf1:
1249 if (fn not in deleted and
1249 if (fn not in deleted and
1250 (mf1.flags(fn) != mf2.flags(fn) or
1250 (mf1.flags(fn) != mf2.flags(fn) or
1251 (mf1[fn] != mf2[fn] and
1251 (mf1[fn] != mf2[fn] and
1252 (mf2[fn] or ctx1[fn].cmp(ctx2[fn]))))):
1252 (mf2[fn] or ctx1[fn].cmp(ctx2[fn]))))):
1253 modified.append(fn)
1253 modified.append(fn)
1254 elif listclean:
1254 elif listclean:
1255 clean.append(fn)
1255 clean.append(fn)
1256 del mf1[fn]
1256 del mf1[fn]
1257 elif fn not in deleted:
1257 elif fn not in deleted:
1258 added.append(fn)
1258 added.append(fn)
1259 removed = mf1.keys()
1259 removed = mf1.keys()
1260
1260
1261 r = modified, added, removed, deleted, unknown, ignored, clean
1261 r = modified, added, removed, deleted, unknown, ignored, clean
1262
1262
1263 if listsubrepos:
1263 if listsubrepos:
1264 for subpath, sub in subrepo.itersubrepos(ctx1, ctx2):
1264 for subpath, sub in subrepo.itersubrepos(ctx1, ctx2):
1265 if working:
1265 if working:
1266 rev2 = None
1266 rev2 = None
1267 else:
1267 else:
1268 rev2 = ctx2.substate[subpath][1]
1268 rev2 = ctx2.substate[subpath][1]
1269 try:
1269 try:
1270 submatch = matchmod.narrowmatcher(subpath, match)
1270 submatch = matchmod.narrowmatcher(subpath, match)
1271 s = sub.status(rev2, match=submatch, ignored=listignored,
1271 s = sub.status(rev2, match=submatch, ignored=listignored,
1272 clean=listclean, unknown=listunknown,
1272 clean=listclean, unknown=listunknown,
1273 listsubrepos=True)
1273 listsubrepos=True)
1274 for rfiles, sfiles in zip(r, s):
1274 for rfiles, sfiles in zip(r, s):
1275 rfiles.extend("%s/%s" % (subpath, f) for f in sfiles)
1275 rfiles.extend("%s/%s" % (subpath, f) for f in sfiles)
1276 except error.LookupError:
1276 except error.LookupError:
1277 self.ui.status(_("skipping missing subrepository: %s\n")
1277 self.ui.status(_("skipping missing subrepository: %s\n")
1278 % subpath)
1278 % subpath)
1279
1279
1280 for l in r:
1280 for l in r:
1281 l.sort()
1281 l.sort()
1282 return r
1282 return r
1283
1283
1284 def heads(self, start=None):
1284 def heads(self, start=None):
1285 heads = self.changelog.heads(start)
1285 heads = self.changelog.heads(start)
1286 # sort the output in rev descending order
1286 # sort the output in rev descending order
1287 return sorted(heads, key=self.changelog.rev, reverse=True)
1287 return sorted(heads, key=self.changelog.rev, reverse=True)
1288
1288
1289 def branchheads(self, branch=None, start=None, closed=False):
1289 def branchheads(self, branch=None, start=None, closed=False):
1290 '''return a (possibly filtered) list of heads for the given branch
1290 '''return a (possibly filtered) list of heads for the given branch
1291
1291
1292 Heads are returned in topological order, from newest to oldest.
1292 Heads are returned in topological order, from newest to oldest.
1293 If branch is None, use the dirstate branch.
1293 If branch is None, use the dirstate branch.
1294 If start is not None, return only heads reachable from start.
1294 If start is not None, return only heads reachable from start.
1295 If closed is True, return heads that are marked as closed as well.
1295 If closed is True, return heads that are marked as closed as well.
1296 '''
1296 '''
1297 if branch is None:
1297 if branch is None:
1298 branch = self[None].branch()
1298 branch = self[None].branch()
1299 branches = self.branchmap()
1299 branches = self.branchmap()
1300 if branch not in branches:
1300 if branch not in branches:
1301 return []
1301 return []
1302 # the cache returns heads ordered lowest to highest
1302 # the cache returns heads ordered lowest to highest
1303 bheads = list(reversed(branches[branch]))
1303 bheads = list(reversed(branches[branch]))
1304 if start is not None:
1304 if start is not None:
1305 # filter out the heads that cannot be reached from startrev
1305 # filter out the heads that cannot be reached from startrev
1306 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
1306 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
1307 bheads = [h for h in bheads if h in fbheads]
1307 bheads = [h for h in bheads if h in fbheads]
1308 if not closed:
1308 if not closed:
1309 bheads = [h for h in bheads if
1309 bheads = [h for h in bheads if
1310 ('close' not in self.changelog.read(h)[5])]
1310 ('close' not in self.changelog.read(h)[5])]
1311 return bheads
1311 return bheads
1312
1312
1313 def branches(self, nodes):
1313 def branches(self, nodes):
1314 if not nodes:
1314 if not nodes:
1315 nodes = [self.changelog.tip()]
1315 nodes = [self.changelog.tip()]
1316 b = []
1316 b = []
1317 for n in nodes:
1317 for n in nodes:
1318 t = n
1318 t = n
1319 while True:
1319 while True:
1320 p = self.changelog.parents(n)
1320 p = self.changelog.parents(n)
1321 if p[1] != nullid or p[0] == nullid:
1321 if p[1] != nullid or p[0] == nullid:
1322 b.append((t, n, p[0], p[1]))
1322 b.append((t, n, p[0], p[1]))
1323 break
1323 break
1324 n = p[0]
1324 n = p[0]
1325 return b
1325 return b
1326
1326
1327 def between(self, pairs):
1327 def between(self, pairs):
1328 r = []
1328 r = []
1329
1329
1330 for top, bottom in pairs:
1330 for top, bottom in pairs:
1331 n, l, i = top, [], 0
1331 n, l, i = top, [], 0
1332 f = 1
1332 f = 1
1333
1333
1334 while n != bottom and n != nullid:
1334 while n != bottom and n != nullid:
1335 p = self.changelog.parents(n)[0]
1335 p = self.changelog.parents(n)[0]
1336 if i == f:
1336 if i == f:
1337 l.append(n)
1337 l.append(n)
1338 f = f * 2
1338 f = f * 2
1339 n = p
1339 n = p
1340 i += 1
1340 i += 1
1341
1341
1342 r.append(l)
1342 r.append(l)
1343
1343
1344 return r
1344 return r
1345
1345
1346 def pull(self, remote, heads=None, force=False):
1346 def pull(self, remote, heads=None, force=False):
1347 lock = self.lock()
1347 lock = self.lock()
1348 try:
1348 try:
1349 tmp = discovery.findcommonincoming(self, remote, heads=heads,
1349 tmp = discovery.findcommonincoming(self, remote, heads=heads,
1350 force=force)
1350 force=force)
1351 common, fetch, rheads = tmp
1351 common, fetch, rheads = tmp
1352 if not fetch:
1352 if not fetch:
1353 self.ui.status(_("no changes found\n"))
1353 self.ui.status(_("no changes found\n"))
1354 result = 0
1354 result = 0
1355 else:
1355 else:
1356 if heads is None and list(common) == [nullid]:
1356 if heads is None and list(common) == [nullid]:
1357 self.ui.status(_("requesting all changes\n"))
1357 self.ui.status(_("requesting all changes\n"))
1358 elif heads is None and remote.capable('changegroupsubset'):
1358 elif heads is None and remote.capable('changegroupsubset'):
1359 # issue1320, avoid a race if remote changed after discovery
1359 # issue1320, avoid a race if remote changed after discovery
1360 heads = rheads
1360 heads = rheads
1361
1361
1362 if remote.capable('getbundle'):
1362 if remote.capable('getbundle'):
1363 cg = remote.getbundle('pull', common=common,
1363 cg = remote.getbundle('pull', common=common,
1364 heads=heads or rheads)
1364 heads=heads or rheads)
1365 elif heads is None:
1365 elif heads is None:
1366 cg = remote.changegroup(fetch, 'pull')
1366 cg = remote.changegroup(fetch, 'pull')
1367 elif not remote.capable('changegroupsubset'):
1367 elif not remote.capable('changegroupsubset'):
1368 raise util.Abort(_("partial pull cannot be done because "
1368 raise util.Abort(_("partial pull cannot be done because "
1369 "other repository doesn't support "
1369 "other repository doesn't support "
1370 "changegroupsubset."))
1370 "changegroupsubset."))
1371 else:
1371 else:
1372 cg = remote.changegroupsubset(fetch, heads, 'pull')
1372 cg = remote.changegroupsubset(fetch, heads, 'pull')
1373 result = self.addchangegroup(cg, 'pull', remote.url(),
1373 result = self.addchangegroup(cg, 'pull', remote.url(),
1374 lock=lock)
1374 lock=lock)
1375 finally:
1375 finally:
1376 lock.release()
1376 lock.release()
1377
1377
1378 return result
1378 return result
1379
1379
1380 def checkpush(self, force, revs):
1380 def checkpush(self, force, revs):
1381 """Extensions can override this function if additional checks have
1381 """Extensions can override this function if additional checks have
1382 to be performed before pushing, or call it if they override push
1382 to be performed before pushing, or call it if they override push
1383 command.
1383 command.
1384 """
1384 """
1385 pass
1385 pass
1386
1386
1387 def push(self, remote, force=False, revs=None, newbranch=False):
1387 def push(self, remote, force=False, revs=None, newbranch=False):
1388 '''Push outgoing changesets (limited by revs) from the current
1388 '''Push outgoing changesets (limited by revs) from the current
1389 repository to remote. Return an integer:
1389 repository to remote. Return an integer:
1390 - 0 means HTTP error *or* nothing to push
1390 - 0 means HTTP error *or* nothing to push
1391 - 1 means we pushed and remote head count is unchanged *or*
1391 - 1 means we pushed and remote head count is unchanged *or*
1392 we have outgoing changesets but refused to push
1392 we have outgoing changesets but refused to push
1393 - other values as described by addchangegroup()
1393 - other values as described by addchangegroup()
1394 '''
1394 '''
1395 # there are two ways to push to remote repo:
1395 # there are two ways to push to remote repo:
1396 #
1396 #
1397 # addchangegroup assumes local user can lock remote
1397 # addchangegroup assumes local user can lock remote
1398 # repo (local filesystem, old ssh servers).
1398 # repo (local filesystem, old ssh servers).
1399 #
1399 #
1400 # unbundle assumes local user cannot lock remote repo (new ssh
1400 # unbundle assumes local user cannot lock remote repo (new ssh
1401 # servers, http servers).
1401 # servers, http servers).
1402
1402
1403 self.checkpush(force, revs)
1403 self.checkpush(force, revs)
1404 lock = None
1404 lock = None
1405 unbundle = remote.capable('unbundle')
1405 unbundle = remote.capable('unbundle')
1406 if not unbundle:
1406 if not unbundle:
1407 lock = remote.lock()
1407 lock = remote.lock()
1408 try:
1408 try:
1409 cg, remote_heads = discovery.prepush(self, remote, force, revs,
1409 cg, remote_heads = discovery.prepush(self, remote, force, revs,
1410 newbranch)
1410 newbranch)
1411 ret = remote_heads
1411 ret = remote_heads
1412 if cg is not None:
1412 if cg is not None:
1413 if unbundle:
1413 if unbundle:
1414 # local repo finds heads on server, finds out what
1414 # local repo finds heads on server, finds out what
1415 # revs it must push. once revs transferred, if server
1415 # revs it must push. once revs transferred, if server
1416 # finds it has different heads (someone else won
1416 # finds it has different heads (someone else won
1417 # commit/push race), server aborts.
1417 # commit/push race), server aborts.
1418 if force:
1418 if force:
1419 remote_heads = ['force']
1419 remote_heads = ['force']
1420 # ssh: return remote's addchangegroup()
1420 # ssh: return remote's addchangegroup()
1421 # http: return remote's addchangegroup() or 0 for error
1421 # http: return remote's addchangegroup() or 0 for error
1422 ret = remote.unbundle(cg, remote_heads, 'push')
1422 ret = remote.unbundle(cg, remote_heads, 'push')
1423 else:
1423 else:
1424 # we return an integer indicating remote head count change
1424 # we return an integer indicating remote head count change
1425 ret = remote.addchangegroup(cg, 'push', self.url(),
1425 ret = remote.addchangegroup(cg, 'push', self.url(),
1426 lock=lock)
1426 lock=lock)
1427 finally:
1427 finally:
1428 if lock is not None:
1428 if lock is not None:
1429 lock.release()
1429 lock.release()
1430
1430
1431 self.ui.debug("checking for updated bookmarks\n")
1431 self.ui.debug("checking for updated bookmarks\n")
1432 rb = remote.listkeys('bookmarks')
1432 rb = remote.listkeys('bookmarks')
1433 for k in rb.keys():
1433 for k in rb.keys():
1434 if k in self._bookmarks:
1434 if k in self._bookmarks:
1435 nr, nl = rb[k], hex(self._bookmarks[k])
1435 nr, nl = rb[k], hex(self._bookmarks[k])
1436 if nr in self:
1436 if nr in self:
1437 cr = self[nr]
1437 cr = self[nr]
1438 cl = self[nl]
1438 cl = self[nl]
1439 if cl in cr.descendants():
1439 if cl in cr.descendants():
1440 r = remote.pushkey('bookmarks', k, nr, nl)
1440 r = remote.pushkey('bookmarks', k, nr, nl)
1441 if r:
1441 if r:
1442 self.ui.status(_("updating bookmark %s\n") % k)
1442 self.ui.status(_("updating bookmark %s\n") % k)
1443 else:
1443 else:
1444 self.ui.warn(_('updating bookmark %s'
1444 self.ui.warn(_('updating bookmark %s'
1445 ' failed!\n') % k)
1445 ' failed!\n') % k)
1446
1446
1447 return ret
1447 return ret
1448
1448
1449 def changegroupinfo(self, nodes, source):
1449 def changegroupinfo(self, nodes, source):
1450 if self.ui.verbose or source == 'bundle':
1450 if self.ui.verbose or source == 'bundle':
1451 self.ui.status(_("%d changesets found\n") % len(nodes))
1451 self.ui.status(_("%d changesets found\n") % len(nodes))
1452 if self.ui.debugflag:
1452 if self.ui.debugflag:
1453 self.ui.debug("list of changesets:\n")
1453 self.ui.debug("list of changesets:\n")
1454 for node in nodes:
1454 for node in nodes:
1455 self.ui.debug("%s\n" % hex(node))
1455 self.ui.debug("%s\n" % hex(node))
1456
1456
1457 def changegroupsubset(self, bases, heads, source):
1457 def changegroupsubset(self, bases, heads, source):
1458 """Compute a changegroup consisting of all the nodes that are
1458 """Compute a changegroup consisting of all the nodes that are
1459 descendents of any of the bases and ancestors of any of the heads.
1459 descendants of any of the bases and ancestors of any of the heads.
1460 Return a chunkbuffer object whose read() method will return
1460 Return a chunkbuffer object whose read() method will return
1461 successive changegroup chunks.
1461 successive changegroup chunks.
1462
1462
1463 It is fairly complex as determining which filenodes and which
1463 It is fairly complex as determining which filenodes and which
1464 manifest nodes need to be included for the changeset to be complete
1464 manifest nodes need to be included for the changeset to be complete
1465 is non-trivial.
1465 is non-trivial.
1466
1466
1467 Another wrinkle is doing the reverse, figuring out which changeset in
1467 Another wrinkle is doing the reverse, figuring out which changeset in
1468 the changegroup a particular filenode or manifestnode belongs to.
1468 the changegroup a particular filenode or manifestnode belongs to.
1469 """
1469 """
1470 cl = self.changelog
1470 cl = self.changelog
1471 if not bases:
1471 if not bases:
1472 bases = [nullid]
1472 bases = [nullid]
1473 csets, bases, heads = cl.nodesbetween(bases, heads)
1473 csets, bases, heads = cl.nodesbetween(bases, heads)
1474 # We assume that all ancestors of bases are known
1474 # We assume that all ancestors of bases are known
1475 common = set(cl.ancestors(*[cl.rev(n) for n in bases]))
1475 common = set(cl.ancestors(*[cl.rev(n) for n in bases]))
1476 return self._changegroupsubset(common, csets, heads, source)
1476 return self._changegroupsubset(common, csets, heads, source)
1477
1477
1478 def getbundle(self, source, heads=None, common=None):
1478 def getbundle(self, source, heads=None, common=None):
1479 """Like changegroupsubset, but returns the set difference between the
1479 """Like changegroupsubset, but returns the set difference between the
1480 ancestors of heads and the ancestors common.
1480 ancestors of heads and the ancestors common.
1481
1481
1482 If heads is None, use the local heads. If common is None, use [nullid].
1482 If heads is None, use the local heads. If common is None, use [nullid].
1483
1483
1484 The nodes in common might not all be known locally due to the way the
1484 The nodes in common might not all be known locally due to the way the
1485 current discovery protocol works.
1485 current discovery protocol works.
1486 """
1486 """
1487 cl = self.changelog
1487 cl = self.changelog
1488 if common:
1488 if common:
1489 nm = cl.nodemap
1489 nm = cl.nodemap
1490 common = [n for n in common if n in nm]
1490 common = [n for n in common if n in nm]
1491 else:
1491 else:
1492 common = [nullid]
1492 common = [nullid]
1493 if not heads:
1493 if not heads:
1494 heads = cl.heads()
1494 heads = cl.heads()
1495 common, missing = cl.findcommonmissing(common, heads)
1495 common, missing = cl.findcommonmissing(common, heads)
1496 if not missing:
1496 if not missing:
1497 return None
1497 return None
1498 return self._changegroupsubset(common, missing, heads, source)
1498 return self._changegroupsubset(common, missing, heads, source)
1499
1499
1500 def _changegroupsubset(self, commonrevs, csets, heads, source):
1500 def _changegroupsubset(self, commonrevs, csets, heads, source):
1501
1501
1502 cl = self.changelog
1502 cl = self.changelog
1503 mf = self.manifest
1503 mf = self.manifest
1504 mfs = {} # needed manifests
1504 mfs = {} # needed manifests
1505 fnodes = {} # needed file nodes
1505 fnodes = {} # needed file nodes
1506 changedfiles = set()
1506 changedfiles = set()
1507 fstate = ['', {}]
1507 fstate = ['', {}]
1508 count = [0]
1508 count = [0]
1509
1509
1510 # can we go through the fast path ?
1510 # can we go through the fast path ?
1511 heads.sort()
1511 heads.sort()
1512 if heads == sorted(self.heads()):
1512 if heads == sorted(self.heads()):
1513 return self._changegroup(csets, source)
1513 return self._changegroup(csets, source)
1514
1514
1515 # slow path
1515 # slow path
1516 self.hook('preoutgoing', throw=True, source=source)
1516 self.hook('preoutgoing', throw=True, source=source)
1517 self.changegroupinfo(csets, source)
1517 self.changegroupinfo(csets, source)
1518
1518
1519 # filter any nodes that claim to be part of the known set
1519 # filter any nodes that claim to be part of the known set
1520 def prune(revlog, missing):
1520 def prune(revlog, missing):
1521 return [n for n in missing
1521 return [n for n in missing
1522 if revlog.linkrev(revlog.rev(n)) not in commonrevs]
1522 if revlog.linkrev(revlog.rev(n)) not in commonrevs]
1523
1523
1524 def lookup(revlog, x):
1524 def lookup(revlog, x):
1525 if revlog == cl:
1525 if revlog == cl:
1526 c = cl.read(x)
1526 c = cl.read(x)
1527 changedfiles.update(c[3])
1527 changedfiles.update(c[3])
1528 mfs.setdefault(c[0], x)
1528 mfs.setdefault(c[0], x)
1529 count[0] += 1
1529 count[0] += 1
1530 self.ui.progress(_('bundling'), count[0],
1530 self.ui.progress(_('bundling'), count[0],
1531 unit=_('changesets'), total=len(csets))
1531 unit=_('changesets'), total=len(csets))
1532 return x
1532 return x
1533 elif revlog == mf:
1533 elif revlog == mf:
1534 clnode = mfs[x]
1534 clnode = mfs[x]
1535 mdata = mf.readfast(x)
1535 mdata = mf.readfast(x)
1536 for f in changedfiles:
1536 for f in changedfiles:
1537 if f in mdata:
1537 if f in mdata:
1538 fnodes.setdefault(f, {}).setdefault(mdata[f], clnode)
1538 fnodes.setdefault(f, {}).setdefault(mdata[f], clnode)
1539 count[0] += 1
1539 count[0] += 1
1540 self.ui.progress(_('bundling'), count[0],
1540 self.ui.progress(_('bundling'), count[0],
1541 unit=_('manifests'), total=len(mfs))
1541 unit=_('manifests'), total=len(mfs))
1542 return mfs[x]
1542 return mfs[x]
1543 else:
1543 else:
1544 self.ui.progress(
1544 self.ui.progress(
1545 _('bundling'), count[0], item=fstate[0],
1545 _('bundling'), count[0], item=fstate[0],
1546 unit=_('files'), total=len(changedfiles))
1546 unit=_('files'), total=len(changedfiles))
1547 return fstate[1][x]
1547 return fstate[1][x]
1548
1548
1549 bundler = changegroup.bundle10(lookup)
1549 bundler = changegroup.bundle10(lookup)
1550 reorder = self.ui.config('bundle', 'reorder', 'auto')
1550 reorder = self.ui.config('bundle', 'reorder', 'auto')
1551 if reorder == 'auto':
1551 if reorder == 'auto':
1552 reorder = None
1552 reorder = None
1553 else:
1553 else:
1554 reorder = util.parsebool(reorder)
1554 reorder = util.parsebool(reorder)
1555
1555
1556 def gengroup():
1556 def gengroup():
1557 # Create a changenode group generator that will call our functions
1557 # Create a changenode group generator that will call our functions
1558 # back to lookup the owning changenode and collect information.
1558 # back to lookup the owning changenode and collect information.
1559 for chunk in cl.group(csets, bundler, reorder=reorder):
1559 for chunk in cl.group(csets, bundler, reorder=reorder):
1560 yield chunk
1560 yield chunk
1561 self.ui.progress(_('bundling'), None)
1561 self.ui.progress(_('bundling'), None)
1562
1562
1563 # Create a generator for the manifestnodes that calls our lookup
1563 # Create a generator for the manifestnodes that calls our lookup
1564 # and data collection functions back.
1564 # and data collection functions back.
1565 count[0] = 0
1565 count[0] = 0
1566 for chunk in mf.group(prune(mf, mfs), bundler, reorder=reorder):
1566 for chunk in mf.group(prune(mf, mfs), bundler, reorder=reorder):
1567 yield chunk
1567 yield chunk
1568 self.ui.progress(_('bundling'), None)
1568 self.ui.progress(_('bundling'), None)
1569
1569
1570 mfs.clear()
1570 mfs.clear()
1571
1571
1572 # Go through all our files in order sorted by name.
1572 # Go through all our files in order sorted by name.
1573 count[0] = 0
1573 count[0] = 0
1574 for fname in sorted(changedfiles):
1574 for fname in sorted(changedfiles):
1575 filerevlog = self.file(fname)
1575 filerevlog = self.file(fname)
1576 if not len(filerevlog):
1576 if not len(filerevlog):
1577 raise util.Abort(_("empty or missing revlog for %s") % fname)
1577 raise util.Abort(_("empty or missing revlog for %s") % fname)
1578 fstate[0] = fname
1578 fstate[0] = fname
1579 fstate[1] = fnodes.pop(fname, {})
1579 fstate[1] = fnodes.pop(fname, {})
1580
1580
1581 nodelist = prune(filerevlog, fstate[1])
1581 nodelist = prune(filerevlog, fstate[1])
1582 if nodelist:
1582 if nodelist:
1583 count[0] += 1
1583 count[0] += 1
1584 yield bundler.fileheader(fname)
1584 yield bundler.fileheader(fname)
1585 for chunk in filerevlog.group(nodelist, bundler, reorder):
1585 for chunk in filerevlog.group(nodelist, bundler, reorder):
1586 yield chunk
1586 yield chunk
1587
1587
1588 # Signal that no more groups are left.
1588 # Signal that no more groups are left.
1589 yield bundler.close()
1589 yield bundler.close()
1590 self.ui.progress(_('bundling'), None)
1590 self.ui.progress(_('bundling'), None)
1591
1591
1592 if csets:
1592 if csets:
1593 self.hook('outgoing', node=hex(csets[0]), source=source)
1593 self.hook('outgoing', node=hex(csets[0]), source=source)
1594
1594
1595 return changegroup.unbundle10(util.chunkbuffer(gengroup()), 'UN')
1595 return changegroup.unbundle10(util.chunkbuffer(gengroup()), 'UN')
1596
1596
1597 def changegroup(self, basenodes, source):
1597 def changegroup(self, basenodes, source):
1598 # to avoid a race we use changegroupsubset() (issue1320)
1598 # to avoid a race we use changegroupsubset() (issue1320)
1599 return self.changegroupsubset(basenodes, self.heads(), source)
1599 return self.changegroupsubset(basenodes, self.heads(), source)
1600
1600
1601 def _changegroup(self, nodes, source):
1601 def _changegroup(self, nodes, source):
1602 """Compute the changegroup of all nodes that we have that a recipient
1602 """Compute the changegroup of all nodes that we have that a recipient
1603 doesn't. Return a chunkbuffer object whose read() method will return
1603 doesn't. Return a chunkbuffer object whose read() method will return
1604 successive changegroup chunks.
1604 successive changegroup chunks.
1605
1605
1606 This is much easier than the previous function as we can assume that
1606 This is much easier than the previous function as we can assume that
1607 the recipient has any changenode we aren't sending them.
1607 the recipient has any changenode we aren't sending them.
1608
1608
1609 nodes is the set of nodes to send"""
1609 nodes is the set of nodes to send"""
1610
1610
1611 cl = self.changelog
1611 cl = self.changelog
1612 mf = self.manifest
1612 mf = self.manifest
1613 mfs = {}
1613 mfs = {}
1614 changedfiles = set()
1614 changedfiles = set()
1615 fstate = ['']
1615 fstate = ['']
1616 count = [0]
1616 count = [0]
1617
1617
1618 self.hook('preoutgoing', throw=True, source=source)
1618 self.hook('preoutgoing', throw=True, source=source)
1619 self.changegroupinfo(nodes, source)
1619 self.changegroupinfo(nodes, source)
1620
1620
1621 revset = set([cl.rev(n) for n in nodes])
1621 revset = set([cl.rev(n) for n in nodes])
1622
1622
1623 def gennodelst(log):
1623 def gennodelst(log):
1624 return [log.node(r) for r in log if log.linkrev(r) in revset]
1624 return [log.node(r) for r in log if log.linkrev(r) in revset]
1625
1625
1626 def lookup(revlog, x):
1626 def lookup(revlog, x):
1627 if revlog == cl:
1627 if revlog == cl:
1628 c = cl.read(x)
1628 c = cl.read(x)
1629 changedfiles.update(c[3])
1629 changedfiles.update(c[3])
1630 mfs.setdefault(c[0], x)
1630 mfs.setdefault(c[0], x)
1631 count[0] += 1
1631 count[0] += 1
1632 self.ui.progress(_('bundling'), count[0],
1632 self.ui.progress(_('bundling'), count[0],
1633 unit=_('changesets'), total=len(nodes))
1633 unit=_('changesets'), total=len(nodes))
1634 return x
1634 return x
1635 elif revlog == mf:
1635 elif revlog == mf:
1636 count[0] += 1
1636 count[0] += 1
1637 self.ui.progress(_('bundling'), count[0],
1637 self.ui.progress(_('bundling'), count[0],
1638 unit=_('manifests'), total=len(mfs))
1638 unit=_('manifests'), total=len(mfs))
1639 return cl.node(revlog.linkrev(revlog.rev(x)))
1639 return cl.node(revlog.linkrev(revlog.rev(x)))
1640 else:
1640 else:
1641 self.ui.progress(
1641 self.ui.progress(
1642 _('bundling'), count[0], item=fstate[0],
1642 _('bundling'), count[0], item=fstate[0],
1643 total=len(changedfiles), unit=_('files'))
1643 total=len(changedfiles), unit=_('files'))
1644 return cl.node(revlog.linkrev(revlog.rev(x)))
1644 return cl.node(revlog.linkrev(revlog.rev(x)))
1645
1645
1646 bundler = changegroup.bundle10(lookup)
1646 bundler = changegroup.bundle10(lookup)
1647 reorder = self.ui.config('bundle', 'reorder', 'auto')
1647 reorder = self.ui.config('bundle', 'reorder', 'auto')
1648 if reorder == 'auto':
1648 if reorder == 'auto':
1649 reorder = None
1649 reorder = None
1650 else:
1650 else:
1651 reorder = util.parsebool(reorder)
1651 reorder = util.parsebool(reorder)
1652
1652
1653 def gengroup():
1653 def gengroup():
1654 '''yield a sequence of changegroup chunks (strings)'''
1654 '''yield a sequence of changegroup chunks (strings)'''
1655 # construct a list of all changed files
1655 # construct a list of all changed files
1656
1656
1657 for chunk in cl.group(nodes, bundler, reorder=reorder):
1657 for chunk in cl.group(nodes, bundler, reorder=reorder):
1658 yield chunk
1658 yield chunk
1659 self.ui.progress(_('bundling'), None)
1659 self.ui.progress(_('bundling'), None)
1660
1660
1661 count[0] = 0
1661 count[0] = 0
1662 for chunk in mf.group(gennodelst(mf), bundler, reorder=reorder):
1662 for chunk in mf.group(gennodelst(mf), bundler, reorder=reorder):
1663 yield chunk
1663 yield chunk
1664 self.ui.progress(_('bundling'), None)
1664 self.ui.progress(_('bundling'), None)
1665
1665
1666 count[0] = 0
1666 count[0] = 0
1667 for fname in sorted(changedfiles):
1667 for fname in sorted(changedfiles):
1668 filerevlog = self.file(fname)
1668 filerevlog = self.file(fname)
1669 if not len(filerevlog):
1669 if not len(filerevlog):
1670 raise util.Abort(_("empty or missing revlog for %s") % fname)
1670 raise util.Abort(_("empty or missing revlog for %s") % fname)
1671 fstate[0] = fname
1671 fstate[0] = fname
1672 nodelist = gennodelst(filerevlog)
1672 nodelist = gennodelst(filerevlog)
1673 if nodelist:
1673 if nodelist:
1674 count[0] += 1
1674 count[0] += 1
1675 yield bundler.fileheader(fname)
1675 yield bundler.fileheader(fname)
1676 for chunk in filerevlog.group(nodelist, bundler, reorder):
1676 for chunk in filerevlog.group(nodelist, bundler, reorder):
1677 yield chunk
1677 yield chunk
1678 yield bundler.close()
1678 yield bundler.close()
1679 self.ui.progress(_('bundling'), None)
1679 self.ui.progress(_('bundling'), None)
1680
1680
1681 if nodes:
1681 if nodes:
1682 self.hook('outgoing', node=hex(nodes[0]), source=source)
1682 self.hook('outgoing', node=hex(nodes[0]), source=source)
1683
1683
1684 return changegroup.unbundle10(util.chunkbuffer(gengroup()), 'UN')
1684 return changegroup.unbundle10(util.chunkbuffer(gengroup()), 'UN')
1685
1685
1686 def addchangegroup(self, source, srctype, url, emptyok=False, lock=None):
1686 def addchangegroup(self, source, srctype, url, emptyok=False, lock=None):
1687 """Add the changegroup returned by source.read() to this repo.
1687 """Add the changegroup returned by source.read() to this repo.
1688 srctype is a string like 'push', 'pull', or 'unbundle'. url is
1688 srctype is a string like 'push', 'pull', or 'unbundle'. url is
1689 the URL of the repo where this changegroup is coming from.
1689 the URL of the repo where this changegroup is coming from.
1690 If lock is not None, the function takes ownership of the lock
1690 If lock is not None, the function takes ownership of the lock
1691 and releases it after the changegroup is added.
1691 and releases it after the changegroup is added.
1692
1692
1693 Return an integer summarizing the change to this repo:
1693 Return an integer summarizing the change to this repo:
1694 - nothing changed or no source: 0
1694 - nothing changed or no source: 0
1695 - more heads than before: 1+added heads (2..n)
1695 - more heads than before: 1+added heads (2..n)
1696 - fewer heads than before: -1-removed heads (-2..-n)
1696 - fewer heads than before: -1-removed heads (-2..-n)
1697 - number of heads stays the same: 1
1697 - number of heads stays the same: 1
1698 """
1698 """
1699 def csmap(x):
1699 def csmap(x):
1700 self.ui.debug("add changeset %s\n" % short(x))
1700 self.ui.debug("add changeset %s\n" % short(x))
1701 return len(cl)
1701 return len(cl)
1702
1702
1703 def revmap(x):
1703 def revmap(x):
1704 return cl.rev(x)
1704 return cl.rev(x)
1705
1705
1706 if not source:
1706 if not source:
1707 return 0
1707 return 0
1708
1708
1709 self.hook('prechangegroup', throw=True, source=srctype, url=url)
1709 self.hook('prechangegroup', throw=True, source=srctype, url=url)
1710
1710
1711 changesets = files = revisions = 0
1711 changesets = files = revisions = 0
1712 efiles = set()
1712 efiles = set()
1713
1713
1714 # write changelog data to temp files so concurrent readers will not see
1714 # write changelog data to temp files so concurrent readers will not see
1715 # inconsistent view
1715 # inconsistent view
1716 cl = self.changelog
1716 cl = self.changelog
1717 cl.delayupdate()
1717 cl.delayupdate()
1718 oldheads = cl.heads()
1718 oldheads = cl.heads()
1719
1719
1720 tr = self.transaction("\n".join([srctype, util.hidepassword(url)]))
1720 tr = self.transaction("\n".join([srctype, util.hidepassword(url)]))
1721 try:
1721 try:
1722 trp = weakref.proxy(tr)
1722 trp = weakref.proxy(tr)
1723 # pull off the changeset group
1723 # pull off the changeset group
1724 self.ui.status(_("adding changesets\n"))
1724 self.ui.status(_("adding changesets\n"))
1725 clstart = len(cl)
1725 clstart = len(cl)
1726 class prog(object):
1726 class prog(object):
1727 step = _('changesets')
1727 step = _('changesets')
1728 count = 1
1728 count = 1
1729 ui = self.ui
1729 ui = self.ui
1730 total = None
1730 total = None
1731 def __call__(self):
1731 def __call__(self):
1732 self.ui.progress(self.step, self.count, unit=_('chunks'),
1732 self.ui.progress(self.step, self.count, unit=_('chunks'),
1733 total=self.total)
1733 total=self.total)
1734 self.count += 1
1734 self.count += 1
1735 pr = prog()
1735 pr = prog()
1736 source.callback = pr
1736 source.callback = pr
1737
1737
1738 source.changelogheader()
1738 source.changelogheader()
1739 if (cl.addgroup(source, csmap, trp) is None
1739 if (cl.addgroup(source, csmap, trp) is None
1740 and not emptyok):
1740 and not emptyok):
1741 raise util.Abort(_("received changelog group is empty"))
1741 raise util.Abort(_("received changelog group is empty"))
1742 clend = len(cl)
1742 clend = len(cl)
1743 changesets = clend - clstart
1743 changesets = clend - clstart
1744 for c in xrange(clstart, clend):
1744 for c in xrange(clstart, clend):
1745 efiles.update(self[c].files())
1745 efiles.update(self[c].files())
1746 efiles = len(efiles)
1746 efiles = len(efiles)
1747 self.ui.progress(_('changesets'), None)
1747 self.ui.progress(_('changesets'), None)
1748
1748
1749 # pull off the manifest group
1749 # pull off the manifest group
1750 self.ui.status(_("adding manifests\n"))
1750 self.ui.status(_("adding manifests\n"))
1751 pr.step = _('manifests')
1751 pr.step = _('manifests')
1752 pr.count = 1
1752 pr.count = 1
1753 pr.total = changesets # manifests <= changesets
1753 pr.total = changesets # manifests <= changesets
1754 # no need to check for empty manifest group here:
1754 # no need to check for empty manifest group here:
1755 # if the result of the merge of 1 and 2 is the same in 3 and 4,
1755 # if the result of the merge of 1 and 2 is the same in 3 and 4,
1756 # no new manifest will be created and the manifest group will
1756 # no new manifest will be created and the manifest group will
1757 # be empty during the pull
1757 # be empty during the pull
1758 source.manifestheader()
1758 source.manifestheader()
1759 self.manifest.addgroup(source, revmap, trp)
1759 self.manifest.addgroup(source, revmap, trp)
1760 self.ui.progress(_('manifests'), None)
1760 self.ui.progress(_('manifests'), None)
1761
1761
1762 needfiles = {}
1762 needfiles = {}
1763 if self.ui.configbool('server', 'validate', default=False):
1763 if self.ui.configbool('server', 'validate', default=False):
1764 # validate incoming csets have their manifests
1764 # validate incoming csets have their manifests
1765 for cset in xrange(clstart, clend):
1765 for cset in xrange(clstart, clend):
1766 mfest = self.changelog.read(self.changelog.node(cset))[0]
1766 mfest = self.changelog.read(self.changelog.node(cset))[0]
1767 mfest = self.manifest.readdelta(mfest)
1767 mfest = self.manifest.readdelta(mfest)
1768 # store file nodes we must see
1768 # store file nodes we must see
1769 for f, n in mfest.iteritems():
1769 for f, n in mfest.iteritems():
1770 needfiles.setdefault(f, set()).add(n)
1770 needfiles.setdefault(f, set()).add(n)
1771
1771
1772 # process the files
1772 # process the files
1773 self.ui.status(_("adding file changes\n"))
1773 self.ui.status(_("adding file changes\n"))
1774 pr.step = 'files'
1774 pr.step = 'files'
1775 pr.count = 1
1775 pr.count = 1
1776 pr.total = efiles
1776 pr.total = efiles
1777 source.callback = None
1777 source.callback = None
1778
1778
1779 while True:
1779 while True:
1780 chunkdata = source.filelogheader()
1780 chunkdata = source.filelogheader()
1781 if not chunkdata:
1781 if not chunkdata:
1782 break
1782 break
1783 f = chunkdata["filename"]
1783 f = chunkdata["filename"]
1784 self.ui.debug("adding %s revisions\n" % f)
1784 self.ui.debug("adding %s revisions\n" % f)
1785 pr()
1785 pr()
1786 fl = self.file(f)
1786 fl = self.file(f)
1787 o = len(fl)
1787 o = len(fl)
1788 if fl.addgroup(source, revmap, trp) is None:
1788 if fl.addgroup(source, revmap, trp) is None:
1789 raise util.Abort(_("received file revlog group is empty"))
1789 raise util.Abort(_("received file revlog group is empty"))
1790 revisions += len(fl) - o
1790 revisions += len(fl) - o
1791 files += 1
1791 files += 1
1792 if f in needfiles:
1792 if f in needfiles:
1793 needs = needfiles[f]
1793 needs = needfiles[f]
1794 for new in xrange(o, len(fl)):
1794 for new in xrange(o, len(fl)):
1795 n = fl.node(new)
1795 n = fl.node(new)
1796 if n in needs:
1796 if n in needs:
1797 needs.remove(n)
1797 needs.remove(n)
1798 if not needs:
1798 if not needs:
1799 del needfiles[f]
1799 del needfiles[f]
1800 self.ui.progress(_('files'), None)
1800 self.ui.progress(_('files'), None)
1801
1801
1802 for f, needs in needfiles.iteritems():
1802 for f, needs in needfiles.iteritems():
1803 fl = self.file(f)
1803 fl = self.file(f)
1804 for n in needs:
1804 for n in needs:
1805 try:
1805 try:
1806 fl.rev(n)
1806 fl.rev(n)
1807 except error.LookupError:
1807 except error.LookupError:
1808 raise util.Abort(
1808 raise util.Abort(
1809 _('missing file data for %s:%s - run hg verify') %
1809 _('missing file data for %s:%s - run hg verify') %
1810 (f, hex(n)))
1810 (f, hex(n)))
1811
1811
1812 dh = 0
1812 dh = 0
1813 if oldheads:
1813 if oldheads:
1814 heads = cl.heads()
1814 heads = cl.heads()
1815 dh = len(heads) - len(oldheads)
1815 dh = len(heads) - len(oldheads)
1816 for h in heads:
1816 for h in heads:
1817 if h not in oldheads and 'close' in self[h].extra():
1817 if h not in oldheads and 'close' in self[h].extra():
1818 dh -= 1
1818 dh -= 1
1819 htext = ""
1819 htext = ""
1820 if dh:
1820 if dh:
1821 htext = _(" (%+d heads)") % dh
1821 htext = _(" (%+d heads)") % dh
1822
1822
1823 self.ui.status(_("added %d changesets"
1823 self.ui.status(_("added %d changesets"
1824 " with %d changes to %d files%s\n")
1824 " with %d changes to %d files%s\n")
1825 % (changesets, revisions, files, htext))
1825 % (changesets, revisions, files, htext))
1826
1826
1827 if changesets > 0:
1827 if changesets > 0:
1828 p = lambda: cl.writepending() and self.root or ""
1828 p = lambda: cl.writepending() and self.root or ""
1829 self.hook('pretxnchangegroup', throw=True,
1829 self.hook('pretxnchangegroup', throw=True,
1830 node=hex(cl.node(clstart)), source=srctype,
1830 node=hex(cl.node(clstart)), source=srctype,
1831 url=url, pending=p)
1831 url=url, pending=p)
1832
1832
1833 # make changelog see real files again
1833 # make changelog see real files again
1834 cl.finalize(trp)
1834 cl.finalize(trp)
1835
1835
1836 tr.close()
1836 tr.close()
1837 finally:
1837 finally:
1838 tr.release()
1838 tr.release()
1839 if lock:
1839 if lock:
1840 lock.release()
1840 lock.release()
1841
1841
1842 if changesets > 0:
1842 if changesets > 0:
1843 # forcefully update the on-disk branch cache
1843 # forcefully update the on-disk branch cache
1844 self.ui.debug("updating the branch cache\n")
1844 self.ui.debug("updating the branch cache\n")
1845 self.updatebranchcache()
1845 self.updatebranchcache()
1846 self.hook("changegroup", node=hex(cl.node(clstart)),
1846 self.hook("changegroup", node=hex(cl.node(clstart)),
1847 source=srctype, url=url)
1847 source=srctype, url=url)
1848
1848
1849 for i in xrange(clstart, clend):
1849 for i in xrange(clstart, clend):
1850 self.hook("incoming", node=hex(cl.node(i)),
1850 self.hook("incoming", node=hex(cl.node(i)),
1851 source=srctype, url=url)
1851 source=srctype, url=url)
1852
1852
1853 # never return 0 here:
1853 # never return 0 here:
1854 if dh < 0:
1854 if dh < 0:
1855 return dh - 1
1855 return dh - 1
1856 else:
1856 else:
1857 return dh + 1
1857 return dh + 1
1858
1858
1859 def stream_in(self, remote, requirements):
1859 def stream_in(self, remote, requirements):
1860 lock = self.lock()
1860 lock = self.lock()
1861 try:
1861 try:
1862 fp = remote.stream_out()
1862 fp = remote.stream_out()
1863 l = fp.readline()
1863 l = fp.readline()
1864 try:
1864 try:
1865 resp = int(l)
1865 resp = int(l)
1866 except ValueError:
1866 except ValueError:
1867 raise error.ResponseError(
1867 raise error.ResponseError(
1868 _('Unexpected response from remote server:'), l)
1868 _('Unexpected response from remote server:'), l)
1869 if resp == 1:
1869 if resp == 1:
1870 raise util.Abort(_('operation forbidden by server'))
1870 raise util.Abort(_('operation forbidden by server'))
1871 elif resp == 2:
1871 elif resp == 2:
1872 raise util.Abort(_('locking the remote repository failed'))
1872 raise util.Abort(_('locking the remote repository failed'))
1873 elif resp != 0:
1873 elif resp != 0:
1874 raise util.Abort(_('the server sent an unknown error code'))
1874 raise util.Abort(_('the server sent an unknown error code'))
1875 self.ui.status(_('streaming all changes\n'))
1875 self.ui.status(_('streaming all changes\n'))
1876 l = fp.readline()
1876 l = fp.readline()
1877 try:
1877 try:
1878 total_files, total_bytes = map(int, l.split(' ', 1))
1878 total_files, total_bytes = map(int, l.split(' ', 1))
1879 except (ValueError, TypeError):
1879 except (ValueError, TypeError):
1880 raise error.ResponseError(
1880 raise error.ResponseError(
1881 _('Unexpected response from remote server:'), l)
1881 _('Unexpected response from remote server:'), l)
1882 self.ui.status(_('%d files to transfer, %s of data\n') %
1882 self.ui.status(_('%d files to transfer, %s of data\n') %
1883 (total_files, util.bytecount(total_bytes)))
1883 (total_files, util.bytecount(total_bytes)))
1884 start = time.time()
1884 start = time.time()
1885 for i in xrange(total_files):
1885 for i in xrange(total_files):
1886 # XXX doesn't support '\n' or '\r' in filenames
1886 # XXX doesn't support '\n' or '\r' in filenames
1887 l = fp.readline()
1887 l = fp.readline()
1888 try:
1888 try:
1889 name, size = l.split('\0', 1)
1889 name, size = l.split('\0', 1)
1890 size = int(size)
1890 size = int(size)
1891 except (ValueError, TypeError):
1891 except (ValueError, TypeError):
1892 raise error.ResponseError(
1892 raise error.ResponseError(
1893 _('Unexpected response from remote server:'), l)
1893 _('Unexpected response from remote server:'), l)
1894 self.ui.debug('adding %s (%s)\n' % (name, util.bytecount(size)))
1894 self.ui.debug('adding %s (%s)\n' % (name, util.bytecount(size)))
1895 # for backwards compat, name was partially encoded
1895 # for backwards compat, name was partially encoded
1896 ofp = self.sopener(store.decodedir(name), 'w')
1896 ofp = self.sopener(store.decodedir(name), 'w')
1897 for chunk in util.filechunkiter(fp, limit=size):
1897 for chunk in util.filechunkiter(fp, limit=size):
1898 ofp.write(chunk)
1898 ofp.write(chunk)
1899 ofp.close()
1899 ofp.close()
1900 elapsed = time.time() - start
1900 elapsed = time.time() - start
1901 if elapsed <= 0:
1901 if elapsed <= 0:
1902 elapsed = 0.001
1902 elapsed = 0.001
1903 self.ui.status(_('transferred %s in %.1f seconds (%s/sec)\n') %
1903 self.ui.status(_('transferred %s in %.1f seconds (%s/sec)\n') %
1904 (util.bytecount(total_bytes), elapsed,
1904 (util.bytecount(total_bytes), elapsed,
1905 util.bytecount(total_bytes / elapsed)))
1905 util.bytecount(total_bytes / elapsed)))
1906
1906
1907 # new requirements = old non-format requirements + new format-related
1907 # new requirements = old non-format requirements + new format-related
1908 # requirements from the streamed-in repository
1908 # requirements from the streamed-in repository
1909 requirements.update(set(self.requirements) - self.supportedformats)
1909 requirements.update(set(self.requirements) - self.supportedformats)
1910 self._applyrequirements(requirements)
1910 self._applyrequirements(requirements)
1911 self._writerequirements()
1911 self._writerequirements()
1912
1912
1913 self.invalidate()
1913 self.invalidate()
1914 return len(self.heads()) + 1
1914 return len(self.heads()) + 1
1915 finally:
1915 finally:
1916 lock.release()
1916 lock.release()
1917
1917
1918 def clone(self, remote, heads=[], stream=False):
1918 def clone(self, remote, heads=[], stream=False):
1919 '''clone remote repository.
1919 '''clone remote repository.
1920
1920
1921 keyword arguments:
1921 keyword arguments:
1922 heads: list of revs to clone (forces use of pull)
1922 heads: list of revs to clone (forces use of pull)
1923 stream: use streaming clone if possible'''
1923 stream: use streaming clone if possible'''
1924
1924
1925 # now, all clients that can request uncompressed clones can
1925 # now, all clients that can request uncompressed clones can
1926 # read repo formats supported by all servers that can serve
1926 # read repo formats supported by all servers that can serve
1927 # them.
1927 # them.
1928
1928
1929 # if revlog format changes, client will have to check version
1929 # if revlog format changes, client will have to check version
1930 # and format flags on "stream" capability, and use
1930 # and format flags on "stream" capability, and use
1931 # uncompressed only if compatible.
1931 # uncompressed only if compatible.
1932
1932
1933 if stream and not heads:
1933 if stream and not heads:
1934 # 'stream' means remote revlog format is revlogv1 only
1934 # 'stream' means remote revlog format is revlogv1 only
1935 if remote.capable('stream'):
1935 if remote.capable('stream'):
1936 return self.stream_in(remote, set(('revlogv1',)))
1936 return self.stream_in(remote, set(('revlogv1',)))
1937 # otherwise, 'streamreqs' contains the remote revlog format
1937 # otherwise, 'streamreqs' contains the remote revlog format
1938 streamreqs = remote.capable('streamreqs')
1938 streamreqs = remote.capable('streamreqs')
1939 if streamreqs:
1939 if streamreqs:
1940 streamreqs = set(streamreqs.split(','))
1940 streamreqs = set(streamreqs.split(','))
1941 # if we support it, stream in and adjust our requirements
1941 # if we support it, stream in and adjust our requirements
1942 if not streamreqs - self.supportedformats:
1942 if not streamreqs - self.supportedformats:
1943 return self.stream_in(remote, streamreqs)
1943 return self.stream_in(remote, streamreqs)
1944 return self.pull(remote, heads)
1944 return self.pull(remote, heads)
1945
1945
1946 def pushkey(self, namespace, key, old, new):
1946 def pushkey(self, namespace, key, old, new):
1947 self.hook('prepushkey', throw=True, namespace=namespace, key=key,
1947 self.hook('prepushkey', throw=True, namespace=namespace, key=key,
1948 old=old, new=new)
1948 old=old, new=new)
1949 ret = pushkey.push(self, namespace, key, old, new)
1949 ret = pushkey.push(self, namespace, key, old, new)
1950 self.hook('pushkey', namespace=namespace, key=key, old=old, new=new,
1950 self.hook('pushkey', namespace=namespace, key=key, old=old, new=new,
1951 ret=ret)
1951 ret=ret)
1952 return ret
1952 return ret
1953
1953
1954 def listkeys(self, namespace):
1954 def listkeys(self, namespace):
1955 self.hook('prelistkeys', throw=True, namespace=namespace)
1955 self.hook('prelistkeys', throw=True, namespace=namespace)
1956 values = pushkey.list(self, namespace)
1956 values = pushkey.list(self, namespace)
1957 self.hook('listkeys', namespace=namespace, values=values)
1957 self.hook('listkeys', namespace=namespace, values=values)
1958 return values
1958 return values
1959
1959
1960 def debugwireargs(self, one, two, three=None, four=None, five=None):
1960 def debugwireargs(self, one, two, three=None, four=None, five=None):
1961 '''used to test argument passing over the wire'''
1961 '''used to test argument passing over the wire'''
1962 return "%s %s %s %s %s" % (one, two, three, four, five)
1962 return "%s %s %s %s %s" % (one, two, three, four, five)
1963
1963
1964 def savecommitmessage(self, text):
1964 def savecommitmessage(self, text):
1965 fp = self.opener('last-message.txt', 'wb')
1965 fp = self.opener('last-message.txt', 'wb')
1966 try:
1966 try:
1967 fp.write(text)
1967 fp.write(text)
1968 finally:
1968 finally:
1969 fp.close()
1969 fp.close()
1970 return self.pathto(fp.name[len(self.root)+1:])
1970 return self.pathto(fp.name[len(self.root)+1:])
1971
1971
1972 # used to avoid circular references so destructors work
1972 # used to avoid circular references so destructors work
1973 def aftertrans(files):
1973 def aftertrans(files):
1974 renamefiles = [tuple(t) for t in files]
1974 renamefiles = [tuple(t) for t in files]
1975 def a():
1975 def a():
1976 for src, dest in renamefiles:
1976 for src, dest in renamefiles:
1977 util.rename(src, dest)
1977 util.rename(src, dest)
1978 return a
1978 return a
1979
1979
1980 def undoname(fn):
1980 def undoname(fn):
1981 base, name = os.path.split(fn)
1981 base, name = os.path.split(fn)
1982 assert name.startswith('journal')
1982 assert name.startswith('journal')
1983 return os.path.join(base, name.replace('journal', 'undo', 1))
1983 return os.path.join(base, name.replace('journal', 'undo', 1))
1984
1984
1985 def instance(ui, path, create):
1985 def instance(ui, path, create):
1986 return localrepository(ui, util.localpath(path), create)
1986 return localrepository(ui, util.localpath(path), create)
1987
1987
1988 def islocal(path):
1988 def islocal(path):
1989 return True
1989 return True
@@ -1,1278 +1,1278 b''
1 # revlog.py - storage back-end for mercurial
1 # revlog.py - storage back-end for mercurial
2 #
2 #
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 """Storage back-end for Mercurial.
8 """Storage back-end for Mercurial.
9
9
10 This provides efficient delta storage with O(1) retrieve and append
10 This provides efficient delta storage with O(1) retrieve and append
11 and O(changes) merge between branches.
11 and O(changes) merge between branches.
12 """
12 """
13
13
14 # import stuff from node for others to import from revlog
14 # import stuff from node for others to import from revlog
15 from node import bin, hex, nullid, nullrev
15 from node import bin, hex, nullid, nullrev
16 from i18n import _
16 from i18n import _
17 import ancestor, mdiff, parsers, error, util, dagutil
17 import ancestor, mdiff, parsers, error, util, dagutil
18 import struct, zlib, errno
18 import struct, zlib, errno
19
19
20 _pack = struct.pack
20 _pack = struct.pack
21 _unpack = struct.unpack
21 _unpack = struct.unpack
22 _compress = zlib.compress
22 _compress = zlib.compress
23 _decompress = zlib.decompress
23 _decompress = zlib.decompress
24 _sha = util.sha1
24 _sha = util.sha1
25
25
26 # revlog header flags
26 # revlog header flags
27 REVLOGV0 = 0
27 REVLOGV0 = 0
28 REVLOGNG = 1
28 REVLOGNG = 1
29 REVLOGNGINLINEDATA = (1 << 16)
29 REVLOGNGINLINEDATA = (1 << 16)
30 REVLOGGENERALDELTA = (1 << 17)
30 REVLOGGENERALDELTA = (1 << 17)
31 REVLOG_DEFAULT_FLAGS = REVLOGNGINLINEDATA
31 REVLOG_DEFAULT_FLAGS = REVLOGNGINLINEDATA
32 REVLOG_DEFAULT_FORMAT = REVLOGNG
32 REVLOG_DEFAULT_FORMAT = REVLOGNG
33 REVLOG_DEFAULT_VERSION = REVLOG_DEFAULT_FORMAT | REVLOG_DEFAULT_FLAGS
33 REVLOG_DEFAULT_VERSION = REVLOG_DEFAULT_FORMAT | REVLOG_DEFAULT_FLAGS
34 REVLOGNG_FLAGS = REVLOGNGINLINEDATA | REVLOGGENERALDELTA
34 REVLOGNG_FLAGS = REVLOGNGINLINEDATA | REVLOGGENERALDELTA
35
35
36 # revlog index flags
36 # revlog index flags
37 REVIDX_KNOWN_FLAGS = 0
37 REVIDX_KNOWN_FLAGS = 0
38
38
39 # max size of revlog with inline data
39 # max size of revlog with inline data
40 _maxinline = 131072
40 _maxinline = 131072
41 _chunksize = 1048576
41 _chunksize = 1048576
42
42
43 RevlogError = error.RevlogError
43 RevlogError = error.RevlogError
44 LookupError = error.LookupError
44 LookupError = error.LookupError
45
45
46 def getoffset(q):
46 def getoffset(q):
47 return int(q >> 16)
47 return int(q >> 16)
48
48
49 def gettype(q):
49 def gettype(q):
50 return int(q & 0xFFFF)
50 return int(q & 0xFFFF)
51
51
52 def offset_type(offset, type):
52 def offset_type(offset, type):
53 return long(long(offset) << 16 | type)
53 return long(long(offset) << 16 | type)
54
54
55 nullhash = _sha(nullid)
55 nullhash = _sha(nullid)
56
56
57 def hash(text, p1, p2):
57 def hash(text, p1, p2):
58 """generate a hash from the given text and its parent hashes
58 """generate a hash from the given text and its parent hashes
59
59
60 This hash combines both the current file contents and its history
60 This hash combines both the current file contents and its history
61 in a manner that makes it easy to distinguish nodes with the same
61 in a manner that makes it easy to distinguish nodes with the same
62 content in the revision graph.
62 content in the revision graph.
63 """
63 """
64 # As of now, if one of the parent node is null, p2 is null
64 # As of now, if one of the parent node is null, p2 is null
65 if p2 == nullid:
65 if p2 == nullid:
66 # deep copy of a hash is faster than creating one
66 # deep copy of a hash is faster than creating one
67 s = nullhash.copy()
67 s = nullhash.copy()
68 s.update(p1)
68 s.update(p1)
69 else:
69 else:
70 # none of the parent nodes are nullid
70 # none of the parent nodes are nullid
71 l = [p1, p2]
71 l = [p1, p2]
72 l.sort()
72 l.sort()
73 s = _sha(l[0])
73 s = _sha(l[0])
74 s.update(l[1])
74 s.update(l[1])
75 s.update(text)
75 s.update(text)
76 return s.digest()
76 return s.digest()
77
77
78 def compress(text):
78 def compress(text):
79 """ generate a possibly-compressed representation of text """
79 """ generate a possibly-compressed representation of text """
80 if not text:
80 if not text:
81 return ("", text)
81 return ("", text)
82 l = len(text)
82 l = len(text)
83 bin = None
83 bin = None
84 if l < 44:
84 if l < 44:
85 pass
85 pass
86 elif l > 1000000:
86 elif l > 1000000:
87 # zlib makes an internal copy, thus doubling memory usage for
87 # zlib makes an internal copy, thus doubling memory usage for
88 # large files, so lets do this in pieces
88 # large files, so lets do this in pieces
89 z = zlib.compressobj()
89 z = zlib.compressobj()
90 p = []
90 p = []
91 pos = 0
91 pos = 0
92 while pos < l:
92 while pos < l:
93 pos2 = pos + 2**20
93 pos2 = pos + 2**20
94 p.append(z.compress(text[pos:pos2]))
94 p.append(z.compress(text[pos:pos2]))
95 pos = pos2
95 pos = pos2
96 p.append(z.flush())
96 p.append(z.flush())
97 if sum(map(len, p)) < l:
97 if sum(map(len, p)) < l:
98 bin = "".join(p)
98 bin = "".join(p)
99 else:
99 else:
100 bin = _compress(text)
100 bin = _compress(text)
101 if bin is None or len(bin) > l:
101 if bin is None or len(bin) > l:
102 if text[0] == '\0':
102 if text[0] == '\0':
103 return ("", text)
103 return ("", text)
104 return ('u', text)
104 return ('u', text)
105 return ("", bin)
105 return ("", bin)
106
106
107 def decompress(bin):
107 def decompress(bin):
108 """ decompress the given input """
108 """ decompress the given input """
109 if not bin:
109 if not bin:
110 return bin
110 return bin
111 t = bin[0]
111 t = bin[0]
112 if t == '\0':
112 if t == '\0':
113 return bin
113 return bin
114 if t == 'x':
114 if t == 'x':
115 return _decompress(bin)
115 return _decompress(bin)
116 if t == 'u':
116 if t == 'u':
117 return bin[1:]
117 return bin[1:]
118 raise RevlogError(_("unknown compression type %r") % t)
118 raise RevlogError(_("unknown compression type %r") % t)
119
119
120 indexformatv0 = ">4l20s20s20s"
120 indexformatv0 = ">4l20s20s20s"
121 v0shaoffset = 56
121 v0shaoffset = 56
122
122
123 class revlogoldio(object):
123 class revlogoldio(object):
124 def __init__(self):
124 def __init__(self):
125 self.size = struct.calcsize(indexformatv0)
125 self.size = struct.calcsize(indexformatv0)
126
126
127 def parseindex(self, data, inline):
127 def parseindex(self, data, inline):
128 s = self.size
128 s = self.size
129 index = []
129 index = []
130 nodemap = {nullid: nullrev}
130 nodemap = {nullid: nullrev}
131 n = off = 0
131 n = off = 0
132 l = len(data)
132 l = len(data)
133 while off + s <= l:
133 while off + s <= l:
134 cur = data[off:off + s]
134 cur = data[off:off + s]
135 off += s
135 off += s
136 e = _unpack(indexformatv0, cur)
136 e = _unpack(indexformatv0, cur)
137 # transform to revlogv1 format
137 # transform to revlogv1 format
138 e2 = (offset_type(e[0], 0), e[1], -1, e[2], e[3],
138 e2 = (offset_type(e[0], 0), e[1], -1, e[2], e[3],
139 nodemap.get(e[4], nullrev), nodemap.get(e[5], nullrev), e[6])
139 nodemap.get(e[4], nullrev), nodemap.get(e[5], nullrev), e[6])
140 index.append(e2)
140 index.append(e2)
141 nodemap[e[6]] = n
141 nodemap[e[6]] = n
142 n += 1
142 n += 1
143
143
144 # add the magic null revision at -1
144 # add the magic null revision at -1
145 index.append((0, 0, 0, -1, -1, -1, -1, nullid))
145 index.append((0, 0, 0, -1, -1, -1, -1, nullid))
146
146
147 return index, nodemap, None
147 return index, nodemap, None
148
148
149 def packentry(self, entry, node, version, rev):
149 def packentry(self, entry, node, version, rev):
150 if gettype(entry[0]):
150 if gettype(entry[0]):
151 raise RevlogError(_("index entry flags need RevlogNG"))
151 raise RevlogError(_("index entry flags need RevlogNG"))
152 e2 = (getoffset(entry[0]), entry[1], entry[3], entry[4],
152 e2 = (getoffset(entry[0]), entry[1], entry[3], entry[4],
153 node(entry[5]), node(entry[6]), entry[7])
153 node(entry[5]), node(entry[6]), entry[7])
154 return _pack(indexformatv0, *e2)
154 return _pack(indexformatv0, *e2)
155
155
156 # index ng:
156 # index ng:
157 # 6 bytes: offset
157 # 6 bytes: offset
158 # 2 bytes: flags
158 # 2 bytes: flags
159 # 4 bytes: compressed length
159 # 4 bytes: compressed length
160 # 4 bytes: uncompressed length
160 # 4 bytes: uncompressed length
161 # 4 bytes: base rev
161 # 4 bytes: base rev
162 # 4 bytes: link rev
162 # 4 bytes: link rev
163 # 4 bytes: parent 1 rev
163 # 4 bytes: parent 1 rev
164 # 4 bytes: parent 2 rev
164 # 4 bytes: parent 2 rev
165 # 32 bytes: nodeid
165 # 32 bytes: nodeid
166 indexformatng = ">Qiiiiii20s12x"
166 indexformatng = ">Qiiiiii20s12x"
167 ngshaoffset = 32
167 ngshaoffset = 32
168 versionformat = ">I"
168 versionformat = ">I"
169
169
170 class revlogio(object):
170 class revlogio(object):
171 def __init__(self):
171 def __init__(self):
172 self.size = struct.calcsize(indexformatng)
172 self.size = struct.calcsize(indexformatng)
173
173
174 def parseindex(self, data, inline):
174 def parseindex(self, data, inline):
175 # call the C implementation to parse the index data
175 # call the C implementation to parse the index data
176 index, cache = parsers.parse_index2(data, inline)
176 index, cache = parsers.parse_index2(data, inline)
177 return index, None, cache
177 return index, None, cache
178
178
179 def packentry(self, entry, node, version, rev):
179 def packentry(self, entry, node, version, rev):
180 p = _pack(indexformatng, *entry)
180 p = _pack(indexformatng, *entry)
181 if rev == 0:
181 if rev == 0:
182 p = _pack(versionformat, version) + p[4:]
182 p = _pack(versionformat, version) + p[4:]
183 return p
183 return p
184
184
185 class revlog(object):
185 class revlog(object):
186 """
186 """
187 the underlying revision storage object
187 the underlying revision storage object
188
188
189 A revlog consists of two parts, an index and the revision data.
189 A revlog consists of two parts, an index and the revision data.
190
190
191 The index is a file with a fixed record size containing
191 The index is a file with a fixed record size containing
192 information on each revision, including its nodeid (hash), the
192 information on each revision, including its nodeid (hash), the
193 nodeids of its parents, the position and offset of its data within
193 nodeids of its parents, the position and offset of its data within
194 the data file, and the revision it's based on. Finally, each entry
194 the data file, and the revision it's based on. Finally, each entry
195 contains a linkrev entry that can serve as a pointer to external
195 contains a linkrev entry that can serve as a pointer to external
196 data.
196 data.
197
197
198 The revision data itself is a linear collection of data chunks.
198 The revision data itself is a linear collection of data chunks.
199 Each chunk represents a revision and is usually represented as a
199 Each chunk represents a revision and is usually represented as a
200 delta against the previous chunk. To bound lookup time, runs of
200 delta against the previous chunk. To bound lookup time, runs of
201 deltas are limited to about 2 times the length of the original
201 deltas are limited to about 2 times the length of the original
202 version data. This makes retrieval of a version proportional to
202 version data. This makes retrieval of a version proportional to
203 its size, or O(1) relative to the number of revisions.
203 its size, or O(1) relative to the number of revisions.
204
204
205 Both pieces of the revlog are written to in an append-only
205 Both pieces of the revlog are written to in an append-only
206 fashion, which means we never need to rewrite a file to insert or
206 fashion, which means we never need to rewrite a file to insert or
207 remove data, and can use some simple techniques to avoid the need
207 remove data, and can use some simple techniques to avoid the need
208 for locking while reading.
208 for locking while reading.
209 """
209 """
210 def __init__(self, opener, indexfile):
210 def __init__(self, opener, indexfile):
211 """
211 """
212 create a revlog object
212 create a revlog object
213
213
214 opener is a function that abstracts the file opening operation
214 opener is a function that abstracts the file opening operation
215 and can be used to implement COW semantics or the like.
215 and can be used to implement COW semantics or the like.
216 """
216 """
217 self.indexfile = indexfile
217 self.indexfile = indexfile
218 self.datafile = indexfile[:-2] + ".d"
218 self.datafile = indexfile[:-2] + ".d"
219 self.opener = opener
219 self.opener = opener
220 self._cache = None
220 self._cache = None
221 self._basecache = (0, 0)
221 self._basecache = (0, 0)
222 self._chunkcache = (0, '')
222 self._chunkcache = (0, '')
223 self.index = []
223 self.index = []
224 self._pcache = {}
224 self._pcache = {}
225 self._nodecache = {nullid: nullrev}
225 self._nodecache = {nullid: nullrev}
226 self._nodepos = None
226 self._nodepos = None
227
227
228 v = REVLOG_DEFAULT_VERSION
228 v = REVLOG_DEFAULT_VERSION
229 if hasattr(opener, 'options'):
229 if hasattr(opener, 'options'):
230 if 'revlogv1' in opener.options:
230 if 'revlogv1' in opener.options:
231 if 'generaldelta' in opener.options:
231 if 'generaldelta' in opener.options:
232 v |= REVLOGGENERALDELTA
232 v |= REVLOGGENERALDELTA
233 else:
233 else:
234 v = 0
234 v = 0
235
235
236 i = ''
236 i = ''
237 self._initempty = True
237 self._initempty = True
238 try:
238 try:
239 f = self.opener(self.indexfile)
239 f = self.opener(self.indexfile)
240 i = f.read()
240 i = f.read()
241 f.close()
241 f.close()
242 if len(i) > 0:
242 if len(i) > 0:
243 v = struct.unpack(versionformat, i[:4])[0]
243 v = struct.unpack(versionformat, i[:4])[0]
244 self._initempty = False
244 self._initempty = False
245 except IOError, inst:
245 except IOError, inst:
246 if inst.errno != errno.ENOENT:
246 if inst.errno != errno.ENOENT:
247 raise
247 raise
248
248
249 self.version = v
249 self.version = v
250 self._inline = v & REVLOGNGINLINEDATA
250 self._inline = v & REVLOGNGINLINEDATA
251 self._generaldelta = v & REVLOGGENERALDELTA
251 self._generaldelta = v & REVLOGGENERALDELTA
252 flags = v & ~0xFFFF
252 flags = v & ~0xFFFF
253 fmt = v & 0xFFFF
253 fmt = v & 0xFFFF
254 if fmt == REVLOGV0 and flags:
254 if fmt == REVLOGV0 and flags:
255 raise RevlogError(_("index %s unknown flags %#04x for format v0")
255 raise RevlogError(_("index %s unknown flags %#04x for format v0")
256 % (self.indexfile, flags >> 16))
256 % (self.indexfile, flags >> 16))
257 elif fmt == REVLOGNG and flags & ~REVLOGNG_FLAGS:
257 elif fmt == REVLOGNG and flags & ~REVLOGNG_FLAGS:
258 raise RevlogError(_("index %s unknown flags %#04x for revlogng")
258 raise RevlogError(_("index %s unknown flags %#04x for revlogng")
259 % (self.indexfile, flags >> 16))
259 % (self.indexfile, flags >> 16))
260 elif fmt > REVLOGNG:
260 elif fmt > REVLOGNG:
261 raise RevlogError(_("index %s unknown format %d")
261 raise RevlogError(_("index %s unknown format %d")
262 % (self.indexfile, fmt))
262 % (self.indexfile, fmt))
263
263
264 self._io = revlogio()
264 self._io = revlogio()
265 if self.version == REVLOGV0:
265 if self.version == REVLOGV0:
266 self._io = revlogoldio()
266 self._io = revlogoldio()
267 try:
267 try:
268 d = self._io.parseindex(i, self._inline)
268 d = self._io.parseindex(i, self._inline)
269 except (ValueError, IndexError):
269 except (ValueError, IndexError):
270 raise RevlogError(_("index %s is corrupted") % (self.indexfile))
270 raise RevlogError(_("index %s is corrupted") % (self.indexfile))
271 self.index, nodemap, self._chunkcache = d
271 self.index, nodemap, self._chunkcache = d
272 if nodemap is not None:
272 if nodemap is not None:
273 self.nodemap = self._nodecache = nodemap
273 self.nodemap = self._nodecache = nodemap
274 if not self._chunkcache:
274 if not self._chunkcache:
275 self._chunkclear()
275 self._chunkclear()
276
276
277 def tip(self):
277 def tip(self):
278 return self.node(len(self.index) - 2)
278 return self.node(len(self.index) - 2)
279 def __len__(self):
279 def __len__(self):
280 return len(self.index) - 1
280 return len(self.index) - 1
281 def __iter__(self):
281 def __iter__(self):
282 for i in xrange(len(self)):
282 for i in xrange(len(self)):
283 yield i
283 yield i
284
284
285 @util.propertycache
285 @util.propertycache
286 def nodemap(self):
286 def nodemap(self):
287 self.rev(self.node(0))
287 self.rev(self.node(0))
288 return self._nodecache
288 return self._nodecache
289
289
290 def rev(self, node):
290 def rev(self, node):
291 try:
291 try:
292 return self._nodecache[node]
292 return self._nodecache[node]
293 except KeyError:
293 except KeyError:
294 n = self._nodecache
294 n = self._nodecache
295 i = self.index
295 i = self.index
296 p = self._nodepos
296 p = self._nodepos
297 if p is None:
297 if p is None:
298 p = len(i) - 2
298 p = len(i) - 2
299 for r in xrange(p, -1, -1):
299 for r in xrange(p, -1, -1):
300 v = i[r][7]
300 v = i[r][7]
301 n[v] = r
301 n[v] = r
302 if v == node:
302 if v == node:
303 self._nodepos = r - 1
303 self._nodepos = r - 1
304 return r
304 return r
305 raise LookupError(node, self.indexfile, _('no node'))
305 raise LookupError(node, self.indexfile, _('no node'))
306
306
307 def node(self, rev):
307 def node(self, rev):
308 return self.index[rev][7]
308 return self.index[rev][7]
309 def linkrev(self, rev):
309 def linkrev(self, rev):
310 return self.index[rev][4]
310 return self.index[rev][4]
311 def parents(self, node):
311 def parents(self, node):
312 i = self.index
312 i = self.index
313 d = i[self.rev(node)]
313 d = i[self.rev(node)]
314 return i[d[5]][7], i[d[6]][7] # map revisions to nodes inline
314 return i[d[5]][7], i[d[6]][7] # map revisions to nodes inline
315 def parentrevs(self, rev):
315 def parentrevs(self, rev):
316 return self.index[rev][5:7]
316 return self.index[rev][5:7]
317 def start(self, rev):
317 def start(self, rev):
318 return int(self.index[rev][0] >> 16)
318 return int(self.index[rev][0] >> 16)
319 def end(self, rev):
319 def end(self, rev):
320 return self.start(rev) + self.length(rev)
320 return self.start(rev) + self.length(rev)
321 def length(self, rev):
321 def length(self, rev):
322 return self.index[rev][1]
322 return self.index[rev][1]
323 def chainbase(self, rev):
323 def chainbase(self, rev):
324 index = self.index
324 index = self.index
325 base = index[rev][3]
325 base = index[rev][3]
326 while base != rev:
326 while base != rev:
327 rev = base
327 rev = base
328 base = index[rev][3]
328 base = index[rev][3]
329 return base
329 return base
330 def flags(self, rev):
330 def flags(self, rev):
331 return self.index[rev][0] & 0xFFFF
331 return self.index[rev][0] & 0xFFFF
332 def rawsize(self, rev):
332 def rawsize(self, rev):
333 """return the length of the uncompressed text for a given revision"""
333 """return the length of the uncompressed text for a given revision"""
334 l = self.index[rev][2]
334 l = self.index[rev][2]
335 if l >= 0:
335 if l >= 0:
336 return l
336 return l
337
337
338 t = self.revision(self.node(rev))
338 t = self.revision(self.node(rev))
339 return len(t)
339 return len(t)
340 size = rawsize
340 size = rawsize
341
341
342 def reachable(self, node, stop=None):
342 def reachable(self, node, stop=None):
343 """return the set of all nodes ancestral to a given node, including
343 """return the set of all nodes ancestral to a given node, including
344 the node itself, stopping when stop is matched"""
344 the node itself, stopping when stop is matched"""
345 reachable = set((node,))
345 reachable = set((node,))
346 visit = [node]
346 visit = [node]
347 if stop:
347 if stop:
348 stopn = self.rev(stop)
348 stopn = self.rev(stop)
349 else:
349 else:
350 stopn = 0
350 stopn = 0
351 while visit:
351 while visit:
352 n = visit.pop(0)
352 n = visit.pop(0)
353 if n == stop:
353 if n == stop:
354 continue
354 continue
355 if n == nullid:
355 if n == nullid:
356 continue
356 continue
357 for p in self.parents(n):
357 for p in self.parents(n):
358 if self.rev(p) < stopn:
358 if self.rev(p) < stopn:
359 continue
359 continue
360 if p not in reachable:
360 if p not in reachable:
361 reachable.add(p)
361 reachable.add(p)
362 visit.append(p)
362 visit.append(p)
363 return reachable
363 return reachable
364
364
365 def ancestors(self, *revs):
365 def ancestors(self, *revs):
366 """Generate the ancestors of 'revs' in reverse topological order.
366 """Generate the ancestors of 'revs' in reverse topological order.
367
367
368 Yield a sequence of revision numbers starting with the parents
368 Yield a sequence of revision numbers starting with the parents
369 of each revision in revs, i.e., each revision is *not* considered
369 of each revision in revs, i.e., each revision is *not* considered
370 an ancestor of itself. Results are in breadth-first order:
370 an ancestor of itself. Results are in breadth-first order:
371 parents of each rev in revs, then parents of those, etc. Result
371 parents of each rev in revs, then parents of those, etc. Result
372 does not include the null revision."""
372 does not include the null revision."""
373 visit = list(revs)
373 visit = list(revs)
374 seen = set([nullrev])
374 seen = set([nullrev])
375 while visit:
375 while visit:
376 for parent in self.parentrevs(visit.pop(0)):
376 for parent in self.parentrevs(visit.pop(0)):
377 if parent not in seen:
377 if parent not in seen:
378 visit.append(parent)
378 visit.append(parent)
379 seen.add(parent)
379 seen.add(parent)
380 yield parent
380 yield parent
381
381
382 def descendants(self, *revs):
382 def descendants(self, *revs):
383 """Generate the descendants of 'revs' in revision order.
383 """Generate the descendants of 'revs' in revision order.
384
384
385 Yield a sequence of revision numbers starting with a child of
385 Yield a sequence of revision numbers starting with a child of
386 some rev in revs, i.e., each revision is *not* considered a
386 some rev in revs, i.e., each revision is *not* considered a
387 descendant of itself. Results are ordered by revision number (a
387 descendant of itself. Results are ordered by revision number (a
388 topological sort)."""
388 topological sort)."""
389 first = min(revs)
389 first = min(revs)
390 if first == nullrev:
390 if first == nullrev:
391 for i in self:
391 for i in self:
392 yield i
392 yield i
393 return
393 return
394
394
395 seen = set(revs)
395 seen = set(revs)
396 for i in xrange(first + 1, len(self)):
396 for i in xrange(first + 1, len(self)):
397 for x in self.parentrevs(i):
397 for x in self.parentrevs(i):
398 if x != nullrev and x in seen:
398 if x != nullrev and x in seen:
399 seen.add(i)
399 seen.add(i)
400 yield i
400 yield i
401 break
401 break
402
402
403 def findcommonmissing(self, common=None, heads=None):
403 def findcommonmissing(self, common=None, heads=None):
404 """Return a tuple of the ancestors of common and the ancestors of heads
404 """Return a tuple of the ancestors of common and the ancestors of heads
405 that are not ancestors of common.
405 that are not ancestors of common.
406
406
407 More specifically, the second element is a list of nodes N such that
407 More specifically, the second element is a list of nodes N such that
408 every N satisfies the following constraints:
408 every N satisfies the following constraints:
409
409
410 1. N is an ancestor of some node in 'heads'
410 1. N is an ancestor of some node in 'heads'
411 2. N is not an ancestor of any node in 'common'
411 2. N is not an ancestor of any node in 'common'
412
412
413 The list is sorted by revision number, meaning it is
413 The list is sorted by revision number, meaning it is
414 topologically sorted.
414 topologically sorted.
415
415
416 'heads' and 'common' are both lists of node IDs. If heads is
416 'heads' and 'common' are both lists of node IDs. If heads is
417 not supplied, uses all of the revlog's heads. If common is not
417 not supplied, uses all of the revlog's heads. If common is not
418 supplied, uses nullid."""
418 supplied, uses nullid."""
419 if common is None:
419 if common is None:
420 common = [nullid]
420 common = [nullid]
421 if heads is None:
421 if heads is None:
422 heads = self.heads()
422 heads = self.heads()
423
423
424 common = [self.rev(n) for n in common]
424 common = [self.rev(n) for n in common]
425 heads = [self.rev(n) for n in heads]
425 heads = [self.rev(n) for n in heads]
426
426
427 # we want the ancestors, but inclusive
427 # we want the ancestors, but inclusive
428 has = set(self.ancestors(*common))
428 has = set(self.ancestors(*common))
429 has.add(nullrev)
429 has.add(nullrev)
430 has.update(common)
430 has.update(common)
431
431
432 # take all ancestors from heads that aren't in has
432 # take all ancestors from heads that aren't in has
433 missing = set()
433 missing = set()
434 visit = [r for r in heads if r not in has]
434 visit = [r for r in heads if r not in has]
435 while visit:
435 while visit:
436 r = visit.pop(0)
436 r = visit.pop(0)
437 if r in missing:
437 if r in missing:
438 continue
438 continue
439 else:
439 else:
440 missing.add(r)
440 missing.add(r)
441 for p in self.parentrevs(r):
441 for p in self.parentrevs(r):
442 if p not in has:
442 if p not in has:
443 visit.append(p)
443 visit.append(p)
444 missing = list(missing)
444 missing = list(missing)
445 missing.sort()
445 missing.sort()
446 return has, [self.node(r) for r in missing]
446 return has, [self.node(r) for r in missing]
447
447
448 def findmissing(self, common=None, heads=None):
448 def findmissing(self, common=None, heads=None):
449 """Return the ancestors of heads that are not ancestors of common.
449 """Return the ancestors of heads that are not ancestors of common.
450
450
451 More specifically, return a list of nodes N such that every N
451 More specifically, return a list of nodes N such that every N
452 satisfies the following constraints:
452 satisfies the following constraints:
453
453
454 1. N is an ancestor of some node in 'heads'
454 1. N is an ancestor of some node in 'heads'
455 2. N is not an ancestor of any node in 'common'
455 2. N is not an ancestor of any node in 'common'
456
456
457 The list is sorted by revision number, meaning it is
457 The list is sorted by revision number, meaning it is
458 topologically sorted.
458 topologically sorted.
459
459
460 'heads' and 'common' are both lists of node IDs. If heads is
460 'heads' and 'common' are both lists of node IDs. If heads is
461 not supplied, uses all of the revlog's heads. If common is not
461 not supplied, uses all of the revlog's heads. If common is not
462 supplied, uses nullid."""
462 supplied, uses nullid."""
463 _common, missing = self.findcommonmissing(common, heads)
463 _common, missing = self.findcommonmissing(common, heads)
464 return missing
464 return missing
465
465
466 def nodesbetween(self, roots=None, heads=None):
466 def nodesbetween(self, roots=None, heads=None):
467 """Return a topological path from 'roots' to 'heads'.
467 """Return a topological path from 'roots' to 'heads'.
468
468
469 Return a tuple (nodes, outroots, outheads) where 'nodes' is a
469 Return a tuple (nodes, outroots, outheads) where 'nodes' is a
470 topologically sorted list of all nodes N that satisfy both of
470 topologically sorted list of all nodes N that satisfy both of
471 these constraints:
471 these constraints:
472
472
473 1. N is a descendant of some node in 'roots'
473 1. N is a descendant of some node in 'roots'
474 2. N is an ancestor of some node in 'heads'
474 2. N is an ancestor of some node in 'heads'
475
475
476 Every node is considered to be both a descendant and an ancestor
476 Every node is considered to be both a descendant and an ancestor
477 of itself, so every reachable node in 'roots' and 'heads' will be
477 of itself, so every reachable node in 'roots' and 'heads' will be
478 included in 'nodes'.
478 included in 'nodes'.
479
479
480 'outroots' is the list of reachable nodes in 'roots', i.e., the
480 'outroots' is the list of reachable nodes in 'roots', i.e., the
481 subset of 'roots' that is returned in 'nodes'. Likewise,
481 subset of 'roots' that is returned in 'nodes'. Likewise,
482 'outheads' is the subset of 'heads' that is also in 'nodes'.
482 'outheads' is the subset of 'heads' that is also in 'nodes'.
483
483
484 'roots' and 'heads' are both lists of node IDs. If 'roots' is
484 'roots' and 'heads' are both lists of node IDs. If 'roots' is
485 unspecified, uses nullid as the only root. If 'heads' is
485 unspecified, uses nullid as the only root. If 'heads' is
486 unspecified, uses list of all of the revlog's heads."""
486 unspecified, uses list of all of the revlog's heads."""
487 nonodes = ([], [], [])
487 nonodes = ([], [], [])
488 if roots is not None:
488 if roots is not None:
489 roots = list(roots)
489 roots = list(roots)
490 if not roots:
490 if not roots:
491 return nonodes
491 return nonodes
492 lowestrev = min([self.rev(n) for n in roots])
492 lowestrev = min([self.rev(n) for n in roots])
493 else:
493 else:
494 roots = [nullid] # Everybody's a descendent of nullid
494 roots = [nullid] # Everybody's a descendant of nullid
495 lowestrev = nullrev
495 lowestrev = nullrev
496 if (lowestrev == nullrev) and (heads is None):
496 if (lowestrev == nullrev) and (heads is None):
497 # We want _all_ the nodes!
497 # We want _all_ the nodes!
498 return ([self.node(r) for r in self], [nullid], list(self.heads()))
498 return ([self.node(r) for r in self], [nullid], list(self.heads()))
499 if heads is None:
499 if heads is None:
500 # All nodes are ancestors, so the latest ancestor is the last
500 # All nodes are ancestors, so the latest ancestor is the last
501 # node.
501 # node.
502 highestrev = len(self) - 1
502 highestrev = len(self) - 1
503 # Set ancestors to None to signal that every node is an ancestor.
503 # Set ancestors to None to signal that every node is an ancestor.
504 ancestors = None
504 ancestors = None
505 # Set heads to an empty dictionary for later discovery of heads
505 # Set heads to an empty dictionary for later discovery of heads
506 heads = {}
506 heads = {}
507 else:
507 else:
508 heads = list(heads)
508 heads = list(heads)
509 if not heads:
509 if not heads:
510 return nonodes
510 return nonodes
511 ancestors = set()
511 ancestors = set()
512 # Turn heads into a dictionary so we can remove 'fake' heads.
512 # Turn heads into a dictionary so we can remove 'fake' heads.
513 # Also, later we will be using it to filter out the heads we can't
513 # Also, later we will be using it to filter out the heads we can't
514 # find from roots.
514 # find from roots.
515 heads = dict.fromkeys(heads, False)
515 heads = dict.fromkeys(heads, False)
516 # Start at the top and keep marking parents until we're done.
516 # Start at the top and keep marking parents until we're done.
517 nodestotag = set(heads)
517 nodestotag = set(heads)
518 # Remember where the top was so we can use it as a limit later.
518 # Remember where the top was so we can use it as a limit later.
519 highestrev = max([self.rev(n) for n in nodestotag])
519 highestrev = max([self.rev(n) for n in nodestotag])
520 while nodestotag:
520 while nodestotag:
521 # grab a node to tag
521 # grab a node to tag
522 n = nodestotag.pop()
522 n = nodestotag.pop()
523 # Never tag nullid
523 # Never tag nullid
524 if n == nullid:
524 if n == nullid:
525 continue
525 continue
526 # A node's revision number represents its place in a
526 # A node's revision number represents its place in a
527 # topologically sorted list of nodes.
527 # topologically sorted list of nodes.
528 r = self.rev(n)
528 r = self.rev(n)
529 if r >= lowestrev:
529 if r >= lowestrev:
530 if n not in ancestors:
530 if n not in ancestors:
531 # If we are possibly a descendent of one of the roots
531 # If we are possibly a descendant of one of the roots
532 # and we haven't already been marked as an ancestor
532 # and we haven't already been marked as an ancestor
533 ancestors.add(n) # Mark as ancestor
533 ancestors.add(n) # Mark as ancestor
534 # Add non-nullid parents to list of nodes to tag.
534 # Add non-nullid parents to list of nodes to tag.
535 nodestotag.update([p for p in self.parents(n) if
535 nodestotag.update([p for p in self.parents(n) if
536 p != nullid])
536 p != nullid])
537 elif n in heads: # We've seen it before, is it a fake head?
537 elif n in heads: # We've seen it before, is it a fake head?
538 # So it is, real heads should not be the ancestors of
538 # So it is, real heads should not be the ancestors of
539 # any other heads.
539 # any other heads.
540 heads.pop(n)
540 heads.pop(n)
541 if not ancestors:
541 if not ancestors:
542 return nonodes
542 return nonodes
543 # Now that we have our set of ancestors, we want to remove any
543 # Now that we have our set of ancestors, we want to remove any
544 # roots that are not ancestors.
544 # roots that are not ancestors.
545
545
546 # If one of the roots was nullid, everything is included anyway.
546 # If one of the roots was nullid, everything is included anyway.
547 if lowestrev > nullrev:
547 if lowestrev > nullrev:
548 # But, since we weren't, let's recompute the lowest rev to not
548 # But, since we weren't, let's recompute the lowest rev to not
549 # include roots that aren't ancestors.
549 # include roots that aren't ancestors.
550
550
551 # Filter out roots that aren't ancestors of heads
551 # Filter out roots that aren't ancestors of heads
552 roots = [n for n in roots if n in ancestors]
552 roots = [n for n in roots if n in ancestors]
553 # Recompute the lowest revision
553 # Recompute the lowest revision
554 if roots:
554 if roots:
555 lowestrev = min([self.rev(n) for n in roots])
555 lowestrev = min([self.rev(n) for n in roots])
556 else:
556 else:
557 # No more roots? Return empty list
557 # No more roots? Return empty list
558 return nonodes
558 return nonodes
559 else:
559 else:
560 # We are descending from nullid, and don't need to care about
560 # We are descending from nullid, and don't need to care about
561 # any other roots.
561 # any other roots.
562 lowestrev = nullrev
562 lowestrev = nullrev
563 roots = [nullid]
563 roots = [nullid]
564 # Transform our roots list into a set.
564 # Transform our roots list into a set.
565 descendents = set(roots)
565 descendants = set(roots)
566 # Also, keep the original roots so we can filter out roots that aren't
566 # Also, keep the original roots so we can filter out roots that aren't
567 # 'real' roots (i.e. are descended from other roots).
567 # 'real' roots (i.e. are descended from other roots).
568 roots = descendents.copy()
568 roots = descendants.copy()
569 # Our topologically sorted list of output nodes.
569 # Our topologically sorted list of output nodes.
570 orderedout = []
570 orderedout = []
571 # Don't start at nullid since we don't want nullid in our output list,
571 # Don't start at nullid since we don't want nullid in our output list,
572 # and if nullid shows up in descedents, empty parents will look like
572 # and if nullid shows up in descedents, empty parents will look like
573 # they're descendents.
573 # they're descendants.
574 for r in xrange(max(lowestrev, 0), highestrev + 1):
574 for r in xrange(max(lowestrev, 0), highestrev + 1):
575 n = self.node(r)
575 n = self.node(r)
576 isdescendent = False
576 isdescendant = False
577 if lowestrev == nullrev: # Everybody is a descendent of nullid
577 if lowestrev == nullrev: # Everybody is a descendant of nullid
578 isdescendent = True
578 isdescendant = True
579 elif n in descendents:
579 elif n in descendants:
580 # n is already a descendent
580 # n is already a descendant
581 isdescendent = True
581 isdescendant = True
582 # This check only needs to be done here because all the roots
582 # This check only needs to be done here because all the roots
583 # will start being marked is descendents before the loop.
583 # will start being marked is descendants before the loop.
584 if n in roots:
584 if n in roots:
585 # If n was a root, check if it's a 'real' root.
585 # If n was a root, check if it's a 'real' root.
586 p = tuple(self.parents(n))
586 p = tuple(self.parents(n))
587 # If any of its parents are descendents, it's not a root.
587 # If any of its parents are descendants, it's not a root.
588 if (p[0] in descendents) or (p[1] in descendents):
588 if (p[0] in descendants) or (p[1] in descendants):
589 roots.remove(n)
589 roots.remove(n)
590 else:
590 else:
591 p = tuple(self.parents(n))
591 p = tuple(self.parents(n))
592 # A node is a descendent if either of its parents are
592 # A node is a descendant if either of its parents are
593 # descendents. (We seeded the dependents list with the roots
593 # descendants. (We seeded the dependents list with the roots
594 # up there, remember?)
594 # up there, remember?)
595 if (p[0] in descendents) or (p[1] in descendents):
595 if (p[0] in descendants) or (p[1] in descendants):
596 descendents.add(n)
596 descendants.add(n)
597 isdescendent = True
597 isdescendant = True
598 if isdescendent and ((ancestors is None) or (n in ancestors)):
598 if isdescendant and ((ancestors is None) or (n in ancestors)):
599 # Only include nodes that are both descendents and ancestors.
599 # Only include nodes that are both descendants and ancestors.
600 orderedout.append(n)
600 orderedout.append(n)
601 if (ancestors is not None) and (n in heads):
601 if (ancestors is not None) and (n in heads):
602 # We're trying to figure out which heads are reachable
602 # We're trying to figure out which heads are reachable
603 # from roots.
603 # from roots.
604 # Mark this head as having been reached
604 # Mark this head as having been reached
605 heads[n] = True
605 heads[n] = True
606 elif ancestors is None:
606 elif ancestors is None:
607 # Otherwise, we're trying to discover the heads.
607 # Otherwise, we're trying to discover the heads.
608 # Assume this is a head because if it isn't, the next step
608 # Assume this is a head because if it isn't, the next step
609 # will eventually remove it.
609 # will eventually remove it.
610 heads[n] = True
610 heads[n] = True
611 # But, obviously its parents aren't.
611 # But, obviously its parents aren't.
612 for p in self.parents(n):
612 for p in self.parents(n):
613 heads.pop(p, None)
613 heads.pop(p, None)
614 heads = [n for n, flag in heads.iteritems() if flag]
614 heads = [n for n, flag in heads.iteritems() if flag]
615 roots = list(roots)
615 roots = list(roots)
616 assert orderedout
616 assert orderedout
617 assert roots
617 assert roots
618 assert heads
618 assert heads
619 return (orderedout, roots, heads)
619 return (orderedout, roots, heads)
620
620
621 def headrevs(self):
621 def headrevs(self):
622 count = len(self)
622 count = len(self)
623 if not count:
623 if not count:
624 return [nullrev]
624 return [nullrev]
625 ishead = [1] * (count + 1)
625 ishead = [1] * (count + 1)
626 index = self.index
626 index = self.index
627 for r in xrange(count):
627 for r in xrange(count):
628 e = index[r]
628 e = index[r]
629 ishead[e[5]] = ishead[e[6]] = 0
629 ishead[e[5]] = ishead[e[6]] = 0
630 return [r for r in xrange(count) if ishead[r]]
630 return [r for r in xrange(count) if ishead[r]]
631
631
632 def heads(self, start=None, stop=None):
632 def heads(self, start=None, stop=None):
633 """return the list of all nodes that have no children
633 """return the list of all nodes that have no children
634
634
635 if start is specified, only heads that are descendants of
635 if start is specified, only heads that are descendants of
636 start will be returned
636 start will be returned
637 if stop is specified, it will consider all the revs from stop
637 if stop is specified, it will consider all the revs from stop
638 as if they had no children
638 as if they had no children
639 """
639 """
640 if start is None and stop is None:
640 if start is None and stop is None:
641 if not len(self):
641 if not len(self):
642 return [nullid]
642 return [nullid]
643 return [self.node(r) for r in self.headrevs()]
643 return [self.node(r) for r in self.headrevs()]
644
644
645 if start is None:
645 if start is None:
646 start = nullid
646 start = nullid
647 if stop is None:
647 if stop is None:
648 stop = []
648 stop = []
649 stoprevs = set([self.rev(n) for n in stop])
649 stoprevs = set([self.rev(n) for n in stop])
650 startrev = self.rev(start)
650 startrev = self.rev(start)
651 reachable = set((startrev,))
651 reachable = set((startrev,))
652 heads = set((startrev,))
652 heads = set((startrev,))
653
653
654 parentrevs = self.parentrevs
654 parentrevs = self.parentrevs
655 for r in xrange(startrev + 1, len(self)):
655 for r in xrange(startrev + 1, len(self)):
656 for p in parentrevs(r):
656 for p in parentrevs(r):
657 if p in reachable:
657 if p in reachable:
658 if r not in stoprevs:
658 if r not in stoprevs:
659 reachable.add(r)
659 reachable.add(r)
660 heads.add(r)
660 heads.add(r)
661 if p in heads and p not in stoprevs:
661 if p in heads and p not in stoprevs:
662 heads.remove(p)
662 heads.remove(p)
663
663
664 return [self.node(r) for r in heads]
664 return [self.node(r) for r in heads]
665
665
666 def children(self, node):
666 def children(self, node):
667 """find the children of a given node"""
667 """find the children of a given node"""
668 c = []
668 c = []
669 p = self.rev(node)
669 p = self.rev(node)
670 for r in range(p + 1, len(self)):
670 for r in range(p + 1, len(self)):
671 prevs = [pr for pr in self.parentrevs(r) if pr != nullrev]
671 prevs = [pr for pr in self.parentrevs(r) if pr != nullrev]
672 if prevs:
672 if prevs:
673 for pr in prevs:
673 for pr in prevs:
674 if pr == p:
674 if pr == p:
675 c.append(self.node(r))
675 c.append(self.node(r))
676 elif p == nullrev:
676 elif p == nullrev:
677 c.append(self.node(r))
677 c.append(self.node(r))
678 return c
678 return c
679
679
680 def descendant(self, start, end):
680 def descendant(self, start, end):
681 if start == nullrev:
681 if start == nullrev:
682 return True
682 return True
683 for i in self.descendants(start):
683 for i in self.descendants(start):
684 if i == end:
684 if i == end:
685 return True
685 return True
686 elif i > end:
686 elif i > end:
687 break
687 break
688 return False
688 return False
689
689
690 def ancestor(self, a, b):
690 def ancestor(self, a, b):
691 """calculate the least common ancestor of nodes a and b"""
691 """calculate the least common ancestor of nodes a and b"""
692
692
693 # fast path, check if it is a descendant
693 # fast path, check if it is a descendant
694 a, b = self.rev(a), self.rev(b)
694 a, b = self.rev(a), self.rev(b)
695 start, end = sorted((a, b))
695 start, end = sorted((a, b))
696 if self.descendant(start, end):
696 if self.descendant(start, end):
697 return self.node(start)
697 return self.node(start)
698
698
699 def parents(rev):
699 def parents(rev):
700 return [p for p in self.parentrevs(rev) if p != nullrev]
700 return [p for p in self.parentrevs(rev) if p != nullrev]
701
701
702 c = ancestor.ancestor(a, b, parents)
702 c = ancestor.ancestor(a, b, parents)
703 if c is None:
703 if c is None:
704 return nullid
704 return nullid
705
705
706 return self.node(c)
706 return self.node(c)
707
707
708 def _match(self, id):
708 def _match(self, id):
709 if isinstance(id, (long, int)):
709 if isinstance(id, (long, int)):
710 # rev
710 # rev
711 return self.node(id)
711 return self.node(id)
712 if len(id) == 20:
712 if len(id) == 20:
713 # possibly a binary node
713 # possibly a binary node
714 # odds of a binary node being all hex in ASCII are 1 in 10**25
714 # odds of a binary node being all hex in ASCII are 1 in 10**25
715 try:
715 try:
716 node = id
716 node = id
717 self.rev(node) # quick search the index
717 self.rev(node) # quick search the index
718 return node
718 return node
719 except LookupError:
719 except LookupError:
720 pass # may be partial hex id
720 pass # may be partial hex id
721 try:
721 try:
722 # str(rev)
722 # str(rev)
723 rev = int(id)
723 rev = int(id)
724 if str(rev) != id:
724 if str(rev) != id:
725 raise ValueError
725 raise ValueError
726 if rev < 0:
726 if rev < 0:
727 rev = len(self) + rev
727 rev = len(self) + rev
728 if rev < 0 or rev >= len(self):
728 if rev < 0 or rev >= len(self):
729 raise ValueError
729 raise ValueError
730 return self.node(rev)
730 return self.node(rev)
731 except (ValueError, OverflowError):
731 except (ValueError, OverflowError):
732 pass
732 pass
733 if len(id) == 40:
733 if len(id) == 40:
734 try:
734 try:
735 # a full hex nodeid?
735 # a full hex nodeid?
736 node = bin(id)
736 node = bin(id)
737 self.rev(node)
737 self.rev(node)
738 return node
738 return node
739 except (TypeError, LookupError):
739 except (TypeError, LookupError):
740 pass
740 pass
741
741
742 def _partialmatch(self, id):
742 def _partialmatch(self, id):
743 if id in self._pcache:
743 if id in self._pcache:
744 return self._pcache[id]
744 return self._pcache[id]
745
745
746 if len(id) < 40:
746 if len(id) < 40:
747 try:
747 try:
748 # hex(node)[:...]
748 # hex(node)[:...]
749 l = len(id) // 2 # grab an even number of digits
749 l = len(id) // 2 # grab an even number of digits
750 prefix = bin(id[:l * 2])
750 prefix = bin(id[:l * 2])
751 nl = [e[7] for e in self.index if e[7].startswith(prefix)]
751 nl = [e[7] for e in self.index if e[7].startswith(prefix)]
752 nl = [n for n in nl if hex(n).startswith(id)]
752 nl = [n for n in nl if hex(n).startswith(id)]
753 if len(nl) > 0:
753 if len(nl) > 0:
754 if len(nl) == 1:
754 if len(nl) == 1:
755 self._pcache[id] = nl[0]
755 self._pcache[id] = nl[0]
756 return nl[0]
756 return nl[0]
757 raise LookupError(id, self.indexfile,
757 raise LookupError(id, self.indexfile,
758 _('ambiguous identifier'))
758 _('ambiguous identifier'))
759 return None
759 return None
760 except TypeError:
760 except TypeError:
761 pass
761 pass
762
762
763 def lookup(self, id):
763 def lookup(self, id):
764 """locate a node based on:
764 """locate a node based on:
765 - revision number or str(revision number)
765 - revision number or str(revision number)
766 - nodeid or subset of hex nodeid
766 - nodeid or subset of hex nodeid
767 """
767 """
768 n = self._match(id)
768 n = self._match(id)
769 if n is not None:
769 if n is not None:
770 return n
770 return n
771 n = self._partialmatch(id)
771 n = self._partialmatch(id)
772 if n:
772 if n:
773 return n
773 return n
774
774
775 raise LookupError(id, self.indexfile, _('no match found'))
775 raise LookupError(id, self.indexfile, _('no match found'))
776
776
777 def cmp(self, node, text):
777 def cmp(self, node, text):
778 """compare text with a given file revision
778 """compare text with a given file revision
779
779
780 returns True if text is different than what is stored.
780 returns True if text is different than what is stored.
781 """
781 """
782 p1, p2 = self.parents(node)
782 p1, p2 = self.parents(node)
783 return hash(text, p1, p2) != node
783 return hash(text, p1, p2) != node
784
784
785 def _addchunk(self, offset, data):
785 def _addchunk(self, offset, data):
786 o, d = self._chunkcache
786 o, d = self._chunkcache
787 # try to add to existing cache
787 # try to add to existing cache
788 if o + len(d) == offset and len(d) + len(data) < _chunksize:
788 if o + len(d) == offset and len(d) + len(data) < _chunksize:
789 self._chunkcache = o, d + data
789 self._chunkcache = o, d + data
790 else:
790 else:
791 self._chunkcache = offset, data
791 self._chunkcache = offset, data
792
792
793 def _loadchunk(self, offset, length):
793 def _loadchunk(self, offset, length):
794 if self._inline:
794 if self._inline:
795 df = self.opener(self.indexfile)
795 df = self.opener(self.indexfile)
796 else:
796 else:
797 df = self.opener(self.datafile)
797 df = self.opener(self.datafile)
798
798
799 readahead = max(65536, length)
799 readahead = max(65536, length)
800 df.seek(offset)
800 df.seek(offset)
801 d = df.read(readahead)
801 d = df.read(readahead)
802 self._addchunk(offset, d)
802 self._addchunk(offset, d)
803 if readahead > length:
803 if readahead > length:
804 return d[:length]
804 return d[:length]
805 return d
805 return d
806
806
807 def _getchunk(self, offset, length):
807 def _getchunk(self, offset, length):
808 o, d = self._chunkcache
808 o, d = self._chunkcache
809 l = len(d)
809 l = len(d)
810
810
811 # is it in the cache?
811 # is it in the cache?
812 cachestart = offset - o
812 cachestart = offset - o
813 cacheend = cachestart + length
813 cacheend = cachestart + length
814 if cachestart >= 0 and cacheend <= l:
814 if cachestart >= 0 and cacheend <= l:
815 if cachestart == 0 and cacheend == l:
815 if cachestart == 0 and cacheend == l:
816 return d # avoid a copy
816 return d # avoid a copy
817 return d[cachestart:cacheend]
817 return d[cachestart:cacheend]
818
818
819 return self._loadchunk(offset, length)
819 return self._loadchunk(offset, length)
820
820
821 def _chunkraw(self, startrev, endrev):
821 def _chunkraw(self, startrev, endrev):
822 start = self.start(startrev)
822 start = self.start(startrev)
823 length = self.end(endrev) - start
823 length = self.end(endrev) - start
824 if self._inline:
824 if self._inline:
825 start += (startrev + 1) * self._io.size
825 start += (startrev + 1) * self._io.size
826 return self._getchunk(start, length)
826 return self._getchunk(start, length)
827
827
828 def _chunk(self, rev):
828 def _chunk(self, rev):
829 return decompress(self._chunkraw(rev, rev))
829 return decompress(self._chunkraw(rev, rev))
830
830
831 def _chunkbase(self, rev):
831 def _chunkbase(self, rev):
832 return self._chunk(rev)
832 return self._chunk(rev)
833
833
834 def _chunkclear(self):
834 def _chunkclear(self):
835 self._chunkcache = (0, '')
835 self._chunkcache = (0, '')
836
836
837 def deltaparent(self, rev):
837 def deltaparent(self, rev):
838 """return deltaparent of the given revision"""
838 """return deltaparent of the given revision"""
839 base = self.index[rev][3]
839 base = self.index[rev][3]
840 if base == rev:
840 if base == rev:
841 return nullrev
841 return nullrev
842 elif self._generaldelta:
842 elif self._generaldelta:
843 return base
843 return base
844 else:
844 else:
845 return rev - 1
845 return rev - 1
846
846
847 def revdiff(self, rev1, rev2):
847 def revdiff(self, rev1, rev2):
848 """return or calculate a delta between two revisions"""
848 """return or calculate a delta between two revisions"""
849 if rev1 != nullrev and self.deltaparent(rev2) == rev1:
849 if rev1 != nullrev and self.deltaparent(rev2) == rev1:
850 return self._chunk(rev2)
850 return self._chunk(rev2)
851
851
852 return mdiff.textdiff(self.revision(self.node(rev1)),
852 return mdiff.textdiff(self.revision(self.node(rev1)),
853 self.revision(self.node(rev2)))
853 self.revision(self.node(rev2)))
854
854
855 def revision(self, node):
855 def revision(self, node):
856 """return an uncompressed revision of a given node"""
856 """return an uncompressed revision of a given node"""
857 cachedrev = None
857 cachedrev = None
858 if node == nullid:
858 if node == nullid:
859 return ""
859 return ""
860 if self._cache:
860 if self._cache:
861 if self._cache[0] == node:
861 if self._cache[0] == node:
862 return self._cache[2]
862 return self._cache[2]
863 cachedrev = self._cache[1]
863 cachedrev = self._cache[1]
864
864
865 # look up what we need to read
865 # look up what we need to read
866 text = None
866 text = None
867 rev = self.rev(node)
867 rev = self.rev(node)
868
868
869 # check rev flags
869 # check rev flags
870 if self.flags(rev) & ~REVIDX_KNOWN_FLAGS:
870 if self.flags(rev) & ~REVIDX_KNOWN_FLAGS:
871 raise RevlogError(_('incompatible revision flag %x') %
871 raise RevlogError(_('incompatible revision flag %x') %
872 (self.flags(rev) & ~REVIDX_KNOWN_FLAGS))
872 (self.flags(rev) & ~REVIDX_KNOWN_FLAGS))
873
873
874 # build delta chain
874 # build delta chain
875 chain = []
875 chain = []
876 index = self.index # for performance
876 index = self.index # for performance
877 generaldelta = self._generaldelta
877 generaldelta = self._generaldelta
878 iterrev = rev
878 iterrev = rev
879 e = index[iterrev]
879 e = index[iterrev]
880 while iterrev != e[3] and iterrev != cachedrev:
880 while iterrev != e[3] and iterrev != cachedrev:
881 chain.append(iterrev)
881 chain.append(iterrev)
882 if generaldelta:
882 if generaldelta:
883 iterrev = e[3]
883 iterrev = e[3]
884 else:
884 else:
885 iterrev -= 1
885 iterrev -= 1
886 e = index[iterrev]
886 e = index[iterrev]
887 chain.reverse()
887 chain.reverse()
888 base = iterrev
888 base = iterrev
889
889
890 if iterrev == cachedrev:
890 if iterrev == cachedrev:
891 # cache hit
891 # cache hit
892 text = self._cache[2]
892 text = self._cache[2]
893
893
894 # drop cache to save memory
894 # drop cache to save memory
895 self._cache = None
895 self._cache = None
896
896
897 self._chunkraw(base, rev)
897 self._chunkraw(base, rev)
898 if text is None:
898 if text is None:
899 text = self._chunkbase(base)
899 text = self._chunkbase(base)
900
900
901 bins = [self._chunk(r) for r in chain]
901 bins = [self._chunk(r) for r in chain]
902 text = mdiff.patches(text, bins)
902 text = mdiff.patches(text, bins)
903
903
904 text = self._checkhash(text, node, rev)
904 text = self._checkhash(text, node, rev)
905
905
906 self._cache = (node, rev, text)
906 self._cache = (node, rev, text)
907 return text
907 return text
908
908
909 def _checkhash(self, text, node, rev):
909 def _checkhash(self, text, node, rev):
910 p1, p2 = self.parents(node)
910 p1, p2 = self.parents(node)
911 if node != hash(text, p1, p2):
911 if node != hash(text, p1, p2):
912 raise RevlogError(_("integrity check failed on %s:%d")
912 raise RevlogError(_("integrity check failed on %s:%d")
913 % (self.indexfile, rev))
913 % (self.indexfile, rev))
914 return text
914 return text
915
915
916 def checkinlinesize(self, tr, fp=None):
916 def checkinlinesize(self, tr, fp=None):
917 if not self._inline or (self.start(-2) + self.length(-2)) < _maxinline:
917 if not self._inline or (self.start(-2) + self.length(-2)) < _maxinline:
918 return
918 return
919
919
920 trinfo = tr.find(self.indexfile)
920 trinfo = tr.find(self.indexfile)
921 if trinfo is None:
921 if trinfo is None:
922 raise RevlogError(_("%s not found in the transaction")
922 raise RevlogError(_("%s not found in the transaction")
923 % self.indexfile)
923 % self.indexfile)
924
924
925 trindex = trinfo[2]
925 trindex = trinfo[2]
926 dataoff = self.start(trindex)
926 dataoff = self.start(trindex)
927
927
928 tr.add(self.datafile, dataoff)
928 tr.add(self.datafile, dataoff)
929
929
930 if fp:
930 if fp:
931 fp.flush()
931 fp.flush()
932 fp.close()
932 fp.close()
933
933
934 df = self.opener(self.datafile, 'w')
934 df = self.opener(self.datafile, 'w')
935 try:
935 try:
936 for r in self:
936 for r in self:
937 df.write(self._chunkraw(r, r))
937 df.write(self._chunkraw(r, r))
938 finally:
938 finally:
939 df.close()
939 df.close()
940
940
941 fp = self.opener(self.indexfile, 'w', atomictemp=True)
941 fp = self.opener(self.indexfile, 'w', atomictemp=True)
942 self.version &= ~(REVLOGNGINLINEDATA)
942 self.version &= ~(REVLOGNGINLINEDATA)
943 self._inline = False
943 self._inline = False
944 for i in self:
944 for i in self:
945 e = self._io.packentry(self.index[i], self.node, self.version, i)
945 e = self._io.packentry(self.index[i], self.node, self.version, i)
946 fp.write(e)
946 fp.write(e)
947
947
948 # if we don't call rename, the temp file will never replace the
948 # if we don't call rename, the temp file will never replace the
949 # real index
949 # real index
950 fp.rename()
950 fp.rename()
951
951
952 tr.replace(self.indexfile, trindex * self._io.size)
952 tr.replace(self.indexfile, trindex * self._io.size)
953 self._chunkclear()
953 self._chunkclear()
954
954
955 def addrevision(self, text, transaction, link, p1, p2, cachedelta=None):
955 def addrevision(self, text, transaction, link, p1, p2, cachedelta=None):
956 """add a revision to the log
956 """add a revision to the log
957
957
958 text - the revision data to add
958 text - the revision data to add
959 transaction - the transaction object used for rollback
959 transaction - the transaction object used for rollback
960 link - the linkrev data to add
960 link - the linkrev data to add
961 p1, p2 - the parent nodeids of the revision
961 p1, p2 - the parent nodeids of the revision
962 cachedelta - an optional precomputed delta
962 cachedelta - an optional precomputed delta
963 """
963 """
964 node = hash(text, p1, p2)
964 node = hash(text, p1, p2)
965 if node in self.nodemap:
965 if node in self.nodemap:
966 return node
966 return node
967
967
968 dfh = None
968 dfh = None
969 if not self._inline:
969 if not self._inline:
970 dfh = self.opener(self.datafile, "a")
970 dfh = self.opener(self.datafile, "a")
971 ifh = self.opener(self.indexfile, "a+")
971 ifh = self.opener(self.indexfile, "a+")
972 try:
972 try:
973 return self._addrevision(node, text, transaction, link, p1, p2,
973 return self._addrevision(node, text, transaction, link, p1, p2,
974 cachedelta, ifh, dfh)
974 cachedelta, ifh, dfh)
975 finally:
975 finally:
976 if dfh:
976 if dfh:
977 dfh.close()
977 dfh.close()
978 ifh.close()
978 ifh.close()
979
979
980 def _addrevision(self, node, text, transaction, link, p1, p2,
980 def _addrevision(self, node, text, transaction, link, p1, p2,
981 cachedelta, ifh, dfh):
981 cachedelta, ifh, dfh):
982 """internal function to add revisions to the log
982 """internal function to add revisions to the log
983
983
984 see addrevision for argument descriptions.
984 see addrevision for argument descriptions.
985 invariants:
985 invariants:
986 - text is optional (can be None); if not set, cachedelta must be set.
986 - text is optional (can be None); if not set, cachedelta must be set.
987 if both are set, they must correspond to eachother.
987 if both are set, they must correspond to eachother.
988 """
988 """
989 btext = [text]
989 btext = [text]
990 def buildtext():
990 def buildtext():
991 if btext[0] is not None:
991 if btext[0] is not None:
992 return btext[0]
992 return btext[0]
993 # flush any pending writes here so we can read it in revision
993 # flush any pending writes here so we can read it in revision
994 if dfh:
994 if dfh:
995 dfh.flush()
995 dfh.flush()
996 ifh.flush()
996 ifh.flush()
997 basetext = self.revision(self.node(cachedelta[0]))
997 basetext = self.revision(self.node(cachedelta[0]))
998 btext[0] = mdiff.patch(basetext, cachedelta[1])
998 btext[0] = mdiff.patch(basetext, cachedelta[1])
999 chk = hash(btext[0], p1, p2)
999 chk = hash(btext[0], p1, p2)
1000 if chk != node:
1000 if chk != node:
1001 raise RevlogError(_("consistency error in delta"))
1001 raise RevlogError(_("consistency error in delta"))
1002 return btext[0]
1002 return btext[0]
1003
1003
1004 def builddelta(rev):
1004 def builddelta(rev):
1005 # can we use the cached delta?
1005 # can we use the cached delta?
1006 if cachedelta and cachedelta[0] == rev:
1006 if cachedelta and cachedelta[0] == rev:
1007 delta = cachedelta[1]
1007 delta = cachedelta[1]
1008 else:
1008 else:
1009 t = buildtext()
1009 t = buildtext()
1010 ptext = self.revision(self.node(rev))
1010 ptext = self.revision(self.node(rev))
1011 delta = mdiff.textdiff(ptext, t)
1011 delta = mdiff.textdiff(ptext, t)
1012 data = compress(delta)
1012 data = compress(delta)
1013 l = len(data[1]) + len(data[0])
1013 l = len(data[1]) + len(data[0])
1014 if basecache[0] == rev:
1014 if basecache[0] == rev:
1015 chainbase = basecache[1]
1015 chainbase = basecache[1]
1016 else:
1016 else:
1017 chainbase = self.chainbase(rev)
1017 chainbase = self.chainbase(rev)
1018 dist = l + offset - self.start(chainbase)
1018 dist = l + offset - self.start(chainbase)
1019 if self._generaldelta:
1019 if self._generaldelta:
1020 base = rev
1020 base = rev
1021 else:
1021 else:
1022 base = chainbase
1022 base = chainbase
1023 return dist, l, data, base, chainbase
1023 return dist, l, data, base, chainbase
1024
1024
1025 curr = len(self)
1025 curr = len(self)
1026 prev = curr - 1
1026 prev = curr - 1
1027 base = chainbase = curr
1027 base = chainbase = curr
1028 offset = self.end(prev)
1028 offset = self.end(prev)
1029 flags = 0
1029 flags = 0
1030 d = None
1030 d = None
1031 basecache = self._basecache
1031 basecache = self._basecache
1032 p1r, p2r = self.rev(p1), self.rev(p2)
1032 p1r, p2r = self.rev(p1), self.rev(p2)
1033
1033
1034 # should we try to build a delta?
1034 # should we try to build a delta?
1035 if prev != nullrev:
1035 if prev != nullrev:
1036 if self._generaldelta:
1036 if self._generaldelta:
1037 if p1r >= basecache[1]:
1037 if p1r >= basecache[1]:
1038 d = builddelta(p1r)
1038 d = builddelta(p1r)
1039 elif p2r >= basecache[1]:
1039 elif p2r >= basecache[1]:
1040 d = builddelta(p2r)
1040 d = builddelta(p2r)
1041 else:
1041 else:
1042 d = builddelta(prev)
1042 d = builddelta(prev)
1043 else:
1043 else:
1044 d = builddelta(prev)
1044 d = builddelta(prev)
1045 dist, l, data, base, chainbase = d
1045 dist, l, data, base, chainbase = d
1046
1046
1047 # full versions are inserted when the needed deltas
1047 # full versions are inserted when the needed deltas
1048 # become comparable to the uncompressed text
1048 # become comparable to the uncompressed text
1049 if text is None:
1049 if text is None:
1050 textlen = mdiff.patchedsize(self.rawsize(cachedelta[0]),
1050 textlen = mdiff.patchedsize(self.rawsize(cachedelta[0]),
1051 cachedelta[1])
1051 cachedelta[1])
1052 else:
1052 else:
1053 textlen = len(text)
1053 textlen = len(text)
1054 if d is None or dist > textlen * 2:
1054 if d is None or dist > textlen * 2:
1055 text = buildtext()
1055 text = buildtext()
1056 data = compress(text)
1056 data = compress(text)
1057 l = len(data[1]) + len(data[0])
1057 l = len(data[1]) + len(data[0])
1058 base = chainbase = curr
1058 base = chainbase = curr
1059
1059
1060 e = (offset_type(offset, flags), l, textlen,
1060 e = (offset_type(offset, flags), l, textlen,
1061 base, link, p1r, p2r, node)
1061 base, link, p1r, p2r, node)
1062 self.index.insert(-1, e)
1062 self.index.insert(-1, e)
1063 self.nodemap[node] = curr
1063 self.nodemap[node] = curr
1064
1064
1065 entry = self._io.packentry(e, self.node, self.version, curr)
1065 entry = self._io.packentry(e, self.node, self.version, curr)
1066 if not self._inline:
1066 if not self._inline:
1067 transaction.add(self.datafile, offset)
1067 transaction.add(self.datafile, offset)
1068 transaction.add(self.indexfile, curr * len(entry))
1068 transaction.add(self.indexfile, curr * len(entry))
1069 if data[0]:
1069 if data[0]:
1070 dfh.write(data[0])
1070 dfh.write(data[0])
1071 dfh.write(data[1])
1071 dfh.write(data[1])
1072 dfh.flush()
1072 dfh.flush()
1073 ifh.write(entry)
1073 ifh.write(entry)
1074 else:
1074 else:
1075 offset += curr * self._io.size
1075 offset += curr * self._io.size
1076 transaction.add(self.indexfile, offset, curr)
1076 transaction.add(self.indexfile, offset, curr)
1077 ifh.write(entry)
1077 ifh.write(entry)
1078 ifh.write(data[0])
1078 ifh.write(data[0])
1079 ifh.write(data[1])
1079 ifh.write(data[1])
1080 self.checkinlinesize(transaction, ifh)
1080 self.checkinlinesize(transaction, ifh)
1081
1081
1082 if type(text) == str: # only accept immutable objects
1082 if type(text) == str: # only accept immutable objects
1083 self._cache = (node, curr, text)
1083 self._cache = (node, curr, text)
1084 self._basecache = (curr, chainbase)
1084 self._basecache = (curr, chainbase)
1085 return node
1085 return node
1086
1086
1087 def group(self, nodelist, bundler, reorder=None):
1087 def group(self, nodelist, bundler, reorder=None):
1088 """Calculate a delta group, yielding a sequence of changegroup chunks
1088 """Calculate a delta group, yielding a sequence of changegroup chunks
1089 (strings).
1089 (strings).
1090
1090
1091 Given a list of changeset revs, return a set of deltas and
1091 Given a list of changeset revs, return a set of deltas and
1092 metadata corresponding to nodes. The first delta is
1092 metadata corresponding to nodes. The first delta is
1093 first parent(nodelist[0]) -> nodelist[0], the receiver is
1093 first parent(nodelist[0]) -> nodelist[0], the receiver is
1094 guaranteed to have this parent as it has all history before
1094 guaranteed to have this parent as it has all history before
1095 these changesets. In the case firstparent is nullrev the
1095 these changesets. In the case firstparent is nullrev the
1096 changegroup starts with a full revision.
1096 changegroup starts with a full revision.
1097 """
1097 """
1098
1098
1099 # if we don't have any revisions touched by these changesets, bail
1099 # if we don't have any revisions touched by these changesets, bail
1100 if len(nodelist) == 0:
1100 if len(nodelist) == 0:
1101 yield bundler.close()
1101 yield bundler.close()
1102 return
1102 return
1103
1103
1104 # for generaldelta revlogs, we linearize the revs; this will both be
1104 # for generaldelta revlogs, we linearize the revs; this will both be
1105 # much quicker and generate a much smaller bundle
1105 # much quicker and generate a much smaller bundle
1106 if (self._generaldelta and reorder is not False) or reorder:
1106 if (self._generaldelta and reorder is not False) or reorder:
1107 dag = dagutil.revlogdag(self)
1107 dag = dagutil.revlogdag(self)
1108 revs = set(self.rev(n) for n in nodelist)
1108 revs = set(self.rev(n) for n in nodelist)
1109 revs = dag.linearize(revs)
1109 revs = dag.linearize(revs)
1110 else:
1110 else:
1111 revs = sorted([self.rev(n) for n in nodelist])
1111 revs = sorted([self.rev(n) for n in nodelist])
1112
1112
1113 # add the parent of the first rev
1113 # add the parent of the first rev
1114 p = self.parentrevs(revs[0])[0]
1114 p = self.parentrevs(revs[0])[0]
1115 revs.insert(0, p)
1115 revs.insert(0, p)
1116
1116
1117 # build deltas
1117 # build deltas
1118 for r in xrange(len(revs) - 1):
1118 for r in xrange(len(revs) - 1):
1119 prev, curr = revs[r], revs[r + 1]
1119 prev, curr = revs[r], revs[r + 1]
1120 for c in bundler.revchunk(self, curr, prev):
1120 for c in bundler.revchunk(self, curr, prev):
1121 yield c
1121 yield c
1122
1122
1123 yield bundler.close()
1123 yield bundler.close()
1124
1124
1125 def addgroup(self, bundle, linkmapper, transaction):
1125 def addgroup(self, bundle, linkmapper, transaction):
1126 """
1126 """
1127 add a delta group
1127 add a delta group
1128
1128
1129 given a set of deltas, add them to the revision log. the
1129 given a set of deltas, add them to the revision log. the
1130 first delta is against its parent, which should be in our
1130 first delta is against its parent, which should be in our
1131 log, the rest are against the previous delta.
1131 log, the rest are against the previous delta.
1132 """
1132 """
1133
1133
1134 # track the base of the current delta log
1134 # track the base of the current delta log
1135 node = None
1135 node = None
1136
1136
1137 r = len(self)
1137 r = len(self)
1138 end = 0
1138 end = 0
1139 if r:
1139 if r:
1140 end = self.end(r - 1)
1140 end = self.end(r - 1)
1141 ifh = self.opener(self.indexfile, "a+")
1141 ifh = self.opener(self.indexfile, "a+")
1142 isize = r * self._io.size
1142 isize = r * self._io.size
1143 if self._inline:
1143 if self._inline:
1144 transaction.add(self.indexfile, end + isize, r)
1144 transaction.add(self.indexfile, end + isize, r)
1145 dfh = None
1145 dfh = None
1146 else:
1146 else:
1147 transaction.add(self.indexfile, isize, r)
1147 transaction.add(self.indexfile, isize, r)
1148 transaction.add(self.datafile, end)
1148 transaction.add(self.datafile, end)
1149 dfh = self.opener(self.datafile, "a")
1149 dfh = self.opener(self.datafile, "a")
1150
1150
1151 try:
1151 try:
1152 # loop through our set of deltas
1152 # loop through our set of deltas
1153 chain = None
1153 chain = None
1154 while True:
1154 while True:
1155 chunkdata = bundle.deltachunk(chain)
1155 chunkdata = bundle.deltachunk(chain)
1156 if not chunkdata:
1156 if not chunkdata:
1157 break
1157 break
1158 node = chunkdata['node']
1158 node = chunkdata['node']
1159 p1 = chunkdata['p1']
1159 p1 = chunkdata['p1']
1160 p2 = chunkdata['p2']
1160 p2 = chunkdata['p2']
1161 cs = chunkdata['cs']
1161 cs = chunkdata['cs']
1162 deltabase = chunkdata['deltabase']
1162 deltabase = chunkdata['deltabase']
1163 delta = chunkdata['delta']
1163 delta = chunkdata['delta']
1164
1164
1165 link = linkmapper(cs)
1165 link = linkmapper(cs)
1166 if node in self.nodemap:
1166 if node in self.nodemap:
1167 # this can happen if two branches make the same change
1167 # this can happen if two branches make the same change
1168 chain = node
1168 chain = node
1169 continue
1169 continue
1170
1170
1171 for p in (p1, p2):
1171 for p in (p1, p2):
1172 if not p in self.nodemap:
1172 if not p in self.nodemap:
1173 raise LookupError(p, self.indexfile,
1173 raise LookupError(p, self.indexfile,
1174 _('unknown parent'))
1174 _('unknown parent'))
1175
1175
1176 if deltabase not in self.nodemap:
1176 if deltabase not in self.nodemap:
1177 raise LookupError(deltabase, self.indexfile,
1177 raise LookupError(deltabase, self.indexfile,
1178 _('unknown delta base'))
1178 _('unknown delta base'))
1179
1179
1180 baserev = self.rev(deltabase)
1180 baserev = self.rev(deltabase)
1181 chain = self._addrevision(node, None, transaction, link,
1181 chain = self._addrevision(node, None, transaction, link,
1182 p1, p2, (baserev, delta), ifh, dfh)
1182 p1, p2, (baserev, delta), ifh, dfh)
1183 if not dfh and not self._inline:
1183 if not dfh and not self._inline:
1184 # addrevision switched from inline to conventional
1184 # addrevision switched from inline to conventional
1185 # reopen the index
1185 # reopen the index
1186 ifh.close()
1186 ifh.close()
1187 dfh = self.opener(self.datafile, "a")
1187 dfh = self.opener(self.datafile, "a")
1188 ifh = self.opener(self.indexfile, "a")
1188 ifh = self.opener(self.indexfile, "a")
1189 finally:
1189 finally:
1190 if dfh:
1190 if dfh:
1191 dfh.close()
1191 dfh.close()
1192 ifh.close()
1192 ifh.close()
1193
1193
1194 return node
1194 return node
1195
1195
1196 def strip(self, minlink, transaction):
1196 def strip(self, minlink, transaction):
1197 """truncate the revlog on the first revision with a linkrev >= minlink
1197 """truncate the revlog on the first revision with a linkrev >= minlink
1198
1198
1199 This function is called when we're stripping revision minlink and
1199 This function is called when we're stripping revision minlink and
1200 its descendants from the repository.
1200 its descendants from the repository.
1201
1201
1202 We have to remove all revisions with linkrev >= minlink, because
1202 We have to remove all revisions with linkrev >= minlink, because
1203 the equivalent changelog revisions will be renumbered after the
1203 the equivalent changelog revisions will be renumbered after the
1204 strip.
1204 strip.
1205
1205
1206 So we truncate the revlog on the first of these revisions, and
1206 So we truncate the revlog on the first of these revisions, and
1207 trust that the caller has saved the revisions that shouldn't be
1207 trust that the caller has saved the revisions that shouldn't be
1208 removed and that it'll readd them after this truncation.
1208 removed and that it'll readd them after this truncation.
1209 """
1209 """
1210 if len(self) == 0:
1210 if len(self) == 0:
1211 return
1211 return
1212
1212
1213 for rev in self:
1213 for rev in self:
1214 if self.index[rev][4] >= minlink:
1214 if self.index[rev][4] >= minlink:
1215 break
1215 break
1216 else:
1216 else:
1217 return
1217 return
1218
1218
1219 # first truncate the files on disk
1219 # first truncate the files on disk
1220 end = self.start(rev)
1220 end = self.start(rev)
1221 if not self._inline:
1221 if not self._inline:
1222 transaction.add(self.datafile, end)
1222 transaction.add(self.datafile, end)
1223 end = rev * self._io.size
1223 end = rev * self._io.size
1224 else:
1224 else:
1225 end += rev * self._io.size
1225 end += rev * self._io.size
1226
1226
1227 transaction.add(self.indexfile, end)
1227 transaction.add(self.indexfile, end)
1228
1228
1229 # then reset internal state in memory to forget those revisions
1229 # then reset internal state in memory to forget those revisions
1230 self._cache = None
1230 self._cache = None
1231 self._chunkclear()
1231 self._chunkclear()
1232 for x in xrange(rev, len(self)):
1232 for x in xrange(rev, len(self)):
1233 del self.nodemap[self.node(x)]
1233 del self.nodemap[self.node(x)]
1234
1234
1235 del self.index[rev:-1]
1235 del self.index[rev:-1]
1236
1236
1237 def checksize(self):
1237 def checksize(self):
1238 expected = 0
1238 expected = 0
1239 if len(self):
1239 if len(self):
1240 expected = max(0, self.end(len(self) - 1))
1240 expected = max(0, self.end(len(self) - 1))
1241
1241
1242 try:
1242 try:
1243 f = self.opener(self.datafile)
1243 f = self.opener(self.datafile)
1244 f.seek(0, 2)
1244 f.seek(0, 2)
1245 actual = f.tell()
1245 actual = f.tell()
1246 f.close()
1246 f.close()
1247 dd = actual - expected
1247 dd = actual - expected
1248 except IOError, inst:
1248 except IOError, inst:
1249 if inst.errno != errno.ENOENT:
1249 if inst.errno != errno.ENOENT:
1250 raise
1250 raise
1251 dd = 0
1251 dd = 0
1252
1252
1253 try:
1253 try:
1254 f = self.opener(self.indexfile)
1254 f = self.opener(self.indexfile)
1255 f.seek(0, 2)
1255 f.seek(0, 2)
1256 actual = f.tell()
1256 actual = f.tell()
1257 f.close()
1257 f.close()
1258 s = self._io.size
1258 s = self._io.size
1259 i = max(0, actual // s)
1259 i = max(0, actual // s)
1260 di = actual - (i * s)
1260 di = actual - (i * s)
1261 if self._inline:
1261 if self._inline:
1262 databytes = 0
1262 databytes = 0
1263 for r in self:
1263 for r in self:
1264 databytes += max(0, self.length(r))
1264 databytes += max(0, self.length(r))
1265 dd = 0
1265 dd = 0
1266 di = actual - len(self) * s - databytes
1266 di = actual - len(self) * s - databytes
1267 except IOError, inst:
1267 except IOError, inst:
1268 if inst.errno != errno.ENOENT:
1268 if inst.errno != errno.ENOENT:
1269 raise
1269 raise
1270 di = 0
1270 di = 0
1271
1271
1272 return (dd, di)
1272 return (dd, di)
1273
1273
1274 def files(self):
1274 def files(self):
1275 res = [self.indexfile]
1275 res = [self.indexfile]
1276 if not self._inline:
1276 if not self._inline:
1277 res.append(self.datafile)
1277 res.append(self.datafile)
1278 return res
1278 return res
@@ -1,453 +1,453 b''
1 # Copyright (C) 2004, 2005 Canonical Ltd
1 # Copyright (C) 2004, 2005 Canonical Ltd
2 #
2 #
3 # This program is free software; you can redistribute it and/or modify
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
6 # (at your option) any later version.
7 #
7 #
8 # This program is distributed in the hope that it will be useful,
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
11 # GNU General Public License for more details.
12 #
12 #
13 # You should have received a copy of the GNU General Public License
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
16
17 # mbp: "you know that thing where cvs gives you conflict markers?"
17 # mbp: "you know that thing where cvs gives you conflict markers?"
18 # s: "i hate that."
18 # s: "i hate that."
19
19
20 from i18n import _
20 from i18n import _
21 import scmutil, util, mdiff
21 import scmutil, util, mdiff
22 import sys, os
22 import sys, os
23
23
24 class CantReprocessAndShowBase(Exception):
24 class CantReprocessAndShowBase(Exception):
25 pass
25 pass
26
26
27 def intersect(ra, rb):
27 def intersect(ra, rb):
28 """Given two ranges return the range where they intersect or None.
28 """Given two ranges return the range where they intersect or None.
29
29
30 >>> intersect((0, 10), (0, 6))
30 >>> intersect((0, 10), (0, 6))
31 (0, 6)
31 (0, 6)
32 >>> intersect((0, 10), (5, 15))
32 >>> intersect((0, 10), (5, 15))
33 (5, 10)
33 (5, 10)
34 >>> intersect((0, 10), (10, 15))
34 >>> intersect((0, 10), (10, 15))
35 >>> intersect((0, 9), (10, 15))
35 >>> intersect((0, 9), (10, 15))
36 >>> intersect((0, 9), (7, 15))
36 >>> intersect((0, 9), (7, 15))
37 (7, 9)
37 (7, 9)
38 """
38 """
39 assert ra[0] <= ra[1]
39 assert ra[0] <= ra[1]
40 assert rb[0] <= rb[1]
40 assert rb[0] <= rb[1]
41
41
42 sa = max(ra[0], rb[0])
42 sa = max(ra[0], rb[0])
43 sb = min(ra[1], rb[1])
43 sb = min(ra[1], rb[1])
44 if sa < sb:
44 if sa < sb:
45 return sa, sb
45 return sa, sb
46 else:
46 else:
47 return None
47 return None
48
48
49 def compare_range(a, astart, aend, b, bstart, bend):
49 def compare_range(a, astart, aend, b, bstart, bend):
50 """Compare a[astart:aend] == b[bstart:bend], without slicing.
50 """Compare a[astart:aend] == b[bstart:bend], without slicing.
51 """
51 """
52 if (aend - astart) != (bend - bstart):
52 if (aend - astart) != (bend - bstart):
53 return False
53 return False
54 for ia, ib in zip(xrange(astart, aend), xrange(bstart, bend)):
54 for ia, ib in zip(xrange(astart, aend), xrange(bstart, bend)):
55 if a[ia] != b[ib]:
55 if a[ia] != b[ib]:
56 return False
56 return False
57 else:
57 else:
58 return True
58 return True
59
59
60 class Merge3Text(object):
60 class Merge3Text(object):
61 """3-way merge of texts.
61 """3-way merge of texts.
62
62
63 Given strings BASE, OTHER, THIS, tries to produce a combined text
63 Given strings BASE, OTHER, THIS, tries to produce a combined text
64 incorporating the changes from both BASE->OTHER and BASE->THIS."""
64 incorporating the changes from both BASE->OTHER and BASE->THIS."""
65 def __init__(self, basetext, atext, btext, base=None, a=None, b=None):
65 def __init__(self, basetext, atext, btext, base=None, a=None, b=None):
66 self.basetext = basetext
66 self.basetext = basetext
67 self.atext = atext
67 self.atext = atext
68 self.btext = btext
68 self.btext = btext
69 if base is None:
69 if base is None:
70 base = mdiff.splitnewlines(basetext)
70 base = mdiff.splitnewlines(basetext)
71 if a is None:
71 if a is None:
72 a = mdiff.splitnewlines(atext)
72 a = mdiff.splitnewlines(atext)
73 if b is None:
73 if b is None:
74 b = mdiff.splitnewlines(btext)
74 b = mdiff.splitnewlines(btext)
75 self.base = base
75 self.base = base
76 self.a = a
76 self.a = a
77 self.b = b
77 self.b = b
78
78
79 def merge_lines(self,
79 def merge_lines(self,
80 name_a=None,
80 name_a=None,
81 name_b=None,
81 name_b=None,
82 name_base=None,
82 name_base=None,
83 start_marker='<<<<<<<',
83 start_marker='<<<<<<<',
84 mid_marker='=======',
84 mid_marker='=======',
85 end_marker='>>>>>>>',
85 end_marker='>>>>>>>',
86 base_marker=None,
86 base_marker=None,
87 reprocess=False):
87 reprocess=False):
88 """Return merge in cvs-like form.
88 """Return merge in cvs-like form.
89 """
89 """
90 self.conflicts = False
90 self.conflicts = False
91 newline = '\n'
91 newline = '\n'
92 if len(self.a) > 0:
92 if len(self.a) > 0:
93 if self.a[0].endswith('\r\n'):
93 if self.a[0].endswith('\r\n'):
94 newline = '\r\n'
94 newline = '\r\n'
95 elif self.a[0].endswith('\r'):
95 elif self.a[0].endswith('\r'):
96 newline = '\r'
96 newline = '\r'
97 if base_marker and reprocess:
97 if base_marker and reprocess:
98 raise CantReprocessAndShowBase()
98 raise CantReprocessAndShowBase()
99 if name_a:
99 if name_a:
100 start_marker = start_marker + ' ' + name_a
100 start_marker = start_marker + ' ' + name_a
101 if name_b:
101 if name_b:
102 end_marker = end_marker + ' ' + name_b
102 end_marker = end_marker + ' ' + name_b
103 if name_base and base_marker:
103 if name_base and base_marker:
104 base_marker = base_marker + ' ' + name_base
104 base_marker = base_marker + ' ' + name_base
105 merge_regions = self.merge_regions()
105 merge_regions = self.merge_regions()
106 if reprocess is True:
106 if reprocess is True:
107 merge_regions = self.reprocess_merge_regions(merge_regions)
107 merge_regions = self.reprocess_merge_regions(merge_regions)
108 for t in merge_regions:
108 for t in merge_regions:
109 what = t[0]
109 what = t[0]
110 if what == 'unchanged':
110 if what == 'unchanged':
111 for i in range(t[1], t[2]):
111 for i in range(t[1], t[2]):
112 yield self.base[i]
112 yield self.base[i]
113 elif what == 'a' or what == 'same':
113 elif what == 'a' or what == 'same':
114 for i in range(t[1], t[2]):
114 for i in range(t[1], t[2]):
115 yield self.a[i]
115 yield self.a[i]
116 elif what == 'b':
116 elif what == 'b':
117 for i in range(t[1], t[2]):
117 for i in range(t[1], t[2]):
118 yield self.b[i]
118 yield self.b[i]
119 elif what == 'conflict':
119 elif what == 'conflict':
120 self.conflicts = True
120 self.conflicts = True
121 yield start_marker + newline
121 yield start_marker + newline
122 for i in range(t[3], t[4]):
122 for i in range(t[3], t[4]):
123 yield self.a[i]
123 yield self.a[i]
124 if base_marker is not None:
124 if base_marker is not None:
125 yield base_marker + newline
125 yield base_marker + newline
126 for i in range(t[1], t[2]):
126 for i in range(t[1], t[2]):
127 yield self.base[i]
127 yield self.base[i]
128 yield mid_marker + newline
128 yield mid_marker + newline
129 for i in range(t[5], t[6]):
129 for i in range(t[5], t[6]):
130 yield self.b[i]
130 yield self.b[i]
131 yield end_marker + newline
131 yield end_marker + newline
132 else:
132 else:
133 raise ValueError(what)
133 raise ValueError(what)
134
134
135 def merge_annotated(self):
135 def merge_annotated(self):
136 """Return merge with conflicts, showing origin of lines.
136 """Return merge with conflicts, showing origin of lines.
137
137
138 Most useful for debugging merge.
138 Most useful for debugging merge.
139 """
139 """
140 for t in self.merge_regions():
140 for t in self.merge_regions():
141 what = t[0]
141 what = t[0]
142 if what == 'unchanged':
142 if what == 'unchanged':
143 for i in range(t[1], t[2]):
143 for i in range(t[1], t[2]):
144 yield 'u | ' + self.base[i]
144 yield 'u | ' + self.base[i]
145 elif what == 'a' or what == 'same':
145 elif what == 'a' or what == 'same':
146 for i in range(t[1], t[2]):
146 for i in range(t[1], t[2]):
147 yield what[0] + ' | ' + self.a[i]
147 yield what[0] + ' | ' + self.a[i]
148 elif what == 'b':
148 elif what == 'b':
149 for i in range(t[1], t[2]):
149 for i in range(t[1], t[2]):
150 yield 'b | ' + self.b[i]
150 yield 'b | ' + self.b[i]
151 elif what == 'conflict':
151 elif what == 'conflict':
152 yield '<<<<\n'
152 yield '<<<<\n'
153 for i in range(t[3], t[4]):
153 for i in range(t[3], t[4]):
154 yield 'A | ' + self.a[i]
154 yield 'A | ' + self.a[i]
155 yield '----\n'
155 yield '----\n'
156 for i in range(t[5], t[6]):
156 for i in range(t[5], t[6]):
157 yield 'B | ' + self.b[i]
157 yield 'B | ' + self.b[i]
158 yield '>>>>\n'
158 yield '>>>>\n'
159 else:
159 else:
160 raise ValueError(what)
160 raise ValueError(what)
161
161
162 def merge_groups(self):
162 def merge_groups(self):
163 """Yield sequence of line groups. Each one is a tuple:
163 """Yield sequence of line groups. Each one is a tuple:
164
164
165 'unchanged', lines
165 'unchanged', lines
166 Lines unchanged from base
166 Lines unchanged from base
167
167
168 'a', lines
168 'a', lines
169 Lines taken from a
169 Lines taken from a
170
170
171 'same', lines
171 'same', lines
172 Lines taken from a (and equal to b)
172 Lines taken from a (and equal to b)
173
173
174 'b', lines
174 'b', lines
175 Lines taken from b
175 Lines taken from b
176
176
177 'conflict', base_lines, a_lines, b_lines
177 'conflict', base_lines, a_lines, b_lines
178 Lines from base were changed to either a or b and conflict.
178 Lines from base were changed to either a or b and conflict.
179 """
179 """
180 for t in self.merge_regions():
180 for t in self.merge_regions():
181 what = t[0]
181 what = t[0]
182 if what == 'unchanged':
182 if what == 'unchanged':
183 yield what, self.base[t[1]:t[2]]
183 yield what, self.base[t[1]:t[2]]
184 elif what == 'a' or what == 'same':
184 elif what == 'a' or what == 'same':
185 yield what, self.a[t[1]:t[2]]
185 yield what, self.a[t[1]:t[2]]
186 elif what == 'b':
186 elif what == 'b':
187 yield what, self.b[t[1]:t[2]]
187 yield what, self.b[t[1]:t[2]]
188 elif what == 'conflict':
188 elif what == 'conflict':
189 yield (what,
189 yield (what,
190 self.base[t[1]:t[2]],
190 self.base[t[1]:t[2]],
191 self.a[t[3]:t[4]],
191 self.a[t[3]:t[4]],
192 self.b[t[5]:t[6]])
192 self.b[t[5]:t[6]])
193 else:
193 else:
194 raise ValueError(what)
194 raise ValueError(what)
195
195
196 def merge_regions(self):
196 def merge_regions(self):
197 """Return sequences of matching and conflicting regions.
197 """Return sequences of matching and conflicting regions.
198
198
199 This returns tuples, where the first value says what kind we
199 This returns tuples, where the first value says what kind we
200 have:
200 have:
201
201
202 'unchanged', start, end
202 'unchanged', start, end
203 Take a region of base[start:end]
203 Take a region of base[start:end]
204
204
205 'same', astart, aend
205 'same', astart, aend
206 b and a are different from base but give the same result
206 b and a are different from base but give the same result
207
207
208 'a', start, end
208 'a', start, end
209 Non-clashing insertion from a[start:end]
209 Non-clashing insertion from a[start:end]
210
210
211 Method is as follows:
211 Method is as follows:
212
212
213 The two sequences align only on regions which match the base
213 The two sequences align only on regions which match the base
214 and both descendents. These are found by doing a two-way diff
214 and both descendants. These are found by doing a two-way diff
215 of each one against the base, and then finding the
215 of each one against the base, and then finding the
216 intersections between those regions. These "sync regions"
216 intersections between those regions. These "sync regions"
217 are by definition unchanged in both and easily dealt with.
217 are by definition unchanged in both and easily dealt with.
218
218
219 The regions in between can be in any of three cases:
219 The regions in between can be in any of three cases:
220 conflicted, or changed on only one side.
220 conflicted, or changed on only one side.
221 """
221 """
222
222
223 # section a[0:ia] has been disposed of, etc
223 # section a[0:ia] has been disposed of, etc
224 iz = ia = ib = 0
224 iz = ia = ib = 0
225
225
226 for zmatch, zend, amatch, aend, bmatch, bend in self.find_sync_regions():
226 for zmatch, zend, amatch, aend, bmatch, bend in self.find_sync_regions():
227 #print 'match base [%d:%d]' % (zmatch, zend)
227 #print 'match base [%d:%d]' % (zmatch, zend)
228
228
229 matchlen = zend - zmatch
229 matchlen = zend - zmatch
230 assert matchlen >= 0
230 assert matchlen >= 0
231 assert matchlen == (aend - amatch)
231 assert matchlen == (aend - amatch)
232 assert matchlen == (bend - bmatch)
232 assert matchlen == (bend - bmatch)
233
233
234 len_a = amatch - ia
234 len_a = amatch - ia
235 len_b = bmatch - ib
235 len_b = bmatch - ib
236 len_base = zmatch - iz
236 len_base = zmatch - iz
237 assert len_a >= 0
237 assert len_a >= 0
238 assert len_b >= 0
238 assert len_b >= 0
239 assert len_base >= 0
239 assert len_base >= 0
240
240
241 #print 'unmatched a=%d, b=%d' % (len_a, len_b)
241 #print 'unmatched a=%d, b=%d' % (len_a, len_b)
242
242
243 if len_a or len_b:
243 if len_a or len_b:
244 # try to avoid actually slicing the lists
244 # try to avoid actually slicing the lists
245 equal_a = compare_range(self.a, ia, amatch,
245 equal_a = compare_range(self.a, ia, amatch,
246 self.base, iz, zmatch)
246 self.base, iz, zmatch)
247 equal_b = compare_range(self.b, ib, bmatch,
247 equal_b = compare_range(self.b, ib, bmatch,
248 self.base, iz, zmatch)
248 self.base, iz, zmatch)
249 same = compare_range(self.a, ia, amatch,
249 same = compare_range(self.a, ia, amatch,
250 self.b, ib, bmatch)
250 self.b, ib, bmatch)
251
251
252 if same:
252 if same:
253 yield 'same', ia, amatch
253 yield 'same', ia, amatch
254 elif equal_a and not equal_b:
254 elif equal_a and not equal_b:
255 yield 'b', ib, bmatch
255 yield 'b', ib, bmatch
256 elif equal_b and not equal_a:
256 elif equal_b and not equal_a:
257 yield 'a', ia, amatch
257 yield 'a', ia, amatch
258 elif not equal_a and not equal_b:
258 elif not equal_a and not equal_b:
259 yield 'conflict', iz, zmatch, ia, amatch, ib, bmatch
259 yield 'conflict', iz, zmatch, ia, amatch, ib, bmatch
260 else:
260 else:
261 raise AssertionError("can't handle a=b=base but unmatched")
261 raise AssertionError("can't handle a=b=base but unmatched")
262
262
263 ia = amatch
263 ia = amatch
264 ib = bmatch
264 ib = bmatch
265 iz = zmatch
265 iz = zmatch
266
266
267 # if the same part of the base was deleted on both sides
267 # if the same part of the base was deleted on both sides
268 # that's OK, we can just skip it.
268 # that's OK, we can just skip it.
269
269
270
270
271 if matchlen > 0:
271 if matchlen > 0:
272 assert ia == amatch
272 assert ia == amatch
273 assert ib == bmatch
273 assert ib == bmatch
274 assert iz == zmatch
274 assert iz == zmatch
275
275
276 yield 'unchanged', zmatch, zend
276 yield 'unchanged', zmatch, zend
277 iz = zend
277 iz = zend
278 ia = aend
278 ia = aend
279 ib = bend
279 ib = bend
280
280
281 def reprocess_merge_regions(self, merge_regions):
281 def reprocess_merge_regions(self, merge_regions):
282 """Where there are conflict regions, remove the agreed lines.
282 """Where there are conflict regions, remove the agreed lines.
283
283
284 Lines where both A and B have made the same changes are
284 Lines where both A and B have made the same changes are
285 eliminated.
285 eliminated.
286 """
286 """
287 for region in merge_regions:
287 for region in merge_regions:
288 if region[0] != "conflict":
288 if region[0] != "conflict":
289 yield region
289 yield region
290 continue
290 continue
291 type, iz, zmatch, ia, amatch, ib, bmatch = region
291 type, iz, zmatch, ia, amatch, ib, bmatch = region
292 a_region = self.a[ia:amatch]
292 a_region = self.a[ia:amatch]
293 b_region = self.b[ib:bmatch]
293 b_region = self.b[ib:bmatch]
294 matches = mdiff.get_matching_blocks(''.join(a_region),
294 matches = mdiff.get_matching_blocks(''.join(a_region),
295 ''.join(b_region))
295 ''.join(b_region))
296 next_a = ia
296 next_a = ia
297 next_b = ib
297 next_b = ib
298 for region_ia, region_ib, region_len in matches[:-1]:
298 for region_ia, region_ib, region_len in matches[:-1]:
299 region_ia += ia
299 region_ia += ia
300 region_ib += ib
300 region_ib += ib
301 reg = self.mismatch_region(next_a, region_ia, next_b,
301 reg = self.mismatch_region(next_a, region_ia, next_b,
302 region_ib)
302 region_ib)
303 if reg is not None:
303 if reg is not None:
304 yield reg
304 yield reg
305 yield 'same', region_ia, region_len + region_ia
305 yield 'same', region_ia, region_len + region_ia
306 next_a = region_ia + region_len
306 next_a = region_ia + region_len
307 next_b = region_ib + region_len
307 next_b = region_ib + region_len
308 reg = self.mismatch_region(next_a, amatch, next_b, bmatch)
308 reg = self.mismatch_region(next_a, amatch, next_b, bmatch)
309 if reg is not None:
309 if reg is not None:
310 yield reg
310 yield reg
311
311
312 def mismatch_region(next_a, region_ia, next_b, region_ib):
312 def mismatch_region(next_a, region_ia, next_b, region_ib):
313 if next_a < region_ia or next_b < region_ib:
313 if next_a < region_ia or next_b < region_ib:
314 return 'conflict', None, None, next_a, region_ia, next_b, region_ib
314 return 'conflict', None, None, next_a, region_ia, next_b, region_ib
315 mismatch_region = staticmethod(mismatch_region)
315 mismatch_region = staticmethod(mismatch_region)
316
316
317 def find_sync_regions(self):
317 def find_sync_regions(self):
318 """Return a list of sync regions, where both descendents match the base.
318 """Return a list of sync regions, where both descendants match the base.
319
319
320 Generates a list of (base1, base2, a1, a2, b1, b2). There is
320 Generates a list of (base1, base2, a1, a2, b1, b2). There is
321 always a zero-length sync region at the end of all the files.
321 always a zero-length sync region at the end of all the files.
322 """
322 """
323
323
324 ia = ib = 0
324 ia = ib = 0
325 amatches = mdiff.get_matching_blocks(self.basetext, self.atext)
325 amatches = mdiff.get_matching_blocks(self.basetext, self.atext)
326 bmatches = mdiff.get_matching_blocks(self.basetext, self.btext)
326 bmatches = mdiff.get_matching_blocks(self.basetext, self.btext)
327 len_a = len(amatches)
327 len_a = len(amatches)
328 len_b = len(bmatches)
328 len_b = len(bmatches)
329
329
330 sl = []
330 sl = []
331
331
332 while ia < len_a and ib < len_b:
332 while ia < len_a and ib < len_b:
333 abase, amatch, alen = amatches[ia]
333 abase, amatch, alen = amatches[ia]
334 bbase, bmatch, blen = bmatches[ib]
334 bbase, bmatch, blen = bmatches[ib]
335
335
336 # there is an unconflicted block at i; how long does it
336 # there is an unconflicted block at i; how long does it
337 # extend? until whichever one ends earlier.
337 # extend? until whichever one ends earlier.
338 i = intersect((abase, abase + alen), (bbase, bbase + blen))
338 i = intersect((abase, abase + alen), (bbase, bbase + blen))
339 if i:
339 if i:
340 intbase = i[0]
340 intbase = i[0]
341 intend = i[1]
341 intend = i[1]
342 intlen = intend - intbase
342 intlen = intend - intbase
343
343
344 # found a match of base[i[0], i[1]]; this may be less than
344 # found a match of base[i[0], i[1]]; this may be less than
345 # the region that matches in either one
345 # the region that matches in either one
346 assert intlen <= alen
346 assert intlen <= alen
347 assert intlen <= blen
347 assert intlen <= blen
348 assert abase <= intbase
348 assert abase <= intbase
349 assert bbase <= intbase
349 assert bbase <= intbase
350
350
351 asub = amatch + (intbase - abase)
351 asub = amatch + (intbase - abase)
352 bsub = bmatch + (intbase - bbase)
352 bsub = bmatch + (intbase - bbase)
353 aend = asub + intlen
353 aend = asub + intlen
354 bend = bsub + intlen
354 bend = bsub + intlen
355
355
356 assert self.base[intbase:intend] == self.a[asub:aend], \
356 assert self.base[intbase:intend] == self.a[asub:aend], \
357 (self.base[intbase:intend], self.a[asub:aend])
357 (self.base[intbase:intend], self.a[asub:aend])
358
358
359 assert self.base[intbase:intend] == self.b[bsub:bend]
359 assert self.base[intbase:intend] == self.b[bsub:bend]
360
360
361 sl.append((intbase, intend,
361 sl.append((intbase, intend,
362 asub, aend,
362 asub, aend,
363 bsub, bend))
363 bsub, bend))
364
364
365 # advance whichever one ends first in the base text
365 # advance whichever one ends first in the base text
366 if (abase + alen) < (bbase + blen):
366 if (abase + alen) < (bbase + blen):
367 ia += 1
367 ia += 1
368 else:
368 else:
369 ib += 1
369 ib += 1
370
370
371 intbase = len(self.base)
371 intbase = len(self.base)
372 abase = len(self.a)
372 abase = len(self.a)
373 bbase = len(self.b)
373 bbase = len(self.b)
374 sl.append((intbase, intbase, abase, abase, bbase, bbase))
374 sl.append((intbase, intbase, abase, abase, bbase, bbase))
375
375
376 return sl
376 return sl
377
377
378 def find_unconflicted(self):
378 def find_unconflicted(self):
379 """Return a list of ranges in base that are not conflicted."""
379 """Return a list of ranges in base that are not conflicted."""
380 am = mdiff.get_matching_blocks(self.basetext, self.atext)
380 am = mdiff.get_matching_blocks(self.basetext, self.atext)
381 bm = mdiff.get_matching_blocks(self.basetext, self.btext)
381 bm = mdiff.get_matching_blocks(self.basetext, self.btext)
382
382
383 unc = []
383 unc = []
384
384
385 while am and bm:
385 while am and bm:
386 # there is an unconflicted block at i; how long does it
386 # there is an unconflicted block at i; how long does it
387 # extend? until whichever one ends earlier.
387 # extend? until whichever one ends earlier.
388 a1 = am[0][0]
388 a1 = am[0][0]
389 a2 = a1 + am[0][2]
389 a2 = a1 + am[0][2]
390 b1 = bm[0][0]
390 b1 = bm[0][0]
391 b2 = b1 + bm[0][2]
391 b2 = b1 + bm[0][2]
392 i = intersect((a1, a2), (b1, b2))
392 i = intersect((a1, a2), (b1, b2))
393 if i:
393 if i:
394 unc.append(i)
394 unc.append(i)
395
395
396 if a2 < b2:
396 if a2 < b2:
397 del am[0]
397 del am[0]
398 else:
398 else:
399 del bm[0]
399 del bm[0]
400
400
401 return unc
401 return unc
402
402
403 def simplemerge(ui, local, base, other, **opts):
403 def simplemerge(ui, local, base, other, **opts):
404 def readfile(filename):
404 def readfile(filename):
405 f = open(filename, "rb")
405 f = open(filename, "rb")
406 text = f.read()
406 text = f.read()
407 f.close()
407 f.close()
408 if util.binary(text):
408 if util.binary(text):
409 msg = _("%s looks like a binary file.") % filename
409 msg = _("%s looks like a binary file.") % filename
410 if not opts.get('quiet'):
410 if not opts.get('quiet'):
411 ui.warn(_('warning: %s\n') % msg)
411 ui.warn(_('warning: %s\n') % msg)
412 if not opts.get('text'):
412 if not opts.get('text'):
413 raise util.Abort(msg)
413 raise util.Abort(msg)
414 return text
414 return text
415
415
416 name_a = local
416 name_a = local
417 name_b = other
417 name_b = other
418 labels = opts.get('label', [])
418 labels = opts.get('label', [])
419 if labels:
419 if labels:
420 name_a = labels.pop(0)
420 name_a = labels.pop(0)
421 if labels:
421 if labels:
422 name_b = labels.pop(0)
422 name_b = labels.pop(0)
423 if labels:
423 if labels:
424 raise util.Abort(_("can only specify two labels."))
424 raise util.Abort(_("can only specify two labels."))
425
425
426 try:
426 try:
427 localtext = readfile(local)
427 localtext = readfile(local)
428 basetext = readfile(base)
428 basetext = readfile(base)
429 othertext = readfile(other)
429 othertext = readfile(other)
430 except util.Abort:
430 except util.Abort:
431 return 1
431 return 1
432
432
433 local = os.path.realpath(local)
433 local = os.path.realpath(local)
434 if not opts.get('print'):
434 if not opts.get('print'):
435 opener = scmutil.opener(os.path.dirname(local))
435 opener = scmutil.opener(os.path.dirname(local))
436 out = opener(os.path.basename(local), "w", atomictemp=True)
436 out = opener(os.path.basename(local), "w", atomictemp=True)
437 else:
437 else:
438 out = sys.stdout
438 out = sys.stdout
439
439
440 reprocess = not opts.get('no_minimal')
440 reprocess = not opts.get('no_minimal')
441
441
442 m3 = Merge3Text(basetext, localtext, othertext)
442 m3 = Merge3Text(basetext, localtext, othertext)
443 for line in m3.merge_lines(name_a=name_a, name_b=name_b,
443 for line in m3.merge_lines(name_a=name_a, name_b=name_b,
444 reprocess=reprocess):
444 reprocess=reprocess):
445 out.write(line)
445 out.write(line)
446
446
447 if not opts.get('print'):
447 if not opts.get('print'):
448 out.rename()
448 out.rename()
449
449
450 if m3.conflicts:
450 if m3.conflicts:
451 if not opts.get('quiet'):
451 if not opts.get('quiet'):
452 ui.warn(_("warning: conflicts during merge.\n"))
452 ui.warn(_("warning: conflicts during merge.\n"))
453 return 1
453 return 1
General Comments 0
You need to be logged in to leave comments. Login now