git-workon 0.1.1

A git plugin for managing worktrees
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! New command with interactive mode, PR support, and auto-copy.
//!
//! Creates new worktrees with interactive prompts, pull request support, and
//! automatic file copying integration.
//!
//! ## Interactive Prompts
//!
//! When no name is provided:
//! 1. Prompts for branch name using Input widget
//! 2. Prompts for base branch using FuzzySelect (shows local branches + configured default)
//! 3. PR detection still works - user can type `pr#123` at the name prompt
//!
//! Use `--no-interactive` to bypass prompts (required for testing/scripting).
//!
//! ## PR Support Integration
//!
//! Detects PR references in the name and handles them specially:
//! - Parses PR reference (see pr.rs for supported formats)
//! - Auto-detects remote (upstream → origin → first)
//! - Auto-fetches if PR branch not present
//! - Names worktree using `workon.prFormat` config
//! - Sets up tracking to PR head branch
//!
//! Combined with smart routing in main.rs, enables: `git workon #123`
//!
//! ## Automatic File Copying
//!
//! If `workon.autoCopyUntracked=true`:
//! - Copies files from base branch's worktree (or HEAD's worktree if no base)
//! - Uses `workon.copyPattern` patterns (or defaults to `**/*`)
//! - Respects `workon.copyExclude` patterns
//! - Runs after worktree creation, before post-create hooks
//! - Can be overridden with `--(no-)copy-untracked` flags
//!
//! ## Execution Order
//!
//! 1. Create worktree
//! 2. Copy files (if auto-copy enabled)
//! 3. Execute post-create hooks (from hooks.rs)
//!
//! ## gh CLI Integration
//!
//! PR support uses gh CLI for robust metadata handling:
//! - Fetches PR title, author, branch name, and base branch
//! - Supports fork-based PRs by auto-adding fork remotes
//! - Properly sets upstream tracking for PR branches
//! - Enables format placeholders: {number}, {title}, {author}, {branch}

use dialoguer::{FuzzySelect, Input};
use log::debug;
use miette::{bail, IntoDiagnostic, Result, WrapErr};

use crate::cli::New;
use crate::hooks::execute_post_create_hooks;
use crate::output;
use workon::{
    add_worktree, copy_untracked, get_repo, get_worktrees, workon_root, BranchType, CopyOptions,
    WorktreeDescriptor,
};

use super::Run;

// Ability to easily create a worktree with namespcaing.
// Also see: https://lists.mcs.anl.gov/pipermail/petsc-dev/2021-May/027436.html
//
// The anatomy of the command is:
//
//   `git worktree add --track -b <branch> ../<path> <remote>/<remote-branch>`
//
// we want `<branch>` to exactly match `<remote-branch>`
// We want `<path>` to exactly match `<branch>`
//
// Use case: checking out an existing branch
//
//   `git worktree add --track -b bdo/browser-reporter ../bdo/browser-reporter origin/bdo/browser-reporter`
//
// Use case: creating a new branch
// In this case, we aren't tracking a remote (yet?)
//
//   `git worktree add -b lettertwo/some-thing ../lettertwo/some-thing`
//
// Hooks: on creation, we will often want to copy artifacts from the base worktree (e.g., node_modules, build dirs)
// One approach to this is the `copyuntracked` util that can (perhaps interactively?) copy over
// any untracked or git ignored files. It would be nice if this script was also SCM-aware, in that it could
// suggest rebuilds, or re-running install, etc, if the base artifacts are much older than the new worktree HEAD.

