git-claw 0.3.3

Deterministic git worktree orchestration engine for developers and AI agents
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
use assert_cmd::Command;
use git_claw::core::branch::BranchType;
use git_claw::infra::registry::SlotRegistry;
use git_claw::workflow::{
    finish_worktree, publish_worktree, start_worktree, FinishOptions, PublishOptions, StartOptions,
};
use predicates::prelude::*;
use std::fs;
use tempfile::tempdir;

fn setup_repo() -> (tempfile::TempDir, tempfile::TempDir) {
    let repo_dir = tempdir().expect("repo dir");
    let wt_parent = tempdir().expect("wt parent dir");

    let repo_path = repo_dir.path();
    let init = std::process::Command::new("git")
        .args(["init", "-b", "main"])
        .current_dir(repo_path)
        .output()
        .expect("git init");
    assert!(init.status.success());

    let _ = std::process::Command::new("git")
        .args(["config", "user.name", "Tester"])
        .current_dir(repo_path)
        .output();
    let _ = std::process::Command::new("git")
        .args(["config", "user.email", "tester@test.local"])
        .current_dir(repo_path)
        .output();

    fs::write(repo_path.join("README.md"), "# Test").expect("write readme");
    let config_content = format!(
        r#"
[project]
main_branch = "main"
worktree_root = "{}"

[ports]
web = 3000
"#,
        wt_parent.path().display()
    );
    fs::write(repo_path.join(".git-claw.toml"), config_content).expect("write config");

    let add = std::process::Command::new("git")
        .args(["add", "."])
        .current_dir(repo_path)
        .output()
        .expect("git add");
    assert!(add.status.success());

    let commit = std::process::Command::new("git")
        .args(["commit", "-m", "Initial"])
        .current_dir(repo_path)
        .output()
        .expect("git commit");
    assert!(commit.status.success());

    (repo_dir, wt_parent)
}

#[test]
fn test_finish_worktree_merges_and_cleans_up() {
    let (repo_dir, wt_parent) = setup_repo();
    let repo_path = repo_dir.path();

    // Start worktree
    start_worktree(StartOptions {
        branch_type: BranchType::Feature,
        name: "login",
        isolated: false,
        repo_root: Some(repo_path),
    })
    .expect("start worktree");

    let wt_path = wt_parent.path().join("login");
    assert!(wt_path.exists());

    // Commit file in worktree
    fs::write(wt_path.join("login.txt"), "login code").expect("write login.txt");
    let add = std::process::Command::new("git")
        .args(["add", "login.txt"])
        .current_dir(&wt_path)
        .output()
        .expect("git add in wt");
    assert!(add.status.success());
    let commit = std::process::Command::new("git")
        .args(["commit", "-m", "Add login"])
        .current_dir(&wt_path)
        .output()
        .expect("git commit in wt");
    assert!(commit.status.success());

    // Create untracked IDE folder to simulate IDE activity
    let idea_dir = wt_path.join(".idea");
    fs::create_dir_all(&idea_dir).expect("create .idea");
    fs::write(idea_dir.join("workspace.xml"), "<xml/>").expect("write workspace.xml");

    // Finish worktree
    finish_worktree(FinishOptions {
        name: Some("login"),
        force: false,
        keep_remote: false,
        repo_root: Some(repo_path),
    })
    .expect("finish worktree");

    // Verify main branch has login.txt
    assert!(repo_path.join("login.txt").exists());
    let content = fs::read_to_string(repo_path.join("login.txt")).expect("read merged file");
    assert_eq!(content, "login code");

    // Verify worktree directory is removed
    assert!(!wt_path.exists());

    // Verify registry slot is freed
    let registry_path = repo_path.join(".git/claw/slots.json");
    let registry = SlotRegistry::load_or_empty(&registry_path).expect("load registry");
    assert!(registry.slots.is_empty());
}

