Skip to content

Commit

Permalink
[clone] refs can now be written into a specified directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Sep 11, 2020
1 parent 445be27 commit fb1f048
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 26 deletions.
28 changes: 13 additions & 15 deletions gitoxide-core/src/pack/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct Context<W: io::Write> {
struct CloneDelegate<W: io::Write> {
ctx: Context<W>,
directory: Option<PathBuf>,
write_refs: bool,
refs_directory: Option<PathBuf>,
}

impl<W: io::Write> git_protocol::fetch::Delegate for CloneDelegate<W> {
Expand Down Expand Up @@ -47,27 +47,25 @@ impl<W: io::Write> git_protocol::fetch::Delegate for CloneDelegate<W> {
index_kind: pack::index::Kind::V2,
iteration_mode: pack::data::iter::Mode::Verify,
};
let outcome = pack::bundle::Bundle::write_stream_to_directory(input, self.directory.clone(), progress, options)
let outcome = pack::bundle::Bundle::write_stream_to_directory(input, self.directory.take(), progress, options)
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;

if let Some(directory) = self.directory.take() {
if let Some(directory) = self.refs_directory.take() {
let assure_dir = |path: &git_object::bstr::BString| {
assert!(!path.starts_with_str("/"), "no ref start with a /, they are relative");
let path = directory.join(path.to_path_lossy());
std::fs::create_dir_all(path.parent().expect("multi-component path")).map(|_| path)
};
if self.write_refs {
for r in refs {
match r {
Ref::Symbolic { path, target, .. } => {
assure_dir(path).map(|path| (path, format!("ref: {}", target)))
}
Ref::Peeled { path, tag: object, .. } | Ref::Direct { path, object } => {
assure_dir(path).map(|path| (path, object.to_string()))
}
for r in refs {
match r {
Ref::Symbolic { path, target, .. } => {
assure_dir(path).map(|path| (path, format!("ref: {}", target)))
}
Ref::Peeled { path, tag: object, .. } | Ref::Direct { path, object } => {
assure_dir(path).map(|path| (path, object.to_string()))
}
.and_then(|(path, content)| std::fs::write(path, content.as_bytes()))?;
}
.and_then(|(path, content)| std::fs::write(path, content.as_bytes()))?;
}
}

Expand Down Expand Up @@ -144,7 +142,7 @@ pub fn receive<P, W: io::Write>(
protocol: Option<Protocol>,
url: &str,
directory: Option<PathBuf>,
write_refs: bool,
refs_directory: Option<PathBuf>,
progress: P,
ctx: Context<W>,
) -> anyhow::Result<()>
Expand All @@ -157,7 +155,7 @@ where
let mut delegate = CloneDelegate {
ctx,
directory,
write_refs,
refs_directory,
};
git_protocol::fetch(transport, &mut delegate, git_protocol::credentials::helper, progress)?;
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions src/plumbing/lean/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ pub fn main() -> Result<()> {
protocol,
url,
directory,
write_refs,
refs_directory,
}) => {
let (_handle, progress) = prepare(verbose, "pack-receive", core::pack::receive::PROGRESS_RANGE);
core::pack::receive(
protocol,
&url,
directory,
write_refs,
refs_directory,
progress::DoOrDiscard::from(progress),
core::pack::receive::Context {
thread_limit,
Expand Down
8 changes: 5 additions & 3 deletions src/plumbing/lean/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,11 @@ pub struct PackReceive {
#[argh(option, short = 'p')]
pub protocol: Option<core::Protocol>,

/// write references to the given directory as well, right next to the pack and index file.
#[argh(switch, short = 'r')]
pub write_refs: bool,
/// the directory into which to write references. Existing files will be overwritten.
///
/// Note that the directory will be created if needed.
#[argh(option, short = 'r')]
pub refs_directory: Option<PathBuf>,

/// the URLs or path from which to receive the pack.
///
Expand Down
4 changes: 2 additions & 2 deletions src/plumbing/pretty/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ pub fn main() -> Result<()> {
match cmd {
Subcommands::PackReceive {
protocol,
write_refs,
url,
directory,
refs_directory,
} => prepare_and_run(
"pack-receive",
verbose,
Expand All @@ -141,7 +141,7 @@ pub fn main() -> Result<()> {
protocol,
&url,
directory,
write_refs,
refs_directory,
git_features::progress::DoOrDiscard::from(progress),
core::pack::receive::Context {
thread_limit,
Expand Down
6 changes: 4 additions & 2 deletions src/plumbing/pretty/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ pub enum Subcommands {
#[clap(long, short = "p")]
protocol: Option<core::Protocol>,

/// Write references to the given directory as well, right next to the pack and index file.
/// the directory into which to write references. Existing files will be overwritten.
///
/// Note that the directory will be created if needed.
#[clap(long, short = "r")]
write_refs: bool,
refs_directory: Option<PathBuf>,

/// The URLs or path from which to receive the pack.
///
Expand Down
1 change: 1 addition & 0 deletions tests/snapshots/plumbing/pack-receive/repo-refs/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/master
8 changes: 6 additions & 2 deletions tests/stateless-journey.sh
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,13 @@ snapshot="$snapshot/plumbing"
(with "--write-refs set"
it "generates the correct output" && {
WITH_SNAPSHOT="$snapshot/file-v-any-with-output" \
expect_run $SUCCESSFULLY "$exe_plumbing" pack-receive -p 1 --write-refs .git out/
expect_run $SUCCESSFULLY "$exe_plumbing" pack-receive -p 1 --refs-directory out/all-refs .git out/
}
it "writes references into the refs folder of the output directory" && {
expect_snapshot "$snapshot/repo-refs" out/refs
expect_snapshot "$snapshot/repo-refs" out/all-refs
}
)
rm -Rf out
)
if test "$kind" = "max"; then
(with "--format json"
Expand All @@ -133,6 +134,7 @@ snapshot="$snapshot/plumbing"
}
)
(with "output directory"
mkdir out/
it "generates the correct output" && {
WITH_SNAPSHOT="$snapshot/file-v-any-with-output" \
expect_run $SUCCESSFULLY "$exe_plumbing" pack-receive .git out/
Expand All @@ -141,6 +143,7 @@ snapshot="$snapshot/plumbing"
WITH_SNAPSHOT="$snapshot/ls-in-output-dir" \
expect_run $SUCCESSFULLY ls out/
}
rm -Rf out
)
if test "$kind" = "max"; then
(with "--format json"
Expand All @@ -162,6 +165,7 @@ snapshot="$snapshot/plumbing"
}
)
(with "output directory"
mkdir out
it "generates the correct output" && {
WITH_SNAPSHOT="$snapshot/file-v-any-with-output" \
expect_run $SUCCESSFULLY "$exe_plumbing" pack-receive -p 1 git://localhost/ out/
Expand Down

0 comments on commit fb1f048

Please sign in to comment.