git-std 0.11.10

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
465
466
467
468
469
470
471
472
473
//! Integration tests for `git std init` (#400).

use std::path::Path;

use assert_cmd::Command;
use predicates::prelude::*;

// ---------------------------------------------------------------------------
// 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_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()
}

// ===========================================================================
// init from scratch
// ===========================================================================

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

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

    // core.hooksPath is set
    let hooks_path = git(dir.path(), &["config", "core.hooksPath"]);
    assert_eq!(hooks_path, ".githooks");

    // .githooks/ directory exists
    assert!(
        dir.path().join(".githooks").is_dir(),
        ".githooks/ should exist"
    );

    // .hooks templates written for known hooks
    for hook in &["pre-commit", "commit-msg", "pre-push"] {
        let tpl = dir.path().join(format!(".githooks/{hook}.hooks"));
        assert!(tpl.exists(), "{hook}.hooks template should exist");
    }

    // All shims are .off (since GIT_STD_HOOKS_ENABLE=none)
    assert!(dir.path().join(".githooks/pre-commit.off").exists());
    assert!(dir.path().join(".githooks/commit-msg.off").exists());

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

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

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

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

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

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

    assert!(!dir.path().join(".githooks").exists());

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

    assert!(
        dir.path().join(".githooks").is_dir(),
        ".githooks/ should be created"
    );
}

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

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

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

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

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

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

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

    let staged = git(dir.path(), &["diff", "--cached", "--name-only"]);
    assert!(
        staged.contains("bootstrap"),
        "bootstrap should be staged, got: {staged}"
    );
    assert!(
        staged.contains(".githooks"),
        ".githooks/ contents should be staged, got: {staged}"
    );
}

#[test]
fn init_appends_bootstrap_marker_to_readme() {
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());
    std::fs::write(dir.path().join("README.md"), "# My Project\n").unwrap();

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

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

#[test]
fn init_appends_marker_to_agents_md() {
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());
    std::fs::write(dir.path().join("AGENTS.md"), "# Agents\n").unwrap();

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

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

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

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

    let hooks_dir = dir.path().join(".githooks");
    assert!(
        hooks_dir.join("pre-commit").exists(),
        "pre-commit shim active"
    );
    assert!(
        hooks_dir.join("commit-msg").exists(),
        "commit-msg shim active"
    );
    assert!(!hooks_dir.join("pre-commit.off").exists());
    assert!(hooks_dir.join("pre-push.off").exists(), "pre-push disabled");
}

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

    // Run twice — both should succeed
    run_init(dir.path(), &[]).success();
    run_init(dir.path(), &[]).success();

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

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

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["--color", "never", "init"])
        .current_dir(dir.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains(
            "interactive prompt requires a TTY",
        ))
        .stderr(predicate::str::contains("GIT_STD_HOOKS_ENABLE"));
}

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

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

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

    let a = run_init(dir.path(), &[]).success();
    let err = stderr_text(&a);
    assert!(
        err.contains("bootstrap created"),
        "should confirm bootstrap created, got: {err}"
    );
}

// ===========================================================================
// init --force
// ===========================================================================

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

    // Pre-create files
    std::fs::write(dir.path().join("bootstrap"), "existing content\n").unwrap();
    std::fs::create_dir_all(dir.path().join(".githooks")).unwrap();
    std::fs::write(
        dir.path().join(".githooks/bootstrap.hooks"),
        "existing hooks\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}"
    );

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

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

    // Pre-create files with old content
    std::fs::write(dir.path().join("bootstrap"), "old content\n").unwrap();
    std::fs::create_dir_all(dir.path().join(".githooks")).unwrap();
    std::fs::write(dir.path().join(".githooks/bootstrap.hooks"), "old hooks\n").unwrap();

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

    // bootstrap should have new content
    let content = std::fs::read_to_string(dir.path().join("bootstrap")).unwrap();
    assert!(
        content.contains("MIN_VERSION"),
        "bootstrap should have new content after --force, got: {content}"
    );

    // bootstrap.hooks should have new content
    let hooks_content =
        std::fs::read_to_string(dir.path().join(".githooks/bootstrap.hooks")).unwrap();
    assert!(
        hooks_content.contains("git std bootstrap"),
        "bootstrap.hooks should have template content"
    );
}

#[test]
fn init_force_does_not_double_append_marker() {
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());
    std::fs::write(dir.path().join("README.md"), "# Project\n").unwrap();

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

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

#[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 subdirectory
    assert!(
        dir.path().join("bootstrap").exists(),
        "bootstrap should be at repo root"
    );
    assert!(
        dir.path().join(".githooks").is_dir(),
        ".githooks should be at repo root"
    );
    assert!(
        !subdir.join("bootstrap").exists(),
        "bootstrap should not be in subdir"
    );
    assert!(
        !subdir.join(".githooks").exists(),
        ".githooks should not be in subdir"
    );
}

#[test]
fn init_not_in_git_repo_fails() {
    let dir = tempfile::tempdir().unwrap();
    // No git init

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["--color", "never", "init"])
        .env("GIT_STD_HOOKS_ENABLE", "none")
        .current_dir(dir.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains("not inside a git repository"));
}

// ===========================================================================
// lifecycle hook templates (#443)
// ===========================================================================

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

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

    for hook in &["pre-bump", "post-version", "post-changelog", "post-bump"] {
        let path = dir.path().join(format!(".githooks/{hook}.hooks"));
        assert!(path.exists(), "{hook}.hooks should be created by init");
        let content = std::fs::read_to_string(&path).unwrap();
        assert!(
            content.contains(&format!("# git-std hooks — {hook}.hooks")),
            "{hook}.hooks should have a header comment"
        );
    }
}

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

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

    // Lifecycle hook files must not have corresponding shims
    for hook in &["pre-bump", "post-version", "post-changelog", "post-bump"] {
        assert!(
            !dir.path().join(format!(".githooks/{hook}")).exists(),
            "no shim should exist for lifecycle hook {hook}"
        );
        assert!(
            !dir.path().join(format!(".githooks/{hook}.off")).exists(),
            "no .off shim should exist for lifecycle hook {hook}"
        );
    }
}

#[test]
fn init_skips_existing_lifecycle_hooks_without_force() {
    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(".githooks/pre-bump.hooks"),
        "# custom content\n",
    )
    .unwrap();

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

    let content = std::fs::read_to_string(dir.path().join(".githooks/pre-bump.hooks")).unwrap();
    assert_eq!(
        content, "# custom content\n",
        "existing file should not be overwritten"
    );
}

#[test]
fn init_force_overwrites_lifecycle_hooks() {
    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(".githooks/pre-bump.hooks"),
        "# old content\n",
    )
    .unwrap();

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

    let content = std::fs::read_to_string(dir.path().join(".githooks/pre-bump.hooks")).unwrap();
    assert!(
        content.contains("git-std hooks — pre-bump.hooks"),
        "pre-bump.hooks should have template content after --force"
    );
}