git-workflow 0.4.1

Git guardrails for AI coding agents - safe git workflows with clear state feedback
Documentation
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
//! `gw worktree pool` commands — Per-leader worktree pool management
//!
//! Each leader worktree owns its own pool under `.worktrees/`.
//! Pool state is derived from the filesystem, not from an inventory file.
//! Marker files in the per-worktree git dir track acquisition state.

use std::path::{Path, PathBuf};

use crate::error::{GwError, Result};
use crate::git;
use crate::output;
use crate::pool::{PoolEntry, PoolLock, PoolNextAction, PoolState, WorktreeStatus};

/// Directory name under the per-worktree git dir for pool metadata
const POOL_META_DIR: &str = "pool";

/// Directory name under worktree root for pool worktrees
const POOL_WORKTREES_DIR: &str = ".worktrees";

/// Setup hook path relative to repo root
const SETUP_HOOK: &str = ".gw/setup";

/// Subdirectory for acquire markers
const ACQUIRED_DIR: &str = "acquired";

/// Canonicalize a path, stripping the `\\?\` prefix on Windows so that
/// external tools (like git) can consume the path without issues.
fn canonicalize_clean(path: &Path) -> PathBuf {
    let canonical = std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
    #[cfg(target_os = "windows")]
    {
        let s = canonical.to_string_lossy();
        if let Some(stripped) = s.strip_prefix(r"\\?\") {
            return PathBuf::from(stripped);
        }
    }
    canonical
}

/// Get the current worktree root (works from main repo or any worktree)
fn leader_root() -> Result<PathBuf> {
    let root = git::worktree_root()?;
    Ok(canonicalize_clean(&root))
}

/// Get the leader name (the worktree directory name, e.g., "web-2").
/// Sanitizes the name to be valid as part of a git branch name.
fn leader_name() -> Result<String> {
    let root = leader_root()?;
    let raw = root
        .file_name()
        .and_then(|s| s.to_str())
        .map(String::from)
        .ok_or_else(|| GwError::Other("Could not determine leader name".to_string()))?;
    // Strip leading dots (invalid in git branch names)
    let sanitized = raw.trim_start_matches('.');
    if sanitized.is_empty() {
        return Err(GwError::Other(format!(
            "Leader directory name is not valid for branch naming: {raw}"
        )));
    }
    Ok(sanitized.to_string())
}

/// Pool entry name prefix for the current leader (e.g., "web-2-pool-")
fn pool_prefix() -> Result<String> {
    Ok(format!("{}-pool-", leader_name()?))
}

/// Get the main repository root (parent of .git), even from inside a worktree.
fn main_repo_root() -> Result<PathBuf> {
    let common = git::git_common_dir()?;
    let common = canonicalize_clean(&common);
    common
        .parent()
        .map(|p| p.to_path_buf())
        .ok_or_else(|| GwError::Other("Could not determine main repository root".to_string()))
}

/// Resolve the pool metadata directory (per-worktree: {git_dir}/pool/)
fn pool_dir() -> Result<PathBuf> {
    let git_dir = git::git_dir()?;
    let git_dir = canonicalize_clean(&git_dir);
    Ok(git_dir.join(POOL_META_DIR))
}

/// Resolve the acquired markers directory
fn acquired_dir() -> Result<PathBuf> {
    Ok(pool_dir()?.join(ACQUIRED_DIR))
}

/// Resolve the worktrees directory ({leader_root}/.worktrees/)
fn worktrees_dir() -> Result<PathBuf> {
    let root = leader_root()?;
    Ok(root.join(POOL_WORKTREES_DIR))
}

