git_index/file/
write.rs

1use git_features::hash;
2
3use crate::{write, File, Version};
4
5/// The error produced by [`File::write()`].
6#[derive(Debug, thiserror::Error)]
7#[allow(missing_docs)]
8pub enum Error {
9    #[error(transparent)]
10    Io(#[from] std::io::Error),
11    #[error("Could not acquire lock for index file")]
12    AcquireLock(#[from] git_lock::acquire::Error),
13    #[error("Could not commit lock for index file")]
14    CommitLock(#[from] git_lock::commit::Error<git_lock::File>),
15}
16
17impl File {
18    /// Write the index to `out` with `options`, to be readable by [`File::at()`], returning the version that was actually written
19    /// to retain all information of this index.
20    pub fn write_to(
21        &self,
22        mut out: impl std::io::Write,
23        options: write::Options,
24    ) -> std::io::Result<(Version, git_hash::ObjectId)> {
25        let mut hasher = hash::Write::new(&mut out, self.state.object_hash);
26        let version = self.state.write_to(&mut hasher, options)?;
27
28        let hash = hasher.hash.digest();
29        out.write_all(&hash)?;
30        Ok((version, git_hash::ObjectId::from(hash)))
31    }
32
33    /// Write ourselves to the path we were read from after acquiring a lock, using `options`.
34    ///
35    /// Note that the hash produced will be stored which is why we need to be mutable.
36    pub fn write(&mut self, options: write::Options) -> Result<(), Error> {
37        let mut lock = std::io::BufWriter::new(git_lock::File::acquire_to_update_resource(
38            &self.path,
39            git_lock::acquire::Fail::Immediately,
40            None,
41        )?);
42        let (version, digest) = self.write_to(&mut lock, options)?;
43        match lock.into_inner() {
44            Ok(lock) => lock.commit()?,
45            Err(err) => return Err(err.into_error().into()),
46        };
47        self.state.version = version;
48        self.checksum = Some(digest);
49        Ok(())
50    }
51}