hgext/largefiles/lfutil.py
author Matt Mackall <mpm@selenic.com>
Mon, 31 Oct 2011 15:41:39 -0500
branchstable
changeset 15392 d7bfbc92a1c0
parent 15391 a5a6a9b7f3b9
child 15408 db8b0ee74025
permissions -rw-r--r--
util: add a doctest for empty sha() calls
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     1
# Copyright 2009-2010 Gregory P. Ward
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     2
# Copyright 2009-2010 Intelerad Medical Systems Incorporated
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     3
# Copyright 2010-2011 Fog Creek Software
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     4
# Copyright 2010-2011 Unity Technologies
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     5
#
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     6
# This software may be used and distributed according to the terms of the
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     7
# GNU General Public License version 2 or any later version.
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     8
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     9
'''largefiles utility code: must not import other modules in this package.'''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    10
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    11
import os
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    12
import errno
15320
681267a5f491 largefiles: use XDG and OS X-specific cache locations by default (issue3067)
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15319
diff changeset
    13
import platform
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    14
import shutil
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    15
import stat
15391
a5a6a9b7f3b9 largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents: 15371
diff changeset
    16
import tempfile
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    17
15226
2223ea21c98f largefiles: cleanup import, now that we can assume > 1.9 for bundled extension
Na'Tosha Bard <natosha@unity3d.com>
parents: 15224
diff changeset
    18
from mercurial import dirstate, httpconnection, match as match_, util, scmutil
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    19
from mercurial.i18n import _
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    20
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    21
shortname = '.hglf'
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    22
longname = 'largefiles'
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    23
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    24
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    25
# -- Portability wrappers ----------------------------------------------
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    26
15224
7c604d8c7e83 largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents: 15206
diff changeset
    27
def dirstate_walk(dirstate, matcher, unknown=False, ignored=False):
7c604d8c7e83 largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents: 15206
diff changeset
    28
    return dirstate.walk(matcher, [], unknown, ignored)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    29
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    30
def repo_add(repo, list):
15224
7c604d8c7e83 largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents: 15206
diff changeset
    31
    add = repo[None].add
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    32
    return add(list)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    33
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    34
def repo_remove(repo, list, unlink=False):
15224
7c604d8c7e83 largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents: 15206
diff changeset
    35
    def remove(list, unlink):
7c604d8c7e83 largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents: 15206
diff changeset
    36
        wlock = repo.wlock()
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    37
        try:
15224
7c604d8c7e83 largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents: 15206
diff changeset
    38
            if unlink:
7c604d8c7e83 largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents: 15206
diff changeset
    39
                for f in list:
7c604d8c7e83 largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents: 15206
diff changeset
    40
                    try:
7c604d8c7e83 largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents: 15206
diff changeset
    41
                        util.unlinkpath(repo.wjoin(f))
7c604d8c7e83 largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents: 15206
diff changeset
    42
                    except OSError, inst:
7c604d8c7e83 largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents: 15206
diff changeset
    43
                        if inst.errno != errno.ENOENT:
7c604d8c7e83 largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents: 15206
diff changeset
    44
                            raise
7c604d8c7e83 largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents: 15206
diff changeset
    45
            repo[None].forget(list)
7c604d8c7e83 largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents: 15206
diff changeset
    46
        finally:
7c604d8c7e83 largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents: 15206
diff changeset
    47
            wlock.release()
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    48
    return remove(list, unlink=unlink)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    49
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    50
def repo_forget(repo, list):
15224
7c604d8c7e83 largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents: 15206
diff changeset
    51
    forget = repo[None].forget
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    52
    return forget(list)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    53
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    54
def findoutgoing(repo, remote, force):
15224
7c604d8c7e83 largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents: 15206
diff changeset
    55
    from mercurial import discovery
