2
0

index.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. # index.py -- File parser/writer for the git index file
  2. # Copyright (C) 2008-2013 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """Parser for the git index file format."""
  21. import collections
  22. import errno
  23. import os
  24. import stat
  25. import struct
  26. import sys
  27. from dulwich.file import GitFile
  28. from dulwich.objects import (
  29. Blob,
  30. S_IFGITLINK,
  31. S_ISGITLINK,
  32. Tree,
  33. hex_to_sha,
  34. sha_to_hex,
  35. )
  36. from dulwich.pack import (
  37. SHA1Reader,
  38. SHA1Writer,
  39. )
  40. IndexEntry = collections.namedtuple(
  41. 'IndexEntry', [
  42. 'ctime', 'mtime', 'dev', 'ino', 'mode', 'uid', 'gid', 'size', 'sha',
  43. 'flags'])
  44. FLAG_STAGEMASK = 0x3000
  45. FLAG_VALID = 0x8000
  46. FLAG_EXTENDED = 0x4000
  47. def pathsplit(path):
  48. """Split a /-delimited path into a directory part and a basename.
  49. :param path: The path to split.
  50. :return: Tuple with directory name and basename
  51. """
  52. try:
  53. (dirname, basename) = path.rsplit(b"/", 1)
  54. except ValueError:
  55. return (b"", path)
  56. else:
  57. return (dirname, basename)
  58. def pathjoin(*args):
  59. """Join a /-delimited path.
  60. """
  61. return b"/".join([p for p in args if p])
  62. def read_cache_time(f):
  63. """Read a cache time.
  64. :param f: File-like object to read from
  65. :return: Tuple with seconds and nanoseconds
  66. """
  67. return struct.unpack(">LL", f.read(8))
  68. def write_cache_time(f, t):
  69. """Write a cache time.
  70. :param f: File-like object to write to
  71. :param t: Time to write (as int, float or tuple with secs and nsecs)
  72. """
  73. if isinstance(t, int):
  74. t = (t, 0)
  75. elif isinstance(t, float):
  76. (secs, nsecs) = divmod(t, 1.0)
  77. t = (int(secs), int(nsecs * 1000000000))
  78. elif not isinstance(t, tuple):
  79. raise TypeError(t)
  80. f.write(struct.pack(">LL", *t))
  81. def read_cache_entry(f):
  82. """Read an entry from a cache file.
  83. :param f: File-like object to read from
  84. :return: tuple with: device, inode, mode, uid, gid, size, sha, flags
  85. """
  86. beginoffset = f.tell()
  87. ctime = read_cache_time(f)
  88. mtime = read_cache_time(f)
  89. (dev, ino, mode, uid, gid, size, sha, flags, ) = \
  90. struct.unpack(">LLLLLL20sH", f.read(20 + 4 * 6 + 2))
  91. name = f.read((flags & 0x0fff))
  92. # Padding:
  93. real_size = ((f.tell() - beginoffset + 8) & ~7)
  94. f.read((beginoffset + real_size) - f.tell())
  95. return (name, ctime, mtime, dev, ino, mode, uid, gid, size,
  96. sha_to_hex(sha), flags & ~0x0fff)
  97. def write_cache_entry(f, entry):
  98. """Write an index entry to a file.
  99. :param f: File object
  100. :param entry: Entry to write, tuple with:
  101. (name, ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags)
  102. """
  103. beginoffset = f.tell()
  104. (name, ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = entry
  105. write_cache_time(f, ctime)
  106. write_cache_time(f, mtime)
  107. flags = len(name) | (flags & ~0x0fff)
  108. f.write(struct.pack(
  109. b'>LLLLLL20sH', dev & 0xFFFFFFFF, ino & 0xFFFFFFFF,
  110. mode, uid, gid, size, hex_to_sha(sha), flags))
  111. f.write(name)
  112. real_size = ((f.tell() - beginoffset + 8) & ~7)
  113. f.write(b'\0' * ((beginoffset + real_size) - f.tell()))
  114. def read_index(f):
  115. """Read an index file, yielding the individual entries."""
  116. header = f.read(4)
  117. if header != b'DIRC':
  118. raise AssertionError("Invalid index file header: %r" % header)
  119. (version, num_entries) = struct.unpack(b'>LL', f.read(4 * 2))
  120. assert version in (1, 2)
  121. for i in range(num_entries):
  122. yield read_cache_entry(f)
  123. def read_index_dict(f):
  124. """Read an index file and return it as a dictionary.
  125. :param f: File object to read from
  126. """
  127. ret = {}
  128. for x in read_index(f):
  129. ret[x[0]] = IndexEntry(*x[1:])
  130. return ret
  131. def write_index(f, entries):
  132. """Write an index file.
  133. :param f: File-like object to write to
  134. :param entries: Iterable over the entries to write
  135. """
  136. f.write(b'DIRC')
  137. f.write(struct.pack(b'>LL', 2, len(entries)))
  138. for x in entries:
  139. write_cache_entry(f, x)
  140. def write_index_dict(f, entries):
  141. """Write an index file based on the contents of a dictionary.
  142. """
  143. entries_list = []
  144. for name in sorted(entries):
  145. entries_list.append((name,) + tuple(entries[name]))
  146. write_index(f, entries_list)
  147. def cleanup_mode(mode):
  148. """Cleanup a mode value.
  149. This will return a mode that can be stored in a tree object.
  150. :param mode: Mode to clean up.
  151. """
  152. if stat.S_ISLNK(mode):
  153. return stat.S_IFLNK
  154. elif stat.S_ISDIR(mode):
  155. return stat.S_IFDIR
  156. elif S_ISGITLINK(mode):
  157. return S_IFGITLINK
  158. ret = stat.S_IFREG | 0o644
  159. ret |= (mode & 0o111)
  160. return ret
  161. class Index(object):
  162. """A Git Index file."""
  163. def __init__(self, filename):
  164. """Open an index file.
  165. :param filename: Path to the index file
  166. """
  167. self._filename = filename
  168. self.clear()
  169. self.read()
  170. @property
  171. def path(self):
  172. return self._filename
  173. def __repr__(self):
  174. return "%s(%r)" % (self.__class__.__name__, self._filename)
  175. def write(self):
  176. """Write current contents of index to disk."""
  177. f = GitFile(self._filename, 'wb')
  178. try:
  179. f = SHA1Writer(f)
  180. write_index_dict(f, self._byname)
  181. finally:
  182. f.close()
  183. def read(self):
  184. """Read current contents of index from disk."""
  185. if not os.path.exists(self._filename):
  186. return
  187. f = GitFile(self._filename, 'rb')
  188. try:
  189. f = SHA1Reader(f)
  190. for x in read_index(f):
  191. self[x[0]] = IndexEntry(*x[1:])
  192. # FIXME: Additional data?
  193. f.read(os.path.getsize(self._filename)-f.tell()-20)
  194. f.check_sha()
  195. finally:
  196. f.close()
  197. def __len__(self):
  198. """Number of entries in this index file."""
  199. return len(self._byname)
  200. def __getitem__(self, name):
  201. """Retrieve entry by relative path.
  202. :return: tuple with (ctime, mtime, dev, ino, mode, uid, gid, size, sha,
  203. flags)
  204. """
  205. return self._byname[name]
  206. def __iter__(self):
  207. """Iterate over the paths in this index."""
  208. return iter(self._byname)
  209. def get_sha1(self, path):
  210. """Return the (git object) SHA1 for the object at a path."""
  211. return self[path].sha
  212. def get_mode(self, path):
  213. """Return the POSIX file mode for the object at a path."""
  214. return self[path].mode
  215. def iterobjects(self):
  216. """Iterate over path, sha, mode tuples for use with commit_tree."""
  217. for path in self:
  218. entry = self[path]
  219. yield path, entry.sha, cleanup_mode(entry.mode)
  220. def iterblobs(self):
  221. import warnings
  222. warnings.warn('Use iterobjects() instead.', PendingDeprecationWarning)
  223. return self.iterobjects()
  224. def clear(self):
  225. """Remove all contents from this index."""
  226. self._byname = {}
  227. def __setitem__(self, name, x):
  228. assert isinstance(name, bytes)
  229. assert len(x) == 10
  230. # Remove the old entry if any
  231. self._byname[name] = IndexEntry(*x)
  232. def __delitem__(self, name):
  233. assert isinstance(name, bytes)
  234. del self._byname[name]
  235. def iteritems(self):
  236. return self._byname.items()
  237. def items(self):
  238. return self._byname.items()
  239. def update(self, entries):
  240. for name, value in entries.items():
  241. self[name] = value
  242. def changes_from_tree(self, object_store, tree, want_unchanged=False):
  243. """Find the differences between the contents of this index and a tree.
  244. :param object_store: Object store to use for retrieving tree contents
  245. :param tree: SHA1 of the root tree
  246. :param want_unchanged: Whether unchanged files should be reported
  247. :return: Iterator over tuples with (oldpath, newpath), (oldmode,
  248. newmode), (oldsha, newsha)
  249. """
  250. def lookup_entry(path):
  251. entry = self[path]
  252. return entry.sha, entry.mode
  253. for (name, mode, sha) in changes_from_tree(
  254. self._byname.keys(), lookup_entry, object_store, tree,
  255. want_unchanged=want_unchanged):
  256. yield (name, mode, sha)
  257. def commit(self, object_store):
  258. """Create a new tree from an index.
  259. :param object_store: Object store to save the tree in
  260. :return: Root tree SHA
  261. """
  262. return commit_tree(object_store, self.iterobjects())
  263. def commit_tree(object_store, blobs):
  264. """Commit a new tree.
  265. :param object_store: Object store to add trees to
  266. :param blobs: Iterable over blob path, sha, mode entries
  267. :return: SHA1 of the created tree.
  268. """
  269. trees = {b'': {}}
  270. def add_tree(path):
  271. if path in trees:
  272. return trees[path]
  273. dirname, basename = pathsplit(path)
  274. t = add_tree(dirname)
  275. assert isinstance(basename, bytes)
  276. newtree = {}
  277. t[basename] = newtree
  278. trees[path] = newtree
  279. return newtree
  280. for path, sha, mode in blobs:
  281. tree_path, basename = pathsplit(path)
  282. tree = add_tree(tree_path)
  283. tree[basename] = (mode, sha)
  284. def build_tree(path):
  285. tree = Tree()
  286. for basename, entry in trees[path].items():
  287. if isinstance(entry, dict):
  288. mode = stat.S_IFDIR
  289. sha = build_tree(pathjoin(path, basename))
  290. else:
  291. (mode, sha) = entry
  292. tree.add(basename, mode, sha)
  293. object_store.add_object(tree)
  294. return tree.id
  295. return build_tree(b'')
  296. def commit_index(object_store, index):
  297. """Create a new tree from an index.
  298. :param object_store: Object store to save the tree in
  299. :param index: Index file
  300. :note: This function is deprecated, use index.commit() instead.
  301. :return: Root tree sha.
  302. """
  303. return commit_tree(object_store, index.iterobjects())
  304. def changes_from_tree(names, lookup_entry, object_store, tree,
  305. want_unchanged=False):
  306. """Find the differences between the contents of a tree and
  307. a working copy.
  308. :param names: Iterable of names in the working copy
  309. :param lookup_entry: Function to lookup an entry in the working copy
  310. :param object_store: Object store to use for retrieving tree contents
  311. :param tree: SHA1 of the root tree, or None for an empty tree
  312. :param want_unchanged: Whether unchanged files should be reported
  313. :return: Iterator over tuples with (oldpath, newpath), (oldmode, newmode),
  314. (oldsha, newsha)
  315. """
  316. # TODO(jelmer): Support a include_trees option
  317. other_names = set(names)
  318. if tree is not None:
  319. for (name, mode, sha) in object_store.iter_tree_contents(tree):
  320. try:
  321. (other_sha, other_mode) = lookup_entry(name)
  322. except KeyError:
  323. # Was removed
  324. yield ((name, None), (mode, None), (sha, None))
  325. else:
  326. other_names.remove(name)
  327. if (want_unchanged or other_sha != sha or other_mode != mode):
  328. yield ((name, name), (mode, other_mode), (sha, other_sha))
  329. # Mention added files
  330. for name in other_names:
  331. try:
  332. (other_sha, other_mode) = lookup_entry(name)
  333. except KeyError:
  334. pass
  335. else:
  336. yield ((None, name), (None, other_mode), (None, other_sha))
  337. def index_entry_from_stat(stat_val, hex_sha, flags, mode=None):
  338. """Create a new index entry from a stat value.
  339. :param stat_val: POSIX stat_result instance
  340. :param hex_sha: Hex sha of the object
  341. :param flags: Index flags
  342. """
  343. if mode is None:
  344. mode = cleanup_mode(stat_val.st_mode)
  345. return IndexEntry(
  346. stat_val.st_ctime, stat_val.st_mtime, stat_val.st_dev,
  347. stat_val.st_ino, mode, stat_val.st_uid,
  348. stat_val.st_gid, stat_val.st_size, hex_sha, flags)
  349. def build_file_from_blob(blob, mode, target_path, honor_filemode=True):
  350. """Build a file or symlink on disk based on a Git object.
  351. :param obj: The git object
  352. :param mode: File mode
  353. :param target_path: Path to write to
  354. :param honor_filemode: An optional flag to honor core.filemode setting in
  355. config file, default is core.filemode=True, change executable bit
  356. :return: stat object for the file
  357. """
  358. try:
  359. oldstat = os.lstat(target_path)
  360. except OSError as e:
  361. if e.errno == errno.ENOENT:
  362. oldstat = None
  363. else:
  364. raise
  365. contents = blob.as_raw_string()
  366. if stat.S_ISLNK(mode):
  367. # FIXME: This will fail on Windows. What should we do instead?
  368. if oldstat:
  369. os.unlink(target_path)
  370. if sys.platform == 'win32' and sys.version_info[0] == 3:
  371. # os.readlink on Python3 on Windows requires a unicode string.
  372. # TODO(jelmer): Don't assume tree_encoding == fs_encoding
  373. tree_encoding = sys.getfilesystemencoding()
  374. contents = contents.decode(tree_encoding)
  375. target_path = target_path.decode(tree_encoding)
  376. os.symlink(contents, target_path)
  377. else:
  378. if oldstat is not None and oldstat.st_size == len(contents):
  379. with open(target_path, 'rb') as f:
  380. if f.read() == contents:
  381. return oldstat
  382. with open(target_path, 'wb') as f:
  383. # Write out file
  384. f.write(contents)
  385. if honor_filemode:
  386. os.chmod(target_path, mode)
  387. return os.lstat(target_path)
  388. INVALID_DOTNAMES = (b".git", b".", b"..", b"")
  389. def validate_path_element_default(element):
  390. return element.lower() not in INVALID_DOTNAMES
  391. def validate_path_element_ntfs(element):
  392. stripped = element.rstrip(b". ").lower()
  393. if stripped in INVALID_DOTNAMES:
  394. return False
  395. if stripped == b"git~1":
  396. return False
  397. return True
  398. def validate_path(path, element_validator=validate_path_element_default):
  399. """Default path validator that just checks for .git/."""
  400. parts = path.split(b"/")
  401. for p in parts:
  402. if not element_validator(p):
  403. return False
  404. else:
  405. return True
  406. def build_index_from_tree(root_path, index_path, object_store, tree_id,
  407. honor_filemode=True,
  408. validate_path_element=validate_path_element_default):
  409. """Generate and materialize index from a tree
  410. :param tree_id: Tree to materialize
  411. :param root_path: Target dir for materialized index files
  412. :param index_path: Target path for generated index
  413. :param object_store: Non-empty object store holding tree contents
  414. :param honor_filemode: An optional flag to honor core.filemode setting in
  415. config file, default is core.filemode=True, change executable bit
  416. :param validate_path_element: Function to validate path elements to check
  417. out; default just refuses .git and .. directories.
  418. :note:: existing index is wiped and contents are not merged
  419. in a working dir. Suitable only for fresh clones.
  420. """
  421. index = Index(index_path)
  422. if not isinstance(root_path, bytes):
  423. root_path = root_path.encode(sys.getfilesystemencoding())
  424. for entry in object_store.iter_tree_contents(tree_id):
  425. if not validate_path(entry.path, validate_path_element):
  426. continue
  427. full_path = _tree_to_fs_path(root_path, entry.path)
  428. if not os.path.exists(os.path.dirname(full_path)):
  429. os.makedirs(os.path.dirname(full_path))
  430. # TODO(jelmer): Merge new index into working tree
  431. if S_ISGITLINK(entry.mode):
  432. if not os.path.isdir(full_path):
  433. os.mkdir(full_path)
  434. st = os.lstat(full_path)
  435. # TODO(jelmer): record and return submodule paths
  436. else:
  437. obj = object_store[entry.sha]
  438. st = build_file_from_blob(
  439. obj, entry.mode, full_path, honor_filemode=honor_filemode)
  440. # Add file to index
  441. if not honor_filemode or S_ISGITLINK(entry.mode):
  442. # we can not use tuple slicing to build a new tuple,
  443. # because on windows that will convert the times to
  444. # longs, which causes errors further along
  445. st_tuple = (entry.mode, st.st_ino, st.st_dev, st.st_nlink,
  446. st.st_uid, st.st_gid, st.st_size, st.st_atime,
  447. st.st_mtime, st.st_ctime)
  448. st = st.__class__(st_tuple)
  449. index[entry.path] = index_entry_from_stat(st, entry.sha, 0)
  450. index.write()
  451. def blob_from_path_and_stat(fs_path, st):
  452. """Create a blob from a path and a stat object.
  453. :param fs_path: Full file system path to file
  454. :param st: A stat object
  455. :return: A `Blob` object
  456. """
  457. assert isinstance(fs_path, bytes)
  458. blob = Blob()
  459. if not stat.S_ISLNK(st.st_mode):
  460. with open(fs_path, 'rb') as f:
  461. blob.data = f.read()
  462. else:
  463. if sys.platform == 'win32' and sys.version_info[0] == 3:
  464. # os.readlink on Python3 on Windows requires a unicode string.
  465. # TODO(jelmer): Don't assume tree_encoding == fs_encoding
  466. tree_encoding = sys.getfilesystemencoding()
  467. fs_path = fs_path.decode(tree_encoding)
  468. blob.data = os.readlink(fs_path).encode(tree_encoding)
  469. else:
  470. blob.data = os.readlink(fs_path)
  471. return blob
  472. def read_submodule_head(path):
  473. """Read the head commit of a submodule.
  474. :param path: path to the submodule
  475. :return: HEAD sha, None if not a valid head/repository
  476. """
  477. from dulwich.errors import NotGitRepository
  478. from dulwich.repo import Repo
  479. # Repo currently expects a "str", so decode if necessary.
  480. # TODO(jelmer): Perhaps move this into Repo() ?
  481. if not isinstance(path, str):
  482. path = path.decode(sys.getfilesystemencoding())
  483. try:
  484. repo = Repo(path)
  485. except NotGitRepository:
  486. return None
  487. try:
  488. return repo.head()
  489. except KeyError:
  490. return None
  491. def get_unstaged_changes(index, root_path):
  492. """Walk through an index and check for differences against working tree.
  493. :param index: index to check
  494. :param root_path: path in which to find files
  495. :return: iterator over paths with unstaged changes
  496. """
  497. # For each entry in the index check the sha1 & ensure not staged
  498. if not isinstance(root_path, bytes):
  499. root_path = root_path.encode(sys.getfilesystemencoding())
  500. for tree_path, entry in index.iteritems():
  501. full_path = _tree_to_fs_path(root_path, tree_path)
  502. try:
  503. blob = blob_from_path_and_stat(full_path, os.lstat(full_path))
  504. except OSError as e:
  505. if e.errno != errno.ENOENT:
  506. raise
  507. # The file was removed, so we assume that counts as
  508. # different from whatever file used to exist.
  509. yield tree_path
  510. except IOError as e:
  511. if e.errno != errno.EISDIR:
  512. raise
  513. # This is actually a directory
  514. if os.path.exists(os.path.join(tree_path, '.git')):
  515. # Submodule
  516. head = read_submodule_head(tree_path)
  517. if entry.sha != head:
  518. yield tree_path
  519. else:
  520. # The file was changed to a directory, so consider it removed.
  521. yield tree_path
  522. else:
  523. if blob.id != entry.sha:
  524. yield tree_path
  525. os_sep_bytes = os.sep.encode('ascii')
  526. def _tree_to_fs_path(root_path, tree_path):
  527. """Convert a git tree path to a file system path.
  528. :param root_path: Root filesystem path
  529. :param tree_path: Git tree path as bytes
  530. :return: File system path.
  531. """
  532. assert isinstance(tree_path, bytes)
  533. if os_sep_bytes != b'/':
  534. sep_corrected_path = tree_path.replace(b'/', os_sep_bytes)
  535. else:
  536. sep_corrected_path = tree_path
  537. return os.path.join(root_path, sep_corrected_path)
  538. def _fs_to_tree_path(fs_path, fs_encoding=None):
  539. """Convert a file system path to a git tree path.
  540. :param fs_path: File system path.
  541. :param fs_encoding: File system encoding
  542. :return: Git tree path as bytes
  543. """
  544. if fs_encoding is None:
  545. fs_encoding = sys.getfilesystemencoding()
  546. if not isinstance(fs_path, bytes):
  547. fs_path_bytes = fs_path.encode(fs_encoding)
  548. else:
  549. fs_path_bytes = fs_path
  550. if os_sep_bytes != b'/':
  551. tree_path = fs_path_bytes.replace(os_sep_bytes, b'/')
  552. else:
  553. tree_path = fs_path_bytes
  554. return tree_path
  555. def index_entry_from_path(path, object_store=None):
  556. """Create an index from a filesystem path.
  557. This returns an index value for files, symlinks
  558. and tree references. for directories and
  559. non-existant files it returns None
  560. :param path: Path to create an index entry for
  561. :param object_store: Optional object store to
  562. save new blobs in
  563. :return: An index entry
  564. """
  565. assert isinstance(path, bytes)
  566. try:
  567. st = os.lstat(path)
  568. blob = blob_from_path_and_stat(path, st)
  569. except EnvironmentError as e:
  570. if e.errno == errno.EISDIR:
  571. if os.path.exists(os.path.join(path, b'.git')):
  572. head = read_submodule_head(path)
  573. if head is None:
  574. return None
  575. return index_entry_from_stat(
  576. st, head, 0, mode=S_IFGITLINK)
  577. else:
  578. raise
  579. else:
  580. raise
  581. else:
  582. if object_store is not None:
  583. object_store.add_object(blob)
  584. return index_entry_from_stat(st, blob.id, 0)
  585. def iter_fresh_entries(paths, root_path, object_store=None):
  586. """Iterate over current versions of index entries on disk.
  587. :param paths: Paths to iterate over
  588. :param root_path: Root path to access from
  589. :param store: Optional store to save new blobs in
  590. :return: Iterator over path, index_entry
  591. """
  592. for path in paths:
  593. p = _tree_to_fs_path(root_path, path)
  594. try:
  595. entry = index_entry_from_path(p, object_store=object_store)
  596. except EnvironmentError as e:
  597. if e.errno in (errno.ENOENT, errno.EISDIR):
  598. entry = None
  599. else:
  600. raise
  601. yield path, entry
  602. def iter_fresh_blobs(index, root_path):
  603. """Iterate over versions of blobs on disk referenced by index.
  604. Don't use this function; it removes missing entries from index.
  605. :param index: Index file
  606. :param root_path: Root path to access from
  607. :param include_deleted: Include deleted entries with sha and
  608. mode set to None
  609. :return: Iterator over path, sha, mode
  610. """
  611. import warnings
  612. warnings.warn(PendingDeprecationWarning,
  613. "Use iter_fresh_objects instead.")
  614. for entry in iter_fresh_objects(
  615. index, root_path, include_deleted=True):
  616. if entry[1] is None:
  617. del index[entry[0]]
  618. else:
  619. yield entry
  620. def iter_fresh_objects(paths, root_path, include_deleted=False,
  621. object_store=None):
  622. """Iterate over versions of objecs on disk referenced by index.
  623. :param index: Index file
  624. :param root_path: Root path to access from
  625. :param include_deleted: Include deleted entries with sha and
  626. mode set to None
  627. :param object_store: Optional object store to report new items to
  628. :return: Iterator over path, sha, mode
  629. """
  630. for path, entry in iter_fresh_entries(paths, root_path,
  631. object_store=object_store):
  632. if entry is None:
  633. if include_deleted:
  634. yield path, None, None
  635. else:
  636. entry = IndexEntry(*entry)
  637. yield path, entry.sha, cleanup_mode(entry.mode)
  638. def refresh_index(index, root_path):
  639. """Refresh the contents of an index.
  640. This is the equivalent to running 'git commit -a'.
  641. :param index: Index to update
  642. :param root_path: Root filesystem path
  643. """
  644. for path, entry in iter_fresh_entries(index, root_path):
  645. index[path] = path