author | Eli Carter <eli.carter@tektronix.com> |
Tue, 18 Oct 2011 16:40:59 -0500 | |
branch | stable |
changeset 15303 | 07811b3b119b |
parent 15255 | 7ab05d752405 |
child 15313 | 3eb1a90ea409 |
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 |
||
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15230
diff
changeset
|
9 |
'''High-level command function for lfconvert, plus the cmdtable.''' |
15168 | 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): |
|
15230 | 23 |
'''convert a normal repository to a largefiles repository |
15168 | 24 |
|
15230 | 25 |
Convert repository SOURCE to a new repository DEST, identical to |
26 |
SOURCE except that certain files will be converted as largefiles: |
|
27 |
specifically, any file that matches any PATTERN *or* whose size is |
|
28 |
above the minimum size threshold is converted as a largefile. The |
|
29 |
size used to determine whether or not to track a file as a |
|
30 |
largefile is the size of the first version of the file. The |
|
31 |
minimum size can be specified either with --size or in |
|
32 |
configuration as ``largefiles.size``. |
|
33 |
||
34 |
After running this command you will need to make sure that |
|
35 |
largefiles is enabled anywhere you intend to push the new |
|
36 |
repository. |
|
37 |
||
38 |
Use --tonormal to convert largefiles back to normal files; after |
|
39 |
this, the DEST repository can be used without largefiles at all.''' |
|
15168 | 40 |
|
41 |
if opts['tonormal']: |
|
42 |
tolfile = False |
|
43 |
else: |
|
44 |
tolfile = True |
|
15227
a7686abf73a6
largefiles: factor out lfutil.getminsize()
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
45 |
size = lfutil.getminsize(ui, True, opts.get('size'), default=None) |
15168 | 46 |
try: |
47 |
rsrc = hg.repository(ui, src) |
|
48 |
if not rsrc.local(): |
|
49 |
raise util.Abort(_('%s is not a local Mercurial repo') % src) |
|
50 |
except error.RepoError, err: |
|
51 |
ui.traceback() |
|
52 |
raise util.Abort(err.args[0]) |
|
53 |
if os.path.exists(dest): |
|
54 |
if not os.path.isdir(dest): |
|
55 |
raise util.Abort(_('destination %s already exists') % dest) |
|
56 |
elif os.listdir(dest): |
|
57 |
raise util.Abort(_('destination %s is not empty') % dest) |
|
58 |
try: |
|
59 |
ui.status(_('initializing destination %s\n') % dest) |
|
60 |
rdst = hg.repository(ui, dest, create=True) |
|
61 |
if not rdst.local(): |
|
62 |
raise util.Abort(_('%s is not a local Mercurial repo') % dest) |
|
63 |
except error.RepoError: |
|
64 |
ui.traceback() |
|
65 |
raise util.Abort(_('%s is not a repo') % dest) |
|
66 |
||
15171
547da6115d1d
largefiles: eliminate naked exceptions
Matt Mackall <mpm@selenic.com>
parents:
15170
diff
changeset
|
67 |
success = False |
15168 | 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))) |
|
15171
547da6115d1d
largefiles: eliminate naked exceptions
Matt Mackall <mpm@selenic.com>
parents:
15170
diff
changeset
|
109 |
except OSError: |
15168 | 110 |
pass |
111 |
||
15303
07811b3b119b
largefiles: include 'largefiles' in converted repository requirements
Eli Carter <eli.carter@tektronix.com>
parents:
15255
diff
changeset
|
112 |
# If there were any files converted to largefiles, add largefiles |
07811b3b119b
largefiles: include 'largefiles' in converted repository requirements
Eli Carter <eli.carter@tektronix.com>
parents:
15255
diff
changeset
|
113 |
# to the destination repository's requirements. |
07811b3b119b
largefiles: include 'largefiles' in converted repository requirements
Eli Carter <eli.carter@tektronix.com>
parents:
15255
diff
changeset
|
114 |
if lfiles: |
07811b3b119b
largefiles: include 'largefiles' in converted repository requirements
Eli Carter <eli.carter@tektronix.com>
parents:
15255
diff
changeset
|
115 |
rdst.requirements.add('largefiles') |
07811b3b119b
largefiles: include 'largefiles' in converted repository requirements
Eli Carter <eli.carter@tektronix.com>
parents:
15255
diff
changeset
|
116 |
rdst._writerequirements() |
15168 | 117 |
else: |
118 |
for ctx in ctxs: |
|
119 |
ui.progress(_('converting revisions'), ctx.rev(), |
|
120 |
unit=_('revision'), total=rsrc['tip'].rev()) |
|
121 |
_addchangeset(ui, rsrc, rdst, ctx, revmap) |
|
122 |
||
123 |
ui.progress(_('converting revisions'), None) |
|
15171
547da6115d1d
largefiles: eliminate naked exceptions
Matt Mackall <mpm@selenic.com>
parents:
15170
diff
changeset
|
124 |
success = True |
15168 | 125 |
finally: |
15171
547da6115d1d
largefiles: eliminate naked exceptions
Matt Mackall <mpm@selenic.com>
parents:
15170
diff
changeset
|
126 |
if not success: |
547da6115d1d
largefiles: eliminate naked exceptions
Matt Mackall <mpm@selenic.com>
parents:
15170
diff
changeset
|
127 |
# we failed, remove the new directory |
547da6115d1d
largefiles: eliminate naked exceptions
Matt Mackall <mpm@selenic.com>
parents:
15170
diff
changeset
|
128 |
shutil.rmtree(rdst.root) |
15168 | 129 |
dst_lock.release() |
130 |
||
131 |
def _addchangeset(ui, rsrc, rdst, ctx, revmap): |
|
132 |
# Convert src parents to dst parents |
|
133 |
parents = [] |
|
134 |
for p in ctx.parents(): |
|
135 |
parents.append(revmap[p.node()]) |
|
136 |
while len(parents) < 2: |
|
137 |
parents.append(node.nullid) |
|
138 |
||
139 |
# Generate list of changed files |
|
140 |
files = set(ctx.files()) |
|
141 |
if node.nullid not in parents: |
|
142 |
mc = ctx.manifest() |
|
143 |
mp1 = ctx.parents()[0].manifest() |
|
144 |
mp2 = ctx.parents()[1].manifest() |
|
145 |
files |= (set(mp1) | set(mp2)) - set(mc) |
|
146 |
for f in mc: |
|
147 |
if mc[f] != mp1.get(f, None) or mc[f] != mp2.get(f, None): |
|
148 |
files.add(f) |
|
149 |
||
150 |
def getfilectx(repo, memctx, f): |
|
151 |
if lfutil.standin(f) in files: |
|
152 |
# if the file isn't in the manifest then it was removed |
|
153 |
# or renamed, raise IOError to indicate this |
|
154 |
try: |
|
155 |
fctx = ctx.filectx(lfutil.standin(f)) |
|
156 |
except error.LookupError: |
|
157 |
raise IOError() |
|
158 |
renamed = fctx.renamed() |
|
159 |
if renamed: |
|
160 |
renamed = lfutil.splitstandin(renamed[0]) |
|
161 |
||
162 |
hash = fctx.data().strip() |
|
163 |
path = lfutil.findfile(rsrc, hash) |
|
164 |
### TODO: What if the file is not cached? |
|
165 |
data = '' |
|
166 |
fd = None |
|
167 |
try: |
|
168 |
fd = open(path, 'rb') |
|
169 |
data = fd.read() |
|
170 |
finally: |
|
15172
fb1dcd2aae2a
largefiles: fix multistatement line
Matt Mackall <mpm@selenic.com>
parents:
15171
diff
changeset
|
171 |
if fd: |
fb1dcd2aae2a
largefiles: fix multistatement line
Matt Mackall <mpm@selenic.com>
parents:
15171
diff
changeset
|
172 |
fd.close() |
15168 | 173 |
return context.memfilectx(f, data, 'l' in fctx.flags(), |
174 |
'x' in fctx.flags(), renamed) |
|
175 |
else: |
|
176 |
try: |
|
177 |
fctx = ctx.filectx(f) |
|
178 |
except error.LookupError: |
|
179 |
raise IOError() |
|
180 |
renamed = fctx.renamed() |
|
181 |
if renamed: |
|
182 |
renamed = renamed[0] |
|
183 |
data = fctx.data() |
|
184 |
if f == '.hgtags': |
|
185 |
newdata = [] |
|
186 |
for line in data.splitlines(): |
|
187 |
id, name = line.split(' ', 1) |
|
188 |
newdata.append('%s %s\n' % (node.hex(revmap[node.bin(id)]), |
|
189 |
name)) |
|
190 |
data = ''.join(newdata) |
|
191 |
return context.memfilectx(f, data, 'l' in fctx.flags(), |
|
192 |
'x' in fctx.flags(), renamed) |
|
193 |
||
194 |
dstfiles = [] |
|
195 |
for file in files: |
|
196 |
if lfutil.isstandin(file): |
|
197 |
dstfiles.append(lfutil.splitstandin(file)) |
|
198 |
else: |
|
199 |
dstfiles.append(file) |
|
200 |
# Commit |
|
201 |
mctx = context.memctx(rdst, parents, ctx.description(), dstfiles, |
|
202 |
getfilectx, ctx.user(), ctx.date(), ctx.extra()) |
|
203 |
ret = rdst.commitctx(mctx) |
|
204 |
rdst.dirstate.setparents(ret) |
|
205 |
revmap[ctx.node()] = rdst.changelog.tip() |
|
206 |
||
207 |
def _lfconvert_addchangeset(rsrc, rdst, ctx, revmap, lfiles, normalfiles, |
|
208 |
matcher, size, lfiletohash): |
|
209 |
# Convert src parents to dst parents |
|
210 |
parents = [] |
|
211 |
for p in ctx.parents(): |
|
212 |
parents.append(revmap[p.node()]) |
|
213 |
while len(parents) < 2: |
|
214 |
parents.append(node.nullid) |
|
215 |
||
216 |
# Generate list of changed files |
|
217 |
files = set(ctx.files()) |
|
218 |
if node.nullid not in parents: |
|
219 |
mc = ctx.manifest() |
|
220 |
mp1 = ctx.parents()[0].manifest() |
|
221 |
mp2 = ctx.parents()[1].manifest() |
|
222 |
files |= (set(mp1) | set(mp2)) - set(mc) |
|
223 |
for f in mc: |
|
224 |
if mc[f] != mp1.get(f, None) or mc[f] != mp2.get(f, None): |
|
225 |
files.add(f) |
|
226 |
||
227 |
dstfiles = [] |
|
228 |
for f in files: |
|
229 |
if f not in lfiles and f not in normalfiles: |
|
230 |
islfile = _islfile(f, ctx, matcher, size) |
|
231 |
# If this file was renamed or copied then copy |
|
232 |
# the lfileness of its predecessor |
|
233 |
if f in ctx.manifest(): |
|
234 |
fctx = ctx.filectx(f) |
|
235 |
renamed = fctx.renamed() |
|
236 |
renamedlfile = renamed and renamed[0] in lfiles |
|
237 |
islfile |= renamedlfile |
|
238 |
if 'l' in fctx.flags(): |
|
239 |
if renamedlfile: |
|
240 |
raise util.Abort( |
|
15170
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15168
diff
changeset
|
241 |
_('Renamed/copied largefile %s becomes symlink') |
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15168
diff
changeset
|
242 |
% f) |
15168 | 243 |
islfile = False |
244 |
if islfile: |
|
245 |
lfiles.add(f) |
|
246 |
else: |
|
247 |
normalfiles.add(f) |
|
248 |
||
249 |
if f in lfiles: |
|
250 |
dstfiles.append(lfutil.standin(f)) |
|
15254
dd03d3a9f888
largefiles: more work on cleaning up comments
Greg Ward <greg@gerg.ca>
parents:
15253
diff
changeset
|
251 |
# largefile in manifest if it has not been removed/renamed |
15168 | 252 |
if f in ctx.manifest(): |
253 |
if 'l' in ctx.filectx(f).flags(): |
|
254 |
if renamed and renamed[0] in lfiles: |
|
255 |
raise util.Abort(_('largefile %s becomes symlink') % f) |
|
256 |
||
15254
dd03d3a9f888
largefiles: more work on cleaning up comments
Greg Ward <greg@gerg.ca>
parents:
15253
diff
changeset
|
257 |
# largefile was modified, update standins |
15168 | 258 |
fullpath = rdst.wjoin(f) |
259 |
lfutil.createdir(os.path.dirname(fullpath)) |
|
260 |
m = util.sha1('') |
|
261 |
m.update(ctx[f].data()) |
|
262 |
hash = m.hexdigest() |
|
263 |
if f not in lfiletohash or lfiletohash[f] != hash: |
|
264 |
try: |
|
265 |
fd = open(fullpath, 'wb') |
|
266 |
fd.write(ctx[f].data()) |
|
267 |
finally: |
|
268 |
if fd: |
|
269 |
fd.close() |
|
270 |
executable = 'x' in ctx[f].flags() |
|
271 |
os.chmod(fullpath, lfutil.getmode(executable)) |
|
272 |
lfutil.writestandin(rdst, lfutil.standin(f), hash, |
|
273 |
executable) |
|
274 |
lfiletohash[f] = hash |
|
275 |
else: |
|
276 |
# normal file |
|
277 |
dstfiles.append(f) |
|
278 |
||
279 |
def getfilectx(repo, memctx, f): |
|
280 |
if lfutil.isstandin(f): |
|
281 |
# if the file isn't in the manifest then it was removed |
|
282 |
# or renamed, raise IOError to indicate this |
|
283 |
srcfname = lfutil.splitstandin(f) |
|
284 |
try: |
|
285 |
fctx = ctx.filectx(srcfname) |
|
286 |
except error.LookupError: |
|
287 |
raise IOError() |
|
288 |
renamed = fctx.renamed() |
|
289 |
if renamed: |
|
15254
dd03d3a9f888
largefiles: more work on cleaning up comments
Greg Ward <greg@gerg.ca>
parents:
15253
diff
changeset
|
290 |
# standin is always a largefile because largefile-ness |
15168 | 291 |
# doesn't change after rename or copy |
292 |
renamed = lfutil.standin(renamed[0]) |
|
293 |
||
294 |
return context.memfilectx(f, lfiletohash[srcfname], 'l' in |
|
295 |
fctx.flags(), 'x' in fctx.flags(), renamed) |
|
296 |
else: |
|
297 |
try: |
|
298 |
fctx = ctx.filectx(f) |
|
299 |
except error.LookupError: |
|
300 |
raise IOError() |
|
301 |
renamed = fctx.renamed() |
|
302 |
if renamed: |
|
303 |
renamed = renamed[0] |
|
304 |
||
305 |
data = fctx.data() |
|
306 |
if f == '.hgtags': |
|
307 |
newdata = [] |
|
308 |
for line in data.splitlines(): |
|
309 |
id, name = line.split(' ', 1) |
|
310 |
newdata.append('%s %s\n' % (node.hex(revmap[node.bin(id)]), |
|
311 |
name)) |
|
312 |
data = ''.join(newdata) |
|
313 |
return context.memfilectx(f, data, 'l' in fctx.flags(), |
|
314 |
'x' in fctx.flags(), renamed) |
|
315 |
||
316 |
# Commit |
|
317 |
mctx = context.memctx(rdst, parents, ctx.description(), dstfiles, |
|
318 |
getfilectx, ctx.user(), ctx.date(), ctx.extra()) |
|
319 |
ret = rdst.commitctx(mctx) |
|
320 |
rdst.dirstate.setparents(ret) |
|
321 |
revmap[ctx.node()] = rdst.changelog.tip() |
|
322 |
||
323 |
def _islfile(file, ctx, matcher, size): |
|
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15230
diff
changeset
|
324 |
'''Return true if file should be considered a largefile, i.e. |
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15230
diff
changeset
|
325 |
matcher matches it or it is larger than size.''' |
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15230
diff
changeset
|
326 |
# never store special .hg* files as largefiles |
15168 | 327 |
if file == '.hgtags' or file == '.hgignore' or file == '.hgsigs': |
328 |
return False |
|
329 |
if matcher and matcher(file): |
|
330 |
return True |
|
331 |
try: |
|
332 |
return ctx.filectx(file).size() >= size * 1024 * 1024 |
|
333 |
except error.LookupError: |
|
334 |
return False |
|
335 |
||
336 |
def uploadlfiles(ui, rsrc, rdst, files): |
|
337 |
'''upload largefiles to the central store''' |
|
338 |
||
339 |
# Don't upload locally. All largefiles are in the system wide cache |
|
340 |
# so the other repo can just get them from there. |
|
341 |
if not files or rdst.local(): |
|
342 |
return |
|
343 |
||
344 |
store = basestore._openstore(rsrc, rdst, put=True) |
|
345 |
||
346 |
at = 0 |
|
347 |
files = filter(lambda h: not store.exists(h), files) |
|
348 |
for hash in files: |
|
15170
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15168
diff
changeset
|
349 |
ui.progress(_('uploading largefiles'), at, unit='largefile', |
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15168
diff
changeset
|
350 |
total=len(files)) |
15168 | 351 |
source = lfutil.findfile(rsrc, hash) |
352 |
if not source: |
|
15253
67d010779907
largefiles: improve error reporting
Greg Ward <greg@gerg.ca>
parents:
15252
diff
changeset
|
353 |
raise util.Abort(_('largefile %s missing from store' |
67d010779907
largefiles: improve error reporting
Greg Ward <greg@gerg.ca>
parents:
15252
diff
changeset
|
354 |
' (needs to be uploaded)') % hash) |
15168 | 355 |
# XXX check for errors here |
356 |
store.put(source, hash) |
|
357 |
at += 1 |
|
15173
3d27a8ff895f
largefiles: mark a string for translation
Matt Mackall <mpm@selenic.com>
parents:
15172
diff
changeset
|
358 |
ui.progress(_('uploading largefiles'), None) |
15168 | 359 |
|
360 |
def verifylfiles(ui, repo, all=False, contents=False): |
|
361 |
'''Verify that every big file revision in the current changeset |
|
362 |
exists in the central store. With --contents, also verify that |
|
363 |
the contents of each big file revision are correct (SHA-1 hash |
|
364 |
matches the revision ID). With --all, check every changeset in |
|
365 |
this repository.''' |
|
366 |
if all: |
|
367 |
# Pass a list to the function rather than an iterator because we know a |
|
368 |
# list will work. |
|
369 |
revs = range(len(repo)) |
|
370 |
else: |
|
371 |
revs = ['.'] |
|
372 |
||
373 |
store = basestore._openstore(repo) |
|
374 |
return store.verify(revs, contents=contents) |
|
375 |
||
376 |
def cachelfiles(ui, repo, node): |
|
377 |
'''cachelfiles ensures that all largefiles needed by the specified revision |
|
378 |
are present in the repository's largefile cache. |
|
379 |
||
380 |
returns a tuple (cached, missing). cached is the list of files downloaded |
|
381 |
by this operation; missing is the list of files that were needed but could |
|
382 |
not be found.''' |
|
383 |
lfiles = lfutil.listlfiles(repo, node) |
|
384 |
toget = [] |
|
385 |
||
386 |
for lfile in lfiles: |
|
387 |
expectedhash = repo[node][lfutil.standin(lfile)].data().strip() |
|
388 |
# if it exists and its hash matches, it might have been locally |
|
389 |
# modified before updating and the user chose 'local'. in this case, |
|
390 |
# it will not be in any store, so don't look for it. |
|
15255
7ab05d752405
largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents:
15254
diff
changeset
|
391 |
if ((not os.path.exists(repo.wjoin(lfile)) or |
7ab05d752405
largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents:
15254
diff
changeset
|
392 |
expectedhash != lfutil.hashfile(repo.wjoin(lfile))) and |
7ab05d752405
largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents:
15254
diff
changeset
|
393 |
not lfutil.findfile(repo, expectedhash)): |
15168 | 394 |
toget.append((lfile, expectedhash)) |
395 |
||
396 |
if toget: |
|
397 |
store = basestore._openstore(repo) |
|
398 |
ret = store.get(toget) |
|
399 |
return ret |
|
400 |
||
401 |
return ([], []) |
|
402 |
||
403 |
def updatelfiles(ui, repo, filelist=None, printmessage=True): |
|
404 |
wlock = repo.wlock() |
|
405 |
try: |
|
406 |
lfdirstate = lfutil.openlfdirstate(ui, repo) |
|
407 |
lfiles = set(lfutil.listlfiles(repo)) | set(lfdirstate) |
|
408 |
||
409 |
if filelist is not None: |
|
410 |
lfiles = [f for f in lfiles if f in filelist] |
|
411 |
||
412 |
printed = False |
|
413 |
if printmessage and lfiles: |
|
414 |
ui.status(_('getting changed largefiles\n')) |
|
415 |
printed = True |
|
416 |
cachelfiles(ui, repo, '.') |
|
417 |
||
418 |
updated, removed = 0, 0 |
|
419 |
for i in map(lambda f: _updatelfile(repo, lfdirstate, f), lfiles): |
|
420 |
# increment the appropriate counter according to _updatelfile's |
|
421 |
# return value |
|
422 |
updated += i > 0 and i or 0 |
|
423 |
removed -= i < 0 and i or 0 |
|
424 |
if printmessage and (removed or updated) and not printed: |
|
425 |
ui.status(_('getting changed largefiles\n')) |
|
426 |
printed = True |
|
427 |
||
428 |
lfdirstate.write() |
|
429 |
if printed and printmessage: |
|
430 |
ui.status(_('%d largefiles updated, %d removed\n') % (updated, |
|
431 |
removed)) |
|
432 |
finally: |
|
433 |
wlock.release() |
|
434 |
||
435 |
def _updatelfile(repo, lfdirstate, lfile): |
|
436 |
'''updates a single largefile and copies the state of its standin from |
|
437 |
the repository's dirstate to its state in the lfdirstate. |
|
438 |
||
439 |
returns 1 if the file was modified, -1 if the file was removed, 0 if the |
|
440 |
file was unchanged, and None if the needed largefile was missing from the |
|
441 |
cache.''' |
|
442 |
ret = 0 |
|
443 |
abslfile = repo.wjoin(lfile) |
|
444 |
absstandin = repo.wjoin(lfutil.standin(lfile)) |
|
445 |
if os.path.exists(absstandin): |
|
446 |
if os.path.exists(absstandin+'.orig'): |
|
447 |
shutil.copyfile(abslfile, abslfile+'.orig') |
|
448 |
expecthash = lfutil.readstandin(repo, lfile) |
|
15255
7ab05d752405
largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents:
15254
diff
changeset
|
449 |
if (expecthash != '' and |
7ab05d752405
largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents:
15254
diff
changeset
|
450 |
(not os.path.exists(abslfile) or |
7ab05d752405
largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents:
15254
diff
changeset
|
451 |
expecthash != lfutil.hashfile(abslfile))): |
15168 | 452 |
if not lfutil.copyfromcache(repo, expecthash, lfile): |
453 |
return None # don't try to set the mode or update the dirstate |
|
454 |
ret = 1 |
|
455 |
mode = os.stat(absstandin).st_mode |
|
456 |
if mode != os.stat(abslfile).st_mode: |
|
457 |
os.chmod(abslfile, mode) |
|
458 |
ret = 1 |
|
459 |
else: |
|
460 |
if os.path.exists(abslfile): |
|
461 |
os.unlink(abslfile) |
|
462 |
ret = -1 |
|
463 |
state = repo.dirstate[lfutil.standin(lfile)] |
|
464 |
if state == 'n': |
|
465 |
lfdirstate.normal(lfile) |
|
466 |
elif state == 'r': |
|
467 |
lfdirstate.remove(lfile) |
|
468 |
elif state == 'a': |
|
469 |
lfdirstate.add(lfile) |
|
470 |
elif state == '?': |
|
15224
7c604d8c7e83
largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents:
15173
diff
changeset
|
471 |
lfdirstate.drop(lfile) |
15168 | 472 |
return ret |
473 |
||
474 |
# -- hg commands declarations ------------------------------------------------ |
|
475 |
||
476 |
cmdtable = { |
|
477 |
'lfconvert': (lfconvert, |
|
15230 | 478 |
[('s', 'size', '', |
479 |
_('minimum size (MB) for files to be converted ' |
|
480 |
'as largefiles'), |
|
481 |
'SIZE'), |
|
482 |
('', 'tonormal', False, |
|
483 |
_('convert from a largefiles repo to a normal repo')), |
|
484 |
], |
|
15168 | 485 |
_('hg lfconvert SOURCE DEST [FILE ...]')), |
486 |
} |