annotate hgext/largefiles/lfutil.py @ 15171:547da6115d1d

largefiles: eliminate naked exceptions
author Matt Mackall <mpm@selenic.com>
date Thu, 29 Sep 2011 17:14:47 -0500
parents c1a4a3220711
children 8e115063950d
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
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
13 import inspect
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 import hashlib
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
17
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
18 from mercurial import cmdutil, dirstate, httpconnection, match as match_, \
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
19 url as url_, util
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
20 from mercurial.i18n import _
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
21
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
22 try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
23 from mercurial import scmutil
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
24 except ImportError:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
25 pass
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
26
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
27 shortname = '.hglf'
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
28 longname = 'largefiles'
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
29
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
30
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
31 # -- Portability wrappers ----------------------------------------------
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
32
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
33 if 'subrepos' in inspect.getargspec(dirstate.dirstate.status)[0]:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
34 # for Mercurial >= 1.5
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
35 def dirstate_walk(dirstate, matcher, unknown=False, ignored=False):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
36 return dirstate.walk(matcher, [], unknown, ignored)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
37 else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
38 # for Mercurial <= 1.4
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
39 def dirstate_walk(dirstate, matcher, unknown=False, ignored=False):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
40 return dirstate.walk(matcher, unknown, ignored)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
41
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
42 def repo_add(repo, list):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
43 try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
44 # Mercurial <= 1.5
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
45 add = repo.add
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
46 except AttributeError:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
47 # Mercurial >= 1.6
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
48 add = repo[None].add
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
49 return add(list)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
50
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
51 def repo_remove(repo, list, unlink=False):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
52 try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
53 # Mercurial <= 1.5
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
54 remove = repo.remove
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
55 except AttributeError:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
56 # Mercurial >= 1.6
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
57 try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
58 # Mercurial <= 1.8
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
59 remove = repo[None].remove
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
60 except AttributeError:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
61 # Mercurial >= 1.9
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
62 def remove(list, unlink):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
63 wlock = repo.wlock()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
64 try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
65 if unlink:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
66 for f in list:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
67 try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
68 util.unlinkpath(repo.wjoin(f))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
69 except OSError, inst:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
70 if inst.errno != errno.ENOENT:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
71 raise
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
72 repo[None].forget(list)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
73 finally:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
74 wlock.release()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
75
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
76 return remove(list, unlink=unlink)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
77
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
78 def repo_forget(repo, list):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
79 try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
80 # Mercurial <= 1.5
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
81 forget = repo.forget
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
82 except AttributeError:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
83 # Mercurial >= 1.6
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
84 forget = repo[None].forget
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
85 return forget(list)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
86
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
87 def findoutgoing(repo, remote, force):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
88 # First attempt is for Mercurial <= 1.5 second is for >= 1.6
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
89 try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
90 return repo.findoutgoing(remote)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
91 except AttributeError:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
92 from mercurial import discovery
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
93 try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
94 # Mercurial <= 1.8
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
95 return discovery.findoutgoing(repo, remote, force=force)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
96 except AttributeError:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
97 # Mercurial >= 1.9
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
98 common, _anyinc, _heads = discovery.findcommonincoming(repo,
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
99 remote, force=force)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
100 return repo.changelog.findmissing(common)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
101
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
102 # -- Private worker functions ------------------------------------------
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
103
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
104 if os.name == 'nt':
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
105 from mercurial import win32
15171
547da6115d1d largefiles: eliminate naked exceptions
Matt Mackall <mpm@selenic.com>
parents: 15170
diff changeset
106 linkfn = win32.oslink
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
107
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
108 def link(src, dest):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
109 try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
110 linkfn(src, dest)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
111 except OSError:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
112 # If hardlinks fail fall back on copy
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
113 shutil.copyfile(src, dest)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
114 os.chmod(dest, os.stat(src).st_mode)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
115
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
116 def systemcachepath(ui, hash):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
117 path = ui.config(longname, 'systemcache', None)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
118 if path:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
119 path = os.path.join(path, hash)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
120 else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
121 if os.name == 'nt':
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
122 path = os.path.join(os.getenv('LOCALAPPDATA') or \
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
123 os.getenv('APPDATA'), longname, hash)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
124 elif os.name == 'posix':
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
125 path = os.path.join(os.getenv('HOME'), '.' + longname, hash)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
126 else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
127 raise util.Abort(_('Unknown operating system: %s\n') % os.name)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
128 return path
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
129
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
130 def insystemcache(ui, hash):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
131 return os.path.exists(systemcachepath(ui, hash))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
132
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
133 def findfile(repo, hash):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
134 if incache(repo, hash):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
135 repo.ui.note(_('Found %s in cache\n') % hash)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
136 return cachepath(repo, hash)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
137 if insystemcache(repo.ui, hash):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
138 repo.ui.note(_('Found %s in system cache\n') % hash)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
139 return systemcachepath(repo.ui, hash)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
140 return None
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
141
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
142 class largefiles_dirstate(dirstate.dirstate):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
143 def __getitem__(self, key):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
144 return super(largefiles_dirstate, self).__getitem__(unixpath(key))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
145 def normal(self, f):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
146 return super(largefiles_dirstate, self).normal(unixpath(f))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
147 def remove(self, f):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
148 return super(largefiles_dirstate, self).remove(unixpath(f))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
149 def add(self, f):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
150 return super(largefiles_dirstate, self).add(unixpath(f))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
151 def drop(self, f):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
152 return super(largefiles_dirstate, self).drop(unixpath(f))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
153 def forget(self, f):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
154 return super(largefiles_dirstate, self).forget(unixpath(f))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
155
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
156 def openlfdirstate(ui, repo):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
157 '''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
158 Return a dirstate object that tracks big files: i.e. its root is the
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
159 repo root, but it is saved in .hg/largefiles/dirstate.
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
160 '''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
161 admin = repo.join(longname)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
162 try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
163 # Mercurial >= 1.9
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
164 opener = scmutil.opener(admin)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
165 except ImportError:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
166 # Mercurial <= 1.8
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
167 opener = util.opener(admin)
15169
aa262fff87ac largefile: fix up hasattr usage
Matt Mackall <mpm@selenic.com>
parents: 15168
diff changeset
168 if util.safehasattr(repo.dirstate, '_validate'):
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
169 lfdirstate = largefiles_dirstate(opener, ui, repo.root,
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
170 repo.dirstate._validate)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
171 else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
172 lfdirstate = largefiles_dirstate(opener, ui, repo.root)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
173
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
174 # If the largefiles dirstate does not exist, populate and create it. This
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
175 # ensures that we create it on the first meaningful largefiles operation in
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
176 # a new clone. It also gives us an easy way to forcibly rebuild largefiles
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
177 # state:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
178 # rm .hg/largefiles/dirstate && hg status
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
179 # Or even, if things are really messed up:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
180 # rm -rf .hg/largefiles && hg status
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
181 if not os.path.exists(os.path.join(admin, 'dirstate')):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
182 util.makedirs(admin)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
183 matcher = getstandinmatcher(repo)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
184 for standin in dirstate_walk(repo.dirstate, matcher):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
185 lfile = splitstandin(standin)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
186 hash = readstandin(repo, lfile)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
187 lfdirstate.normallookup(lfile)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
188 try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
189 if hash == hashfile(lfile):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
190 lfdirstate.normal(lfile)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
191 except IOError, err:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
192 if err.errno != errno.ENOENT:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
193 raise
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
194
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
195 lfdirstate.write()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
196
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
197 return lfdirstate
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
198
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
199 def lfdirstate_status(lfdirstate, repo, rev):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
200 wlock = repo.wlock()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
201 try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
202 match = match_.always(repo.root, repo.getcwd())
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
203 s = lfdirstate.status(match, [], False, False, False)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
204 unsure, modified, added, removed, missing, unknown, ignored, clean = s
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
205 for lfile in unsure:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
206 if repo[rev][standin(lfile)].data().strip() != \
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
207 hashfile(repo.wjoin(lfile)):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
208 modified.append(lfile)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
209 else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
210 clean.append(lfile)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
211 lfdirstate.normal(lfile)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
212 lfdirstate.write()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
213 finally:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
214 wlock.release()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
215 return (modified, added, removed, missing, unknown, ignored, clean)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
216
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
217 def listlfiles(repo, rev=None, matcher=None):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
218 '''list largefiles in the working copy or specified changeset'''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
219
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
220 if matcher is None:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
221 matcher = getstandinmatcher(repo)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
222
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
223 # ignore unknown files in working directory
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
224 return [splitstandin(f) for f in repo[rev].walk(matcher) \
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
225 if rev is not None or repo.dirstate[f] != '?']
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
226
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
227 def incache(repo, hash):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
228 return os.path.exists(cachepath(repo, hash))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
229
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
230 def createdir(dir):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
231 if not os.path.exists(dir):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
232 os.makedirs(dir)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
233
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
234 def cachepath(repo, hash):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
235 return repo.join(os.path.join(longname, hash))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
236
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
237 def copyfromcache(repo, hash, filename):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
238 '''copyfromcache copies the specified largefile from the repo or system
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
239 cache to the specified location in the repository. It will not throw an
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
240 exception on failure, as it is meant to be called only after ensuring that
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
241 the needed largefile exists in the cache.'''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
242 path = findfile(repo, hash)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
243 if path is None:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
244 return False
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
245 util.makedirs(os.path.dirname(repo.wjoin(filename)))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
246 shutil.copy(path, repo.wjoin(filename))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
247 return True
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
248
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
249 def copytocache(repo, rev, file, uploaded=False):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
250 hash = readstandin(repo, file)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
251 if incache(repo, hash):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
252 return
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
253 copytocacheabsolute(repo, repo.wjoin(file), hash)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
254
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
255 def copytocacheabsolute(repo, file, hash):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
256 createdir(os.path.dirname(cachepath(repo, hash)))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
257 if insystemcache(repo.ui, hash):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
258 link(systemcachepath(repo.ui, hash), cachepath(repo, hash))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
259 else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
260 shutil.copyfile(file, cachepath(repo, hash))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
261 os.chmod(cachepath(repo, hash), os.stat(file).st_mode)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
262 linktosystemcache(repo, hash)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
263
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
264 def linktosystemcache(repo, hash):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
265 createdir(os.path.dirname(systemcachepath(repo.ui, hash)))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
266 link(cachepath(repo, hash), systemcachepath(repo.ui, hash))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
267
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
268 def getstandinmatcher(repo, pats=[], opts={}):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
269 '''Return a match object that applies pats to the standin directory'''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
270 standindir = repo.pathto(shortname)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
271 if pats:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
272 # patterns supplied: search standin directory relative to current dir
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
273 cwd = repo.getcwd()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
274 if os.path.isabs(cwd):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
275 # cwd is an absolute path for hg -R <reponame>
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
276 # work relative to the repository root in this case
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
277 cwd = ''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
278 pats = [os.path.join(standindir, cwd, pat) for pat in pats]
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
279 elif os.path.isdir(standindir):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
280 # no patterns: relative to repo root
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
281 pats = [standindir]
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
282 else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
283 # no patterns and no standin dir: return matcher that matches nothing
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
284 match = match_.match(repo.root, None, [], exact=True)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
285 match.matchfn = lambda f: False
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
286 return match
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
287 return getmatcher(repo, pats, opts, showbad=False)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
288
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
289 def getmatcher(repo, pats=[], opts={}, showbad=True):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
290 '''Wrapper around scmutil.match() that adds showbad: if false, neuter
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
291 the match object\'s bad() method so it does not print any warnings
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
292 about missing files or directories.'''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
293 try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
294 # Mercurial >= 1.9
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
295 match = scmutil.match(repo[None], pats, opts)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
296 except ImportError:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
297 # Mercurial <= 1.8
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
298 match = cmdutil.match(repo, pats, opts)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
299
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
300 if not showbad:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
301 match.bad = lambda f, msg: None
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
302 return match
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
303
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
304 def composestandinmatcher(repo, rmatcher):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
305 '''Return a matcher that accepts standins corresponding to the files
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
306 accepted by rmatcher. Pass the list of files in the matcher as the
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
307 paths specified by the user.'''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
308 smatcher = getstandinmatcher(repo, rmatcher.files())
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
309 isstandin = smatcher.matchfn
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
310 def composed_matchfn(f):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
311 return isstandin(f) and rmatcher.matchfn(splitstandin(f))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
312 smatcher.matchfn = composed_matchfn
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
313
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
314 return smatcher
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
315
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
316 def standin(filename):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
317 '''Return the repo-relative path to the standin for the specified big
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
318 file.'''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
319 # Notes:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
320 # 1) Most callers want an absolute path, but _create_standin() needs
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
321 # it repo-relative so lfadd() can pass it to repo_add(). So leave
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
322 # it up to the caller to use repo.wjoin() to get an absolute path.
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
323 # 2) Join with '/' because that's what dirstate always uses, even on
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
324 # Windows. Change existing separator to '/' first in case we are
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
325 # passed filenames from an external source (like the command line).
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
326 return shortname + '/' + filename.replace(os.sep, '/')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
327
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
328 def isstandin(filename):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
329 '''Return true if filename is a big file standin. filename must
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
330 be in Mercurial\'s internal form (slash-separated).'''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
331 return filename.startswith(shortname + '/')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
332
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
333 def splitstandin(filename):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
334 # Split on / because that's what dirstate always uses, even on Windows.
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
335 # Change local separator to / first just in case we are passed filenames
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
336 # from an external source (like the command line).
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
337 bits = filename.replace(os.sep, '/').split('/', 1)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
338 if len(bits) == 2 and bits[0] == shortname:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
339 return bits[1]
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
340 else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
341 return None
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
342
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
343 def updatestandin(repo, standin):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
344 file = repo.wjoin(splitstandin(standin))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
345 if os.path.exists(file):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
346 hash = hashfile(file)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
347 executable = getexecutable(file)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
348 writestandin(repo, standin, hash, executable)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
349
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
350 def readstandin(repo, filename, node=None):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
351 '''read hex hash from standin for filename at given node, or working
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
352 directory if no node is given'''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
353 return repo[node][standin(filename)].data().strip()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
354
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
355 def writestandin(repo, standin, hash, executable):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
356 '''write hhash to <repo.root>/<standin>'''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
357 writehash(hash, repo.wjoin(standin), executable)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
358
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
359 def copyandhash(instream, outfile):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
360 '''Read bytes from instream (iterable) and write them to outfile,
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
361 computing the SHA-1 hash of the data along the way. Close outfile
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
362 when done and return the binary hash.'''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
363 hasher = util.sha1('')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
364 for data in instream:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
365 hasher.update(data)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
366 outfile.write(data)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
367
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
368 # Blecch: closing a file that somebody else opened is rude and
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
369 # wrong. But it's so darn convenient and practical! After all,
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
370 # outfile was opened just to copy and hash.
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
371 outfile.close()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
372
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
373 return hasher.digest()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
374
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
375 def hashrepofile(repo, file):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
376 return hashfile(repo.wjoin(file))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
377
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
378 def hashfile(file):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
379 if not os.path.exists(file):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
380 return ''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
381 hasher = util.sha1('')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
382 fd = open(file, 'rb')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
383 for data in blockstream(fd):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
384 hasher.update(data)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
385 fd.close()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
386 return hasher.hexdigest()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
387
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
388 class limitreader(object):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
389 def __init__(self, f, limit):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
390 self.f = f
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
391 self.limit = limit
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
392
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
393 def read(self, length):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
394 if self.limit == 0:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
395 return ''
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
396 length = length > self.limit and self.limit or length
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
397 self.limit -= length
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
398 return self.f.read(length)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
399
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
400 def close(self):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
401 pass
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
402
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
403 def blockstream(infile, blocksize=128 * 1024):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
404 """Generator that yields blocks of data from infile and closes infile."""
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
405 while True:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
406 data = infile.read(blocksize)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
407 if not data:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
408 break
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
409 yield data
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
410 # Same blecch as above.
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
411 infile.close()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
412
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
413 def readhash(filename):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
414 rfile = open(filename, 'rb')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
415 hash = rfile.read(40)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
416 rfile.close()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
417 if len(hash) < 40:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
418 raise util.Abort(_('bad hash in \'%s\' (only %d bytes long)')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
419 % (filename, len(hash)))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
420 return hash
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
421
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
422 def writehash(hash, filename, executable):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
423 util.makedirs(os.path.dirname(filename))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
424 if os.path.exists(filename):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
425 os.unlink(filename)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
426 wfile = open(filename, 'wb')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
427
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
428 try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
429 wfile.write(hash)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
430 wfile.write('\n')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
431 finally:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
432 wfile.close()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
433 if os.path.exists(filename):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
434 os.chmod(filename, getmode(executable))
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
435
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
436 def getexecutable(filename):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
437 mode = os.stat(filename).st_mode
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
438 return (mode & stat.S_IXUSR) and (mode & stat.S_IXGRP) and (mode & \
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
439 stat.S_IXOTH)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
440
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
441 def getmode(executable):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
442 if executable:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
443 return 0755
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
444 else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
445 return 0644
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
446
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
447 def urljoin(first, second, *arg):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
448 def join(left, right):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
449 if not left.endswith('/'):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
450 left += '/'
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
451 if right.startswith('/'):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
452 right = right[1:]
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
453 return left + right
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
454
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
455 url = join(first, second)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
456 for a in arg:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
457 url = join(url, a)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
458 return url
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
459
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
460 def hexsha1(data):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
461 """hexsha1 returns the hex-encoded sha1 sum of the data in the file-like
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
462 object data"""
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
463 h = hashlib.sha1()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
464 for chunk in util.filechunkiter(data):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
465 h.update(chunk)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
466 return h.hexdigest()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
467
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
468 def httpsendfile(ui, filename):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
469 try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
470 # Mercurial >= 1.9
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
471 return httpconnection.httpsendfile(ui, filename, 'rb')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
472 except ImportError:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
473 if 'ui' in inspect.getargspec(url_.httpsendfile.__init__)[0]:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
474 # Mercurial == 1.8
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
475 return url_.httpsendfile(ui, filename, 'rb')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
476 else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
477 # Mercurial <= 1.7
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
478 return url_.httpsendfile(filename, 'rb')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
479
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
480 # Convert a path to a unix style path. This is used to give a
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
481 # canonical path to the lfdirstate.
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
482 def unixpath(path):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
483 return os.path.normpath(path).replace(os.sep, '/')
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
484
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
485 def islfilesrepo(repo):
15170
c1a4a3220711 largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents: 15169
diff changeset
486 return ('largefiles' in repo.requirements and
c1a4a3220711 largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents: 15169
diff changeset
487 any_(shortname + '/' in f[0] for f in repo.store.datafiles())
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
488
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
489 def any_(gen):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
490 for x in gen:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
491 if x:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
492 return True
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
493 return False
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
494
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
495 class storeprotonotcapable(BaseException):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
496 def __init__(self, storetypes):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
497 self.storetypes = storetypes