hgext/win32text.py
author Joerg Sonnenberger <joerg@bec.de>
Sat, 20 Jul 2024 00:43:08 +0200
changeset 51914 3dbbb7d913a9
parent 51863 f4733654f144
permissions -rw-r--r--
revlogutils: remember known metadata parents for issue6528 In the cases where the parent revs tell us for sure that the parent has metadata, remember this fact to avoid content recomputations later.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6481
e837dded56c7 win32text: Add macencode/macdecode
OHASHI Hideya <ohachige@gmail.com>
parents: 6247
diff changeset
     1
# win32text.py - LF <-> CRLF/CR translation utilities for Windows/Mac users
5675
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
     2
#
46819
d4ba4d51f85f contributor: change mentions of mpm to olivia
Raphaël Gomès <rgomes@octobus.net>
parents: 45942
diff changeset
     3
#  Copyright 2005, 2007-2009 Olivia Mackall <olivia@selenic.com> and others
8253
e553fe565c61 win32text: add copyright header
Martin Geisler <mg@lazybytes.net>
parents: 8150
diff changeset
     4
#
e553fe565c61 win32text: add copyright header
Martin Geisler <mg@lazybytes.net>
parents: 8150
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: 10132
diff changeset
     6
# GNU General Public License version 2 or any later version.
8873
e872ef2e6758 help: add/fix docstrings for a bunch of extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8253
diff changeset
     7
20624
23146e4d9cee extensions: mark win32text deprecated
Matt Mackall <mpm@selenic.com>
parents: 18054
diff changeset
     8
'''perform automatic newline conversion (DEPRECATED)
8873
e872ef2e6758 help: add/fix docstrings for a bunch of extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8253
diff changeset
     9
11269
5f01fd602542 win32text: mark this extension as deprecated
Martin Geisler <mg@aragost.com>
parents: 10263
diff changeset
    10
  Deprecation: The win32text extension requires each user to configure
5f01fd602542 win32text: mark this extension as deprecated
Martin Geisler <mg@aragost.com>
parents: 10263
diff changeset
    11
  the extension again and again for each clone since the configuration
5f01fd602542 win32text: mark this extension as deprecated
Martin Geisler <mg@aragost.com>
parents: 10263
diff changeset
    12
  is not copied when cloning.
5f01fd602542 win32text: mark this extension as deprecated
Martin Geisler <mg@aragost.com>
parents: 10263
diff changeset
    13
5f01fd602542 win32text: mark this extension as deprecated
Martin Geisler <mg@aragost.com>
parents: 10263
diff changeset
    14
  We have therefore made the ``eol`` as an alternative. The ``eol``
5f01fd602542 win32text: mark this extension as deprecated
Martin Geisler <mg@aragost.com>
parents: 10263
diff changeset
    15
  uses a version controlled file for its configuration and each clone
5f01fd602542 win32text: mark this extension as deprecated
Martin Geisler <mg@aragost.com>
parents: 10263
diff changeset
    16
  will therefore use the right settings from the start.
5f01fd602542 win32text: mark this extension as deprecated
Martin Geisler <mg@aragost.com>
parents: 10263
diff changeset
    17
9217
9cdb6964c3a3 win32text: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 8936
diff changeset
    18
To perform automatic newline conversion, use::
8873
e872ef2e6758 help: add/fix docstrings for a bunch of extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8253
diff changeset
    19
9217
9cdb6964c3a3 win32text: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 8936
diff changeset
    20
  [extensions]
10112
703db37d186b hgext: enable extensions without "hgext." prefix in help texts
Martin Geisler <mg@lazybytes.net>
parents: 9217
diff changeset
    21
  win32text =
9217
9cdb6964c3a3 win32text: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 8936
diff changeset
    22
  [encode]
9cdb6964c3a3 win32text: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 8936
diff changeset
    23
  ** = cleverencode:
9cdb6964c3a3 win32text: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 8936
diff changeset
    24
  # or ** = macencode:
8873
e872ef2e6758 help: add/fix docstrings for a bunch of extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8253
diff changeset
    25
9217
9cdb6964c3a3 win32text: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 8936
diff changeset
    26
  [decode]
9cdb6964c3a3 win32text: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 8936
diff changeset
    27
  ** = cleverdecode:
9cdb6964c3a3 win32text: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 8936
diff changeset
    28
  # or ** = macdecode:
8873
e872ef2e6758 help: add/fix docstrings for a bunch of extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8253
diff changeset
    29
9217
9cdb6964c3a3 win32text: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 8936
diff changeset
    30
If not doing conversion, to make sure you do not commit CRLF/CR by accident::
8873
e872ef2e6758 help: add/fix docstrings for a bunch of extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8253
diff changeset
    31
9217
9cdb6964c3a3 win32text: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 8936
diff changeset
    32
  [hooks]
9cdb6964c3a3 win32text: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 8936
diff changeset
    33
  pretxncommit.crlf = python:hgext.win32text.forbidcrlf
9cdb6964c3a3 win32text: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 8936
diff changeset
    34
  # or pretxncommit.cr = python:hgext.win32text.forbidcr
8873
e872ef2e6758 help: add/fix docstrings for a bunch of extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8253
diff changeset
    35
e872ef2e6758 help: add/fix docstrings for a bunch of extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8253
diff changeset
    36
To do the same check on a server to prevent CRLF/CR from being
9217
9cdb6964c3a3 win32text: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 8936
diff changeset
    37
pushed or pulled::
8873
e872ef2e6758 help: add/fix docstrings for a bunch of extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8253
diff changeset
    38
9217
9cdb6964c3a3 win32text: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 8936
diff changeset
    39
  [hooks]
9cdb6964c3a3 win32text: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 8936
diff changeset
    40
  pretxnchangegroup.crlf = python:hgext.win32text.forbidcrlf
9cdb6964c3a3 win32text: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 8936
diff changeset
    41
  # or pretxnchangegroup.cr = python:hgext.win32text.forbidcr
8873
e872ef2e6758 help: add/fix docstrings for a bunch of extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8253
diff changeset
    42
'''
5675
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
    43
