hgext/relink.py
author Mads Kiilerich <madski@unity3d.com>
Mon, 12 Oct 2015 19:22:34 +0200
changeset 26627 832c98d79587
parent 26587 56b2bcea2529
child 26778 a95c975f42e3
permissions -rw-r--r--
largefiles: better handling of merge of largefiles that not are available Before, when merging revisions with missing largefiles, the missing largefiles would be fetched as a part of the merge. If that failed (for example because the main repository temporarily was unavailable), the largefile would be left missing. However, the next commit would abort and (seemed to) fail when markcommitted tried to mark the standin file as normal and thus had to hash the largefile that didn't exist. (Actually, the commit would succeed but the largefile update that follows right after the commit transaction would abort - quite confusing.) To fix that, make sure that synclfdirstate only marks files as normal if they actually exist.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
     1
# Mercurial extension to provide 'hg relink' command
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     2
#
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     3
# Copyright (C) 2007 Brendan Cully <brendan@kublai.com>
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     4
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 4270
diff changeset
     5
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 10225
diff changeset
     6
# GNU General Public License version 2 or any later version.
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     7
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
     8
"""recreates hardlinks between repository clones"""
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     9
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25660
diff changeset
    10
from mercurial import cmdutil, hg, util, error
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    11
from mercurial.i18n import _
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    12
import os, stat
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    13
21252
da0eb4970913 relink: declare command using decorator
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20083
diff changeset
    14
cmdtable = {}
da0eb4970913 relink: declare command using decorator
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20083
diff changeset
    15
command = cmdutil.command(cmdtable)
25186
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 21252
diff changeset
    16
# Note for extension authors: ONLY specify testedwith = 'internal' for
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 21252
diff changeset
    17
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 21252
diff changeset
    18
# be specifying the version(s) of Mercurial they are tested with, or
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 21252
diff changeset
    19
# leave the attribute unspecified.
16743
38caf405d010 hgext: mark all first-party extensions as such
Augie Fackler <raf@durin42.com>
parents: 16686
diff changeset
    20
testedwith = 'internal'
38caf405d010 hgext: mark all first-party extensions as such
Augie Fackler <raf@durin42.com>
parents: 16686
diff changeset
    21
21252
da0eb4970913 relink: declare command using decorator
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20083
diff changeset
    22
@command('relink', [], _('[ORIGIN]'))
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    23
def relink(ui, repo, origin=None, **opts):
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    24
    """recreate hardlinks between two repositories
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    25
9886
56af3f240a22 relink: wrap long lines in docstring
Martin Geisler <mg@lazybytes.net>
parents: 9790
diff changeset
    26
    When repositories are cloned locally, their data files will be
56af3f240a22 relink: wrap long lines in docstring
Martin Geisler <mg@lazybytes.net>
parents: 9790
diff changeset
    27
    hardlinked so that they only use the space of a single repository.
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    28
9886
56af3f240a22 relink: wrap long lines in docstring
Martin Geisler <mg@lazybytes.net>
parents: 9790
diff changeset
    29
    Unfortunately, subsequent pulls into either repository will break
56af3f240a22 relink: wrap long lines in docstring
Martin Geisler <mg@lazybytes.net>
parents: 9790
diff changeset
    30
    hardlinks for any files touched by the new changesets, even if
56af3f240a22 relink: wrap long lines in docstring
Martin Geisler <mg@lazybytes.net>
parents: 9790
diff changeset
    31
    both repositories end up pulling the same changes.
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    32
9886
56af3f240a22 relink: wrap long lines in docstring
Martin Geisler <mg@lazybytes.net>
parents: 9790
diff changeset
    33
    Similarly, passing --rev to "hg clone" will fail to use any
56af3f240a22 relink: wrap long lines in docstring
Martin Geisler <mg@lazybytes.net>
parents: 9790
diff changeset
    34
    hardlinks, falling back to a complete copy of the source
56af3f240a22 relink: wrap long lines in docstring
Martin Geisler <mg@lazybytes.net>
parents: 9790
diff changeset
    35
    repository.
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    36
9886
56af3f240a22 relink: wrap long lines in docstring
Martin Geisler <mg@lazybytes.net>
parents: 9790
diff changeset
    37
    This command lets you recreate those hardlinks and reclaim that
56af3f240a22 relink: wrap long lines in docstring
Martin Geisler <mg@lazybytes.net>
parents: 9790
diff changeset
    38
    wasted space.
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    39
9886
56af3f240a22 relink: wrap long lines in docstring
Martin Geisler <mg@lazybytes.net>
parents: 9790
diff changeset
    40
    This repository will be relinked to share space with ORIGIN, which
56af3f240a22 relink: wrap long lines in docstring
Martin Geisler <mg@lazybytes.net>
parents: 9790
diff changeset
    41
    must be on the same local disk. If ORIGIN is omitted, looks for
56af3f240a22 relink: wrap long lines in docstring
Martin Geisler <mg@lazybytes.net>
parents: 9790
diff changeset
    42
    "default-relink", then "default", in [paths].
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    43
9886
56af3f240a22 relink: wrap long lines in docstring
Martin Geisler <mg@lazybytes.net>
parents: 9790
diff changeset
    44
    Do not attempt any read operations on this repository while the
56af3f240a22 relink: wrap long lines in docstring
Martin Geisler <mg@lazybytes.net>
parents: 9790
diff changeset
    45
    command is running. (Both repositories will be locked against
56af3f240a22 relink: wrap long lines in docstring
Martin Geisler <mg@lazybytes.net>
parents: 9790
diff changeset
    46
    writes.)
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    47
    """
