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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! Scaffold generation for `git std init`.
//!
//! Owns: `.git-std.toml` config, lifecycle hook templates, agent skill files
//! and their `.claude/skills/` symlinks.

use std::path::Path;

use crate::ui;

use super::{
    AGENTS_SKILL_BUMP_DIR, AGENTS_SKILL_COMMIT_DIR, CLAUDE_SKILL_BUMP, CLAUDE_SKILL_COMMIT,
    CONFIG_FILE, FileResult,
};

// ---------------------------------------------------------------------------
// Writers
// ---------------------------------------------------------------------------

/// Write `.git-std.toml` starter config with taplo schema directive.
///
/// When the file does not exist, writes the full template.
/// When it exists and `force` is false, merges missing default keys
/// into the existing config (backing up the original first).
/// When `force` is true, overwrites entirely.
pub fn write_config_file(root: &Path, force: bool) -> FileResult {
    let path = root.join(CONFIG_FILE);

    if !path.exists() || force {
        let template = generate_config_template();
        if let Err(e) = std::fs::write(&path, &template) {
            ui::error(&format!("cannot write {CONFIG_FILE}: {e}"));
            return FileResult::Error;
        }
        return FileResult::Created;
    }

    // File exists and !force → attempt smart merge.
    let existing_content = match std::fs::read_to_string(&path) {
        Ok(c) => c,
        Err(e) => {
            ui::error(&format!("cannot read {CONFIG_FILE}: {e}"));
            return FileResult::Error;
        }
    };

    let mut existing: toml::Table = match existing_content.parse() {
        Ok(t) => t,
        Err(_) => {
            // Unparseable config — don't touch it.
            return FileResult::Skipped;
        }
    };

    let defaults = default_config_table();
    let added = merge_defaults(&mut existing, &defaults);

    if added.is_empty() {
        return FileResult::Skipped;
    }

    // Back up existing config.
    let backup = root.join(format!("{CONFIG_FILE}.backup"));
    if let Err(e) = std::fs::copy(&path, &backup) {
        ui::error(&format!("cannot back up {CONFIG_FILE}: {e}"));
        return FileResult::Error;
    }

    // Preserve the schema directive and write merged config.
    let schema_line =
        "#:schema https://driftsys.github.io/git-std/schemas/v1/git-std.schema.json\n\n";
    let merged = format!(
        "{schema_line}{}",
        toml::to_string_pretty(&existing).unwrap_or_default()
    );

    if let Err(e) = std::fs::write(&path, &merged) {
        ui::error(&format!("cannot write {CONFIG_FILE}: {e}"));
        return FileResult::Error;
    }

    for key in &added {
        ui::info(&format!("  added default: {key}"));
    }

    FileResult::Created
}

/// Create a symlink from `.agents/skills/<name>/SKILL.md` to `../../skills/<name>.md`.
pub fn write_skill_source(
    root: &Path,
    skill_dir: &str,
    skill_name: &str,
    force: bool,
) -> FileResult {
    let skill_path = root.join(skill_dir).join("SKILL.md");

    // Ensure parent directory exists
    if let Some(parent) = skill_path.parent()
        && let Err(e) = std::fs::create_dir_all(parent)
    {
        ui::error(&format!("cannot create {}: {e}", parent.display()));
        return FileResult::Error;
    }

    // Remove existing file/symlink if force is set
    if skill_path.exists() || skill_path.symlink_metadata().is_ok() {
        if !force {
            return FileResult::Skipped;
        }
        if let Err(e) = std::fs::remove_file(&skill_path) {
            ui::error(&format!("cannot remove {}: {e}", skill_path.display()));
            return FileResult::Error;
        }
    }

    // Create relative symlink: .agents/skills/std-commit/SKILL.md → ../../skills/std-commit.md
    let relative_target = format!("../../skills/{skill_name}.md");

    #[cfg(unix)]
    {
        use std::os::unix::fs::symlink;
        if let Err(e) = symlink(&relative_target, &skill_path) {
            ui::error(&format!(
                "cannot create symlink {}: {e}",
                skill_path.display()
            ));
            return FileResult::Error;
        }
    }
    #[cfg(not(unix))]
    {
        // On non-Unix, write a text file pointing to the target as a fallback
        if let Err(e) = std::fs::write(&skill_path, format!("{relative_target}\n")) {
            ui::error(&format!("cannot write {}: {e}", skill_path.display()));
            return FileResult::Error;
        }
    }

    FileResult::Created
}

