Skip to content

Commit

Permalink
Merge branch 'archive-gz'
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 7, 2023
2 parents 4604f83 + feba76d commit c7d9129
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 90 deletions.
62 changes: 1 addition & 61 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion gitoxide-core/src/repository/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ fn format_from_ext(path: &Path) -> anyhow::Result<archive::Format> {
Ok(match path.extension().and_then(std::ffi::OsStr::to_str) {
None => bail!("Cannot derive archive format from a file without extension"),
Some("tar") => archive::Format::Tar,
Some("gz") => archive::Format::TarGz,
Some("gz") => archive::Format::TarGz {
compression_level: None,
},
Some("zip") => archive::Format::Zip {
compression_level: None,
},
Expand Down
4 changes: 2 additions & 2 deletions gix-archive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ default = ["tar", "tar_gz", "zip"]
## Enable the `tar` archive format. It has support for all information, except for object ids.
tar = ["dep:tar", "dep:gix-path"]
## Enable the `tar.gz` archive format.
tar_gz = ["tar", "dep:libflate"]
tar_gz = ["tar", "dep:flate2"]

## Enable the `zip` archive format.
zip = ["dep:zip", "dep:time"]
Expand All @@ -29,7 +29,7 @@ gix-object = { version = "^0.34.0", path = "../gix-object" }
gix-path = { version = "^0.8.4", path = "../gix-path", optional = true }
gix-date = { version = "^0.7.2", path = "../gix-date" }

libflate = { version = "2.0.0", optional = true }
flate2 = { version = "1.0.26", optional = true }
zip = { version = "0.6.6", optional = true, default-features = false, features = ["deflate", "time"] }
time = { version = "0.3.23", optional = true, default-features = false, features = ["std"] }

Expand Down
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
19 changes: 13 additions & 6 deletions gix-archive/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ where
#[cfg(feature = "tar")]
Tar((tar::Builder<W>, Vec<u8>)),
#[cfg(feature = "tar_gz")]
TarGz((tar::Builder<libflate::gzip::Encoder<W>>, Vec<u8>)),
TarGz((tar::Builder<flate2::write::GzEncoder<W>>, Vec<u8>)),
}

impl<W: std::io::Write> State<W> {
pub fn new(format: Format, out: W) -> Result<Self, Error> {
pub fn new(format: Format, mtime: gix_date::SecondsSinceUnixEpoch, out: W) -> Result<Self, Error> {
Ok(match format {
Format::InternalTransientNonPersistable => unreachable!("handled earlier"),
Format::Zip { .. } => return Err(Error::ZipWithoutSeek),
Expand All @@ -55,12 +55,19 @@ where
Err(Error::SupportNotCompiledIn { wanted: Format::Tar })
}
}
Format::TarGz => {
Format::TarGz { compression_level } => {
#[cfg(feature = "tar_gz")]
{
State::TarGz((
{
let mut ar = tar::Builder::new(libflate::gzip::Encoder::new(out)?);
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 All @@ -76,7 +83,7 @@ where
}
}

let mut state = State::new(opts.format, out)?;
let mut state = State::new(opts.format, opts.modification_time, out)?;
while let Some(entry) = next_entry(stream)? {
match &mut state {
#[cfg(feature = "tar")]
Expand All @@ -97,7 +104,7 @@ where
}
#[cfg(feature = "tar_gz")]
State::TarGz((ar, _)) => {
ar.into_inner()?.finish();
ar.into_inner()?.finish()?;
}
}
}
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() < 385,
"quite a bit smaller than uncompressed: {} < 385",
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
12 changes: 6 additions & 6 deletions gix-revision/fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ pub fn main() -> Result<()> {
gix::worktree::archive::Format::InternalTransientNonPersistable
}
crate::plumbing::options::archive::Format::Tar => gix::worktree::archive::Format::Tar,
crate::plumbing::options::archive::Format::TarGz => gix::worktree::archive::Format::TarGz,
crate::plumbing::options::archive::Format::TarGz => {
gix::worktree::archive::Format::TarGz { compression_level }
}
crate::plumbing::options::archive::Format::Zip => {
gix::worktree::archive::Format::Zip { compression_level }
}
Expand Down
4 changes: 2 additions & 2 deletions src/plumbing/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ pub mod archive {
/// Apply the prefix verbatim to any path we add to the archive. Use a trailing `/` if prefix is a directory.
#[clap(long)]
pub prefix: Option<String>,
/// The compression strength to use. Currently only used for `.zip` archives, valid from 0-9.
#[clap(long, short = 'l', value_enum)]
/// The compression strength to use for `.zip` and `.tar.gz` archives, valid from 0-9.
#[clap(long, short = 'l', requires = "format")]
pub compression_level: Option<u8>,
/// Add the given path to the archive. Directories will always be empty.
#[clap(long, short = 'p')]
Expand Down

0 comments on commit c7d9129

Please sign in to comment.