##// END OF EJS Templates
git: make dirstate status() respect matcher...
Augie Fackler -
r45990:0c6b2cc9 default
parent child Browse files
Show More
@@ -1,320 +1,322 b''
1 1 from __future__ import absolute_import
2 2
3 3 import contextlib
4 4 import errno
5 5 import os
6 6
7 7 from mercurial import (
8 8 error,
9 9 extensions,
10 10 match as matchmod,
11 11 node as nodemod,
12 12 pycompat,
13 13 scmutil,
14 14 util,
15 15 )
16 16 from mercurial.interfaces import (
17 17 dirstate as intdirstate,
18 18 util as interfaceutil,
19 19 )
20 20
21 21 from . import gitutil
22 22
23 23 pygit2 = gitutil.get_pygit2()
24 24
25 25
26 26 def readpatternfile(orig, filepath, warn, sourceinfo=False):
27 27 if not (b'info/exclude' in filepath or filepath.endswith(b'.gitignore')):
28 28 return orig(filepath, warn, sourceinfo=False)
29 29 result = []
30 30 warnings = []
31 31 with open(filepath, b'rb') as fp:
32 32 for l in fp:
33 33 l = l.strip()
34 34 if not l or l.startswith(b'#'):
35 35 continue
36 36 if l.startswith(b'!'):
37 37 warnings.append(b'unsupported ignore pattern %s' % l)
38 38 continue
39 39 if l.startswith(b'/'):
40 40 result.append(b'rootglob:' + l[1:])
41 41 else:
42 42 result.append(b'relglob:' + l)
43 43 return result, warnings
44 44
45 45
46 46 extensions.wrapfunction(matchmod, b'readpatternfile', readpatternfile)
47 47
48 48
49 49 _STATUS_MAP = {}
50 50 if pygit2:
51 51 _STATUS_MAP = {
52 52 pygit2.GIT_STATUS_CONFLICTED: b'm',
53 53 pygit2.GIT_STATUS_CURRENT: b'n',
54 54 pygit2.GIT_STATUS_IGNORED: b'?',
55 55 pygit2.GIT_STATUS_INDEX_DELETED: b'r',
56 56 pygit2.GIT_STATUS_INDEX_MODIFIED: b'n',
57 57 pygit2.GIT_STATUS_INDEX_NEW: b'a',
58 58 pygit2.GIT_STATUS_INDEX_RENAMED: b'a',
59 59 pygit2.GIT_STATUS_INDEX_TYPECHANGE: b'n',
60 60 pygit2.GIT_STATUS_WT_DELETED: b'r',
61 61 pygit2.GIT_STATUS_WT_MODIFIED: b'n',
62 62 pygit2.GIT_STATUS_WT_NEW: b'?',
63 63 pygit2.GIT_STATUS_WT_RENAMED: b'a',
64 64 pygit2.GIT_STATUS_WT_TYPECHANGE: b'n',
65 65 pygit2.GIT_STATUS_WT_UNREADABLE: b'?',
66 66 pygit2.GIT_STATUS_INDEX_MODIFIED | pygit2.GIT_STATUS_WT_MODIFIED: 'm',
67 67 }
68 68
69 69
70 70 @interfaceutil.implementer(intdirstate.idirstate)
71 71 class gitdirstate(object):
72 72 def __init__(self, ui, root, gitrepo):
73 73 self._ui = ui
74 74 self._root = os.path.dirname(root)
75 75 self.git = gitrepo
76 76 self._plchangecallbacks = {}
77 77
78 78 def p1(self):
79 79 try:
80 80 return self.git.head.peel().id.raw
81 81 except pygit2.GitError:
82 82 # Typically happens when peeling HEAD fails, as in an
83 83 # empty repository.
84 84 return nodemod.nullid
85 85
86 86 def p2(self):
87 87 # TODO: MERGE_HEAD? something like that, right?
88 88 return nodemod.nullid
89 89
90 90 def setparents(self, p1, p2=nodemod.nullid):
91 91 assert p2 == nodemod.nullid, b'TODO merging support'
92 92 self.git.head.set_target(gitutil.togitnode(p1))
93 93
94 94 @util.propertycache
95 95 def identity(self):
96 96 return util.filestat.frompath(
97 97 os.path.join(self._root, b'.git', b'index')
98 98 )
99 99
100 100 def branch(self):
101 101 return b'default'
102 102
103 103 def parents(self):
104 104 # TODO how on earth do we find p2 if a merge is in flight?
105 105 return self.p1(), nodemod.nullid
106 106
107 107 def __iter__(self):
108 108 return (pycompat.fsencode(f.path) for f in self.git.index)
109 109
110 110 def items(self):
111 111 for ie in self.git.index:
112 112 yield ie.path, None # value should be a dirstatetuple
113 113
114 114 # py2,3 compat forward
115 115 iteritems = items
116 116
117 117 def __getitem__(self, filename):
118 118 try:
119 119 gs = self.git.status_file(filename)
120 120 except KeyError:
121 121 return b'?'
122 122 return _STATUS_MAP[gs]
123 123
124 124 def __contains__(self, filename):
125 125 try:
126 126 gs = self.git.status_file(filename)
127 127 return _STATUS_MAP[gs] != b'?'
128 128 except KeyError:
129 129 return False
130 130
131 131 def status(self, match, subrepos, ignored, clean, unknown):
132 132 # TODO handling of clean files - can we get that from git.status()?
133 133 modified, added, removed, deleted, unknown, ignored, clean = (
134 134 [],
135 135 [],
136 136 [],
137 137 [],
138 138 [],
139 139 [],
140 140 [],
141 141 )
142 142 gstatus = self.git.status()
143 143 for path, status in gstatus.items():
144 144 path = pycompat.fsencode(path)
145 if not match(path):
146 continue
145 147 if status == pygit2.GIT_STATUS_IGNORED:
146 148 if path.endswith(b'/'):
147 149 continue
148 150 ignored.append(path)
149 151 elif status in (
150 152 pygit2.GIT_STATUS_WT_MODIFIED,
151 153 pygit2.GIT_STATUS_INDEX_MODIFIED,
152 154 pygit2.GIT_STATUS_WT_MODIFIED
153 155 | pygit2.GIT_STATUS_INDEX_MODIFIED,
154 156 ):
155 157 modified.append(path)
156 158 elif status == pygit2.GIT_STATUS_INDEX_NEW:
157 159 added.append(path)
158 160 elif status == pygit2.GIT_STATUS_WT_NEW:
159 161 unknown.append(path)
160 162 elif status == pygit2.GIT_STATUS_WT_DELETED:
161 163 deleted.append(path)
162 164 elif status == pygit2.GIT_STATUS_INDEX_DELETED:
163 165 removed.append(path)
164 166 else:
165 167 raise error.Abort(
166 168 b'unhandled case: status for %r is %r' % (path, status)
167 169 )
168 170
169 171 # TODO are we really always sure of status here?
170 172 return (
171 173 False,
172 174 scmutil.status(
173 175 modified, added, removed, deleted, unknown, ignored, clean
174 176 ),
175 177 )
176 178
177 179 def flagfunc(self, buildfallback):
178 180 # TODO we can do better
179 181 return buildfallback()
180 182
181 183 def getcwd(self):
182 184 # TODO is this a good way to do this?
183 185 return os.path.dirname(
184 186 os.path.dirname(pycompat.fsencode(self.git.path))
185 187 )
186 188
187 189 def normalize(self, path):
188 190 normed = util.normcase(path)
189 191 assert normed == path, b"TODO handling of case folding: %s != %s" % (
190 192 normed,
191 193 path,
192 194 )
193 195 return path
194 196
195 197 @property
196 198 def _checklink(self):
197 199 return util.checklink(os.path.dirname(pycompat.fsencode(self.git.path)))
198 200
199 201 def copies(self):
200 202 # TODO support copies?
201 203 return {}
202 204
203 205 # # TODO what the heck is this
204 206 _filecache = set()
205 207
206 208 def pendingparentchange(self):
207 209 # TODO: we need to implement the context manager bits and
208 210 # correctly stage/revert index edits.
209 211 return False
210 212
211 213 def write(self, tr):
212 214 # TODO: call parent change callbacks
213 215
214 216 if tr:
215 217
216 218 def writeinner(category):
217 219 self.git.index.write()
218 220
219 221 tr.addpending(b'gitdirstate', writeinner)
220 222 else:
221 223 self.git.index.write()
222 224
223 225 def pathto(self, f, cwd=None):
224 226 if cwd is None:
225 227 cwd = self.getcwd()
226 228 # TODO core dirstate does something about slashes here
227 229 assert isinstance(f, bytes)
228 230 r = util.pathto(self._root, cwd, f)
229 231 return r
230 232
231 233 def matches(self, match):
232 234 for x in self.git.index:
233 235 p = pycompat.fsencode(x.path)
234 236 if match(p):
235 237 yield p
236 238
237 239 def normal(self, f, parentfiledata=None):
238 240 """Mark a file normal and clean."""
239 241 # TODO: for now we just let libgit2 re-stat the file. We can
240 242 # clearly do better.
241 243
242 244 def normallookup(self, f):
243 245 """Mark a file normal, but possibly dirty."""
244 246 # TODO: for now we just let libgit2 re-stat the file. We can
245 247 # clearly do better.
246 248
247 249 def walk(self, match, subrepos, unknown, ignored, full=True):
248 250 # TODO: we need to use .status() and not iterate the index,
249 251 # because the index doesn't force a re-walk and so `hg add` of
250 252 # a new file without an intervening call to status will
251 253 # silently do nothing.
252 254 r = {}
253 255 cwd = self.getcwd()
254 256 for path, status in self.git.status().items():
255 257 if path.startswith('.hg/'):
256 258 continue
257 259 path = pycompat.fsencode(path)
258 260 if not match(path):
259 261 continue
260 262 # TODO construct the stat info from the status object?
261 263 try:
262 264 s = os.stat(os.path.join(cwd, path))
263 265 except OSError as e:
264 266 if e.errno != errno.ENOENT:
265 267 raise
266 268 continue
267 269 r[path] = s
268 270 return r
269 271
270 272 def savebackup(self, tr, backupname):
271 273 # TODO: figure out a strategy for saving index backups.
272 274 pass
273 275
274 276 def restorebackup(self, tr, backupname):
275 277 # TODO: figure out a strategy for saving index backups.
276 278 pass
277 279
278 280 def add(self, f):
279 281 index = self.git.index
280 282 index.read()
281 283 index.add(pycompat.fsdecode(f))
282 284 index.write()
283 285
284 286 def drop(self, f):
285 287 index = self.git.index
286 288 index.read()
287 289 index.remove(pycompat.fsdecode(f))
288 290 index.write()
289 291
290 292 def remove(self, f):
291 293 index = self.git.index
292 294 index.read()
293 295 index.remove(pycompat.fsdecode(f))
294 296 index.write()
295 297
296 298 def copied(self, path):
297 299 # TODO: track copies?
298 300 return None
299 301
300 302 def prefetch_parents(self):
301 303 # TODO
302 304 pass
303 305
304 306 @contextlib.contextmanager
305 307 def parentchange(self):
306 308 # TODO: track this maybe?
307 309 yield
308 310
309 311 def addparentchangecallback(self, category, callback):
310 312 # TODO: should this be added to the dirstate interface?
311 313 self._plchangecallbacks[category] = callback
312 314
313 315 def clearbackup(self, tr, backupname):
314 316 # TODO
315 317 pass
316 318
317 319 def setbranch(self, branch):
318 320 raise error.Abort(
319 321 b'git repos do not support branches. try using bookmarks'
320 322 )
General Comments 0
You need to be logged in to leave comments. Login now