#[test]
fn test_finish_worktree_pre_hook_abort_and_force() {
    let (repo_dir, wt_parent) = setup_repo();
    let repo_path = repo_dir.path();

    let config_content = format!(
        r#"
[project]
main_branch = "main"
worktree_root = "{}"

[hooks]
pre_finish = "exit 1"
"#,
        wt_parent.path().display()
    );
    fs::write(repo_path.join(".git-claw.toml"), config_content).expect("write config");
    let _ = std::process::Command::new("git")
        .args(["commit", "-am", "Update hooks config"])
        .current_dir(repo_path)
        .output();

    start_worktree(StartOptions {
        branch_type: BranchType::Feature,
        name: "hook-test",
        isolated: false,
        repo_root: Some(repo_path),
    })
    .expect("start worktree");

    let wt_path = wt_parent.path().join("hook-test");
    assert!(wt_path.exists());

    fs::write(wt_path.join("hook.txt"), "hook code").expect("write hook.txt");
    let _ = std::process::Command::new("git")
        .args(["add", "."])
        .current_dir(&wt_path)
        .output();
    let _ = std::process::Command::new("git")
        .args(["commit", "-m", "Hook commit"])
        .current_dir(&wt_path)
        .output();

    // Finish without force should fail
    let res = finish_worktree(FinishOptions {
        name: Some("hook-test"),
        force: false,
        keep_remote: false,
        repo_root: Some(repo_path),
    });
    assert!(res.is_err());
    assert!(wt_path.exists());

    // Finish with force should succeed
    let res_force = finish_worktree(FinishOptions {
        name: Some("hook-test"),
        force: true,
        keep_remote: false,
        repo_root: Some(repo_path),
    });
    assert!(res_force.is_ok());
    assert!(!wt_path.exists());
    assert!(repo_path.join("hook.txt").exists());
}

#[test]
fn test_cli_finish_subcommand() {
    let (repo_dir, wt_parent) = setup_repo();
    let repo_path = repo_dir.path();

    // Start worktree via CLI
    let mut cmd_start = Command::cargo_bin("git-claw").expect("binary");
    cmd_start
        .current_dir(repo_path)
        .args(["feature", "start", "cli-finish"])
        .assert()
        .success();

    let wt_path = wt_parent.path().join("cli-finish");
    assert!(wt_path.exists());

    // Finish worktree via CLI
    let mut cmd_finish = Command::cargo_bin("git-claw").expect("binary");
    cmd_finish
        .current_dir(repo_path)
        .args(["finish", "cli-finish"])
        .assert()
        .success()
        .stdout(predicate::str::contains("successfully merged into 'main'"));

    assert!(!wt_path.exists());
}

fn setup_repo_with_remote() -> (tempfile::TempDir, tempfile::TempDir, tempfile::TempDir) {
    let remote_dir = tempdir().expect("remote dir");
    let repo_dir = tempdir().expect("repo dir");
    let wt_parent = tempdir().expect("wt parent dir");

    let init_remote = std::process::Command::new("git")
        .args(["init", "--bare", "-b", "main"])
        .current_dir(remote_dir.path())
        .output()
        .expect("git init bare remote");
    assert!(init_remote.status.success());

    let repo_path = repo_dir.path();
    let init = std::process::Command::new("git")
        .args(["init", "-b", "main"])
        .current_dir(repo_path)
        .output()
        .expect("git init");
    assert!(init.status.success());

    let _ = std::process::Command::new("git")
        .args(["config", "user.name", "Tester"])
        .current_dir(repo_path)
        .output();
    let _ = std::process::Command::new("git")
        .args(["config", "user.email", "tester@test.local"])
        .current_dir(repo_path)
        .output();

    let add_remote = std::process::Command::new("git")
        .args([
            "remote",
            "add",
            "origin",
            &remote_dir.path().to_string_lossy(),
        ])
        .current_dir(repo_path)
        .output()
        .expect("git remote add");
    assert!(add_remote.status.success());

    fs::write(repo_path.join("README.md"), "# Test").expect("write readme");
    let config_content = format!(
        r#"
[project]
main_branch = "main"
worktree_root = "{}"

[ports]
web = 3000
"#,
        wt_parent.path().display()
    );
    fs::write(repo_path.join(".git-claw.toml"), config_content).expect("write config");

    let add = std::process::Command::new("git")
        .args(["add", "."])
        .current_dir(repo_path)
        .output()
        .expect("git add");
    assert!(add.status.success());

    let commit = std::process::Command::new("git")
        .args(["commit", "-m", "Initial"])
        .current_dir(repo_path)
        .output()
        .expect("git commit");
    assert!(commit.status.success());

    let push_main = std::process::Command::new("git")
        .args(["push", "-u", "origin", "main"])
        .current_dir(repo_path)
        .output()
        .expect("git push main");
    assert!(push_main.status.success());

    (remote_dir, repo_dir, wt_parent)
}

