Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework config option names #74

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
98 changes: 62 additions & 36 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,44 +93,60 @@ fn config_file_path(dirs: &BaseDirectories) -> miette::Result<Utf8PathBuf> {
///
/// The default configuration file is accessible as [`Config::DEFAULT`].
#[derive(Debug, Default, Deserialize, PartialEq, Eq)]
#[serde(default)]
#[serde(default, deny_unknown_fields)]
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
6 changes: 3 additions & 3 deletions test-harness/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ pub fn setup_repo_multiple_remotes(
done
git clone "{remote_path}" "{repo}"
cd "{repo}" || exit
git remote add a ../my-remotes/a
git remote add b ../my-remotes/b
git remote add c ../my-remotes/c
git remote add a ../{remote_path}/../a
git remote add b ../{remote_path}/../b
git remote add c ../{remote_path}/../c
"#
))?;

Expand Down
2 changes: 1 addition & 1 deletion tests/add_from_bare_no_worktrees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn add_from_bare_no_worktrees() -> miette::Result<()> {

prole.write_config(
r#"
default_branches = []
branch_names = []
"#,
)?;

Expand Down
2 changes: 1 addition & 1 deletion tests/add_from_container_no_default_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn add_from_container_no_default_branch() -> miette::Result<()> {

prole.write_config(
r#"
default_branches = []
branch_names = []
"#,
)?;

Expand Down
2 changes: 1 addition & 1 deletion tests/add_from_non_worktree_repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn add_from_non_worktree_repo() -> miette::Result<()> {

prole.write_config(
r#"
default_branches = []
branch_names = []
"#,
)?;

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.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,16 @@ 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.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,12 @@ 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.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,16 @@ 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.branch_replacements]]
find = '''puppy'''
replace = '''doggy'''

[[branch_replacements]]
[[add.branch_replacements]]
find = '''doggy'''
replace = '''cutie'''
"#,
Expand Down
3 changes: 2 additions & 1 deletion tests/config_commands.rs → tests/config_add_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ 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#"
[add]
commands = [
"sh -c 'echo Puppy wuz here > puppy-log'",
{ sh = '''
Expand Down
Loading
Loading