7c604d8c7e83 largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents: 15206
diff changeset
    56
    common, _anyinc, _heads = discovery.findcommonincoming(repo,
7c604d8c7e83 largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents: 15206
diff changeset
    57
        remote, force=force)
7c604d8c7e83 largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents: 15206
diff changeset
    58
    return repo.changelog.findmissing(common)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    59
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    60
# -- Private worker functions ------------------------------------------
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    61
15227
a7686abf73a6 largefiles: factor out lfutil.getminsize()
Greg Ward <greg@gerg.ca>
parents: 15226
diff changeset
    62
def getminsize(ui, assumelfiles, opt, default=10):
a7686abf73a6 largefiles: factor out lfutil.getminsize()
Greg Ward <greg@gerg.ca>
parents: 15226
diff changeset
    63
    lfsize = opt
a7686abf73a6 largefiles: factor out lfutil.getminsize()
Greg Ward <greg@gerg.ca>
parents: 15226
diff changeset
    64
    if not lfsize and assumelfiles:
15304
9aa9d4bb3d88 largefiles: rename config setting 'size' to 'minsize'
Greg Ward <greg@gerg.ca>
parents: 15255
diff changeset
    65
        lfsize = ui.config(longname, 'minsize', default=default)
15227
a7686abf73a6 largefiles: factor out lfutil.getminsize()
Greg Ward <greg@gerg.ca>
parents: 15226
diff changeset
    66
    if lfsize:
a7686abf73a6 largefiles: factor out lfutil.getminsize()
Greg Ward <greg@gerg.ca>
parents: 15226
diff changeset
    67
        try:
15228
ee625de3541e largefiles: allow minimum size to be a float
Greg Ward <greg@gerg.ca>
parents: 15227
diff changeset
    68
            lfsize = float(lfsize)
15227
a7686abf73a6 largefiles: factor out lfutil.getminsize()
Greg Ward <greg@gerg.ca>
parents: 15226
diff changeset
    69
        except ValueError:
15228
ee625de3541e largefiles: allow minimum size to be a float
Greg Ward <greg@gerg.ca>
parents: 15227
diff changeset
    70
            raise util.Abort(_('largefiles: size must be number (not %s)\n')
15227
a7686abf73a6 largefiles: factor out lfutil.getminsize()
Greg Ward <greg@gerg.ca>
parents: 15226
diff changeset
    71
                             % lfsize)
a7686abf73a6 largefiles: factor out lfutil.getminsize()
Greg Ward <greg@gerg.ca>
parents: 15226
diff changeset
    72
    if lfsize is None:
a7686abf73a6 largefiles: factor out lfutil.getminsize()
Greg Ward <greg@gerg.ca>
parents: 15226
diff changeset
    73
        raise util.Abort(_('minimum size for largefiles must be specified'))
a7686abf73a6 largefiles: factor out lfutil.getminsize()
Greg Ward <greg@gerg.ca>
parents: 15226
diff changeset
    74
    return lfsize
a7686abf73a6 largefiles: factor out lfutil.getminsize()
Greg Ward <greg@gerg.ca>
parents: 15226
diff changeset
    75
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    76
def link(src, dest):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    77
    try:
15206
f85c76b16f27 largefiles: fix commit of specified file on non-windows
Na'Tosha Bard <natosha@unity3d.com>
parents: 15188
diff changeset
    78
        util.oslink(src, dest)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    79
    except OSError:
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
    80
        # if hardlinks fail, fallback on copy
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    81
        shutil.copyfile(src, dest)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    82
        os.chmod(dest, os.stat(src).st_mode)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    83
15316
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
    84
def usercachepath(ui, hash):
15350
8b8dd13295db largefiles: use ui.configpath() where appropriate
Greg Ward <greg@gerg.ca>
parents: 15349
diff changeset
    85
    path = ui.configpath(longname, 'usercache', None)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    86
    if path:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    87
        path = os.path.join(path, hash)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    88
    else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    89
        if os.name == 'nt':
