author | Matt Mackall <mpm@selenic.com> |
Thu, 29 Sep 2011 17:04:57 -0500 | |
changeset 15170 | c1a4a3220711 |
parent 15169 | aa262fff87ac |
child 15224 | 7c604d8c7e83 |
permissions | -rw-r--r-- |
15168 | 1 |
# Copyright 2009-2010 Gregory P. Ward |
2 |
# Copyright 2009-2010 Intelerad Medical Systems Incorporated |
|
3 |
# Copyright 2010-2011 Fog Creek Software |
|
4 |
# Copyright 2010-2011 Unity Technologies |
|
5 |
# |
|
6 |
# This software may be used and distributed according to the terms of the |
|
7 |
# GNU General Public License version 2 or any later version. |
|
8 |
||
9 |
'''Overridden Mercurial commands and functions for the largefiles extension''' |
|
10 |
||
11 |
import os |
|
12 |
import copy |
|
13 |
||
14 |
from mercurial import hg, commands, util, cmdutil, match as match_, node, \ |
|
15 |
archival, error, merge |
|
16 |
from mercurial.i18n import _ |
|
17 |
from mercurial.node import hex |
|
18 |
from hgext import rebase |
|
19 |
||
20 |
try: |
|
21 |
from mercurial import scmutil |
|
22 |
except ImportError: |
|
23 |
pass |
|
24 |
||
25 |
import lfutil |
|
26 |
import lfcommands |
|
27 |
||
28 |
def installnormalfilesmatchfn(manifest): |
|
29 |
'''overrides scmutil.match so that the matcher it returns will ignore all |
|
30 |
largefiles''' |
|
31 |
oldmatch = None # for the closure |
|
32 |
def override_match(repo, pats=[], opts={}, globbed=False, |
|
33 |
default='relpath'): |
|
34 |
match = oldmatch(repo, pats, opts, globbed, default) |
|
35 |
m = copy.copy(match) |
|
36 |
notlfile = lambda f: not (lfutil.isstandin(f) or lfutil.standin(f) in |
|
37 |
manifest) |
|
38 |
m._files = filter(notlfile, m._files) |
|
39 |
m._fmap = set(m._files) |
|
40 |
orig_matchfn = m.matchfn |
|
41 |
m.matchfn = lambda f: notlfile(f) and orig_matchfn(f) or None |
|
42 |
return m |
|
43 |
oldmatch = installmatchfn(override_match) |
|
44 |
||
45 |
def installmatchfn(f): |
|
46 |
try: |
|
47 |
# Mercurial >= 1.9 |
|
48 |
oldmatch = scmutil.match |
|
49 |
except ImportError: |
|
50 |
# Mercurial <= 1.8 |
|
51 |
oldmatch = cmdutil.match |
|
52 |
setattr(f, 'oldmatch', oldmatch) |
|
53 |
try: |
|
54 |
# Mercurial >= 1.9 |
|
55 |
scmutil.match = f |
|
56 |
except ImportError: |
|
57 |
# Mercurial <= 1.8 |
|
58 |
cmdutil.match = f |
|
59 |
return oldmatch |
|
60 |
||
61 |
def restorematchfn(): |
|
62 |
'''restores scmutil.match to what it was before installnormalfilesmatchfn |
|
63 |
was called. no-op if scmutil.match is its original function. |
|
64 |
||
65 |
Note that n calls to installnormalfilesmatchfn will require n calls to |
|
66 |
restore matchfn to reverse''' |
|
67 |
try: |
|
68 |
# Mercurial >= 1.9 |
|
69 |
scmutil.match = getattr(scmutil.match, 'oldmatch', scmutil.match) |
|
70 |
except ImportError: |
|
71 |
# Mercurial <= 1.8 |
|
72 |
cmdutil.match = getattr(cmdutil.match, 'oldmatch', cmdutil.match) |
|
73 |
||
74 |
# -- Wrappers: modify existing commands -------------------------------- |
|
75 |
||
76 |
# Add works by going through the files that the user wanted to add |
|
77 |
# and checking if they should be added as lfiles. Then making a new |
|
78 |
# matcher which matches only the normal files and running the original |
|
79 |
# version of add. |
|
80 |
def override_add(orig, ui, repo, *pats, **opts): |
|
81 |
large = opts.pop('large', None) |
|
82 |
||
83 |
lfsize = opts.pop('lfsize', None) |
|
84 |
if not lfsize and lfutil.islfilesrepo(repo): |
|
85 |
lfsize = ui.config(lfutil.longname, 'size', default='10') |
|
86 |
if lfsize: |
|
87 |
try: |
|
88 |
lfsize = int(lfsize) |
|
89 |
except ValueError: |
|
15170
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15169
diff
changeset
|
90 |
raise util.Abort(_('largefiles: size must be an integer, was %s\n') |
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15169
diff
changeset
|
91 |
% lfsize) |
15168 | 92 |
|
93 |
lfmatcher = None |
|
94 |
if os.path.exists(repo.wjoin(lfutil.shortname)): |
|
95 |
lfpats = ui.config(lfutil.longname, 'patterns', default=()) |
|
96 |
if lfpats: |
|
97 |
lfpats = lfpats.split(' ') |
|
98 |
lfmatcher = match_.match(repo.root, '', list(lfpats)) |
|
99 |
||
100 |
lfnames = [] |
|
101 |
try: |
|
102 |
# Mercurial >= 1.9 |
|
103 |
m = scmutil.match(repo[None], pats, opts) |
|
104 |
except ImportError: |
|
105 |
# Mercurial <= 1.8 |
|
106 |
m = cmdutil.match(repo, pats, opts) |
|
107 |
m.bad = lambda x, y: None |
|
108 |
wctx = repo[None] |
|
109 |
for f in repo.walk(m): |
|
110 |
exact = m.exact(f) |
|
111 |
lfile = lfutil.standin(f) in wctx |
|
112 |
nfile = f in wctx |
|
113 |
exists = lfile or nfile |
|
114 |
||
115 |
# Don't warn the user when they attempt to add a normal tracked file. |
|
116 |
# The normal add code will do that for us. |
|
117 |
if exact and exists: |
|
118 |
if lfile: |
|
119 |
ui.warn(_('%s already a largefile\n') % f) |
|
120 |
continue |
|
121 |
||
122 |
if exact or not exists: |
|
123 |
if large or (lfsize and os.path.getsize(repo.wjoin(f)) >= \ |
|
124 |
lfsize * 1024 * 1024) or (lfmatcher and lfmatcher(f)): |
|
125 |
lfnames.append(f) |
|
126 |
if ui.verbose or not exact: |
|
127 |
ui.status(_('adding %s as a largefile\n') % m.rel(f)) |
|
128 |
||
129 |
bad = [] |
|
130 |
standins = [] |
|
131 |
||
132 |
# Need to lock otherwise there could be a race condition inbetween when |
|
133 |
# standins are created and added to the repo |
|
134 |
wlock = repo.wlock() |
|
135 |
try: |
|
136 |
if not opts.get('dry_run'): |
|
137 |
lfdirstate = lfutil.openlfdirstate(ui, repo) |
|
138 |
for f in lfnames: |
|
139 |
standinname = lfutil.standin(f) |
|
140 |
lfutil.writestandin(repo, standinname, hash='', |
|
141 |
executable=lfutil.getexecutable(repo.wjoin(f))) |
|
142 |
standins.append(standinname) |
|
143 |
if lfdirstate[f] == 'r': |
|
144 |
lfdirstate.normallookup(f) |
|
145 |
else: |
|
146 |
lfdirstate.add(f) |
|
147 |
lfdirstate.write() |
|
148 |
bad += [lfutil.splitstandin(f) for f in lfutil.repo_add(repo, |
|
149 |
standins) if f in m.files()] |
|
150 |
finally: |
|
151 |
wlock.release() |
|
152 |
||
153 |
installnormalfilesmatchfn(repo[None].manifest()) |
|
154 |
result = orig(ui, repo, *pats, **opts) |
|
155 |
restorematchfn() |
|
156 |
||
157 |
return (result == 1 or bad) and 1 or 0 |
|
158 |
||
159 |
def override_remove(orig, ui, repo, *pats, **opts): |
|
160 |
manifest = repo[None].manifest() |
|
161 |
installnormalfilesmatchfn(manifest) |
|
162 |
orig(ui, repo, *pats, **opts) |
|
163 |
restorematchfn() |
|
164 |
||
165 |
after, force = opts.get('after'), opts.get('force') |
|
166 |
if not pats and not after: |
|
167 |
raise util.Abort(_('no files specified')) |
|
168 |
try: |
|
169 |
# Mercurial >= 1.9 |
|
170 |
m = scmutil.match(repo[None], pats, opts) |
|
171 |
except ImportError: |
|
172 |
# Mercurial <= 1.8 |
|
173 |
m = cmdutil.match(repo, pats, opts) |
|
174 |
try: |
|
175 |
repo.lfstatus = True |
|
176 |
s = repo.status(match=m, clean=True) |
|
177 |
finally: |
|
178 |
repo.lfstatus = False |
|
179 |
modified, added, deleted, clean = [[f for f in list if lfutil.standin(f) \ |
|
180 |
in manifest] for list in [s[0], s[1], s[3], s[6]]] |
|
181 |
||
182 |
def warn(files, reason): |
|
183 |
for f in files: |
|
184 |
ui.warn(_('not removing %s: file %s (use -f to force removal)\n') |
|
185 |
% (m.rel(f), reason)) |
|
186 |
||
187 |
if force: |
|
188 |
remove, forget = modified + deleted + clean, added |
|
189 |
elif after: |
|
190 |
remove, forget = deleted, [] |
|
191 |
warn(modified + added + clean, _('still exists')) |
|
192 |
else: |
|
193 |
remove, forget = deleted + clean, [] |
|
194 |
warn(modified, _('is modified')) |
|
195 |
warn(added, _('has been marked for add')) |
|
196 |
||
197 |
for f in sorted(remove + forget): |
|
198 |
if ui.verbose or not m.exact(f): |
|
199 |
ui.status(_('removing %s\n') % m.rel(f)) |
|
200 |
||
201 |
# Need to lock because standin files are deleted then removed from the |
|
202 |
# repository and we could race inbetween. |
|
203 |
wlock = repo.wlock() |
|
204 |
try: |
|
205 |
lfdirstate = lfutil.openlfdirstate(ui, repo) |
|
206 |
for f in remove: |
|
207 |
if not after: |
|
208 |
os.unlink(repo.wjoin(f)) |
|
209 |
currentdir = os.path.split(f)[0] |
|
210 |
while currentdir and not os.listdir(repo.wjoin(currentdir)): |
|
211 |
os.rmdir(repo.wjoin(currentdir)) |
|
212 |
currentdir = os.path.split(currentdir)[0] |
|
213 |
lfdirstate.remove(f) |
|
214 |
lfdirstate.write() |
|
215 |
||
216 |
forget = [lfutil.standin(f) for f in forget] |
|
217 |
remove = [lfutil.standin(f) for f in remove] |
|
218 |
lfutil.repo_forget(repo, forget) |
|
219 |
lfutil.repo_remove(repo, remove, unlink=True) |
|
220 |
finally: |
|
221 |
wlock.release() |
|
222 |
||
223 |
def override_status(orig, ui, repo, *pats, **opts): |
|
224 |
try: |
|
225 |
repo.lfstatus = True |
|
226 |
return orig(ui, repo, *pats, **opts) |
|
227 |
finally: |
|
228 |
repo.lfstatus = False |
|
229 |
||
230 |
def override_log(orig, ui, repo, *pats, **opts): |
|
231 |
try: |
|
232 |
repo.lfstatus = True |
|
233 |
orig(ui, repo, *pats, **opts) |
|
234 |
finally: |
|
235 |
repo.lfstatus = False |
|
236 |
||
237 |
def override_verify(orig, ui, repo, *pats, **opts): |
|
238 |
large = opts.pop('large', False) |
|
239 |
all = opts.pop('lfa', False) |
|
240 |
contents = opts.pop('lfc', False) |
|
241 |
||
242 |
result = orig(ui, repo, *pats, **opts) |
|
243 |
if large: |
|
244 |
result = result or lfcommands.verifylfiles(ui, repo, all, contents) |
|
245 |
return result |
|
246 |
||
247 |
# Override needs to refresh standins so that update's normal merge |
|
248 |
# will go through properly. Then the other update hook (overriding repo.update) |
|
249 |
# will get the new files. Filemerge is also overriden so that the merge |
|
250 |
# will merge standins correctly. |
|
251 |
def override_update(orig, ui, repo, *pats, **opts): |
|
252 |
lfdirstate = lfutil.openlfdirstate(ui, repo) |
|
253 |
s = lfdirstate.status(match_.always(repo.root, repo.getcwd()), [], False, |
|
254 |
False, False) |
|
255 |
(unsure, modified, added, removed, missing, unknown, ignored, clean) = s |
|
256 |
||
257 |
# Need to lock between the standins getting updated and their lfiles |
|
258 |
# getting updated |
|
259 |
wlock = repo.wlock() |
|
260 |
try: |
|
261 |
if opts['check']: |
|
262 |
mod = len(modified) > 0 |
|
263 |
for lfile in unsure: |
|
264 |
standin = lfutil.standin(lfile) |
|
265 |
if repo['.'][standin].data().strip() != \ |
|
266 |
lfutil.hashfile(repo.wjoin(lfile)): |
|
267 |
mod = True |
|
268 |
else: |
|
269 |
lfdirstate.normal(lfile) |
|
270 |
lfdirstate.write() |
|
271 |
if mod: |
|
272 |
raise util.Abort(_('uncommitted local changes')) |
|
273 |
# XXX handle removed differently |
|
274 |
if not opts['clean']: |
|
275 |
for lfile in unsure + modified + added: |
|
276 |
lfutil.updatestandin(repo, lfutil.standin(lfile)) |
|
277 |
finally: |
|
278 |
wlock.release() |
|
279 |
return orig(ui, repo, *pats, **opts) |
|
280 |
||
281 |
# Override filemerge to prompt the user about how they wish to merge lfiles. |
|
282 |
# This will handle identical edits, and copy/rename + edit without prompting |
|
283 |
# the user. |
|
284 |
def override_filemerge(origfn, repo, mynode, orig, fcd, fco, fca): |
|
285 |
# Use better variable names here. Because this is a wrapper we cannot |
|
286 |
# change the variable names in the function declaration. |
|
287 |
fcdest, fcother, fcancestor = fcd, fco, fca |
|
288 |
if not lfutil.isstandin(orig): |
|
289 |
return origfn(repo, mynode, orig, fcdest, fcother, fcancestor) |
|
290 |
else: |
|
291 |
if not fcother.cmp(fcdest): # files identical? |
|
292 |
return None |
|
293 |
||
294 |
# backwards, use working dir parent as ancestor |
|
295 |
if fcancestor == fcother: |
|
296 |
fcancestor = fcdest.parents()[0] |
|
297 |
||
298 |
if orig != fcother.path(): |
|
299 |
repo.ui.status(_('merging %s and %s to %s\n') |
|
300 |
% (lfutil.splitstandin(orig), |
|
301 |
lfutil.splitstandin(fcother.path()), |
|
302 |
lfutil.splitstandin(fcdest.path()))) |
|
303 |
else: |
|
304 |
repo.ui.status(_('merging %s\n') |
|
305 |
% lfutil.splitstandin(fcdest.path())) |
|
306 |
||
307 |
if fcancestor.path() != fcother.path() and fcother.data() == \ |
|
308 |
fcancestor.data(): |
|
309 |
return 0 |
|
310 |
if fcancestor.path() != fcdest.path() and fcdest.data() == \ |
|
311 |
fcancestor.data(): |
|
312 |
repo.wwrite(fcdest.path(), fcother.data(), fcother.flags()) |
|
313 |
return 0 |
|
314 |
||
315 |
if repo.ui.promptchoice(_('largefile %s has a merge conflict\n' |
|
316 |
'keep (l)ocal or take (o)ther?') % |
|
317 |
lfutil.splitstandin(orig), |
|
318 |
(_('&Local'), _('&Other')), 0) == 0: |
|
319 |
return 0 |
|
320 |
else: |
|
321 |
repo.wwrite(fcdest.path(), fcother.data(), fcother.flags()) |
|
322 |
return 0 |
|
323 |
||
324 |
# Copy first changes the matchers to match standins instead of lfiles. |
|
325 |
# Then it overrides util.copyfile in that function it checks if the destination |
|
326 |
# lfile already exists. It also keeps a list of copied files so that the lfiles |
|
327 |
# can be copied and the dirstate updated. |
|
328 |
def override_copy(orig, ui, repo, pats, opts, rename=False): |
|
329 |
# doesn't remove lfile on rename |
|
330 |
if len(pats) < 2: |
|
331 |
# this isn't legal, let the original function deal with it |
|
332 |
return orig(ui, repo, pats, opts, rename) |
|
333 |
||
334 |
def makestandin(relpath): |
|
335 |
try: |
|
336 |
# Mercurial >= 1.9 |
|
337 |
path = scmutil.canonpath(repo.root, repo.getcwd(), relpath) |
|
338 |
except ImportError: |
|
339 |
# Mercurial <= 1.8 |
|
340 |
path = util.canonpath(repo.root, repo.getcwd(), relpath) |
|
341 |
return os.path.join(os.path.relpath('.', repo.getcwd()), |
|
342 |
lfutil.standin(path)) |
|
343 |
||
344 |
try: |
|
345 |
# Mercurial >= 1.9 |
|
346 |
fullpats = scmutil.expandpats(pats) |
|
347 |
except ImportError: |
|
348 |
# Mercurial <= 1.8 |
|
349 |
fullpats = cmdutil.expandpats(pats) |
|
350 |
dest = fullpats[-1] |
|
351 |
||
352 |
if os.path.isdir(dest): |
|
353 |
if not os.path.isdir(makestandin(dest)): |
|
354 |
os.makedirs(makestandin(dest)) |
|
355 |
# This could copy both lfiles and normal files in one command, but we don't |
|
356 |
# want to do that first replace their matcher to only match normal files |
|
357 |
# and run it then replace it to just match lfiles and run it again |
|
358 |
nonormalfiles = False |
|
359 |
nolfiles = False |
|
360 |
try: |
|
361 |
installnormalfilesmatchfn(repo[None].manifest()) |
|
362 |
result = orig(ui, repo, pats, opts, rename) |
|
363 |
except util.Abort, e: |
|
364 |
if str(e) != 'no files to copy': |
|
365 |
raise e |
|
366 |
else: |
|
367 |
nonormalfiles = True |
|
368 |
result = 0 |
|
369 |
finally: |
|
370 |
restorematchfn() |
|
371 |
||
372 |
# The first rename can cause our current working directory to be removed. |
|
373 |
# In that case there is nothing left to copy/rename so just quit. |
|
374 |
try: |
|
375 |
repo.getcwd() |
|
376 |
except OSError: |
|
377 |
return result |
|
378 |
||
379 |
try: |
|
380 |
# When we call orig below it creates the standins but we don't add them |
|
381 |
# to the dir state until later so lock during that time. |
|
382 |
wlock = repo.wlock() |
|
383 |
||
384 |
manifest = repo[None].manifest() |
|
385 |
oldmatch = None # for the closure |
|
386 |
def override_match(repo, pats=[], opts={}, globbed=False, |
|
387 |
default='relpath'): |
|
388 |
newpats = [] |
|
389 |
# The patterns were previously mangled to add the standin |
|
390 |
# directory; we need to remove that now |
|
391 |
for pat in pats: |
|
392 |
if match_.patkind(pat) is None and lfutil.shortname in pat: |
|
393 |
newpats.append(pat.replace(lfutil.shortname, '')) |
|
394 |
else: |
|
395 |
newpats.append(pat) |
|
396 |
match = oldmatch(repo, newpats, opts, globbed, default) |
|
397 |
m = copy.copy(match) |
|
398 |
lfile = lambda f: lfutil.standin(f) in manifest |
|
399 |
m._files = [lfutil.standin(f) for f in m._files if lfile(f)] |
|
400 |
m._fmap = set(m._files) |
|
401 |
orig_matchfn = m.matchfn |
|
402 |
m.matchfn = lambda f: lfutil.isstandin(f) and \ |
|
403 |
lfile(lfutil.splitstandin(f)) and \ |
|
404 |
orig_matchfn(lfutil.splitstandin(f)) or None |
|
405 |
return m |
|
406 |
oldmatch = installmatchfn(override_match) |
|
407 |
listpats = [] |
|
408 |
for pat in pats: |
|
409 |
if match_.patkind(pat) is not None: |
|
410 |
listpats.append(pat) |
|
411 |
else: |
|
412 |
listpats.append(makestandin(pat)) |
|
413 |
||
414 |
try: |
|
415 |
origcopyfile = util.copyfile |
|
416 |
copiedfiles = [] |
|
417 |
def override_copyfile(src, dest): |
|
418 |
if lfutil.shortname in src and lfutil.shortname in dest: |
|
419 |
destlfile = dest.replace(lfutil.shortname, '') |
|
420 |
if not opts['force'] and os.path.exists(destlfile): |
|
421 |
raise IOError('', |
|
422 |
_('destination largefile already exists')) |
|
423 |
copiedfiles.append((src, dest)) |
|
424 |
origcopyfile(src, dest) |
|
425 |
||
426 |
util.copyfile = override_copyfile |
|
427 |
result += orig(ui, repo, listpats, opts, rename) |
|
428 |
finally: |
|
429 |
util.copyfile = origcopyfile |
|
430 |
||
431 |
lfdirstate = lfutil.openlfdirstate(ui, repo) |
|
432 |
for (src, dest) in copiedfiles: |
|
433 |
if lfutil.shortname in src and lfutil.shortname in dest: |
|
434 |
srclfile = src.replace(lfutil.shortname, '') |
|
435 |
destlfile = dest.replace(lfutil.shortname, '') |
|
436 |
destlfiledir = os.path.dirname(destlfile) or '.' |
|
437 |
if not os.path.isdir(destlfiledir): |
|
438 |
os.makedirs(destlfiledir) |
|
439 |
if rename: |
|
440 |
os.rename(srclfile, destlfile) |
|
441 |
lfdirstate.remove(os.path.relpath(srclfile, |
|
442 |
repo.root)) |
|
443 |
else: |
|
444 |
util.copyfile(srclfile, destlfile) |
|
445 |
lfdirstate.add(os.path.relpath(destlfile, |
|
446 |
repo.root)) |
|
447 |
lfdirstate.write() |
|
448 |
except util.Abort, e: |
|
449 |
if str(e) != 'no files to copy': |
|
450 |
raise e |
|
451 |
else: |
|
452 |
nolfiles = True |
|
453 |
finally: |
|
454 |
restorematchfn() |
|
455 |
wlock.release() |
|
456 |
||
457 |
if nolfiles and nonormalfiles: |
|
458 |
raise util.Abort(_('no files to copy')) |
|
459 |
||
460 |
return result |
|
461 |
||
462 |
# When the user calls revert, we have to be careful to not revert any changes |
|
463 |
# to other lfiles accidentally. This means we have to keep track of the lfiles |
|
464 |
# that are being reverted so we only pull down the necessary lfiles. |
|
465 |
# |
|
466 |
# Standins are only updated (to match the hash of lfiles) before commits. |
|
467 |
# Update the standins then run the original revert (changing the matcher to hit |
|
468 |
# standins instead of lfiles). Based on the resulting standins update the |
|
469 |
# lfiles. Then return the standins to their proper state |
|
470 |
def override_revert(orig, ui, repo, *pats, **opts): |
|
471 |
# Because we put the standins in a bad state (by updating them) and then |
|
472 |
# return them to a correct state we need to lock to prevent others from |
|
473 |
# changing them in their incorrect state. |
|
474 |
wlock = repo.wlock() |
|
475 |
try: |
|
476 |
lfdirstate = lfutil.openlfdirstate(ui, repo) |
|
477 |
(modified, added, removed, missing, unknown, ignored, clean) = \ |
|
478 |
lfutil.lfdirstate_status(lfdirstate, repo, repo['.'].rev()) |
|
479 |
for lfile in modified: |
|
480 |
lfutil.updatestandin(repo, lfutil.standin(lfile)) |
|
481 |
||
482 |
try: |
|
483 |
ctx = repo[opts.get('rev')] |
|
484 |
oldmatch = None # for the closure |
|
485 |
def override_match(ctxorrepo, pats=[], opts={}, globbed=False, |
|
486 |
default='relpath'): |
|
15169
aa262fff87ac
largefile: fix up hasattr usage
Matt Mackall <mpm@selenic.com>
parents:
15168
diff
changeset
|
487 |
if util.safehasattr(ctxorrepo, 'match'): |
15168 | 488 |
ctx0 = ctxorrepo |
489 |
else: |
|
490 |
ctx0 = ctxorrepo[None] |
|
491 |
match = oldmatch(ctxorrepo, pats, opts, globbed, default) |
|
492 |
m = copy.copy(match) |
|
493 |
def tostandin(f): |
|
494 |
if lfutil.standin(f) in ctx0 or lfutil.standin(f) in ctx: |
|
495 |
return lfutil.standin(f) |
|
496 |
elif lfutil.standin(f) in repo[None]: |
|
497 |
return None |
|
498 |
return f |
|
499 |
m._files = [tostandin(f) for f in m._files] |
|
500 |
m._files = [f for f in m._files if f is not None] |
|
501 |
m._fmap = set(m._files) |
|
502 |
orig_matchfn = m.matchfn |
|
503 |
def matchfn(f): |
|
504 |
if lfutil.isstandin(f): |
|
505 |
# We need to keep track of what lfiles are being |
|
506 |
# matched so we know which ones to update later |
|
507 |
# (otherwise we revert changes to other lfiles |
|
508 |
# accidentally). This is repo specific, so duckpunch |
|
509 |
# the repo object to keep the list of lfiles for us |
|
510 |
# later. |
|
511 |
if orig_matchfn(lfutil.splitstandin(f)) and \ |
|
512 |
(f in repo[None] or f in ctx): |
|
513 |
lfileslist = getattr(repo, '_lfilestoupdate', []) |
|
514 |
lfileslist.append(lfutil.splitstandin(f)) |
|
515 |
repo._lfilestoupdate = lfileslist |
|
516 |
return True |
|
517 |
else: |
|
518 |
return False |
|
519 |
return orig_matchfn(f) |
|
520 |
m.matchfn = matchfn |
|
521 |
return m |
|
522 |
oldmatch = installmatchfn(override_match) |
|
523 |
try: |
|
524 |
# Mercurial >= 1.9 |
|
525 |
scmutil.match |
|
526 |
matches = override_match(repo[None], pats, opts) |
|
527 |
except ImportError: |
|
528 |
# Mercurial <= 1.8 |
|
529 |
matches = override_match(repo, pats, opts) |
|
530 |
orig(ui, repo, *pats, **opts) |
|
531 |
finally: |
|
532 |
restorematchfn() |
|
533 |
lfileslist = getattr(repo, '_lfilestoupdate', []) |
|
15170
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15169
diff
changeset
|
534 |
lfcommands.updatelfiles(ui, repo, filelist=lfileslist, |
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15169
diff
changeset
|
535 |
printmessage=False) |
15168 | 536 |
# Empty out the lfiles list so we start fresh next time |
537 |
repo._lfilestoupdate = [] |
|
538 |
for lfile in modified: |
|
539 |
if lfile in lfileslist: |
|
540 |
if os.path.exists(repo.wjoin(lfutil.standin(lfile))) and lfile\ |
|
541 |
in repo['.']: |
|
542 |
lfutil.writestandin(repo, lfutil.standin(lfile), |
|
543 |
repo['.'][lfile].data().strip(), |
|
544 |
'x' in repo['.'][lfile].flags()) |
|
545 |
lfdirstate = lfutil.openlfdirstate(ui, repo) |
|
546 |
for lfile in added: |
|
547 |
standin = lfutil.standin(lfile) |
|
548 |
if standin not in ctx and (standin in matches or opts.get('all')): |
|
549 |
if lfile in lfdirstate: |
|
550 |
try: |
|
551 |
# Mercurial >= 1.9 |
|
552 |
lfdirstate.drop(lfile) |
|
553 |
except AttributeError: |
|
554 |
# Mercurial <= 1.8 |
|
555 |
lfdirstate.forget(lfile) |
|
556 |
util.unlinkpath(repo.wjoin(standin)) |
|
557 |
lfdirstate.write() |
|
558 |
finally: |
|
559 |
wlock.release() |
|
560 |
||
561 |
def hg_update(orig, repo, node): |
|
562 |
result = orig(repo, node) |
|
563 |
# XXX check if it worked first |
|
564 |
lfcommands.updatelfiles(repo.ui, repo) |
|
565 |
return result |
|
566 |
||
567 |
def hg_clean(orig, repo, node, show_stats=True): |
|
568 |
result = orig(repo, node, show_stats) |
|
569 |
lfcommands.updatelfiles(repo.ui, repo) |
|
570 |
return result |
|
571 |
||
572 |
def hg_merge(orig, repo, node, force=None, remind=True): |
|
573 |
result = orig(repo, node, force, remind) |
|
574 |
lfcommands.updatelfiles(repo.ui, repo) |
|
575 |
return result |
|
576 |
||
577 |
# When we rebase a repository with remotely changed lfiles, we need |
|
578 |
# to take some extra care so that the lfiles are correctly updated |
|
579 |
# in the working copy |
|
580 |
def override_pull(orig, ui, repo, source=None, **opts): |
|
581 |
if opts.get('rebase', False): |
|
582 |
repo._isrebasing = True |
|
583 |
try: |
|
584 |
if opts.get('update'): |
|
585 |
del opts['update'] |
|
586 |
ui.debug('--update and --rebase are not compatible, ignoring ' |
|
587 |
'the update flag\n') |
|
588 |
del opts['rebase'] |
|
589 |
try: |
|
590 |
# Mercurial >= 1.9 |
|
591 |
cmdutil.bailifchanged(repo) |
|
592 |
except AttributeError: |
|
593 |
# Mercurial <= 1.8 |
|
594 |
cmdutil.bail_if_changed(repo) |
|
595 |
revsprepull = len(repo) |
|
596 |
origpostincoming = commands.postincoming |
|
597 |
def _dummy(*args, **kwargs): |
|
598 |
pass |
|
599 |
commands.postincoming = _dummy |
|
600 |
repo.lfpullsource = source |
|
601 |
if not source: |
|
602 |
source = 'default' |
|
603 |
try: |
|
604 |
result = commands.pull(ui, repo, source, **opts) |
|
605 |
finally: |
|
606 |
commands.postincoming = origpostincoming |
|
607 |
revspostpull = len(repo) |
|
608 |
if revspostpull > revsprepull: |
|
609 |
result = result or rebase.rebase(ui, repo) |
|
610 |
finally: |
|
611 |
repo._isrebasing = False |
|
612 |
else: |
|
613 |
repo.lfpullsource = source |
|
614 |
if not source: |
|
615 |
source = 'default' |
|
616 |
result = orig(ui, repo, source, **opts) |
|
617 |
return result |
|
618 |
||
619 |
def override_rebase(orig, ui, repo, **opts): |
|
620 |
repo._isrebasing = True |
|
621 |
try: |
|
622 |
orig(ui, repo, **opts) |
|
623 |
finally: |
|
624 |
repo._isrebasing = False |
|
625 |
||
626 |
def override_archive(orig, repo, dest, node, kind, decode=True, matchfn=None, |
|
627 |
prefix=None, mtime=None, subrepos=None): |
|
628 |
# No need to lock because we are only reading history and lfile caches |
|
629 |
# neither of which are modified |
|
630 |
||
631 |
lfcommands.cachelfiles(repo.ui, repo, node) |
|
632 |
||
633 |
if kind not in archival.archivers: |
|
634 |
raise util.Abort(_("unknown archive type '%s'") % kind) |
|
635 |
||
636 |
ctx = repo[node] |
|
637 |
||
638 |
# In Mercurial <= 1.5 the prefix is passed to the archiver so try that |
|
639 |
# if that doesn't work we are probably in Mercurial >= 1.6 where the |
|
640 |
# prefix is not handled by the archiver |
|
641 |
try: |
|
642 |
archiver = archival.archivers[kind](dest, prefix, mtime or \ |
|
643 |
ctx.date()[0]) |
|
644 |
||
645 |
def write(name, mode, islink, getdata): |
|
646 |
if matchfn and not matchfn(name): |
|
647 |
return |
|
648 |
data = getdata() |
|
649 |
if decode: |
|
650 |
data = repo.wwritedata(name, data) |
|
651 |
archiver.addfile(name, mode, islink, data) |
|
652 |
except TypeError: |
|
653 |
if kind == 'files': |
|
654 |
if prefix: |
|
655 |
raise util.Abort( |
|
656 |
_('cannot give prefix when archiving to files')) |
|
657 |
else: |
|
658 |
prefix = archival.tidyprefix(dest, kind, prefix) |
|
659 |
||
660 |
def write(name, mode, islink, getdata): |
|
661 |
if matchfn and not matchfn(name): |
|
662 |
return |
|
663 |
data = getdata() |
|
664 |
if decode: |
|
665 |
data = repo.wwritedata(name, data) |
|
666 |
archiver.addfile(prefix + name, mode, islink, data) |
|
667 |
||
668 |
archiver = archival.archivers[kind](dest, mtime or ctx.date()[0]) |
|
669 |
||
670 |
if repo.ui.configbool("ui", "archivemeta", True): |
|
671 |
def metadata(): |
|
672 |
base = 'repo: %s\nnode: %s\nbranch: %s\n' % ( |
|
673 |
hex(repo.changelog.node(0)), hex(node), ctx.branch()) |
|
674 |
||
675 |
tags = ''.join('tag: %s\n' % t for t in ctx.tags() |
|
676 |
if repo.tagtype(t) == 'global') |
|
677 |
if not tags: |
|
678 |
repo.ui.pushbuffer() |
|
679 |
opts = {'template': '{latesttag}\n{latesttagdistance}', |
|
680 |
'style': '', 'patch': None, 'git': None} |
|
681 |
cmdutil.show_changeset(repo.ui, repo, opts).show(ctx) |
|
682 |
ltags, dist = repo.ui.popbuffer().split('\n') |
|
683 |
tags = ''.join('latesttag: %s\n' % t for t in ltags.split(':')) |
|
684 |
tags += 'latesttagdistance: %s\n' % dist |
|
685 |
||
686 |
return base + tags |
|
687 |
||
688 |
write('.hg_archival.txt', 0644, False, metadata) |
|
689 |
||
690 |
for f in ctx: |
|
691 |
ff = ctx.flags(f) |
|
692 |
getdata = ctx[f].data |
|
693 |
if lfutil.isstandin(f): |
|
694 |
path = lfutil.findfile(repo, getdata().strip()) |
|
695 |
f = lfutil.splitstandin(f) |
|
696 |
||
697 |
def getdatafn(): |
|
698 |
try: |
|
699 |
fd = open(path, 'rb') |
|
700 |
return fd.read() |
|
701 |
finally: |
|
702 |
fd.close() |
|
703 |
||
704 |
getdata = getdatafn |
|
705 |
write(f, 'x' in ff and 0755 or 0644, 'l' in ff, getdata) |
|
706 |
||
707 |
if subrepos: |
|
708 |
for subpath in ctx.substate: |
|
709 |
sub = ctx.sub(subpath) |
|
710 |
try: |
|
711 |
sub.archive(repo.ui, archiver, prefix) |
|
712 |
except TypeError: |
|
713 |
sub.archive(archiver, prefix) |
|
714 |
||
715 |
archiver.done() |
|
716 |
||
717 |
# If a lfile is modified the change is not reflected in its standin until a |
|
718 |
# commit. cmdutil.bailifchanged raises an exception if the repo has |
|
719 |
# uncommitted changes. Wrap it to also check if lfiles were changed. This is |
|
720 |
# used by bisect and backout. |
|
721 |
def override_bailifchanged(orig, repo): |
|
722 |
orig(repo) |
|
723 |
repo.lfstatus = True |
|
724 |
modified, added, removed, deleted = repo.status()[:4] |
|
725 |
repo.lfstatus = False |
|
726 |
if modified or added or removed or deleted: |
|
727 |
raise util.Abort(_('outstanding uncommitted changes')) |
|
728 |
||
729 |
# Fetch doesn't use cmdutil.bail_if_changed so override it to add the check |
|
730 |
def override_fetch(orig, ui, repo, *pats, **opts): |
|
731 |
repo.lfstatus = True |
|
732 |
modified, added, removed, deleted = repo.status()[:4] |
|
733 |
repo.lfstatus = False |
|
734 |
if modified or added or removed or deleted: |
|
735 |
raise util.Abort(_('outstanding uncommitted changes')) |
|
736 |
return orig(ui, repo, *pats, **opts) |
|
737 |
||
738 |
def override_forget(orig, ui, repo, *pats, **opts): |
|
739 |
installnormalfilesmatchfn(repo[None].manifest()) |
|
740 |
orig(ui, repo, *pats, **opts) |
|
741 |
restorematchfn() |
|
742 |
try: |
|
743 |
# Mercurial >= 1.9 |
|
744 |
m = scmutil.match(repo[None], pats, opts) |
|
745 |
except ImportError: |
|
746 |
# Mercurial <= 1.8 |
|
747 |
m = cmdutil.match(repo, pats, opts) |
|
748 |
||
749 |
try: |
|
750 |
repo.lfstatus = True |
|
751 |
s = repo.status(match=m, clean=True) |
|
752 |
finally: |
|
753 |
repo.lfstatus = False |
|
754 |
forget = sorted(s[0] + s[1] + s[3] + s[6]) |
|
755 |
forget = [f for f in forget if lfutil.standin(f) in repo[None].manifest()] |
|
756 |
||
757 |
for f in forget: |
|
758 |
if lfutil.standin(f) not in repo.dirstate and not \ |
|
759 |
os.path.isdir(m.rel(lfutil.standin(f))): |
|
760 |
ui.warn(_('not removing %s: file is already untracked\n') |
|
761 |
% m.rel(f)) |
|
762 |
||
763 |
for f in forget: |
|
764 |
if ui.verbose or not m.exact(f): |
|
765 |
ui.status(_('removing %s\n') % m.rel(f)) |
|
766 |
||
767 |
# Need to lock because standin files are deleted then removed from the |
|
768 |
# repository and we could race inbetween. |
|
769 |
wlock = repo.wlock() |
|
770 |
try: |
|
771 |
lfdirstate = lfutil.openlfdirstate(ui, repo) |
|
772 |
for f in forget: |
|
773 |
if lfdirstate[f] == 'a': |
|
774 |
lfdirstate.drop(f) |
|
775 |
else: |
|
776 |
lfdirstate.remove(f) |
|
777 |
lfdirstate.write() |
|
778 |
lfutil.repo_remove(repo, [lfutil.standin(f) for f in forget], |
|
779 |
unlink=True) |
|
780 |
finally: |
|
781 |
wlock.release() |
|
782 |
||
783 |
def getoutgoinglfiles(ui, repo, dest=None, **opts): |
|
784 |
dest = ui.expandpath(dest or 'default-push', dest or 'default') |
|
785 |
dest, branches = hg.parseurl(dest, opts.get('branch')) |
|
786 |
revs, checkout = hg.addbranchrevs(repo, repo, branches, opts.get('rev')) |
|
787 |
if revs: |
|
788 |
revs = [repo.lookup(rev) for rev in revs] |
|
789 |
||
790 |
# Mercurial <= 1.5 had remoteui in cmdutil, then it moved to hg |
|
791 |
try: |
|
792 |
remoteui = cmdutil.remoteui |
|
793 |
except AttributeError: |
|
794 |
remoteui = hg.remoteui |
|
795 |
||
796 |
try: |
|
797 |
remote = hg.repository(remoteui(repo, opts), dest) |
|
798 |
except error.RepoError: |
|
799 |
return None |
|
800 |
o = lfutil.findoutgoing(repo, remote, False) |
|
801 |
if not o: |
|
802 |
return None |
|
803 |
o = repo.changelog.nodesbetween(o, revs)[0] |
|
804 |
if opts.get('newest_first'): |
|
805 |
o.reverse() |
|
806 |
||
807 |
toupload = set() |
|
808 |
for n in o: |
|
809 |
parents = [p for p in repo.changelog.parents(n) if p != node.nullid] |
|
810 |
ctx = repo[n] |
|
811 |
files = set(ctx.files()) |
|
812 |
if len(parents) == 2: |
|
813 |
mc = ctx.manifest() |
|
814 |
mp1 = ctx.parents()[0].manifest() |
|
815 |
mp2 = ctx.parents()[1].manifest() |
|
816 |
for f in mp1: |
|
817 |
if f not in mc: |
|
818 |
files.add(f) |
|
819 |
for f in mp2: |
|
820 |
if f not in mc: |
|
821 |
files.add(f) |
|
822 |
for f in mc: |
|
823 |
if mc[f] != mp1.get(f, None) or mc[f] != mp2.get(f, None): |
|
824 |
files.add(f) |
|
825 |
toupload = toupload.union(set([f for f in files if lfutil.isstandin(f)\ |
|
826 |
and f in ctx])) |
|
827 |
return toupload |
|
828 |
||
829 |
def override_outgoing(orig, ui, repo, dest=None, **opts): |
|
830 |
orig(ui, repo, dest, **opts) |
|
831 |
||
832 |
if opts.pop('large', None): |
|
833 |
toupload = getoutgoinglfiles(ui, repo, dest, **opts) |
|
834 |
if toupload is None: |
|
835 |
ui.status(_('largefiles: No remote repo\n')) |
|
836 |
else: |
|
837 |
ui.status(_('largefiles to upload:\n')) |
|
838 |
for file in toupload: |
|
839 |
ui.status(lfutil.splitstandin(file) + '\n') |
|
840 |
ui.status('\n') |
|
841 |
||
842 |
def override_summary(orig, ui, repo, *pats, **opts): |
|
843 |
orig(ui, repo, *pats, **opts) |
|
844 |
||
845 |
if opts.pop('large', None): |
|
846 |
toupload = getoutgoinglfiles(ui, repo, None, **opts) |
|
847 |
if toupload is None: |
|
848 |
ui.status(_('largefiles: No remote repo\n')) |
|
849 |
else: |
|
850 |
ui.status(_('largefiles: %d to upload\n') % len(toupload)) |
|
851 |
||
852 |
def override_addremove(orig, ui, repo, *pats, **opts): |
|
853 |
# Check if the parent or child has lfiles if they do don't allow it. If |
|
854 |
# there is a symlink in the manifest then getting the manifest throws an |
|
855 |
# exception catch it and let addremove deal with it. This happens in |
|
856 |
# Mercurial's test test-addremove-symlink |
|
857 |
try: |
|
858 |
manifesttip = set(repo['tip'].manifest()) |
|
859 |
except util.Abort: |
|
860 |
manifesttip = set() |
|
861 |
try: |
|
862 |
manifestworking = set(repo[None].manifest()) |
|
863 |
except util.Abort: |
|
864 |
manifestworking = set() |
|
865 |
||
866 |
# Manifests are only iterable so turn them into sets then union |
|
867 |
for file in manifesttip.union(manifestworking): |
|
868 |
if file.startswith(lfutil.shortname): |
|
869 |
raise util.Abort( |
|
870 |
_('addremove cannot be run on a repo with largefiles')) |
|
871 |
||
872 |
return orig(ui, repo, *pats, **opts) |
|
873 |
||
874 |
# Calling purge with --all will cause the lfiles to be deleted. |
|
875 |
# Override repo.status to prevent this from happening. |
|
876 |
def override_purge(orig, ui, repo, *dirs, **opts): |
|
877 |
oldstatus = repo.status |
|
878 |
def override_status(node1='.', node2=None, match=None, ignored=False, |
|
879 |
clean=False, unknown=False, listsubrepos=False): |
|
880 |
r = oldstatus(node1, node2, match, ignored, clean, unknown, |
|
881 |
listsubrepos) |
|
882 |
lfdirstate = lfutil.openlfdirstate(ui, repo) |
|
883 |
modified, added, removed, deleted, unknown, ignored, clean = r |
|
884 |
unknown = [f for f in unknown if lfdirstate[f] == '?'] |
|
885 |
ignored = [f for f in ignored if lfdirstate[f] == '?'] |
|
886 |
return modified, added, removed, deleted, unknown, ignored, clean |
|
887 |
repo.status = override_status |
|
888 |
orig(ui, repo, *dirs, **opts) |
|
889 |
repo.status = oldstatus |
|
890 |
||
891 |
def override_rollback(orig, ui, repo, **opts): |
|
892 |
result = orig(ui, repo, **opts) |
|
893 |
merge.update(repo, node=None, branchmerge=False, force=True, |
|
894 |
partial=lfutil.isstandin) |
|
895 |
lfdirstate = lfutil.openlfdirstate(ui, repo) |
|
896 |
lfiles = lfutil.listlfiles(repo) |
|
897 |
oldlfiles = lfutil.listlfiles(repo, repo[None].parents()[0].rev()) |
|
898 |
for file in lfiles: |
|
899 |
if file in oldlfiles: |
|
900 |
lfdirstate.normallookup(file) |
|
901 |
else: |
|
902 |
lfdirstate.add(file) |
|
903 |
lfdirstate.write() |
|
904 |
return result |