impl Run for New {
    fn run(&self) -> Result<Option<WorktreeDescriptor>> {
        let name = match &self.name {
            Some(name) => name.clone(),
            None => {
                if self.no_interactive {
                    bail!("No worktree name provided. Specify a name or remove --no-interactive.");
                }

                // Prompt for branch name
                let name: String = Input::new()
                    .with_prompt("Branch name")
                    .interact_text()
                    .into_diagnostic()
                    .wrap_err("Failed to read branch name")?;

                if name.trim().is_empty() {
                    bail!("Branch name cannot be empty");
                }

                name.trim().to_string()
            }
        };

        let repo = get_repo(None).wrap_err("Failed to find git repository")?;
        let config = workon::WorkonConfig::new(&repo)?;

        // Check if this is a PR reference
        // Only treat as PR if no conflicting flags are provided
        let pr_info = if !self.orphan && !self.detach && self.base.is_none() {
            let info = workon::parse_pr_reference(&name)?;
            if info.is_some() {
                debug!("Detected PR reference in '{}'", name);
            }
            info
        } else {
            debug!("Skipping PR detection (conflicting flags)");
            None
        };

        let (worktree_name, base_branch, branch_type) = if let Some(pr) = pr_info {
            // This is a PR reference - use gh CLI workflow
            let pr_format = config.pr_format(None)?;

            // Phase 1: fetch PR metadata
            let pb = output::create_spinner();
            pb.set_message(format!("Fetching PR #{} metadata...", pr.number));
            let (worktree_name, remote_ref, base_ref) =
                workon::prepare_pr_worktree(&repo, pr.number, &pr_format)
                    .wrap_err(format!("Failed to prepare PR #{} worktree", pr.number))
                    .inspect_err(|_| pb.finish_and_clear())?;
            pb.finish_and_clear();

            // Phase 2: create worktree
            let pb = output::create_spinner();
            pb.set_message("Creating worktree...");
            let worktree =
                add_worktree(&repo, &worktree_name, BranchType::Normal, Some(&remote_ref))
                    .inspect_err(|_| pb.finish_and_clear())?;
            pb.finish_and_clear();

            // Fix upstream tracking
            // remote_ref is in format "remote/branch" - extract both parts
            let parts: Vec<&str> = remote_ref.split('/').collect();
            let remote_name = parts.first().copied().unwrap_or("origin");
            let branch_name = parts[1..].join("/"); // Handle branches with slashes
            let branch_ref = format!("refs/heads/{}", branch_name);
            workon::set_upstream_tracking(&worktree, remote_name, &branch_ref)
                .wrap_err("Failed to set upstream tracking for PR branch")?;

            // Copy files if configured
            let copy_override = if self.copy_untracked {
                Some(true)
            } else if self.no_copy_untracked {
                Some(false)
            } else {
                None
            };

            if config.auto_copy_untracked(copy_override)? {
                if let Err(e) = copy_untracked_files(
                    &repo,
                    &worktree,
                    Some(&base_ref),
                    &config,
                    self.copy_ignored,
                ) {
                    output::warn(&format!("Failed to copy untracked files: {}", e));
                }
            }

            // Execute post-create hooks
            if !self.no_hooks {
                if let Err(e) = execute_post_create_hooks(&worktree, Some(&base_ref), &config) {
                    output::warn(&format!("Post-create hook failed: {}", e));
                }
            }

            return Ok(Some(worktree));
        } else {
            // Regular worktree creation

            // Determine base branch
            let base_branch = if let Some(base) = &self.base {
                debug!("Using explicit base branch: {}", base);
                config.default_branch(Some(base))?
            } else if !self.no_interactive && self.name.is_none() {
                // Interactive mode: prompt for base branch
                debug!("Prompting for base branch (interactive mode)");
                prompt_for_base_branch(&repo, &config)?
            } else {
                debug!("Using default base branch from config");
                config.default_branch(None)?
            };

            let branch_type = if self.orphan {
                BranchType::Orphan
            } else if self.detach {
                BranchType::Detached
            } else {
                BranchType::Normal
            };

            (name, base_branch, branch_type)
        };

        let worktree = add_worktree(&repo, &worktree_name, branch_type, base_branch.as_deref())
            .wrap_err(format!("Failed to create worktree '{}'", worktree_name))?;

        // Copy untracked files if enabled
        let copy_override = if self.copy_untracked {
            Some(true)
        } else if self.no_copy_untracked {
            Some(false)
        } else {
            None
        };

        if config.auto_copy_untracked(copy_override)? {
            debug!("Auto-copy enabled, copying from base worktree");
            if let Err(e) = copy_untracked_files(
                &repo,
                &worktree,
                base_branch.as_deref(),
                &config,
                self.copy_ignored,
            ) {
                output::warn(&format!("Failed to copy untracked files: {}", e));
                // Continue - worktree is still valid
            }
        } else {
            debug!("Auto-copy disabled");
        }

        // Execute post-create hooks after successful worktree creation
        if !self.no_hooks {
            debug!("Executing post-create hooks");
            if let Err(e) = execute_post_create_hooks(&worktree, base_branch.as_deref(), &config) {
                output::warn(&format!("Post-create hook failed: {}", e));
                // Continue - worktree is still valid
            }
        } else {
            debug!("Hooks skipped (--no-hooks)");
        }

        Ok(Some(worktree))
    }
}