/// Run the setup hook if it exists
fn run_setup_hook(repo_root: &Path, worktree_path: &str, verbose: bool) -> Result<()> {
    let hook = repo_root.join(SETUP_HOOK);
    if !hook.exists() {
        return Ok(());
    }

    if verbose {
        output::action(&format!("Running setup hook: {}", hook.display()));
    }

    let status = std::process::Command::new(&hook)
        .arg(worktree_path)
        .current_dir(worktree_path)
        .status()?;

    if !status.success() {
        return Err(GwError::Other(format!(
            "Setup hook failed with exit code: {}",
            status.code().unwrap_or(-1)
        )));
    }
    Ok(())
}

/// Get the current branch of a worktree by running git in that directory
fn worktree_current_branch(path: &Path) -> String {
    let path_str = path.to_string_lossy();
    std::process::Command::new("git")
        .args(["rev-parse", "--abbrev-ref", "HEAD"])
        .current_dir(&*path_str)
        .output()
        .ok()
        .filter(|o| o.status.success())
        .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
        .unwrap_or_else(|| "???".to_string())
}

/// Ensure `.worktrees/` is excluded via `.git/info/exclude`.
/// This is local-only and never pollutes `.gitignore` or the working tree.
fn ensure_excluded() -> Result<()> {
    let common = git::git_common_dir()?;
    let exclude_path = common.join("info").join("exclude");
    let entry = ".worktrees/";

    if exclude_path.exists() {
        let content = std::fs::read_to_string(&exclude_path)?;
        if content.lines().any(|line| line.trim() == entry) {
            return Ok(());
        }
        let prefix = if content.ends_with('\n') { "" } else { "\n" };
        std::fs::write(&exclude_path, format!("{content}{prefix}{entry}\n"))?;
    } else {
        std::fs::create_dir_all(common.join("info"))?;
        std::fs::write(&exclude_path, format!("{entry}\n"))?;
    }

    Ok(())
}

/// Release a single pool worktree back to the pool.
///
/// Just removes the acquire marker. The worktree should have been
/// cleaned up (gw cleanup) before release.
fn release_one(entry: &PoolEntry, acquired_dir: &Path) -> Result<()> {
    let marker = acquired_dir.join(&entry.name);
    if marker.exists() {
        std::fs::remove_file(&marker)?;
    }
    output::success(&format!("{} released", entry.name));
    Ok(())
}

// --- Pool commands ---

/// `gw worktree pool warm <n>`
pub fn warm(count: usize, verbose: bool) -> Result<()> {
    if !git::is_git_repo() {
        return Err(GwError::NotAGitRepository);
    }

    let pool_dir = pool_dir()?;
    let wt_dir = worktrees_dir()?;
    let acquired_dir = acquired_dir()?;
    let repo_root = main_repo_root()?;
    let prefix = pool_prefix()?;

    println!();
    output::info(&format!(
        "Warming worktree pool to {} available",
        output::bold(&count.to_string())
    ));

    // Acquire lock and scan filesystem
    let _lock = PoolLock::acquire(&pool_dir)?;
    let mut state = PoolState::scan(&wt_dir, &acquired_dir, &prefix)?;

    let available = state.count_by_status(&WorktreeStatus::Available);
    let acquired = state.count_by_status(&WorktreeStatus::Acquired);
    let total = state.entries.len();
    if available >= count {
        output::success(&format!(
            "Pool already has {available} available ({acquired} acquired, {total} total), nothing to do"
        ));
        return Ok(());
    }

    let to_create = count - available;

    // Ensure .worktrees/ is excluded locally (via .git/info/exclude)
    ensure_excluded()?;

    // Fetch once
    output::info("Fetching from origin...");
    git::fetch_prune(verbose)?;
    output::success("Fetched");

    let default_remote = git::get_default_remote_branch()?;

    // Create worktrees dir
    std::fs::create_dir_all(&wt_dir)?;

    let mut created = 0;
    for i in 0..to_create {
        let name = state.next_name(&prefix);
        let abs_path = canonicalize_clean(&wt_dir).join(&name);
        let abs_path_str = abs_path.to_string_lossy().to_string();
        // Branch name = directory name (gw convention: dir name = home branch)
        let branch = name.clone();

        output::info(&format!(
            "[{}/{}] Creating {}...",
            i + 1,
            to_create,
            output::bold(&name)
        ));

        // Create the worktree
        if let Err(e) = git::worktree_add(&abs_path_str, &branch, &default_remote, verbose) {
            output::warn(&format!("Failed to create {name}: {e}"));
            continue;
        }

        // Run setup hook
        if let Err(e) = run_setup_hook(&repo_root, &abs_path_str, verbose) {
            output::warn(&format!(
                "Setup hook failed for {name}: {e}. Removing worktree."
            ));
            let _ = git::worktree_remove(&abs_path_str, verbose);
            let _ = git::force_delete_branch(&branch, verbose);
            continue;
        }

        // Track in-memory for next_name() to work correctly
        state.entries.push(PoolEntry {
            name: name.clone(),
            path: abs_path,
            branch,
            status: WorktreeStatus::Available,
            owner: None,
        });
        created += 1;

        output::success(&format!("[{}/{}] Created {}", i + 1, to_create, name));
    }

    // Re-scan for accurate final counts
    let final_state = PoolState::scan(&wt_dir, &acquired_dir, &prefix)?;
    let total = final_state.entries.len();
    let available = final_state.count_by_status(&WorktreeStatus::Available);

    println!();
    output::success(&format!(
        "Pool warmed: {created} created, {available} available, {total} total"
    ));

    Ok(())
}

