hgext/relink.py
author Matt Harbison <matt_harbison@yahoo.com>
Thu, 24 Oct 2024 22:47:31 -0400
changeset 52127 fd200f5bcaea
parent 51863 f4733654f144
permissions -rw-r--r--
wireprototypes: make `baseprotocolhandler` methods abstract The documentation says it's an abstract base class, so let's enforce it. The `typing.Protocol` class is already an ABC, but it only prevents instantiation if there are abstract attrs that are missing. For example, from `hg debugshell`: >>> from mercurial import wireprototypes >>> x = wireprototypes.baseprotocolhandler() Traceback (most recent call last): File "<console>", line 1, in <module> TypeError: Can't instantiate abstract class baseprotocolhandler with abstract method name >>> class fake(wireprototypes.baseprotocolhandler): ... pass ... >>> x = fake() Traceback (most recent call last): File "<console>", line 1, in <module> TypeError: Can't instantiate abstract class fake with abstract method name That's great, but it doesn't protect against calling non-abstract methods at runtime, rather it depends on the protocol type hint being added to method signatures or class attrs, and then running a type checker to notice when an instance is assigned that doesn't conform to the protocol. We don't widely use type hints yet, and do have a lot of class hierarchy in the repository area, which could lead to surprises like this: >>> class fake(wireprototypes.baseprotocolhandler): ... @property ... def name(self) -> bytes: ... return b'name' ... >>> z = fake() >>> z.client() >>> print(z.client()) None Oops. That was supposed to return `bytes`. So not only is a bad/unexpected value returned, but it's one that violates the type hints (since the base client() method will be annotated to return bytes). With this change, we get: >>> from mercurial import wireprototypes >>> class fake(wireprototypes.baseprotocolhandler): ... @property ... def name(self) -> bytes: ... return b'name' ... >>> x = fake() Traceback (most recent call last): File "<console>", line 1, in <module> TypeError: Can't instantiate abstract class fake with abstract methods addcapabilities, checkperm, client, getargs, getpayload, getprotocaps, mayberedirectstdio So this looks like a reasonable safety harness to me, and lets us catch problems by running the standard tests while the type hints are being added, and pytype is improved. We should probably do this for all Protocol class methods that don't supply a method implementation.
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
51863
f4733654f144 typing: add `from __future__ import annotations` to most files
Matt Harbison <matt_harbison@yahoo.com>
parents: 50928
diff changeset
    10
from __future__ import annotations
f4733654f144 typing: add `from __future__ import annotations` to most files
Matt Harbison <matt_harbison@yahoo.com>
parents: 50928
diff changeset
    11
28380
ad266834251b relink: use absolute_import
timeless <timeless@mozdev.org>
parents: 26778
diff changeset
    12
import os
ad266834251b relink: use absolute_import
timeless <timeless@mozdev.org>
parents: 26778
diff changeset
    13
import stat
ad266834251b relink: use absolute_import
timeless <timeless@mozdev.org>
parents: 26778
diff changeset
    14
29205
a0939666b836 py3: move up symbol imports to enforce import-checker rules
Yuya Nishihara <yuya@tcha.org>
parents: 28380
diff changeset
    15
from mercurial.i18n import _
43085
eef9a2d67051 py3: manually import pycompat.open into files that need it
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43077
diff changeset
    16
from mercurial.pycompat import open
28380
ad266834251b relink: use absolute_import
timeless <timeless@mozdev.org>
parents: 26778
diff changeset
    17
from mercurial import (
ad266834251b relink: use absolute_import
timeless <timeless@mozdev.org>
parents: 26778
diff changeset
    18
    error,
ad266834251b relink: use absolute_import
timeless <timeless@mozdev.org>
parents: 26778
diff changeset
    19
    hg,
32337
46ba2cdda476 registrar: move cmdutil.command to registrar module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 29841
diff changeset
    20
    registrar,
28380
ad266834251b relink: use absolute_import
timeless <timeless@mozdev.org>
parents: 26778
diff changeset
    21
    util,
ad266834251b relink: use absolute_import
timeless <timeless@mozdev.org>
parents: 26778
diff changeset
    22
)
46942
b5e7cdb93abc relink: use `get_unique_pull_path`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43085
diff changeset
    23