/// Create a `.claude/skills/` symlink pointing back to `.agents/skills/`.
pub fn write_skill_symlink(root: &Path, link: &str, target: &str, force: bool) -> FileResult {
    // Ensure .claude/skills/ exists
    let link_path = root.join(link);
    if let Some(parent) = link_path.parent()
        && let Err(e) = std::fs::create_dir_all(parent)
    {
        ui::error(&format!("cannot create {}: {e}", parent.display()));
        return FileResult::Error;
    }
    if link_path.exists() || link_path.symlink_metadata().is_ok() {
        if !force {
            return FileResult::Skipped;
        }
        let _ = std::fs::remove_file(&link_path);
    }
    // Relative symlink: from .claude/skills/std-commit → ../../.agents/skills/std-commit
    let relative_target = format!("../../{target}");
    #[cfg(unix)]
    {
        use std::os::unix::fs::symlink;
        if let Err(e) = symlink(&relative_target, &link_path) {
            ui::error(&format!("cannot create symlink {link}: {e}"));
            return FileResult::Error;
        }
    }
    #[cfg(not(unix))]
    {
        // On non-Unix, write a text file pointing to the target as a fallback
        if let Err(e) = std::fs::write(&link_path, format!("{relative_target}\n")) {
            ui::error(&format!("cannot write {link}: {e}"));
            return FileResult::Error;
        }
    }
    FileResult::Created
}

/// Return all skill definitions for scaffolding.
///
/// Each tuple: `(skill_name, skill_dir, claude_link)`.
pub fn skill_definitions() -> Vec<(&'static str, &'static str, &'static str)> {
    vec![
        ("std-commit", AGENTS_SKILL_COMMIT_DIR, CLAUDE_SKILL_COMMIT),
        ("std-bump", AGENTS_SKILL_BUMP_DIR, CLAUDE_SKILL_BUMP),
    ]
}

// ---------------------------------------------------------------------------
// Generated content
// ---------------------------------------------------------------------------

/// Generate the `.git-std.toml` starter config content.
fn generate_config_template() -> String {
    "\
#:schema https://driftsys.github.io/git-std/schemas/v1/git-std.schema.json

# scheme = \"semver\"          # semver | calver | patch
# strict = false             # enforce types/scopes without --strict flag
# types = [\"feat\", \"fix\", \"docs\", \"style\", \"refactor\",
#           \"perf\", \"test\", \"chore\", \"ci\", \"build\", \"revert\"]
# scopes = \"auto\"            # \"auto\" | [\"scope1\", \"scope2\"] | omit
"
    .to_string()
}

