##// END OF EJS Templates
largefiles: introduce "_lfstatuswriters" to customize status reporting...
FUJIWARA Katsunori -
r23188:94ac64bc default
parent child Browse files
Show More
@@ -1,559 +1,576 b''
1 # Copyright 2009-2010 Gregory P. Ward
1 # Copyright 2009-2010 Gregory P. Ward
2 # Copyright 2009-2010 Intelerad Medical Systems Incorporated
2 # Copyright 2009-2010 Intelerad Medical Systems Incorporated
3 # Copyright 2010-2011 Fog Creek Software
3 # Copyright 2010-2011 Fog Creek Software
4 # Copyright 2010-2011 Unity Technologies
4 # Copyright 2010-2011 Unity Technologies
5 #
5 #
6 # This software may be used and distributed according to the terms of the
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.
7 # GNU General Public License version 2 or any later version.
8
8
9 '''largefiles utility code: must not import other modules in this package.'''
9 '''largefiles utility code: must not import other modules in this package.'''
10
10
11 import os
11 import os
12 import platform
12 import platform
13 import shutil
13 import shutil
14 import stat
14 import stat
15 import copy
15 import copy
16
16
17 from mercurial import dirstate, httpconnection, match as match_, util, scmutil
17 from mercurial import dirstate, httpconnection, match as match_, util, scmutil
18 from mercurial.i18n import _
18 from mercurial.i18n import _
19 from mercurial import node
19 from mercurial import node
20
20
21 shortname = '.hglf'
21 shortname = '.hglf'
22 shortnameslash = shortname + '/'
22 shortnameslash = shortname + '/'
23 longname = 'largefiles'
23 longname = 'largefiles'
24
24
25
25
26 # -- Private worker functions ------------------------------------------
26 # -- Private worker functions ------------------------------------------
27
27
28 def getminsize(ui, assumelfiles, opt, default=10):
28 def getminsize(ui, assumelfiles, opt, default=10):
29 lfsize = opt
29 lfsize = opt
30 if not lfsize and assumelfiles:
30 if not lfsize and assumelfiles:
31 lfsize = ui.config(longname, 'minsize', default=default)
31 lfsize = ui.config(longname, 'minsize', default=default)
32 if lfsize:
32 if lfsize:
33 try:
33 try:
34 lfsize = float(lfsize)
34 lfsize = float(lfsize)
35 except ValueError:
35 except ValueError:
36 raise util.Abort(_('largefiles: size must be number (not %s)\n')
36 raise util.Abort(_('largefiles: size must be number (not %s)\n')
37 % lfsize)
37 % lfsize)
38 if lfsize is None:
38 if lfsize is None:
39 raise util.Abort(_('minimum size for largefiles must be specified'))
39 raise util.Abort(_('minimum size for largefiles must be specified'))
40 return lfsize
40 return lfsize
41
41
42 def link(src, dest):
42 def link(src, dest):
43 util.makedirs(os.path.dirname(dest))
43 util.makedirs(os.path.dirname(dest))
44 try:
44 try:
45 util.oslink(src, dest)
45 util.oslink(src, dest)
46 except OSError:
46 except OSError:
47 # if hardlinks fail, fallback on atomic copy
47 # if hardlinks fail, fallback on atomic copy
48 dst = util.atomictempfile(dest)
48 dst = util.atomictempfile(dest)
49 for chunk in util.filechunkiter(open(src, 'rb')):
49 for chunk in util.filechunkiter(open(src, 'rb')):
50 dst.write(chunk)
50 dst.write(chunk)
51 dst.close()
51 dst.close()
52 os.chmod(dest, os.stat(src).st_mode)
52 os.chmod(dest, os.stat(src).st_mode)
53
53
54 def usercachepath(ui, hash):
54 def usercachepath(ui, hash):
55 path = ui.configpath(longname, 'usercache', None)
55 path = ui.configpath(longname, 'usercache', None)
56 if path:
56 if path:
57 path = os.path.join(path, hash)
57 path = os.path.join(path, hash)
58 else:
58 else:
59 if os.name == 'nt':
59 if os.name == 'nt':
60 appdata = os.getenv('LOCALAPPDATA', os.getenv('APPDATA'))
60 appdata = os.getenv('LOCALAPPDATA', os.getenv('APPDATA'))
61 if appdata:
61 if appdata:
62 path = os.path.join(appdata, longname, hash)
62 path = os.path.join(appdata, longname, hash)
63 elif platform.system() == 'Darwin':
63 elif platform.system() == 'Darwin':
64 home = os.getenv('HOME')
64 home = os.getenv('HOME')
65 if home:
65 if home:
66 path = os.path.join(home, 'Library', 'Caches',
66 path = os.path.join(home, 'Library', 'Caches',
67 longname, hash)
67 longname, hash)
68 elif os.name == 'posix':
68 elif os.name == 'posix':
69 path = os.getenv('XDG_CACHE_HOME')
69 path = os.getenv('XDG_CACHE_HOME')
70 if path:
70 if path:
71 path = os.path.join(path, longname, hash)
71 path = os.path.join(path, longname, hash)
72 else:
72 else:
73 home = os.getenv('HOME')
73 home = os.getenv('HOME')
74 if home:
74 if home:
75 path = os.path.join(home, '.cache', longname, hash)
75 path = os.path.join(home, '.cache', longname, hash)
76 else:
76 else:
77 raise util.Abort(_('unknown operating system: %s\n') % os.name)
77 raise util.Abort(_('unknown operating system: %s\n') % os.name)
78 return path
78 return path
79
79
80 def inusercache(ui, hash):
80 def inusercache(ui, hash):
81 path = usercachepath(ui, hash)
81 path = usercachepath(ui, hash)
82 return path and os.path.exists(path)
82 return path and os.path.exists(path)
83
83
84 def findfile(repo, hash):
84 def findfile(repo, hash):
85 if instore(repo, hash):
85 if instore(repo, hash):
86 repo.ui.note(_('found %s in store\n') % hash)
86 repo.ui.note(_('found %s in store\n') % hash)
87 return storepath(repo, hash)
87 return storepath(repo, hash)
88 elif inusercache(repo.ui, hash):
88 elif inusercache(repo.ui, hash):
89 repo.ui.note(_('found %s in system cache\n') % hash)
89 repo.ui.note(_('found %s in system cache\n') % hash)
90 path = storepath(repo, hash)
90 path = storepath(repo, hash)
91 link(usercachepath(repo.ui, hash), path)
91 link(usercachepath(repo.ui, hash), path)
92 return path
92 return path
93 return None
93 return None
94
94
95 class largefilesdirstate(dirstate.dirstate):
95 class largefilesdirstate(dirstate.dirstate):
96 def __getitem__(self, key):
96 def __getitem__(self, key):
97 return super(largefilesdirstate, self).__getitem__(unixpath(key))
97 return super(largefilesdirstate, self).__getitem__(unixpath(key))
98 def normal(self, f):
98 def normal(self, f):
99 return super(largefilesdirstate, self).normal(unixpath(f))
99 return super(largefilesdirstate, self).normal(unixpath(f))
100 def remove(self, f):
100 def remove(self, f):
101 return super(largefilesdirstate, self).remove(unixpath(f))
101 return super(largefilesdirstate, self).remove(unixpath(f))
102 def add(self, f):
102 def add(self, f):
103 return super(largefilesdirstate, self).add(unixpath(f))
103 return super(largefilesdirstate, self).add(unixpath(f))
104 def drop(self, f):
104 def drop(self, f):
105 return super(largefilesdirstate, self).drop(unixpath(f))
105 return super(largefilesdirstate, self).drop(unixpath(f))
106 def forget(self, f):
106 def forget(self, f):
107 return super(largefilesdirstate, self).forget(unixpath(f))
107 return super(largefilesdirstate, self).forget(unixpath(f))
108 def normallookup(self, f):
108 def normallookup(self, f):
109 return super(largefilesdirstate, self).normallookup(unixpath(f))
109 return super(largefilesdirstate, self).normallookup(unixpath(f))
110 def _ignore(self, f):
110 def _ignore(self, f):
111 return False
111 return False
112
112
113 def openlfdirstate(ui, repo, create=True):
113 def openlfdirstate(ui, repo, create=True):
114 '''
114 '''
115 Return a dirstate object that tracks largefiles: i.e. its root is
115 Return a dirstate object that tracks largefiles: i.e. its root is
116 the repo root, but it is saved in .hg/largefiles/dirstate.
116 the repo root, but it is saved in .hg/largefiles/dirstate.
117 '''
117 '''
118 lfstoredir = repo.join(longname)
118 lfstoredir = repo.join(longname)
119 opener = scmutil.opener(lfstoredir)
119 opener = scmutil.opener(lfstoredir)
120 lfdirstate = largefilesdirstate(opener, ui, repo.root,
120 lfdirstate = largefilesdirstate(opener, ui, repo.root,
121 repo.dirstate._validate)
121 repo.dirstate._validate)
122
122
123 # If the largefiles dirstate does not exist, populate and create
123 # If the largefiles dirstate does not exist, populate and create
124 # it. This ensures that we create it on the first meaningful
124 # it. This ensures that we create it on the first meaningful
125 # largefiles operation in a new clone.
125 # largefiles operation in a new clone.
126 if create and not os.path.exists(os.path.join(lfstoredir, 'dirstate')):
126 if create and not os.path.exists(os.path.join(lfstoredir, 'dirstate')):
127 matcher = getstandinmatcher(repo)
127 matcher = getstandinmatcher(repo)
128 standins = repo.dirstate.walk(matcher, [], False, False)
128 standins = repo.dirstate.walk(matcher, [], False, False)
129
129
130 if len(standins) > 0:
130 if len(standins) > 0:
131 util.makedirs(lfstoredir)
131 util.makedirs(lfstoredir)
132
132
133 for standin in standins:
133 for standin in standins:
134 lfile = splitstandin(standin)
134 lfile = splitstandin(standin)
135 lfdirstate.normallookup(lfile)
135 lfdirstate.normallookup(lfile)
136 return lfdirstate
136 return lfdirstate
137
137
138 def lfdirstatestatus(lfdirstate, repo):
138 def lfdirstatestatus(lfdirstate, repo):
139 wctx = repo['.']
139 wctx = repo['.']
140 match = match_.always(repo.root, repo.getcwd())
140 match = match_.always(repo.root, repo.getcwd())
141 unsure, s = lfdirstate.status(match, [], False, False, False)
141 unsure, s = lfdirstate.status(match, [], False, False, False)
142 modified, clean = s.modified, s.clean
142 modified, clean = s.modified, s.clean
143 for lfile in unsure:
143 for lfile in unsure:
144 try:
144 try:
145 fctx = wctx[standin(lfile)]
145 fctx = wctx[standin(lfile)]
146 except LookupError:
146 except LookupError:
147 fctx = None
147 fctx = None
148 if not fctx or fctx.data().strip() != hashfile(repo.wjoin(lfile)):
148 if not fctx or fctx.data().strip() != hashfile(repo.wjoin(lfile)):
149 modified.append(lfile)
149 modified.append(lfile)
150 else:
150 else:
151 clean.append(lfile)
151 clean.append(lfile)
152 lfdirstate.normal(lfile)
152 lfdirstate.normal(lfile)
153 return s
153 return s
154
154
155 def listlfiles(repo, rev=None, matcher=None):
155 def listlfiles(repo, rev=None, matcher=None):
156 '''return a list of largefiles in the working copy or the
156 '''return a list of largefiles in the working copy or the
157 specified changeset'''
157 specified changeset'''
158
158
159 if matcher is None:
159 if matcher is None:
160 matcher = getstandinmatcher(repo)
160 matcher = getstandinmatcher(repo)
161
161
162 # ignore unknown files in working directory
162 # ignore unknown files in working directory
163 return [splitstandin(f)
163 return [splitstandin(f)
164 for f in repo[rev].walk(matcher)
164 for f in repo[rev].walk(matcher)
165 if rev is not None or repo.dirstate[f] != '?']
165 if rev is not None or repo.dirstate[f] != '?']
166
166
167 def instore(repo, hash):
167 def instore(repo, hash):
168 return os.path.exists(storepath(repo, hash))
168 return os.path.exists(storepath(repo, hash))
169
169
170 def storepath(repo, hash):
170 def storepath(repo, hash):
171 return repo.join(os.path.join(longname, hash))
171 return repo.join(os.path.join(longname, hash))
172
172
173 def copyfromcache(repo, hash, filename):
173 def copyfromcache(repo, hash, filename):
174 '''Copy the specified largefile from the repo or system cache to
174 '''Copy the specified largefile from the repo or system cache to
175 filename in the repository. Return true on success or false if the
175 filename in the repository. Return true on success or false if the
176 file was not found in either cache (which should not happened:
176 file was not found in either cache (which should not happened:
177 this is meant to be called only after ensuring that the needed
177 this is meant to be called only after ensuring that the needed
178 largefile exists in the cache).'''
178 largefile exists in the cache).'''
179 path = findfile(repo, hash)
179 path = findfile(repo, hash)
180 if path is None:
180 if path is None:
181 return False
181 return False
182 util.makedirs(os.path.dirname(repo.wjoin(filename)))
182 util.makedirs(os.path.dirname(repo.wjoin(filename)))
183 # The write may fail before the file is fully written, but we
183 # The write may fail before the file is fully written, but we
184 # don't use atomic writes in the working copy.
184 # don't use atomic writes in the working copy.
185 shutil.copy(path, repo.wjoin(filename))
185 shutil.copy(path, repo.wjoin(filename))
186 return True
186 return True
187
187
188 def copytostore(repo, rev, file, uploaded=False):
188 def copytostore(repo, rev, file, uploaded=False):
189 hash = readstandin(repo, file, rev)
189 hash = readstandin(repo, file, rev)
190 if instore(repo, hash):
190 if instore(repo, hash):
191 return
191 return
192 copytostoreabsolute(repo, repo.wjoin(file), hash)
192 copytostoreabsolute(repo, repo.wjoin(file), hash)
193
193
194 def copyalltostore(repo, node):
194 def copyalltostore(repo, node):
195 '''Copy all largefiles in a given revision to the store'''
195 '''Copy all largefiles in a given revision to the store'''
196
196
197 ctx = repo[node]
197 ctx = repo[node]
198 for filename in ctx.files():
198 for filename in ctx.files():
199 if isstandin(filename) and filename in ctx.manifest():
199 if isstandin(filename) and filename in ctx.manifest():
200 realfile = splitstandin(filename)
200 realfile = splitstandin(filename)
201 copytostore(repo, ctx.node(), realfile)
201 copytostore(repo, ctx.node(), realfile)
202
202
203
203
204 def copytostoreabsolute(repo, file, hash):
204 def copytostoreabsolute(repo, file, hash):
205 if inusercache(repo.ui, hash):
205 if inusercache(repo.ui, hash):
206 link(usercachepath(repo.ui, hash), storepath(repo, hash))
206 link(usercachepath(repo.ui, hash), storepath(repo, hash))
207 elif not getattr(repo, "_isconverting", False):
207 elif not getattr(repo, "_isconverting", False):
208 util.makedirs(os.path.dirname(storepath(repo, hash)))
208 util.makedirs(os.path.dirname(storepath(repo, hash)))
209 dst = util.atomictempfile(storepath(repo, hash),
209 dst = util.atomictempfile(storepath(repo, hash),
210 createmode=repo.store.createmode)
210 createmode=repo.store.createmode)
211 for chunk in util.filechunkiter(open(file, 'rb')):
211 for chunk in util.filechunkiter(open(file, 'rb')):
212 dst.write(chunk)
212 dst.write(chunk)
213 dst.close()
213 dst.close()
214 linktousercache(repo, hash)
214 linktousercache(repo, hash)
215
215
216 def linktousercache(repo, hash):
216 def linktousercache(repo, hash):
217 path = usercachepath(repo.ui, hash)
217 path = usercachepath(repo.ui, hash)
218 if path:
218 if path:
219 link(storepath(repo, hash), path)
219 link(storepath(repo, hash), path)
220
220
221 def getstandinmatcher(repo, pats=[], opts={}):
221 def getstandinmatcher(repo, pats=[], opts={}):
222 '''Return a match object that applies pats to the standin directory'''
222 '''Return a match object that applies pats to the standin directory'''
223 standindir = repo.wjoin(shortname)
223 standindir = repo.wjoin(shortname)
224 if pats:
224 if pats:
225 pats = [os.path.join(standindir, pat) for pat in pats]
225 pats = [os.path.join(standindir, pat) for pat in pats]
226 else:
226 else:
227 # no patterns: relative to repo root
227 # no patterns: relative to repo root
228 pats = [standindir]
228 pats = [standindir]
229 # no warnings about missing files or directories
229 # no warnings about missing files or directories
230 match = scmutil.match(repo[None], pats, opts)
230 match = scmutil.match(repo[None], pats, opts)
231 match.bad = lambda f, msg: None
231 match.bad = lambda f, msg: None
232 return match
232 return match
233
233
234 def composestandinmatcher(repo, rmatcher):
234 def composestandinmatcher(repo, rmatcher):
235 '''Return a matcher that accepts standins corresponding to the
235 '''Return a matcher that accepts standins corresponding to the
236 files accepted by rmatcher. Pass the list of files in the matcher
236 files accepted by rmatcher. Pass the list of files in the matcher
237 as the paths specified by the user.'''
237 as the paths specified by the user.'''
238 smatcher = getstandinmatcher(repo, rmatcher.files())
238 smatcher = getstandinmatcher(repo, rmatcher.files())
239 isstandin = smatcher.matchfn
239 isstandin = smatcher.matchfn
240 def composedmatchfn(f):
240 def composedmatchfn(f):
241 return isstandin(f) and rmatcher.matchfn(splitstandin(f))
241 return isstandin(f) and rmatcher.matchfn(splitstandin(f))
242 smatcher.matchfn = composedmatchfn
242 smatcher.matchfn = composedmatchfn
243
243
244 return smatcher
244 return smatcher
245
245
246 def standin(filename):
246 def standin(filename):
247 '''Return the repo-relative path to the standin for the specified big
247 '''Return the repo-relative path to the standin for the specified big
248 file.'''
248 file.'''
249 # Notes:
249 # Notes:
250 # 1) Some callers want an absolute path, but for instance addlargefiles
250 # 1) Some callers want an absolute path, but for instance addlargefiles
251 # needs it repo-relative so it can be passed to repo[None].add(). So
251 # needs it repo-relative so it can be passed to repo[None].add(). So
252 # leave it up to the caller to use repo.wjoin() to get an absolute path.
252 # leave it up to the caller to use repo.wjoin() to get an absolute path.
253 # 2) Join with '/' because that's what dirstate always uses, even on
253 # 2) Join with '/' because that's what dirstate always uses, even on
254 # Windows. Change existing separator to '/' first in case we are
254 # Windows. Change existing separator to '/' first in case we are
255 # passed filenames from an external source (like the command line).
255 # passed filenames from an external source (like the command line).
256 return shortnameslash + util.pconvert(filename)
256 return shortnameslash + util.pconvert(filename)
257
257
258 def isstandin(filename):
258 def isstandin(filename):
259 '''Return true if filename is a big file standin. filename must be
259 '''Return true if filename is a big file standin. filename must be
260 in Mercurial's internal form (slash-separated).'''
260 in Mercurial's internal form (slash-separated).'''
261 return filename.startswith(shortnameslash)
261 return filename.startswith(shortnameslash)
262
262
263 def splitstandin(filename):
263 def splitstandin(filename):
264 # Split on / because that's what dirstate always uses, even on Windows.
264 # Split on / because that's what dirstate always uses, even on Windows.
265 # Change local separator to / first just in case we are passed filenames
265 # Change local separator to / first just in case we are passed filenames
266 # from an external source (like the command line).
266 # from an external source (like the command line).
267 bits = util.pconvert(filename).split('/', 1)
267 bits = util.pconvert(filename).split('/', 1)
268 if len(bits) == 2 and bits[0] == shortname:
268 if len(bits) == 2 and bits[0] == shortname:
269 return bits[1]
269 return bits[1]
270 else:
270 else:
271 return None
271 return None
272
272
273 def updatestandin(repo, standin):
273 def updatestandin(repo, standin):
274 file = repo.wjoin(splitstandin(standin))
274 file = repo.wjoin(splitstandin(standin))
275 if os.path.exists(file):
275 if os.path.exists(file):
276 hash = hashfile(file)
276 hash = hashfile(file)
277 executable = getexecutable(file)
277 executable = getexecutable(file)
278 writestandin(repo, standin, hash, executable)
278 writestandin(repo, standin, hash, executable)
279
279
280 def readstandin(repo, filename, node=None):
280 def readstandin(repo, filename, node=None):
281 '''read hex hash from standin for filename at given node, or working
281 '''read hex hash from standin for filename at given node, or working
282 directory if no node is given'''
282 directory if no node is given'''
283 return repo[node][standin(filename)].data().strip()
283 return repo[node][standin(filename)].data().strip()
284
284
285 def writestandin(repo, standin, hash, executable):
285 def writestandin(repo, standin, hash, executable):
286 '''write hash to <repo.root>/<standin>'''
286 '''write hash to <repo.root>/<standin>'''
287 repo.wwrite(standin, hash + '\n', executable and 'x' or '')
287 repo.wwrite(standin, hash + '\n', executable and 'x' or '')
288
288
289 def copyandhash(instream, outfile):
289 def copyandhash(instream, outfile):
290 '''Read bytes from instream (iterable) and write them to outfile,
290 '''Read bytes from instream (iterable) and write them to outfile,
291 computing the SHA-1 hash of the data along the way. Return the hash.'''
291 computing the SHA-1 hash of the data along the way. Return the hash.'''
292 hasher = util.sha1('')
292 hasher = util.sha1('')
293 for data in instream:
293 for data in instream:
294 hasher.update(data)
294 hasher.update(data)
295 outfile.write(data)
295 outfile.write(data)
296 return hasher.hexdigest()
296 return hasher.hexdigest()
297
297
298 def hashrepofile(repo, file):
298 def hashrepofile(repo, file):
299 return hashfile(repo.wjoin(file))
299 return hashfile(repo.wjoin(file))
300
300
301 def hashfile(file):
301 def hashfile(file):
302 if not os.path.exists(file):
302 if not os.path.exists(file):
303 return ''
303 return ''
304 hasher = util.sha1('')
304 hasher = util.sha1('')
305 fd = open(file, 'rb')
305 fd = open(file, 'rb')
306 for data in util.filechunkiter(fd, 128 * 1024):
306 for data in util.filechunkiter(fd, 128 * 1024):
307 hasher.update(data)
307 hasher.update(data)
308 fd.close()
308 fd.close()
309 return hasher.hexdigest()
309 return hasher.hexdigest()
310
310
311 def getexecutable(filename):
311 def getexecutable(filename):
312 mode = os.stat(filename).st_mode
312 mode = os.stat(filename).st_mode
313 return ((mode & stat.S_IXUSR) and
313 return ((mode & stat.S_IXUSR) and
314 (mode & stat.S_IXGRP) and
314 (mode & stat.S_IXGRP) and
315 (mode & stat.S_IXOTH))
315 (mode & stat.S_IXOTH))
316
316
317 def urljoin(first, second, *arg):
317 def urljoin(first, second, *arg):
318 def join(left, right):
318 def join(left, right):
319 if not left.endswith('/'):
319 if not left.endswith('/'):
320 left += '/'
320 left += '/'
321 if right.startswith('/'):
321 if right.startswith('/'):
322 right = right[1:]
322 right = right[1:]
323 return left + right
323 return left + right
324
324
325 url = join(first, second)
325 url = join(first, second)
326 for a in arg:
326 for a in arg:
327 url = join(url, a)
327 url = join(url, a)
328 return url
328 return url
329
329
330 def hexsha1(data):
330 def hexsha1(data):
331 """hexsha1 returns the hex-encoded sha1 sum of the data in the file-like
331 """hexsha1 returns the hex-encoded sha1 sum of the data in the file-like
332 object data"""
332 object data"""
333 h = util.sha1()
333 h = util.sha1()
334 for chunk in util.filechunkiter(data):
334 for chunk in util.filechunkiter(data):
335 h.update(chunk)
335 h.update(chunk)
336 return h.hexdigest()
336 return h.hexdigest()
337
337
338 def httpsendfile(ui, filename):
338 def httpsendfile(ui, filename):
339 return httpconnection.httpsendfile(ui, filename, 'rb')
339 return httpconnection.httpsendfile(ui, filename, 'rb')
340
340
341 def unixpath(path):
341 def unixpath(path):
342 '''Return a version of path normalized for use with the lfdirstate.'''
342 '''Return a version of path normalized for use with the lfdirstate.'''
343 return util.pconvert(os.path.normpath(path))
343 return util.pconvert(os.path.normpath(path))
344
344
345 def islfilesrepo(repo):
345 def islfilesrepo(repo):
346 if ('largefiles' in repo.requirements and
346 if ('largefiles' in repo.requirements and
347 util.any(shortnameslash in f[0] for f in repo.store.datafiles())):
347 util.any(shortnameslash in f[0] for f in repo.store.datafiles())):
348 return True
348 return True
349
349
350 return util.any(openlfdirstate(repo.ui, repo, False))
350 return util.any(openlfdirstate(repo.ui, repo, False))
351
351
352 class storeprotonotcapable(Exception):
352 class storeprotonotcapable(Exception):
353 def __init__(self, storetypes):
353 def __init__(self, storetypes):
354 self.storetypes = storetypes
354 self.storetypes = storetypes
355
355
356 def getstandinsstate(repo):
356 def getstandinsstate(repo):
357 standins = []
357 standins = []
358 matcher = getstandinmatcher(repo)
358 matcher = getstandinmatcher(repo)
359 for standin in repo.dirstate.walk(matcher, [], False, False):
359 for standin in repo.dirstate.walk(matcher, [], False, False):
360 lfile = splitstandin(standin)
360 lfile = splitstandin(standin)
361 try:
361 try:
362 hash = readstandin(repo, lfile)
362 hash = readstandin(repo, lfile)
363 except IOError:
363 except IOError:
364 hash = None
364 hash = None
365 standins.append((lfile, hash))
365 standins.append((lfile, hash))
366 return standins
366 return standins
367
367
368 def synclfdirstate(repo, lfdirstate, lfile, normallookup):
368 def synclfdirstate(repo, lfdirstate, lfile, normallookup):
369 lfstandin = standin(lfile)
369 lfstandin = standin(lfile)
370 if lfstandin in repo.dirstate:
370 if lfstandin in repo.dirstate:
371 stat = repo.dirstate._map[lfstandin]
371 stat = repo.dirstate._map[lfstandin]
372 state, mtime = stat[0], stat[3]
372 state, mtime = stat[0], stat[3]
373 else:
373 else:
374 state, mtime = '?', -1
374 state, mtime = '?', -1
375 if state == 'n':
375 if state == 'n':
376 if normallookup or mtime < 0:
376 if normallookup or mtime < 0:
377 # state 'n' doesn't ensure 'clean' in this case
377 # state 'n' doesn't ensure 'clean' in this case
378 lfdirstate.normallookup(lfile)
378 lfdirstate.normallookup(lfile)
379 else:
379 else:
380 lfdirstate.normal(lfile)
380 lfdirstate.normal(lfile)
381 elif state == 'm':
381 elif state == 'm':
382 lfdirstate.normallookup(lfile)
382 lfdirstate.normallookup(lfile)
383 elif state == 'r':
383 elif state == 'r':
384 lfdirstate.remove(lfile)
384 lfdirstate.remove(lfile)
385 elif state == 'a':
385 elif state == 'a':
386 lfdirstate.add(lfile)
386 lfdirstate.add(lfile)
387 elif state == '?':
387 elif state == '?':
388 lfdirstate.drop(lfile)
388 lfdirstate.drop(lfile)
389
389
390 def markcommitted(orig, ctx, node):
390 def markcommitted(orig, ctx, node):
391 repo = ctx._repo
391 repo = ctx._repo
392
392
393 orig(node)
393 orig(node)
394
394
395 lfdirstate = openlfdirstate(repo.ui, repo)
395 lfdirstate = openlfdirstate(repo.ui, repo)
396 for f in ctx.files():
396 for f in ctx.files():
397 if isstandin(f):
397 if isstandin(f):
398 lfile = splitstandin(f)
398 lfile = splitstandin(f)
399 synclfdirstate(repo, lfdirstate, lfile, False)
399 synclfdirstate(repo, lfdirstate, lfile, False)
400 lfdirstate.write()
400 lfdirstate.write()
401
401
402 def getlfilestoupdate(oldstandins, newstandins):
402 def getlfilestoupdate(oldstandins, newstandins):
403 changedstandins = set(oldstandins).symmetric_difference(set(newstandins))
403 changedstandins = set(oldstandins).symmetric_difference(set(newstandins))
404 filelist = []
404 filelist = []
405 for f in changedstandins:
405 for f in changedstandins:
406 if f[0] not in filelist:
406 if f[0] not in filelist:
407 filelist.append(f[0])
407 filelist.append(f[0])
408 return filelist
408 return filelist
409
409
410 def getlfilestoupload(repo, missing, addfunc):
410 def getlfilestoupload(repo, missing, addfunc):
411 for n in missing:
411 for n in missing:
412 parents = [p for p in repo.changelog.parents(n) if p != node.nullid]
412 parents = [p for p in repo.changelog.parents(n) if p != node.nullid]
413 ctx = repo[n]
413 ctx = repo[n]
414 files = set(ctx.files())
414 files = set(ctx.files())
415 if len(parents) == 2:
415 if len(parents) == 2:
416 mc = ctx.manifest()
416 mc = ctx.manifest()
417 mp1 = ctx.parents()[0].manifest()
417 mp1 = ctx.parents()[0].manifest()
418 mp2 = ctx.parents()[1].manifest()
418 mp2 = ctx.parents()[1].manifest()
419 for f in mp1:
419 for f in mp1:
420 if f not in mc:
420 if f not in mc:
421 files.add(f)
421 files.add(f)
422 for f in mp2:
422 for f in mp2:
423 if f not in mc:
423 if f not in mc:
424 files.add(f)
424 files.add(f)
425 for f in mc:
425 for f in mc:
426 if mc[f] != mp1.get(f, None) or mc[f] != mp2.get(f, None):
426 if mc[f] != mp1.get(f, None) or mc[f] != mp2.get(f, None):
427 files.add(f)
427 files.add(f)
428 for fn in files:
428 for fn in files:
429 if isstandin(fn) and fn in ctx:
429 if isstandin(fn) and fn in ctx:
430 addfunc(fn, ctx[fn].data().strip())
430 addfunc(fn, ctx[fn].data().strip())
431
431
432 def updatestandinsbymatch(repo, match):
432 def updatestandinsbymatch(repo, match):
433 '''Update standins in the working directory according to specified match
433 '''Update standins in the working directory according to specified match
434
434
435 This returns (possibly modified) ``match`` object to be used for
435 This returns (possibly modified) ``match`` object to be used for
436 subsequent commit process.
436 subsequent commit process.
437 '''
437 '''
438
438
439 ui = repo.ui
439 ui = repo.ui
440
440
441 # Case 0: Automated committing
441 # Case 0: Automated committing
442 #
442 #
443 # While automated committing (like rebase, transplant
443 # While automated committing (like rebase, transplant
444 # and so on), this code path is used to avoid:
444 # and so on), this code path is used to avoid:
445 # (1) updating standins, because standins should
445 # (1) updating standins, because standins should
446 # be already updated at this point
446 # be already updated at this point
447 # (2) aborting when standins are matched by "match",
447 # (2) aborting when standins are matched by "match",
448 # because automated committing may specify them directly
448 # because automated committing may specify them directly
449 #
449 #
450 if getattr(repo, "_istransplanting", False):
450 if getattr(repo, "_istransplanting", False):
451 return match
451 return match
452
452
453 # Case 1: user calls commit with no specific files or
453 # Case 1: user calls commit with no specific files or
454 # include/exclude patterns: refresh and commit all files that
454 # include/exclude patterns: refresh and commit all files that
455 # are "dirty".
455 # are "dirty".
456 if match is None or match.always():
456 if match is None or match.always():
457 # Spend a bit of time here to get a list of files we know
457 # Spend a bit of time here to get a list of files we know
458 # are modified so we can compare only against those.
458 # are modified so we can compare only against those.
459 # It can cost a lot of time (several seconds)
459 # It can cost a lot of time (several seconds)
460 # otherwise to update all standins if the largefiles are
460 # otherwise to update all standins if the largefiles are
461 # large.
461 # large.
462 lfdirstate = openlfdirstate(ui, repo)
462 lfdirstate = openlfdirstate(ui, repo)
463 dirtymatch = match_.always(repo.root, repo.getcwd())
463 dirtymatch = match_.always(repo.root, repo.getcwd())
464 unsure, s = lfdirstate.status(dirtymatch, [], False, False,
464 unsure, s = lfdirstate.status(dirtymatch, [], False, False,
465 False)
465 False)
466 modifiedfiles = unsure + s.modified + s.added + s.removed
466 modifiedfiles = unsure + s.modified + s.added + s.removed
467 lfiles = listlfiles(repo)
467 lfiles = listlfiles(repo)
468 # this only loops through largefiles that exist (not
468 # this only loops through largefiles that exist (not
469 # removed/renamed)
469 # removed/renamed)
470 for lfile in lfiles:
470 for lfile in lfiles:
471 if lfile in modifiedfiles:
471 if lfile in modifiedfiles:
472 if os.path.exists(
472 if os.path.exists(
473 repo.wjoin(standin(lfile))):
473 repo.wjoin(standin(lfile))):
474 # this handles the case where a rebase is being
474 # this handles the case where a rebase is being
475 # performed and the working copy is not updated
475 # performed and the working copy is not updated
476 # yet.
476 # yet.
477 if os.path.exists(repo.wjoin(lfile)):
477 if os.path.exists(repo.wjoin(lfile)):
478 updatestandin(repo,
478 updatestandin(repo,
479 standin(lfile))
479 standin(lfile))
480
480
481 return match
481 return match
482
482
483 lfiles = listlfiles(repo)
483 lfiles = listlfiles(repo)
484 match._files = repo._subdirlfs(match.files(), lfiles)
484 match._files = repo._subdirlfs(match.files(), lfiles)
485
485
486 # Case 2: user calls commit with specified patterns: refresh
486 # Case 2: user calls commit with specified patterns: refresh
487 # any matching big files.
487 # any matching big files.
488 smatcher = composestandinmatcher(repo, match)
488 smatcher = composestandinmatcher(repo, match)
489 standins = repo.dirstate.walk(smatcher, [], False, False)
489 standins = repo.dirstate.walk(smatcher, [], False, False)
490
490
491 # No matching big files: get out of the way and pass control to
491 # No matching big files: get out of the way and pass control to
492 # the usual commit() method.
492 # the usual commit() method.
493 if not standins:
493 if not standins:
494 return match
494 return match
495
495
496 # Refresh all matching big files. It's possible that the
496 # Refresh all matching big files. It's possible that the
497 # commit will end up failing, in which case the big files will
497 # commit will end up failing, in which case the big files will
498 # stay refreshed. No harm done: the user modified them and
498 # stay refreshed. No harm done: the user modified them and
499 # asked to commit them, so sooner or later we're going to
499 # asked to commit them, so sooner or later we're going to
500 # refresh the standins. Might as well leave them refreshed.
500 # refresh the standins. Might as well leave them refreshed.
501 lfdirstate = openlfdirstate(ui, repo)
501 lfdirstate = openlfdirstate(ui, repo)
502 for fstandin in standins:
502 for fstandin in standins:
503 lfile = splitstandin(fstandin)
503 lfile = splitstandin(fstandin)
504 if lfdirstate[lfile] != 'r':
504 if lfdirstate[lfile] != 'r':
505 updatestandin(repo, fstandin)
505 updatestandin(repo, fstandin)
506
506
507 # Cook up a new matcher that only matches regular files or
507 # Cook up a new matcher that only matches regular files or
508 # standins corresponding to the big files requested by the
508 # standins corresponding to the big files requested by the
509 # user. Have to modify _files to prevent commit() from
509 # user. Have to modify _files to prevent commit() from
510 # complaining "not tracked" for big files.
510 # complaining "not tracked" for big files.
511 match = copy.copy(match)
511 match = copy.copy(match)
512 origmatchfn = match.matchfn
512 origmatchfn = match.matchfn
513
513
514 # Check both the list of largefiles and the list of
514 # Check both the list of largefiles and the list of
515 # standins because if a largefile was removed, it
515 # standins because if a largefile was removed, it
516 # won't be in the list of largefiles at this point
516 # won't be in the list of largefiles at this point
517 match._files += sorted(standins)
517 match._files += sorted(standins)
518
518
519 actualfiles = []
519 actualfiles = []
520 for f in match._files:
520 for f in match._files:
521 fstandin = standin(f)
521 fstandin = standin(f)
522
522
523 # ignore known largefiles and standins
523 # ignore known largefiles and standins
524 if f in lfiles or fstandin in standins:
524 if f in lfiles or fstandin in standins:
525 continue
525 continue
526
526
527 actualfiles.append(f)
527 actualfiles.append(f)
528 match._files = actualfiles
528 match._files = actualfiles
529
529
530 def matchfn(f):
530 def matchfn(f):
531 if origmatchfn(f):
531 if origmatchfn(f):
532 return f not in lfiles
532 return f not in lfiles
533 else:
533 else:
534 return f in standins
534 return f in standins
535
535
536 match.matchfn = matchfn
536 match.matchfn = matchfn
537
537
538 return match
538 return match
539
539
540 class automatedcommithook(object):
540 class automatedcommithook(object):
541 '''Statefull hook to update standins at the 1st commit of resuming
541 '''Statefull hook to update standins at the 1st commit of resuming
542
542
543 For efficiency, updating standins in the working directory should
543 For efficiency, updating standins in the working directory should
544 be avoided while automated committing (like rebase, transplant and
544 be avoided while automated committing (like rebase, transplant and
545 so on), because they should be updated before committing.
545 so on), because they should be updated before committing.
546
546
547 But the 1st commit of resuming automated committing (e.g. ``rebase
547 But the 1st commit of resuming automated committing (e.g. ``rebase
548 --continue``) should update them, because largefiles may be
548 --continue``) should update them, because largefiles may be
549 modified manually.
549 modified manually.
550 '''
550 '''
551 def __init__(self, resuming):
551 def __init__(self, resuming):
552 self.resuming = resuming
552 self.resuming = resuming
553
553
554 def __call__(self, repo, match):
554 def __call__(self, repo, match):
555 if self.resuming:
555 if self.resuming:
556 self.resuming = False # avoids updating at subsequent commits
556 self.resuming = False # avoids updating at subsequent commits
557 return updatestandinsbymatch(repo, match)
557 return updatestandinsbymatch(repo, match)
558 else:
558 else:
559 return match
559 return match
560
561 def getstatuswriter(ui, repo, forcibly=None):
562 '''Return the function to write largefiles specific status out
563
564 If ``forcibly`` is ``None``, this returns the last element of
565 ``repo._lfupdatereporters`` as "default" writer function.
566
567 Otherwise, this returns the function to always write out (or
568 ignore if ``not forcibly``) status.
569 '''
570 if forcibly is None:
571 return repo._lfstatuswriters[-1]
572 else:
573 if forcibly:
574 return ui.status # forcibly WRITE OUT
575 else:
576 return lambda *msg, **opts: None # forcibly IGNORE
@@ -1,362 +1,367 b''
1 # Copyright 2009-2010 Gregory P. Ward
1 # Copyright 2009-2010 Gregory P. Ward
2 # Copyright 2009-2010 Intelerad Medical Systems Incorporated
2 # Copyright 2009-2010 Intelerad Medical Systems Incorporated
3 # Copyright 2010-2011 Fog Creek Software
3 # Copyright 2010-2011 Fog Creek Software
4 # Copyright 2010-2011 Unity Technologies
4 # Copyright 2010-2011 Unity Technologies
5 #
5 #
6 # This software may be used and distributed according to the terms of the
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.
7 # GNU General Public License version 2 or any later version.
8
8
9 '''setup for largefiles repositories: reposetup'''
9 '''setup for largefiles repositories: reposetup'''
10 import copy
10 import copy
11 import os
11 import os
12
12
13 from mercurial import error, manifest, match as match_, util
13 from mercurial import error, manifest, match as match_, util
14 from mercurial.i18n import _
14 from mercurial.i18n import _
15 from mercurial import localrepo, scmutil
15 from mercurial import localrepo, scmutil
16
16
17 import lfcommands
17 import lfcommands
18 import lfutil
18 import lfutil
19
19
20 def reposetup(ui, repo):
20 def reposetup(ui, repo):
21 # wire repositories should be given new wireproto functions
21 # wire repositories should be given new wireproto functions
22 # by "proto.wirereposetup()" via "hg.wirepeersetupfuncs"
22 # by "proto.wirereposetup()" via "hg.wirepeersetupfuncs"
23 if not repo.local():
23 if not repo.local():
24 return
24 return
25
25
26 class lfilesrepo(repo.__class__):
26 class lfilesrepo(repo.__class__):
27 lfstatus = False
27 lfstatus = False
28 def status_nolfiles(self, *args, **kwargs):
28 def status_nolfiles(self, *args, **kwargs):
29 return super(lfilesrepo, self).status(*args, **kwargs)
29 return super(lfilesrepo, self).status(*args, **kwargs)
30
30
31 # When lfstatus is set, return a context that gives the names
31 # When lfstatus is set, return a context that gives the names
32 # of largefiles instead of their corresponding standins and
32 # of largefiles instead of their corresponding standins and
33 # identifies the largefiles as always binary, regardless of
33 # identifies the largefiles as always binary, regardless of
34 # their actual contents.
34 # their actual contents.
35 def __getitem__(self, changeid):
35 def __getitem__(self, changeid):
36 ctx = super(lfilesrepo, self).__getitem__(changeid)
36 ctx = super(lfilesrepo, self).__getitem__(changeid)
37 if self.lfstatus:
37 if self.lfstatus:
38 class lfilesmanifestdict(manifest.manifestdict):
38 class lfilesmanifestdict(manifest.manifestdict):
39 def __contains__(self, filename):
39 def __contains__(self, filename):
40 orig = super(lfilesmanifestdict, self).__contains__
40 orig = super(lfilesmanifestdict, self).__contains__
41 return orig(filename) or orig(lfutil.standin(filename))
41 return orig(filename) or orig(lfutil.standin(filename))
42 class lfilesctx(ctx.__class__):
42 class lfilesctx(ctx.__class__):
43 def files(self):
43 def files(self):
44 filenames = super(lfilesctx, self).files()
44 filenames = super(lfilesctx, self).files()
45 return [lfutil.splitstandin(f) or f for f in filenames]
45 return [lfutil.splitstandin(f) or f for f in filenames]
46 def manifest(self):
46 def manifest(self):
47 man1 = super(lfilesctx, self).manifest()
47 man1 = super(lfilesctx, self).manifest()
48 man1.__class__ = lfilesmanifestdict
48 man1.__class__ = lfilesmanifestdict
49 return man1
49 return man1
50 def filectx(self, path, fileid=None, filelog=None):
50 def filectx(self, path, fileid=None, filelog=None):
51 orig = super(lfilesctx, self).filectx
51 orig = super(lfilesctx, self).filectx
52 try:
52 try:
53 if filelog is not None:
53 if filelog is not None:
54 result = orig(path, fileid, filelog)
54 result = orig(path, fileid, filelog)
55 else:
55 else:
56 result = orig(path, fileid)
56 result = orig(path, fileid)
57 except error.LookupError:
57 except error.LookupError:
58 # Adding a null character will cause Mercurial to
58 # Adding a null character will cause Mercurial to
59 # identify this as a binary file.
59 # identify this as a binary file.
60 if filelog is not None:
60 if filelog is not None:
61 result = orig(lfutil.standin(path), fileid,
61 result = orig(lfutil.standin(path), fileid,
62 filelog)
62 filelog)
63 else:
63 else:
64 result = orig(lfutil.standin(path), fileid)
64 result = orig(lfutil.standin(path), fileid)
65 olddata = result.data
65 olddata = result.data
66 result.data = lambda: olddata() + '\0'
66 result.data = lambda: olddata() + '\0'
67 return result
67 return result
68 ctx.__class__ = lfilesctx
68 ctx.__class__ = lfilesctx
69 return ctx
69 return ctx
70
70
71 # Figure out the status of big files and insert them into the
71 # Figure out the status of big files and insert them into the
72 # appropriate list in the result. Also removes standin files
72 # appropriate list in the result. Also removes standin files
73 # from the listing. Revert to the original status if
73 # from the listing. Revert to the original status if
74 # self.lfstatus is False.
74 # self.lfstatus is False.
75 # XXX large file status is buggy when used on repo proxy.
75 # XXX large file status is buggy when used on repo proxy.
76 # XXX this needs to be investigated.
76 # XXX this needs to be investigated.
77 @localrepo.unfilteredmethod
77 @localrepo.unfilteredmethod
78 def status(self, node1='.', node2=None, match=None, ignored=False,
78 def status(self, node1='.', node2=None, match=None, ignored=False,
79 clean=False, unknown=False, listsubrepos=False):
79 clean=False, unknown=False, listsubrepos=False):
80 listignored, listclean, listunknown = ignored, clean, unknown
80 listignored, listclean, listunknown = ignored, clean, unknown
81 orig = super(lfilesrepo, self).status
81 orig = super(lfilesrepo, self).status
82 if not self.lfstatus:
82 if not self.lfstatus:
83 return orig(node1, node2, match, listignored, listclean,
83 return orig(node1, node2, match, listignored, listclean,
84 listunknown, listsubrepos)
84 listunknown, listsubrepos)
85
85
86 # some calls in this function rely on the old version of status
86 # some calls in this function rely on the old version of status
87 self.lfstatus = False
87 self.lfstatus = False
88 ctx1 = self[node1]
88 ctx1 = self[node1]
89 ctx2 = self[node2]
89 ctx2 = self[node2]
90 working = ctx2.rev() is None
90 working = ctx2.rev() is None
91 parentworking = working and ctx1 == self['.']
91 parentworking = working and ctx1 == self['.']
92
92
93 if match is None:
93 if match is None:
94 match = match_.always(self.root, self.getcwd())
94 match = match_.always(self.root, self.getcwd())
95
95
96 wlock = None
96 wlock = None
97 try:
97 try:
98 try:
98 try:
99 # updating the dirstate is optional
99 # updating the dirstate is optional
100 # so we don't wait on the lock
100 # so we don't wait on the lock
101 wlock = self.wlock(False)
101 wlock = self.wlock(False)
102 except error.LockError:
102 except error.LockError:
103 pass
103 pass
104
104
105 # First check if paths or patterns were specified on the
105 # First check if paths or patterns were specified on the
106 # command line. If there were, and they don't match any
106 # command line. If there were, and they don't match any
107 # largefiles, we should just bail here and let super
107 # largefiles, we should just bail here and let super
108 # handle it -- thus gaining a big performance boost.
108 # handle it -- thus gaining a big performance boost.
109 lfdirstate = lfutil.openlfdirstate(ui, self)
109 lfdirstate = lfutil.openlfdirstate(ui, self)
110 if not match.always():
110 if not match.always():
111 for f in lfdirstate:
111 for f in lfdirstate:
112 if match(f):
112 if match(f):
113 break
113 break
114 else:
114 else:
115 return orig(node1, node2, match, listignored, listclean,
115 return orig(node1, node2, match, listignored, listclean,
116 listunknown, listsubrepos)
116 listunknown, listsubrepos)
117
117
118 # Create a copy of match that matches standins instead
118 # Create a copy of match that matches standins instead
119 # of largefiles.
119 # of largefiles.
120 def tostandins(files):
120 def tostandins(files):
121 if not working:
121 if not working:
122 return files
122 return files
123 newfiles = []
123 newfiles = []
124 dirstate = self.dirstate
124 dirstate = self.dirstate
125 for f in files:
125 for f in files:
126 sf = lfutil.standin(f)
126 sf = lfutil.standin(f)
127 if sf in dirstate:
127 if sf in dirstate:
128 newfiles.append(sf)
128 newfiles.append(sf)
129 elif sf in dirstate.dirs():
129 elif sf in dirstate.dirs():
130 # Directory entries could be regular or
130 # Directory entries could be regular or
131 # standin, check both
131 # standin, check both
132 newfiles.extend((f, sf))
132 newfiles.extend((f, sf))
133 else:
133 else:
134 newfiles.append(f)
134 newfiles.append(f)
135 return newfiles
135 return newfiles
136
136
137 m = copy.copy(match)
137 m = copy.copy(match)
138 m._files = tostandins(m._files)
138 m._files = tostandins(m._files)
139
139
140 result = orig(node1, node2, m, ignored, clean, unknown,
140 result = orig(node1, node2, m, ignored, clean, unknown,
141 listsubrepos)
141 listsubrepos)
142 if working:
142 if working:
143
143
144 def sfindirstate(f):
144 def sfindirstate(f):
145 sf = lfutil.standin(f)
145 sf = lfutil.standin(f)
146 dirstate = self.dirstate
146 dirstate = self.dirstate
147 return sf in dirstate or sf in dirstate.dirs()
147 return sf in dirstate or sf in dirstate.dirs()
148
148
149 match._files = [f for f in match._files
149 match._files = [f for f in match._files
150 if sfindirstate(f)]
150 if sfindirstate(f)]
151 # Don't waste time getting the ignored and unknown
151 # Don't waste time getting the ignored and unknown
152 # files from lfdirstate
152 # files from lfdirstate
153 unsure, s = lfdirstate.status(match, [], False, listclean,
153 unsure, s = lfdirstate.status(match, [], False, listclean,
154 False)
154 False)
155 (modified, added, removed, clean) = (s.modified, s.added,
155 (modified, added, removed, clean) = (s.modified, s.added,
156 s.removed, s.clean)
156 s.removed, s.clean)
157 if parentworking:
157 if parentworking:
158 for lfile in unsure:
158 for lfile in unsure:
159 standin = lfutil.standin(lfile)
159 standin = lfutil.standin(lfile)
160 if standin not in ctx1:
160 if standin not in ctx1:
161 # from second parent
161 # from second parent
162 modified.append(lfile)
162 modified.append(lfile)
163 elif ctx1[standin].data().strip() \
163 elif ctx1[standin].data().strip() \
164 != lfutil.hashfile(self.wjoin(lfile)):
164 != lfutil.hashfile(self.wjoin(lfile)):
165 modified.append(lfile)
165 modified.append(lfile)
166 else:
166 else:
167 if listclean:
167 if listclean:
168 clean.append(lfile)
168 clean.append(lfile)
169 lfdirstate.normal(lfile)
169 lfdirstate.normal(lfile)
170 else:
170 else:
171 tocheck = unsure + modified + added + clean
171 tocheck = unsure + modified + added + clean
172 modified, added, clean = [], [], []
172 modified, added, clean = [], [], []
173
173
174 for lfile in tocheck:
174 for lfile in tocheck:
175 standin = lfutil.standin(lfile)
175 standin = lfutil.standin(lfile)
176 if standin in ctx1:
176 if standin in ctx1:
177 abslfile = self.wjoin(lfile)
177 abslfile = self.wjoin(lfile)
178 if ((ctx1[standin].data().strip() !=
178 if ((ctx1[standin].data().strip() !=
179 lfutil.hashfile(abslfile)) or
179 lfutil.hashfile(abslfile)) or
180 (('x' in ctx1.flags(standin)) !=
180 (('x' in ctx1.flags(standin)) !=
181 bool(lfutil.getexecutable(abslfile)))):
181 bool(lfutil.getexecutable(abslfile)))):
182 modified.append(lfile)
182 modified.append(lfile)
183 elif listclean:
183 elif listclean:
184 clean.append(lfile)
184 clean.append(lfile)
185 else:
185 else:
186 added.append(lfile)
186 added.append(lfile)
187
187
188 # at this point, 'removed' contains largefiles
188 # at this point, 'removed' contains largefiles
189 # marked as 'R' in the working context.
189 # marked as 'R' in the working context.
190 # then, largefiles not managed also in the target
190 # then, largefiles not managed also in the target
191 # context should be excluded from 'removed'.
191 # context should be excluded from 'removed'.
192 removed = [lfile for lfile in removed
192 removed = [lfile for lfile in removed
193 if lfutil.standin(lfile) in ctx1]
193 if lfutil.standin(lfile) in ctx1]
194
194
195 # Standins no longer found in lfdirstate has been
195 # Standins no longer found in lfdirstate has been
196 # removed
196 # removed
197 for standin in ctx1.walk(lfutil.getstandinmatcher(self)):
197 for standin in ctx1.walk(lfutil.getstandinmatcher(self)):
198 lfile = lfutil.splitstandin(standin)
198 lfile = lfutil.splitstandin(standin)
199 if not match(lfile):
199 if not match(lfile):
200 continue
200 continue
201 if lfile not in lfdirstate:
201 if lfile not in lfdirstate:
202 removed.append(lfile)
202 removed.append(lfile)
203
203
204 # Filter result lists
204 # Filter result lists
205 result = list(result)
205 result = list(result)
206
206
207 # Largefiles are not really removed when they're
207 # Largefiles are not really removed when they're
208 # still in the normal dirstate. Likewise, normal
208 # still in the normal dirstate. Likewise, normal
209 # files are not really removed if they are still in
209 # files are not really removed if they are still in
210 # lfdirstate. This happens in merges where files
210 # lfdirstate. This happens in merges where files
211 # change type.
211 # change type.
212 removed = [f for f in removed
212 removed = [f for f in removed
213 if f not in self.dirstate]
213 if f not in self.dirstate]
214 result[2] = [f for f in result[2]
214 result[2] = [f for f in result[2]
215 if f not in lfdirstate]
215 if f not in lfdirstate]
216
216
217 lfiles = set(lfdirstate._map)
217 lfiles = set(lfdirstate._map)
218 # Unknown files
218 # Unknown files
219 result[4] = set(result[4]).difference(lfiles)
219 result[4] = set(result[4]).difference(lfiles)
220 # Ignored files
220 # Ignored files
221 result[5] = set(result[5]).difference(lfiles)
221 result[5] = set(result[5]).difference(lfiles)
222 # combine normal files and largefiles
222 # combine normal files and largefiles
223 normals = [[fn for fn in filelist
223 normals = [[fn for fn in filelist
224 if not lfutil.isstandin(fn)]
224 if not lfutil.isstandin(fn)]
225 for filelist in result]
225 for filelist in result]
226 lfstatus = (modified, added, removed, s.deleted, [], [],
226 lfstatus = (modified, added, removed, s.deleted, [], [],
227 clean)
227 clean)
228 result = [sorted(list1 + list2)
228 result = [sorted(list1 + list2)
229 for (list1, list2) in zip(normals, lfstatus)]
229 for (list1, list2) in zip(normals, lfstatus)]
230 else: # not against working directory
230 else: # not against working directory
231 result = [[lfutil.splitstandin(f) or f for f in items]
231 result = [[lfutil.splitstandin(f) or f for f in items]
232 for items in result]
232 for items in result]
233
233
234 if wlock:
234 if wlock:
235 lfdirstate.write()
235 lfdirstate.write()
236
236
237 finally:
237 finally:
238 if wlock:
238 if wlock:
239 wlock.release()
239 wlock.release()
240
240
241 self.lfstatus = True
241 self.lfstatus = True
242 return scmutil.status(*result)
242 return scmutil.status(*result)
243
243
244 # As part of committing, copy all of the largefiles into the
244 # As part of committing, copy all of the largefiles into the
245 # cache.
245 # cache.
246 def commitctx(self, ctx, *args, **kwargs):
246 def commitctx(self, ctx, *args, **kwargs):
247 node = super(lfilesrepo, self).commitctx(ctx, *args, **kwargs)
247 node = super(lfilesrepo, self).commitctx(ctx, *args, **kwargs)
248 lfutil.copyalltostore(self, node)
248 lfutil.copyalltostore(self, node)
249 class lfilesctx(ctx.__class__):
249 class lfilesctx(ctx.__class__):
250 def markcommitted(self, node):
250 def markcommitted(self, node):
251 orig = super(lfilesctx, self).markcommitted
251 orig = super(lfilesctx, self).markcommitted
252 return lfutil.markcommitted(orig, self, node)
252 return lfutil.markcommitted(orig, self, node)
253 ctx.__class__ = lfilesctx
253 ctx.__class__ = lfilesctx
254 return node
254 return node
255
255
256 # Before commit, largefile standins have not had their
256 # Before commit, largefile standins have not had their
257 # contents updated to reflect the hash of their largefile.
257 # contents updated to reflect the hash of their largefile.
258 # Do that here.
258 # Do that here.
259 def commit(self, text="", user=None, date=None, match=None,
259 def commit(self, text="", user=None, date=None, match=None,
260 force=False, editor=False, extra={}):
260 force=False, editor=False, extra={}):
261 orig = super(lfilesrepo, self).commit
261 orig = super(lfilesrepo, self).commit
262
262
263 wlock = self.wlock()
263 wlock = self.wlock()
264 try:
264 try:
265 lfcommithook = self._lfcommithooks[-1]
265 lfcommithook = self._lfcommithooks[-1]
266 match = lfcommithook(self, match)
266 match = lfcommithook(self, match)
267 result = orig(text=text, user=user, date=date, match=match,
267 result = orig(text=text, user=user, date=date, match=match,
268 force=force, editor=editor, extra=extra)
268 force=force, editor=editor, extra=extra)
269 return result
269 return result
270 finally:
270 finally:
271 wlock.release()
271 wlock.release()
272
272
273 def push(self, remote, force=False, revs=None, newbranch=False):
273 def push(self, remote, force=False, revs=None, newbranch=False):
274 if remote.local():
274 if remote.local():
275 missing = set(self.requirements) - remote.local().supported
275 missing = set(self.requirements) - remote.local().supported
276 if missing:
276 if missing:
277 msg = _("required features are not"
277 msg = _("required features are not"
278 " supported in the destination:"
278 " supported in the destination:"
279 " %s") % (', '.join(sorted(missing)))
279 " %s") % (', '.join(sorted(missing)))
280 raise util.Abort(msg)
280 raise util.Abort(msg)
281 return super(lfilesrepo, self).push(remote, force=force, revs=revs,
281 return super(lfilesrepo, self).push(remote, force=force, revs=revs,
282 newbranch=newbranch)
282 newbranch=newbranch)
283
283
284 # TODO: _subdirlfs should be moved into "lfutil.py", because
284 # TODO: _subdirlfs should be moved into "lfutil.py", because
285 # it is referred only from "lfutil.updatestandinsbymatch"
285 # it is referred only from "lfutil.updatestandinsbymatch"
286 def _subdirlfs(self, files, lfiles):
286 def _subdirlfs(self, files, lfiles):
287 '''
287 '''
288 Adjust matched file list
288 Adjust matched file list
289 If we pass a directory to commit whose only commitable files
289 If we pass a directory to commit whose only commitable files
290 are largefiles, the core commit code aborts before finding
290 are largefiles, the core commit code aborts before finding
291 the largefiles.
291 the largefiles.
292 So we do the following:
292 So we do the following:
293 For directories that only have largefiles as matches,
293 For directories that only have largefiles as matches,
294 we explicitly add the largefiles to the match list and remove
294 we explicitly add the largefiles to the match list and remove
295 the directory.
295 the directory.
296 In other cases, we leave the match list unmodified.
296 In other cases, we leave the match list unmodified.
297 '''
297 '''
298 actualfiles = []
298 actualfiles = []
299 dirs = []
299 dirs = []
300 regulars = []
300 regulars = []
301
301
302 for f in files:
302 for f in files:
303 if lfutil.isstandin(f + '/'):
303 if lfutil.isstandin(f + '/'):
304 raise util.Abort(
304 raise util.Abort(
305 _('file "%s" is a largefile standin') % f,
305 _('file "%s" is a largefile standin') % f,
306 hint=('commit the largefile itself instead'))
306 hint=('commit the largefile itself instead'))
307 # Scan directories
307 # Scan directories
308 if os.path.isdir(self.wjoin(f)):
308 if os.path.isdir(self.wjoin(f)):
309 dirs.append(f)
309 dirs.append(f)
310 else:
310 else:
311 regulars.append(f)
311 regulars.append(f)
312
312
313 for f in dirs:
313 for f in dirs:
314 matcheddir = False
314 matcheddir = False
315 d = self.dirstate.normalize(f) + '/'
315 d = self.dirstate.normalize(f) + '/'
316 # Check for matched normal files
316 # Check for matched normal files
317 for mf in regulars:
317 for mf in regulars:
318 if self.dirstate.normalize(mf).startswith(d):
318 if self.dirstate.normalize(mf).startswith(d):
319 actualfiles.append(f)
319 actualfiles.append(f)
320 matcheddir = True
320 matcheddir = True
321 break
321 break
322 if not matcheddir:
322 if not matcheddir:
323 # If no normal match, manually append
323 # If no normal match, manually append
324 # any matching largefiles
324 # any matching largefiles
325 for lf in lfiles:
325 for lf in lfiles:
326 if self.dirstate.normalize(lf).startswith(d):
326 if self.dirstate.normalize(lf).startswith(d):
327 actualfiles.append(lf)
327 actualfiles.append(lf)
328 if not matcheddir:
328 if not matcheddir:
329 actualfiles.append(lfutil.standin(f))
329 actualfiles.append(lfutil.standin(f))
330 matcheddir = True
330 matcheddir = True
331 # Nothing in dir, so readd it
331 # Nothing in dir, so readd it
332 # and let commit reject it
332 # and let commit reject it
333 if not matcheddir:
333 if not matcheddir:
334 actualfiles.append(f)
334 actualfiles.append(f)
335
335
336 # Always add normal files
336 # Always add normal files
337 actualfiles += regulars
337 actualfiles += regulars
338 return actualfiles
338 return actualfiles
339
339
340 repo.__class__ = lfilesrepo
340 repo.__class__ = lfilesrepo
341
341
342 # stack of hooks being executed before committing.
342 # stack of hooks being executed before committing.
343 # only last element ("_lfcommithooks[-1]") is used for each committing.
343 # only last element ("_lfcommithooks[-1]") is used for each committing.
344 repo._lfcommithooks = [lfutil.updatestandinsbymatch]
344 repo._lfcommithooks = [lfutil.updatestandinsbymatch]
345
345
346 # Stack of status writer functions taking "*msg, **opts" arguments
347 # like "ui.status()". Only last element ("_lfupdatereporters[-1]")
348 # is used to write status out.
349 repo._lfstatuswriters = [ui.status]
350
346 def prepushoutgoinghook(local, remote, outgoing):
351 def prepushoutgoinghook(local, remote, outgoing):
347 if outgoing.missing:
352 if outgoing.missing:
348 toupload = set()
353 toupload = set()
349 addfunc = lambda fn, lfhash: toupload.add(lfhash)
354 addfunc = lambda fn, lfhash: toupload.add(lfhash)
350 lfutil.getlfilestoupload(local, outgoing.missing, addfunc)
355 lfutil.getlfilestoupload(local, outgoing.missing, addfunc)
351 lfcommands.uploadlfiles(ui, local, remote, toupload)
356 lfcommands.uploadlfiles(ui, local, remote, toupload)
352 repo.prepushoutgoinghooks.add("largefiles", prepushoutgoinghook)
357 repo.prepushoutgoinghooks.add("largefiles", prepushoutgoinghook)
353
358
354 def checkrequireslfiles(ui, repo, **kwargs):
359 def checkrequireslfiles(ui, repo, **kwargs):
355 if 'largefiles' not in repo.requirements and util.any(
360 if 'largefiles' not in repo.requirements and util.any(
356 lfutil.shortname+'/' in f[0] for f in repo.store.datafiles()):
361 lfutil.shortname+'/' in f[0] for f in repo.store.datafiles()):
357 repo.requirements.add('largefiles')
362 repo.requirements.add('largefiles')
358 repo._writerequirements()
363 repo._writerequirements()
359
364
360 ui.setconfig('hooks', 'changegroup.lfiles', checkrequireslfiles,
365 ui.setconfig('hooks', 'changegroup.lfiles', checkrequireslfiles,
361 'largefiles')
366 'largefiles')
362 ui.setconfig('hooks', 'commit.lfiles', checkrequireslfiles, 'largefiles')
367 ui.setconfig('hooks', 'commit.lfiles', checkrequireslfiles, 'largefiles')
General Comments 0
You need to be logged in to leave comments. Login now