index.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. # index.py -- File parser/writer for the git index file
  2. # Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # of the License or (at your opinion) any later version of the license.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. """Parser for the git index file format."""
  19. import os
  20. import stat
  21. import struct
  22. from dulwich.file import GitFile
  23. from dulwich.objects import (
  24. S_IFGITLINK,
  25. S_ISGITLINK,
  26. Tree,
  27. hex_to_sha,
  28. sha_to_hex,
  29. )
  30. from dulwich.pack import (
  31. SHA1Reader,
  32. SHA1Writer,
  33. )
  34. def pathsplit(path):
  35. """Split a /-delimited path into a directory part and a basename.
  36. :param path: The path to split.
  37. :return: Tuple with directory name and basename
  38. """
  39. try:
  40. (dirname, basename) = path.rsplit("/", 1)
  41. except ValueError:
  42. return ("", path)
  43. else:
  44. return (dirname, basename)
  45. def pathjoin(*args):
  46. """Join a /-delimited path.
  47. """
  48. return "/".join([p for p in args if p])
  49. def read_cache_time(f):
  50. """Read a cache time.
  51. :param f: File-like object to read from
  52. :return: Tuple with seconds and nanoseconds
  53. """
  54. return struct.unpack(">LL", f.read(8))
  55. def write_cache_time(f, t):
  56. """Write a cache time.
  57. :param f: File-like object to write to
  58. :param t: Time to write (as int, float or tuple with secs and nsecs)
  59. """
  60. if isinstance(t, int):
  61. t = (t, 0)
  62. elif isinstance(t, float):
  63. (secs, nsecs) = divmod(t, 1.0)
  64. t = (int(secs), int(nsecs * 1000000000))
  65. elif not isinstance(t, tuple):
  66. raise TypeError(t)
  67. f.write(struct.pack(">LL", *t))
  68. def read_cache_entry(f):
  69. """Read an entry from a cache file.
  70. :param f: File-like object to read from
  71. :return: tuple with: device, inode, mode, uid, gid, size, sha, flags
  72. """
  73. beginoffset = f.tell()
  74. ctime = read_cache_time(f)
  75. mtime = read_cache_time(f)
  76. (dev, ino, mode, uid, gid, size, sha, flags, ) = \
  77. struct.unpack(">LLLLLL20sH", f.read(20 + 4 * 6 + 2))
  78. name = f.read((flags & 0x0fff))
  79. # Padding:
  80. real_size = ((f.tell() - beginoffset + 8) & ~7)
  81. data = f.read((beginoffset + real_size) - f.tell())
  82. return (name, ctime, mtime, dev, ino, mode, uid, gid, size,
  83. sha_to_hex(sha), flags & ~0x0fff)
  84. def write_cache_entry(f, entry):
  85. """Write an index entry to a file.
  86. :param f: File object
  87. :param entry: Entry to write, tuple with:
  88. (name, ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags)
  89. """
  90. beginoffset = f.tell()
  91. (name, ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = entry
  92. write_cache_time(f, ctime)
  93. write_cache_time(f, mtime)
  94. flags = len(name) | (flags &~ 0x0fff)
  95. f.write(struct.pack(">LLLLLL20sH", dev, ino, mode, uid, gid, size, hex_to_sha(sha), flags))
  96. f.write(name)
  97. real_size = ((f.tell() - beginoffset + 8) & ~7)
  98. f.write("\0" * ((beginoffset + real_size) - f.tell()))
  99. def read_index(f):
  100. """Read an index file, yielding the individual entries."""
  101. header = f.read(4)
  102. if header != "DIRC":
  103. raise AssertionError("Invalid index file header: %r" % header)
  104. (version, num_entries) = struct.unpack(">LL", f.read(4 * 2))
  105. assert version in (1, 2)
  106. for i in range(num_entries):
  107. yield read_cache_entry(f)
  108. def read_index_dict(f):
  109. """Read an index file and return it as a dictionary.
  110. :param f: File object to read from
  111. """
  112. ret = {}
  113. for x in read_index(f):
  114. ret[x[0]] = tuple(x[1:])
  115. return ret
  116. def write_index(f, entries):
  117. """Write an index file.
  118. :param f: File-like object to write to
  119. :param entries: Iterable over the entries to write
  120. """
  121. f.write("DIRC")
  122. f.write(struct.pack(">LL", 2, len(entries)))
  123. for x in entries:
  124. write_cache_entry(f, x)
  125. def write_index_dict(f, entries):
  126. """Write an index file based on the contents of a dictionary.
  127. """
  128. entries_list = []
  129. for name in sorted(entries):
  130. entries_list.append((name,) + tuple(entries[name]))
  131. write_index(f, entries_list)
  132. def cleanup_mode(mode):
  133. """Cleanup a mode value.
  134. This will return a mode that can be stored in a tree object.
  135. :param mode: Mode to clean up.
  136. """
  137. if stat.S_ISLNK(mode):
  138. return stat.S_IFLNK
  139. elif stat.S_ISDIR(mode):
  140. return stat.S_IFDIR
  141. elif S_ISGITLINK(mode):
  142. return S_IFGITLINK
  143. ret = stat.S_IFREG | 0644
  144. ret |= (mode & 0111)
  145. return ret
  146. class Index(object):
  147. """A Git Index file."""
  148. def __init__(self, filename):
  149. """Open an index file.
  150. :param filename: Path to the index file
  151. """
  152. self._filename = filename
  153. self.clear()
  154. self.read()
  155. def write(self):
  156. """Write current contents of index to disk."""
  157. f = GitFile(self._filename, 'wb')
  158. try:
  159. f = SHA1Writer(f)
  160. write_index_dict(f, self._byname)
  161. finally:
  162. f.close()
  163. def read(self):
  164. """Read current contents of index from disk."""
  165. if not os.path.exists(self._filename):
  166. return
  167. f = GitFile(self._filename, 'rb')
  168. try:
  169. f = SHA1Reader(f)
  170. for x in read_index(f):
  171. self[x[0]] = tuple(x[1:])
  172. # FIXME: Additional data?
  173. f.read(os.path.getsize(self._filename)-f.tell()-20)
  174. f.check_sha()
  175. finally:
  176. f.close()
  177. def __len__(self):
  178. """Number of entries in this index file."""
  179. return len(self._byname)
  180. def __getitem__(self, name):
  181. """Retrieve entry by relative path.
  182. :return: tuple with (ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags)
  183. """
  184. return self._byname[name]
  185. def __iter__(self):
  186. """Iterate over the paths in this index."""
  187. return iter(self._byname)
  188. def get_sha1(self, path):
  189. """Return the (git object) SHA1 for the object at a path."""
  190. return self[path][-2]
  191. def get_mode(self, path):
  192. """Return the POSIX file mode for the object at a path."""
  193. return self[path][-6]
  194. def iterblobs(self):
  195. """Iterate over path, sha, mode tuples for use with commit_tree."""
  196. for path in self:
  197. entry = self[path]
  198. yield path, entry[-2], cleanup_mode(entry[-6])
  199. def clear(self):
  200. """Remove all contents from this index."""
  201. self._byname = {}
  202. def __setitem__(self, name, x):
  203. assert isinstance(name, str)
  204. assert len(x) == 10
  205. # Remove the old entry if any
  206. self._byname[name] = x
  207. def __delitem__(self, name):
  208. assert isinstance(name, str)
  209. del self._byname[name]
  210. def iteritems(self):
  211. return self._byname.iteritems()
  212. def update(self, entries):
  213. for name, value in entries.iteritems():
  214. self[name] = value
  215. def changes_from_tree(self, object_store, tree, want_unchanged=False):
  216. """Find the differences between the contents of this index and a tree.
  217. :param object_store: Object store to use for retrieving tree contents
  218. :param tree: SHA1 of the root tree
  219. :param want_unchanged: Whether unchanged files should be reported
  220. :return: Iterator over tuples with (oldpath, newpath), (oldmode, newmode), (oldsha, newsha)
  221. """
  222. def lookup_entry(path):
  223. entry = self[path]
  224. return entry[-2], entry[-6]
  225. for (name, mode, sha) in changes_from_tree(self._byname.keys(),
  226. lookup_entry, object_store, tree,
  227. want_unchanged=want_unchanged):
  228. yield (name, mode, sha)
  229. def commit(self, object_store):
  230. """Create a new tree from an index.
  231. :param object_store: Object store to save the tree in
  232. :return: Root tree SHA
  233. """
  234. return commit_tree(object_store, self.iterblobs())
  235. def commit_tree(object_store, blobs):
  236. """Commit a new tree.
  237. :param object_store: Object store to add trees to
  238. :param blobs: Iterable over blob path, sha, mode entries
  239. :return: SHA1 of the created tree.
  240. """
  241. trees = {"": {}}
  242. def add_tree(path):
  243. if path in trees:
  244. return trees[path]
  245. dirname, basename = pathsplit(path)
  246. t = add_tree(dirname)
  247. assert isinstance(basename, str)
  248. newtree = {}
  249. t[basename] = newtree
  250. trees[path] = newtree
  251. return newtree
  252. for path, sha, mode in blobs:
  253. tree_path, basename = pathsplit(path)
  254. tree = add_tree(tree_path)
  255. tree[basename] = (mode, sha)
  256. def build_tree(path):
  257. tree = Tree()
  258. for basename, entry in trees[path].iteritems():
  259. if type(entry) == dict:
  260. mode = stat.S_IFDIR
  261. sha = build_tree(pathjoin(path, basename))
  262. else:
  263. (mode, sha) = entry
  264. tree.add(basename, mode, sha)
  265. object_store.add_object(tree)
  266. return tree.id
  267. return build_tree("")
  268. def commit_index(object_store, index):
  269. """Create a new tree from an index.
  270. :param object_store: Object store to save the tree in
  271. :param index: Index file
  272. :note: This function is deprecated, use index.commit() instead.
  273. :return: Root tree sha.
  274. """
  275. return commit_tree(object_store, index.iterblobs())
  276. def changes_from_tree(names, lookup_entry, object_store, tree,
  277. want_unchanged=False):
  278. """Find the differences between the contents of a tree and
  279. a working copy.
  280. :param names: Iterable of names in the working copy
  281. :param lookup_entry: Function to lookup an entry in the working copy
  282. :param object_store: Object store to use for retrieving tree contents
  283. :param tree: SHA1 of the root tree
  284. :param want_unchanged: Whether unchanged files should be reported
  285. :return: Iterator over tuples with (oldpath, newpath), (oldmode, newmode),
  286. (oldsha, newsha)
  287. """
  288. other_names = set(names)
  289. for (name, mode, sha) in object_store.iter_tree_contents(tree):
  290. try:
  291. (other_sha, other_mode) = lookup_entry(name)
  292. except KeyError:
  293. # Was removed
  294. yield ((name, None), (mode, None), (sha, None))
  295. else:
  296. other_names.remove(name)
  297. if (want_unchanged or other_sha != sha or other_mode != mode):
  298. yield ((name, name), (mode, other_mode), (sha, other_sha))
  299. # Mention added files
  300. for name in other_names:
  301. (other_sha, other_mode) = lookup_entry(name)
  302. yield ((None, name), (None, other_mode), (None, other_sha))
  303. def index_entry_from_stat(stat_val, hex_sha, flags, mode=None):
  304. """Create a new index entry from a stat value.
  305. :param stat_val: POSIX stat_result instance
  306. :param hex_sha: Hex sha of the object
  307. :param flags: Index flags
  308. """
  309. if mode is None:
  310. mode = stat_val.st_mode
  311. return (stat_val.st_ctime, stat_val.st_mtime, stat_val.st_dev,
  312. stat_val.st_ino, mode, stat_val.st_uid,
  313. stat_val.st_gid, stat_val.st_size, hex_sha, flags)