Skip to content

Commit

Permalink
feat!: support for compression with tar.gz format.
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 7, 2023
1 parent e0432d1 commit 088adad
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
9 changes: 6 additions & 3 deletions gix-archive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ pub enum Format {
/// Use it as well if a custom container format is desired. The idea is to decode it on a separate thread
/// to rewrite the data to the desired format.
Tar,
/// A convenience format that will `gzip` deflate the `tar` stream, using the default compression level.
// TODO: figure out how to do this with `libflate`.
TarGz,
/// A convenience format that will `gzip` deflate the `tar` stream.
TarGz {
/// If `None`, use the default compression level. Otherwise use the given one which
/// ranges from 0-9 for the deflate algorithm.
compression_level: Option<u8>,
},
/// A standard `zip` archive. Note that this format silently converts illformed UTF-8 to UTF-8, which will
/// equal a change of path.
///
Expand Down
14 changes: 8 additions & 6 deletions gix-archive/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,18 @@ where
Err(Error::SupportNotCompiledIn { wanted: Format::Tar })
}
}
Format::TarGz => {
Format::TarGz { compression_level } => {
#[cfg(feature = "tar_gz")]
{
State::TarGz((
{
// TODO: compression
let gz = flate2::GzBuilder::new()
.filename("git.tar")
.mtime(mtime as u32)
.write(out, flate2::Compression::default());
let gz = flate2::GzBuilder::new().mtime(mtime as u32).write(
out,
match compression_level {
None => flate2::Compression::default(),
Some(level) => flate2::Compression::new(level as u32),
},
);
let mut ar = tar::Builder::new(gz);
ar.mode(tar::HeaderMode::Deterministic);
ar
Expand Down
21 changes: 13 additions & 8 deletions gix-archive/tests/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,19 @@ mod from_tree {
#[test]
#[cfg(feature = "tar_gz")]
fn basic_usage_tar_gz() -> gix_testtools::Result {
basic_usage(gix_archive::Format::TarGz, |buf| {
assert!(
buf.len() < 350,
"quite a bit smaller than uncompressed: {} < 350",
buf.len()
);
Ok(())
})
basic_usage(
gix_archive::Format::TarGz {
compression_level: Some(9),
},
|buf| {
assert!(
buf.len() < 340,
"quite a bit smaller than uncompressed: {} < 340",
buf.len()
);
Ok(())
},
)
}

#[test]
Expand Down

0 comments on commit 088adad

Please sign in to comment.