relink.py
187 lines
| 6.3 KiB
| text/x-python
|
PythonLexer
/ hgext / relink.py
|
r9729 | # Mercurial extension to provide 'hg relink' command | ||
# | ||||
# Copyright (C) 2007 Brendan Cully <brendan@kublai.com> | ||||
# | ||||
# This software may be used and distributed according to the terms of the | ||||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r9729 | |||
"""recreates hardlinks between repository clones""" | ||||
|
r26587 | from mercurial import cmdutil, hg, util, error | ||
|
r9729 | from mercurial.i18n import _ | ||
import os, stat | ||||
|
r21252 | cmdtable = {} | ||
command = cmdutil.command(cmdtable) | ||||
|
r25186 | # Note for extension authors: ONLY specify testedwith = 'internal' for | ||
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should | ||||
# be specifying the version(s) of Mercurial they are tested with, or | ||||
# leave the attribute unspecified. | ||||
|
r16743 | testedwith = 'internal' | ||
|
r21252 | @command('relink', [], _('[ORIGIN]')) | ||
|
r9729 | def relink(ui, repo, origin=None, **opts): | ||
"""recreate hardlinks between two repositories | ||||
|
r9886 | When repositories are cloned locally, their data files will be | ||
hardlinked so that they only use the space of a single repository. | ||||
|
r9729 | |||
|
r9886 | Unfortunately, subsequent pulls into either repository will break | ||
hardlinks for any files touched by the new changesets, even if | ||||
both repositories end up pulling the same changes. | ||||
|
r9729 | |||
|
r9886 | Similarly, passing --rev to "hg clone" will fail to use any | ||
hardlinks, falling back to a complete copy of the source | ||||
repository. | ||||
|
r9729 | |||
|
r9886 | This command lets you recreate those hardlinks and reclaim that | ||
wasted space. | ||||
|
r9729 | |||
|
r9886 | This repository will be relinked to share space with ORIGIN, which | ||
must be on the same local disk. If ORIGIN is omitted, looks for | ||||
"default-relink", then "default", in [paths]. | ||||
|
r9729 | |||
|
r9886 | Do not attempt any read operations on this repository while the | ||
command is running. (Both repositories will be locked against | ||||
writes.) | ||||
|
r9729 | """ | ||
|
r14945 | if (not util.safehasattr(util, 'samefile') or | ||
not util.safehasattr(util, 'samedevice')): | ||||
|
r26587 | raise error.Abort(_('hardlinks are not supported on this system')) | ||
|
r18825 | src = hg.repository(repo.baseui, ui.expandpath(origin or 'default-relink', | ||
|
r14556 | origin or 'default')) | ||
|
r9729 | ui.status(_('relinking %s to %s\n') % (src.store.path, repo.store.path)) | ||
|
r13657 | if repo.root == src.root: | ||
ui.status(_('there is nothing to relink\n')) | ||||
return | ||||
|
r20083 | if not util.samedevice(src.store.path, repo.store.path): | ||
# No point in continuing | ||||
|
r26587 | raise error.Abort(_('source and destination are on different devices')) | ||
|
r20083 | |||
|
r9729 | locallock = repo.lock() | ||
try: | ||||
remotelock = src.lock() | ||||
try: | ||||
|
r11355 | candidates = sorted(collect(src, ui)) | ||
|
r10218 | targets = prune(candidates, src.store.path, repo.store.path, ui) | ||
|
r9729 | do_relink(src.store.path, repo.store.path, targets, ui) | ||
finally: | ||||
remotelock.release() | ||||
finally: | ||||
locallock.release() | ||||
def collect(src, ui): | ||||
seplen = len(os.path.sep) | ||||
candidates = [] | ||||
|
r11355 | live = len(src['tip'].manifest()) | ||
# Your average repository has some files which were deleted before | ||||
# the tip revision. We account for that by assuming that there are | ||||
# 3 tracked files for every 2 live files as of the tip version of | ||||
# the repository. | ||||
# | ||||
# mozilla-central as of 2010-06-10 had a ratio of just over 7:5. | ||||
total = live * 3 // 2 | ||||
src = src.store.path | ||||
pos = 0 | ||||
ui.status(_("tip has %d files, estimated total number of files: %s\n") | ||||
% (live, total)) | ||||
|
r9729 | for dirpath, dirnames, filenames in os.walk(src): | ||
|
r11357 | dirnames.sort() | ||
|
r9729 | relpath = dirpath[len(src) + seplen:] | ||
|
r11357 | for filename in sorted(filenames): | ||
|
r16686 | if filename[-2:] not in ('.d', '.i'): | ||
|
r9729 | continue | ||
st = os.stat(os.path.join(dirpath, filename)) | ||||
if not stat.S_ISREG(st.st_mode): | ||||
continue | ||||
|
r11355 | pos += 1 | ||
|
r9729 | candidates.append((os.path.join(relpath, filename), st)) | ||
|
r11355 | ui.progress(_('collecting'), pos, filename, _('files'), total) | ||
|
r9729 | |||
|
r11355 | ui.progress(_('collecting'), None) | ||
|
r9729 | ui.status(_('collected %d candidate storage files\n') % len(candidates)) | ||
return candidates | ||||
|
r10218 | def prune(candidates, src, dst, ui): | ||
def linkfilter(src, dst, st): | ||||
|
r9729 | try: | ||
ts = os.stat(dst) | ||||
except OSError: | ||||
# Destination doesn't have this file? | ||||
return False | ||||
|
r10218 | if util.samefile(src, dst): | ||
|
r9729 | return False | ||
|
r10218 | if not util.samedevice(src, dst): | ||
|
r9729 | # No point in continuing | ||
|
r26587 | raise error.Abort( | ||
|
r9729 | _('source and destination are on different devices')) | ||
if st.st_size != ts.st_size: | ||||
return False | ||||
return st | ||||
targets = [] | ||||
|
r11354 | total = len(candidates) | ||
pos = 0 | ||||
|
r9729 | for fn, st in candidates: | ||
|
r11354 | pos += 1 | ||
|
r10218 | srcpath = os.path.join(src, fn) | ||
|
r9729 | tgt = os.path.join(dst, fn) | ||
|
r10218 | ts = linkfilter(srcpath, tgt, st) | ||
|
r9729 | if not ts: | ||
|
r14709 | ui.debug('not linkable: %s\n' % fn) | ||
|
r9729 | continue | ||
targets.append((fn, ts.st_size)) | ||||
|
r12744 | ui.progress(_('pruning'), pos, fn, _('files'), total) | ||
|
r9729 | |||
|
r11354 | ui.progress(_('pruning'), None) | ||
|
r9729 | ui.status(_('pruned down to %d probably relinkable files\n') % len(targets)) | ||
return targets | ||||
def do_relink(src, dst, files, ui): | ||||
def relinkfile(src, dst): | ||||
bak = dst + '.bak' | ||||
os.rename(dst, bak) | ||||
try: | ||||
|
r14235 | util.oslink(src, dst) | ||
|
r9729 | except OSError: | ||
os.rename(bak, dst) | ||||
raise | ||||
os.remove(bak) | ||||
CHUNKLEN = 65536 | ||||
relinked = 0 | ||||
savedbytes = 0 | ||||
pos = 0 | ||||
total = len(files) | ||||
for f, sz in files: | ||||
pos += 1 | ||||
source = os.path.join(src, f) | ||||
tgt = os.path.join(dst, f) | ||||
|
r10218 | # Binary mode, so that read() works correctly, especially on Windows | ||
sfp = file(source, 'rb') | ||||
dfp = file(tgt, 'rb') | ||||
|
r9729 | sin = sfp.read(CHUNKLEN) | ||
while sin: | ||||
din = dfp.read(CHUNKLEN) | ||||
if sin != din: | ||||
break | ||||
sin = sfp.read(CHUNKLEN) | ||||
|
r10218 | sfp.close() | ||
dfp.close() | ||||
|
r9729 | if sin: | ||
|
r14709 | ui.debug('not linkable: %s\n' % f) | ||
|
r9729 | continue | ||
try: | ||||
relinkfile(source, tgt) | ||||
|
r12744 | ui.progress(_('relinking'), pos, f, _('files'), total) | ||
|
r9729 | relinked += 1 | ||
savedbytes += sz | ||||
|
r25660 | except OSError as inst: | ||
|
r9790 | ui.warn('%s: %s\n' % (tgt, str(inst))) | ||
|
r9729 | |||
|
r10724 | ui.progress(_('relinking'), None) | ||
|
r10424 | |||
|
r13656 | ui.status(_('relinked %d files (%s reclaimed)\n') % | ||
(relinked, util.bytecount(savedbytes))) | ||||