/// `gw worktree pool acquire`
pub fn acquire(verbose: bool) -> Result<()> {
    if !git::is_git_repo() {
        return Err(GwError::NotAGitRepository);
    }

    let pool_dir = pool_dir()?;
    let wt_dir = worktrees_dir()?;
    let acquired_dir = acquired_dir()?;
    let prefix = pool_prefix()?;

    if !wt_dir.exists() {
        return Err(GwError::PoolNotInitialized);
    }

    let _lock = PoolLock::acquire(&pool_dir)?;

    // Ensure acquired dir exists
    std::fs::create_dir_all(&acquired_dir)?;

    let state = PoolState::scan(&wt_dir, &acquired_dir, &prefix)?;

    if state.entries.is_empty() {
        return Err(GwError::PoolNotInitialized);
    }

    let entry = state.find_available().ok_or(GwError::PoolExhausted)?;

    // Create marker file with leader name as owner
    let owner = leader_name()?;
    std::fs::write(acquired_dir.join(&entry.name), &owner)?;

    // Sync worktree to latest (gw home equivalent)
    let wt_path = entry.path.to_string_lossy().to_string();
    git::git_run_in_dir(&wt_path, &["fetch", "--prune"], verbose)?;
    let default_remote = git::get_default_remote_branch()?;
    let default_branch = default_remote.strip_prefix("origin/").unwrap_or("main");
    git::git_run_in_dir(&wt_path, &["pull", "origin", default_branch], verbose)?;

    let path = entry.path.to_string_lossy().to_string();
    let name = entry.name.clone();

    let remaining = state.count_by_status(&WorktreeStatus::Available) - 1;
    eprintln!(
        "\x1b[0;32m\u{2713}\x1b[0m Acquired {} ({} remaining)",
        name, remaining,
    );

    // Print ONLY the path to stdout for `path=$(gw worktree pool acquire)`
    println!("{path}");

    Ok(())
}