15255
7ab05d752405 largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents: 15253
diff changeset
    90
            appdata = os.getenv('LOCALAPPDATA', os.getenv('APPDATA'))
7ab05d752405 largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents: 15253
diff changeset
    91
            path = os.path.join(appdata, longname, hash)
15320
681267a5f491 largefiles: use XDG and OS X-specific cache locations by default (issue3067)
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15319
diff changeset
    92
        elif platform.system() == 'Darwin':
681267a5f491 largefiles: use XDG and OS X-specific cache locations by default (issue3067)
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15319
diff changeset
    93
            path = os.path.join(os.getenv('HOME'), 'Library', 'Caches',
681267a5f491 largefiles: use XDG and OS X-specific cache locations by default (issue3067)
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15319
diff changeset
    94
                                longname, hash)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    95
        elif os.name == 'posix':
15320
681267a5f491 largefiles: use XDG and OS X-specific cache locations by default (issue3067)
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15319
diff changeset
    96
            path = os.getenv('XDG_CACHE_HOME')
681267a5f491 largefiles: use XDG and OS X-specific cache locations by default (issue3067)
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15319
diff changeset
    97
            if path:
681267a5f491 largefiles: use XDG and OS X-specific cache locations by default (issue3067)
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15319
diff changeset
    98
                path = os.path.join(path, longname, hash)
681267a5f491 largefiles: use XDG and OS X-specific cache locations by default (issue3067)
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15319
diff changeset
    99
            else:
681267a5f491 largefiles: use XDG and OS X-specific cache locations by default (issue3067)
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15319
diff changeset
   100
                path = os.path.join(os.getenv('HOME'), '.cache', longname, hash)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   101
        else:
15253
67d010779907 largefiles: improve error reporting
Greg Ward <greg@gerg.ca>
parents: 15252
diff changeset
   102
            raise util.Abort(_('unknown operating system: %s\n') % os.name)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   103
    return path
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   104
15316
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
   105
def inusercache(ui, hash):
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
   106
    return os.path.exists(usercachepath(ui, hash))
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   107
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   108
def findfile(repo, hash):
15316
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
   109
    if instore(repo, hash):
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
   110
        repo.ui.note(_('Found %s in store\n') % hash)
15317
41f371150ccb largefiles: make the store primary, and the user cache secondary
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15316
diff changeset
   111
    elif inusercache(repo.ui, hash):
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   112
        repo.ui.note(_('Found %s in system cache\n') % hash)
15317
41f371150ccb largefiles: make the store primary, and the user cache secondary
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15316
diff changeset
   113
        link(usercachepath(repo.ui, hash), storepath(repo, hash))
41f371150ccb largefiles: make the store primary, and the user cache secondary
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15316
diff changeset
   114
    else:
41f371150ccb largefiles: make the store primary, and the user cache secondary
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15316
diff changeset
   115
        return None
41f371150ccb largefiles: make the store primary, and the user cache secondary
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15316
diff changeset
   116
    return storepath(repo, hash)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   117
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   118
class largefiles_dirstate(dirstate.dirstate):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   119
    def __getitem__(self, key):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   120
        return super(largefiles_dirstate, self).__getitem__(unixpath(key))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   121
    def normal(self, f):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   122
        return super(largefiles_dirstate, self).normal(unixpath(f))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   123
    def remove(self, f):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   124
        return super(largefiles_dirstate, self).remove(unixpath(f))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   125
    def add(self, f):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   126
        return super(largefiles_dirstate, self).add(unixpath(f))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   127
    def drop(self, f):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   128
        return super(largefiles_dirstate, self).drop(unixpath(f))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   129
    def forget(self, f):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   130
        return super(largefiles_dirstate, self).forget(unixpath(f))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   131
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   132
def openlfdirstate(ui, repo):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   133
    '''
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   134
    Return a dirstate object that tracks largefiles: i.e. its root is
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   135
    the repo root, but it is saved in .hg/largefiles/dirstate.
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   136
    '''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   137
    admin = repo.join(longname)
