Kiln » Dependencies » Dulwich Read More
Clone URL:  
Pushed to one repository · View In Graph Contained in master

Add extra base class for DiskObjectStore.

Changeset 634b2cd1f061

Parent 4f730ba2f61d

by Jelmer Vernooij

Changes to one file · Browse files at 634b2cd1f061 Showing diff from parent 4f730ba2f61d Diff from another changeset...

 
237
238
239
240
241
242
243
244
245
246
247
248
 
 
 
249
250
251
252
253
254
255
256
257
 
260
261
262
263
264
265
266
 
 
 
 
 
 
 
 
 
267
268
269
 
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
 
 
 
 
 
 
 
 
 
 
 
 
 
 
313
314
315
 
332
333
334
335
 
336
337
338
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
339
340
341
 
362
363
364
365
 
366
367
368
 
379
380
381
382
 
383
384
385
 
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
 
237
238
239
 
 
 
 
 
 
 
 
 
240
241
242
243
 
 
 
 
 
244
245
246
 
249
250
251
 
 
 
 
252
253
254
255
256
257
258
259
260
261
262
263
 
266
267
268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
 
302
303
304
 
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
 
389
390
391
 
392
393
394
395
 
406
407
408
 
409
410
411
412
 
454
455
456
 
 
 
 
 
 
 
 
 
 
 
 
457
458
459
@@ -237,21 +237,10 @@
  return self.iter_shas(self.find_missing_objects(have, want))     -class DiskObjectStore(BaseObjectStore): - """Git-style object store that exists on disk.""" - - def __init__(self, path): - """Open an object store. - - :param path: Path of the object store. - """ - self.path = path +class PackBasedObjectStore(BaseObjectStore): + + def __init__(self):   self._pack_cache = None - self.pack_dir = os.path.join(self.path, PACKDIR) - - def contains_loose(self, sha): - """Check if a particular object is present by SHA1 and is loose.""" - return self._get_shafile(sha) is not None     def contains_packed(self, sha):   """Check if a particular object is present by SHA1 and is packed.""" @@ -260,10 +249,15 @@
  return True   return False   - def __iter__(self): - """Iterate over the SHAs that are present in this store.""" - iterables = self.packs + [self._iter_shafile_shas()] - return itertools.chain(*iterables) + def _load_packs(self): + raise NotImplementedError(self._load_packs) + + def _add_known_pack(self, pack): + """Add a newly appeared pack to the cache by path. + + """ + if self._pack_cache is not None: + self._pack_cache.append(pack)     @property   def packs(self): @@ -272,44 +266,20 @@
  self._pack_cache = self._load_packs()   return self._pack_cache   - def _load_packs(self): - if not os.path.exists(self.pack_dir): - return [] - pack_files = [] - for name in os.listdir(self.pack_dir): - # TODO: verify that idx exists first - if name.startswith("pack-") and name.endswith(".pack"): - filename = os.path.join(self.pack_dir, name) - pack_files.append((os.stat(filename).st_mtime, filename)) - pack_files.sort(reverse=True) - suffix_len = len(".pack") - return [Pack(f[:-suffix_len]) for _, f in pack_files] - - def _add_known_pack(self, path): - """Add a newly appeared pack to the cache by path. - - """ - if self._pack_cache is not None: - self._pack_cache.append(Pack(path)) - - def _get_shafile_path(self, sha): - dir = sha[:2] - file = sha[2:] - # Check from object dir - return os.path.join(self.path, dir, file) - - def _iter_shafile_shas(self): - for base in os.listdir(self.path): - if len(base) != 2: - continue - for rest in os.listdir(os.path.join(self.path, base)): - yield base+rest - - def _get_shafile(self, sha): - path = self._get_shafile_path(sha) - if os.path.exists(path): - return ShaFile.from_file(path) - return None + def _iter_loose_objects(self): + raise NotImplementedError(self._iter_loose_objects) + + def _get_loose_object(self, sha): + raise NotImplementedError(self._get_loose_object) + + def __iter__(self): + """Iterate over the SHAs that are present in this store.""" + iterables = self.packs + [self._iter_loose_objects()] + return itertools.chain(*iterables) + + def contains_loose(self, sha): + """Check if a particular object is present by SHA1 and is loose.""" + return self._get_loose_object(sha) is not None     def get_raw(self, name):   """Obtain the raw text for an object. @@ -332,10 +302,67 @@
  pass   if hexsha is None:   hexsha = sha_to_hex(name) - ret = self._get_shafile(hexsha) + ret = self._get_loose_object(hexsha)   if ret is not None:   return ret.type, ret.as_raw_string()   raise KeyError(hexsha) + + def add_objects(self, objects): + """Add a set of objects to this object store. + + :param objects: Iterable over objects, should support __len__. + """ + if len(objects) == 0: + # Don't bother writing an empty pack file + return + f, commit = self.add_pack() + write_pack_data(f, objects, len(objects)) + commit() + + +class DiskObjectStore(PackBasedObjectStore): + """Git-style object store that exists on disk.""" + + def __init__(self, path): + """Open an object store. + + :param path: Path of the object store. + """ + super(DiskObjectStore, self).__init__() + self.path = path + self.pack_dir = os.path.join(self.path, PACKDIR) + + def _load_packs(self): + if not os.path.exists(self.pack_dir): + return [] + pack_files = [] + for name in os.listdir(self.pack_dir): + # TODO: verify that idx exists first + if name.startswith("pack-") and name.endswith(".pack"): + filename = os.path.join(self.pack_dir, name) + pack_files.append((os.stat(filename).st_mtime, filename)) + pack_files.sort(reverse=True) + suffix_len = len(".pack") + return [Pack(f[:-suffix_len]) for _, f in pack_files] + + def _get_shafile_path(self, sha): + dir = sha[:2] + file = sha[2:] + # Check from object dir + return os.path.join(self.path, dir, file) + + def _iter_loose_objects(self): + for base in os.listdir(self.path): + if len(base) != 2: + continue + for rest in os.listdir(os.path.join(self.path, base)): + yield base+rest + + def _get_loose_object(self, sha): + path = self._get_shafile_path(sha) + if os.path.exists(path): + return ShaFile.from_file(path) + return None     def move_in_thin_pack(self, path):   """Move a specific file containing a pack into the pack directory. @@ -362,7 +389,7 @@
  newbasename = os.path.join(self.pack_dir, "pack-%s" % pack_sha)   os.rename(temppath+".pack", newbasename+".pack")   os.rename(temppath+".idx", newbasename+".idx") - self._add_known_pack(newbasename) + self._add_known_pack(Pack(newbasename))     def move_in_pack(self, path):   """Move a specific file containing a pack into the pack directory. @@ -379,7 +406,7 @@
  write_pack_index_v2(basename+".idx", entries, p.get_stored_checksum())   p.close()   os.rename(path, basename + ".pack") - self._add_known_pack(basename) + self._add_known_pack(Pack(basename))     def add_thin_pack(self):   """Add a new thin pack to this object store. @@ -427,18 +454,6 @@
  f.write(obj.as_legacy_object())   finally:   f.close() - - def add_objects(self, objects): - """Add a set of objects to this object store. - - :param objects: Iterable over objects, should support __len__. - """ - if len(objects) == 0: - # Don't bother writing an empty pack file - return - f, commit = self.add_pack() - write_pack_data(f, objects, len(objects)) - commit()      class MemoryObjectStore(BaseObjectStore):