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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
//! Integration tests for `gw worktree pool` commands
//!
//! Pool worktrees are now per-leader: each worktree creates its own pool
//! under `.worktrees/` with names prefixed by the leader name.

use std::path::Path;
use std::process::{Command, Output};

use regex::Regex;
use tempfile::TempDir;

/// Strip ANSI escape codes from a string
fn strip_ansi(s: &str) -> String {
    let re = Regex::new(r"\x1b\[[0-9;]*m").unwrap();
    re.replace_all(s, "").to_string()
}

/// Create a bare repository to use as "origin"
fn create_origin_repo() -> TempDir {
    let dir = TempDir::new().expect("Failed to create temp dir");
    run_git(dir.path(), &["init", "--bare", "--initial-branch=main"]);
    dir
}

/// Create a local repository with origin configured
fn create_local_repo(origin_path: &Path) -> TempDir {
    let dir = TempDir::new().expect("Failed to create temp dir");

    run_git(dir.path(), &["init"]);
    run_git(dir.path(), &["config", "user.email", "test@example.com"]);
    run_git(dir.path(), &["config", "user.name", "Test User"]);
    run_git(dir.path(), &["checkout", "-b", "main"]);

    std::fs::write(dir.path().join("README.md"), "# Test").expect("Failed to write file");
    run_git(dir.path(), &["add", "."]);
    run_git(dir.path(), &["commit", "-m", "Initial commit"]);

    let origin_url = format!("file://{}", origin_path.display());
    run_git(dir.path(), &["remote", "add", "origin", &origin_url]);
    run_git(dir.path(), &["push", "-u", "origin", "main"]);

    dir
}

/// Run a git command in a specific directory
fn run_git(dir: &Path, args: &[&str]) -> String {
    let output = Command::new("git")
        .args(args)
        .current_dir(dir)
        .output()
        .expect("Failed to run git command");

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        panic!("git {} failed: {}", args.join(" "), stderr);
    }

    String::from_utf8_lossy(&output.stdout).trim().to_string()
}

/// Run gw command in a specific directory
fn run_gw(dir: &Path, args: &[&str]) -> Output {
    let gw_path = env!("CARGO_BIN_EXE_gw");
    Command::new(gw_path)
        .args(args)
        .current_dir(dir)
        .env("NO_COLOR", "1")
        .output()
        .expect("Failed to run gw command")
}

/// Get stdout from output as stripped string
fn stdout_str(output: &Output) -> String {
    strip_ansi(&String::from_utf8_lossy(&output.stdout))
}

/// Get stderr from output as stripped string
fn stderr_str(output: &Output) -> String {
    strip_ansi(&String::from_utf8_lossy(&output.stderr))
}

/// Assert command succeeded, panic with stderr if not
fn assert_success(output: &Output, context: &str) {
    assert!(
        output.status.success(),
        "{context} failed (exit {}):\nstdout: {}\nstderr: {}",
        output.status.code().unwrap_or(-1),
        stdout_str(output),
        stderr_str(output),
    );
}

/// Get the leader name as gw computes it (dir name with leading dots stripped)
fn leader_name_for(path: &Path) -> String {
    let raw = path.file_name().unwrap().to_string_lossy().to_string();
    raw.trim_start_matches('.').to_string()
}

// --- warm ---

