hgext/narrow/narrowdirstate.py
author Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
Mon, 27 May 2019 16:55:46 -0400
changeset 42456 87a34c767384
parent 40087 1d09ba0d2ed3
child 43076 2372284d9457
permissions -rw-r--r--
merge: fix race that could cause wrong size in dirstate The problem is that hg merge/update/etc work the following way: 1. figure out what files to update 2. apply the update to disk 3. apply the update to in-memory dirstate 4. write dirstate where step3 looks at the filesystem and assumes it sees the result of step2. If a file is changed between step2 and step3, step3 will record incorrect information in the dirstate. I avoid this by passing the size step3 needs directly from step2, for the common path (not implemented for change/delete conflicts for instance). I didn't fix the same race for the exec bit for now, because it's less likely to be problematic and I had trouble due to the fact that the dirstate stores the permissions differently from the manifest (st_mode vs '' 'l' 'x'), in combination with tests that pretend that symlinks are not supported. However, I moved the lstat from step3 to step2, which should tighten the race window markedly, both for the exec bit and for the mtime. Differential Revision: https://phab.mercurial-scm.org/D6475
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36079
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     1
# narrowdirstate.py - extensions to mercurial dirstate to support narrow clones
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     2
#
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     3
# Copyright 2017 Google, Inc.
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     4
#
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     7
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     8
from __future__ import absolute_import
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     9
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    10
from mercurial.i18n import _
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    11
from mercurial import (
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    12
    error,
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    13
)
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    14
38128
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    15
def wrapdirstate(repo, dirstate):
36079
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    16
    """Add narrow spec dirstate ignore, block changes outside narrow spec."""
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    17
38128
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    18
    def _editfunc(fn):
42456
87a34c767384 merge: fix race that could cause wrong size in dirstate
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 40087
diff changeset
    19
        def _wrapper(self, *args, **kwargs):
36079
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    20
            narrowmatch = repo.narrowmatch()
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    21
            for f in args:
39960
1a7d901a0a0c narrow: avoid looking up dirstate again when editing dirstate
Martin von Zweigbergk <martinvonz@google.com>
parents: 38869
diff changeset
    22
                if f is not None and not narrowmatch(f) and f not in self:
36079
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    23
                    raise error.Abort(_("cannot track '%s' - it is outside " +
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    24
                        "the narrow clone") % f)
42456
87a34c767384 merge: fix race that could cause wrong size in dirstate
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 40087
diff changeset
    25
            return fn(self, *args, **kwargs)
38128
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    26
        return _wrapper
36079
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    27
38128
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    28
    class narrowdirstate(dirstate.__class__):
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    29
        # Prevent adding/editing/copying/deleting files that are outside the
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    30
        # sparse checkout
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    31
        @_editfunc
42456
87a34c767384 merge: fix race that could cause wrong size in dirstate
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 40087
diff changeset
    32
        def normal(self, *args, **kwargs):
87a34c767384 merge: fix race that could cause wrong size in dirstate
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 40087
diff changeset
    33
            return super(narrowdirstate, self).normal(*args, **kwargs)
36079
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    34
38128
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    35
        @_editfunc
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    36
        def add(self, *args):
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    37
            return super(narrowdirstate, self).add(*args)
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    38
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    39
        @_editfunc
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    40
        def normallookup(self, *args):
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    41
            return super(narrowdirstate, self).normallookup(*args)
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    42
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    43
        @_editfunc
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    44
        def copy(self, *args):
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    45
            return super(narrowdirstate, self).copy(*args)
36079
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    46
38128
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    47
        @_editfunc
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    48
        def remove(self, *args):
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    49
            return super(narrowdirstate, self).remove(*args)
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    50
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    51
        @_editfunc
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    52
        def merge(self, *args):
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    53
            return super(narrowdirstate, self).merge(*args)
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    54
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    55
        def rebuild(self, parent, allfiles, changedfiles=None):
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    56
            if changedfiles is None:
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    57
                # Rebuilding entire dirstate, let's filter allfiles to match the
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    58
                # narrowspec.
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    59
                allfiles = [f for f in allfiles if repo.narrowmatch()(f)]
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    60
            super(narrowdirstate, self).rebuild(parent, allfiles, changedfiles)
36079
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    61
38128
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    62
    dirstate.__class__ = narrowdirstate
1cba497491be narrow: only wrap dirstate functions once, instead of per-reposetup
Kyle Lippincott <spectral@google.com>
parents: 36200
diff changeset
    63
    return dirstate