15224
7c604d8c7e83 largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents: 15206
diff changeset
   138
    opener = scmutil.opener(admin)
15349
63455eb771af largefiles: drop more unnecessary compatibility checks
Greg Ward <greg@gerg.ca>
parents: 15347
diff changeset
   139
    lfdirstate = largefiles_dirstate(opener, ui, repo.root,
63455eb771af largefiles: drop more unnecessary compatibility checks
Greg Ward <greg@gerg.ca>
parents: 15347
diff changeset
   140
                                     repo.dirstate._validate)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   141
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   142
    # If the largefiles dirstate does not exist, populate and create
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   143
    # it. This ensures that we create it on the first meaningful
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   144
    # largefiles operation in a new clone. It also gives us an easy
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   145
    # way to forcibly rebuild largefiles state:
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   146
    #   rm .hg/largefiles/dirstate && hg status
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   147
    # Or even, if things are really messed up:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   148
    #   rm -rf .hg/largefiles && hg status
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   149
    if not os.path.exists(os.path.join(admin, 'dirstate')):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   150
        util.makedirs(admin)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   151
        matcher = getstandinmatcher(repo)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   152
        for standin in dirstate_walk(repo.dirstate, matcher):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   153
            lfile = splitstandin(standin)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   154
            hash = readstandin(repo, lfile)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   155
            lfdirstate.normallookup(lfile)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   156
            try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   157
                if hash == hashfile(lfile):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   158
                    lfdirstate.normal(lfile)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   159
            except IOError, err:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   160
                if err.errno != errno.ENOENT:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   161
                    raise
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   162
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   163
        lfdirstate.write()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   164
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   165
    return lfdirstate
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   166
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   167
def lfdirstate_status(lfdirstate, repo, rev):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   168
    wlock = repo.wlock()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   169
    try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   170
        match = match_.always(repo.root, repo.getcwd())
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   171
        s = lfdirstate.status(match, [], False, False, False)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   172
        unsure, modified, added, removed, missing, unknown, ignored, clean = s
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   173
        for lfile in unsure:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   174
            if repo[rev][standin(lfile)].data().strip() != \
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   175
                    hashfile(repo.wjoin(lfile)):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   176
                modified.append(lfile)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   177
            else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   178
                clean.append(lfile)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   179
                lfdirstate.normal(lfile)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   180
        lfdirstate.write()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   181
    finally:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   182
        wlock.release()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   183
    return (modified, added, removed, missing, unknown, ignored, clean)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   184
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   185
def listlfiles(repo, rev=None, matcher=None):
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   186
    '''return a list of largefiles in the working copy or the
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   187
    specified changeset'''
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   188
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   189
    if matcher is None:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   190
        matcher = getstandinmatcher(repo)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   191
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   192
    # ignore unknown files in working directory
15255
7ab05d752405 largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents: 15253
diff changeset
   193
    return [splitstandin(f)
7ab05d752405 largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents: 15253
diff changeset
   194
            for f in repo[rev].walk(matcher)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   195
            if rev is not None or repo.dirstate[f] != '?']
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   196
15316
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
   197
def instore(repo, hash):
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
   198
    return os.path.exists(storepath(repo, hash))
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   199
15316
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
   200
def storepath(repo, hash):
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   201
    return repo.join(os.path.join(longname, hash))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   202
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   203
def copyfromcache(repo, hash, filename):
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   204
    '''Copy the specified largefile from the repo or system cache to
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   205
    filename in the repository. Return true on success or false if the
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   206
    file was not found in either cache (which should not happened:
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   207
    this is meant to be called only after ensuring that the needed
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   208
    largefile exists in the cache).'''
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   209
    path = findfile(repo, hash)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   210
    if path is None:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   211
        return False
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   212
    util.makedirs(os.path.dirname(repo.wjoin(filename)))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   213
    shutil.copy(path, repo.wjoin(filename))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   214
    return True
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   215
15316
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
   216