/// Generate a bump lifecycle hook template for the given hook name.
pub fn generate_lifecycle_hook_template(hook_name: &str) -> String {
    match hook_name {
        "pre-bump" => "\
# git-std hooks — pre-bump.hooks
#
# Runs before version detection. Non-zero exit aborts the bump.
# Use for: guard checks (clean tree, correct branch, tests pass).
#
#   !  required   abort bump on failure
#   ?  advisory   warn on failure, never abort
#
# Examples:
#   ! cargo test --workspace
#   ! git diff --exit-code   # abort if working tree is dirty
#
"
        .to_string(),
        "post-version" => "\
# git-std hooks — post-version.hooks
#
# Runs after version files are updated. $1 is the new version string.
# Use for: building artifacts, stamping binaries, generating manifests.
#
#   !  required   abort bump on failure
#   ?  advisory   warn on failure, never abort
#
# Examples:
#   ! cargo build --release
#   ? cp target/release/mybin dist/
#
"
        .to_string(),
        "post-changelog" => "\
# git-std hooks — post-changelog.hooks
#
# Runs after CHANGELOG.md is written, before staging and commit.
# Use for: linting or reformatting the changelog.
#
#   !  required   abort bump on failure
#   ?  advisory   warn on failure, never abort
#
# Examples:
#   ? npx markdownlint CHANGELOG.md
#
"
        .to_string(),
        "post-bump" => "\
# git-std hooks — post-bump.hooks
#
# Runs after commit and tag are created (and after push if --push).
# Use for: publishing, deployment, notifications.
#
#   !  required   report failure
#   ?  advisory   warn on failure, always continues
#
# Examples:
#   ! cargo publish
#   ? curl -X POST https://hooks.slack.com/...
#
"
        .to_string(),
        _ => format!("# git-std hooks — {hook_name}.hooks\n"),
    }
}

// ---------------------------------------------------------------------------
// Config merge helpers
// ---------------------------------------------------------------------------

/// Build a TOML table of default config values.
fn default_config_table() -> toml::Table {
    let mut t = toml::Table::new();
    t.insert("scheme".into(), toml::Value::String("semver".into()));
    t.insert("strict".into(), toml::Value::Boolean(false));
    t.insert(
        "types".into(),
        toml::Value::Array(
            [
                "feat", "fix", "docs", "style", "refactor", "perf", "test", "chore", "ci", "build",
                "revert",
            ]
            .iter()
            .map(|s| toml::Value::String((*s).to_string()))
            .collect(),
        ),
    );
    t.insert("scopes".into(), toml::Value::String("auto".into()));
    t
}

