##// END OF EJS Templates
cmdutil: make in-memory changes visible to external editor (issue4378)...
cmdutil: make in-memory changes visible to external editor (issue4378) Before this patch, external editor process for the commit log can't view some in-memory changes (especially, of dirstate), because they aren't written out until the end of transaction (or wlock). This causes unexpected output of Mercurial commands spawned from that editor process. To make in-memory changes visible to external editor process, this patch does: - write (or schedule to write) in-memory dirstate changes, and - set HG_PENDING environment variable, if: - a transaction is running, and - there are in-memory changes to be visible "hg diff" spawned from external editor process for "hg qrefresh" shows: - "changes newly imported into the topmost" before 49148d7868df(*) - "all changes recorded in the topmost by refreshing" after this patch (*) 49148d7868df changed steps invoking editor process Even though backward compatibility may be broken, the latter behavior looks reasonable, because "hg diff" spawned from the editor process consistently shows "what changes new revision records" regardless of invocation context. In fact, issue4378 itself should be resolved by 800e090e9c64, which made 'repo.transaction()' write in-memory dirstate changes out explicitly before starting transaction. It also made "hg qrefresh" imply 'dirstate.write()' before external editor invocation in call chain below. - mq.queue.refresh - strip.strip - repair.strip - localrepository.transaction - dirstate.write - localrepository.commit - invoke external editor Though, this patch has '(issue4378)' in own summary line to indicate that issues like issue4378 should be fixed by this. BTW, this patch adds '-m' option to a 'hg ci --amend' execution in 'test-commit-amend.t', to avoid invoking external editor process. In this case, "unsure" states may be changed to "clean" according to timestamp or so on. These changes should be written into pending file, if external editor invocation is required, Then, writing dirstate changes out breaks stability of test, because it shows "transaction abort!/rollback completed" occasionally. Aborting after editor process invocation while commands below may cause similar instability of tests, too (AFAIK, there is no more such one, at this revision) - commit --amend - without --message/--logfile - import - without --message/--logfile, - without --no-commit, - without --bypass, - one of below, and - patch has no description text, or - with --edit - aborting at the 1st patch, which adds or removes file(s) - if it only changes existing files, status is checked only for changed files by 'scmutil.matchfiles()', and transition from "unsure" to "normal" in dirstate doesn't occur (= dirstate isn't changed, and written out) - aborting at the 2nd or later patch implies other pending changes (e.g. changelog), and always causes showing "transaction abort!/rollback completed"

File last commit:

r26587:56b2bcea default
r26750:9f9ec4ab default
Show More
relink.py
187 lines | 6.3 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"""
Pierre-Yves David
error: get Abort from 'error' instead of 'util'...
r26587 from mercurial import cmdutil, hg, util, error
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 from mercurial.i18n import _
import os, stat
Gregory Szorc
relink: declare command using decorator
r21252 cmdtable = {}
command = cmdutil.command(cmdtable)
Augie Fackler
extensions: document that `testedwith = 'internal'` is special...
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.
Augie Fackler
hgext: mark all first-party extensions as such
r16743 testedwith = 'internal'
Gregory Szorc
relink: declare command using decorator
r21252 @command('relink', [], _('[ORIGIN]'))
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
hgext: replace uses of hasattr with util.safehasattr
r14945 if (not util.safehasattr(util, 'samefile') or
not util.safehasattr(util, 'samedevice')):
Pierre-Yves David
error: get Abort from 'error' instead of 'util'...
r26587 raise error.Abort(_('hardlinks are not supported on this system'))
Simon Heimberg
repo: repo isolation, do not pass on repo.ui for creating new repos...
r18825 src = hg.repository(repo.baseui, ui.expandpath(origin or 'default-relink',
Matt Mackall
hg: change various repository() users to use peer() where appropriate...
r14556 origin or 'default'))
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
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
Pierre-Yves David
error: get Abort from 'error' instead of 'util'...
r26587 raise error.Abort(_('source and destination are on different devices'))
Simon Heimberg
relink: abort earlier when on different devices (issue3916)...
r20083
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):
Brodie Rao
cleanup: "not x in y" -> "x not in y"
r16686 if filename[-2:] not in ('.d', '.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))
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
Pierre-Yves David
error: get Abort from 'error' instead of 'util'...
r26587 raise error.Abort(
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 _('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:
Matt Mackall
check-code: don't mark debug messages for translation
r14709 ui.debug('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))
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:
Matt Mackall
check-code: don't mark debug messages for translation
r14709 ui.debug('not linkable: %s\n' % f)
Jesse Glick
Issue919: add a standard extension to recreate hardlinks between repositories....
r9729 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
Gregory Szorc
global: mass rewrite to use modern exception syntax...
r25660 except OSError as 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)))