51863
f4733654f144 typing: add `from __future__ import annotations` to most files
Matt Harbison <matt_harbison@yahoo.com>
parents: 50014
diff changeset
    44
from __future__ import annotations
29485
6a98f9408a50 py3: make files use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26421
diff changeset
    45
6a98f9408a50 py3: make files use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26421
diff changeset
    46
import re
7225
59b4ae211584 i18n: import _ instead of gettext
Martin Geisler <mg@daimi.au.dk>
parents: 6750
diff changeset
    47
from mercurial.i18n import _
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
    48
from mercurial.node import short
29485
6a98f9408a50 py3: make files use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26421
diff changeset
    49
from mercurial import (
48388
f5dea753fe4e win32text: drop associated dirstate cache information on revert
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46819
diff changeset
    50
    cmdutil,
f5dea753fe4e win32text: drop associated dirstate cache information on revert
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46819
diff changeset
    51
    extensions,
34185
f84358f75978 configitems: register the 'win32text.warn' config
Boris Feld <boris.feld@octobus.net>
parents: 31337
diff changeset
    52
    registrar,
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 35675
diff changeset
    53
)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
    54
from mercurial.utils import stringutil
4859
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
    55
29841
d5883fd055c6 extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents: 29485
diff changeset
    56
# 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: 20624
diff changeset
    57
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 20624
diff changeset
    58
# 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: 20624
diff changeset
    59
# leave the attribute unspecified.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    60
testedwith = b'ships-with-hg-core'
16743
38caf405d010 hgext: mark all first-party extensions as such
Augie Fackler <raf@durin42.com>
parents: 12837
diff changeset
    61
34185
f84358f75978 configitems: register the 'win32text.warn' config
Boris Feld <boris.feld@octobus.net>
parents: 31337
diff changeset
    62
configtable = {}
f84358f75978 configitems: register the 'win32text.warn' config
Boris Feld <boris.feld@octobus.net>
parents: 31337
diff changeset
    63
configitem = registrar.configitem(configtable)
f84358f75978 configitems: register the 'win32text.warn' config
Boris Feld <boris.feld@octobus.net>
parents: 31337
diff changeset
    64
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
    65
configitem(
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43105
diff changeset
    66
    b'win32text',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43105
diff changeset
    67
    b'warn',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43105
diff changeset
    68
    default=True,
34185
f84358f75978 configitems: register the 'win32text.warn' config
Boris Feld <boris.feld@octobus.net>
parents: 31337
diff changeset
    69
)
f84358f75978 configitems: register the 'win32text.warn' config
Boris Feld <boris.feld@octobus.net>
parents: 31337
diff changeset
    70
4859
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
    71
