##// END OF EJS Templates
peer: stop having a `peer()` method on `peer()`...
peer: stop having a `peer()` method on `peer()` This is already a peer, why do you want a peer if you already have one.

File last commit:

r50625:c6ae9051 default
r50643:78af51ba default
Show More
relink.py
214 lines | 6.6 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"""
timeless
relink: use absolute_import
r28380 import os
import stat
Yuya Nishihara
py3: move up symbol imports to enforce import-checker rules...
r29205 from mercurial.i18n import _
Gregory Szorc
py3: manually import pycompat.open into files that need it...
r43355 from mercurial.pycompat import open
timeless
relink: use absolute_import
r28380 from mercurial import (
error,
hg,
Yuya Nishihara
registrar: move cmdutil.command to registrar module (API)...
r32337 registrar,
timeless
relink: use absolute_import
r28380 util,
)
relink: use `get_unique_pull_path`...
r47704 from mercurial.utils import (
stringutil,
urlutil,
)
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729
Gregory Szorc
relink: declare command using decorator
r21252 cmdtable = {}
Yuya Nishihara
registrar: move cmdutil.command to registrar module (API)...
r32337 command = registrar.command(cmdtable)
Augie Fackler
extensions: change magic "shipped with hg" string...
r29841 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
Augie Fackler
extensions: document that `testedwith = 'internal'` is special...
r25186 # 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.
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 testedwith = b'ships-with-hg-core'
Augie Fackler
hgext: mark all first-party extensions as such
r16743
Augie Fackler
formatting: blacken the codebase...
r43346
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 @command(
b'relink', [], _(b'[ORIGIN]'), helpcategory=command.CATEGORY_MAINTENANCE
)
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 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 """
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if not util.safehasattr(util, b'samefile') or not util.safehasattr(
util, b'samedevice'
Augie Fackler
formatting: blacken the codebase...
r43346 ):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.Abort(_(b'hardlinks are not supported on this system'))
relink: use `get_unique_pull_path`...
r47704
if origin is None and b'default-relink' in ui.paths:
origin = b'default-relink'
path: use `get_unique_pull_path_obj` in `hg relink`...
r50625 path = urlutil.get_unique_pull_path_obj(b'relink', ui, origin)
src = hg.repository(repo.baseui, path.loc)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 ui.status(_(b'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:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 ui.status(_(b'there is nothing to relink\n'))
Martin Geisler
relink: avoid trying to lock the same repo twice
r13657 return
Simon Heimberg
relink: abort earlier when on different devices (issue3916)...
r20083 if not util.samedevice(src.store.path, repo.store.path):
# No point in continuing
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.Abort(_(b'source and destination are on different devices'))
Simon Heimberg
relink: abort earlier when on different devices (issue3916)...
r20083
Matt Harbison
relink: use context manager for lock management
r38461 with repo.lock(), src.lock():
candidates = sorted(collect(src, ui))
targets = prune(candidates, src.store.path, repo.store.path, ui)
do_relink(src.store.path, repo.store.path, targets, ui)
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729
Augie Fackler
formatting: blacken the codebase...
r43346
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 def collect(src, ui):
seplen = len(os.path.sep)
candidates = []
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 live = len(src[b'tip'].manifest())
timeless
relink/progress: Adding progress for collecting stage
r11355 # 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
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 progress = ui.makeprogress(_(b'collecting'), unit=_(b'files'), total=total)
timeless
relink/progress: Adding progress for collecting stage
r11355 pos = 0
Augie Fackler
formatting: blacken the codebase...
r43346 ui.status(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 _(b"tip has %d files, estimated total number of files: %d\n")
Augie Fackler
formatting: blacken the codebase...
r43346 % (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()
Augie Fackler
formatting: blacken the codebase...
r43346 relpath = dirpath[len(src) + seplen :]
Martin Geisler
relink: ensure deterministic directory walk in collect
r11357 for filename in sorted(filenames):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if filename[-2:] not in (b'.d', b'.i'):
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 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))
Martin von Zweigbergk
relink: use progress helper...
r38423 progress.update(pos, item=filename)
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729
Martin von Zweigbergk
relink: use progress helper...
r38423 progress.complete()
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 ui.status(_(b'collected %d candidate storage files\n') % len(candidates))
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 return candidates
Augie Fackler
formatting: blacken the codebase...
r43346
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
Pierre-Yves David
error: get Abort from 'error' instead of 'util'...
r26587 raise error.Abort(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 _(b'source and destination are on different devices')
Augie Fackler
formatting: blacken the codebase...
r43346 )
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 if st.st_size != ts.st_size:
return False
return st
targets = []
Augie Fackler
formatting: blacken the codebase...
r43346 progress = ui.makeprogress(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 _(b'pruning'), unit=_(b'files'), total=len(candidates)
Augie Fackler
formatting: blacken the codebase...
r43346 )
timeless
relink/progress: Adding progress for pruning stage
r11354 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:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 ui.debug(b'not linkable: %s\n' % fn)
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 continue
targets.append((fn, ts.st_size))
Martin von Zweigbergk
relink: use progress helper...
r38423 progress.update(pos, item=fn)
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729
Martin von Zweigbergk
relink: use progress helper...
r38423 progress.complete()
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 ui.status(
_(b'pruned down to %d probably relinkable files\n') % len(targets)
)
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 return targets
Augie Fackler
formatting: blacken the codebase...
r43346
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 def do_relink(src, dst, files, ui):
def relinkfile(src, dst):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 bak = dst + b'.bak'
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 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
Augie Fackler
formatting: blacken the codebase...
r43346 progress = ui.makeprogress(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 _(b'relinking'), unit=_(b'files'), total=len(files)
Augie Fackler
formatting: blacken the codebase...
r43346 )
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 pos = 0
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
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 sfp = open(source, b'rb')
dfp = open(tgt, b'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:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 ui.debug(b'not linkable: %s\n' % f)
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 continue
try:
relinkfile(source, tgt)
Martin von Zweigbergk
relink: use progress helper...
r38423 progress.update(pos, item=f)
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 relinked += 1
savedbytes += sz
Gregory Szorc
global: mass rewrite to use modern exception syntax...
r25660 except OSError as inst:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 ui.warn(b'%s: %s\n' % (tgt, stringutil.forcebytestr(inst)))
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729
Martin von Zweigbergk
relink: use progress helper...
r38423 progress.complete()
Augie Fackler
relink: properly use the progress API
r10424
Augie Fackler
formatting: blacken the codebase...
r43346 ui.status(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 _(b'relinked %d files (%s reclaimed)\n')
Augie Fackler
formatting: blacken the codebase...
r43346 % (relinked, util.bytecount(savedbytes))
)