def copytostore(repo, rev, file, uploaded=False):
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   217
    hash = readstandin(repo, file)
15316
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
   218
    if instore(repo, hash):
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   219
        return
15316
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
   220
    copytostoreabsolute(repo, repo.wjoin(file), hash)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   221
15316
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
   222
def copytostoreabsolute(repo, file, hash):
15371
f26ed4ea46d8 largefiles: remove lfutil.createdir, replace calls with util.makedirs
Hao Lian <hao@fogcreek.com>
parents: 15350
diff changeset
   223
    util.makedirs(os.path.dirname(storepath(repo, hash)))
15316
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
   224
    if inusercache(repo.ui, hash):
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
   225
        link(usercachepath(repo.ui, hash), storepath(repo, hash))
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   226
    else:
15316
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
   227
        shutil.copyfile(file, storepath(repo, hash))
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
   228
        os.chmod(storepath(repo, hash), os.stat(file).st_mode)
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
   229
        linktousercache(repo, hash)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   230
15316
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
   231
def linktousercache(repo, hash):
15371
f26ed4ea46d8 largefiles: remove lfutil.createdir, replace calls with util.makedirs
Hao Lian <hao@fogcreek.com>
parents: 15350
diff changeset
   232
    util.makedirs(os.path.dirname(usercachepath(repo.ui, hash)))
15316
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
   233
    link(storepath(repo, hash), usercachepath(repo.ui, hash))
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   234
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   235
def getstandinmatcher(repo, pats=[], opts={}):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   236
    '''Return a match object that applies pats to the standin directory'''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   237
    standindir = repo.pathto(shortname)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   238
    if pats:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   239
        # patterns supplied: search standin directory relative to current dir
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   240
        cwd = repo.getcwd()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   241
        if os.path.isabs(cwd):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   242
            # cwd is an absolute path for hg -R <reponame>
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   243
            # work relative to the repository root in this case
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   244
            cwd = ''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   245
        pats = [os.path.join(standindir, cwd, pat) for pat in pats]
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   246
    elif os.path.isdir(standindir):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   247
        # no patterns: relative to repo root
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   248
        pats = [standindir]
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   249
    else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   250
        # no patterns and no standin dir: return matcher that matches nothing
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   251
        match = match_.match(repo.root, None, [], exact=True)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   252
        match.matchfn = lambda f: False
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   253
        return match
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   254
    return getmatcher(repo, pats, opts, showbad=False)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   255
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   256
def getmatcher(repo, pats=[], opts={}, showbad=True):
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   257
    '''Wrapper around scmutil.match() that adds showbad: if false,
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   258
    neuter the match object's bad() method so it does not print any
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   259
    warnings about missing files or directories.'''