/// `gw worktree pool release [name]`
///
/// Removes acquire markers. No git operations — cleanup should have
/// been run inside the worktree before releasing.
pub fn release(name: Option<String>, _verbose: bool) -> Result<()> {
    if !git::is_git_repo() {
        return Err(GwError::NotAGitRepository);
    }

    let pool_dir = pool_dir()?;
    let wt_dir = worktrees_dir()?;
    let acquired_dir = acquired_dir()?;
    let prefix = pool_prefix()?;

    if !wt_dir.exists() {
        return Err(GwError::PoolNotInitialized);
    }

    let _lock = PoolLock::acquire(&pool_dir)?;
    let state = PoolState::scan(&wt_dir, &acquired_dir, &prefix)?;

    if state.entries.is_empty() {
        return Err(GwError::PoolNotInitialized);
    }

    match name {
        Some(ref n) => {
            let entry = state
                .find_by_name_or_path(n)
                .ok_or_else(|| GwError::PoolWorktreeNotFound(n.clone()))?;

            if entry.status != WorktreeStatus::Acquired {
                return Err(GwError::PoolWorktreeNotAcquired(entry.name.clone()));
            }

            release_one(entry, &acquired_dir)?;
        }
        None => {
            let acquired: Vec<_> = state
                .entries
                .iter()
                .filter(|e| e.status == WorktreeStatus::Acquired)
                .collect();

            if acquired.is_empty() {
                return Err(GwError::PoolNoneAcquired);
            }

            for entry in &acquired {
                release_one(entry, &acquired_dir)?;
            }
        }
    }

    // Re-scan for final counts
    let final_state = PoolState::scan(&wt_dir, &acquired_dir, &prefix)?;
    let available = final_state.count_by_status(&WorktreeStatus::Available);
    let acquired_count = final_state.count_by_status(&WorktreeStatus::Acquired);
    let total = final_state.entries.len();

    println!();
    output::success(&format!(
        "Pool: {} available, {} acquired, {} total",
        available, acquired_count, total
    ));

    Ok(())
}

/// `gw worktree pool status`
pub fn status(verbose: bool) -> Result<()> {
    if !git::is_git_repo() {
        return Err(GwError::NotAGitRepository);
    }

    let wt_dir = worktrees_dir()?;
    let acquired_dir = acquired_dir()?;
    let prefix = pool_prefix()?;

    if !wt_dir.exists() {
        return Err(GwError::PoolNotInitialized);
    }

    // Read-only — no lock needed
    let state = PoolState::scan(&wt_dir, &acquired_dir, &prefix)?;

    if state.entries.is_empty() {
        return Err(GwError::PoolNotInitialized);
    }

    let available = state.count_by_status(&WorktreeStatus::Available);
    let acquired = state.count_by_status(&WorktreeStatus::Acquired);
    let total = state.entries.len();

    println!();
    output::info(&format!(
        "Pool: {} available, {} acquired, {} total",
        output::bold(&available.to_string()),
        output::bold(&acquired.to_string()),
        output::bold(&total.to_string()),
    ));

    if acquired > 0 {
        println!();
        let header = format!("{:<24} {}", "NAME", "BRANCH");
        println!("{header}");
        println!("{}", "-".repeat(48));

        for entry in &state.entries {
            if entry.status != WorktreeStatus::Acquired {
                continue;
            }
            let branch = worktree_current_branch(&entry.path);
            let branch_display = if branch == entry.name {
                "(idle)".to_string()
            } else {
                branch
            };
            println!("{:<24} {}", entry.name, branch_display);
            println!("    {}", entry.path.display());
        }
    }

    if verbose {
        // --verbose: show all entries
        println!();
        output::info("All entries:");
        println!();
        let header = format!("{:<24} {:<12} {:<24}", "NAME", "STATUS", "BRANCH");
        println!("{header}");
        println!("{}", "-".repeat(60));

        for entry in &state.entries {
            let branch = if entry.status == WorktreeStatus::Acquired {
                worktree_current_branch(&entry.path)
            } else {
                entry.branch.clone()
            };
            println!("{:<24} {:<12} {}", entry.name, entry.status, branch);
        }
    }

    // Show next action
    let next = state.next_action();
    println!();
    display_pool_next_action(&next);

    println!();
    Ok(())
}

