Skip to content

Commit

Permalink
Clone just the single branch, if given
Browse files Browse the repository at this point in the history
  • Loading branch information
Notgnoshi committed Jul 14, 2024
1 parent 7736ec2 commit 9d292b5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
12 changes: 9 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,22 @@ 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<String>,

/// The path to clone the repository
///
/// Given a URL like `https://github.com/Notgnoshi/herostratus.git`, the repository will be
/// cloned to `cgit/Notgnoshi/herostratus.git/` in the application data directory by default.
#[clap(short, long, verbatim_doc_comment)]
pub path: Option<PathBuf>,

// 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,
Expand Down
12 changes: 5 additions & 7 deletions src/commands/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
1 change: 1 addition & 0 deletions src/git/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
27 changes: 27 additions & 0 deletions tests/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

0 comments on commit 9d292b5

Please sign in to comment.