#[test]
fn test_finish_deletes_published_remote_branch() {
    let (remote_dir, repo_dir, wt_parent) = setup_repo_with_remote();
    let repo_path = repo_dir.path();

    start_worktree(StartOptions {
        branch_type: BranchType::Feature,
        name: "rem-del",
        isolated: false,
        repo_root: Some(repo_path),
    })
    .expect("start worktree");

    let wt_path = wt_parent.path().join("rem-del");
    fs::write(wt_path.join("code.txt"), "code").expect("write code");
    let _ = std::process::Command::new("git")
        .args(["add", "."])
        .current_dir(&wt_path)
        .output();
    let _ = std::process::Command::new("git")
        .args(["commit", "-m", "Add code"])
        .current_dir(&wt_path)
        .output();

    publish_worktree(PublishOptions {
        branch_type: Some(BranchType::Feature),
        name: Some("rem-del"),
        remote: None,
        repo_root: Some(repo_path),
    })
    .expect("publish worktree");

    let verify = std::process::Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/feature/rem-del"])
        .current_dir(remote_dir.path())
        .output()
        .expect("verify remote branch");
    assert!(verify.status.success());

    finish_worktree(FinishOptions {
        name: Some("rem-del"),
        force: false,
        keep_remote: false,
        repo_root: Some(repo_path),
    })
    .expect("finish worktree");

    let verify_after = std::process::Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/feature/rem-del"])
        .current_dir(remote_dir.path())
        .output()
        .expect("verify remote branch deleted");
    assert!(!verify_after.status.success());
    assert!(!wt_path.exists());
    assert!(repo_path.join("code.txt").exists());
}

#[test]
fn test_finish_retains_remote_branch_when_keep_remote_true() {
    let (remote_dir, repo_dir, wt_parent) = setup_repo_with_remote();
    let repo_path = repo_dir.path();

    start_worktree(StartOptions {
        branch_type: BranchType::Feature,
        name: "rem-keep",
        isolated: false,
        repo_root: Some(repo_path),
    })
    .expect("start worktree");

    let wt_path = wt_parent.path().join("rem-keep");
    fs::write(wt_path.join("keep.txt"), "keep").expect("write keep");
    let _ = std::process::Command::new("git")
        .args(["add", "."])
        .current_dir(&wt_path)
        .output();
    let _ = std::process::Command::new("git")
        .args(["commit", "-m", "Keep code"])
        .current_dir(&wt_path)
        .output();

    publish_worktree(PublishOptions {
        branch_type: Some(BranchType::Feature),
        name: Some("rem-keep"),
        remote: None,
        repo_root: Some(repo_path),
    })
    .expect("publish worktree");

    let verify = std::process::Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/feature/rem-keep"])
        .current_dir(remote_dir.path())
        .output()
        .expect("verify remote branch");
    assert!(verify.status.success());

    finish_worktree(FinishOptions {
        name: Some("rem-keep"),
        force: false,
        keep_remote: true,
        repo_root: Some(repo_path),
    })
    .expect("finish worktree");

    let verify_after = std::process::Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/feature/rem-keep"])
        .current_dir(remote_dir.path())
        .output()
        .expect("verify remote branch retained");
    assert!(verify_after.status.success());
    assert!(!wt_path.exists());
    assert!(repo_path.join("keep.txt").exists());
}

