Show More
@@ -1,40 +1,94 b'' | |||
|
1 | 1 | # Copyright 2009-2010 Gregory P. Ward |
|
2 | 2 | # Copyright 2009-2010 Intelerad Medical Systems Incorporated |
|
3 | 3 | # Copyright 2010-2011 Fog Creek Software |
|
4 | 4 | # Copyright 2010-2011 Unity Technologies |
|
5 | 5 | # |
|
6 | 6 | # This software may be used and distributed according to the terms of the |
|
7 | 7 | # GNU General Public License version 2 or any later version. |
|
8 | 8 | |
|
9 | 9 | '''track large binary files |
|
10 | 10 | |
|
11 |
Large binary files tend to be not very compressible, not very |
|
|
12 |
not at all mergeable. |
|
|
13 | format (revlog), which is based on compressed binary deltas. largefiles solves | |
|
14 | this problem by adding a centralized client-server layer on top of Mercurial: | |
|
15 | largefiles live in a *central store* out on the network somewhere, and you only | |
|
16 | fetch the ones that you need when you need them. | |
|
11 | Large binary files tend to be not very compressible, not very | |
|
12 | diffable, and not at all mergeable. Such files are not handled | |
|
13 | efficiently by Mercurial's storage format (revlog), which is based on | |
|
14 | compressed binary deltas; storing large binary files as regular | |
|
15 | Mercurial files wastes bandwidth and disk space and increases | |
|
16 | Mercurial's memory usage. The largefiles extension addresses these | |
|
17 | problems by adding a centralized client-server layer on top of | |
|
18 | Mercurial: largefiles live in a *central store* out on the network | |
|
19 | somewhere, and you only fetch the revisions that you need when you | |
|
20 | need them. | |
|
21 | ||
|
22 | largefiles works by maintaining a "standin file" in .hglf/ for each | |
|
23 | largefile. The standins are small (41 bytes: an SHA-1 hash plus | |
|
24 | newline) and are tracked by Mercurial. Largefile revisions are | |
|
25 | identified by the SHA-1 hash of their contents, which is written to | |
|
26 | the standin. largefiles uses that revision ID to get/put largefile | |
|
27 | revisions from/to the central store. This saves both disk space and | |
|
28 | bandwidth, since you don't need to retrieve all historical revisions | |
|
29 | of large files when you clone or pull. | |
|
30 | ||
|
31 | To start a new repository or add new large binary files, just add | |
|
32 | --large to your ``hg add`` command. For example:: | |
|
33 | ||
|
34 | $ dd if=/dev/urandom of=randomdata count=2000 | |
|
35 | $ hg add --large randomdata | |
|
36 | $ hg commit -m 'add randomdata as a largefile' | |
|
37 | ||
|
38 | When you push a changeset that adds/modifies largefiles to a remote | |
|
39 | repository, its largefile revisions will be uploaded along with it. | |
|
40 | Note that the remote Mercurial must also have the largefiles extension | |
|
41 | enabled for this to work. | |
|
17 | 42 | |
|
18 | largefiles works by maintaining a *standin* in .hglf/ for each largefile. The | |
|
19 | standins are small (41 bytes: an SHA-1 hash plus newline) and are tracked by | |
|
20 | Mercurial. Largefile revisions are identified by the SHA-1 hash of their | |
|
21 | contents, which is written to the standin. largefiles uses that revision ID to | |
|
22 | get/put largefile revisions from/to the central store. | |
|
43 | When you pull a changeset that affects largefiles from a remote | |
|
44 | repository, Mercurial behaves as normal. However, when you update to | |
|
45 | such a revision, any largefiles needed by that revision are downloaded | |
|
46 | and cached (if they have never been downloaded before). This means | |
|
47 | that network access may be required to update to changesets you have | |
|
48 | not previously updated to. | |
|
49 | ||
|
50 | If you already have large files tracked by Mercurial without the | |
|
51 | largefiles extension, you will need to convert your repository in | |
|
52 | order to benefit from largefiles. This is done with the 'hg lfconvert' | |
|
53 | command:: | |
|
54 | ||
|
55 | $ hg lfconvert --size 10 oldrepo newrepo | |
|
23 | 56 | |
|
24 | A complete tutorial for using lfiles is included in ``usage.txt`` in the lfiles | |
|
25 | source distribution. See | |
|
26 | https://developers.kilnhg.com/Repo/Kiln/largefiles/largefiles/File/usage.txt | |
|
57 | In repositories that already have largefiles in them, any new file | |
|
58 | over 10MB will automatically be added as a largefile. To change this | |
|
59 | threshhold, set ``largefiles.size`` in your Mercurial config file to | |
|
60 | the minimum size in megabytes to track as a largefile, or use the | |
|
61 | --lfsize option to the add command (also in megabytes):: | |
|
62 | ||
|
63 | [largefiles] | |
|
64 | size = 2 XXX wouldn't minsize be a better name? | |
|
65 | ||
|
66 | $ hg add --lfsize 2 | |
|
67 | ||
|
68 | The ``largefiles.patterns`` config option allows you to specify a list | |
|
69 | of filename patterns (see ``hg help patterns``) that should always be | |
|
70 | tracked as largefiles:: | |
|
71 | ||
|
72 | [largefiles] | |
|
73 | patterns = | |
|
74 | *.jpg | |
|
75 | re:.*\.(png|bmp)$ | |
|
76 | library.zip | |
|
77 | content/audio/* | |
|
78 | ||
|
79 | Files that match one of these patterns will be added as largefiles | |
|
80 | regardless of their size. | |
|
27 | 81 | ''' |
|
28 | 82 | |
|
29 | 83 | from mercurial import commands |
|
30 | 84 | |
|
31 | 85 | import lfcommands |
|
32 | 86 | import reposetup |
|
33 | 87 | import uisetup |
|
34 | 88 | |
|
35 | 89 | reposetup = reposetup.reposetup |
|
36 | 90 | uisetup = uisetup.uisetup |
|
37 | 91 | |
|
38 | 92 | commands.norepo += " lfconvert" |
|
39 | 93 | |
|
40 | 94 | cmdtable = lfcommands.cmdtable |
@@ -1,473 +1,484 b'' | |||
|
1 | 1 | # Copyright 2009-2010 Gregory P. Ward |
|
2 | 2 | # Copyright 2009-2010 Intelerad Medical Systems Incorporated |
|
3 | 3 | # Copyright 2010-2011 Fog Creek Software |
|
4 | 4 | # Copyright 2010-2011 Unity Technologies |
|
5 | 5 | # |
|
6 | 6 | # This software may be used and distributed according to the terms of the |
|
7 | 7 | # GNU General Public License version 2 or any later version. |
|
8 | 8 | |
|
9 | 9 | '''High-level command functions: lfadd() et. al, plus the cmdtable.''' |
|
10 | 10 | |
|
11 | 11 | import os |
|
12 | 12 | import shutil |
|
13 | 13 | |
|
14 | 14 | from mercurial import util, match as match_, hg, node, context, error |
|
15 | 15 | from mercurial.i18n import _ |
|
16 | 16 | |
|
17 | 17 | import lfutil |
|
18 | 18 | import basestore |
|
19 | 19 | |
|
20 | 20 | # -- Commands ---------------------------------------------------------- |
|
21 | 21 | |
|
22 | 22 | def lfconvert(ui, src, dest, *pats, **opts): |
|
23 |
''' |
|
|
23 | '''convert a normal repository to a largefiles repository | |
|
24 | 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.''' | |
|
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.''' | |
|
31 | 40 | |
|
32 | 41 | if opts['tonormal']: |
|
33 | 42 | tolfile = False |
|
34 | 43 | else: |
|
35 | 44 | tolfile = True |
|
36 | 45 | size = lfutil.getminsize(ui, True, opts.get('size'), default=None) |
|
37 | 46 | try: |
|
38 | 47 | rsrc = hg.repository(ui, src) |
|
39 | 48 | if not rsrc.local(): |
|
40 | 49 | raise util.Abort(_('%s is not a local Mercurial repo') % src) |
|
41 | 50 | except error.RepoError, err: |
|
42 | 51 | ui.traceback() |
|
43 | 52 | raise util.Abort(err.args[0]) |
|
44 | 53 | if os.path.exists(dest): |
|
45 | 54 | if not os.path.isdir(dest): |
|
46 | 55 | raise util.Abort(_('destination %s already exists') % dest) |
|
47 | 56 | elif os.listdir(dest): |
|
48 | 57 | raise util.Abort(_('destination %s is not empty') % dest) |
|
49 | 58 | try: |
|
50 | 59 | ui.status(_('initializing destination %s\n') % dest) |
|
51 | 60 | rdst = hg.repository(ui, dest, create=True) |
|
52 | 61 | if not rdst.local(): |
|
53 | 62 | raise util.Abort(_('%s is not a local Mercurial repo') % dest) |
|
54 | 63 | except error.RepoError: |
|
55 | 64 | ui.traceback() |
|
56 | 65 | raise util.Abort(_('%s is not a repo') % dest) |
|
57 | 66 | |
|
58 | 67 | success = False |
|
59 | 68 | try: |
|
60 | 69 | # Lock destination to prevent modification while it is converted to. |
|
61 | 70 | # Don't need to lock src because we are just reading from its history |
|
62 | 71 | # which can't change. |
|
63 | 72 | dst_lock = rdst.lock() |
|
64 | 73 | |
|
65 | 74 | # Get a list of all changesets in the source. The easy way to do this |
|
66 | 75 | # is to simply walk the changelog, using changelog.nodesbewteen(). |
|
67 | 76 | # Take a look at mercurial/revlog.py:639 for more details. |
|
68 | 77 | # Use a generator instead of a list to decrease memory usage |
|
69 | 78 | ctxs = (rsrc[ctx] for ctx in rsrc.changelog.nodesbetween(None, |
|
70 | 79 | rsrc.heads())[0]) |
|
71 | 80 | revmap = {node.nullid: node.nullid} |
|
72 | 81 | if tolfile: |
|
73 | 82 | lfiles = set() |
|
74 | 83 | normalfiles = set() |
|
75 | 84 | if not pats: |
|
76 | 85 | pats = ui.config(lfutil.longname, 'patterns', default=()) |
|
77 | 86 | if pats: |
|
78 | 87 | pats = pats.split(' ') |
|
79 | 88 | if pats: |
|
80 | 89 | matcher = match_.match(rsrc.root, '', list(pats)) |
|
81 | 90 | else: |
|
82 | 91 | matcher = None |
|
83 | 92 | |
|
84 | 93 | lfiletohash = {} |
|
85 | 94 | for ctx in ctxs: |
|
86 | 95 | ui.progress(_('converting revisions'), ctx.rev(), |
|
87 | 96 | unit=_('revision'), total=rsrc['tip'].rev()) |
|
88 | 97 | _lfconvert_addchangeset(rsrc, rdst, ctx, revmap, |
|
89 | 98 | lfiles, normalfiles, matcher, size, lfiletohash) |
|
90 | 99 | ui.progress(_('converting revisions'), None) |
|
91 | 100 | |
|
92 | 101 | if os.path.exists(rdst.wjoin(lfutil.shortname)): |
|
93 | 102 | shutil.rmtree(rdst.wjoin(lfutil.shortname)) |
|
94 | 103 | |
|
95 | 104 | for f in lfiletohash.keys(): |
|
96 | 105 | if os.path.isfile(rdst.wjoin(f)): |
|
97 | 106 | os.unlink(rdst.wjoin(f)) |
|
98 | 107 | try: |
|
99 | 108 | os.removedirs(os.path.dirname(rdst.wjoin(f))) |
|
100 | 109 | except OSError: |
|
101 | 110 | pass |
|
102 | 111 | |
|
103 | 112 | else: |
|
104 | 113 | for ctx in ctxs: |
|
105 | 114 | ui.progress(_('converting revisions'), ctx.rev(), |
|
106 | 115 | unit=_('revision'), total=rsrc['tip'].rev()) |
|
107 | 116 | _addchangeset(ui, rsrc, rdst, ctx, revmap) |
|
108 | 117 | |
|
109 | 118 | ui.progress(_('converting revisions'), None) |
|
110 | 119 | success = True |
|
111 | 120 | finally: |
|
112 | 121 | if not success: |
|
113 | 122 | # we failed, remove the new directory |
|
114 | 123 | shutil.rmtree(rdst.root) |
|
115 | 124 | dst_lock.release() |
|
116 | 125 | |
|
117 | 126 | def _addchangeset(ui, rsrc, rdst, ctx, revmap): |
|
118 | 127 | # Convert src parents to dst parents |
|
119 | 128 | parents = [] |
|
120 | 129 | for p in ctx.parents(): |
|
121 | 130 | parents.append(revmap[p.node()]) |
|
122 | 131 | while len(parents) < 2: |
|
123 | 132 | parents.append(node.nullid) |
|
124 | 133 | |
|
125 | 134 | # Generate list of changed files |
|
126 | 135 | files = set(ctx.files()) |
|
127 | 136 | if node.nullid not in parents: |
|
128 | 137 | mc = ctx.manifest() |
|
129 | 138 | mp1 = ctx.parents()[0].manifest() |
|
130 | 139 | mp2 = ctx.parents()[1].manifest() |
|
131 | 140 | files |= (set(mp1) | set(mp2)) - set(mc) |
|
132 | 141 | for f in mc: |
|
133 | 142 | if mc[f] != mp1.get(f, None) or mc[f] != mp2.get(f, None): |
|
134 | 143 | files.add(f) |
|
135 | 144 | |
|
136 | 145 | def getfilectx(repo, memctx, f): |
|
137 | 146 | if lfutil.standin(f) in files: |
|
138 | 147 | # if the file isn't in the manifest then it was removed |
|
139 | 148 | # or renamed, raise IOError to indicate this |
|
140 | 149 | try: |
|
141 | 150 | fctx = ctx.filectx(lfutil.standin(f)) |
|
142 | 151 | except error.LookupError: |
|
143 | 152 | raise IOError() |
|
144 | 153 | renamed = fctx.renamed() |
|
145 | 154 | if renamed: |
|
146 | 155 | renamed = lfutil.splitstandin(renamed[0]) |
|
147 | 156 | |
|
148 | 157 | hash = fctx.data().strip() |
|
149 | 158 | path = lfutil.findfile(rsrc, hash) |
|
150 | 159 | ### TODO: What if the file is not cached? |
|
151 | 160 | data = '' |
|
152 | 161 | fd = None |
|
153 | 162 | try: |
|
154 | 163 | fd = open(path, 'rb') |
|
155 | 164 | data = fd.read() |
|
156 | 165 | finally: |
|
157 | 166 | if fd: |
|
158 | 167 | fd.close() |
|
159 | 168 | return context.memfilectx(f, data, 'l' in fctx.flags(), |
|
160 | 169 | 'x' in fctx.flags(), renamed) |
|
161 | 170 | else: |
|
162 | 171 | try: |
|
163 | 172 | fctx = ctx.filectx(f) |
|
164 | 173 | except error.LookupError: |
|
165 | 174 | raise IOError() |
|
166 | 175 | renamed = fctx.renamed() |
|
167 | 176 | if renamed: |
|
168 | 177 | renamed = renamed[0] |
|
169 | 178 | data = fctx.data() |
|
170 | 179 | if f == '.hgtags': |
|
171 | 180 | newdata = [] |
|
172 | 181 | for line in data.splitlines(): |
|
173 | 182 | id, name = line.split(' ', 1) |
|
174 | 183 | newdata.append('%s %s\n' % (node.hex(revmap[node.bin(id)]), |
|
175 | 184 | name)) |
|
176 | 185 | data = ''.join(newdata) |
|
177 | 186 | return context.memfilectx(f, data, 'l' in fctx.flags(), |
|
178 | 187 | 'x' in fctx.flags(), renamed) |
|
179 | 188 | |
|
180 | 189 | dstfiles = [] |
|
181 | 190 | for file in files: |
|
182 | 191 | if lfutil.isstandin(file): |
|
183 | 192 | dstfiles.append(lfutil.splitstandin(file)) |
|
184 | 193 | else: |
|
185 | 194 | dstfiles.append(file) |
|
186 | 195 | # Commit |
|
187 | 196 | mctx = context.memctx(rdst, parents, ctx.description(), dstfiles, |
|
188 | 197 | getfilectx, ctx.user(), ctx.date(), ctx.extra()) |
|
189 | 198 | ret = rdst.commitctx(mctx) |
|
190 | 199 | rdst.dirstate.setparents(ret) |
|
191 | 200 | revmap[ctx.node()] = rdst.changelog.tip() |
|
192 | 201 | |
|
193 | 202 | def _lfconvert_addchangeset(rsrc, rdst, ctx, revmap, lfiles, normalfiles, |
|
194 | 203 | matcher, size, lfiletohash): |
|
195 | 204 | # Convert src parents to dst parents |
|
196 | 205 | parents = [] |
|
197 | 206 | for p in ctx.parents(): |
|
198 | 207 | parents.append(revmap[p.node()]) |
|
199 | 208 | while len(parents) < 2: |
|
200 | 209 | parents.append(node.nullid) |
|
201 | 210 | |
|
202 | 211 | # Generate list of changed files |
|
203 | 212 | files = set(ctx.files()) |
|
204 | 213 | if node.nullid not in parents: |
|
205 | 214 | mc = ctx.manifest() |
|
206 | 215 | mp1 = ctx.parents()[0].manifest() |
|
207 | 216 | mp2 = ctx.parents()[1].manifest() |
|
208 | 217 | files |= (set(mp1) | set(mp2)) - set(mc) |
|
209 | 218 | for f in mc: |
|
210 | 219 | if mc[f] != mp1.get(f, None) or mc[f] != mp2.get(f, None): |
|
211 | 220 | files.add(f) |
|
212 | 221 | |
|
213 | 222 | dstfiles = [] |
|
214 | 223 | for f in files: |
|
215 | 224 | if f not in lfiles and f not in normalfiles: |
|
216 | 225 | islfile = _islfile(f, ctx, matcher, size) |
|
217 | 226 | # If this file was renamed or copied then copy |
|
218 | 227 | # the lfileness of its predecessor |
|
219 | 228 | if f in ctx.manifest(): |
|
220 | 229 | fctx = ctx.filectx(f) |
|
221 | 230 | renamed = fctx.renamed() |
|
222 | 231 | renamedlfile = renamed and renamed[0] in lfiles |
|
223 | 232 | islfile |= renamedlfile |
|
224 | 233 | if 'l' in fctx.flags(): |
|
225 | 234 | if renamedlfile: |
|
226 | 235 | raise util.Abort( |
|
227 | 236 | _('Renamed/copied largefile %s becomes symlink') |
|
228 | 237 | % f) |
|
229 | 238 | islfile = False |
|
230 | 239 | if islfile: |
|
231 | 240 | lfiles.add(f) |
|
232 | 241 | else: |
|
233 | 242 | normalfiles.add(f) |
|
234 | 243 | |
|
235 | 244 | if f in lfiles: |
|
236 | 245 | dstfiles.append(lfutil.standin(f)) |
|
237 | 246 | # lfile in manifest if it has not been removed/renamed |
|
238 | 247 | if f in ctx.manifest(): |
|
239 | 248 | if 'l' in ctx.filectx(f).flags(): |
|
240 | 249 | if renamed and renamed[0] in lfiles: |
|
241 | 250 | raise util.Abort(_('largefile %s becomes symlink') % f) |
|
242 | 251 | |
|
243 | 252 | # lfile was modified, update standins |
|
244 | 253 | fullpath = rdst.wjoin(f) |
|
245 | 254 | lfutil.createdir(os.path.dirname(fullpath)) |
|
246 | 255 | m = util.sha1('') |
|
247 | 256 | m.update(ctx[f].data()) |
|
248 | 257 | hash = m.hexdigest() |
|
249 | 258 | if f not in lfiletohash or lfiletohash[f] != hash: |
|
250 | 259 | try: |
|
251 | 260 | fd = open(fullpath, 'wb') |
|
252 | 261 | fd.write(ctx[f].data()) |
|
253 | 262 | finally: |
|
254 | 263 | if fd: |
|
255 | 264 | fd.close() |
|
256 | 265 | executable = 'x' in ctx[f].flags() |
|
257 | 266 | os.chmod(fullpath, lfutil.getmode(executable)) |
|
258 | 267 | lfutil.writestandin(rdst, lfutil.standin(f), hash, |
|
259 | 268 | executable) |
|
260 | 269 | lfiletohash[f] = hash |
|
261 | 270 | else: |
|
262 | 271 | # normal file |
|
263 | 272 | dstfiles.append(f) |
|
264 | 273 | |
|
265 | 274 | def getfilectx(repo, memctx, f): |
|
266 | 275 | if lfutil.isstandin(f): |
|
267 | 276 | # if the file isn't in the manifest then it was removed |
|
268 | 277 | # or renamed, raise IOError to indicate this |
|
269 | 278 | srcfname = lfutil.splitstandin(f) |
|
270 | 279 | try: |
|
271 | 280 | fctx = ctx.filectx(srcfname) |
|
272 | 281 | except error.LookupError: |
|
273 | 282 | raise IOError() |
|
274 | 283 | renamed = fctx.renamed() |
|
275 | 284 | if renamed: |
|
276 | 285 | # standin is always a lfile because lfileness |
|
277 | 286 | # doesn't change after rename or copy |
|
278 | 287 | renamed = lfutil.standin(renamed[0]) |
|
279 | 288 | |
|
280 | 289 | return context.memfilectx(f, lfiletohash[srcfname], 'l' in |
|
281 | 290 | fctx.flags(), 'x' in fctx.flags(), renamed) |
|
282 | 291 | else: |
|
283 | 292 | try: |
|
284 | 293 | fctx = ctx.filectx(f) |
|
285 | 294 | except error.LookupError: |
|
286 | 295 | raise IOError() |
|
287 | 296 | renamed = fctx.renamed() |
|
288 | 297 | if renamed: |
|
289 | 298 | renamed = renamed[0] |
|
290 | 299 | |
|
291 | 300 | data = fctx.data() |
|
292 | 301 | if f == '.hgtags': |
|
293 | 302 | newdata = [] |
|
294 | 303 | for line in data.splitlines(): |
|
295 | 304 | id, name = line.split(' ', 1) |
|
296 | 305 | newdata.append('%s %s\n' % (node.hex(revmap[node.bin(id)]), |
|
297 | 306 | name)) |
|
298 | 307 | data = ''.join(newdata) |
|
299 | 308 | return context.memfilectx(f, data, 'l' in fctx.flags(), |
|
300 | 309 | 'x' in fctx.flags(), renamed) |
|
301 | 310 | |
|
302 | 311 | # Commit |
|
303 | 312 | mctx = context.memctx(rdst, parents, ctx.description(), dstfiles, |
|
304 | 313 | getfilectx, ctx.user(), ctx.date(), ctx.extra()) |
|
305 | 314 | ret = rdst.commitctx(mctx) |
|
306 | 315 | rdst.dirstate.setparents(ret) |
|
307 | 316 | revmap[ctx.node()] = rdst.changelog.tip() |
|
308 | 317 | |
|
309 | 318 | def _islfile(file, ctx, matcher, size): |
|
310 | 319 | ''' |
|
311 | 320 | A file is a lfile if it matches a pattern or is over |
|
312 | 321 | the given size. |
|
313 | 322 | ''' |
|
314 | 323 | # Never store hgtags or hgignore as lfiles |
|
315 | 324 | if file == '.hgtags' or file == '.hgignore' or file == '.hgsigs': |
|
316 | 325 | return False |
|
317 | 326 | if matcher and matcher(file): |
|
318 | 327 | return True |
|
319 | 328 | try: |
|
320 | 329 | return ctx.filectx(file).size() >= size * 1024 * 1024 |
|
321 | 330 | except error.LookupError: |
|
322 | 331 | return False |
|
323 | 332 | |
|
324 | 333 | def uploadlfiles(ui, rsrc, rdst, files): |
|
325 | 334 | '''upload largefiles to the central store''' |
|
326 | 335 | |
|
327 | 336 | # Don't upload locally. All largefiles are in the system wide cache |
|
328 | 337 | # so the other repo can just get them from there. |
|
329 | 338 | if not files or rdst.local(): |
|
330 | 339 | return |
|
331 | 340 | |
|
332 | 341 | store = basestore._openstore(rsrc, rdst, put=True) |
|
333 | 342 | |
|
334 | 343 | at = 0 |
|
335 | 344 | files = filter(lambda h: not store.exists(h), files) |
|
336 | 345 | for hash in files: |
|
337 | 346 | ui.progress(_('uploading largefiles'), at, unit='largefile', |
|
338 | 347 | total=len(files)) |
|
339 | 348 | source = lfutil.findfile(rsrc, hash) |
|
340 | 349 | if not source: |
|
341 | 350 | raise util.Abort(_('Missing largefile %s needs to be uploaded') |
|
342 | 351 | % hash) |
|
343 | 352 | # XXX check for errors here |
|
344 | 353 | store.put(source, hash) |
|
345 | 354 | at += 1 |
|
346 | 355 | ui.progress(_('uploading largefiles'), None) |
|
347 | 356 | |
|
348 | 357 | def verifylfiles(ui, repo, all=False, contents=False): |
|
349 | 358 | '''Verify that every big file revision in the current changeset |
|
350 | 359 | exists in the central store. With --contents, also verify that |
|
351 | 360 | the contents of each big file revision are correct (SHA-1 hash |
|
352 | 361 | matches the revision ID). With --all, check every changeset in |
|
353 | 362 | this repository.''' |
|
354 | 363 | if all: |
|
355 | 364 | # Pass a list to the function rather than an iterator because we know a |
|
356 | 365 | # list will work. |
|
357 | 366 | revs = range(len(repo)) |
|
358 | 367 | else: |
|
359 | 368 | revs = ['.'] |
|
360 | 369 | |
|
361 | 370 | store = basestore._openstore(repo) |
|
362 | 371 | return store.verify(revs, contents=contents) |
|
363 | 372 | |
|
364 | 373 | def cachelfiles(ui, repo, node): |
|
365 | 374 | '''cachelfiles ensures that all largefiles needed by the specified revision |
|
366 | 375 | are present in the repository's largefile cache. |
|
367 | 376 | |
|
368 | 377 | returns a tuple (cached, missing). cached is the list of files downloaded |
|
369 | 378 | by this operation; missing is the list of files that were needed but could |
|
370 | 379 | not be found.''' |
|
371 | 380 | lfiles = lfutil.listlfiles(repo, node) |
|
372 | 381 | toget = [] |
|
373 | 382 | |
|
374 | 383 | for lfile in lfiles: |
|
375 | 384 | expectedhash = repo[node][lfutil.standin(lfile)].data().strip() |
|
376 | 385 | # if it exists and its hash matches, it might have been locally |
|
377 | 386 | # modified before updating and the user chose 'local'. in this case, |
|
378 | 387 | # it will not be in any store, so don't look for it. |
|
379 | 388 | if (not os.path.exists(repo.wjoin(lfile)) \ |
|
380 | 389 | or expectedhash != lfutil.hashfile(repo.wjoin(lfile))) and \ |
|
381 | 390 | not lfutil.findfile(repo, expectedhash): |
|
382 | 391 | toget.append((lfile, expectedhash)) |
|
383 | 392 | |
|
384 | 393 | if toget: |
|
385 | 394 | store = basestore._openstore(repo) |
|
386 | 395 | ret = store.get(toget) |
|
387 | 396 | return ret |
|
388 | 397 | |
|
389 | 398 | return ([], []) |
|
390 | 399 | |
|
391 | 400 | def updatelfiles(ui, repo, filelist=None, printmessage=True): |
|
392 | 401 | wlock = repo.wlock() |
|
393 | 402 | try: |
|
394 | 403 | lfdirstate = lfutil.openlfdirstate(ui, repo) |
|
395 | 404 | lfiles = set(lfutil.listlfiles(repo)) | set(lfdirstate) |
|
396 | 405 | |
|
397 | 406 | if filelist is not None: |
|
398 | 407 | lfiles = [f for f in lfiles if f in filelist] |
|
399 | 408 | |
|
400 | 409 | printed = False |
|
401 | 410 | if printmessage and lfiles: |
|
402 | 411 | ui.status(_('getting changed largefiles\n')) |
|
403 | 412 | printed = True |
|
404 | 413 | cachelfiles(ui, repo, '.') |
|
405 | 414 | |
|
406 | 415 | updated, removed = 0, 0 |
|
407 | 416 | for i in map(lambda f: _updatelfile(repo, lfdirstate, f), lfiles): |
|
408 | 417 | # increment the appropriate counter according to _updatelfile's |
|
409 | 418 | # return value |
|
410 | 419 | updated += i > 0 and i or 0 |
|
411 | 420 | removed -= i < 0 and i or 0 |
|
412 | 421 | if printmessage and (removed or updated) and not printed: |
|
413 | 422 | ui.status(_('getting changed largefiles\n')) |
|
414 | 423 | printed = True |
|
415 | 424 | |
|
416 | 425 | lfdirstate.write() |
|
417 | 426 | if printed and printmessage: |
|
418 | 427 | ui.status(_('%d largefiles updated, %d removed\n') % (updated, |
|
419 | 428 | removed)) |
|
420 | 429 | finally: |
|
421 | 430 | wlock.release() |
|
422 | 431 | |
|
423 | 432 | def _updatelfile(repo, lfdirstate, lfile): |
|
424 | 433 | '''updates a single largefile and copies the state of its standin from |
|
425 | 434 | the repository's dirstate to its state in the lfdirstate. |
|
426 | 435 | |
|
427 | 436 | returns 1 if the file was modified, -1 if the file was removed, 0 if the |
|
428 | 437 | file was unchanged, and None if the needed largefile was missing from the |
|
429 | 438 | cache.''' |
|
430 | 439 | ret = 0 |
|
431 | 440 | abslfile = repo.wjoin(lfile) |
|
432 | 441 | absstandin = repo.wjoin(lfutil.standin(lfile)) |
|
433 | 442 | if os.path.exists(absstandin): |
|
434 | 443 | if os.path.exists(absstandin+'.orig'): |
|
435 | 444 | shutil.copyfile(abslfile, abslfile+'.orig') |
|
436 | 445 | expecthash = lfutil.readstandin(repo, lfile) |
|
437 | 446 | if expecthash != '' and \ |
|
438 | 447 | (not os.path.exists(abslfile) or \ |
|
439 | 448 | expecthash != lfutil.hashfile(abslfile)): |
|
440 | 449 | if not lfutil.copyfromcache(repo, expecthash, lfile): |
|
441 | 450 | return None # don't try to set the mode or update the dirstate |
|
442 | 451 | ret = 1 |
|
443 | 452 | mode = os.stat(absstandin).st_mode |
|
444 | 453 | if mode != os.stat(abslfile).st_mode: |
|
445 | 454 | os.chmod(abslfile, mode) |
|
446 | 455 | ret = 1 |
|
447 | 456 | else: |
|
448 | 457 | if os.path.exists(abslfile): |
|
449 | 458 | os.unlink(abslfile) |
|
450 | 459 | ret = -1 |
|
451 | 460 | state = repo.dirstate[lfutil.standin(lfile)] |
|
452 | 461 | if state == 'n': |
|
453 | 462 | lfdirstate.normal(lfile) |
|
454 | 463 | elif state == 'r': |
|
455 | 464 | lfdirstate.remove(lfile) |
|
456 | 465 | elif state == 'a': |
|
457 | 466 | lfdirstate.add(lfile) |
|
458 | 467 | elif state == '?': |
|
459 | 468 | lfdirstate.drop(lfile) |
|
460 | 469 | return ret |
|
461 | 470 | |
|
462 | 471 | # -- hg commands declarations ------------------------------------------------ |
|
463 | 472 | |
|
464 | 473 | |
|
465 | 474 | cmdtable = { |
|
466 | 475 | 'lfconvert': (lfconvert, |
|
467 |
[('s', 'size', '', |
|
|
468 | 'will be considered largefiles. This can also be specified ' | |
|
469 |
' |
|
|
476 | [('s', 'size', '', | |
|
477 | _('minimum size (MB) for files to be converted ' | |
|
478 | 'as largefiles'), | |
|
479 | 'SIZE'), | |
|
470 | 480 | ('','tonormal',False, |
|
471 |
|
|
|
481 | _('convert from a largefiles repo to a normal repo')), | |
|
482 | ], | |
|
472 | 483 | _('hg lfconvert SOURCE DEST [FILE ...]')), |
|
473 | 484 | } |
General Comments 0
You need to be logged in to leave comments.
Login now