Skip to content

Commit

Permalink
Rework config option names
Browse files Browse the repository at this point in the history
  • Loading branch information
9999years committed Oct 21, 2024
1 parent a827b3f commit dd2322a
Show file tree
Hide file tree
Showing 20 changed files with 125 additions and 83 deletions.
33 changes: 22 additions & 11 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# 2. Any remotes listed here.
#
# This is used to pick a default branch; see `default_branches`.
remotes = [
remote_names = [
"upstream",
"origin",
]
Expand All @@ -27,12 +27,27 @@ remotes = [
# When `git prole add` is used to create a new worktree, if a new branch is
# created, the branch will be set to the default branch unless another starting
# point is given explicitly.
default_branches = [
branch_names = [
"main",
"master",
"trunk",
]

# Clone a repository into a worktree repository.
#
# `man git-prole-clone`
[clone]
# When you run `git prole clone foo/bar`, if `enable_gh = true` and `gh` is
# installed, I'll run `gh repo clone foo/bar` as a shortcut for GitHub
# repositories.
#
# See: https://cli.github.com/
enable_gh = false

# Add a new worktree to the current repository.
#
# `man git-prole-add`
[add]
# When `git prole add` is used to create a new worktree, untracked files are
# copied to the new worktree from the current worktree by default.
#
Expand All @@ -42,13 +57,6 @@ default_branches = [
# behavior if needed.
copy_untracked = true

# When you run `git prole clone foo/bar`, if `enable_gh = true` and `gh` is
# installed, I'll run `gh repo clone foo/bar` as a shortcut for GitHub
# repositories.
#
# See: https://cli.github.com/
enable_gh = false

# Commands to run when a new worktree is added.
commands = [
# "direnv allow",
Expand All @@ -62,6 +70,11 @@ commands = [
# A list of regex replacements which are applied to branch names to determine
# directory names.
#
# This settings is also used by `git prole convert` to resolve branch names
# into directory names.
#
# For syntax, see: https://docs.rs/regex/latest/regex/#syntax
#
# By default, when you create a worktree for a branch with a `/` in it, `git
# prole` will use the last component of the name; e.g., `git prole add
# -b puppy/doggy` creates a directory named `doggy` for a new branch
Expand All @@ -87,6 +100,4 @@ commands = [
# find = '''puppy'''
# replace = '''doggy'''
# count = 1
#
# See: https://docs.rs/regex/latest/regex/#syntax
branch_replacements = []
4 changes: 2 additions & 2 deletions src/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl<'a> WorktreePlan<'a> {

#[instrument(level = "trace")]
fn untracked_plan(git: &AppGit<'_, Utf8PathBuf>) -> miette::Result<Vec<Utf8PathBuf>> {
if git.config.file.copy_untracked() && git.worktree().is_inside()? {
if git.config.file.add.copy_untracked() && git.worktree().is_inside()? {
git.status().untracked_files()
} else {
Ok(Vec::new())
Expand Down Expand Up @@ -189,7 +189,7 @@ impl<'a> WorktreePlan<'a> {

#[instrument(level = "trace")]
fn run_commands(&self) -> miette::Result<()> {
for command in self.git.config.file.commands() {
for command in self.git.config.file.add.commands() {
let mut command = command.as_command();
let command_display = Utf8ProgramAndArgs::from(&command);
tracing::info!(
Expand Down
2 changes: 1 addition & 1 deletion src/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ where
return Err(miette!("--dry-run is not supported for this command yet"));
}

if git.config.file.enable_gh()
if git.config.file.clone.enable_gh()
&& looks_like_gh_url(&args.repository)
&& which_global("gh").is_ok()
{
Expand Down
96 changes: 61 additions & 35 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,42 +95,58 @@ fn config_file_path(dirs: &BaseDirectories) -> miette::Result<Utf8PathBuf> {
#[derive(Debug, Default, Deserialize, PartialEq, Eq)]
#[serde(default)]
pub struct ConfigFile {
remotes: Vec<String>,
default_branches: Vec<String>,
copy_untracked: Option<bool>,
enable_gh: Option<bool>,
commands: Vec<ShellCommand>,
branch_replacements: Vec<BranchReplacement>,
remote_names: Vec<String>,
branch_names: Vec<String>,
pub clone: CloneConfig,
pub add: AddConfig,
}

impl ConfigFile {
pub const FILE_NAME: &str = "config.toml";

pub fn remotes(&self) -> Vec<String> {
pub fn remote_names(&self) -> Vec<String> {
// Yeah this basically sucks. But how big could these lists really be?
if self.remotes.is_empty() {
if self.remote_names.is_empty() {
vec!["upstream".to_owned(), "origin".to_owned()]
} else {
self.remotes.clone()
self.remote_names.clone()
}
}

pub fn default_branches(&self) -> Vec<String> {
pub fn branch_names(&self) -> Vec<String> {
// Yeah this basically sucks. But how big could these lists really be?
if self.default_branches.is_empty() {
if self.branch_names.is_empty() {
vec!["main".to_owned(), "master".to_owned(), "trunk".to_owned()]
} else {
self.default_branches.clone()
self.branch_names.clone()
}
}
}

pub fn copy_untracked(&self) -> bool {
self.copy_untracked.unwrap_or(true)
}
#[derive(Debug, Default, Deserialize, PartialEq, Eq)]
#[serde(default)]
pub struct CloneConfig {
enable_gh: Option<bool>,
}

impl CloneConfig {
pub fn enable_gh(&self) -> bool {
self.enable_gh.unwrap_or(false)
}
}

#[derive(Debug, Default, Deserialize, PartialEq, Eq)]
#[serde(default)]
pub struct AddConfig {
copy_untracked: Option<bool>,
commands: Vec<ShellCommand>,
branch_replacements: Vec<BranchReplacement>,
}

impl AddConfig {
pub fn copy_untracked(&self) -> bool {
self.copy_untracked.unwrap_or(true)
}

pub fn commands(&self) -> &[ShellCommand] {
&self.commands
Expand Down Expand Up @@ -231,33 +247,43 @@ mod tests {
assert_eq!(
default_config,
ConfigFile {
remotes: vec!["upstream".to_owned(), "origin".to_owned(),],
default_branches: vec!["main".to_owned(), "master".to_owned(), "trunk".to_owned(),],
copy_untracked: Some(true),
enable_gh: Some(false),
commands: vec![],
branch_replacements: vec![],
remote_names: vec!["upstream".to_owned(), "origin".to_owned(),],
branch_names: vec!["main".to_owned(), "master".to_owned(), "trunk".to_owned(),],
clone: CloneConfig {
enable_gh: Some(false)
},
add: AddConfig {
copy_untracked: Some(true),
commands: vec![],
branch_replacements: vec![],
}
}
);

let empty_config = toml::from_str::<ConfigFile>("").unwrap();
assert_eq!(
default_config,
ConfigFile {
remotes: empty_config.remotes(),
default_branches: empty_config.default_branches(),
copy_untracked: Some(empty_config.copy_untracked()),
enable_gh: Some(empty_config.enable_gh()),
commands: empty_config
.commands()
.iter()
.map(|command| command.to_owned())
.collect(),
branch_replacements: empty_config
.branch_replacements()
.iter()
.map(|replacement| replacement.to_owned())
.collect()
remote_names: empty_config.remote_names(),
branch_names: empty_config.branch_names(),
clone: CloneConfig {
enable_gh: Some(empty_config.clone.enable_gh()),
},
add: AddConfig {
copy_untracked: Some(empty_config.add.copy_untracked()),
commands: empty_config
.add
.commands()
.iter()
.map(|command| command.to_owned())
.collect(),
branch_replacements: empty_config
.add
.branch_replacements()
.iter()
.map(|replacement| replacement.to_owned())
.collect(),
},
}
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/git/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ where
.map(Some);
}

let preferred_branches = self.0.config.file.default_branches();
let preferred_branches = self.0.config.file.branch_names();
let all_branches = self.0.branch().list_local()?;
for preferred_branch in preferred_branches {
let preferred_branch = LocalBranchRef::new(preferred_branch);
Expand Down
2 changes: 1 addition & 1 deletion src/git/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ where
}
}

let preferred_remotes = self.0.config.file.remotes();
let preferred_remotes = self.0.config.file.remote_names();
for remote in preferred_remotes {
if let Some(remote) = all_remotes.take(&remote) {
sorted.push(remote);
Expand Down
2 changes: 1 addition & 1 deletion src/git/worktree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ where
/// E.g. to convert a repo `~/puppy` with default branch `main`, this will return `main`,
/// to indicate a worktree to be placed in `~/puppy/main`.
pub fn dirname_for<'b>(&self, branch: &'b str) -> Cow<'b, str> {
let branch_replacements = self.0.config.file.branch_replacements();
let branch_replacements = self.0.config.file.add.branch_replacements();
if branch_replacements.is_empty() {
Cow::Borrowed(final_component(branch))
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use test_harness::GitProle;
use test_harness::WorktreeState;

#[test]
fn config_branch_replacements() -> miette::Result<()> {
fn config_add_branch_replacements() -> miette::Result<()> {
let prole = GitProle::new()?;
prole.setup_worktree_repo("my-repo")?;
prole.write_config(
r#"
[[branch_replacements]]
[[add.add_branch_replacements]]
find = '''\w+/\w{1,4}-\d{1,5}-(\w+(?:-\w+){0,2}).*'''
replace = '''$1'''
"#,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ use test_harness::GitProle;
use test_harness::WorktreeState;

#[test]
fn config_branch_replacements_multiple() -> miette::Result<()> {
fn config_add_branch_replacements_multiple() -> miette::Result<()> {
let prole = GitProle::new()?;
prole.setup_worktree_repo("my-repo")?;
prole.write_config(
r#"
[[branch_replacements]]
[add]
[[add_branch_replacements]]
find = '''puppy'''
replace = '''doggy'''
[[branch_replacements]]
[[add_branch_replacements]]
find = '''doggy'''
replace = '''cutie'''
"#,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ use test_harness::GitProle;
use test_harness::WorktreeState;

#[test]
fn config_branch_replacements_count() -> miette::Result<()> {
fn config_add_branch_replacements_count() -> miette::Result<()> {
let prole = GitProle::new()?;
prole.setup_worktree_repo("my-repo")?;
prole.write_config(
r#"
[[branch_replacements]]
[add]
[[add_branch_replacements]]
find = '''puppy'''
replace = '''doggy'''
count = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use test_harness::GitProle;
use test_harness::WorktreeState;

#[test]
fn config_branch_replacements_default() -> miette::Result<()> {
fn config_add_branch_replacements_default() -> miette::Result<()> {
let prole = GitProle::new()?;
prole.setup_worktree_repo("my-repo")?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ use test_harness::GitProle;
use test_harness::WorktreeState;

#[test]
fn config_branch_replacements_multiple() -> miette::Result<()> {
fn config_add_branch_replacements_multiple() -> miette::Result<()> {
let prole = GitProle::new()?;
prole.setup_worktree_repo("my-repo")?;
prole.write_config(
r#"
[[branch_replacements]]
[add]
[[add_branch_replacements]]
find = '''puppy'''
replace = '''doggy'''
[[branch_replacements]]
[[add_branch_replacements]]
find = '''doggy'''
replace = '''cutie'''
"#,
Expand Down
5 changes: 3 additions & 2 deletions tests/config_commands.rs → tests/config_add_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ use test_harness::GitProle;
use test_harness::WorktreeState;

#[test]
fn config_commands() -> miette::Result<()> {
fn config_add_commands() -> miette::Result<()> {
let prole = GitProle::new()?;
prole.setup_worktree_repo("my-repo")?;

prole.write_config(
r#"
commands = [
[add]
add_commands = [
"sh -c 'echo Puppy wuz here > puppy-log'",
{ sh = '''
echo 2wice the Pupyluv >> puppy-log
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use test_harness::GitProle;
use test_harness::WorktreeState;

#[test]
fn config_commands_default() -> miette::Result<()> {
fn config_add_commands_default() -> miette::Result<()> {
let prole = GitProle::new()?;
prole.setup_worktree_repo("my-repo")?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use test_harness::GitProle;
use test_harness::WorktreeState;

#[test]
fn config_copy_untracked() -> miette::Result<()> {
fn config_add_copy_untracked() -> miette::Result<()> {
let prole = GitProle::new()?;

prole.setup_worktree_repo("my-repo")?;

prole.write_config(
"
copy_untracked = false
[add]
add_copy_untracked = false
",
)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use test_harness::GitProle;
use test_harness::WorktreeState;

#[test]
fn config_copy_untracked_default() -> miette::Result<()> {
fn config_add_copy_untracked_default() -> miette::Result<()> {
let prole = GitProle::new()?;

prole.setup_worktree_repo("my-repo")?;
Expand Down
Loading

0 comments on commit dd2322a

Please sign in to comment.