from mercurial.utils import (
b5e7cdb93abc relink: use `get_unique_pull_path`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43085
diff changeset
    24
    stringutil,
b5e7cdb93abc relink: use `get_unique_pull_path`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43085
diff changeset
    25
    urlutil,
b5e7cdb93abc relink: use `get_unique_pull_path`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43085
diff changeset
    26
)
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    27
21252
da0eb4970913 relink: declare command using decorator
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20083
diff changeset
    28
cmdtable = {}
32337
46ba2cdda476 registrar: move cmdutil.command to registrar module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 29841
diff changeset
    29
command = registrar.command(cmdtable)
29841
d5883fd055c6 extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents: 29205
diff changeset
    30
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
25186
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 21252
diff changeset
    31
# 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
    32
# 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
    33
# leave the attribute unspecified.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    34
testedwith = b'ships-with-hg-core'
16743
38caf405d010 hgext: mark all first-party extensions as such
Augie Fackler <raf@durin42.com>
parents: 16686
diff changeset
    35
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
    36
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    37
@command(
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    38
    b'relink', [], _(b'[ORIGIN]'), helpcategory=command.CATEGORY_MAINTENANCE
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    39
)
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    40
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
    41
    """recreate hardlinks between two repositories
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    42
9886
56af3f240a22 relink: wrap long lines in docstring
Martin Geisler <mg@lazybytes.net>
parents: 9790
diff changeset
    43
    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
    44
    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
    45
9886
56af3f240a22 relink: wrap long lines in docstring
Martin Geisler <mg@lazybytes.net>
parents: 9790
diff changeset
    46
    Unfortunately, subsequent pulls into either repository will break
56af3f240a22 relink: wrap long lines in docstring
Martin Geisler <mg@lazybytes.net>
parents: 9790
diff changeset
    47
    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
    48
    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
    49
9886
56af3f240a22 relink: wrap long lines in docstring
Martin Geisler <mg@lazybytes.net>
parents: 9790
diff changeset
    50
    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
    51
    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
    52
    repository.
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    53
9886
56af3f240a22 relink: wrap long lines in docstring
Martin Geisler <mg@lazybytes.net>
parents: 9790
diff changeset
    54
    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
    55
    wasted space.
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    56
9886
56af3f240a22 relink: wrap long lines in docstring
Martin Geisler <mg@lazybytes.net>
parents: 9790
diff changeset
    57
    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
    58
    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
    59
    "default-relink", then "default", in [paths].
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    60
9886
56af3f240a22 relink: wrap long lines in docstring
Martin Geisler <mg@lazybytes.net>
parents: 9790
diff changeset
    61
    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
    62
    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
    63
    writes.)
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    64
    """
50928
d718eddf01d9 safehasattr: drop usage in favor of hasattr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50899
diff changeset
    65
    if not hasattr(util, 'samefile') or not hasattr(util, 'samedevice'):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    66
        raise error.Abort(_(b'hardlinks are not supported on this system'))
46942
b5e7cdb93abc relink: use `get_unique_pull_path`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43085
diff changeset
    67
b5e7cdb93abc relink: use `get_unique_pull_path`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43085
diff changeset
    68
    if origin is None and b'default-relink' in ui.paths:
b5e7cdb93abc relink: use `get_unique_pull_path`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43085
diff changeset
    69
        origin = b'default-relink'
49730
c6ae90515660 path: use `get_unique_pull_path_obj` in `hg relink`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48875
diff changeset
    70
    path = urlutil.get_unique_pull_path_obj(b'relink', ui, origin)