#[test]
fn test_warm_creates_worktrees() {
    let origin = create_origin_repo();
    let local = create_local_repo(origin.path());
    let leader = leader_name_for(local.path());
    let prefix = format!("{leader}-pool-");

    let output = run_gw(local.path(), &["worktree", "pool", "warm", "3"]);
    assert_success(&output, "warm 3");

    let out = stdout_str(&output);
    assert!(
        out.contains(&format!("Created {prefix}001")),
        "output: {out}"
    );
    assert!(
        out.contains(&format!("Created {prefix}002")),
        "output: {out}"
    );
    assert!(
        out.contains(&format!("Created {prefix}003")),
        "output: {out}"
    );
    assert!(
        out.contains("3 created, 3 available, 3 total"),
        "output: {out}"
    );

    // Verify worktree directories exist
    assert!(
        local
            .path()
            .join(format!(".worktrees/{prefix}001"))
            .exists()
    );
    assert!(
        local
            .path()
            .join(format!(".worktrees/{prefix}002"))
            .exists()
    );
    assert!(
        local
            .path()
            .join(format!(".worktrees/{prefix}003"))
            .exists()
    );

    // Verify branches were created with leader prefix
    let branches = run_git(local.path(), &["branch", "--list", &format!("{prefix}*")]);
    assert!(
        branches.contains(&format!("{prefix}001")),
        "branches: {branches}"
    );
    assert!(
        branches.contains(&format!("{prefix}002")),
        "branches: {branches}"
    );
    assert!(
        branches.contains(&format!("{prefix}003")),
        "branches: {branches}"
    );

    // Verify .worktrees/ was added to .git/info/exclude (not .gitignore)
    let exclude = std::fs::read_to_string(local.path().join(".git/info/exclude"))
        .expect(".git/info/exclude should exist");
    assert!(
        exclude.contains(".worktrees/"),
        ".git/info/exclude should contain .worktrees/: {exclude}"
    );
    // .gitignore should NOT be modified
    let gitignore = std::fs::read_to_string(local.path().join(".gitignore")).unwrap_or_default();
    assert!(
        !gitignore.contains(".worktrees/"),
        ".gitignore should not contain .worktrees/: {gitignore}"
    );
}

#[test]
fn test_warm_exclude_idempotent() {
    let origin = create_origin_repo();
    let local = create_local_repo(origin.path());

    // Warm twice
    run_gw(local.path(), &["worktree", "pool", "warm", "1"]);
    run_gw(local.path(), &["worktree", "pool", "warm", "2"]);

    // .worktrees/ should appear exactly once in .git/info/exclude
    let exclude = std::fs::read_to_string(local.path().join(".git/info/exclude"))
        .expect(".git/info/exclude should exist");
    let count = exclude
        .lines()
        .filter(|l| l.trim() == ".worktrees/")
        .count();
    assert_eq!(count, 1, ".worktrees/ should appear once: {exclude}");
}

#[test]
fn test_warm_is_idempotent() {
    let origin = create_origin_repo();
    let local = create_local_repo(origin.path());

    // Warm to 2
    let output = run_gw(local.path(), &["worktree", "pool", "warm", "2"]);
    assert_success(&output, "warm 2");

    // Warm to 2 again — should do nothing
    let output = run_gw(local.path(), &["worktree", "pool", "warm", "2"]);
    assert_success(&output, "warm 2 (idempotent)");

    let out = stdout_str(&output);
    assert!(out.contains("already has 2 available"), "output: {out}");
}

#[test]
fn test_warm_incremental() {
    let origin = create_origin_repo();
    let local = create_local_repo(origin.path());

    // Warm to 1
    let output = run_gw(local.path(), &["worktree", "pool", "warm", "1"]);
    assert_success(&output, "warm 1");

    // Warm to 3 — should create 2 more
    let output = run_gw(local.path(), &["worktree", "pool", "warm", "3"]);
    assert_success(&output, "warm 3");

    let out = stdout_str(&output);
    assert!(
        out.contains("2 created, 3 available, 3 total"),
        "output: {out}"
    );
}

// --- acquire ---

#[test]
fn test_acquire_prints_path_to_stdout() {
    let origin = create_origin_repo();
    let local = create_local_repo(origin.path());
    let leader = leader_name_for(local.path());
    let prefix = format!("{leader}-pool-");

    run_gw(local.path(), &["worktree", "pool", "warm", "1"]);

    let output = run_gw(local.path(), &["worktree", "pool", "acquire"]);
    assert_success(&output, "acquire");

    // stdout should contain exactly the worktree path (plus newline)
    let path = String::from_utf8_lossy(&output.stdout).trim().to_string();
    let path_normalized = path.replace('\\', "/");
    assert!(
        path_normalized.ends_with(&format!(".worktrees/{prefix}001")),
        "Expected worktree path, got: {path}"
    );
    assert!(
        Path::new(&path).exists(),
        "Acquired path should exist: {path}"
    );

    // stderr should show the acquire info
    let err = stderr_str(&output);
    assert!(
        err.contains(&format!("Acquired {prefix}001")),
        "stderr: {err}"
    );
    assert!(err.contains("0 remaining"), "stderr: {err}");
}

