git-std 0.11.8

Standard git workflow — commits, versioning, hooks
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
use std::path::Path;

use assert_cmd::Command;

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn git(dir: &Path, args: &[&str]) -> String {
    let output = std::process::Command::new("git")
        .current_dir(dir)
        .args(args)
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "git {:?} failed: {}",
        args,
        String::from_utf8_lossy(&output.stderr)
    );
    String::from_utf8_lossy(&output.stdout).trim().to_string()
}

fn init_repo(dir: &Path) {
    git(dir, &["init"]);
    git(dir, &["config", "user.name", "Test"]);
    git(dir, &["config", "user.email", "test@test.com"]);
}

fn run_bootstrap(dir: &Path, extra_args: &[&str]) -> assert_cmd::assert::Assert {
    let mut args = vec!["--color", "never", "bootstrap"];
    args.extend_from_slice(extra_args);
    Command::cargo_bin("git-std")
        .unwrap()
        .args(&args)
        .current_dir(dir)
        .assert()
}

fn run_init(dir: &Path, extra_args: &[&str]) -> assert_cmd::assert::Assert {
    let mut args = vec!["--color", "never", "init"];
    args.extend_from_slice(extra_args);
    Command::cargo_bin("git-std")
        .unwrap()
        .args(&args)
        .env("GIT_STD_HOOKS_ENABLE", "none")
        .current_dir(dir)
        .assert()
}

fn stderr_text(assert: &assert_cmd::assert::Assert) -> String {
    String::from_utf8_lossy(&assert.get_output().stderr).to_string()
}

// ===========================================================================
// #294 — git std bootstrap (run)
// ===========================================================================

#[test]
fn bootstrap_sets_hooks_path_when_githooks_exists() {
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());
    std::fs::create_dir_all(dir.path().join(".githooks")).unwrap();

    let a = run_bootstrap(dir.path(), &[]).success();
    let err = stderr_text(&a);
    assert!(
        err.contains("git hooks configured"),
        "should confirm hooks configured, got: {err}"
    );

    let val = git(dir.path(), &["config", "core.hooksPath"]);
    assert_eq!(val, ".githooks");
}

#[test]
fn bootstrap_skips_hooks_path_when_no_githooks() {
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());

    let a = run_bootstrap(dir.path(), &[]).success();
    let err = stderr_text(&a);
    // No output when nothing to do
    assert!(
        !err.contains("hook"),
        "should be silent on skip, got: {err}"
    );
}

#[test]
fn bootstrap_skips_lfs_when_no_gitattributes() {
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());

    let a = run_bootstrap(dir.path(), &[]).success();
    let err = stderr_text(&a);
    // No output when nothing to do
    assert!(!err.contains("LFS"), "should be silent on skip, got: {err}");
}

#[test]
fn bootstrap_skips_lfs_when_no_filter_lfs() {
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());
    std::fs::write(dir.path().join(".gitattributes"), "*.bin binary\n").unwrap();

    let a = run_bootstrap(dir.path(), &[]).success();
    let err = stderr_text(&a);
    assert!(!err.contains("LFS"), "should be silent on skip, got: {err}");
}

#[test]
fn bootstrap_sets_blame_ignore_revs() {
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());
    std::fs::write(dir.path().join(".git-blame-ignore-revs"), "# revs\n").unwrap();

    let a = run_bootstrap(dir.path(), &[]).success();
    let err = stderr_text(&a);
    assert!(
        err.contains("blame ignore revs configured"),
        "should confirm blame config, got: {err}"
    );

    let val = git(dir.path(), &["config", "blame.ignoreRevsFile"]);
    assert_eq!(val, ".git-blame-ignore-revs");
}

#[test]
fn bootstrap_skips_blame_when_no_file() {
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());

    let a = run_bootstrap(dir.path(), &[]).success();
    let err = stderr_text(&a);
    assert!(
        !err.contains("blame"),
        "should be silent on skip, got: {err}"
    );
}

#[test]
fn bootstrap_runs_custom_hooks() {
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    std::fs::write(hooks_dir.join("bootstrap.hooks"), "! echo bootstrap-ran\n").unwrap();

    let a = run_bootstrap(dir.path(), &[]).success();

    let stdout = String::from_utf8_lossy(&a.get_output().stdout);
    let stderr = stderr_text(&a);
    let combined = format!("{stdout}{stderr}");
    assert!(
        combined.contains("bootstrap-ran"),
        "custom hook should execute, got: {combined}"
    );
}