fn display_pool_next_action(action: &PoolNextAction) {
    match action {
        PoolNextAction::WarmPool => {
            output::action("Next: warm the pool");
            println!("  gw worktree pool warm <count>");
        }
        PoolNextAction::Ready { available } => {
            output::action(&format!("Ready: {} worktree(s) available", available));
            println!("  gw worktree pool acquire");
            println!("  gw worktree pool release [name]");
        }
        PoolNextAction::Exhausted { acquired } => {
            output::action(&format!(
                "All {} worktree(s) acquired. Release or warm more.",
                acquired
            ));
            println!("  gw worktree pool release [name]");
            println!("  gw worktree pool warm <count>");
        }
        PoolNextAction::AllIdle { available } => {
            output::action(&format!(
                "All {} worktree(s) idle. Acquire or drain.",
                available
            ));
            println!("  gw worktree pool acquire");
            println!("  gw worktree pool drain");
        }
    }
}

/// `gw worktree pool drain [--force]`
pub fn drain(force: bool, verbose: bool) -> Result<()> {
    if !git::is_git_repo() {
        return Err(GwError::NotAGitRepository);
    }

    let pool_dir = pool_dir()?;
    let wt_dir = worktrees_dir()?;
    let acquired_dir = acquired_dir()?;
    let prefix = pool_prefix()?;
    // Resolve all paths upfront — the cwd might be inside a pool worktree
    // that we're about to delete.
    let leader = leader_root()?;
    let leader_str = leader.to_string_lossy().to_string();

    if !wt_dir.exists() {
        return Err(GwError::PoolNotInitialized);
    }

    println!();
    output::info("Draining worktree pool...");

    let _lock = PoolLock::acquire(&pool_dir)?;
    let state = PoolState::scan(&wt_dir, &acquired_dir, &prefix)?;

    if state.entries.is_empty() {
        return Err(GwError::PoolNotInitialized);
    }

    // Check for acquired worktrees
    let acquired = state.count_by_status(&WorktreeStatus::Acquired);
    if acquired > 0 && !force {
        return Err(GwError::PoolHasAcquiredWorktrees(acquired));
    }

    let total = state.entries.len();

    for (i, entry) in state.entries.iter().enumerate() {
        output::info(&format!(
            "[{}/{}] Removing {}...",
            i + 1,
            total,
            output::bold(&entry.name)
        ));

        let path_str = entry.path.to_string_lossy().to_string();

        // Remove the worktree (run from leader root so it works even if cwd is deleted)
        if let Err(e) = git::git_run_in_dir(
            &leader_str,
            &["worktree", "remove", "--force", &path_str],
            verbose,
        ) {
            output::warn(&format!("Failed to remove worktree {}: {e}", entry.name));
            let _ = std::fs::remove_dir_all(&entry.path);
        }

        // Delete the pool branch
        if let Err(e) = git::git_run_in_dir(&leader_str, &["branch", "-D", &entry.branch], verbose)
        {
            output::warn(&format!("Failed to delete branch {}: {e}", entry.branch));
        }

        // Remove acquired marker if present
        let marker = acquired_dir.join(&entry.name);
        let _ = std::fs::remove_file(&marker);

        output::success(&format!("[{}/{}] Removed {}", i + 1, total, entry.name));
    }

    // Clean up pool metadata
    if acquired_dir.exists() {
        let _ = std::fs::remove_dir_all(&acquired_dir);
    }
    let _ = std::fs::remove_file(pool_dir.join("pool.lock"));

    // Prune worktree references
    git::git_run_in_dir(&leader_str, &["worktree", "prune"], verbose)?;

    // Remove empty directories
    if wt_dir.exists() {
        let _ = std::fs::remove_dir(&wt_dir);
    }
    drop(_lock);
    let _ = std::fs::remove_dir(&pool_dir);

    println!();
    output::success(&format!("Drained {total} worktree(s) from pool"));

    Ok(())
}