/// Deep-merge `defaults` into `existing`, adding only missing keys.
///
/// For table values, recurses into sub-tables. For all other types,
/// the existing value always wins. Returns the list of keys that were added.
fn merge_defaults(existing: &mut toml::Table, defaults: &toml::Table) -> Vec<String> {
    let mut added = Vec::new();
    for (key, default_val) in defaults {
        match existing.get_mut(key) {
            Some(toml::Value::Table(existing_sub)) => {
                if let toml::Value::Table(default_sub) = default_val {
                    let sub_added = merge_defaults(existing_sub, default_sub);
                    if !sub_added.is_empty() {
                        added.push(key.clone());
                    }
                }
            }
            Some(_) => {}
            None => {
                existing.insert(key.clone(), default_val.clone());
                added.push(key.clone());
            }
        }
    }
    added
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn config_template_has_schema_directive() {
        let t = generate_config_template();
        assert!(t.starts_with("#:schema "));
        assert!(t.contains("git-std.schema.json"));
    }

    #[test]
    fn config_template_has_commented_fields() {
        let t = generate_config_template();
        assert!(t.contains("# scheme"));
        assert!(t.contains("# strict"));
        assert!(t.contains("# types"));
        assert!(t.contains("# scopes"));
    }

    #[test]
    fn lifecycle_hook_templates_have_headers() {
        for hook in super::super::LIFECYCLE_HOOKS {
            let t = generate_lifecycle_hook_template(hook);
            assert!(
                t.contains(&format!("# git-std hooks — {hook}.hooks")),
                "{hook}.hooks template should have header"
            );
            assert!(
                t.contains("!  required"),
                "{hook}.hooks should document ! sigil"
            );
            assert!(
                t.contains("?  advisory"),
                "{hook}.hooks should document ? sigil"
            );
        }
    }

    #[test]
    fn pre_bump_template_mentions_when_it_runs() {
        let t = generate_lifecycle_hook_template("pre-bump");
        assert!(t.contains("before version detection"));
        assert!(t.contains("abort bump on failure"));
    }

    #[test]
    fn post_version_template_mentions_version_arg() {
        let t = generate_lifecycle_hook_template("post-version");
        assert!(t.contains("$1 is the new version string"));
    }

    #[test]
    fn post_changelog_template_mentions_when_it_runs() {
        let t = generate_lifecycle_hook_template("post-changelog");
        assert!(t.contains("after CHANGELOG.md is written"));
    }

    #[test]
    fn post_bump_template_mentions_when_it_runs() {
        let t = generate_lifecycle_hook_template("post-bump");
        assert!(t.contains("after commit and tag are created"));
    }

    #[test]
    fn std_commit_skill_has_frontmatter() {
        let s = include_str!("../../../../../skills/std-commit.md");
        assert!(s.starts_with("---\nname: std-commit\n"));
        assert!(s.contains("git std --context"));
        assert!(s.contains("git std commit"));
    }

    #[test]
    fn std_commit_skill_includes_message_guidelines() {
        let s = include_str!("../../../../../skills/std-commit.md");
        assert!(
            s.contains("50 characters"),
            "skill should document 50 char limit"
        );
        assert!(
            s.contains("72 characters"),
            "skill should document 72 char body wrap"
        );
        assert!(s.contains("--body"), "skill should mention --body flag");
        assert!(
            s.contains("what") && s.contains("why"),
            "skill should explain what/why guidance"
        );
    }

    #[test]
    fn std_bump_skill_has_frontmatter() {
        let s = include_str!("../../../../../skills/std-bump.md");
        assert!(s.starts_with("---\nname: std-bump\n"));
        assert!(s.contains("git std bump --dry-run"));
        assert!(s.contains("--push"));
    }

    #[test]
    fn merge_adds_missing_keys() {
        let mut existing: toml::Table = r#"scheme = "semver""#.parse().unwrap();
        let defaults = default_config_table();
        let added = merge_defaults(&mut existing, &defaults);
        assert!(added.contains(&"strict".to_string()));
        assert!(added.contains(&"types".to_string()));
        assert!(!added.contains(&"scheme".to_string()));
        assert_eq!(
            existing["scheme"],
            toml::Value::String("semver".to_string())
        );
    }

    #[test]
    fn merge_preserves_user_values() {
        let mut existing: toml::Table = r#"
scheme = "calver"
types = ["feat", "fix"]
"#
        .parse()
        .unwrap();
        let defaults = default_config_table();
        merge_defaults(&mut existing, &defaults);
        assert_eq!(
            existing["scheme"],
            toml::Value::String("calver".to_string())
        );
        // User's shorter types list is preserved
        if let toml::Value::Array(arr) = &existing["types"] {
            assert_eq!(arr.len(), 2);
        } else {
            panic!("types should be an array");
        }
    }

    #[test]
    fn merge_no_changes_returns_empty() {
        let mut existing: toml::Table = r#"
scheme = "semver"
strict = false
types = ["feat"]
scopes = "auto"
"#
        .parse()
        .unwrap();
        let defaults = default_config_table();
        let added = merge_defaults(&mut existing, &defaults);
        assert!(added.is_empty());
    }

    #[test]
    fn write_config_merges_existing() {
        let dir = tempfile::tempdir().unwrap();
        let config_path = dir.path().join(CONFIG_FILE);
        std::fs::write(&config_path, "scheme = \"calver\"\n").unwrap();

        let result = write_config_file(dir.path(), false);
        assert!(matches!(result, FileResult::Created));

        let content = std::fs::read_to_string(&config_path).unwrap();
        assert!(content.contains("scheme = \"calver\""));
        assert!(content.contains("strict"));
        assert!(content.contains("types"));

        // Backup should exist
        assert!(dir.path().join(format!("{CONFIG_FILE}.backup")).exists());
    }
}