# regexp for single LF without CR preceding.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    72
re_single_lf = re.compile(b'(^|[^\r])\n', re.MULTILINE)
4859
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
    73
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    74
newlinestr = {b'\r\n': b'CRLF', b'\r': b'CR'}
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    75
filterstr = {b'\r\n': b'clever', b'\r': b'mac'}
6481
e837dded56c7 win32text: Add macencode/macdecode
OHASHI Hideya <ohachige@gmail.com>
parents: 6247
diff changeset
    76
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
    77
6481
e837dded56c7 win32text: Add macencode/macdecode
OHASHI Hideya <ohachige@gmail.com>
parents: 6247
diff changeset
    78
def checknewline(s, newline, ui=None, repo=None, filename=None):
e837dded56c7 win32text: Add macencode/macdecode
OHASHI Hideya <ohachige@gmail.com>
parents: 6247
diff changeset
    79
    # warn if already has 'newline' in repository.
4859
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
    80
    # it might cause unexpected eol conversion.
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
    81
    # see issue 302:
26420
2fc86d92c4a9 urls: bulk-change BTS urls to new location
Matt Mackall <mpm@selenic.com>
parents: 25829
diff changeset
    82
    #   https://bz.mercurial-scm.org/302
6481
e837dded56c7 win32text: Add macencode/macdecode
OHASHI Hideya <ohachige@gmail.com>
parents: 6247
diff changeset
    83
    if newline in s and ui and filename and repo:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
    84
        ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
    85
            _(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    86
                b'WARNING: %s already has %s line endings\n'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    87
                b'and does not need EOL conversion by the win32text plugin.\n'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    88
                b'Before your next commit, please reconsider your '
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    89
                b'encode/decode settings in \nMercurial.ini or %s.\n'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
    90
            )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    91
            % (filename, newlinestr[newline], repo.vfs.join(b'hgrc'))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
    92
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
    93
6481
e837dded56c7 win32text: Add macencode/macdecode
OHASHI Hideya <ohachige@gmail.com>
parents: 6247
diff changeset
    94
e837dded56c7 win32text: Add macencode/macdecode
OHASHI Hideya <ohachige@gmail.com>
parents: 6247
diff changeset
    95
def dumbdecode(s, cmd, **kwargs):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    96
    checknewline(s, b'\r\n', **kwargs)
4859
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
    97
    # replace single LF to CRLF
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    98
    return re_single_lf.sub(b'\\1\r\n', s)
4859
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
    99
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   100
4859
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
   101
def dumbencode(s, cmd):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   102
    return s.replace(b'\r\n', b'\n')
4859
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
   103
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   104
6481
e837dded56c7 win32text: Add macencode/macdecode
OHASHI Hideya <ohachige@gmail.com>
parents: 6247
diff changeset
   105
def macdumbdecode(s, cmd, **kwargs):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   106
    checknewline(s, b'\r', **kwargs)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   107
    return s.replace(b'\n', b'\r')
6481
e837dded56c7 win32text: Add macencode/macdecode
OHASHI Hideya <ohachige@gmail.com>
parents: 6247
diff changeset
   108
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   109
6481
e837dded56c7 win32text: Add macencode/macdecode
OHASHI Hideya <ohachige@gmail.com>
parents: 6247
diff changeset
   110
def macdumbencode(s, cmd):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   111
    return s.replace(b'\r', b'\n')
6481
e837dded56c7 win32text: Add macencode/macdecode
OHASHI Hideya <ohachige@gmail.com>
parents: 6247
diff changeset
   112
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   113
5967
f8ad3b76e923 Provide better context for custom Python encode/decode filters.
Jesse Glick <jesse.glick@sun.com>
parents: 5966
diff changeset
   114
def cleverdecode(s, cmd, **kwargs):
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 35675
diff changeset
   115
    if not stringutil.binary(s):
6473
9c897ffd3637 Backed out changeset 7f4257b5cbfc
Patrick Mezard <pmezard@gmail.com>
parents: 6247
diff changeset
   116
        return dumbdecode(s, cmd, **kwargs)
9c897ffd3637 Backed out changeset 7f4257b5cbfc
Patrick Mezard <pmezard@gmail.com>
parents: 6247
diff changeset
   117
    return s
4859
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
   118
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   119
4859
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
   120
def cleverencode(s, cmd):
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 35675
diff changeset
   121
    if not stringutil.binary(s):
6473
9c897ffd3637 Backed out changeset 7f4257b5cbfc
Patrick Mezard <pmezard@gmail.com>
parents: 6247
diff changeset
   122
        return dumbencode(s, cmd)