#[test]
fn test_acquire_fails_when_exhausted() {
    let origin = create_origin_repo();
    let local = create_local_repo(origin.path());

    run_gw(local.path(), &["worktree", "pool", "warm", "1"]);

    // Acquire the only one
    let output = run_gw(local.path(), &["worktree", "pool", "acquire"]);
    assert_success(&output, "acquire 1");

    // Second acquire should fail
    let output = run_gw(local.path(), &["worktree", "pool", "acquire"]);
    assert!(
        !output.status.success(),
        "Expected acquire to fail when pool is exhausted"
    );

    let err = stderr_str(&output);
    assert!(err.contains("No available worktrees"), "stderr: {err}");
}

#[test]
fn test_acquire_fails_when_not_initialized() {
    let origin = create_origin_repo();
    let local = create_local_repo(origin.path());

    let output = run_gw(local.path(), &["worktree", "pool", "acquire"]);
    assert!(!output.status.success());

    let err = stderr_str(&output);
    assert!(err.contains("not initialized"), "stderr: {err}");
}

// --- status ---

#[test]
fn test_status_shows_pool_info() {
    let origin = create_origin_repo();
    let local = create_local_repo(origin.path());
    let leader = leader_name_for(local.path());
    let prefix = format!("{leader}-pool-");

    run_gw(local.path(), &["worktree", "pool", "warm", "2"]);
    run_gw(local.path(), &["worktree", "pool", "acquire"]);

    let output = run_gw(local.path(), &["worktree", "pool", "status"]);
    assert_success(&output, "status");

    let out = stdout_str(&output);
    // Summary line shows counts
    assert!(out.contains("1 available"), "output: {out}");
    assert!(out.contains("1 acquired"), "output: {out}");
    assert!(out.contains("2 total"), "output: {out}");
    // Default: shows acquired worktrees
    assert!(out.contains(&format!("{prefix}001")), "output: {out}");
    assert!(
        out.contains("BRANCH"),
        "output should have BRANCH column: {out}"
    );
}

#[test]
fn test_status_verbose_shows_all() {
    let origin = create_origin_repo();
    let local = create_local_repo(origin.path());
    let leader = leader_name_for(local.path());
    let prefix = format!("{leader}-pool-");

    run_gw(local.path(), &["worktree", "pool", "warm", "2"]);
    run_gw(local.path(), &["worktree", "pool", "acquire"]);

    let output = run_gw(local.path(), &["worktree", "pool", "status", "-v"]);
    assert_success(&output, "status -v");

    let out = stdout_str(&output);
    // Verbose shows all entries
    assert!(out.contains(&format!("{prefix}001")), "output: {out}");
    assert!(out.contains(&format!("{prefix}002")), "output: {out}");
}

#[test]
fn test_status_fails_when_not_initialized() {
    let origin = create_origin_repo();
    let local = create_local_repo(origin.path());

    let output = run_gw(local.path(), &["worktree", "pool", "status"]);
    assert!(!output.status.success());
}

// --- drain ---

#[test]
fn test_drain_removes_all_worktrees() {
    let origin = create_origin_repo();
    let local = create_local_repo(origin.path());
    let leader = leader_name_for(local.path());
    let prefix = format!("{leader}-pool-");

    run_gw(local.path(), &["worktree", "pool", "warm", "2"]);

    let output = run_gw(local.path(), &["worktree", "pool", "drain"]);
    assert_success(&output, "drain");

    let out = stdout_str(&output);
    assert!(out.contains("Drained 2 worktree(s)"), "output: {out}");

    // Worktree directories should be gone
    assert!(
        !local
            .path()
            .join(format!(".worktrees/{prefix}001"))
            .exists()
    );
    assert!(
        !local
            .path()
            .join(format!(".worktrees/{prefix}002"))
            .exists()
    );

    // Pool branches should be gone
    let branches = run_git(local.path(), &["branch", "--list", &format!("{prefix}*")]);
    assert!(branches.is_empty(), "branches still exist: {branches}");

    // Status should fail (pool gone)
    let output = run_gw(local.path(), &["worktree", "pool", "status"]);
    assert!(!output.status.success());
}

#[test]
fn test_drain_refuses_with_acquired_worktrees() {
    let origin = create_origin_repo();
    let local = create_local_repo(origin.path());

    run_gw(local.path(), &["worktree", "pool", "warm", "2"]);
    run_gw(local.path(), &["worktree", "pool", "acquire"]);

    let output = run_gw(local.path(), &["worktree", "pool", "drain"]);
    assert!(
        !output.status.success(),
        "Expected drain to fail with acquired worktrees"
    );

    let err = stderr_str(&output);
    assert!(err.contains("acquired worktree"), "stderr: {err}");
}

#[test]
fn test_drain_force_with_acquired_worktrees() {
    let origin = create_origin_repo();
    let local = create_local_repo(origin.path());

    run_gw(local.path(), &["worktree", "pool", "warm", "2"]);
    run_gw(local.path(), &["worktree", "pool", "acquire"]);

    let output = run_gw(local.path(), &["worktree", "pool", "drain", "--force"]);
    assert_success(&output, "drain --force");

    let out = stdout_str(&output);
    assert!(out.contains("Drained 2 worktree(s)"), "output: {out}");
}

#[test]
fn test_drain_then_warm_again() {
    let origin = create_origin_repo();
    let local = create_local_repo(origin.path());

    // Warm, drain, then warm again
    run_gw(local.path(), &["worktree", "pool", "warm", "1"]);
    run_gw(local.path(), &["worktree", "pool", "drain"]);

    let output = run_gw(local.path(), &["worktree", "pool", "warm", "1"]);
    assert_success(&output, "re-warm after drain");

    let out = stdout_str(&output);
    assert!(out.contains("1 created"), "output: {out}");
}

// --- release ---

#[test]
fn test_release_returns_worktree_to_pool() {
    let origin = create_origin_repo();
    let local = create_local_repo(origin.path());

    run_gw(local.path(), &["worktree", "pool", "warm", "2"]);
    run_gw(local.path(), &["worktree", "pool", "acquire"]);

    // Status: 1 available, 1 acquired
    let output = run_gw(local.path(), &["worktree", "pool", "status"]);
    let out = stdout_str(&output);
    assert!(out.contains("1 available"), "before release: {out}");
    assert!(out.contains("1 acquired"), "before release: {out}");

    // Release all
    let output = run_gw(local.path(), &["worktree", "pool", "release"]);
    assert_success(&output, "release");

    // Status: 2 available, 0 acquired
    let output = run_gw(local.path(), &["worktree", "pool", "status"]);
    let out = stdout_str(&output);
    assert!(out.contains("2 available"), "after release: {out}");
    assert!(out.contains("0 acquired"), "after release: {out}");
}

#[test]
fn test_release_by_name() {
    let origin = create_origin_repo();
    let local = create_local_repo(origin.path());
    let leader = leader_name_for(local.path());
    let prefix = format!("{leader}-pool-");

    run_gw(local.path(), &["worktree", "pool", "warm", "2"]);

    // Acquire both
    run_gw(local.path(), &["worktree", "pool", "acquire"]);
    run_gw(local.path(), &["worktree", "pool", "acquire"]);

    // Release only the first one by name
    let name = format!("{prefix}001");
    let output = run_gw(local.path(), &["worktree", "pool", "release", &name]);
    assert_success(&output, "release by name");

    // Status: 1 available, 1 acquired
    let output = run_gw(local.path(), &["worktree", "pool", "status"]);
    let out = stdout_str(&output);
    assert!(out.contains("1 available"), "after release by name: {out}");
    assert!(out.contains("1 acquired"), "after release by name: {out}");
}