14945
11aad09a6370 hgext: replace uses of hasattr with util.safehasattr
Augie Fackler <durin42@gmail.com>
parents: 14709
diff changeset
    48
    if (not util.safehasattr(util, 'samefile') or
11aad09a6370 hgext: replace uses of hasattr with util.safehasattr
Augie Fackler <durin42@gmail.com>
parents: 14709
diff changeset
    49
        not util.safehasattr(util, 'samedevice')):
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25660
diff changeset
    50
        raise error.Abort(_('hardlinks are not supported on this system'))
18825
f0564402d059 repo: repo isolation, do not pass on repo.ui for creating new repos
Simon Heimberg <simohe@besonet.ch>
parents: 17191
diff changeset
    51
    src = hg.repository(repo.baseui, ui.expandpath(origin or 'default-relink',
14556
517e1d88bf7e hg: change various repository() users to use peer() where appropriate
Matt Mackall <mpm@selenic.com>
parents: 14235
diff changeset
    52
                                          origin or 'default'))
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    53
    ui.status(_('relinking %s to %s\n') % (src.store.path, repo.store.path))
13657
b69102740e57 relink: avoid trying to lock the same repo twice
Martin Geisler <mg@lazybytes.net>
parents: 12744
diff changeset
    54
    if repo.root == src.root:
b69102740e57 relink: avoid trying to lock the same repo twice
Martin Geisler <mg@lazybytes.net>
parents: 12744
diff changeset
    55
        ui.status(_('there is nothing to relink\n'))
b69102740e57 relink: avoid trying to lock the same repo twice
Martin Geisler <mg@lazybytes.net>
parents: 12744
diff changeset
    56
        return
b69102740e57 relink: avoid trying to lock the same repo twice
Martin Geisler <mg@lazybytes.net>
parents: 12744
diff changeset
    57
20083
c69e5911888d relink: abort earlier when on different devices (issue3916)
Simon Heimberg <simohe@besonet.ch>
parents: 18825
diff changeset
    58
    if not util.samedevice(src.store.path, repo.store.path):
c69e5911888d relink: abort earlier when on different devices (issue3916)
Simon Heimberg <simohe@besonet.ch>
parents: 18825
diff changeset
    59
        # No point in continuing
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25660
diff changeset
    60
        raise error.Abort(_('source and destination are on different devices'))
20083
c69e5911888d relink: abort earlier when on different devices (issue3916)
Simon Heimberg <simohe@besonet.ch>
parents: 18825
diff changeset
    61
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    62
    locallock = repo.lock()
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    63
    try:
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    64
        remotelock = src.lock()
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    65
        try:
11355
9011036ba79d relink/progress: Adding progress for collecting stage
timeless <timeless@gmail.com>
parents: 11354
diff changeset
    66
            candidates = sorted(collect(src, ui))
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9790
diff changeset
    67
            targets = prune(candidates, src.store.path, repo.store.path, ui)
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    68
            do_relink(src.store.path, repo.store.path, targets, ui)
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    69
        finally:
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    70
            remotelock.release()
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    71
    finally:
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    72
        locallock.release()
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    73
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    74
def collect(src, ui):
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    75
    seplen = len(os.path.sep)
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    76
    candidates = []
11355
9011036ba79d relink/progress: Adding progress for collecting stage
timeless <timeless@gmail.com>
parents: 11354
diff changeset
    77
    live = len(src['tip'].manifest())
