Mercurial > public > mercurial-scm > hg
annotate hgext/largefiles/lfcommands.py @ 15170:c1a4a3220711
largefiles: fix over-long lines
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Thu, 29 Sep 2011 17:04:57 -0500 |
parents | cfccd3bee7b3 |
children | 547da6115d1d |
rev | line source |
---|---|
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 '''High-level command functions: lfadd() et. al, plus the cmdtable.''' | |
10 | |
11 import os | |
12 import shutil | |
13 | |
14 from mercurial import util, match as match_, hg, node, context, error | |
15 from mercurial.i18n import _ | |
16 | |
17 import lfutil | |
18 import basestore | |
19 | |
20 # -- Commands ---------------------------------------------------------- | |
21 | |
22 def lfconvert(ui, src, dest, *pats, **opts): | |
23 '''Convert a normal repository to a largefiles repository | |
24 | |
25 Convert source repository creating an identical repository, except that all | |
26 files that match the patterns given, or are over the given size will be | |
27 added as largefiles. The size used to determine whether or not to track a | |
28 file as a largefile is the size of the first version of the file. After | |
29 running this command you will need to make sure that largefiles is enabled | |
30 anywhere you intend to push the new repository.''' | |
31 | |
32 if opts['tonormal']: | |
33 tolfile = False | |
34 else: | |
35 tolfile = True | |
36 size = opts['size'] | |
37 if not size: | |
38 size = ui.config(lfutil.longname, 'size', default=None) | |
39 try: | |
40 size = int(size) | |
41 except ValueError: | |
15170
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15168
diff
changeset
|
42 raise util.Abort(_('largefiles.size must be integer, was %s\n') |
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15168
diff
changeset
|
43 % size) |
15168 | 44 except TypeError: |
45 raise util.Abort(_('size must be specified')) | |
46 | |
47 try: | |
48 rsrc = hg.repository(ui, src) | |
49 if not rsrc.local(): | |
50 raise util.Abort(_('%s is not a local Mercurial repo') % src) | |
51 except error.RepoError, err: | |
52 ui.traceback() | |
53 raise util.Abort(err.args[0]) | |
54 if os.path.exists(dest): | |
55 if not os.path.isdir(dest): | |
56 raise util.Abort(_('destination %s already exists') % dest) | |
57 elif os.listdir(dest): | |
58 raise util.Abort(_('destination %s is not empty') % dest) | |
59 try: | |
60 ui.status(_('initializing destination %s\n') % dest) | |
61 rdst = hg.repository(ui, dest, create=True) | |
62 if not rdst.local(): | |
63 raise util.Abort(_('%s is not a local Mercurial repo') % dest) | |
64 except error.RepoError: | |
65 ui.traceback() | |
66 raise util.Abort(_('%s is not a repo') % dest) | |
67 | |
68 try: | |
69 # Lock destination to prevent modification while it is converted to. | |
70 # Don't need to lock src because we are just reading from its history | |
71 # which can't change. | |
72 dst_lock = rdst.lock() | |
73 | |
74 # Get a list of all changesets in the source. The easy way to do this | |
75 # is to simply walk the changelog, using changelog.nodesbewteen(). | |
76 # Take a look at mercurial/revlog.py:639 for more details. | |
77 # Use a generator instead of a list to decrease memory usage | |
78 ctxs = (rsrc[ctx] for ctx in rsrc.changelog.nodesbetween(None, | |
79 rsrc.heads())[0]) | |
80 revmap = {node.nullid: node.nullid} | |
81 if tolfile: | |
82 lfiles = set() | |
83 normalfiles = set() | |
84 if not pats: | |
85 pats = ui.config(lfutil.longname, 'patterns', default=()) | |
86 if pats: | |
87 pats = pats.split(' ') | |
88 if pats: | |
89 matcher = match_.match(rsrc.root, '', list(pats)) | |
90 else: | |
91 matcher = None | |
92 | |
93 lfiletohash = {} | |
94 for ctx in ctxs: | |
95 ui.progress(_('converting revisions'), ctx.rev(), | |
96 unit=_('revision'), total=rsrc['tip'].rev()) | |
97 _lfconvert_addchangeset(rsrc, rdst, ctx, revmap, | |
98 lfiles, normalfiles, matcher, size, lfiletohash) | |
99 ui.progress(_('converting revisions'), None) | |
100 | |
101 if os.path.exists(rdst.wjoin(lfutil.shortname)): | |
102 shutil.rmtree(rdst.wjoin(lfutil.shortname)) | |
103 | |
104 for f in lfiletohash.keys(): | |
105 if os.path.isfile(rdst.wjoin(f)): | |
106 os.unlink(rdst.wjoin(f)) | |
107 try: | |
108 os.removedirs(os.path.dirname(rdst.wjoin(f))) | |
109 except: | |
110 pass | |
111 | |
112 else: | |
113 for ctx in ctxs: | |
114 ui.progress(_('converting revisions'), ctx.rev(), | |
115 unit=_('revision'), total=rsrc['tip'].rev()) | |
116 _addchangeset(ui, rsrc, rdst, ctx, revmap) | |
117 | |
118 ui.progress(_('converting revisions'), None) | |
119 except: | |
120 # we failed, remove the new directory | |
121 shutil.rmtree(rdst.root) | |
122 raise | |
123 finally: | |
124 dst_lock.release() | |
125 | |
126 def _addchangeset(ui, rsrc, rdst, ctx, revmap): | |
127 # Convert src parents to dst parents | |
128 parents = [] | |
129 for p in ctx.parents(): | |
130 parents.append(revmap[p.node()]) | |
131 while len(parents) < 2: | |
132 parents.append(node.nullid) | |
133 | |
134 # Generate list of changed files | |
135 files = set(ctx.files()) | |
136 if node.nullid not in parents: | |
137 mc = ctx.manifest() | |
138 mp1 = ctx.parents()[0].manifest() | |
139 mp2 = ctx.parents()[1].manifest() | |
140 files |= (set(mp1) | set(mp2)) - set(mc) | |
141 for f in mc: | |
142 if mc[f] != mp1.get(f, None) or mc[f] != mp2.get(f, None): | |
143 files.add(f) | |
144 | |
145 def getfilectx(repo, memctx, f): | |
146 if lfutil.standin(f) in files: | |
147 # if the file isn't in the manifest then it was removed | |
148 # or renamed, raise IOError to indicate this | |
149 try: | |
150 fctx = ctx.filectx(lfutil.standin(f)) | |
151 except error.LookupError: | |
152 raise IOError() | |
153 renamed = fctx.renamed() | |
154 if renamed: | |
155 renamed = lfutil.splitstandin(renamed[0]) | |
156 | |
157 hash = fctx.data().strip() | |
158 path = lfutil.findfile(rsrc, hash) | |
159 ### TODO: What if the file is not cached? | |
160 data = '' | |
161 fd = None | |
162 try: | |
163 fd = open(path, 'rb') | |
164 data = fd.read() | |
165 finally: | |
166 if fd: fd.close() | |
167 return context.memfilectx(f, data, 'l' in fctx.flags(), | |
168 'x' in fctx.flags(), renamed) | |
169 else: | |
170 try: | |
171 fctx = ctx.filectx(f) | |
172 except error.LookupError: | |
173 raise IOError() | |
174 renamed = fctx.renamed() | |
175 if renamed: | |
176 renamed = renamed[0] | |
177 data = fctx.data() | |
178 if f == '.hgtags': | |
179 newdata = [] | |
180 for line in data.splitlines(): | |
181 id, name = line.split(' ', 1) | |
182 newdata.append('%s %s\n' % (node.hex(revmap[node.bin(id)]), | |
183 name)) | |
184 data = ''.join(newdata) | |
185 return context.memfilectx(f, data, 'l' in fctx.flags(), | |
186 'x' in fctx.flags(), renamed) | |
187 | |
188 dstfiles = [] | |
189 for file in files: | |
190 if lfutil.isstandin(file): | |
191 dstfiles.append(lfutil.splitstandin(file)) | |
192 else: | |
193 dstfiles.append(file) | |
194 # Commit | |
195 mctx = context.memctx(rdst, parents, ctx.description(), dstfiles, | |
196 getfilectx, ctx.user(), ctx.date(), ctx.extra()) | |
197 ret = rdst.commitctx(mctx) | |
198 rdst.dirstate.setparents(ret) | |
199 revmap[ctx.node()] = rdst.changelog.tip() | |
200 | |
201 def _lfconvert_addchangeset(rsrc, rdst, ctx, revmap, lfiles, normalfiles, | |
202 matcher, size, lfiletohash): | |
203 # Convert src parents to dst parents | |
204 parents = [] | |
205 for p in ctx.parents(): | |
206 parents.append(revmap[p.node()]) | |
207 while len(parents) < 2: | |
208 parents.append(node.nullid) | |
209 | |
210 # Generate list of changed files | |
211 files = set(ctx.files()) | |
212 if node.nullid not in parents: | |
213 mc = ctx.manifest() | |
214 mp1 = ctx.parents()[0].manifest() | |
215 mp2 = ctx.parents()[1].manifest() | |
216 files |= (set(mp1) | set(mp2)) - set(mc) | |
217 for f in mc: | |
218 if mc[f] != mp1.get(f, None) or mc[f] != mp2.get(f, None): | |
219 files.add(f) | |
220 | |
221 dstfiles = [] | |
222 for f in files: | |
223 if f not in lfiles and f not in normalfiles: | |
224 islfile = _islfile(f, ctx, matcher, size) | |
225 # If this file was renamed or copied then copy | |
226 # the lfileness of its predecessor | |
227 if f in ctx.manifest(): | |
228 fctx = ctx.filectx(f) | |
229 renamed = fctx.renamed() | |
230 renamedlfile = renamed and renamed[0] in lfiles | |
231 islfile |= renamedlfile | |
232 if 'l' in fctx.flags(): | |
233 if renamedlfile: | |
234 raise util.Abort( | |
15170
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15168
diff
changeset
|
235 _('Renamed/copied largefile %s becomes symlink') |
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15168
diff
changeset
|
236 % f) |
15168 | 237 islfile = False |
238 if islfile: | |
239 lfiles.add(f) | |
240 else: | |
241 normalfiles.add(f) | |
242 | |
243 if f in lfiles: | |
244 dstfiles.append(lfutil.standin(f)) | |
245 # lfile in manifest if it has not been removed/renamed | |
246 if f in ctx.manifest(): | |
247 if 'l' in ctx.filectx(f).flags(): | |
248 if renamed and renamed[0] in lfiles: | |
249 raise util.Abort(_('largefile %s becomes symlink') % f) | |
250 | |
251 # lfile was modified, update standins | |
252 fullpath = rdst.wjoin(f) | |
253 lfutil.createdir(os.path.dirname(fullpath)) | |
254 m = util.sha1('') | |
255 m.update(ctx[f].data()) | |
256 hash = m.hexdigest() | |
257 if f not in lfiletohash or lfiletohash[f] != hash: | |
258 try: | |
259 fd = open(fullpath, 'wb') | |
260 fd.write(ctx[f].data()) | |
261 finally: | |
262 if fd: | |
263 fd.close() | |
264 executable = 'x' in ctx[f].flags() | |
265 os.chmod(fullpath, lfutil.getmode(executable)) | |
266 lfutil.writestandin(rdst, lfutil.standin(f), hash, | |
267 executable) | |
268 lfiletohash[f] = hash | |
269 else: | |
270 # normal file | |
271 dstfiles.append(f) | |
272 | |
273 def getfilectx(repo, memctx, f): | |
274 if lfutil.isstandin(f): | |
275 # if the file isn't in the manifest then it was removed | |
276 # or renamed, raise IOError to indicate this | |
277 srcfname = lfutil.splitstandin(f) | |
278 try: | |
279 fctx = ctx.filectx(srcfname) | |
280 except error.LookupError: | |
281 raise IOError() | |
282 renamed = fctx.renamed() | |
283 if renamed: | |
284 # standin is always a lfile because lfileness | |
285 # doesn't change after rename or copy | |
286 renamed = lfutil.standin(renamed[0]) | |
287 | |
288 return context.memfilectx(f, lfiletohash[srcfname], 'l' in | |
289 fctx.flags(), 'x' in fctx.flags(), renamed) | |
290 else: | |
291 try: | |
292 fctx = ctx.filectx(f) | |
293 except error.LookupError: | |
294 raise IOError() | |
295 renamed = fctx.renamed() | |
296 if renamed: | |
297 renamed = renamed[0] | |
298 | |
299 data = fctx.data() | |
300 if f == '.hgtags': | |
301 newdata = [] | |
302 for line in data.splitlines(): | |
303 id, name = line.split(' ', 1) | |
304 newdata.append('%s %s\n' % (node.hex(revmap[node.bin(id)]), | |
305 name)) | |
306 data = ''.join(newdata) | |
307 return context.memfilectx(f, data, 'l' in fctx.flags(), | |
308 'x' in fctx.flags(), renamed) | |
309 | |
310 # Commit | |
311 mctx = context.memctx(rdst, parents, ctx.description(), dstfiles, | |
312 getfilectx, ctx.user(), ctx.date(), ctx.extra()) | |
313 ret = rdst.commitctx(mctx) | |
314 rdst.dirstate.setparents(ret) | |
315 revmap[ctx.node()] = rdst.changelog.tip() | |
316 | |
317 def _islfile(file, ctx, matcher, size): | |
318 ''' | |
319 A file is a lfile if it matches a pattern or is over | |
320 the given size. | |
321 ''' | |
322 # Never store hgtags or hgignore as lfiles | |
323 if file == '.hgtags' or file == '.hgignore' or file == '.hgsigs': | |
324 return False | |
325 if matcher and matcher(file): | |
326 return True | |
327 try: | |
328 return ctx.filectx(file).size() >= size * 1024 * 1024 | |
329 except error.LookupError: | |
330 return False | |
331 | |
332 def uploadlfiles(ui, rsrc, rdst, files): | |
333 '''upload largefiles to the central store''' | |
334 | |
335 # Don't upload locally. All largefiles are in the system wide cache | |
336 # so the other repo can just get them from there. | |
337 if not files or rdst.local(): | |
338 return | |
339 | |
340 store = basestore._openstore(rsrc, rdst, put=True) | |
341 | |
342 at = 0 | |
343 files = filter(lambda h: not store.exists(h), files) | |
344 for hash in files: | |
15170
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15168
diff
changeset
|
345 ui.progress(_('uploading largefiles'), at, unit='largefile', |
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15168
diff
changeset
|
346 total=len(files)) |
15168 | 347 source = lfutil.findfile(rsrc, hash) |
348 if not source: | |
15170
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15168
diff
changeset
|
349 raise util.Abort(_('Missing largefile %s needs to be uploaded') |
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15168
diff
changeset
|
350 % hash) |
15168 | 351 # XXX check for errors here |
352 store.put(source, hash) | |
353 at += 1 | |
354 ui.progress('uploading largefiles', None) | |
355 | |
356 def verifylfiles(ui, repo, all=False, contents=False): | |
357 '''Verify that every big file revision in the current changeset | |
358 exists in the central store. With --contents, also verify that | |
359 the contents of each big file revision are correct (SHA-1 hash | |
360 matches the revision ID). With --all, check every changeset in | |
361 this repository.''' | |
362 if all: | |
363 # Pass a list to the function rather than an iterator because we know a | |
364 # list will work. | |
365 revs = range(len(repo)) | |
366 else: | |
367 revs = ['.'] | |
368 | |
369 store = basestore._openstore(repo) | |
370 return store.verify(revs, contents=contents) | |
371 | |
372 def cachelfiles(ui, repo, node): | |
373 '''cachelfiles ensures that all largefiles needed by the specified revision | |
374 are present in the repository's largefile cache. | |
375 | |
376 returns a tuple (cached, missing). cached is the list of files downloaded | |
377 by this operation; missing is the list of files that were needed but could | |
378 not be found.''' | |
379 lfiles = lfutil.listlfiles(repo, node) | |
380 toget = [] | |
381 | |
382 for lfile in lfiles: | |
383 expectedhash = repo[node][lfutil.standin(lfile)].data().strip() | |
384 # if it exists and its hash matches, it might have been locally | |
385 # modified before updating and the user chose 'local'. in this case, | |
386 # it will not be in any store, so don't look for it. | |
387 if (not os.path.exists(repo.wjoin(lfile)) \ | |
388 or expectedhash != lfutil.hashfile(repo.wjoin(lfile))) and \ | |
389 not lfutil.findfile(repo, expectedhash): | |
390 toget.append((lfile, expectedhash)) | |
391 | |
392 if toget: | |
393 store = basestore._openstore(repo) | |
394 ret = store.get(toget) | |
395 return ret | |
396 | |
397 return ([], []) | |
398 | |
399 def updatelfiles(ui, repo, filelist=None, printmessage=True): | |
400 wlock = repo.wlock() | |
401 try: | |
402 lfdirstate = lfutil.openlfdirstate(ui, repo) | |
403 lfiles = set(lfutil.listlfiles(repo)) | set(lfdirstate) | |
404 | |
405 if filelist is not None: | |
406 lfiles = [f for f in lfiles if f in filelist] | |
407 | |
408 printed = False | |
409 if printmessage and lfiles: | |
410 ui.status(_('getting changed largefiles\n')) | |
411 printed = True | |
412 cachelfiles(ui, repo, '.') | |
413 | |
414 updated, removed = 0, 0 | |
415 for i in map(lambda f: _updatelfile(repo, lfdirstate, f), lfiles): | |
416 # increment the appropriate counter according to _updatelfile's | |
417 # return value | |
418 updated += i > 0 and i or 0 | |
419 removed -= i < 0 and i or 0 | |
420 if printmessage and (removed or updated) and not printed: | |
421 ui.status(_('getting changed largefiles\n')) | |
422 printed = True | |
423 | |
424 lfdirstate.write() | |
425 if printed and printmessage: | |
426 ui.status(_('%d largefiles updated, %d removed\n') % (updated, | |
427 removed)) | |
428 finally: | |
429 wlock.release() | |
430 | |
431 def _updatelfile(repo, lfdirstate, lfile): | |
432 '''updates a single largefile and copies the state of its standin from | |
433 the repository's dirstate to its state in the lfdirstate. | |
434 | |
435 returns 1 if the file was modified, -1 if the file was removed, 0 if the | |
436 file was unchanged, and None if the needed largefile was missing from the | |
437 cache.''' | |
438 ret = 0 | |
439 abslfile = repo.wjoin(lfile) | |
440 absstandin = repo.wjoin(lfutil.standin(lfile)) | |
441 if os.path.exists(absstandin): | |
442 if os.path.exists(absstandin+'.orig'): | |
443 shutil.copyfile(abslfile, abslfile+'.orig') | |
444 expecthash = lfutil.readstandin(repo, lfile) | |
445 if expecthash != '' and \ | |
446 (not os.path.exists(abslfile) or \ | |
447 expecthash != lfutil.hashfile(abslfile)): | |
448 if not lfutil.copyfromcache(repo, expecthash, lfile): | |
449 return None # don't try to set the mode or update the dirstate | |
450 ret = 1 | |
451 mode = os.stat(absstandin).st_mode | |
452 if mode != os.stat(abslfile).st_mode: | |
453 os.chmod(abslfile, mode) | |
454 ret = 1 | |
455 else: | |
456 if os.path.exists(abslfile): | |
457 os.unlink(abslfile) | |
458 ret = -1 | |
459 state = repo.dirstate[lfutil.standin(lfile)] | |
460 if state == 'n': | |
461 lfdirstate.normal(lfile) | |
462 elif state == 'r': | |
463 lfdirstate.remove(lfile) | |
464 elif state == 'a': | |
465 lfdirstate.add(lfile) | |
466 elif state == '?': | |
467 try: | |
468 # Mercurial >= 1.9 | |
469 lfdirstate.drop(lfile) | |
470 except AttributeError: | |
471 # Mercurial <= 1.8 | |
472 lfdirstate.forget(lfile) | |
473 return ret | |
474 | |
475 # -- hg commands declarations ------------------------------------------------ | |
476 | |
477 | |
478 cmdtable = { | |
479 'lfconvert': (lfconvert, | |
480 [('s', 'size', 0, 'All files over this size (in megabytes) ' | |
15170
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15168
diff
changeset
|
481 'will be considered largefiles. This can also be specified ' |
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15168
diff
changeset
|
482 'in your hgrc as [largefiles].size.'), |
15168 | 483 ('','tonormal',False, |
484 'Convert from a largefiles repo to a normal repo')], | |
485 _('hg lfconvert SOURCE DEST [FILE ...]')), | |
486 } |