def write(self, file_path = None, ignore_tree_extension_data=False): """Write the current state to our file path or to the given one :param file_path: If None, we will write to our stored file path from which we have been initialized. Otherwise we write to the given file path. Please note that this will change the file_path of this index to the one you gave. :param ignore_tree_extension_data: If True, the TREE type extension data read in the index will not be written to disk. Use this if you have altered the index and would like to use pygit-write-tree afterwards to create a tree representing your written changes. If this data is present in the written index, pygit-write-tree will instead write the stored/cached tree. Alternatively, use IndexFile.write_tree() to handle this case automatically :return: self""" # make sure we have our entries read before getting a write lock # else it would be done when streaming. This can happen # if one doesn't change the index, but writes it right away self.entries lfd = LockedFD(file_path or self._file_path) stream = lfd.open(write=True, stream=True) self._serialize(stream, ignore_tree_extension_data) lfd.commit() # make sure we represent what we have written if file_path is not None: self._file_path = file_path
def _set_cache_(self, attr): if attr == "entries": # read the current index # try memory map for speed lfd = LockedFD(self._file_path) try: fd = lfd.open(write=False, stream=False) except OSError: lfd.rollback() # in new repositories, there may be no index, which means we are empty self.entries = dict() return # END exception handling # Here it comes: on windows in python 2.5, memory maps aren't closed properly # Hence we are in trouble if we try to delete a file that is memory mapped, # which happens during read-tree. # In this case, we will just read the memory in directly. # Its insanely bad ... I am disappointed ! allow_mmap = (os.name != 'nt' or sys.version_info[1] > 5) stream = file_contents_ro(fd, stream=True, allow_mmap=allow_mmap) try: self._deserialize(stream) finally: lfd.rollback() # The handles will be closed on desctruction # END read from default index on demand else: super(IndexFile, self)._set_cache_(attr)
def to_file(self, filepath): """Write the contents of the reflog instance to a file at the given filepath. :param filepath: path to file, parent directories are assumed to exist""" lfd = LockedFD(filepath) assure_directory_exists(filepath, is_file=True) fp = lfd.open(write=True, stream=True) try: self._serialize(fp) lfd.commit() except: # on failure it rolls back automatically, but we make it clear lfd.rollback() raise