##// END OF EJS Templates
cmdutil: bail_if_changed to bailifchanged
cmdutil: bail_if_changed to bailifchanged

File last commit:

r14235:b9e1b041 default
r14289:d68ddccf default
Show More
relink.py
184 lines | 6.0 KiB | text/x-python | PythonLexer
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
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
Matt Mackall
Update license to GPLv2+
r10263 # GNU General Public License version 2 or any later version.
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729
"""recreates hardlinks between repository clones"""
Brodie Rao
cleanup: remove unused imports
r12062 from mercurial import hg, util
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 from mercurial.i18n import _
import os, stat
def relink(ui, repo, origin=None, **opts):
"""recreate hardlinks between two repositories
Martin Geisler
relink: wrap long lines in docstring
r9886 When repositories are cloned locally, their data files will be
hardlinked so that they only use the space of a single repository.
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729
Martin Geisler
relink: wrap long lines in docstring
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.
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729
Martin Geisler
relink: wrap long lines in docstring
r9886 Similarly, passing --rev to "hg clone" will fail to use any
hardlinks, falling back to a complete copy of the source
repository.
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729
Martin Geisler
relink: wrap long lines in docstring
r9886 This command lets you recreate those hardlinks and reclaim that
wasted space.
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729
Martin Geisler
relink: wrap long lines in docstring
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].
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729
Martin Geisler
relink: wrap long lines in docstring
r9886 Do not attempt any read operations on this repository while the
command is running. (Both repositories will be locked against
writes.)
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 """
Siddharth Agarwal
Add support for relinking on Windows....
r10218 if not hasattr(util, 'samefile') or not hasattr(util, 'samedevice'):
raise util.Abort(_('hardlinks are not supported on this system'))
Martin Geisler
relink: correct unusual indentation
r13898 src = hg.repository(hg.remoteui(repo, opts),
ui.expandpath(origin or 'default-relink',
origin or 'default'))
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 if not src.local():
Martin Geisler
relink: mark abort message for translation
r13658 raise util.Abort(_('must specify local origin repository'))
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 ui.status(_('relinking %s to %s\n') % (src.store.path, repo.store.path))
Martin Geisler
relink: avoid trying to lock the same repo twice
r13657 if repo.root == src.root:
ui.status(_('there is nothing to relink\n'))
return
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 locallock = repo.lock()
try:
remotelock = src.lock()
try:
timeless
relink/progress: Adding progress for collecting stage
r11355 candidates = sorted(collect(src, ui))
Siddharth Agarwal
Add support for relinking on Windows....
r10218 targets = prune(candidates, src.store.path, repo.store.path, ui)
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
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 = []
timeless
relink/progress: Adding progress for collecting stage
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))
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 for dirpath, dirnames, filenames in os.walk(src):
Martin Geisler
relink: ensure deterministic directory walk in collect
r11357 dirnames.sort()
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 relpath = dirpath[len(src) + seplen:]
Martin Geisler
relink: ensure deterministic directory walk in collect
r11357 for filename in sorted(filenames):
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 if not filename[-2:] in ('.d', '.i'):
continue
st = os.stat(os.path.join(dirpath, filename))
if not stat.S_ISREG(st.st_mode):
continue
timeless
relink/progress: Adding progress for collecting stage
r11355 pos += 1
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 candidates.append((os.path.join(relpath, filename), st))
timeless
relink/progress: Adding progress for collecting stage
r11355 ui.progress(_('collecting'), pos, filename, _('files'), total)
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729
timeless
relink/progress: Adding progress for collecting stage
r11355 ui.progress(_('collecting'), None)
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 ui.status(_('collected %d candidate storage files\n') % len(candidates))
return candidates
Siddharth Agarwal
Add support for relinking on Windows....
r10218 def prune(candidates, src, dst, ui):
def linkfilter(src, dst, st):
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 try:
ts = os.stat(dst)
except OSError:
# Destination doesn't have this file?
return False
Siddharth Agarwal
Add support for relinking on Windows....
r10218 if util.samefile(src, dst):
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 return False
Siddharth Agarwal
Add support for relinking on Windows....
r10218 if not util.samedevice(src, dst):
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 # No point in continuing
raise util.Abort(
_('source and destination are on different devices'))
if st.st_size != ts.st_size:
return False
return st
targets = []
timeless
relink/progress: Adding progress for pruning stage
r11354 total = len(candidates)
pos = 0
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 for fn, st in candidates:
timeless
relink/progress: Adding progress for pruning stage
r11354 pos += 1
Siddharth Agarwal
Add support for relinking on Windows....
r10218 srcpath = os.path.join(src, fn)
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 tgt = os.path.join(dst, fn)
Siddharth Agarwal
Add support for relinking on Windows....
r10218 ts = linkfilter(srcpath, tgt, st)
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 if not ts:
ui.debug(_('not linkable: %s\n') % fn)
continue
targets.append((fn, ts.st_size))
timeless
progress: dropping superfluous space from units
r12744 ui.progress(_('pruning'), pos, fn, _('files'), total)
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729
timeless
relink/progress: Adding progress for pruning stage
r11354 ui.progress(_('pruning'), None)
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
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:
Adrian Buehlmann
rename util.os_link to oslink
r14235 util.oslink(src, dst)
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
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)
Siddharth Agarwal
Add support for relinking on Windows....
r10218 # Binary mode, so that read() works correctly, especially on Windows
sfp = file(source, 'rb')
dfp = file(tgt, 'rb')
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 sin = sfp.read(CHUNKLEN)
while sin:
din = dfp.read(CHUNKLEN)
if sin != din:
break
sin = sfp.read(CHUNKLEN)
Siddharth Agarwal
Add support for relinking on Windows....
r10218 sfp.close()
dfp.close()
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 if sin:
ui.debug(_('not linkable: %s\n') % f)
continue
try:
relinkfile(source, tgt)
timeless
progress: dropping superfluous space from units
r12744 ui.progress(_('relinking'), pos, f, _('files'), total)
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 relinked += 1
savedbytes += sz
except OSError, inst:
Martin Geisler
relink: do not translate format string with no text
r9790 ui.warn('%s: %s\n' % (tgt, str(inst)))
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729
Matt Mackall
progress: drop extra args for pos=None calls (issue2087)
r10724 ui.progress(_('relinking'), None)
Augie Fackler
relink: properly use the progress API
r10424
Martin Geisler
relink: format reclaimed byte count nicely
r13656 ui.status(_('relinked %d files (%s reclaimed)\n') %
(relinked, util.bytecount(savedbytes)))
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729
cmdtable = {
'relink': (
relink,
[],
_('[ORIGIN]')
)
}