| 1 |
# Copyright 2009-2010 Gregory P. Ward |
| 2 |
# Copyright 2009-2010 Intelerad Medical Systems Incorporated |
| 3 |
# Copyright 2010-2011 Fog Creek Software |
| 4 |
# Copyright 2010-2011 Unity Technologies |
| 5 |
# |
| 6 |
# This software may be used and distributed according to the terms of the |
| 7 |
# GNU General Public License version 2 or any later version. |
| 8 |
|
| 9 |
'''largefiles utility code: must not import other modules in this package.''' |
| 10 |
|
| 11 |
from __future__ import annotations |
| 12 |
|
| 13 |
import contextlib |
| 14 |
import copy |
| 15 |
import os |
| 16 |
import stat |
| 17 |
|
| 18 |
from mercurial.i18n import _ |
| 19 |
from mercurial.node import hex |
| 20 |
|
| 21 |
from mercurial import ( |
| 22 |
dirstate, |
| 23 |
encoding, |
| 24 |
error, |
| 25 |
httpconnection, |
| 26 |
match as matchmod, |
| 27 |
pycompat, |
| 28 |
requirements, |
| 29 |
scmutil, |
| 30 |
sparse, |
| 31 |
util, |
| 32 |
vfs as vfsmod, |
| 33 |
) |
| 34 |
from mercurial.utils import hashutil |
| 35 |
from mercurial.dirstateutils import timestamp |
| 36 |
|
| 37 |
shortname = b'.hglf' |
| 38 |
shortnameslash = shortname + b'/' |
| 39 |
longname = b'largefiles' |
| 40 |
|
| 41 |
# -- Private worker functions ------------------------------------------ |
| 42 |
|
| 43 |
|
| 44 |
@contextlib.contextmanager |
| 45 |
def lfstatus(repo, value=True): |
| 46 |
oldvalue = getattr(repo, 'lfstatus', False) |
| 47 |
repo.lfstatus = value |
| 48 |
try: |
| 49 |
yield |
| 50 |
finally: |
| 51 |
repo.lfstatus = oldvalue |
| 52 |
|
| 53 |
|
| 54 |
def getminsize(ui, assumelfiles, opt, default=10): |
| 55 |
lfsize = opt |
| 56 |
if not lfsize and assumelfiles: |
| 57 |
lfsize = ui.config(longname, b'minsize', default=default) |
| 58 |
if lfsize: |
| 59 |
try: |
| 60 |
lfsize = float(lfsize) |
| 61 |
except ValueError: |
| 62 |
raise error.Abort( |
| 63 |
_(b'largefiles: size must be number (not %s)\n') % lfsize |
| 64 |
) |
| 65 |
if lfsize is None: |
| 66 |
raise error.Abort(_(b'minimum size for largefiles must be specified')) |
| 67 |
return lfsize |
| 68 |
|
| 69 |
|
| 70 |
def link(src, dest): |
| 71 |
"""Try to create hardlink - if that fails, efficiently make a copy.""" |
| 72 |
util.makedirs(os.path.dirname(dest)) |
| 73 |
try: |
| 74 |
util.oslink(src, dest) |
| 75 |
except OSError: |
| 76 |
# if hardlinks fail, fallback on atomic copy |
| 77 |
with open(src, 'rb') as srcf, util.atomictempfile(dest) as dstf: |
| 78 |
for chunk in util.filechunkiter(srcf): |
| 79 |
dstf.write(chunk) |
| 80 |
os.chmod(dest, os.stat(src).st_mode) |
| 81 |
|
| 82 |
|
| 83 |
def usercachepath(ui, hash): |
| 84 |
"""Return the correct location in the "global" largefiles cache for a file |
| 85 |
with the given hash. |
| 86 |
This cache is used for sharing of largefiles across repositories - both |
| 87 |
to preserve download bandwidth and storage space.""" |
| 88 |
return os.path.join(_usercachedir(ui), hash) |
| 89 |
|
| 90 |
|
| 91 |
def _usercachedir(ui, name=longname): |
| 92 |
'''Return the location of the "global" largefiles cache.''' |
| 93 |
path = ui.configpath(name, b'usercache') |
| 94 |
if path: |
| 95 |
return path |
| 96 |
|
| 97 |
hint = None |
| 98 |
|
| 99 |
if pycompat.iswindows: |
| 100 |
appdata = encoding.environ.get( |
| 101 |
b'LOCALAPPDATA', encoding.environ.get(b'APPDATA') |
| 102 |
) |
| 103 |
if appdata: |
| 104 |
return os.path.join(appdata, name) |
| 105 |
|
| 106 |
hint = _(b"define %s or %s in the environment, or set %s.usercache") % ( |
| 107 |
b"LOCALAPPDATA", |
| 108 |
b"APPDATA", |
| 109 |
name, |
| 110 |
) |
| 111 |
elif pycompat.isdarwin: |
| 112 |
home = encoding.environ.get(b'HOME') |
| 113 |
if home: |
| 114 |
return os.path.join(home, b'Library', b'Caches', name) |
| 115 |
|
| 116 |
hint = _(b"define %s in the environment, or set %s.usercache") % ( |
| 117 |
b"HOME", |
| 118 |
name, |
| 119 |
) |
| 120 |
elif pycompat.isposix: |
| 121 |
path = encoding.environ.get(b'XDG_CACHE_HOME') |
| 122 |
if path: |
| 123 |
return os.path.join(path, name) |
| 124 |
home = encoding.environ.get(b'HOME') |
| 125 |
if home: |
| 126 |
return os.path.join(home, b'.cache', name) |
| 127 |
|
| 128 |
hint = _(b"define %s or %s in the environment, or set %s.usercache") % ( |
| 129 |
b"XDG_CACHE_HOME", |
| 130 |
b"HOME", |
| 131 |
name, |
| 132 |
) |
| 133 |
else: |
| 134 |
raise error.Abort( |
| 135 |
_(b'unknown operating system: %s\n') % pycompat.osname |
| 136 |
) |
| 137 |
|
| 138 |
raise error.Abort(_(b'unknown %s usercache location') % name, hint=hint) |
| 139 |
|
| 140 |
|
| 141 |
def inusercache(ui, hash): |
| 142 |
path = usercachepath(ui, hash) |
| 143 |
return os.path.exists(path) |
| 144 |
|
| 145 |
|
| 146 |
def findfile(repo, hash): |
| 147 |
"""Return store path of the largefile with the specified hash. |
| 148 |
As a side effect, the file might be linked from user cache. |
| 149 |
Return None if the file can't be found locally.""" |
| 150 |
path, exists = findstorepath(repo, hash) |
| 151 |
if exists: |
| 152 |
repo.ui.note(_(b'found %s in store\n') % hash) |
| 153 |
return path |
| 154 |
elif inusercache(repo.ui, hash): |
| 155 |
repo.ui.note(_(b'found %s in system cache\n') % hash) |
| 156 |
path = storepath(repo, hash) |
| 157 |
link(usercachepath(repo.ui, hash), path) |
| 158 |
return path |
| 159 |
return None |
| 160 |
|
| 161 |
|
| 162 |
class largefilesdirstate(dirstate.dirstate): |
| 163 |
_large_file_dirstate = True |
| 164 |
_tr_key_suffix = b'-large-files' |
| 165 |
|
| 166 |
# XXX: why are there overrides to fix the path, if the path should already |
| 167 |
# be in unix form for the superclass? |
| 168 |
|
| 169 |
def set_tracked(self, f, reset_copy=False): |
| 170 |
return super().set_tracked(unixpath(f), reset_copy=reset_copy) |
| 171 |
|
| 172 |
def set_untracked(self, f): |
| 173 |
return super().set_untracked(unixpath(f)) |
| 174 |
|
| 175 |
def _dirignore(self, f): |
| 176 |
return False |
| 177 |
|
| 178 |
def write(self, tr): |
| 179 |
# (1) disable PENDING mode always |
| 180 |
# (lfdirstate isn't yet managed as a part of the transaction) |
| 181 |
# (2) avoid develwarn 'use dirstate.write with ....' |
| 182 |
if tr: |
| 183 |
tr.addbackup(b'largefiles/dirstate', location=b'plain') |
| 184 |
super().write(None) |
| 185 |
|
| 186 |
|
| 187 |
def openlfdirstate(ui, repo, create=True): |
| 188 |
""" |
| 189 |
Return a dirstate object that tracks largefiles: i.e. its root is |
| 190 |
the repo root, but it is saved in .hg/largefiles/dirstate. |
| 191 |
|
| 192 |
If a dirstate object already exists and is being used for a 'changing_*' |
| 193 |
context, it will be returned. |
| 194 |
""" |
| 195 |
sub_dirstate = getattr(repo.dirstate, '_sub_dirstate', None) |
| 196 |
if sub_dirstate is not None: |
| 197 |
return sub_dirstate |
| 198 |
vfs = repo.vfs |
| 199 |
lfstoredir = longname |
| 200 |
opener = vfsmod.vfs(vfs.join(lfstoredir)) |
| 201 |
use_dirstate_v2 = requirements.DIRSTATE_V2_REQUIREMENT in repo.requirements |
| 202 |
lfdirstate = largefilesdirstate( |
| 203 |
opener, |
| 204 |
ui, |
| 205 |
repo.root, |
| 206 |
repo.dirstate._validate, |
| 207 |
lambda: sparse.matcher(repo), |
| 208 |
repo.nodeconstants, |
| 209 |
use_dirstate_v2, |
| 210 |
) |
| 211 |
|
| 212 |
# If the largefiles dirstate does not exist, populate and create |
| 213 |
# it. This ensures that we create it on the first meaningful |
| 214 |
# largefiles operation in a new clone. |
| 215 |
if create and not vfs.exists(vfs.join(lfstoredir, b'dirstate')): |
| 216 |
try: |
| 217 |
with repo.wlock(wait=False), lfdirstate.changing_files(repo): |
| 218 |
matcher = getstandinmatcher(repo) |
| 219 |
standins = repo.dirstate.walk( |
| 220 |
matcher, subrepos=[], unknown=False, ignored=False |
| 221 |
) |
| 222 |
|
| 223 |
if len(standins) > 0: |
| 224 |
vfs.makedirs(lfstoredir) |
| 225 |
|
| 226 |
for standin in standins: |
| 227 |
lfile = splitstandin(standin) |
| 228 |
lfdirstate.hacky_extension_update_file( |
| 229 |
lfile, |
| 230 |
p1_tracked=True, |
| 231 |
wc_tracked=True, |
| 232 |
possibly_dirty=True, |
| 233 |
) |
| 234 |
except error.LockError: |
| 235 |
# Assume that whatever was holding the lock was important. |
| 236 |
# If we were doing something important, we would already have |
| 237 |
# either the lock or a largefile dirstate. |
| 238 |
pass |
| 239 |
return lfdirstate |
| 240 |
|
| 241 |
|
| 242 |
def lfdirstatestatus(lfdirstate, repo): |
| 243 |
pctx = repo[b'.'] |
| 244 |
match = matchmod.always() |
| 245 |
unsure, s, mtime_boundary = lfdirstate.status( |
| 246 |
match, subrepos=[], ignored=False, clean=False, unknown=False |
| 247 |
) |
| 248 |
modified, clean = s.modified, s.clean |
| 249 |
wctx = repo[None] |
| 250 |
for lfile in unsure: |
| 251 |
try: |
| 252 |
fctx = pctx[standin(lfile)] |
| 253 |
except LookupError: |
| 254 |
fctx = None |
| 255 |
if not fctx or readasstandin(fctx) != hashfile(repo.wjoin(lfile)): |
| 256 |
modified.append(lfile) |
| 257 |
else: |
| 258 |
clean.append(lfile) |
| 259 |
st = wctx[lfile].lstat() |
| 260 |
mode = st.st_mode |
| 261 |
size = st.st_size |
| 262 |
mtime = timestamp.reliable_mtime_of(st, mtime_boundary) |
| 263 |
if mtime is not None: |
| 264 |
cache_data = (mode, size, mtime) |
| 265 |
lfdirstate.set_clean(lfile, cache_data) |
| 266 |
return s |
| 267 |
|
| 268 |
|
| 269 |
def listlfiles(repo, rev=None, matcher=None): |
| 270 |
"""return a list of largefiles in the working copy or the |
| 271 |
specified changeset""" |
| 272 |
|
| 273 |
if matcher is None: |
| 274 |
matcher = getstandinmatcher(repo) |
| 275 |
|
| 276 |
# ignore unknown files in working directory |
| 277 |
return [ |
| 278 |
splitstandin(f) |
| 279 |
for f in repo[rev].walk(matcher) |
| 280 |
if rev is not None or repo.dirstate.get_entry(f).any_tracked |
| 281 |
] |
| 282 |
|
| 283 |
|
| 284 |
def instore(repo, hash, forcelocal=False): |
| 285 |
'''Return true if a largefile with the given hash exists in the store''' |
| 286 |
return os.path.exists(storepath(repo, hash, forcelocal)) |
| 287 |
|
| 288 |
|
| 289 |
def storepath(repo, hash, forcelocal=False): |
| 290 |
"""Return the correct location in the repository largefiles store for a |
| 291 |
file with the given hash.""" |
| 292 |
if not forcelocal and repo.shared(): |
| 293 |
return repo.vfs.reljoin(repo.sharedpath, longname, hash) |
| 294 |
return repo.vfs.join(longname, hash) |
| 295 |
|
| 296 |
|
| 297 |
def findstorepath(repo, hash): |
| 298 |
"""Search through the local store path(s) to find the file for the given |
| 299 |
hash. If the file is not found, its path in the primary store is returned. |
| 300 |
The return value is a tuple of (path, exists(path)). |
| 301 |
""" |
| 302 |
# For shared repos, the primary store is in the share source. But for |
| 303 |
# backward compatibility, force a lookup in the local store if it wasn't |
| 304 |
# found in the share source. |
| 305 |
path = storepath(repo, hash, False) |
| 306 |
|
| 307 |
if instore(repo, hash): |
| 308 |
return (path, True) |
| 309 |
elif repo.shared() and instore(repo, hash, True): |
| 310 |
return storepath(repo, hash, True), True |
| 311 |
|
| 312 |
return (path, False) |
| 313 |
|
| 314 |
|
| 315 |
def copyfromcache(repo, hash, filename): |
| 316 |
"""Copy the specified largefile from the repo or system cache to |
| 317 |
filename in the repository. Return true on success or false if the |
| 318 |
file was not found in either cache (which should not happened: |
| 319 |
this is meant to be called only after ensuring that the needed |
| 320 |
largefile exists in the cache).""" |
| 321 |
wvfs = repo.wvfs |
| 322 |
path = findfile(repo, hash) |
| 323 |
if path is None: |
| 324 |
return False |
| 325 |
wvfs.makedirs(wvfs.dirname(wvfs.join(filename))) |
| 326 |
# The write may fail before the file is fully written, but we |
| 327 |
# don't use atomic writes in the working copy. |
| 328 |
with open(path, 'rb') as srcfd, wvfs(filename, b'wb') as destfd: |
| 329 |
gothash = copyandhash(util.filechunkiter(srcfd), destfd) |
| 330 |
if gothash != hash: |
| 331 |
repo.ui.warn( |
| 332 |
_(b'%s: data corruption in %s with hash %s\n') |
| 333 |
% (filename, path, gothash) |
| 334 |
) |
| 335 |
wvfs.unlink(filename) |
| 336 |
return False |
| 337 |
return True |
| 338 |
|
| 339 |
|
| 340 |
def copytostore(repo, ctx, file, fstandin): |
| 341 |
wvfs = repo.wvfs |
| 342 |
hash = readasstandin(ctx[fstandin]) |
| 343 |
if instore(repo, hash): |
| 344 |
return |
| 345 |
if wvfs.exists(file): |
| 346 |
copytostoreabsolute(repo, wvfs.join(file), hash) |
| 347 |
else: |
| 348 |
repo.ui.warn( |
| 349 |
_(b"%s: largefile %s not available from local store\n") |
| 350 |
% (file, hash) |
| 351 |
) |
| 352 |
|
| 353 |
|
| 354 |
def copyalltostore(repo, node): |
| 355 |
'''Copy all largefiles in a given revision to the store''' |
| 356 |
|
| 357 |
ctx = repo[node] |
| 358 |
for filename in ctx.files(): |
| 359 |
realfile = splitstandin(filename) |
| 360 |
if realfile is not None and filename in ctx.manifest(): |
| 361 |
copytostore(repo, ctx, realfile, filename) |
| 362 |
|
| 363 |
|
| 364 |
def copytostoreabsolute(repo, file, hash): |
| 365 |
if inusercache(repo.ui, hash): |
| 366 |
link(usercachepath(repo.ui, hash), storepath(repo, hash)) |
| 367 |
else: |
| 368 |
util.makedirs(os.path.dirname(storepath(repo, hash))) |
| 369 |
with open(file, 'rb') as srcf: |
| 370 |
with util.atomictempfile( |
| 371 |
storepath(repo, hash), createmode=repo.store.createmode |
| 372 |
) as dstf: |
| 373 |
for chunk in util.filechunkiter(srcf): |
| 374 |
dstf.write(chunk) |
| 375 |
linktousercache(repo, hash) |
| 376 |
|
| 377 |
|
| 378 |
def linktousercache(repo, hash): |
| 379 |
"""Link / copy the largefile with the specified hash from the store |
| 380 |
to the cache.""" |
| 381 |
path = usercachepath(repo.ui, hash) |
| 382 |
link(storepath(repo, hash), path) |
| 383 |
|
| 384 |
|
| 385 |
def getstandinmatcher(repo, rmatcher=None): |
| 386 |
'''Return a match object that applies rmatcher to the standin directory''' |
| 387 |
wvfs = repo.wvfs |
| 388 |
standindir = shortname |
| 389 |
|
| 390 |
# no warnings about missing files or directories |
| 391 |
badfn = lambda f, msg: None |
| 392 |
|
| 393 |
if rmatcher and not rmatcher.always(): |
| 394 |
pats = [wvfs.join(standindir, pat) for pat in rmatcher.files()] |
| 395 |
if not pats: |
| 396 |
pats = [wvfs.join(standindir)] |
| 397 |
match = scmutil.match(repo[None], pats, badfn=badfn) |
| 398 |
else: |
| 399 |
# no patterns: relative to repo root |
| 400 |
match = scmutil.match(repo[None], [wvfs.join(standindir)], badfn=badfn) |
| 401 |
return match |
| 402 |
|
| 403 |
|
| 404 |
def composestandinmatcher(repo, rmatcher): |
| 405 |
"""Return a matcher that accepts standins corresponding to the |
| 406 |
files accepted by rmatcher. Pass the list of files in the matcher |
| 407 |
as the paths specified by the user.""" |
| 408 |
smatcher = getstandinmatcher(repo, rmatcher) |
| 409 |
isstandin = smatcher.matchfn |
| 410 |
|
| 411 |
def composedmatchfn(f): |
| 412 |
return isstandin(f) and rmatcher.matchfn(splitstandin(f)) |
| 413 |
|
| 414 |
smatcher._was_tampered_with = True |
| 415 |
smatcher.matchfn = composedmatchfn |
| 416 |
|
| 417 |
return smatcher |
| 418 |
|
| 419 |
|
| 420 |
def standin(filename): |
| 421 |
"""Return the repo-relative path to the standin for the specified big |
| 422 |
file.""" |
| 423 |
# Notes: |
| 424 |
# 1) Some callers want an absolute path, but for instance addlargefiles |
| 425 |
# needs it repo-relative so it can be passed to repo[None].add(). So |
| 426 |
# leave it up to the caller to use repo.wjoin() to get an absolute path. |
| 427 |
# 2) Join with '/' because that's what dirstate always uses, even on |
| 428 |
# Windows. Change existing separator to '/' first in case we are |
| 429 |
# passed filenames from an external source (like the command line). |
| 430 |
return shortnameslash + util.pconvert(filename) |
| 431 |
|
| 432 |
|
| 433 |
def isstandin(filename): |
| 434 |
"""Return true if filename is a big file standin. filename must be |
| 435 |
in Mercurial's internal form (slash-separated).""" |
| 436 |
return filename.startswith(shortnameslash) |
| 437 |
|
| 438 |
|
| 439 |
def splitstandin(filename): |
| 440 |
# Split on / because that's what dirstate always uses, even on Windows. |
| 441 |
# Change local separator to / first just in case we are passed filenames |
| 442 |
# from an external source (like the command line). |
| 443 |
bits = util.pconvert(filename).split(b'/', 1) |
| 444 |
if len(bits) == 2 and bits[0] == shortname: |
| 445 |
return bits[1] |
| 446 |
else: |
| 447 |
return None |
| 448 |
|
| 449 |
|
| 450 |
def updatestandin(repo, lfile, standin): |
| 451 |
"""Re-calculate hash value of lfile and write it into standin |
| 452 |
|
| 453 |
This assumes that "lfutil.standin(lfile) == standin", for efficiency. |
| 454 |
""" |
| 455 |
file = repo.wjoin(lfile) |
| 456 |
if repo.wvfs.exists(lfile): |
| 457 |
hash = hashfile(file) |
| 458 |
executable = getexecutable(file) |
| 459 |
writestandin(repo, standin, hash, executable) |
| 460 |
else: |
| 461 |
raise error.Abort(_(b'%s: file not found!') % lfile) |
| 462 |
|
| 463 |
|
| 464 |
def readasstandin(fctx): |
| 465 |
"""read hex hash from given filectx of standin file |
| 466 |
|
| 467 |
This encapsulates how "standin" data is stored into storage layer.""" |
| 468 |
return fctx.data().strip() |
| 469 |
|
| 470 |
|
| 471 |
def writestandin(repo, standin, hash, executable): |
| 472 |
'''write hash to <repo.root>/<standin>''' |
| 473 |
repo.wwrite(standin, hash + b'\n', executable and b'x' or b'') |
| 474 |
|
| 475 |
|
| 476 |
def copyandhash(instream, outfile): |
| 477 |
"""Read bytes from instream (iterable) and write them to outfile, |
| 478 |
computing the SHA-1 hash of the data along the way. Return the hash.""" |
| 479 |
hasher = hashutil.sha1(b'') |
| 480 |
for data in instream: |
| 481 |
hasher.update(data) |
| 482 |
outfile.write(data) |
| 483 |
return hex(hasher.digest()) |
| 484 |
|
| 485 |
|
| 486 |
def hashfile(file): |
| 487 |
if not os.path.exists(file): |
| 488 |
return b'' |
| 489 |
with open(file, 'rb') as fd: |
| 490 |
return hexsha1(fd) |
| 491 |
|
| 492 |
|
| 493 |
def getexecutable(filename): |
| 494 |
mode = os.stat(filename).st_mode |
| 495 |
return ( |
| 496 |
(mode & stat.S_IXUSR) |
| 497 |
and (mode & stat.S_IXGRP) |
| 498 |
and (mode & stat.S_IXOTH) |
| 499 |
) |
| 500 |
|
| 501 |
|
| 502 |
def urljoin(first, second, *arg): |
| 503 |
def join(left, right): |
| 504 |
if not left.endswith(b'/'): |
| 505 |
left += b'/' |
| 506 |
if right.startswith(b'/'): |
| 507 |
right = right[1:] |
| 508 |
return left + right |
| 509 |
|
| 510 |
url = join(first, second) |
| 511 |
for a in arg: |
| 512 |
url = join(url, a) |
| 513 |
return url |
| 514 |
|
| 515 |
|
| 516 |
def hexsha1(fileobj): |
| 517 |
"""hexsha1 returns the hex-encoded sha1 sum of the data in the file-like |
| 518 |
object data""" |
| 519 |
h = hashutil.sha1() |
| 520 |
for chunk in util.filechunkiter(fileobj): |
| 521 |
h.update(chunk) |
| 522 |
return hex(h.digest()) |
| 523 |
|
| 524 |
|
| 525 |
def httpsendfile(ui, filename): |
| 526 |
return httpconnection.httpsendfile(ui, filename, b'rb') |
| 527 |
|
| 528 |
|
| 529 |
def unixpath(path): |
| 530 |
'''Return a version of path normalized for use with the lfdirstate.''' |
| 531 |
return util.pconvert(os.path.normpath(path)) |
| 532 |
|
| 533 |
|
| 534 |
def islfilesrepo(repo): |
| 535 |
'''Return true if the repo is a largefile repo.''' |
| 536 |
if b'largefiles' in repo.requirements: |
| 537 |
for entry in repo.store.data_entries(): |
| 538 |
if entry.is_revlog and shortnameslash in entry.target_id: |
| 539 |
return True |
| 540 |
|
| 541 |
return any(openlfdirstate(repo.ui, repo, False)) |
| 542 |
|
| 543 |
|
| 544 |
class storeprotonotcapable(Exception): |
| 545 |
def __init__(self, storetypes): |
| 546 |
self.storetypes = storetypes |
| 547 |
|
| 548 |
|
| 549 |
def getstandinsstate(repo): |
| 550 |
standins = [] |
| 551 |
matcher = getstandinmatcher(repo) |
| 552 |
wctx = repo[None] |
| 553 |
for standin in repo.dirstate.walk( |
| 554 |
matcher, subrepos=[], unknown=False, ignored=False |
| 555 |
): |
| 556 |
lfile = splitstandin(standin) |
| 557 |
try: |
| 558 |
hash = readasstandin(wctx[standin]) |
| 559 |
except OSError: |
| 560 |
hash = None |
| 561 |
standins.append((lfile, hash)) |
| 562 |
return standins |
| 563 |
|
| 564 |
|
| 565 |
def synclfdirstate(repo, lfdirstate, lfile, normallookup): |
| 566 |
lfstandin = standin(lfile) |
| 567 |
if lfstandin not in repo.dirstate: |
| 568 |
lfdirstate.hacky_extension_update_file( |
| 569 |
lfile, |
| 570 |
p1_tracked=False, |
| 571 |
wc_tracked=False, |
| 572 |
) |
| 573 |
else: |
| 574 |
entry = repo.dirstate.get_entry(lfstandin) |
| 575 |
lfdirstate.hacky_extension_update_file( |
| 576 |
lfile, |
| 577 |
wc_tracked=entry.tracked, |
| 578 |
p1_tracked=entry.p1_tracked, |
| 579 |
p2_info=entry.p2_info, |
| 580 |
possibly_dirty=True, |
| 581 |
) |
| 582 |
|
| 583 |
|
| 584 |
def markcommitted(orig, ctx, node): |
| 585 |
repo = ctx.repo() |
| 586 |
|
| 587 |
with repo.dirstate.changing_parents(repo): |
| 588 |
orig(node) |
| 589 |
|
| 590 |
# ATTENTION: "ctx.files()" may differ from "repo[node].files()" |
| 591 |
# because files coming from the 2nd parent are omitted in the latter. |
| 592 |
# |
| 593 |
# The former should be used to get targets of "synclfdirstate", |
| 594 |
# because such files: |
| 595 |
# - are marked as "a" by "patch.patch()" (e.g. via transplant), and |
| 596 |
# - have to be marked as "n" after commit, but |
| 597 |
# - aren't listed in "repo[node].files()" |
| 598 |
|
| 599 |
lfdirstate = openlfdirstate(repo.ui, repo) |
| 600 |
for f in ctx.files(): |
| 601 |
lfile = splitstandin(f) |
| 602 |
if lfile is not None: |
| 603 |
synclfdirstate(repo, lfdirstate, lfile, False) |
| 604 |
|
| 605 |
# As part of committing, copy all of the largefiles into the cache. |
| 606 |
# |
| 607 |
# Using "node" instead of "ctx" implies additional "repo[node]" |
| 608 |
# lookup while copyalltostore(), but can omit redundant check for |
| 609 |
# files comming from the 2nd parent, which should exist in store |
| 610 |
# at merging. |
| 611 |
copyalltostore(repo, node) |
| 612 |
|
| 613 |
|
| 614 |
def getlfilestoupdate(oldstandins, newstandins): |
| 615 |
changedstandins = set(oldstandins).symmetric_difference(set(newstandins)) |
| 616 |
filelist = [] |
| 617 |
for f in changedstandins: |
| 618 |
if f[0] not in filelist: |
| 619 |
filelist.append(f[0]) |
| 620 |
return filelist |
| 621 |
|
| 622 |
|
| 623 |
def getlfilestoupload(repo, missing, addfunc): |
| 624 |
makeprogress = repo.ui.makeprogress |
| 625 |
with makeprogress( |
| 626 |
_(b'finding outgoing largefiles'), |
| 627 |
unit=_(b'revisions'), |
| 628 |
total=len(missing), |
| 629 |
) as progress: |
| 630 |
for i, n in enumerate(missing): |
| 631 |
progress.update(i) |
| 632 |
parents = [p for p in repo[n].parents() if p != repo.nullid] |
| 633 |
|
| 634 |
with lfstatus(repo, value=False): |
| 635 |
ctx = repo[n] |
| 636 |
|
| 637 |
files = set(ctx.files()) |
| 638 |
if len(parents) == 2: |
| 639 |
mc = ctx.manifest() |
| 640 |
mp1 = ctx.p1().manifest() |
| 641 |
mp2 = ctx.p2().manifest() |
| 642 |
for f in mp1: |
| 643 |
if f not in mc: |
| 644 |
files.add(f) |
| 645 |
for f in mp2: |
| 646 |
if f not in mc: |
| 647 |
files.add(f) |
| 648 |
for f in mc: |
| 649 |
if mc[f] != mp1.get(f, None) or mc[f] != mp2.get(f, None): |
| 650 |
files.add(f) |
| 651 |
for fn in files: |
| 652 |
if isstandin(fn) and fn in ctx: |
| 653 |
addfunc(fn, readasstandin(ctx[fn])) |
| 654 |
|
| 655 |
|
| 656 |
def updatestandinsbymatch(repo, match): |
| 657 |
"""Update standins in the working directory according to specified match |
| 658 |
|
| 659 |
This returns (possibly modified) ``match`` object to be used for |
| 660 |
subsequent commit process. |
| 661 |
""" |
| 662 |
|
| 663 |
ui = repo.ui |
| 664 |
|
| 665 |
# Case 1: user calls commit with no specific files or |
| 666 |
# include/exclude patterns: refresh and commit all files that |
| 667 |
# are "dirty". |
| 668 |
if match is None or match.always(): |
| 669 |
# Spend a bit of time here to get a list of files we know |
| 670 |
# are modified so we can compare only against those. |
| 671 |
# It can cost a lot of time (several seconds) |
| 672 |
# otherwise to update all standins if the largefiles are |
| 673 |
# large. |
| 674 |
dirtymatch = matchmod.always() |
| 675 |
with repo.dirstate.running_status(repo): |
| 676 |
lfdirstate = openlfdirstate(ui, repo) |
| 677 |
unsure, s, mtime_boundary = lfdirstate.status( |
| 678 |
dirtymatch, |
| 679 |
subrepos=[], |
| 680 |
ignored=False, |
| 681 |
clean=False, |
| 682 |
unknown=False, |
| 683 |
) |
| 684 |
modifiedfiles = unsure + s.modified + s.added + s.removed |
| 685 |
lfiles = listlfiles(repo) |
| 686 |
# this only loops through largefiles that exist (not |
| 687 |
# removed/renamed) |
| 688 |
for lfile in lfiles: |
| 689 |
if lfile in modifiedfiles: |
| 690 |
fstandin = standin(lfile) |
| 691 |
if repo.wvfs.exists(fstandin): |
| 692 |
# this handles the case where a rebase is being |
| 693 |
# performed and the working copy is not updated |
| 694 |
# yet. |
| 695 |
if repo.wvfs.exists(lfile): |
| 696 |
updatestandin(repo, lfile, fstandin) |
| 697 |
|
| 698 |
return match |
| 699 |
|
| 700 |
lfiles = listlfiles(repo) |
| 701 |
match._was_tampered_with = True |
| 702 |
match._files = repo._subdirlfs(match.files(), lfiles) |
| 703 |
|
| 704 |
# Case 2: user calls commit with specified patterns: refresh |
| 705 |
# any matching big files. |
| 706 |
smatcher = composestandinmatcher(repo, match) |
| 707 |
standins = repo.dirstate.walk( |
| 708 |
smatcher, subrepos=[], unknown=False, ignored=False |
| 709 |
) |
| 710 |
|
| 711 |
# No matching big files: get out of the way and pass control to |
| 712 |
# the usual commit() method. |
| 713 |
if not standins: |
| 714 |
return match |
| 715 |
|
| 716 |
# Refresh all matching big files. It's possible that the |
| 717 |
# commit will end up failing, in which case the big files will |
| 718 |
# stay refreshed. No harm done: the user modified them and |
| 719 |
# asked to commit them, so sooner or later we're going to |
| 720 |
# refresh the standins. Might as well leave them refreshed. |
| 721 |
lfdirstate = openlfdirstate(ui, repo) |
| 722 |
for fstandin in standins: |
| 723 |
lfile = splitstandin(fstandin) |
| 724 |
if lfdirstate.get_entry(lfile).tracked: |
| 725 |
updatestandin(repo, lfile, fstandin) |
| 726 |
|
| 727 |
# Cook up a new matcher that only matches regular files or |
| 728 |
# standins corresponding to the big files requested by the |
| 729 |
# user. Have to modify _files to prevent commit() from |
| 730 |
# complaining "not tracked" for big files. |
| 731 |
match = copy.copy(match) |
| 732 |
match._was_tampered_with = True |
| 733 |
origmatchfn = match.matchfn |
| 734 |
|
| 735 |
# Check both the list of largefiles and the list of |
| 736 |
# standins because if a largefile was removed, it |
| 737 |
# won't be in the list of largefiles at this point |
| 738 |
match._files += sorted(standins) |
| 739 |
|
| 740 |
actualfiles = [] |
| 741 |
for f in match._files: |
| 742 |
fstandin = standin(f) |
| 743 |
|
| 744 |
# For largefiles, only one of the normal and standin should be |
| 745 |
# committed (except if one of them is a remove). In the case of a |
| 746 |
# standin removal, drop the normal file if it is unknown to dirstate. |
| 747 |
# Thus, skip plain largefile names but keep the standin. |
| 748 |
if f in lfiles or fstandin in standins: |
| 749 |
if not repo.dirstate.get_entry(fstandin).removed: |
| 750 |
if not repo.dirstate.get_entry(f).removed: |
| 751 |
continue |
| 752 |
elif not repo.dirstate.get_entry(f).any_tracked: |
| 753 |
continue |
| 754 |
|
| 755 |
actualfiles.append(f) |
| 756 |
match._files = actualfiles |
| 757 |
|
| 758 |
def matchfn(f): |
| 759 |
if origmatchfn(f): |
| 760 |
return f not in lfiles |
| 761 |
else: |
| 762 |
return f in standins |
| 763 |
|
| 764 |
match.matchfn = matchfn |
| 765 |
|
| 766 |
return match |
| 767 |
|
| 768 |
|
| 769 |
class automatedcommithook: |
| 770 |
"""Stateful hook to update standins at the 1st commit of resuming |
| 771 |
|
| 772 |
For efficiency, updating standins in the working directory should |
| 773 |
be avoided while automated committing (like rebase, transplant and |
| 774 |
so on), because they should be updated before committing. |
| 775 |
|
| 776 |
But the 1st commit of resuming automated committing (e.g. ``rebase |
| 777 |
--continue``) should update them, because largefiles may be |
| 778 |
modified manually. |
| 779 |
""" |
| 780 |
|
| 781 |
def __init__(self, resuming): |
| 782 |
self.resuming = resuming |
| 783 |
|
| 784 |
def __call__(self, repo, match): |
| 785 |
if self.resuming: |
| 786 |
self.resuming = False # avoids updating at subsequent commits |
| 787 |
return updatestandinsbymatch(repo, match) |
| 788 |
else: |
| 789 |
return match |
| 790 |
|
| 791 |
|
| 792 |
def getstatuswriter(ui, repo, forcibly=None): |
| 793 |
"""Return the function to write largefiles specific status out |
| 794 |
|
| 795 |
If ``forcibly`` is ``None``, this returns the last element of |
| 796 |
``repo._lfstatuswriters`` as "default" writer function. |
| 797 |
|
| 798 |
Otherwise, this returns the function to always write out (or |
| 799 |
ignore if ``not forcibly``) status. |
| 800 |
""" |
| 801 |
if forcibly is None and hasattr(repo, '_largefilesenabled'): |
| 802 |
return repo._lfstatuswriters[-1] |
| 803 |
else: |
| 804 |
if forcibly: |
| 805 |
return ui.status # forcibly WRITE OUT |
| 806 |
else: |
| 807 |
return lambda *msg, **opts: None # forcibly IGNORE |