9011036ba79d relink/progress: Adding progress for collecting stage
timeless <timeless@gmail.com>
parents: 11354
diff changeset
    78
    # Your average repository has some files which were deleted before
9011036ba79d relink/progress: Adding progress for collecting stage
timeless <timeless@gmail.com>
parents: 11354
diff changeset
    79
    # the tip revision. We account for that by assuming that there are
9011036ba79d relink/progress: Adding progress for collecting stage
timeless <timeless@gmail.com>
parents: 11354
diff changeset
    80
    # 3 tracked files for every 2 live files as of the tip version of
9011036ba79d relink/progress: Adding progress for collecting stage
timeless <timeless@gmail.com>
parents: 11354
diff changeset
    81
    # the repository.
9011036ba79d relink/progress: Adding progress for collecting stage
timeless <timeless@gmail.com>
parents: 11354
diff changeset
    82
    #
9011036ba79d relink/progress: Adding progress for collecting stage
timeless <timeless@gmail.com>
parents: 11354
diff changeset
    83
    # mozilla-central as of 2010-06-10 had a ratio of just over 7:5.
9011036ba79d relink/progress: Adding progress for collecting stage
timeless <timeless@gmail.com>
parents: 11354
diff changeset
    84
    total = live * 3 // 2
9011036ba79d relink/progress: Adding progress for collecting stage
timeless <timeless@gmail.com>
parents: 11354
diff changeset
    85
    src = src.store.path
9011036ba79d relink/progress: Adding progress for collecting stage
timeless <timeless@gmail.com>
parents: 11354
diff changeset
    86
    pos = 0
9011036ba79d relink/progress: Adding progress for collecting stage
timeless <timeless@gmail.com>
parents: 11354
diff changeset
    87
    ui.status(_("tip has %d files, estimated total number of files: %s\n")
9011036ba79d relink/progress: Adding progress for collecting stage
timeless <timeless@gmail.com>
parents: 11354
diff changeset
    88
              % (live, total))
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    89
    for dirpath, dirnames, filenames in os.walk(src):
11357
7914628b4751 relink: ensure deterministic directory walk in collect
Martin Geisler <mg@aragost.com>
parents: 11355
diff changeset
    90
        dirnames.sort()
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    91
        relpath = dirpath[len(src) + seplen:]
11357
7914628b4751 relink: ensure deterministic directory walk in collect
Martin Geisler <mg@aragost.com>
parents: 11355
diff changeset
    92
        for filename in sorted(filenames):
16686
67964cda8701 cleanup: "not x in y" -> "x not in y"
Brodie Rao <brodie@sf.io>
parents: 14945
diff changeset
    93
            if filename[-2:] not in ('.d', '.i'):
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    94
                continue
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    95
            st = os.stat(os.path.join(dirpath, filename))
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    96
            if not stat.S_ISREG(st.st_mode):
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    97
                continue
11355
9011036ba79d relink/progress: Adding progress for collecting stage
timeless <timeless@gmail.com>
parents: 11354
diff changeset
    98
            pos += 1
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    99
            candidates.append((os.path.join(relpath, filename), st))
11355
9011036ba79d relink/progress: Adding progress for collecting stage
timeless <timeless@gmail.com>
parents: 11354
diff changeset
   100
            ui.progress(_('collecting'), pos, filename, _('files'), total)
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   101
11355
9011036ba79d relink/progress: Adding progress for collecting stage
timeless <timeless@gmail.com>
parents: 11354
diff changeset
   102
    ui.progress(_('collecting'), None)
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
   103
    ui.status(_('collected %d candidate storage files\n') % len(candidates))
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   104
    return candidates
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   105
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9790
diff changeset
   106
def prune(candidates, src, dst, ui):
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9790
diff changeset
   107
    def linkfilter(src, dst, st):
4270
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   108
        try:
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   109
            ts = os.stat(dst)
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   110
        except OSError:
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   111
            # Destination doesn't have this file?
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   112
            return False
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9790
diff changeset
   113
        if util.samefile(src, dst):
4270
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   114
            return False
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9790
diff changeset
   115
        if not util.samedevice(src, dst):
4270
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   116
            # No point in continuing
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25660
diff changeset
   117
            raise error.Abort(
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
   118
                _('source and destination are on different devices'))
4270
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   119
        if st.st_size != ts.st_size:
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   120
            return False
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   121
        return st
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   122
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   123
    targets = []
11354
412a6e749f8d relink/progress: Adding progress for pruning stage
timeless <timeless@gmail.com>
parents: 11273
diff changeset
   124
    total = len(candidates)
