From 9d292b54b0e62782b74a81807f3f74bb20ec1f19 Mon Sep 17 00:00:00 2001 From: Austin Gill Date: Sun, 16 Jun 2024 13:44:02 -0500 Subject: [PATCH] Clone just the single branch, if given --- src/cli.rs | 12 +++++++++--- src/commands/add.rs | 12 +++++------- src/git/clone.rs | 1 + tests/add.rs | 27 +++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 1702306..e87ef19 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -80,6 +80,14 @@ pub struct AddArgs { #[clap(verbatim_doc_comment)] pub url: String, + /// The branch to clone, and check + /// + /// If given, only the specified branch will be fetched. + /// + /// If given, this will be the branch that will be checked for achievements instead of the + /// default HEAD. + pub branch: Option, + /// The path to clone the repository /// /// Given a URL like `https://github.com/Notgnoshi/herostratus.git`, the repository will be @@ -87,9 +95,7 @@ pub struct AddArgs { #[clap(short, long, verbatim_doc_comment)] pub path: Option, - // TODO: Add optional branch. If given, will configure the reference to parse, and will do a - // fetch of *just* that branch. This needs to be thought out more, because there should be a - // way to clone the whole thing, and use a rev instead of a ref ... + // TODO: What good is --skip-clone? /// Skip cloning the repository #[clap(long)] pub skip_clone: bool, diff --git a/src/commands/add.rs b/src/commands/add.rs index 2e3a58a..405b3a1 100644 --- a/src/commands/add.rs +++ b/src/commands/add.rs @@ -4,14 +4,12 @@ use crate::cli::AddArgs; use crate::git::clone::{clone_repository, get_clone_path}; pub fn add(args: &AddArgs, data_dir: &Path) -> eyre::Result<()> { - // TODO: Resolve the right clone path from the arguments (including config file?, CLI argument - // overrides) - // - // TODO: Resolve the right reference to process. If possible, only fetch the configured - // reference(s). - + // TODO: Resolve the right clone path in the following priority order + // 1. args.path + // 2. configuration file + // 3. get_clone_path let clone_path = get_clone_path(data_dir, &args.url)?; - let _repo = clone_repository(&clone_path, &args.url, None, args.force)?; + let _repo = clone_repository(&clone_path, &args.url, args.branch.as_deref(), args.force)?; // TODO: Save the configuration parameters to a config file. diff --git a/src/git/clone.rs b/src/git/clone.rs index 77d2e56..2012258 100644 --- a/src/git/clone.rs +++ b/src/git/clone.rs @@ -91,6 +91,7 @@ pub fn clone_repository( // .fetch_options() if let Some(branch) = branch { + tracing::debug!("Cloning just the '{branch}' branch ..."); builder.branch(branch); } diff --git a/tests/add.rs b/tests/add.rs index 1fa0004..72a61a3 100644 --- a/tests/add.rs +++ b/tests/add.rs @@ -36,3 +36,30 @@ fn clone_herostratus() { let output = cmd.captured_output().unwrap(); assert!(output.status.success()); } + +#[test] +#[ignore = "Slow; performs git clone"] +fn clone_herostratus_branch() { + let (mut cmd, temp) = common::herostratus(None); + let clone_dir = temp + .as_ref() + .unwrap() + .path() + .join("git") + .join("Notgnoshi") + .join("herostratus.git"); + + let url = "https://github.com/Notgnoshi/herostratus.git"; + cmd.arg("add").arg(url).arg("test/fixup"); + + assert!(!clone_dir.exists()); + + let output = cmd.captured_output().unwrap(); + assert!(output.status.success()); + assert!(clone_dir.exists()); + + let repo = herostratus::git::clone::find_local_repository(&clone_dir).unwrap(); + let head = repo.head().unwrap(); + let head = head.name().unwrap(); + assert_eq!(head, "refs/heads/test/fixup"); +}