9c897ffd3637 Backed out changeset 7f4257b5cbfc
Patrick Mezard <pmezard@gmail.com>
parents: 6247
diff changeset
   123
    return s
4859
8c5aca855b5d Correct inadvertent line ending change.
Lee Cantey <lcantey@gmail.com>
parents: 4858
diff changeset
   124
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   125
6481
e837dded56c7 win32text: Add macencode/macdecode
OHASHI Hideya <ohachige@gmail.com>
parents: 6247
diff changeset
   126
def macdecode(s, cmd, **kwargs):
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 35675
diff changeset
   127
    if not stringutil.binary(s):
6484
ab8038bf5127 Merge with macencode branch
Patrick Mezard <pmezard@gmail.com>
parents: 6473 6483
diff changeset
   128
        return macdumbdecode(s, cmd, **kwargs)
ab8038bf5127 Merge with macencode branch
Patrick Mezard <pmezard@gmail.com>
parents: 6473 6483
diff changeset
   129
    return s
6481
e837dded56c7 win32text: Add macencode/macdecode
OHASHI Hideya <ohachige@gmail.com>
parents: 6247
diff changeset
   130
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   131
6481
e837dded56c7 win32text: Add macencode/macdecode
OHASHI Hideya <ohachige@gmail.com>
parents: 6247
diff changeset
   132
def macencode(s, cmd):
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 35675
diff changeset
   133
    if not stringutil.binary(s):
6484
ab8038bf5127 Merge with macencode branch
Patrick Mezard <pmezard@gmail.com>
parents: 6473 6483
diff changeset
   134
        return macdumbencode(s, cmd)
ab8038bf5127 Merge with macencode branch
Patrick Mezard <pmezard@gmail.com>
parents: 6473 6483
diff changeset
   135
    return s
6481
e837dded56c7 win32text: Add macencode/macdecode
OHASHI Hideya <ohachige@gmail.com>
parents: 6247
diff changeset
   136
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   137
5966
11af38a592ae Register data filters in a localrepo instead of util
Patrick Mezard <pmezard@gmail.com>
parents: 5675
diff changeset
   138
_filters = {
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   139
    b'dumbdecode:': dumbdecode,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   140
    b'dumbencode:': dumbencode,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   141
    b'cleverdecode:': cleverdecode,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   142
    b'cleverencode:': cleverencode,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   143
    b'macdumbdecode:': macdumbdecode,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   144
    b'macdumbencode:': macdumbencode,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   145
    b'macdecode:': macdecode,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   146
    b'macencode:': macencode,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   147
}
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   148
5675
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
   149
6483
0a803195bb29 win32text: rename forbidcrlforcr() function
Patrick Mezard <pmezard@gmail.com>
parents: 6481
diff changeset
   150
def forbidnewline(ui, repo, hooktype, node, newline, **kwargs):
5675
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
   151
    halt = False
8150
bbc24c0753a0 util: use built-in set and frozenset
Martin Geisler <mg@lazybytes.net>
parents: 8147
diff changeset
   152
    seen = set()
8147
441dc7becd43 win32text: be more careful about rejecting violating changesets
Bryan O'Sullivan <bos@serpentine.com>
parents: 8002
diff changeset
   153
    # we try to walk changesets in reverse order from newest to
441dc7becd43 win32text: be more careful about rejecting violating changesets
Bryan O'Sullivan <bos@serpentine.com>
parents: 8002
diff changeset
   154
    # oldest, so that if we see a file multiple times, we take the
441dc7becd43 win32text: be more careful about rejecting violating changesets
Bryan O'Sullivan <bos@serpentine.com>
parents: 8002
diff changeset
   155
    # newest version as canonical. this prevents us from blocking a
441dc7becd43 win32text: be more careful about rejecting violating changesets
Bryan O'Sullivan <bos@serpentine.com>
parents: 8002
diff changeset
   156
    # changegroup that contains an unacceptable commit followed later
441dc7becd43 win32text: be more careful about rejecting violating changesets
Bryan O'Sullivan <bos@serpentine.com>
parents: 8002
diff changeset
   157
    # by a commit that fixes the problem.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   158
    tip = repo[b'tip']
49284
d44e3c45f0e4 py3: replace `pycompat.xrange` by `range`
Manuel Jacob <me@manueljacob.de>
parents: 48913
diff changeset
   159
    for rev in range(repo.changelog.tiprev(), repo[node].rev() - 1, -1):