#[test]
fn bootstrap_dry_run_no_side_effects() {
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    std::fs::write(dir.path().join(".git-blame-ignore-revs"), "# revs\n").unwrap();

    let a = run_bootstrap(dir.path(), &["--dry-run"]).success();
    let err = stderr_text(&a);
    assert!(
        err.contains("configured"),
        "should show what would be done, got: {err}"
    );

    // Verify no config was actually set
    let output = std::process::Command::new("git")
        .current_dir(dir.path())
        .args(["config", "core.hooksPath"])
        .output()
        .unwrap();
    assert!(
        !output.status.success(),
        "hooksPath should not be set in dry-run"
    );
}

#[test]
fn bootstrap_is_idempotent() {
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    std::fs::write(dir.path().join(".git-blame-ignore-revs"), "# revs\n").unwrap();

    // Run twice
    run_bootstrap(dir.path(), &[]).success();
    run_bootstrap(dir.path(), &[]).success();

    // Both should succeed and leave valid config
    let hooks_path = git(dir.path(), &["config", "core.hooksPath"]);
    assert_eq!(hooks_path, ".githooks");
    let blame = git(dir.path(), &["config", "blame.ignoreRevsFile"]);
    assert_eq!(blame, ".git-blame-ignore-revs");
}

#[test]
fn bootstrap_exits_zero_with_nothing_to_do() {
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());

    run_bootstrap(dir.path(), &[]).success();
}

// ===========================================================================
// #400 — git std init
// ===========================================================================

#[test]
fn init_creates_files() {
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());

    run_init(dir.path(), &[]).success();

    // ./bootstrap exists and is executable
    let script = dir.path().join("bootstrap");
    assert!(script.exists(), "bootstrap script should exist");

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mode = std::fs::metadata(&script).unwrap().permissions().mode();
        assert!(mode & 0o111 != 0, "bootstrap should be executable");
    }

    // .githooks/bootstrap.hooks exists
    let hooks = dir.path().join(".githooks/bootstrap.hooks");
    assert!(hooks.exists(), "bootstrap.hooks should exist");

    // Created files are staged in the git index
    let staged = git(dir.path(), &["diff", "--cached", "--name-only"]);
    assert!(
        staged.contains("bootstrap"),
        "bootstrap should be staged, got: {staged}"
    );
    assert!(
        staged.contains("bootstrap.hooks"),
        "bootstrap.hooks should be staged, got: {staged}"
    );

    // Executable bit is tracked in the index
    let ls = git(dir.path(), &["ls-files", "-s", "bootstrap"]);
    assert!(
        ls.starts_with("100755"),
        "bootstrap should be 100755 in index, got: {ls}"
    );
}

#[test]
fn init_min_version_matches_crate() {
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());

    run_init(dir.path(), &[]).success();

    let script = std::fs::read_to_string(dir.path().join("bootstrap")).unwrap();
    let version = env!("CARGO_PKG_VERSION");
    assert!(
        script.contains(&format!("MIN_VERSION=\"{version}\"")),
        "MIN_VERSION should match crate version"
    );
}

#[test]
fn init_skips_existing_without_force() {
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());

    // Create existing files
    std::fs::write(dir.path().join("bootstrap"), "existing\n").unwrap();
    std::fs::create_dir_all(dir.path().join(".githooks")).unwrap();
    std::fs::write(dir.path().join(".githooks/bootstrap.hooks"), "existing\n").unwrap();

    let a = run_init(dir.path(), &[]).success();
    let err = stderr_text(&a);
    assert!(
        err.contains("already exists"),
        "should warn about existing files, got: {err}"
    );

    // Verify content unchanged
    let content = std::fs::read_to_string(dir.path().join("bootstrap")).unwrap();
    assert_eq!(content, "existing\n");
}

#[test]
fn init_overwrites_with_force() {
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());

    // Create existing files
    std::fs::write(dir.path().join("bootstrap"), "old\n").unwrap();
    std::fs::create_dir_all(dir.path().join(".githooks")).unwrap();
    std::fs::write(dir.path().join(".githooks/bootstrap.hooks"), "old\n").unwrap();

    run_init(dir.path(), &["--force"]).success();

    // Verify content was replaced
    let content = std::fs::read_to_string(dir.path().join("bootstrap")).unwrap();
    assert!(content.contains("MIN_VERSION"), "should have new content");
}

