Skip to content

Commit 8402eed

Browse files
dggsaxCopilot
andcommitted
Rename --base flag to --branch on worktree add
The --base flag was ambiguous — it serves as either a branch to check out from the remote or the starting point for a new branch. Rename to --branch which better reflects that worktree add always deals with branches, not arbitrary refs or commits. Also renames the environment variable from LOKI_WORKTREE_BASE to LOKI_WORKTREE_BRANCH for consistency. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0c2bf21 commit 8402eed

3 files changed

Lines changed: 16 additions & 15 deletions

File tree

src/main.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn styles() -> clap::builder::Styles {
3333
.placeholder(AnsiColor::Cyan.on_default())
3434
}
3535

36-
use vars::{LOKI_NEW_PREFIX, LOKI_REBASE_TARGET, LOKI_WORKTREE_BASE, NO_HOOKS};
36+
use vars::{LOKI_NEW_PREFIX, LOKI_REBASE_TARGET, LOKI_WORKTREE_BRANCH, NO_HOOKS};
3737

3838
#[derive(Debug, Parser)]
3939
struct CommitOptions {
@@ -105,9 +105,10 @@ enum WorktreeSubcommand {
105105
#[clap(long, env = LOKI_NEW_PREFIX)]
106106
prefix: Option<String>,
107107

108-
/// Base ref to create the worktree from.
109-
#[clap(short, long, default_value = "origin/main", env = LOKI_WORKTREE_BASE)]
110-
base: String,
108+
/// Branch to check out if it exists on the remote, or the starting
109+
/// point for a new branch.
110+
#[clap(short, long, default_value = "origin/main", env = LOKI_WORKTREE_BRANCH)]
111+
branch: String,
111112

112113
/// Name parts joined with dashes to form the worktree and branch name.
113114
name: Vec<String>,
@@ -232,8 +233,8 @@ fn main() -> Result<(), String> {
232233
command: RepoSubcommand::Stats(options),
233234
} => repo_stats(options),
234235
Cli::Worktree { command } => match command {
235-
WorktreeSubcommand::Add { name, base, prefix } => {
236-
worktree::worktree_add(name, base, prefix.as_deref())
236+
WorktreeSubcommand::Add { name, branch, prefix } => {
237+
worktree::worktree_add(name, branch, prefix.as_deref())
237238
}
238239
WorktreeSubcommand::Remove { name, force } => {
239240
worktree::worktree_remove(name, *force)

src/vars.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/// Environment variable for the branch name prefix used by `new` and `worktree add`.
22
pub const LOKI_NEW_PREFIX: &str = "LOKI_NEW_PREFIX";
33

4-
/// Environment variable for the base ref used by `worktree add`.
5-
pub const LOKI_WORKTREE_BASE: &str = "LOKI_WORKTREE_BASE";
4+
/// Environment variable for the branch used by `worktree add`.
5+
pub const LOKI_WORKTREE_BRANCH: &str = "LOKI_WORKTREE_BRANCH";
66

77
/// Environment variable for the rebase target branch.
88
pub const LOKI_REBASE_TARGET: &str = "LOKI_REBASE_TARGET";

src/worktree.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ fn list_worktree_entries() -> Result<Vec<WorktreeEntry>, String> {
234234
// ---------------------------------------------------------------------------
235235

236236
/// Creates a worktree at `<parent>/<repo>_<name>`, then creates and pushes a
237-
/// branch with optional prefix. If the base ref is an existing remote branch,
237+
/// branch with optional prefix. If the branch is an existing remote branch,
238238
/// checks it out directly instead. Outputs `cd <path>` to stdout for piping.
239-
pub fn worktree_add(name: &[String], base: &str, prefix: Option<&str>) -> Result<(), String> {
239+
pub fn worktree_add(name: &[String], branch: &str, prefix: Option<&str>) -> Result<(), String> {
240240
if name.is_empty() {
241241
return Err(String::from("name cannot be empty."));
242242
}
@@ -250,11 +250,11 @@ pub fn worktree_add(name: &[String], base: &str, prefix: Option<&str>) -> Result
250250
return Err(format!("Worktree path already exists: {wt_path_str}"));
251251
}
252252

253-
// Check if the base is an existing remote branch to check out directly
254-
if let Some(remote_ref) = find_remote_branch(base) {
253+
// Check if the branch is an existing remote branch to check out directly
254+
if let Some(remote_ref) = find_remote_branch(branch) {
255255
eprintln!(
256256
"Found existing branch {} — checking out into worktree",
257-
base.cyan()
257+
branch.cyan()
258258
);
259259
git_command_status_quiet("fetch", vec!["fetch", "origin"])?;
260260
git_command_status_quiet(
@@ -264,7 +264,7 @@ pub fn worktree_add(name: &[String], base: &str, prefix: Option<&str>) -> Result
264264
"add",
265265
"--track",
266266
"-b",
267-
base,
267+
branch,
268268
wt_path_str.as_ref(),
269269
remote_ref.as_str(),
270270
],
@@ -282,7 +282,7 @@ pub fn worktree_add(name: &[String], base: &str, prefix: Option<&str>) -> Result
282282
eprintln!("Creating worktree at {}", wt_path_str.green());
283283
git_command_status_quiet(
284284
"worktree add",
285-
vec!["worktree", "add", wt_path_str.as_ref(), base],
285+
vec!["worktree", "add", wt_path_str.as_ref(), branch],
286286
)?;
287287

288288
std::env::set_current_dir(&wt_path)

0 commit comments

Comments
 (0)