Skip to content

Commit

Permalink
Add cloned repositories to application config
Browse files Browse the repository at this point in the history
  • Loading branch information
Notgnoshi committed Jul 15, 2024
1 parent 552ea1c commit ebdc64d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/commands/add.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
use std::path::Path;

use crate::cli::AddArgs;
use crate::config::Config;
use crate::config::{Config, RepositoryConfig};
use crate::git::clone::{clone_repository, get_clone_path};

pub fn add(args: &AddArgs, config: &mut Config, data_dir: &Path) -> eyre::Result<()> {
// 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, args.branch.as_deref(), args.force)?;
// TODO: What *should* the name be? The whole URL? Just the path part of the URL?
// TODO: What to do if the repository is already added?
let Some((_, name)) = args.url.rsplit_once('/') else {
eyre::bail!("Failed to parse URL '{}'", args.url)
};
let name = name.to_string();

// TODO: Save the configuration parameters to a config file.
let clone_path = if let Some(cli_path) = &args.path {
cli_path.to_path_buf()
} else {
get_clone_path(data_dir, &args.url)?
};

let repo_config = RepositoryConfig {
path: clone_path,
remote_url: args.url.clone(),
branch: args.branch.clone(),
};

let _repo = clone_repository(
&repo_config.path,
&args.url,
args.branch.as_deref(),
args.force,
)?;
config.repositories.insert(name, repo_config);
Ok(())
}
33 changes: 33 additions & 0 deletions tests/add.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod common;

use common::CommandExt;
use herostratus::config::{config_path, read_config, Config, RepositoryConfig};

#[test]
#[ignore = "Slow; performs git clone"]
Expand All @@ -17,11 +18,27 @@ fn clone_herostratus() {
cmd.arg("add").arg(url);

assert!(!data_dir.join("git").exists());
assert!(!config_path(data_dir).exists());

let output = cmd.captured_output().unwrap();
assert!(output.status.success());
assert!(expected_bare_repo.exists());

let default_config = Config::default();
let actual_config = read_config(data_dir).unwrap();
assert_ne!(
default_config, actual_config,
"Adding the repo modified the config"
);
assert!(actual_config.repositories.contains_key("herostratus.git"));
let repo_config = &actual_config.repositories["herostratus.git"];
let expected = RepositoryConfig {
path: expected_bare_repo,
remote_url: url.to_string(),
branch: None,
};
assert_eq!(repo_config, &expected);

// Adding the same URL again in the same data_dir fails ...
let (mut cmd, _temp) = common::herostratus(Some(data_dir));
cmd.arg("add").arg(url);
Expand Down Expand Up @@ -53,11 +70,27 @@ fn clone_herostratus_branch() {
cmd.arg("add").arg(url).arg("test/fixup");

assert!(!clone_dir.exists());
assert!(!config_path(temp.as_ref().unwrap().path()).exists());

let output = cmd.captured_output().unwrap();
assert!(output.status.success());
assert!(clone_dir.exists());

let default_config = Config::default();
let actual_config = read_config(temp.as_ref().unwrap().path()).unwrap();
assert_ne!(
default_config, actual_config,
"Adding the repo modified the config"
);
assert!(actual_config.repositories.contains_key("herostratus.git"));
let repo_config = &actual_config.repositories["herostratus.git"];
let expected = RepositoryConfig {
path: clone_dir.clone(),
remote_url: url.to_string(),
branch: Some(String::from("test/fixup")),
};
assert_eq!(repo_config, &expected);

let repo = herostratus::git::clone::find_local_repository(&clone_dir).unwrap();
let head = repo.head().unwrap();
let head = head.name().unwrap();
Expand Down

0 comments on commit ebdc64d

Please sign in to comment.