-
-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add simple CLI for
gix archive
- Loading branch information
Showing
7 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use anyhow::bail; | ||
use gix::worktree::archive; | ||
use gix::Progress; | ||
use std::ops::Add; | ||
use std::path::Path; | ||
|
||
pub fn stream( | ||
repo: gix::Repository, | ||
destination_path: Option<&Path>, | ||
rev_spec: Option<&str>, | ||
mut progress: impl Progress, | ||
format: Option<archive::Format>, | ||
) -> anyhow::Result<()> { | ||
let format = format.map_or_else(|| format_from_ext(destination_path), Ok)?; | ||
let object = repo.rev_parse_single(rev_spec.unwrap_or("HEAD"))?.object()?; | ||
let (modification_date, tree) = fetch_rev_info(object)?; | ||
|
||
let start = std::time::Instant::now(); | ||
let (stream, index) = repo.worktree_stream(tree)?; | ||
|
||
let mut entries = progress.add_child("entries"); | ||
entries.init(Some(index.entries().len()), gix::progress::count("entries")); | ||
let mut bytes = progress.add_child("written"); | ||
bytes.init(None, gix::progress::bytes()); | ||
|
||
let mut file = gix::progress::Write { | ||
inner: match destination_path { | ||
Some(path) => Box::new(std::io::BufWriter::with_capacity( | ||
128 * 1024, | ||
std::fs::File::create(path)?, | ||
)) as Box<dyn std::io::Write>, | ||
None => Box::new(std::io::sink()), | ||
}, | ||
progress: &mut bytes, | ||
}; | ||
repo.worktree_archive( | ||
stream, | ||
&mut file, | ||
&mut entries, | ||
&gix::interrupt::IS_INTERRUPTED, | ||
gix::worktree::archive::Options { | ||
format, | ||
tree_prefix: None, | ||
modification_time: modification_date | ||
.map(|t| std::time::UNIX_EPOCH.add(std::time::Duration::from_secs(t as u64))) | ||
.unwrap_or_else(std::time::SystemTime::now), | ||
}, | ||
)?; | ||
|
||
entries.show_throughput(start); | ||
bytes.show_throughput(start); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn fetch_rev_info( | ||
object: gix::Object<'_>, | ||
) -> anyhow::Result<(Option<gix::date::SecondsSinceUnixEpoch>, gix::ObjectId)> { | ||
Ok(match object.kind { | ||
gix::object::Kind::Commit => { | ||
let commit = object.into_commit(); | ||
(Some(commit.committer()?.time.seconds), commit.tree_id()?.detach()) | ||
} | ||
gix::object::Kind::Tree => (None, object.id), | ||
gix::object::Kind::Tag => fetch_rev_info(object.peel_to_kind(gix::object::Kind::Commit)?)?, | ||
gix::object::Kind::Blob => bail!("Cannot derive commit or tree from blob at {}", object.id), | ||
}) | ||
} | ||
|
||
fn format_from_ext(path: Option<&Path>) -> anyhow::Result<archive::Format> { | ||
Ok(match path { | ||
Some(path) => match path.extension().and_then(|ext| ext.to_str()) { | ||
None => bail!("Cannot derive archive format from a file without extension"), | ||
Some("tar") => archive::Format::Tar, | ||
Some("stream") => archive::Format::InternalTransientNonPersistable, | ||
Some(ext) => bail!("Format for extendion '{ext}' is unsupported"), | ||
}, | ||
None => archive::Format::InternalTransientNonPersistable, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters