annotate hgext/largefiles/lfutil.py @ 18148:bf6252d12c34

largefiles: simplify lfdirstate ignore handling - it is only for tracking .hglf
author Mads Kiilerich <madski@unity3d.com>
date Thu, 13 Dec 2012 19:19:06 +0100
parents 79f2493198e1
children 14e31a631e41
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
16
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
17 from mercurial import dirstate, httpconnection, match as match_, util, scmutil
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
18 from mercurial.i18n import _
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
19
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
20 shortname = '.hglf'
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
21 longname = 'largefiles'
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
22
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
23
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
24 # -- Portability wrappers ----------------------------------------------
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
25
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16245
diff changeset
26 def dirstatewalk(dirstate, matcher, unknown=False, ignored=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
27 return dirstate.walk(matcher, [], unknown, ignored)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
28
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16245
diff changeset
29 def repoadd(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
30 add = repo[None].add
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
31 return add(list)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
32
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16245
diff changeset
33 def reporemove(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
34 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
35 wlock = repo.wlock()
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
36 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
37 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
38 for f in list:
18143
242d2f4ec01c util: fold ENOENT check into unlinkpath, controlled by new ignoremissing flag
Mads Kiilerich <madski@unity3d.com>
parents: 17878
diff changeset
39 util.unlinkpath(repo.wjoin(f), ignoremissing=True)
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
40 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
41 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
42 wlock.release()
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
43 return remove(list, unlink=unlink)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
44
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16245
diff changeset
45 def repoforget(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
46 forget = repo[None].forget
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
47 return forget(list)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
48
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
49 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
50 from mercurial import discovery
17794
a03cca2cf6c2 largefiles: update lfutil.findoutgoing() discovery method
Matt Harbison <matt_harbison@yahoo.com>
parents: 17661
diff changeset
51 outgoing = discovery.findcommonoutgoing(repo, remote.peer(), force=force)
a03cca2cf6c2 largefiles: update lfutil.findoutgoing() discovery method
Matt Harbison <matt_harbison@yahoo.com>
parents: 17661
diff changeset
52 return outgoing.missing
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
53
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
54 # -- Private worker functions ------------------------------------------
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
55
15227
a7686abf73a6 largefiles: factor out lfutil.getminsize()
Greg Ward <greg@gerg.ca>
parents: 15226
diff changeset
56 def getminsize(ui, assumelfiles, opt, default=10):
a7686abf73a6 largefiles: factor out lfutil.getminsize()
Greg Ward <greg@gerg.ca>
parents: 15226
diff changeset
57 lfsize = opt
a7686abf73a6 largefiles: factor out lfutil.getminsize()
Greg Ward <greg@gerg.ca>
parents: 15226
diff changeset
58 if not lfsize and assumelfiles:
15304
9aa9d4bb3d88 largefiles: rename config setting 'size' to 'minsize'
Greg Ward <greg@gerg.ca>
parents: 15255
diff changeset
59 lfsize = ui.config(longname, 'minsize', default=default)
15227
a7686abf73a6 largefiles: factor out lfutil.getminsize()
Greg Ward <greg@gerg.ca>
parents: 15226
diff changeset
60 if lfsize:
a7686abf73a6 largefiles: factor out lfutil.getminsize()
Greg Ward <greg@gerg.ca>
parents: 15226
diff changeset
61 try:
15228
ee625de3541e largefiles: allow minimum size to be a float
Greg Ward <greg@gerg.ca>
parents: 15227
diff changeset
62 lfsize = float(lfsize)
15227
a7686abf73a6 largefiles: factor out lfutil.getminsize()
Greg Ward <greg@gerg.ca>
parents: 15226
diff changeset
63 except ValueError:
15228
ee625de3541e largefiles: allow minimum size to be a float
Greg Ward <greg@gerg.ca>
parents: 15227
diff changeset
64 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
65 % lfsize)
a7686abf73a6 largefiles: factor out lfutil.getminsize()
Greg Ward <greg@gerg.ca>
parents: 15226
diff changeset
66 if lfsize is None:
a7686abf73a6 largefiles: factor out lfutil.getminsize()
Greg Ward <greg@gerg.ca>
parents: 15226
diff changeset
67 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
68 return lfsize
a7686abf73a6 largefiles: factor out lfutil.getminsize()
Greg Ward <greg@gerg.ca>
parents: 15226
diff changeset
69
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
70 def link(src, dest):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
71 try:
15206
f85c76b16f27 largefiles: fix commit of specified file on non-windows
Na'Tosha Bard <natosha@unity3d.com>
parents: 15188
diff changeset
72 util.oslink(src, dest)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
73 except OSError:
15572
926bc23d0b6a largefiles: copy files into .hg/largefiles atomically
Martin Geisler <mg@aragost.com>
parents: 15571
diff changeset
74 # if hardlinks fail, fallback on atomic copy
926bc23d0b6a largefiles: copy files into .hg/largefiles atomically
Martin Geisler <mg@aragost.com>
parents: 15571
diff changeset
75 dst = util.atomictempfile(dest)
15699
84e55467093c largefiles: copy files in binary mode (issue3164)
Matt Mackall <mpm@selenic.com>
parents: 15658
diff changeset
76 for chunk in util.filechunkiter(open(src, 'rb')):
15572
926bc23d0b6a largefiles: copy files into .hg/largefiles atomically
Martin Geisler <mg@aragost.com>
parents: 15571
diff changeset
77 dst.write(chunk)
926bc23d0b6a largefiles: copy files into .hg/largefiles atomically
Martin Geisler <mg@aragost.com>
parents: 15571
diff changeset
78 dst.close()
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
79 os.chmod(dest, os.stat(src).st_mode)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
80
15316
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
81 def usercachepath(ui, hash):
15350
8b8dd13295db largefiles: use ui.configpath() where appropriate
Greg Ward <greg@gerg.ca>
parents: 15349
diff changeset
82 path = ui.configpath(longname, 'usercache', None)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
83 if path:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
84 path = os.path.join(path, hash)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
85 else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
86 if os.name == 'nt':
15255
7ab05d752405 largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents: 15253
diff changeset
87 appdata = os.getenv('LOCALAPPDATA', os.getenv('APPDATA'))
15658
971c55ce03b8 largefiles: don't require a user cache (issue3088) (issue3155)
Kevin Gessner <kevin@fogcreek.com>
parents: 15572
diff changeset
88 if appdata:
971c55ce03b8 largefiles: don't require a user cache (issue3088) (issue3155)
Kevin Gessner <kevin@fogcreek.com>
parents: 15572
diff changeset
89 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
90 elif platform.system() == 'Darwin':
15658
971c55ce03b8 largefiles: don't require a user cache (issue3088) (issue3155)
Kevin Gessner <kevin@fogcreek.com>
parents: 15572
diff changeset
91 home = os.getenv('HOME')
971c55ce03b8 largefiles: don't require a user cache (issue3088) (issue3155)
Kevin Gessner <kevin@fogcreek.com>
parents: 15572
diff changeset
92 if home:
971c55ce03b8 largefiles: don't require a user cache (issue3088) (issue3155)
Kevin Gessner <kevin@fogcreek.com>
parents: 15572
diff changeset
93 path = os.path.join(home, 'Library', 'Caches',
971c55ce03b8 largefiles: don't require a user cache (issue3088) (issue3155)
Kevin Gessner <kevin@fogcreek.com>
parents: 15572
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:
15658
971c55ce03b8 largefiles: don't require a user cache (issue3088) (issue3155)
Kevin Gessner <kevin@fogcreek.com>
parents: 15572
diff changeset
100 home = os.getenv('HOME')
971c55ce03b8 largefiles: don't require a user cache (issue3088) (issue3155)
Kevin Gessner <kevin@fogcreek.com>
parents: 15572
diff changeset
101 if home:
971c55ce03b8 largefiles: don't require a user cache (issue3088) (issue3155)
Kevin Gessner <kevin@fogcreek.com>
parents: 15572
diff changeset
102 path = os.path.join(home, '.cache', longname, hash)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
103 else:
15253
67d010779907 largefiles: improve error reporting
Greg Ward <greg@gerg.ca>
parents: 15252
diff changeset
104 raise util.Abort(_('unknown operating system: %s\n') % os.name)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
105 return path
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
106
15316
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
107 def inusercache(ui, hash):
15658
971c55ce03b8 largefiles: don't require a user cache (issue3088) (issue3155)
Kevin Gessner <kevin@fogcreek.com>
parents: 15572
diff changeset
108 path = usercachepath(ui, hash)
971c55ce03b8 largefiles: don't require a user cache (issue3088) (issue3155)
Kevin Gessner <kevin@fogcreek.com>
parents: 15572
diff changeset
109 return path and os.path.exists(path)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
110
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
111 def findfile(repo, hash):
15316
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
112 if instore(repo, hash):
16928
73b9286e667c largefiles: lowercase messages
Martin Geisler <mg@aragost.com>
parents: 16247
diff changeset
113 repo.ui.note(_('found %s in store\n') % hash)
15913
c35dcde25174 largefiles: refactor lfutil.findfiles to be more logical
Na'Tosha Bard <natosha@unity3d.com>
parents: 15796
diff changeset
114 return storepath(repo, hash)
15317
41f371150ccb largefiles: make the store primary, and the user cache secondary
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15316
diff changeset
115 elif inusercache(repo.ui, hash):
16928
73b9286e667c largefiles: lowercase messages
Martin Geisler <mg@aragost.com>
parents: 16247
diff changeset
116 repo.ui.note(_('found %s in system cache\n') % hash)
15408
db8b0ee74025 largefiles: ensure destination directory exists before findfile links to there
Hao Lian <hao@fogcreek.com>
parents: 15392
diff changeset
117 path = storepath(repo, hash)
db8b0ee74025 largefiles: ensure destination directory exists before findfile links to there
Hao Lian <hao@fogcreek.com>
parents: 15392
diff changeset
118 util.makedirs(os.path.dirname(path))
db8b0ee74025 largefiles: ensure destination directory exists before findfile links to there
Hao Lian <hao@fogcreek.com>
parents: 15392
diff changeset
119 link(usercachepath(repo.ui, hash), path)
15913
c35dcde25174 largefiles: refactor lfutil.findfiles to be more logical
Na'Tosha Bard <natosha@unity3d.com>
parents: 15796
diff changeset
120 return path
c35dcde25174 largefiles: refactor lfutil.findfiles to be more logical
Na'Tosha Bard <natosha@unity3d.com>
parents: 15796
diff changeset
121 return None
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
122
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16245
diff changeset
123 class largefilesdirstate(dirstate.dirstate):
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
124 def __getitem__(self, key):
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16245
diff changeset
125 return super(largefilesdirstate, self).__getitem__(unixpath(key))
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
126 def normal(self, f):
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16245
diff changeset
127 return super(largefilesdirstate, self).normal(unixpath(f))
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
128 def remove(self, f):
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16245
diff changeset
129 return super(largefilesdirstate, self).remove(unixpath(f))
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
130 def add(self, f):
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16245
diff changeset
131 return super(largefilesdirstate, self).add(unixpath(f))
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
132 def drop(self, f):
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16245
diff changeset
133 return super(largefilesdirstate, self).drop(unixpath(f))
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
134 def forget(self, f):
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16245
diff changeset
135 return super(largefilesdirstate, self).forget(unixpath(f))
15793
3ef07ecdb0d5 largefiles: correctly handle dirstate status when rebasing
Na'Tosha Bard <natosha@unity3d.com>
parents: 15700
diff changeset
136 def normallookup(self, f):
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16245
diff changeset
137 return super(largefilesdirstate, self).normallookup(unixpath(f))
18148
bf6252d12c34 largefiles: simplify lfdirstate ignore handling - it is only for tracking .hglf
Mads Kiilerich <madski@unity3d.com>
parents: 18147
diff changeset
138 def _ignore(self):
bf6252d12c34 largefiles: simplify lfdirstate ignore handling - it is only for tracking .hglf
Mads Kiilerich <madski@unity3d.com>
parents: 18147
diff changeset
139 return False
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
140
17659
ae57920ac188 largefiles: enable islfilesrepo() prior to a commit (issue3541)
Matt Harbison <matt_harbison@yahoo.com>
parents: 17270
diff changeset
141 def openlfdirstate(ui, repo, create=True):
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
142 '''
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
143 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
144 the repo root, but it is saved in .hg/largefiles/dirstate.
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
145 '''
18147
79f2493198e1 largefiles: rename 'admin' to more descriptive 'lfstoredir'
Mads Kiilerich <madski@unity3d.com>
parents: 18146
diff changeset
146 lfstoredir = repo.join(longname)
79f2493198e1 largefiles: rename 'admin' to more descriptive 'lfstoredir'
Mads Kiilerich <madski@unity3d.com>
parents: 18146
diff changeset
147 opener = scmutil.opener(lfstoredir)
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16245
diff changeset
148 lfdirstate = largefilesdirstate(opener, ui, repo.root,
15349
63455eb771af largefiles: drop more unnecessary compatibility checks
Greg Ward <greg@gerg.ca>
parents: 15347
diff changeset
149 repo.dirstate._validate)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
150
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
151 # 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
152 # it. This ensures that we create it on the first meaningful
15794
0d91211dd12f largefiles: fix inappropriate locking (issue3182)
Levi Bard <levi@unity3d.com>
parents: 15793
diff changeset
153 # largefiles operation in a new clone.
18147
79f2493198e1 largefiles: rename 'admin' to more descriptive 'lfstoredir'
Mads Kiilerich <madski@unity3d.com>
parents: 18146
diff changeset
154 if create and not os.path.exists(os.path.join(lfstoredir, 'dirstate')):
79f2493198e1 largefiles: rename 'admin' to more descriptive 'lfstoredir'
Mads Kiilerich <madski@unity3d.com>
parents: 18146
diff changeset
155 util.makedirs(lfstoredir)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
156 matcher = getstandinmatcher(repo)
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16245
diff changeset
157 for standin in dirstatewalk(repo.dirstate, matcher):
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
158 lfile = splitstandin(standin)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
159 hash = readstandin(repo, lfile)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
160 lfdirstate.normallookup(lfile)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
161 try:
15553
e89385e4ef8d largefiles: file storage should be relative to repo, not relative to cwd
Mads Kiilerich <mads@kiilerich.com>
parents: 15548
diff changeset
162 if hash == hashfile(repo.wjoin(lfile)):
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
163 lfdirstate.normal(lfile)
15548
f76584098c88 largefiles: fix 'hg clone . ../foo' OSError abort
Martin Geisler <mg@lazybytes.net>
parents: 15408
diff changeset
164 except OSError, err:
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
165 if err.errno != errno.ENOENT:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
166 raise
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
167 return lfdirstate
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
168
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16245
diff changeset
169 def lfdirstatestatus(lfdirstate, repo, rev):
15794
0d91211dd12f largefiles: fix inappropriate locking (issue3182)
Levi Bard <levi@unity3d.com>
parents: 15793
diff changeset
170 match = match_.always(repo.root, repo.getcwd())
0d91211dd12f largefiles: fix inappropriate locking (issue3182)
Levi Bard <levi@unity3d.com>
parents: 15793
diff changeset
171 s = lfdirstate.status(match, [], False, False, False)
0d91211dd12f largefiles: fix inappropriate locking (issue3182)
Levi Bard <levi@unity3d.com>
parents: 15793
diff changeset
172 unsure, modified, added, removed, missing, unknown, ignored, clean = s
0d91211dd12f largefiles: fix inappropriate locking (issue3182)
Levi Bard <levi@unity3d.com>
parents: 15793
diff changeset
173 for lfile in unsure:
0d91211dd12f largefiles: fix inappropriate locking (issue3182)
Levi Bard <levi@unity3d.com>
parents: 15793
diff changeset
174 if repo[rev][standin(lfile)].data().strip() != \
0d91211dd12f largefiles: fix inappropriate locking (issue3182)
Levi Bard <levi@unity3d.com>
parents: 15793
diff changeset
175 hashfile(repo.wjoin(lfile)):
0d91211dd12f largefiles: fix inappropriate locking (issue3182)
Levi Bard <levi@unity3d.com>
parents: 15793
diff changeset
176 modified.append(lfile)
0d91211dd12f largefiles: fix inappropriate locking (issue3182)
Levi Bard <levi@unity3d.com>
parents: 15793
diff changeset
177 else:
0d91211dd12f largefiles: fix inappropriate locking (issue3182)
Levi Bard <levi@unity3d.com>
parents: 15793
diff changeset
178 clean.append(lfile)
0d91211dd12f largefiles: fix inappropriate locking (issue3182)
Levi Bard <levi@unity3d.com>
parents: 15793
diff changeset
179 lfdirstate.normal(lfile)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
180 return (modified, added, removed, missing, unknown, ignored, clean)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
181
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
182 def listlfiles(repo, rev=None, matcher=None):
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
183 '''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
184 specified changeset'''
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
185
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
186 if matcher is None:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
187 matcher = getstandinmatcher(repo)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
188
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
189 # ignore unknown files in working directory
15255
7ab05d752405 largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents: 15253
diff changeset
190 return [splitstandin(f)
7ab05d752405 largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents: 15253
diff changeset
191 for f in repo[rev].walk(matcher)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
192 if rev is not None or repo.dirstate[f] != '?']
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
193
15316
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
194 def instore(repo, hash):
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
195 return os.path.exists(storepath(repo, hash))
15168
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 storepath(repo, hash):
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
198 return repo.join(os.path.join(longname, hash))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
199
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
200 def copyfromcache(repo, hash, filename):
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
201 '''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
202 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
203 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
204 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
205 largefile exists in the cache).'''
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
206 path = findfile(repo, hash)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
207 if path is None:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
208 return False
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
209 util.makedirs(os.path.dirname(repo.wjoin(filename)))
15570
0f208626d503 largefiles: add comment about non-atomic working directory
Martin Geisler <mg@aragost.com>
parents: 15553
diff changeset
210 # The write may fail before the file is fully written, but we
0f208626d503 largefiles: add comment about non-atomic working directory
Martin Geisler <mg@aragost.com>
parents: 15553
diff changeset
211 # don't use atomic writes in the working copy.
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
212 shutil.copy(path, repo.wjoin(filename))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
213 return True
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
214
15316
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
215 def copytostore(repo, rev, file, uploaded=False):
17877
92bbb21d4b13 largefiles: respect the rev when reading standins in copytostore() (issue3630)
Matt Harbison <matt_harbison@yahoo.com>
parents: 17794
diff changeset
216 hash = readstandin(repo, file, rev)
15316
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
217 if instore(repo, hash):
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
218 return
15316
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
219 copytostoreabsolute(repo, repo.wjoin(file), hash)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
220
15796
3e5b6045ccfc largefiles: factor out a copyalltostore() function
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 15794
diff changeset
221 def copyalltostore(repo, node):
3e5b6045ccfc largefiles: factor out a copyalltostore() function
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 15794
diff changeset
222 '''Copy all largefiles in a given revision to the store'''
3e5b6045ccfc largefiles: factor out a copyalltostore() function
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 15794
diff changeset
223
3e5b6045ccfc largefiles: factor out a copyalltostore() function
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 15794
diff changeset
224 ctx = repo[node]
3e5b6045ccfc largefiles: factor out a copyalltostore() function
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 15794
diff changeset
225 for filename in ctx.files():
3e5b6045ccfc largefiles: factor out a copyalltostore() function
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 15794
diff changeset
226 if isstandin(filename) and filename in ctx.manifest():
3e5b6045ccfc largefiles: factor out a copyalltostore() function
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 15794
diff changeset
227 realfile = splitstandin(filename)
3e5b6045ccfc largefiles: factor out a copyalltostore() function
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 15794
diff changeset
228 copytostore(repo, ctx.node(), realfile)
3e5b6045ccfc largefiles: factor out a copyalltostore() function
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 15794
diff changeset
229
3e5b6045ccfc largefiles: factor out a copyalltostore() function
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 15794
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 copytostoreabsolute(repo, file, 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(storepath(repo, hash)))
15316
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
233 if inusercache(repo.ui, hash):
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
234 link(usercachepath(repo.ui, hash), storepath(repo, hash))
17878
d1d0140287b8 largefiles: don't copy largefiles from working dir to the store while converting
Matt Harbison <matt_harbison@yahoo.com>
parents: 17877
diff changeset
235 elif not getattr(repo, "_isconverting", False):
16153
05197f9fd1f3 largefiles: use repo.store.createmode for new files in .hg/largefiles
Martin Geisler <mg@aragost.com>
parents: 16103
diff changeset
236 dst = util.atomictempfile(storepath(repo, hash),
05197f9fd1f3 largefiles: use repo.store.createmode for new files in .hg/largefiles
Martin Geisler <mg@aragost.com>
parents: 16103
diff changeset
237 createmode=repo.store.createmode)
15699
84e55467093c largefiles: copy files in binary mode (issue3164)
Matt Mackall <mpm@selenic.com>
parents: 15658
diff changeset
238 for chunk in util.filechunkiter(open(file, 'rb')):
15571
809788118aa2 largefiles: write .hg/largefiles/ files atomically
Martin Geisler <mg@aragost.com>
parents: 15570
diff changeset
239 dst.write(chunk)
809788118aa2 largefiles: write .hg/largefiles/ files atomically
Martin Geisler <mg@aragost.com>
parents: 15570
diff changeset
240 dst.close()
15316
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
241 linktousercache(repo, hash)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
242
15316
c65f5b6e26d4 largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents: 15304
diff changeset
243 def linktousercache(repo, hash):
15658
971c55ce03b8 largefiles: don't require a user cache (issue3088) (issue3155)
Kevin Gessner <kevin@fogcreek.com>
parents: 15572
diff changeset
244 path = usercachepath(repo.ui, hash)
971c55ce03b8 largefiles: don't require a user cache (issue3088) (issue3155)
Kevin Gessner <kevin@fogcreek.com>
parents: 15572
diff changeset
245 if path:
971c55ce03b8 largefiles: don't require a user cache (issue3088) (issue3155)
Kevin Gessner <kevin@fogcreek.com>
parents: 15572
diff changeset
246 util.makedirs(os.path.dirname(path))
971c55ce03b8 largefiles: don't require a user cache (issue3088) (issue3155)
Kevin Gessner <kevin@fogcreek.com>
parents: 15572
diff changeset
247 link(storepath(repo, hash), path)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
248
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
249 def getstandinmatcher(repo, pats=[], opts={}):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
250 '''Return a match object that applies pats to the standin directory'''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
251 standindir = repo.pathto(shortname)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
252 if pats:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
253 # patterns supplied: search standin directory relative to current dir
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
254 cwd = repo.getcwd()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
255 if os.path.isabs(cwd):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
256 # cwd is an absolute path for hg -R <reponame>
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
257 # work relative to the repository root in this case
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
258 cwd = ''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
259 pats = [os.path.join(standindir, cwd, pat) for pat in pats]
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
260 elif os.path.isdir(standindir):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
261 # no patterns: relative to repo root
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
262 pats = [standindir]
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
263 else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
264 # no patterns and no standin dir: return matcher that matches nothing
18146
819c7e10d60d largefiles: simplify lfutil.getstandinmatcher by inlining getmatcher
Mads Kiilerich <madski@unity3d.com>
parents: 18143
diff changeset
265 return match_.match(repo.root, None, [], exact=True)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
266
18146
819c7e10d60d largefiles: simplify lfutil.getstandinmatcher by inlining getmatcher
Mads Kiilerich <madski@unity3d.com>
parents: 18143
diff changeset
267 # no 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
268 match = scmutil.match(repo[None], pats, opts)
18146
819c7e10d60d largefiles: simplify lfutil.getstandinmatcher by inlining getmatcher
Mads Kiilerich <madski@unity3d.com>
parents: 18143
diff changeset
269 match.bad = lambda f, msg: None
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
270 return match
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
271
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
272 def composestandinmatcher(repo, rmatcher):
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
273 '''Return a matcher that accepts standins corresponding to the
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
274 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
275 as the paths specified by the user.'''
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
276 smatcher = getstandinmatcher(repo, rmatcher.files())
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
277 isstandin = smatcher.matchfn
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16245
diff changeset
278 def composedmatchfn(f):
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
279 return isstandin(f) and rmatcher.matchfn(splitstandin(f))
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16245
diff changeset
280 smatcher.matchfn = composedmatchfn
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
281
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
282 return smatcher
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
283
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
284 def standin(filename):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
285 '''Return the repo-relative path to the standin for the specified big
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
286 file.'''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
287 # Notes:
17425
e95ec38f86b0 fix wording and not-completely-trivial spelling errors and bad docstrings
Mads Kiilerich <mads@kiilerich.com>
parents: 17270
diff changeset
288 # 1) Some callers want an absolute path, but for instance addlargefiles
e95ec38f86b0 fix wording and not-completely-trivial spelling errors and bad docstrings
Mads Kiilerich <mads@kiilerich.com>
parents: 17270
diff changeset
289 # needs it repo-relative so it can be passed to repoadd(). So leave
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
290 # it up to the caller to use repo.wjoin() to get an absolute path.
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
291 # 2) Join with '/' because that's what dirstate always uses, even on
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
292 # Windows. Change existing separator to '/' first in case we are
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
293 # passed filenames from an external source (like the command line).
16066
6a42846cf769 i18n: use util.pconvert() instead of 'str.replace()' for problematic encoding
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15915
diff changeset
294 return shortname + '/' + util.pconvert(filename)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
295
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
296 def isstandin(filename):
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
297 '''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
298 in Mercurial's internal form (slash-separated).'''
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
299 return filename.startswith(shortname + '/')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
300
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
301 def splitstandin(filename):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
302 # Split on / because that's what dirstate always uses, even on Windows.
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
303 # Change local separator to / first just in case we are passed filenames
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
304 # from an external source (like the command line).
16066
6a42846cf769 i18n: use util.pconvert() instead of 'str.replace()' for problematic encoding
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15915
diff changeset
305 bits = util.pconvert(filename).split('/', 1)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
306 if len(bits) == 2 and bits[0] == shortname:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
307 return bits[1]
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
308 else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
309 return None
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
310
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
311 def updatestandin(repo, standin):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
312 file = repo.wjoin(splitstandin(standin))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
313 if os.path.exists(file):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
314 hash = hashfile(file)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
315 executable = getexecutable(file)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
316 writestandin(repo, standin, hash, executable)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
317
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
318 def readstandin(repo, filename, node=None):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
319 '''read hex hash from standin for filename at given node, or working
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
320 directory if no node is given'''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
321 return repo[node][standin(filename)].data().strip()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
322
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
323 def writestandin(repo, standin, hash, executable):
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
324 '''write hash to <repo.root>/<standin>'''
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
325 writehash(hash, repo.wjoin(standin), executable)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
326
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
327 def copyandhash(instream, outfile):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
328 '''Read bytes from instream (iterable) and write them to outfile,
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
329 computing the SHA-1 hash of the data along the way. Close outfile
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
330 when done and return the binary hash.'''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
331 hasher = util.sha1('')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
332 for data in instream:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
333 hasher.update(data)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
334 outfile.write(data)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
335
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
336 # 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
337 # wrong. But it's so darn convenient and practical! After all,
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
338 # outfile was opened just to copy and hash.
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
339 outfile.close()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
340
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
341 return hasher.digest()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
342
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
343 def hashrepofile(repo, file):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
344 return hashfile(repo.wjoin(file))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
345
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
346 def hashfile(file):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
347 if not os.path.exists(file):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
348 return ''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
349 hasher = util.sha1('')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
350 fd = open(file, 'rb')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
351 for data in blockstream(fd):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
352 hasher.update(data)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
353 fd.close()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
354 return hasher.hexdigest()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
355
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
356 class limitreader(object):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
357 def __init__(self, f, limit):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
358 self.f = f
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
359 self.limit = limit
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
360
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
361 def read(self, length):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
362 if self.limit == 0:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
363 return ''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
364 length = length > self.limit and self.limit or length
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
365 self.limit -= length
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
366 return self.f.read(length)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
367
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
368 def close(self):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
369 pass
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
370
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
371 def blockstream(infile, blocksize=128 * 1024):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
372 """Generator that yields blocks of data from infile and closes infile."""
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
373 while True:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
374 data = infile.read(blocksize)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
375 if not data:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
376 break
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
377 yield data
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
378 # same blecch as copyandhash() above
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
379 infile.close()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
380
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
381 def writehash(hash, filename, executable):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
382 util.makedirs(os.path.dirname(filename))
15574
c9328c829cd9 largefiles: simplify lfutil.writehash
Martin Geisler <mg@aragost.com>
parents: 15572
diff changeset
383 util.writefile(filename, hash + '\n')
c9328c829cd9 largefiles: simplify lfutil.writehash
Martin Geisler <mg@aragost.com>
parents: 15572
diff changeset
384 os.chmod(filename, getmode(executable))
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
385
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
386 def getexecutable(filename):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
387 mode = os.stat(filename).st_mode
15255
7ab05d752405 largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents: 15253
diff changeset
388 return ((mode & stat.S_IXUSR) and
7ab05d752405 largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents: 15253
diff changeset
389 (mode & stat.S_IXGRP) and
7ab05d752405 largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents: 15253
diff changeset
390 (mode & stat.S_IXOTH))
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
391
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
392 def getmode(executable):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
393 if executable:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
394 return 0755
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
395 else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
396 return 0644
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
397
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
398 def urljoin(first, second, *arg):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
399 def join(left, right):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
400 if not left.endswith('/'):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
401 left += '/'
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
402 if right.startswith('/'):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
403 right = right[1:]
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
404 return left + right
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
405
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
406 url = join(first, second)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
407 for a in arg:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
408 url = join(url, a)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
409 return url
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
410
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
411 def hexsha1(data):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
412 """hexsha1 returns the hex-encoded sha1 sum of the data in the file-like
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
413 object data"""
15347
799e56609ef6 largefiles: use util.sha1() instead of hashlib.sha1() everywhere
Thomas Arendsen Hein <thomas@intevation.de>
parents: 15333
diff changeset
414 h = util.sha1()
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
415 for chunk in util.filechunkiter(data):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
416 h.update(chunk)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
417 return h.hexdigest()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
418
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
419 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
420 return httpconnection.httpsendfile(ui, filename, 'rb')
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
421
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
422 def unixpath(path):
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15228
diff changeset
423 '''Return a version of path normalized for use with the lfdirstate.'''
16066
6a42846cf769 i18n: use util.pconvert() instead of 'str.replace()' for problematic encoding
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15915
diff changeset
424 return util.pconvert(os.path.normpath(path))
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
425
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
426 def islfilesrepo(repo):
17659
ae57920ac188 largefiles: enable islfilesrepo() prior to a commit (issue3541)
Matt Harbison <matt_harbison@yahoo.com>
parents: 17270
diff changeset
427 if ('largefiles' in repo.requirements and
ae57920ac188 largefiles: enable islfilesrepo() prior to a commit (issue3541)
Matt Harbison <matt_harbison@yahoo.com>
parents: 17270
diff changeset
428 util.any(shortname + '/' in f[0] for f in repo.store.datafiles())):
ae57920ac188 largefiles: enable islfilesrepo() prior to a commit (issue3541)
Matt Harbison <matt_harbison@yahoo.com>
parents: 17270
diff changeset
429 return True
ae57920ac188 largefiles: enable islfilesrepo() prior to a commit (issue3541)
Matt Harbison <matt_harbison@yahoo.com>
parents: 17270
diff changeset
430
ae57920ac188 largefiles: enable islfilesrepo() prior to a commit (issue3541)
Matt Harbison <matt_harbison@yahoo.com>
parents: 17270
diff changeset
431 return util.any(openlfdirstate(repo.ui, repo, False))
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
432
15333
f37b71fec602 largefiles: py2.4 doesn't have BaseException
Matt Mackall <mpm@selenic.com>
parents: 15320
diff changeset
433 class storeprotonotcapable(Exception):
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
434 def __init__(self, storetypes):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
435 self.storetypes = storetypes
16103
3e1efb458e8b largefiles: only cache largefiles in new heads
Na'Tosha Bard <natosha@unity3d.com>
parents: 16066
diff changeset
436
3e1efb458e8b largefiles: only cache largefiles in new heads
Na'Tosha Bard <natosha@unity3d.com>
parents: 16066
diff changeset
437 def getcurrentheads(repo):
3e1efb458e8b largefiles: only cache largefiles in new heads
Na'Tosha Bard <natosha@unity3d.com>
parents: 16066
diff changeset
438 branches = repo.branchmap()
3e1efb458e8b largefiles: only cache largefiles in new heads
Na'Tosha Bard <natosha@unity3d.com>
parents: 16066
diff changeset
439 heads = []
3e1efb458e8b largefiles: only cache largefiles in new heads
Na'Tosha Bard <natosha@unity3d.com>
parents: 16066
diff changeset
440 for branch in branches:
3e1efb458e8b largefiles: only cache largefiles in new heads
Na'Tosha Bard <natosha@unity3d.com>
parents: 16066
diff changeset
441 newheads = repo.branchheads(branch)
3e1efb458e8b largefiles: only cache largefiles in new heads
Na'Tosha Bard <natosha@unity3d.com>
parents: 16066
diff changeset
442 heads = heads + newheads
3e1efb458e8b largefiles: only cache largefiles in new heads
Na'Tosha Bard <natosha@unity3d.com>
parents: 16066
diff changeset
443 return heads
16120
47ee41fcf42b largefiles: optimize update speed by only updating changed largefiles
Na'Tosha Bard <natosha@unity3d.com>
parents: 16103
diff changeset
444
47ee41fcf42b largefiles: optimize update speed by only updating changed largefiles
Na'Tosha Bard <natosha@unity3d.com>
parents: 16103
diff changeset
445 def getstandinsstate(repo):
47ee41fcf42b largefiles: optimize update speed by only updating changed largefiles
Na'Tosha Bard <natosha@unity3d.com>
parents: 16103
diff changeset
446 standins = []
47ee41fcf42b largefiles: optimize update speed by only updating changed largefiles
Na'Tosha Bard <natosha@unity3d.com>
parents: 16103
diff changeset
447 matcher = getstandinmatcher(repo)
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16245
diff changeset
448 for standin in dirstatewalk(repo.dirstate, matcher):
16120
47ee41fcf42b largefiles: optimize update speed by only updating changed largefiles
Na'Tosha Bard <natosha@unity3d.com>
parents: 16103
diff changeset
449 lfile = splitstandin(standin)
47ee41fcf42b largefiles: optimize update speed by only updating changed largefiles
Na'Tosha Bard <natosha@unity3d.com>
parents: 16103
diff changeset
450 standins.append((lfile, readstandin(repo, lfile)))
47ee41fcf42b largefiles: optimize update speed by only updating changed largefiles
Na'Tosha Bard <natosha@unity3d.com>
parents: 16103
diff changeset
451 return standins
16245
a18ad914aa21 largefiles: move calculation of largefiles for updating to utility function
Na'Tosha Bard <natosha@unity3d.com>
parents: 16166
diff changeset
452
a18ad914aa21 largefiles: move calculation of largefiles for updating to utility function
Na'Tosha Bard <natosha@unity3d.com>
parents: 16166
diff changeset
453 def getlfilestoupdate(oldstandins, newstandins):
a18ad914aa21 largefiles: move calculation of largefiles for updating to utility function
Na'Tosha Bard <natosha@unity3d.com>
parents: 16166
diff changeset
454 changedstandins = set(oldstandins).symmetric_difference(set(newstandins))
a18ad914aa21 largefiles: move calculation of largefiles for updating to utility function
Na'Tosha Bard <natosha@unity3d.com>
parents: 16166
diff changeset
455 filelist = []
a18ad914aa21 largefiles: move calculation of largefiles for updating to utility function
Na'Tosha Bard <natosha@unity3d.com>
parents: 16166
diff changeset
456 for f in changedstandins:
a18ad914aa21 largefiles: move calculation of largefiles for updating to utility function
Na'Tosha Bard <natosha@unity3d.com>
parents: 16166
diff changeset
457 if f[0] not in filelist:
a18ad914aa21 largefiles: move calculation of largefiles for updating to utility function
Na'Tosha Bard <natosha@unity3d.com>
parents: 16166
diff changeset
458 filelist.append(f[0])
a18ad914aa21 largefiles: move calculation of largefiles for updating to utility function
Na'Tosha Bard <natosha@unity3d.com>
parents: 16166
diff changeset
459 return filelist