6747
f6c00b17387c use repo[changeid] to get a changectx
Matt Mackall <mpm@selenic.com>
parents: 6510
diff changeset
   160
        c = repo[rev]
5675
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
   161
        for f in c.files():
8147
441dc7becd43 win32text: be more careful about rejecting violating changesets
Bryan O'Sullivan <bos@serpentine.com>
parents: 8002
diff changeset
   162
            if f in seen or f not in tip or f not in c:
5675
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
   163
                continue
8147
441dc7becd43 win32text: be more careful about rejecting violating changesets
Bryan O'Sullivan <bos@serpentine.com>
parents: 8002
diff changeset
   164
            seen.add(f)
5675
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
   165
            data = c[f].data()
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 35675
diff changeset
   166
            if not stringutil.binary(data) and newline in data:
5675
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
   167
                if not halt:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   168
                    ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   169
                        _(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   170
                            b'attempt to commit or push text file(s) '
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   171
                            b'using %s line endings\n'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   172
                        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   173
                        % newlinestr[newline]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   174
                    )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   175
                ui.warn(_(b'in %s: %s\n') % (short(c.node()), f))
5675
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
   176
                halt = True
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   177
    if halt and hooktype == b'pretxnchangegroup':
6481
e837dded56c7 win32text: Add macencode/macdecode
OHASHI Hideya <ohachige@gmail.com>
parents: 6247
diff changeset
   178
        crlf = newlinestr[newline].lower()
e837dded56c7 win32text: Add macencode/macdecode
OHASHI Hideya <ohachige@gmail.com>
parents: 6247
diff changeset
   179
        filter = filterstr[newline]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   180
        ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   181
            _(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   182
                b'\nTo prevent this mistake in your local repository,\n'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   183
                b'add to Mercurial.ini or .hg/hgrc:\n'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   184
                b'\n'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   185
                b'[hooks]\n'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   186
                b'pretxncommit.%s = python:hgext.win32text.forbid%s\n'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   187
                b'\n'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   188
                b'and also consider adding:\n'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   189
                b'\n'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   190
                b'[extensions]\n'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   191
                b'win32text =\n'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   192
                b'[encode]\n'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   193
                b'** = %sencode:\n'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   194
                b'[decode]\n'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   195
                b'** = %sdecode:\n'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   196
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   197
            % (crlf, crlf, filter, filter)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   198
        )
5675
a5fe27b83a4a Issue 882: add standard hook to reject text files with CRLF.
Jesse Glick <jesse.glick@sun.com>
parents: 4859
diff changeset
   199
    return halt
5966
11af38a592ae Register data filters in a localrepo instead of util
Patrick Mezard <pmezard@gmail.com>
parents: 5675
diff changeset
   200
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   201
6481
e837dded56c7 win32text: Add macencode/macdecode
OHASHI Hideya <ohachige@gmail.com>
parents: 6247
diff changeset
   202
def forbidcrlf(ui, repo, hooktype, node, **kwargs):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   203
    return forbidnewline(ui, repo, hooktype, node, b'\r\n', **kwargs)
6481
e837dded56c7 win32text: Add macencode/macdecode
OHASHI Hideya <ohachige@gmail.com>
parents: 6247
diff changeset
   204
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   205
6481
e837dded56c7 win32text: Add macencode/macdecode
OHASHI Hideya <ohachige@gmail.com>
parents: 6247
diff changeset
   206
def forbidcr(ui, repo, hooktype, node, **kwargs):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   207
    return forbidnewline(ui, repo, hooktype, node, b'\r', **kwargs)
6481
e837dded56c7 win32text: Add macencode/macdecode
OHASHI Hideya <ohachige@gmail.com>
parents: 6247
diff changeset
   208
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   209
5966
11af38a592ae Register data filters in a localrepo instead of util
Patrick Mezard <pmezard@gmail.com>
parents: 5675
diff changeset
   210
def reposetup(ui, repo):
11af38a592ae Register data filters in a localrepo instead of util
Patrick Mezard <pmezard@gmail.com>
parents: 5675
diff changeset
   211
    if not repo.local():
11af38a592ae Register data filters in a localrepo instead of util
Patrick Mezard <pmezard@gmail.com>
parents: 5675
diff changeset
   212
        return
48913
f254fc73d956 global: bulk replace simple pycompat.iteritems(x) with x.items()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
   213
    for name, fn in _filters.items():
5966
11af38a592ae Register data filters in a localrepo instead of util
Patrick Mezard <pmezard@gmail.com>
parents: 5675
diff changeset
   214
        repo.adddatafilter(name, fn)