#[test]
fn test_release_all() {
    let origin = create_origin_repo();
    let local = create_local_repo(origin.path());

    run_gw(local.path(), &["worktree", "pool", "warm", "3"]);

    // Acquire all 3
    run_gw(local.path(), &["worktree", "pool", "acquire"]);
    run_gw(local.path(), &["worktree", "pool", "acquire"]);
    run_gw(local.path(), &["worktree", "pool", "acquire"]);

    // Release all (no name arg)
    let output = run_gw(local.path(), &["worktree", "pool", "release"]);
    assert_success(&output, "release all");

    // Status: 3 available, 0 acquired
    let output = run_gw(local.path(), &["worktree", "pool", "status"]);
    let out = stdout_str(&output);
    assert!(out.contains("3 available"), "after release all: {out}");
    assert!(out.contains("0 acquired"), "after release all: {out}");
}

#[test]
fn test_release_fails_when_none_acquired() {
    let origin = create_origin_repo();
    let local = create_local_repo(origin.path());

    run_gw(local.path(), &["worktree", "pool", "warm", "1"]);

    // Release with nothing acquired
    let output = run_gw(local.path(), &["worktree", "pool", "release"]);
    assert!(
        !output.status.success(),
        "Expected release to fail when none acquired"
    );

    let err = stderr_str(&output);
    assert!(
        err.contains("No acquired worktrees to release"),
        "stderr: {err}"
    );
}

// --- full workflow ---

#[test]
fn test_full_pool_lifecycle() {
    let origin = create_origin_repo();
    let local = create_local_repo(origin.path());

    // 1. Warm
    let output = run_gw(local.path(), &["worktree", "pool", "warm", "3"]);
    assert_success(&output, "warm");

    // 2. Status shows 3 available
    let output = run_gw(local.path(), &["worktree", "pool", "status"]);
    assert_success(&output, "status");
    let out = stdout_str(&output);
    assert!(out.contains("3 available"), "output: {out}");

    // 3. Acquire
    let output = run_gw(local.path(), &["worktree", "pool", "acquire"]);
    assert_success(&output, "acquire");
    let path = String::from_utf8_lossy(&output.stdout).trim().to_string();
    assert!(Path::new(&path).is_dir());

    // 4. Status shows 2 available, 1 acquired
    let output = run_gw(local.path(), &["worktree", "pool", "status"]);
    let out = stdout_str(&output);
    assert!(out.contains("2 available"), "output: {out}");
    assert!(out.contains("1 acquired"), "output: {out}");

    // 5. Release (not drain!)
    let output = run_gw(local.path(), &["worktree", "pool", "release"]);
    assert_success(&output, "release");

    // 6. Status shows 3 available again
    let output = run_gw(local.path(), &["worktree", "pool", "status"]);
    let out = stdout_str(&output);
    assert!(out.contains("3 available"), "after release: {out}");
    assert!(out.contains("0 acquired"), "after release: {out}");

    // 7. Drain
    let output = run_gw(local.path(), &["worktree", "pool", "drain"]);
    assert_success(&output, "drain");

    let out = stdout_str(&output);
    assert!(out.contains("Drained 3 worktree(s)"), "output: {out}");
}

// --- acquire/drain cycle ---

#[test]
fn test_acquire_drain_cycle() {
    let origin = create_origin_repo();
    let local = create_local_repo(origin.path());

    run_gw(local.path(), &["worktree", "pool", "warm", "2"]);

    // Acquire both
    let out1 = run_gw(local.path(), &["worktree", "pool", "acquire"]);
    assert_success(&out1, "acquire 1");

    let out2 = run_gw(local.path(), &["worktree", "pool", "acquire"]);
    assert_success(&out2, "acquire 2");

    // Pool should be exhausted
    let out3 = run_gw(local.path(), &["worktree", "pool", "acquire"]);
    assert!(!out3.status.success(), "Expected exhaustion");

    // Force drain
    let output = run_gw(local.path(), &["worktree", "pool", "drain", "--force"]);
    assert_success(&output, "drain --force");

    // Re-warm and acquire again
    run_gw(local.path(), &["worktree", "pool", "warm", "1"]);
    let out4 = run_gw(local.path(), &["worktree", "pool", "acquire"]);
    assert_success(&out4, "re-acquire after drain+warm");
}