15224
7c604d8c7e83 largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents: 15206
diff changeset
   260
    match = scmutil.match(repo[None], pats, opts)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   261
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   262
    if not showbad:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   263
        match.bad = lambda f, msg: None
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   264
    return match
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   265
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   266
def composestandinmatcher(repo, rmatcher):
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   267
    '''Return a matcher that accepts standins corresponding to the
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   268
    files accepted by rmatcher. Pass the list of files in the matcher
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   269
    as the paths specified by the user.'''
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   270
    smatcher = getstandinmatcher(repo, rmatcher.files())
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   271
    isstandin = smatcher.matchfn
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   272
    def composed_matchfn(f):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   273
        return isstandin(f) and rmatcher.matchfn(splitstandin(f))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   274
    smatcher.matchfn = composed_matchfn
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   275
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   276
    return smatcher
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   277
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   278
def standin(filename):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   279
    '''Return the repo-relative path to the standin for the specified big
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   280
    file.'''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   281
    # Notes:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   282
    # 1) Most callers want an absolute path, but _create_standin() needs
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   283
    #    it repo-relative so lfadd() can pass it to repo_add().  So leave
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   284
    #    it up to the caller to use repo.wjoin() to get an absolute path.
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   285
    # 2) Join with '/' because that's what dirstate always uses, even on
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   286
    #    Windows. Change existing separator to '/' first in case we are
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   287
    #    passed filenames from an external source (like the command line).
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   288
    return shortname + '/' + filename.replace(os.sep, '/')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   289
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   290
def isstandin(filename):
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   291
    '''Return true if filename is a big file standin. filename must be
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   292
    in Mercurial's internal form (slash-separated).'''
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   293
    return filename.startswith(shortname + '/')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   294
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   295
def splitstandin(filename):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   296
    # Split on / because that's what dirstate always uses, even on Windows.
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   297
    # Change local separator to / first just in case we are passed filenames
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   298
    # from an external source (like the command line).
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   299
    bits = filename.replace(os.sep, '/').split('/', 1)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   300
    if len(bits) == 2 and bits[0] == shortname:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   301
        return bits[1]
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   302
    else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   303
        return None
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   304
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   305
def updatestandin(repo, standin):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   306
    file = repo.wjoin(splitstandin(standin))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   307
    if os.path.exists(file):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   308
        hash = hashfile(file)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   309
        executable = getexecutable(file)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   310
        writestandin(repo, standin, hash, executable)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   311
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   312
def readstandin(repo, filename, node=None):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   313
    '''read hex hash from standin for filename at given node, or working
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   314
    directory if no node is given'''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   315
    return repo[node][standin(filename)].data().strip()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   316
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   317
def writestandin(repo, standin, hash, executable):
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   318
    '''write hash to <repo.root>/<standin>'''
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   319
    writehash(hash, repo.wjoin(standin), executable)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   320
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   321
def copyandhash(instream, outfile):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   322
    '''Read bytes from instream (iterable) and write them to outfile,
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   323
    computing the SHA-1 hash of the data along the way.  Close outfile
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   324
    when done and return the binary hash.'''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   325
    hasher = util.sha1('')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   326
    for data in instream:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   327
        hasher.update(data)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   328
        outfile.write(data)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   329
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   330
    # Blecch: closing a file that somebody else opened is rude and
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   331
    # wrong. But it's so darn convenient and practical! After all,
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   332
    # outfile was opened just to copy and hash.
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   333
    outfile.close()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   334
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   335
    return hasher.digest()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   336
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   337
def hashrepofile(repo, file):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   338
    return hashfile(repo.wjoin(file))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   339
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   340
def hashfile(file):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   341
    if not os.path.exists(file):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   342
        return ''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   343
    hasher = util.sha1('')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   344
    fd = open(file, 'rb')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   345
    for data in blockstream(fd):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   346
        hasher.update(data)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   347
    fd.close()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   348
    return hasher.hexdigest()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   349
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   350
class limitreader(object):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   351
    def __init__(self, f, limit):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   352
        self.f = f
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   353
        self.limit = limit
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   354
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   355
    def read(self, length):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   356
        if self.limit == 0:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   357
            return ''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   358
        length = length > self.limit and self.limit or length
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   359
        self.limit -= length
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   360
        return self.f.read(length)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   361
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   362
    def close(self):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   363
        pass
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   364
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   365
def blockstream(infile, blocksize=128 * 1024):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   366
    """Generator that yields blocks of data from infile and closes infile."""
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   367
    while True:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   368
        data = infile.read(blocksize)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   369
        if not data:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   370
            break
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   371
        yield data
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   372
    # same blecch as copyandhash() above
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   373
    infile.close()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   374
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   375
def readhash(filename):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   376
    rfile = open(filename, 'rb')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   377
    hash = rfile.read(40)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   378
    rfile.close()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   379
    if len(hash) < 40:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   380
        raise util.Abort(_('bad hash in \'%s\' (only %d bytes long)')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   381
                         % (filename, len(hash)))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   382
    return hash
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   383
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   384
def writehash(hash, filename, executable):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   385
    util.makedirs(os.path.dirname(filename))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   386
    if os.path.exists(filename):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   387
        os.unlink(filename)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   388
    wfile = open(filename, 'wb')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   389
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   390
    try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   391
        wfile.write(hash)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   392
        wfile.write('\n')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   393
    finally:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   394
        wfile.close()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   395
    if os.path.exists(filename):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   396
        os.chmod(filename, getmode(executable))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   397
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   398
def getexecutable(filename):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   399
    mode = os.stat(filename).st_mode