11af38a592ae Register data filters in a localrepo instead of util
Patrick Mezard <pmezard@gmail.com>
parents: 5675
diff changeset
   215
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   216
48388
f5dea753fe4e win32text: drop associated dirstate cache information on revert
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46819
diff changeset
   217
def wrap_revert(orig, repo, ctx, names, uipathfn, actions, *args, **kwargs):
f5dea753fe4e win32text: drop associated dirstate cache information on revert
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46819
diff changeset
   218
    # reset dirstate cache for file we touch
f5dea753fe4e win32text: drop associated dirstate cache information on revert
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46819
diff changeset
   219
    ds = repo.dirstate
50013
05a2f65c9e86 win32text: drop the `changing_parents` context in revert upgrade
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50012
diff changeset
   220
    for filename in actions[b'revert'][0]:
05a2f65c9e86 win32text: drop the `changing_parents` context in revert upgrade
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50012
diff changeset
   221
        entry = ds.get_entry(filename)
05a2f65c9e86 win32text: drop the `changing_parents` context in revert upgrade
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50012
diff changeset
   222
        if entry is not None:
05a2f65c9e86 win32text: drop the `changing_parents` context in revert upgrade
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50012
diff changeset
   223
            if entry.p1_tracked:
05a2f65c9e86 win32text: drop the `changing_parents` context in revert upgrade
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50012
diff changeset
   224
                # If we revert the file, it is possibly dirty. However,
05a2f65c9e86 win32text: drop the `changing_parents` context in revert upgrade
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50012
diff changeset
   225
                # this extension meddle with the file content and therefore
05a2f65c9e86 win32text: drop the `changing_parents` context in revert upgrade
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50012
diff changeset
   226
                # its size. As a result, we cannot simply call
05a2f65c9e86 win32text: drop the `changing_parents` context in revert upgrade
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50012
diff changeset
   227
                # `dirstate.set_possibly_dirty` as it will not affet the
05a2f65c9e86 win32text: drop the `changing_parents` context in revert upgrade
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50012
diff changeset
   228
                # expected size of the file.
05a2f65c9e86 win32text: drop the `changing_parents` context in revert upgrade
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50012
diff changeset
   229
                #
05a2f65c9e86 win32text: drop the `changing_parents` context in revert upgrade
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50012
diff changeset
   230
                # At least, now, the quirk is properly documented.
05a2f65c9e86 win32text: drop the `changing_parents` context in revert upgrade
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50012
diff changeset
   231
                ds.hacky_extension_update_file(
05a2f65c9e86 win32text: drop the `changing_parents` context in revert upgrade
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50012
diff changeset
   232
                    filename,
05a2f65c9e86 win32text: drop the `changing_parents` context in revert upgrade
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50012
diff changeset
   233
                    entry.tracked,
50014
b7ddd9ae4bef win32text: make the hacky call cover more cases
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50013
diff changeset
   234
                    p1_tracked=entry.p1_tracked,
50013
05a2f65c9e86 win32text: drop the `changing_parents` context in revert upgrade
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50012
diff changeset
   235
                    p2_info=entry.p2_info,
05a2f65c9e86 win32text: drop the `changing_parents` context in revert upgrade
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50012
diff changeset
   236
                )
48388
f5dea753fe4e win32text: drop associated dirstate cache information on revert
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46819
diff changeset
   237
    return orig(repo, ctx, names, uipathfn, actions, *args, **kwargs)
f5dea753fe4e win32text: drop associated dirstate cache information on revert
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46819
diff changeset
   238
f5dea753fe4e win32text: drop associated dirstate cache information on revert
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46819
diff changeset
   239
12837
c82056f2509f win32text: give deprecation warning
Steve Borho <steve@borho.org>
parents: 11269
diff changeset
   240
def extsetup(ui):
25829
38cf0908bf98 win32text: mark deprecated extension option deprecated
Matt Mackall <mpm@selenic.com>
parents: 25186
diff changeset
   241
    # deprecated config: win32text.warn
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   242
    if ui.configbool(b'win32text', b'warn'):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   243
        ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   244
            _(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   245
                b"win32text is deprecated: "
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   246
                b"https://mercurial-scm.org/wiki/Win32TextExtension\n"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   247
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 38783
diff changeset
   248
        )
48388
f5dea753fe4e win32text: drop associated dirstate cache information on revert
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46819
diff changeset
   249
    extensions.wrapfunction(cmdutil, '_performrevert', wrap_revert)