author | Hao Lian <hao@fogcreek.com> |
Thu, 03 Nov 2011 10:59:32 -0400 | |
branch | stable |
changeset 15408 | db8b0ee74025 |
parent 15392 | d7bfbc92a1c0 |
child 15548 | f76584098c88 |
permissions | -rw-r--r-- |
15168 | 1 |
# Copyright 2009-2010 Gregory P. Ward |
2 |
# Copyright 2009-2010 Intelerad Medical Systems Incorporated |
|
3 |
# Copyright 2010-2011 Fog Creek Software |
|
4 |
# Copyright 2010-2011 Unity Technologies |
|
5 |
# |
|
6 |
# This software may be used and distributed according to the terms of the |
|
7 |
# GNU General Public License version 2 or any later version. |
|
8 |
||
9 |
'''largefiles utility code: must not import other modules in this package.''' |
|
10 |
||
11 |
import os |
|
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 | 14 |
import shutil |
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 | 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 | 19 |
from mercurial.i18n import _ |
20 |
||
21 |
shortname = '.hglf' |
|
22 |
longname = 'largefiles' |
|
23 |
||
24 |
||
25 |
# -- Portability wrappers ---------------------------------------------- |
|
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 | 29 |
|
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 | 32 |
return add(list) |
33 |
||
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 | 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 | 48 |
return remove(list, unlink=unlink) |
49 |
||
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 | 52 |
return forget(list) |
53 |
||
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 | 59 |
|
60 |
# -- Private worker functions ------------------------------------------ |
|
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 | 76 |
def link(src, dest): |
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 | 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 | 81 |
shutil.copyfile(src, dest) |
82 |
os.chmod(dest, os.stat(src).st_mode) |
|
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 | 86 |
if path: |
87 |
path = os.path.join(path, hash) |
|
88 |
else: |
|
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 | 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 | 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 | 103 |
return path |
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 | 107 |
|
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 | 112 |
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
|
113 |
path = storepath(repo, hash) |
db8b0ee74025
largefiles: ensure destination directory exists before findfile links to there
Hao Lian <hao@fogcreek.com>
parents:
15392
diff
changeset
|
114 |
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
|
115 |
link(usercachepath(repo.ui, hash), path) |
15317
41f371150ccb
largefiles: make the store primary, and the user cache secondary
Benjamin Pollack <benjamin@bitquabit.com>
parents:
15316
diff
changeset
|
116 |
else: |
41f371150ccb
largefiles: make the store primary, and the user cache secondary
Benjamin Pollack <benjamin@bitquabit.com>
parents:
15316
diff
changeset
|
117 |
return None |
41f371150ccb
largefiles: make the store primary, and the user cache secondary
Benjamin Pollack <benjamin@bitquabit.com>
parents:
15316
diff
changeset
|
118 |
return storepath(repo, hash) |
15168 | 119 |
|
120 |
class largefiles_dirstate(dirstate.dirstate): |
|
121 |
def __getitem__(self, key): |
|
122 |
return super(largefiles_dirstate, self).__getitem__(unixpath(key)) |
|
123 |
def normal(self, f): |
|
124 |
return super(largefiles_dirstate, self).normal(unixpath(f)) |
|
125 |
def remove(self, f): |
|
126 |
return super(largefiles_dirstate, self).remove(unixpath(f)) |
|
127 |
def add(self, f): |
|
128 |
return super(largefiles_dirstate, self).add(unixpath(f)) |
|
129 |
def drop(self, f): |
|
130 |
return super(largefiles_dirstate, self).drop(unixpath(f)) |
|
131 |
def forget(self, f): |
|
132 |
return super(largefiles_dirstate, self).forget(unixpath(f)) |
|
133 |
||
134 |
def openlfdirstate(ui, repo): |
|
135 |
''' |
|
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15228
diff
changeset
|
136 |
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
|
137 |
the repo root, but it is saved in .hg/largefiles/dirstate. |
15168 | 138 |
''' |
139 |
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
|
140 |
opener = scmutil.opener(admin) |
15349
63455eb771af
largefiles: drop more unnecessary compatibility checks
Greg Ward <greg@gerg.ca>
parents:
15347
diff
changeset
|
141 |
lfdirstate = largefiles_dirstate(opener, ui, repo.root, |
63455eb771af
largefiles: drop more unnecessary compatibility checks
Greg Ward <greg@gerg.ca>
parents:
15347
diff
changeset
|
142 |
repo.dirstate._validate) |
15168 | 143 |
|
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15228
diff
changeset
|
144 |
# 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
|
145 |
# 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
|
146 |
# 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
|
147 |
# way to forcibly rebuild largefiles state: |
15168 | 148 |
# rm .hg/largefiles/dirstate && hg status |
149 |
# Or even, if things are really messed up: |
|
150 |
# rm -rf .hg/largefiles && hg status |
|
151 |
if not os.path.exists(os.path.join(admin, 'dirstate')): |
|
152 |
util.makedirs(admin) |
|
153 |
matcher = getstandinmatcher(repo) |
|
154 |
for standin in dirstate_walk(repo.dirstate, matcher): |
|
155 |
lfile = splitstandin(standin) |
|
156 |
hash = readstandin(repo, lfile) |
|
157 |
lfdirstate.normallookup(lfile) |
|
158 |
try: |
|
159 |
if hash == hashfile(lfile): |
|
160 |
lfdirstate.normal(lfile) |
|
161 |
except IOError, err: |
|
162 |
if err.errno != errno.ENOENT: |
|
163 |
raise |
|
164 |
||
165 |
lfdirstate.write() |
|
166 |
||
167 |
return lfdirstate |
|
168 |
||
169 |
def lfdirstate_status(lfdirstate, repo, rev): |
|
170 |
wlock = repo.wlock() |
|
171 |
try: |
|
172 |
match = match_.always(repo.root, repo.getcwd()) |
|
173 |
s = lfdirstate.status(match, [], False, False, False) |
|
174 |
unsure, modified, added, removed, missing, unknown, ignored, clean = s |
|
175 |
for lfile in unsure: |
|
176 |
if repo[rev][standin(lfile)].data().strip() != \ |
|
177 |
hashfile(repo.wjoin(lfile)): |
|
178 |
modified.append(lfile) |
|
179 |
else: |
|
180 |
clean.append(lfile) |
|
181 |
lfdirstate.normal(lfile) |
|
182 |
lfdirstate.write() |
|
183 |
finally: |
|
184 |
wlock.release() |
|
185 |
return (modified, added, removed, missing, unknown, ignored, clean) |
|
186 |
||
187 |
def listlfiles(repo, rev=None, matcher=None): |
|
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15228
diff
changeset
|
188 |
'''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
|
189 |
specified changeset''' |
15168 | 190 |
|
191 |
if matcher is None: |
|
192 |
matcher = getstandinmatcher(repo) |
|
193 |
||
194 |
# ignore unknown files in working directory |
|
15255
7ab05d752405
largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents:
15253
diff
changeset
|
195 |
return [splitstandin(f) |
7ab05d752405
largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents:
15253
diff
changeset
|
196 |
for f in repo[rev].walk(matcher) |
15168 | 197 |
if rev is not None or repo.dirstate[f] != '?'] |
198 |
||
15316
c65f5b6e26d4
largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents:
15304
diff
changeset
|
199 |
def instore(repo, hash): |
c65f5b6e26d4
largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents:
15304
diff
changeset
|
200 |
return os.path.exists(storepath(repo, hash)) |
15168 | 201 |
|
15316
c65f5b6e26d4
largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents:
15304
diff
changeset
|
202 |
def storepath(repo, hash): |
15168 | 203 |
return repo.join(os.path.join(longname, hash)) |
204 |
||
205 |
def copyfromcache(repo, hash, filename): |
|
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15228
diff
changeset
|
206 |
'''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
|
207 |
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
|
208 |
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
|
209 |
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
|
210 |
largefile exists in the cache).''' |
15168 | 211 |
path = findfile(repo, hash) |
212 |
if path is None: |
|
213 |
return False |
|
214 |
util.makedirs(os.path.dirname(repo.wjoin(filename))) |
|
215 |
shutil.copy(path, repo.wjoin(filename)) |
|
216 |
return True |
|
217 |
||
15316
c65f5b6e26d4
largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents:
15304
diff
changeset
|
218 |
def copytostore(repo, rev, file, uploaded=False): |
15168 | 219 |
hash = readstandin(repo, file) |
15316
c65f5b6e26d4
largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents:
15304
diff
changeset
|
220 |
if instore(repo, hash): |
15168 | 221 |
return |
15316
c65f5b6e26d4
largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents:
15304
diff
changeset
|
222 |
copytostoreabsolute(repo, repo.wjoin(file), hash) |
15168 | 223 |
|
15316
c65f5b6e26d4
largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents:
15304
diff
changeset
|
224 |
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
|
225 |
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
|
226 |
if inusercache(repo.ui, hash): |
c65f5b6e26d4
largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents:
15304
diff
changeset
|
227 |
link(usercachepath(repo.ui, hash), storepath(repo, hash)) |
15168 | 228 |
else: |
15316
c65f5b6e26d4
largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents:
15304
diff
changeset
|
229 |
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
|
230 |
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
|
231 |
linktousercache(repo, hash) |
15168 | 232 |
|
15316
c65f5b6e26d4
largefiles: rename functions and methods to match desired behavior
Benjamin Pollack <benjamin@bitquabit.com>
parents:
15304
diff
changeset
|
233 |
def linktousercache(repo, hash): |
15371
f26ed4ea46d8
largefiles: remove lfutil.createdir, replace calls with util.makedirs
Hao Lian <hao@fogcreek.com>
parents:
15350
diff
changeset
|
234 |
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
|
235 |
link(storepath(repo, hash), usercachepath(repo.ui, hash)) |
15168 | 236 |
|
237 |
def getstandinmatcher(repo, pats=[], opts={}): |
|
238 |
'''Return a match object that applies pats to the standin directory''' |
|
239 |
standindir = repo.pathto(shortname) |
|
240 |
if pats: |
|
241 |
# patterns supplied: search standin directory relative to current dir |
|
242 |
cwd = repo.getcwd() |
|
243 |
if os.path.isabs(cwd): |
|
244 |
# cwd is an absolute path for hg -R <reponame> |
|
245 |
# work relative to the repository root in this case |
|
246 |
cwd = '' |
|
247 |
pats = [os.path.join(standindir, cwd, pat) for pat in pats] |
|
248 |
elif os.path.isdir(standindir): |
|
249 |
# no patterns: relative to repo root |
|
250 |
pats = [standindir] |
|
251 |
else: |
|
252 |
# no patterns and no standin dir: return matcher that matches nothing |
|
253 |
match = match_.match(repo.root, None, [], exact=True) |
|
254 |
match.matchfn = lambda f: False |
|
255 |
return match |
|
256 |
return getmatcher(repo, pats, opts, showbad=False) |
|
257 |
||
258 |
def getmatcher(repo, pats=[], opts={}, showbad=True): |
|
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15228
diff
changeset
|
259 |
'''Wrapper around scmutil.match() that adds showbad: if false, |
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15228
diff
changeset
|
260 |
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
|
261 |
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
|
262 |
match = scmutil.match(repo[None], pats, opts) |
15168 | 263 |
|
264 |
if not showbad: |
|
265 |
match.bad = lambda f, msg: None |
|
266 |
return match |
|
267 |
||
268 |
def composestandinmatcher(repo, rmatcher): |
|
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15228
diff
changeset
|
269 |
'''Return a matcher that accepts standins corresponding to the |
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15228
diff
changeset
|
270 |
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
|
271 |
as the paths specified by the user.''' |
15168 | 272 |
smatcher = getstandinmatcher(repo, rmatcher.files()) |
273 |
isstandin = smatcher.matchfn |
|
274 |
def composed_matchfn(f): |
|
275 |
return isstandin(f) and rmatcher.matchfn(splitstandin(f)) |
|
276 |
smatcher.matchfn = composed_matchfn |
|
277 |
||
278 |
return smatcher |
|
279 |
||
280 |
def standin(filename): |
|
281 |
'''Return the repo-relative path to the standin for the specified big |
|
282 |
file.''' |
|
283 |
# Notes: |
|
284 |
# 1) Most callers want an absolute path, but _create_standin() needs |
|
285 |
# it repo-relative so lfadd() can pass it to repo_add(). So leave |
|
286 |
# it up to the caller to use repo.wjoin() to get an absolute path. |
|
287 |
# 2) Join with '/' because that's what dirstate always uses, even on |
|
288 |
# Windows. Change existing separator to '/' first in case we are |
|
289 |
# passed filenames from an external source (like the command line). |
|
290 |
return shortname + '/' + filename.replace(os.sep, '/') |
|
291 |
||
292 |
def isstandin(filename): |
|
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15228
diff
changeset
|
293 |
'''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
|
294 |
in Mercurial's internal form (slash-separated).''' |
15168 | 295 |
return filename.startswith(shortname + '/') |
296 |
||
297 |
def splitstandin(filename): |
|
298 |
# Split on / because that's what dirstate always uses, even on Windows. |
|
299 |
# Change local separator to / first just in case we are passed filenames |
|
300 |
# from an external source (like the command line). |
|
301 |
bits = filename.replace(os.sep, '/').split('/', 1) |
|
302 |
if len(bits) == 2 and bits[0] == shortname: |
|
303 |
return bits[1] |
|
304 |
else: |
|
305 |
return None |
|
306 |
||
307 |
def updatestandin(repo, standin): |
|
308 |
file = repo.wjoin(splitstandin(standin)) |
|
309 |
if os.path.exists(file): |
|
310 |
hash = hashfile(file) |
|
311 |
executable = getexecutable(file) |
|
312 |
writestandin(repo, standin, hash, executable) |
|
313 |
||
314 |
def readstandin(repo, filename, node=None): |
|
315 |
'''read hex hash from standin for filename at given node, or working |
|
316 |
directory if no node is given''' |
|
317 |
return repo[node][standin(filename)].data().strip() |
|
318 |
||
319 |
def writestandin(repo, standin, hash, executable): |
|
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15228
diff
changeset
|
320 |
'''write hash to <repo.root>/<standin>''' |
15168 | 321 |
writehash(hash, repo.wjoin(standin), executable) |
322 |
||
323 |
def copyandhash(instream, outfile): |
|
324 |
'''Read bytes from instream (iterable) and write them to outfile, |
|
325 |
computing the SHA-1 hash of the data along the way. Close outfile |
|
326 |
when done and return the binary hash.''' |
|
327 |
hasher = util.sha1('') |
|
328 |
for data in instream: |
|
329 |
hasher.update(data) |
|
330 |
outfile.write(data) |
|
331 |
||
332 |
# 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
|
333 |
# wrong. But it's so darn convenient and practical! After all, |
15168 | 334 |
# outfile was opened just to copy and hash. |
335 |
outfile.close() |
|
336 |
||
337 |
return hasher.digest() |
|
338 |
||
339 |
def hashrepofile(repo, file): |
|
340 |
return hashfile(repo.wjoin(file)) |
|
341 |
||
342 |
def hashfile(file): |
|
343 |
if not os.path.exists(file): |
|
344 |
return '' |
|
345 |
hasher = util.sha1('') |
|
346 |
fd = open(file, 'rb') |
|
347 |
for data in blockstream(fd): |
|
348 |
hasher.update(data) |
|
349 |
fd.close() |
|
350 |
return hasher.hexdigest() |
|
351 |
||
352 |
class limitreader(object): |
|
353 |
def __init__(self, f, limit): |
|
354 |
self.f = f |
|
355 |
self.limit = limit |
|
356 |
||
357 |
def read(self, length): |
|
358 |
if self.limit == 0: |
|
359 |
return '' |
|
360 |
length = length > self.limit and self.limit or length |
|
361 |
self.limit -= length |
|
362 |
return self.f.read(length) |
|
363 |
||
364 |
def close(self): |
|
365 |
pass |
|
366 |
||
367 |
def blockstream(infile, blocksize=128 * 1024): |
|
368 |
"""Generator that yields blocks of data from infile and closes infile.""" |
|
369 |
while True: |
|
370 |
data = infile.read(blocksize) |
|
371 |
if not data: |
|
372 |
break |
|
373 |
yield data |
|
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15228
diff
changeset
|
374 |
# same blecch as copyandhash() above |
15168 | 375 |
infile.close() |
376 |
||
377 |
def readhash(filename): |
|
378 |
rfile = open(filename, 'rb') |
|
379 |
hash = rfile.read(40) |
|
380 |
rfile.close() |
|
381 |
if len(hash) < 40: |
|
382 |
raise util.Abort(_('bad hash in \'%s\' (only %d bytes long)') |
|
383 |
% (filename, len(hash))) |
|
384 |
return hash |
|
385 |
||
386 |
def writehash(hash, filename, executable): |
|
387 |
util.makedirs(os.path.dirname(filename)) |
|
388 |
if os.path.exists(filename): |
|
389 |
os.unlink(filename) |
|
390 |
wfile = open(filename, 'wb') |
|
391 |
||
392 |
try: |
|
393 |
wfile.write(hash) |
|
394 |
wfile.write('\n') |
|
395 |
finally: |
|
396 |
wfile.close() |
|
397 |
if os.path.exists(filename): |
|
398 |
os.chmod(filename, getmode(executable)) |
|
399 |
||
400 |
def getexecutable(filename): |
|
401 |
mode = os.stat(filename).st_mode |
|
15255
7ab05d752405
largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents:
15253
diff
changeset
|
402 |
return ((mode & stat.S_IXUSR) and |
7ab05d752405
largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents:
15253
diff
changeset
|
403 |
(mode & stat.S_IXGRP) and |
7ab05d752405
largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents:
15253
diff
changeset
|
404 |
(mode & stat.S_IXOTH)) |
15168 | 405 |
|
406 |
def getmode(executable): |
|
407 |
if executable: |
|
408 |
return 0755 |
|
409 |
else: |
|
410 |
return 0644 |
|
411 |
||
412 |
def urljoin(first, second, *arg): |
|
413 |
def join(left, right): |
|
414 |
if not left.endswith('/'): |
|
415 |
left += '/' |
|
416 |
if right.startswith('/'): |
|
417 |
right = right[1:] |
|
418 |
return left + right |
|
419 |
||
420 |
url = join(first, second) |
|
421 |
for a in arg: |
|
422 |
url = join(url, a) |
|
423 |
return url |
|
424 |
||
425 |
def hexsha1(data): |
|
426 |
"""hexsha1 returns the hex-encoded sha1 sum of the data in the file-like |
|
427 |
object data""" |
|
15347
799e56609ef6
largefiles: use util.sha1() instead of hashlib.sha1() everywhere
Thomas Arendsen Hein <thomas@intevation.de>
parents:
15333
diff
changeset
|
428 |
h = util.sha1() |
15168 | 429 |
for chunk in util.filechunkiter(data): |
430 |
h.update(chunk) |
|
431 |
return h.hexdigest() |
|
432 |
||
433 |
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
|
434 |
return httpconnection.httpsendfile(ui, filename, 'rb') |
15168 | 435 |
|
436 |
def unixpath(path): |
|
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15228
diff
changeset
|
437 |
'''Return a version of path normalized for use with the lfdirstate.''' |
15168 | 438 |
return os.path.normpath(path).replace(os.sep, '/') |
439 |
||
440 |
def islfilesrepo(repo): |
|
15170
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15169
diff
changeset
|
441 |
return ('largefiles' in repo.requirements and |
15319
9da7e96cd5c2
largefiles: remove redundant any_ function
Benjamin Pollack <benjamin@bitquabit.com>
parents:
15317
diff
changeset
|
442 |
util.any(shortname + '/' in f[0] for f in repo.store.datafiles())) |
15168 | 443 |
|
15391
a5a6a9b7f3b9
largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents:
15371
diff
changeset
|
444 |
def mkstemp(repo, prefix): |
a5a6a9b7f3b9
largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents:
15371
diff
changeset
|
445 |
'''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
|
446 |
file in the repo's largefiles store.''' |
a5a6a9b7f3b9
largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents:
15371
diff
changeset
|
447 |
path = repo.join(longname) |
15392
d7bfbc92a1c0
util: add a doctest for empty sha() calls
Matt Mackall <mpm@selenic.com>
parents:
15391
diff
changeset
|
448 |
util.makedirs(path) |
15391
a5a6a9b7f3b9
largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents:
15371
diff
changeset
|
449 |
return tempfile.mkstemp(prefix=prefix, dir=path) |
a5a6a9b7f3b9
largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents:
15371
diff
changeset
|
450 |
|
15333
f37b71fec602
largefiles: py2.4 doesn't have BaseException
Matt Mackall <mpm@selenic.com>
parents:
15320
diff
changeset
|
451 |
class storeprotonotcapable(Exception): |
15168 | 452 |
def __init__(self, storetypes): |
453 |
self.storetypes = storetypes |