def _get_object(self, sha, cls): assert len(sha) in (20, 40) ret = self.get_object(sha) if ret._type != cls._type: if cls is Commit: raise NotCommitError(ret) elif cls is Blob: raise NotBlobError(ret) elif cls is Tree: raise NotTreeError(ret) else: raise Exception("Type invalid: %r != %r" % (ret._type, cls._type)) return ret
def _get_object(self, sha, cls): assert len(sha) in (20, 40) ret = self.get_object(sha) if not isinstance(ret, cls): if cls is Commit: raise NotCommitError(ret) elif cls is Blob: raise NotBlobError(ret) elif cls is Tree: raise NotTreeError(ret) elif cls is Tag: raise NotTagError(ret) else: raise Exception("Type invalid: %r != %r" % ( ret.type_name, cls.type_name)) return ret
def _diff_file(self, path, a, b=None, html=False): """ Use difflib to compare a file between two commits, or a single commit and the working tree. :param a: ref to commit a. :param b: ref to commit b, defaults to the working tree. :param path: path to file, relative to repo root. :param html: format using difflib.HtmlDiff. :raise NotBlobError: if path wasn't present in both trees. """ # resolve commit a = self._resolve_ref(a) # get the trees tree1 = self.repo[self.repo[a].tree] # get the blob blob1 = self._obj_from_tree(tree1, path) # set data or empty string (meaning no blob at path) data1 = blob1.data if type(blob1) is Blob else '' if b is None: with open(os.path.join(self.root, path), 'r') as fp: data2 = fp.read() else: b = self._resolve_ref(b) tree2 = self.repo[self.repo[b].tree] blob2 = self._obj_from_tree(tree2, path) data2 = blob2.data if type(blob2) is Blob else '' # if both blobs were missing => bad path if type(blob1) is not Blob and type(blob2) is not Blob: raise NotBlobError( 'Path did not point to a blob in either tree') diff = list( difflib.context_diff(data1.splitlines(), data2.splitlines())) return '\n'.join(diff)
def from_path(cls, path): blob = ShaFile.from_path(path) if not isinstance(blob, cls): raise NotBlobError(path) return blob