#[test]
fn init_appends_marker_to_docs() {
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());

    std::fs::write(dir.path().join("README.md"), "# My Project\n").unwrap();
    std::fs::write(dir.path().join("AGENTS.md"), "# Agents\n").unwrap();

    run_init(dir.path(), &[]).success();

    let readme = std::fs::read_to_string(dir.path().join("README.md")).unwrap();
    assert!(
        readme.contains("<!-- git-std:bootstrap -->"),
        "README should have marker"
    );
    assert!(
        readme.contains("./bootstrap"),
        "README should mention ./bootstrap"
    );

    let agents = std::fs::read_to_string(dir.path().join("AGENTS.md")).unwrap();
    assert!(
        agents.contains("<!-- git-std:bootstrap -->"),
        "AGENTS should have marker"
    );
}

#[test]
fn init_does_not_double_append() {
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());

    std::fs::write(dir.path().join("README.md"), "# My Project\n").unwrap();

    // Run twice
    run_init(dir.path(), &["--force"]).success();
    run_init(dir.path(), &["--force"]).success();

    let readme = std::fs::read_to_string(dir.path().join("README.md")).unwrap();
    let count = readme.matches("<!-- git-std:bootstrap -->").count();
    assert_eq!(count, 1, "marker should appear exactly once, found {count}");
}

// ===========================================================================
// commit-msg default template (#294 AC — now via git std init)
// ===========================================================================

#[test]
fn init_generates_commit_msg_with_check_command() {
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());

    // Run init with env var to select only commit-msg hook
    Command::cargo_bin("git-std")
        .unwrap()
        .args(["--color", "never", "init"])
        .env("GIT_STD_HOOKS_ENABLE", "commit-msg")
        .current_dir(dir.path())
        .assert()
        .success();

    let hooks_file = dir.path().join(".githooks/commit-msg.hooks");
    assert!(hooks_file.exists(), "commit-msg.hooks should exist");

    let content = std::fs::read_to_string(&hooks_file).unwrap();
    assert!(
        content.contains("! git std lint --file {msg}"),
        "commit-msg.hooks should have default check command, got:\n{content}"
    );
}

#[test]
fn init_commit_msg_shim_is_active() {
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["--color", "never", "init"])
        .env("GIT_STD_HOOKS_ENABLE", "commit-msg")
        .current_dir(dir.path())
        .assert()
        .success();

    // The active shim (no .off extension) should exist
    let shim = dir.path().join(".githooks/commit-msg");
    assert!(shim.exists(), "commit-msg shim should be active (no .off)");

    // The .off file should NOT exist
    let off = dir.path().join(".githooks/commit-msg.off");
    assert!(
        !off.exists(),
        "commit-msg.off should not exist when enabled"
    );
}

// ── repo-root resolution (#317) ─────────────────────────────────

#[test]
fn bootstrap_run_from_subdirectory() {
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());
    std::fs::create_dir_all(dir.path().join(".githooks")).unwrap();
    std::fs::write(dir.path().join(".git-blame-ignore-revs"), "# revs\n").unwrap();

    let subdir = dir.path().join("src").join("nested");
    std::fs::create_dir_all(&subdir).unwrap();

    let a = run_bootstrap(&subdir, &[]).success();
    let err = stderr_text(&a);
    assert!(
        err.contains("git hooks configured"),
        "should configure hooks from subdir, got: {err}"
    );
    assert!(
        err.contains("blame ignore revs configured"),
        "should configure blame from subdir, got: {err}"
    );
}

#[test]
fn init_from_subdirectory() {
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());

    let subdir = dir.path().join("src");
    std::fs::create_dir_all(&subdir).unwrap();

    run_init(&subdir, &[]).success();

    // Files should be at repo root, not in subdirectory
    assert!(
        dir.path().join("bootstrap").exists(),
        "bootstrap script should be at repo root"
    );
    assert!(
        dir.path().join(".githooks/bootstrap.hooks").exists(),
        "bootstrap.hooks should be at repo root"
    );
    assert!(
        !subdir.join("bootstrap").exists(),
        "bootstrap should not be in subdirectory"
    );
    assert!(
        !subdir.join(".githooks").exists(),
        ".githooks should not be in subdirectory"
    );
}