/// Prompt user to select a base branch from available branches
fn prompt_for_base_branch(
    repo: &git2::Repository,
    config: &workon::WorkonConfig,
) -> Result<Option<String>> {
    let branches = repo
        .branches(Some(git2::BranchType::Local))
        .into_diagnostic()?;

    let branch_names: Vec<String> = branches
        .filter_map(|b| {
            b.ok()
                .and_then(|(branch, _)| branch.name().ok().flatten().map(|s| s.to_string()))
        })
        .collect();

    if branch_names.is_empty() {
        return config.default_branch(None).map_err(Into::into);
    }

    let default_branch = config
        .default_branch(None)?
        .unwrap_or_else(|| "main".to_string());
    let mut items = vec![format!("<default: {}>", default_branch)];
    items.extend(branch_names.iter().cloned());

    let selection = FuzzySelect::new()
        .with_prompt("Base branch")
        .items(&items)
        .default(0)
        .interact()
        .into_diagnostic()
        .wrap_err("Failed to select base branch")?;

    if selection == 0 {
        Ok(Some(default_branch))
    } else {
        Ok(Some(branch_names[selection - 1].clone()))
    }
}

/// Copy untracked files from the base worktree to the new worktree
fn copy_untracked_files(
    repo: &git2::Repository,
    worktree: &WorktreeDescriptor,
    base_branch: Option<&str>,
    config: &workon::WorkonConfig,
    include_ignored: bool,
) -> Result<()> {
    let patterns = config.copy_patterns()?;
    let excludes = config.copy_excludes()?;
    let include_ignored = config.copy_include_ignored(Some(include_ignored).filter(|&v| v))?;

    // Determine source branch name: explicit base, or HEAD's branch
    let source_branch_name = if let Some(base) = base_branch {
        base.to_string()
    } else {
        match repo.head() {
            Ok(head) => match head.shorthand() {
                Some(s) => s.to_string(),
                None => return Ok(()), // detached HEAD, skip
            },
            Err(_) => return Ok(()), // can't determine HEAD, skip
        }
    };

    // Find source worktree path via libgit2 worktree list, then fall back to root join.
    // Using get_worktrees avoids breaking on slashed branch names that differ from the
    // worktree's filesystem path, or on PR base refs like "origin/main".
    let source_path = find_worktree_path(repo, &source_branch_name)?;

    let Some(source_path) = source_path else {
        // Source worktree doesn't exist, skip copying
        return Ok(());
    };

    let dest_path = worktree.path().to_path_buf();

    let json_mode = output::is_json_mode();
    let pb = output::create_spinner();
    pb.set_message("Copying files...");

    let mut count = 0usize;
    let pb_copied = pb.clone();
    let copied = copy_untracked(
        &source_path,
        &dest_path,
        CopyOptions {
            patterns: &patterns,
            excludes: &excludes,
            include_ignored,
            on_copied: Box::new(move |rel_path| {
                if !json_mode {
                    count += 1;
                    pb_copied.println(format!(
                        "      {} {}",
                        output::style::green_bold("Copied"),
                        rel_path.display()
                    ));
                    pb_copied.set_message(format!("Copying files... ({} copied)", count));
                }
            }),
            ..Default::default()
        },
    )?;

    pb.finish_and_clear();
    if !copied.is_empty() {
        output::success(&format!(
            "Copied {} file(s) from base worktree",
            copied.len()
        ));
    }

    Ok(())
}

/// Find the filesystem path of a worktree by branch name or worktree name.
///
/// Checks registered worktrees first (handles any naming scheme), then falls back
/// to `root/<name>` which covers the common case of branch-named worktrees.
fn find_worktree_path(
    repo: &git2::Repository,
    branch_name: &str,
) -> Result<Option<std::path::PathBuf>> {
    // Strip remote prefix (e.g., "origin/main" → "main") for matching against branch names
    let local_name = branch_name
        .split_once('/')
        .map(|(_, b)| b)
        .unwrap_or(branch_name);

    if let Ok(worktrees) = get_worktrees(repo) {
        for wt in worktrees {
            // Match by worktree name
            if wt.name() == Some(branch_name) || wt.name() == Some(local_name) {
                return Ok(Some(wt.path().to_path_buf()));
            }
            // Match by branch name
            if let Ok(Some(branch)) = wt.branch() {
                if branch == branch_name || branch == local_name {
                    return Ok(Some(wt.path().to_path_buf()));
                }
            }
        }
    }

    // Fall back to root/<local_name> (the standard git-workon layout)
    let root = workon_root(repo)?;
    let path = root.join(local_name);
    Ok(path.exists().then_some(path))
}