|
| 1 | +//! [Read](read()) and [write](write()) shallow files, while performing typical operations on them. |
| 2 | +#![deny(missing_docs, rust_2018_idioms)] |
| 3 | + |
| 4 | +/// An instruction on how to |
| 5 | +#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)] |
| 6 | +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
| 7 | +pub enum Update { |
| 8 | + /// Shallow the given `id`. |
| 9 | + Shallow(gix_hash::ObjectId), |
| 10 | + /// Don't shallow the given `id` anymore. |
| 11 | + Unshallow(gix_hash::ObjectId), |
| 12 | +} |
| 13 | + |
| 14 | +/// Return a list of shallow commits as unconditionally read from `shallow_file`. |
| 15 | +/// |
| 16 | +/// The list of shallow commits represents the shallow boundary, beyond which we are lacking all (parent) commits. |
| 17 | +/// Note that the list is never empty, as `Ok(None)` is returned in that case indicating the repository |
| 18 | +/// isn't a shallow clone. |
| 19 | +pub fn read(shallow_file: &std::path::Path) -> Result<Option<Vec<gix_hash::ObjectId>>, read::Error> { |
| 20 | + use bstr::ByteSlice; |
| 21 | + let buf = match std::fs::read(shallow_file) { |
| 22 | + Ok(buf) => buf, |
| 23 | + Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(None), |
| 24 | + Err(err) => return Err(err.into()), |
| 25 | + }; |
| 26 | + |
| 27 | + let mut commits = buf |
| 28 | + .lines() |
| 29 | + .map(gix_hash::ObjectId::from_hex) |
| 30 | + .collect::<Result<Vec<_>, _>>()?; |
| 31 | + |
| 32 | + commits.sort(); |
| 33 | + if commits.is_empty() { |
| 34 | + Ok(None) |
| 35 | + } else { |
| 36 | + Ok(Some(commits)) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/// |
| 41 | +pub mod write { |
| 42 | + pub(crate) mod function { |
| 43 | + use std::io::Write; |
| 44 | + |
| 45 | + use super::Error; |
| 46 | + use crate::Update; |
| 47 | + |
| 48 | + /// Write the [previously obtained](crate::read()) (possibly non-existing) `shallow_commits` to the shallow `file` |
| 49 | + /// after applying all `updates`. |
| 50 | + /// |
| 51 | + /// If this leaves the list of shallow commits empty, the file is removed. |
| 52 | + /// |
| 53 | + /// ### Deviation |
| 54 | + /// |
| 55 | + /// Git also prunes the set of shallow commits while writing, we don't until we support some sort of pruning. |
| 56 | + pub fn write( |
| 57 | + mut file: gix_lock::File, |
| 58 | + shallow_commits: Option<Vec<gix_hash::ObjectId>>, |
| 59 | + updates: &[Update], |
| 60 | + ) -> Result<(), Error> { |
| 61 | + let mut shallow_commits = shallow_commits.unwrap_or_default(); |
| 62 | + for update in updates { |
| 63 | + match update { |
| 64 | + Update::Shallow(id) => { |
| 65 | + shallow_commits.push(*id); |
| 66 | + } |
| 67 | + Update::Unshallow(id) => shallow_commits.retain(|oid| oid != id), |
| 68 | + } |
| 69 | + } |
| 70 | + if shallow_commits.is_empty() { |
| 71 | + std::fs::remove_file(file.resource_path())?; |
| 72 | + drop(file); |
| 73 | + return Ok(()); |
| 74 | + } |
| 75 | + |
| 76 | + if shallow_commits.is_empty() { |
| 77 | + if let Err(err) = std::fs::remove_file(file.resource_path()) { |
| 78 | + if err.kind() != std::io::ErrorKind::NotFound { |
| 79 | + return Err(err.into()); |
| 80 | + } |
| 81 | + } |
| 82 | + } else { |
| 83 | + shallow_commits.sort(); |
| 84 | + let mut buf = Vec::<u8>::new(); |
| 85 | + for commit in shallow_commits { |
| 86 | + commit.write_hex_to(&mut buf).map_err(Error::Io)?; |
| 87 | + buf.push(b'\n'); |
| 88 | + } |
| 89 | + file.write_all(&buf).map_err(Error::Io)?; |
| 90 | + file.flush()?; |
| 91 | + } |
| 92 | + file.commit()?; |
| 93 | + Ok(()) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /// The error returned by [`write()`](crate::write()). |
| 98 | + #[derive(Debug, thiserror::Error)] |
| 99 | + #[allow(missing_docs)] |
| 100 | + pub enum Error { |
| 101 | + #[error(transparent)] |
| 102 | + Commit(#[from] gix_lock::commit::Error<gix_lock::File>), |
| 103 | + #[error("Could not remove an empty shallow file")] |
| 104 | + RemoveEmpty(#[from] std::io::Error), |
| 105 | + #[error("Failed to write object id to shallow file")] |
| 106 | + Io(std::io::Error), |
| 107 | + } |
| 108 | +} |
| 109 | +pub use write::function::write; |
| 110 | + |
| 111 | +/// |
| 112 | +pub mod read { |
| 113 | + /// The error returned by [`read`](crate::read()). |
| 114 | + #[derive(Debug, thiserror::Error)] |
| 115 | + #[allow(missing_docs)] |
| 116 | + pub enum Error { |
| 117 | + #[error("Could not open shallow file for reading")] |
| 118 | + Io(#[from] std::io::Error), |
| 119 | + #[error("Could not decode a line in shallow file as hex-encoded object hash")] |
| 120 | + DecodeHash(#[from] gix_hash::decode::Error), |
| 121 | + } |
| 122 | +} |
0 commit comments