packfile/
lib.rs

1#![deny(clippy::pedantic)]
2//! `packfile` is a simple library providing utilities to generate [Git Packfiles] in memory.
3//!
4//! Usage:
5//!
6//! ```rust
7//! # use packfile::{high_level::GitRepository, low_level::PackFile};
8//! #
9//! let mut repo = GitRepository::default();
10//! repo.insert(&["path", "to"], "file.txt", "hello world!".into()).unwrap();
11//! let (_commit_hash, entries) =
12//!     repo.commit("Linus Torvalds", "torvalds@example.com", "Some commit message").unwrap();
13//!
14//! let _packfile = PackFile::new(&entries);
15//! ```
16//!
17//! The generated packfile can then be encoded within a [`SidebandData`] to send the data to a
18//! client
19//!
20//! [Git Packfiles]: https://git-scm.com/book/en/v2/Git-Internals-Packfiles
21//! [`SidebandData`]: crate::codec::Codec::SidebandData
22
23#[cfg(feature = "tokio-util")]
24pub mod codec;
25mod error;
26pub mod high_level;
27pub mod low_level;
28mod packet_line;
29mod util;
30
31pub use error::Error;
32pub use packet_line::PktLine;
33
34#[cfg(test)]
35mod test {
36    use bytes::Bytes;
37    use std::process::{Command, Stdio};
38    use tempfile::TempDir;
39
40    pub fn verify_pack_file(packed: Bytes) -> String {
41        let scratch_dir = TempDir::new().unwrap();
42        let packfile_path = scratch_dir.path().join("example.pack");
43
44        std::fs::write(&packfile_path, packed).unwrap();
45
46        let res = Command::new("git")
47            .arg("index-pack")
48            .arg(&packfile_path)
49            .stdout(Stdio::piped())
50            .spawn()
51            .unwrap()
52            .wait()
53            .unwrap();
54        assert!(res.success());
55
56        let command = Command::new("git")
57            .arg("verify-pack")
58            .arg("-v")
59            .stdout(Stdio::piped())
60            .arg(&packfile_path)
61            .spawn()
62            .unwrap();
63
64        let out = command.wait_with_output().unwrap();
65        assert!(out.status.success(), "git exited non-0");
66
67        String::from_utf8(out.stdout).unwrap()
68    }
69}