#[test]
fn test_cli_finish_remote_cleanup_and_keep_remote_flag() {
    let (remote_dir, repo_dir, wt_parent) = setup_repo_with_remote();
    let repo_path = repo_dir.path();

    start_worktree(StartOptions {
        branch_type: BranchType::Feature,
        name: "cli-rem",
        isolated: false,
        repo_root: Some(repo_path),
    })
    .expect("start worktree");

    let wt_path = wt_parent.path().join("cli-rem");
    fs::write(wt_path.join("clirem.txt"), "clirem").expect("write clirem");
    let _ = std::process::Command::new("git")
        .args(["add", "."])
        .current_dir(&wt_path)
        .output();
    let _ = std::process::Command::new("git")
        .args(["commit", "-m", "CLI remote commit"])
        .current_dir(&wt_path)
        .output();

    publish_worktree(PublishOptions {
        branch_type: Some(BranchType::Feature),
        name: Some("cli-rem"),
        remote: None,
        repo_root: Some(repo_path),
    })
    .expect("publish worktree");

    let mut cmd_finish = Command::cargo_bin("git-claw").expect("binary");
    cmd_finish
        .current_dir(repo_path)
        .args(["finish", "cli-rem"])
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "Remote branch 'feature/cli-rem' deleted from 'origin'",
        ));

    let verify_deleted = std::process::Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/feature/cli-rem"])
        .current_dir(remote_dir.path())
        .output()
        .expect("verify remote branch deleted");
    assert!(!verify_deleted.status.success());

    start_worktree(StartOptions {
        branch_type: BranchType::Feature,
        name: "cli-keep",
        isolated: false,
        repo_root: Some(repo_path),
    })
    .expect("start worktree");

    let wt_path2 = wt_parent.path().join("cli-keep");
    fs::write(wt_path2.join("clikeep.txt"), "clikeep").expect("write clikeep");
    let _ = std::process::Command::new("git")
        .args(["add", "."])
        .current_dir(&wt_path2)
        .output();
    let _ = std::process::Command::new("git")
        .args(["commit", "-m", "CLI keep commit"])
        .current_dir(&wt_path2)
        .output();

    publish_worktree(PublishOptions {
        branch_type: Some(BranchType::Feature),
        name: Some("cli-keep"),
        remote: None,
        repo_root: Some(repo_path),
    })
    .expect("publish worktree");

    let mut cmd_finish_keep = Command::cargo_bin("git-claw").expect("binary");
    cmd_finish_keep
        .current_dir(repo_path)
        .args(["finish", "cli-keep", "--keep-remote"])
        .assert()
        .success();

    let verify_kept = std::process::Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/feature/cli-keep"])
        .current_dir(remote_dir.path())
        .output()
        .expect("verify remote branch kept");
    assert!(verify_kept.status.success());
}

#[test]
fn test_finish_remote_unreachable_is_non_fatal() {
    let (remote_dir, repo_dir, wt_parent) = setup_repo_with_remote();
    let repo_path = repo_dir.path();

    start_worktree(StartOptions {
        branch_type: BranchType::Feature,
        name: "rem-unreach",
        isolated: false,
        repo_root: Some(repo_path),
    })
    .expect("start worktree");

    let wt_path = wt_parent.path().join("rem-unreach");
    fs::write(wt_path.join("file.txt"), "content").expect("write file");
    let _ = std::process::Command::new("git")
        .args(["add", "."])
        .current_dir(&wt_path)
        .output();
    let _ = std::process::Command::new("git")
        .args(["commit", "-m", "Remote unreachable test"])
        .current_dir(&wt_path)
        .output();

    // Corrupt remote directory so remote becomes unreachable
    let _ = fs::remove_dir_all(remote_dir.path());

    // Finish should succeed locally despite remote error
    let res = finish_worktree(FinishOptions {
        name: Some("rem-unreach"),
        force: false,
        keep_remote: false,
        repo_root: Some(repo_path),
    });
    assert!(res.is_ok());
    assert!(!wt_path.exists());
    assert!(repo_path.join("file.txt").exists());
}