412a6e749f8d relink/progress: Adding progress for pruning stage
timeless <timeless@gmail.com>
parents: 11273
diff changeset
   125
    pos = 0
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   126
    for fn, st in candidates:
11354
412a6e749f8d relink/progress: Adding progress for pruning stage
timeless <timeless@gmail.com>
parents: 11273
diff changeset
   127
        pos += 1
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9790
diff changeset
   128
        srcpath = os.path.join(src, fn)
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   129
        tgt = os.path.join(dst, fn)
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9790
diff changeset
   130
        ts = linkfilter(srcpath, tgt, st)
4270
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   131
        if not ts:
14709
6c7283faa967 check-code: don't mark debug messages for translation
Matt Mackall <mpm@selenic.com>
parents: 14556
diff changeset
   132
            ui.debug('not linkable: %s\n' % fn)
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   133
            continue
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   134
        targets.append((fn, ts.st_size))
12744
0793d763e413 progress: dropping superfluous space from units
timeless <timeless@gmail.com>
parents: 12062
diff changeset
   135
        ui.progress(_('pruning'), pos, fn, _('files'), total)
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   136
11354
412a6e749f8d relink/progress: Adding progress for pruning stage
timeless <timeless@gmail.com>
parents: 11273
diff changeset
   137
    ui.progress(_('pruning'), None)
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
   138
    ui.status(_('pruned down to %d probably relinkable files\n') % len(targets))
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   139
    return targets
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   140
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
   141
def do_relink(src, dst, files, ui):
4270
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   142
    def relinkfile(src, dst):
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   143
        bak = dst + '.bak'
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   144
        os.rename(dst, bak)
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   145
        try:
14235
b9e1b041744f rename util.os_link to oslink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13898
diff changeset
   146
            util.oslink(src, dst)
4270
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   147
        except OSError:
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   148
            os.rename(bak, dst)
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   149
            raise
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   150
        os.remove(bak)
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   151
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   152
    CHUNKLEN = 65536
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   153
    relinked = 0
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   154
    savedbytes = 0
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   155
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
   156
    pos = 0
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
   157
    total = len(files)
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   158
    for f, sz in files:
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
   159
        pos += 1
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   160
        source = os.path.join(src, f)
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   161
        tgt = os.path.join(dst, f)
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9790
diff changeset
   162
        # Binary mode, so that read() works correctly, especially on Windows
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9790
diff changeset
   163
        sfp = file(source, 'rb')
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9790
diff changeset
   164
        dfp = file(tgt, 'rb')
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   165
        sin = sfp.read(CHUNKLEN)
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   166
        while sin:
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   167
            din = dfp.read(CHUNKLEN)
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   168
            if sin != din:
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   169
                break
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   170
            sin = sfp.read(CHUNKLEN)
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9790
diff changeset
   171
        sfp.close()
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9790
diff changeset
   172
        dfp.close()
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   173
        if sin:
14709
6c7283faa967 check-code: don't mark debug messages for translation
Matt Mackall <mpm@selenic.com>
parents: 14556
diff changeset
   174
            ui.debug('not linkable: %s\n' % f)
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   175
            continue
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   176
        try:
4270
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   177
            relinkfile(source, tgt)
12744
0793d763e413 progress: dropping superfluous space from units
timeless <timeless@gmail.com>
parents: 12062
diff changeset
   178
            ui.progress(_('relinking'), pos, f, _('files'), total)
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   179
            relinked += 1
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   180
            savedbytes += sz
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25186
diff changeset
   181
        except OSError as inst:
9790
819e6c7085fc relink: do not translate format string with no text
Martin Geisler <mg@lazybytes.net>
parents: 9729
diff changeset
   182
            ui.warn('%s: %s\n' % (tgt, str(inst)))
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   183
10724
cb0a7faa29ea progress: drop extra args for pos=None calls (issue2087)
Matt Mackall <mpm@selenic.com>
parents: 10698
diff changeset
   184
    ui.progress(_('relinking'), None)
10424
93b5abcf5101 relink: properly use the progress API
Augie Fackler <durin42@gmail.com>
parents: 10264
diff changeset
   185
13656
0e200e1801f4 relink: format reclaimed byte count nicely
Martin Geisler <mg@lazybytes.net>
parents: 12744
diff changeset
   186
    ui.status(_('relinked %d files (%s reclaimed)\n') %
0e200e1801f4 relink: format reclaimed byte count nicely
Martin Geisler <mg@lazybytes.net>
parents: 12744
diff changeset
   187
              (relinked, util.bytecount(savedbytes)))