c6ae90515660 path: use `get_unique_pull_path_obj` in `hg relink`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48875
diff changeset
    71
    src = hg.repository(repo.baseui, path.loc)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    72
    ui.status(_(b'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
    73
    if repo.root == src.root:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    74
        ui.status(_(b'there is nothing to relink\n'))
13657
b69102740e57 relink: avoid trying to lock the same repo twice
Martin Geisler <mg@lazybytes.net>
parents: 12744
diff changeset
    75
        return
b69102740e57 relink: avoid trying to lock the same repo twice
Martin Geisler <mg@lazybytes.net>
parents: 12744
diff changeset
    76
20083
c69e5911888d relink: abort earlier when on different devices (issue3916)
Simon Heimberg <simohe@besonet.ch>
parents: 18825
diff changeset
    77
    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
    78
        # No point in continuing
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    79
        raise error.Abort(_(b'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
    80
38442
36edfbac7281 relink: use context manager for lock management
Matt Harbison <matt_harbison@yahoo.com>
parents: 38404
diff changeset
    81
    with repo.lock(), src.lock():
36edfbac7281 relink: use context manager for lock management
Matt Harbison <matt_harbison@yahoo.com>
parents: 38404
diff changeset
    82
        candidates = sorted(collect(src, ui))
36edfbac7281 relink: use context manager for lock management
Matt Harbison <matt_harbison@yahoo.com>
parents: 38404
diff changeset
    83
        targets = prune(candidates, src.store.path, repo.store.path, ui)
36edfbac7281 relink: use context manager for lock management
Matt Harbison <matt_harbison@yahoo.com>
parents: 38404
diff changeset
    84
        do_relink(src.store.path, repo.store.path, targets, ui)
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    85
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
    86
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
    87
def collect(src, ui):
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    88
    seplen = len(os.path.sep)
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    89
    candidates = []
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    90
    live = len(src[b'tip'].manifest())
11355
9011036ba79d relink/progress: Adding progress for collecting stage
timeless <timeless@gmail.com>
parents: 11354
diff changeset
    91
    # 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
    92
    # 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
    93
    # 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
    94
    # the repository.
9011036ba79d relink/progress: Adding progress for collecting stage
timeless <timeless@gmail.com>
parents: 11354
diff changeset
    95
    #
9011036ba79d relink/progress: Adding progress for collecting stage
timeless <timeless@gmail.com>
parents: 11354
diff changeset
    96
    # 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
    97
    total = live * 3 // 2
9011036ba79d relink/progress: Adding progress for collecting stage
timeless <timeless@gmail.com>
parents: 11354
diff changeset
    98
    src = src.store.path
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    99
    progress = ui.makeprogress(_(b'collecting'), unit=_(b'files'), total=total)
11355
9011036ba79d relink/progress: Adding progress for collecting stage
timeless <timeless@gmail.com>
parents: 11354
diff changeset
   100
    pos = 0
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
   101
    ui.status(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   102
        _(b"tip has %d files, estimated total number of files: %d\n")
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
   103
        % (live, total)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
   104
    )
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   105
    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
   106
        dirnames.sort()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
   107
        relpath = dirpath[len(src) + seplen :]
11357
7914628b4751 relink: ensure deterministic directory walk in collect
Martin Geisler <mg@aragost.com>
parents: 11355
diff changeset
   108
        for filename in sorted(filenames):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   109
            if filename[-2:] not in (b'.d', b'.i'):
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   110
                continue
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   111
            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
   112
            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
   113
                continue
11355
9011036ba79d relink/progress: Adding progress for collecting stage
timeless <timeless@gmail.com>
parents: 11354
diff changeset
   114
            pos += 1
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   115
            candidates.append((os.path.join(relpath, filename), st))
38404
398716063c2d relink: use progress helper
Martin von Zweigbergk <martinvonz@google.com>
parents: 37084
diff changeset
   116
            progress.update(pos, item=filename)
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   117
38404
398716063c2d relink: use progress helper
Martin von Zweigbergk <martinvonz@google.com>
parents: 37084
diff changeset
   118
    progress.complete()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   119
    ui.status(_(b'collected %d candidate storage files\n') % len(candidates))
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   120
    return candidates
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   121
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
   122
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9790
diff changeset
   123
def prune(candidates, src, dst, ui):
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9790
diff changeset
   124
    def linkfilter(src, dst, st):
4270
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   125
        try:
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   126
            ts = os.stat(dst)
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   127
        except OSError:
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   128
            # Destination doesn't have this file?
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   129
            return False
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9790
diff changeset
   130
        if util.samefile(src, dst):
4270
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   131
            return False
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9790
diff changeset
   132
        if not util.samedevice(src, dst):
4270
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   133
            # 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
   134
            raise error.Abort(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   135
                _(b'source and destination are on different devices')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
   136
            )
4270
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   137
        if st.st_size != ts.st_size:
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   138
            return False
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   139
        return st
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   140
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   141
    targets = []
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
   142
    progress = ui.makeprogress(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   143
        _(b'pruning'), unit=_(b'files'), total=len(candidates)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
   144
    )
11354
412a6e749f8d relink/progress: Adding progress for pruning stage
timeless <timeless@gmail.com>
parents: 11273
diff changeset
   145
    pos = 0
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   146
    for fn, st in candidates:
11354
412a6e749f8d relink/progress: Adding progress for pruning stage
timeless <timeless@gmail.com>
parents: 11273
diff changeset
   147
        pos += 1
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9790
diff changeset
   148
        srcpath = os.path.join(src, fn)
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   149
        tgt = os.path.join(dst, fn)
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9790
diff changeset
   150
        ts = linkfilter(srcpath, tgt, st)
4270
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   151
        if not ts:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   152
            ui.debug(b'not linkable: %s\n' % fn)
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   153
            continue
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   154
        targets.append((fn, ts.st_size))
38404
398716063c2d relink: use progress helper
Martin von Zweigbergk <martinvonz@google.com>
parents: 37084
diff changeset
   155
        progress.update(pos, item=fn)
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   156
38404
398716063c2d relink: use progress helper
Martin von Zweigbergk <martinvonz@google.com>
parents: 37084
diff changeset
   157
    progress.complete()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   158
    ui.status(
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   159
        _(b'pruned down to %d probably relinkable files\n') % len(targets)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   160
    )
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   161
    return targets
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   162
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
   163
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
   164
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
   165
    def relinkfile(src, dst):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   166
        bak = dst + b'.bak'
4270
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   167
        os.rename(dst, bak)
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   168
        try:
14235
b9e1b041744f rename util.os_link to oslink
Adrian Buehlmann <adrian@cadifra.com>
parents: 13898
diff changeset
   169
            util.oslink(src, dst)
4270
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   170
        except OSError:
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   171
            os.rename(bak, dst)
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   172
            raise
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   173
        os.remove(bak)
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   174
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   175
    CHUNKLEN = 65536
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   176
    relinked = 0
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   177
    savedbytes = 0
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   178
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
   179
    progress = ui.makeprogress(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   180
        _(b'relinking'), unit=_(b'files'), total=len(files)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
   181
    )
9729
aa9ccab5af37 Issue919: add a standard extension to recreate hardlinks between repositories.
Jesse Glick <jesse.glick@sun.com>
parents: 8225
diff changeset
   182
    pos = 0
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   183
    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
   184
        pos += 1
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   185
        source = os.path.join(src, f)
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   186
        tgt = os.path.join(dst, f)
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9790
diff changeset
   187
        # Binary mode, so that read() works correctly, especially on Windows
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   188
        sfp = open(source, b'rb')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   189
        dfp = open(tgt, b'rb')
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   190
        sin = sfp.read(CHUNKLEN)
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   191
        while sin:
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   192
            din = dfp.read(CHUNKLEN)
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   193
            if sin != din:
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   194
                break
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   195
            sin = sfp.read(CHUNKLEN)
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9790
diff changeset
   196
        sfp.close()
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9790
diff changeset
   197
        dfp.close()
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   198
        if sin:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   199
            ui.debug(b'not linkable: %s\n' % f)
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   200
            continue
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   201
        try:
4270
29eb88bd5c8d hg-relink: do not compare .d files
Brendan Cully <brendan@kublai.com>
parents: 4249
diff changeset
   202
            relinkfile(source, tgt)
38404
398716063c2d relink: use progress helper
Martin von Zweigbergk <martinvonz@google.com>
parents: 37084
diff changeset
   203
            progress.update(pos, item=f)
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   204
            relinked += 1
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   205
            savedbytes += sz
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25186
diff changeset
   206
        except OSError as inst:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   207
            ui.warn(b'%s: %s\n' % (tgt, stringutil.forcebytestr(inst)))
4249
7663780b55a7 Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   208
38404
398716063c2d relink: use progress helper
Martin von Zweigbergk <martinvonz@google.com>
parents: 37084
diff changeset
   209
    progress.complete()
10424
93b5abcf5101 relink: properly use the progress API
Augie Fackler <durin42@gmail.com>
parents: 10264
diff changeset
   210
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
   211
    ui.status(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   212
        _(b'relinked %d files (%s reclaimed)\n')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
   213
        % (relinked, util.bytecount(savedbytes))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
   214
    )