15255
7ab05d752405 largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents: 15253
diff changeset
   400
    return ((mode & stat.S_IXUSR) and
7ab05d752405 largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents: 15253
diff changeset
   401
            (mode & stat.S_IXGRP) and
7ab05d752405 largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents: 15253
diff changeset
   402
            (mode & stat.S_IXOTH))
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   403
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   404
def getmode(executable):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   405
    if executable:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   406
        return 0755
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   407
    else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   408
        return 0644
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   409
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   410
def urljoin(first, second, *arg):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   411
    def join(left, right):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   412
        if not left.endswith('/'):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   413
            left += '/'
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   414
        if right.startswith('/'):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   415
            right = right[1:]
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   416
        return left + right
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   417
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   418
    url = join(first, second)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   419
    for a in arg:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   420
        url = join(url, a)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   421
    return url
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   422
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   423
def hexsha1(data):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   424
    """hexsha1 returns the hex-encoded sha1 sum of the data in the file-like
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   425
    object data"""
15347
799e56609ef6 largefiles: use util.sha1() instead of hashlib.sha1() everywhere
Thomas Arendsen Hein <thomas@intevation.de>
parents: 15333
diff changeset
   426
    h = util.sha1()
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   427
    for chunk in util.filechunkiter(data):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   428
        h.update(chunk)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   429
    return h.hexdigest()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   430
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   431
def httpsendfile(ui, filename):
15224
7c604d8c7e83 largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents: 15206
diff changeset
   432
    return httpconnection.httpsendfile(ui, filename, 'rb')
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   433
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   434
def unixpath(path):
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
   435
    '''Return a version of path normalized for use with the lfdirstate.'''
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   436
    return os.path.normpath(path).replace(os.sep, '/')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   437
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   438
def islfilesrepo(repo):
15170
c1a4a3220711 largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents: 15169
diff changeset
   439
    return ('largefiles' in repo.requirements and
15319
9da7e96cd5c2 largefiles: remove redundant any_ function
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15317
diff changeset
   440
            util.any(shortname + '/' in f[0] for f in repo.store.datafiles()))
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   441
15391
a5a6a9b7f3b9 largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents: 15371
diff changeset
   442
def mkstemp(repo, prefix):
a5a6a9b7f3b9 largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents: 15371
diff changeset
   443
    '''Returns a file descriptor and a filename corresponding to a temporary
a5a6a9b7f3b9 largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents: 15371
diff changeset
   444
    file in the repo's largefiles store.'''
a5a6a9b7f3b9 largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents: 15371
diff changeset
   445
    path = repo.join(longname)
15392
d7bfbc92a1c0 util: add a doctest for empty sha() calls
Matt Mackall <mpm@selenic.com>
parents: 15391
diff changeset
   446
    util.makedirs(path)
15391
a5a6a9b7f3b9 largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents: 15371
diff changeset
   447
    return tempfile.mkstemp(prefix=prefix, dir=path)
a5a6a9b7f3b9 largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents: 15371
diff changeset
   448
15333
f37b71fec602 largefiles: py2.4 doesn't have BaseException
Matt Mackall <mpm@selenic.com>
parents: 15320
diff changeset
   449
class storeprotonotcapable(Exception):
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   450
    def __init__(self, storetypes):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   451
        self.storetypes = storetypes