git-std 0.11.11

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
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
use std::process::Command;

use serde::Serialize;
use yansi::Paint;

use standard_githooks::{HookCommand, HookMode, Prefix, default_mode, substitute_msg};

use crate::app::OutputFormat;
use crate::ui;

use super::read_and_parse_hooks;
use super::stash;

/// The result of executing a single hook command.
struct CommandResult {
    /// The exit code (0 = success).
    exit_code: Option<i32>,
    /// Whether this command was advisory.
    advisory: bool,
}

/// JSON output schema for a single executed command.
#[derive(Serialize)]
struct CommandExecutionJson {
    command: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    glob: Option<String>,
    exit_code: Option<i32>,
    success: bool,
    advisory: bool,
    skipped: bool,
}

/// JSON output schema for the hooks run result.
#[derive(Serialize)]
struct HooksRunResultJson {
    hook: String,
    commands: Vec<CommandExecutionJson>,
    passed: usize,
    failed: usize,
    advisory_warnings: usize,
    skipped: usize,
}

/// Print contextual hints after a hook failure.
///
/// Shows how to skip the current hook, how to skip all hooks, and how
/// to disable a specific command in the `.hooks` file.
fn print_failure_hints(hook: &str) {
    let skip_flag = match hook {
        "pre-commit" | "commit-msg" => "git commit --no-verify",
        "pre-push" => "git push --no-verify",
        _ => &format!(
            "GIT_STD_SKIP_HOOKS=1 git {}",
            hook.trim_start_matches("pre-").trim_start_matches("post-")
        ),
    };
    ui::hint(&format!("to skip this hook:    {skip_flag}"));
    ui::hint("to skip all hooks:    GIT_STD_SKIP_HOOKS=1 git ...");
    ui::hint(&format!(
        "to disable a command: comment it out in .githooks/{hook}.hooks"
    ));
}

/// Format a command's display text, appending the glob pattern if present.
fn format_display(command_text: &str, glob: Option<&str>) -> String {
    match glob {
        Some(g) => format!("{command_text} ({g})"),
        None => command_text.to_string(),
    }
}

/// Execute a single hook command, optionally printing its result line.
///
/// When `quiet` is true, the command runs silently (for JSON output mode).
/// Otherwise animates a spinner while the command runs and prints the result.
///
/// `staged_files` is passed as `$@` to the shell command (positional
/// parameters). For `pre-commit` this is the list of staged files; for
/// other hooks it is an empty slice.
///
/// Returns the [`CommandResult`] and whether the command failed (non-advisory).
fn execute_and_print(
    cmd: &HookCommand,
    msg_path: &str,
    staged_files: &[String],
    quiet: bool,
) -> (CommandResult, bool) {
    let command_text = substitute_msg(&cmd.command, msg_path);
    let is_advisory = cmd.prefix == Prefix::Advisory;
    let display = format_display(&command_text, cmd.glob.as_deref());

    // Run the command, capturing output only on TTY (to show on failure).
    // On non-TTY (tests, CI), let output inherit so it's visible.
    let (exit_code, captured) = if !quiet && ui::is_tty() {
        // TTY: use spinner and capture output to show only on failure
        ui::spin_while(&display, || {
            super::exec_sh_capture(&command_text, staged_files)
        })
    } else if !quiet {
        // Non-TTY: show pending, let output inherit, print result
        ui::pending_non_tty(&display);
        let code = super::exec_sh(&command_text, staged_files);
        (code, String::new())
    } else {
        // JSON / quiet mode: no spinner, no output capture or display.
        let code = super::exec_sh(&command_text, staged_files);
        (code, String::new())
    };

    let success = exit_code == Some(0);

    // Print the result line and dump captured output on failure/advisory.
    if !quiet {
        if success {
            ui::info(&format!("{} {}", ui::pass(), display));
        } else if is_advisory {
            let info = match exit_code {
                Some(code) => format!("(advisory, exit {code})"),
                None => "(advisory, killed)".to_string(),
            };
            ui::info(&format!("{} {} {}", ui::warn(), display, info.yellow()));
        } else {
            let info = match exit_code {
                Some(code) => format!("(exit {code})"),
                None => "(killed)".to_string(),
            };
            ui::info(&format!("{} {} {}", ui::fail(), display, info.red()));
        }

        // Dump captured output below the result line on failure or advisory.
        if !success && !captured.is_empty() {
            ui::blank();
            for line in captured.lines() {
                ui::detail(line);
            }
            ui::blank();
        }
    }

    let failed = !success && !is_advisory;

    (
        CommandResult {
            exit_code,
            advisory: is_advisory,
        },
        failed,
    )
}

/// Run the `hooks run <hook>` subcommand. Returns the process exit code.
///
/// Reads `.githooks/<hook>.hooks`, parses commands, executes them
/// according to the hook's default mode and per-command prefix
/// overrides, and prints a summary.
pub fn run(hook: &str, args: &[String], format: OutputFormat) -> i32 {
    // Allow skipping all hook execution via environment variable.
    if let Ok(val) = std::env::var("GIT_STD_SKIP_HOOKS")
        && (val == "1" || val.eq_ignore_ascii_case("true"))
    {
        if format == OutputFormat::Json {
            let result = HooksRunResultJson {
                hook: hook.to_string(),
                commands: vec![],
                passed: 0,
                failed: 0,
                advisory_warnings: 0,
                skipped: 0,
            };
            println!("{}", serde_json::to_string(&result).unwrap());
        } else {
            ui::info(&format!(
                "{} hooks skipped (GIT_STD_SKIP_HOOKS)",
                ui::warn()
            ));
        }
        return 0;
    }

    let hooks_dir = match super::hooks_dir() {
        Ok(d) => d,
        Err(code) => return code,
    };

    let commands = match read_and_parse_hooks(&hooks_dir, hook) {
        Ok(c) => c,
        Err(code) => return code,
    };
    if commands.is_empty() {
        return 0;
    }

    let mode = default_mode(hook);

    // Determine the msg_path from args (first argument after --)
    let msg_path = args.first().map(|s| s.as_str()).unwrap_or("");

    // For pre-commit: fetch staged files for $@ passing, stash dance, and glob filtering.
    let staged_files: Vec<String> = if hook == "pre-commit" {
        stash::fetch_staged("ACMR")
    } else {
        Vec::new()
    };

    // Collect file list for glob filtering (lazy -- only fetched if needed).
    // For pre-commit, reuse the already-fetched staged files to avoid a duplicate git call.
    let file_list: Option<Vec<String>> = if commands.iter().any(|c| c.glob.is_some()) {
        if hook == "pre-commit" {
            Some(staged_files.clone())
        } else {
            stash::fetch_tracked_files()
        }
    } else {
        None
    };

    // Determine whether we need the stash dance.
    // Only applies for pre-commit hooks that contain at least one `~` command.
    let has_fix_commands = commands.iter().any(|c| c.prefix == Prefix::Fix);
    let use_stash_dance = hook == "pre-commit" && has_fix_commands;

    // For non-pre-commit hooks, warn about `~` commands and treat them as `!`.
    if hook != "pre-commit" && has_fix_commands {
        ui::warning("~ prefix is only supported in pre-commit — treating as !");
    }

    // Reject submodule entries when fix mode is active (#283).
    // The stash dance does not handle submodule state correctly.
    if use_stash_dance && stash::has_staged_submodules() {
        ui::error("fix mode (~) does not support submodule entries");
        ui::hint(
            "remove ~ prefix from commands in .githooks/pre-commit.hooks, \
             or unstage the submodule",
        );
        return 1;
    }

    // Temporarily unstage renames before the stash dance to prevent corruption
    // (#387). git stash apply incorrectly splits renames into separate staged
    // additions and unstaged deletions. We unstage them before stash, then
    // re-stage after, so they bypass the stash corruption entirely.
    let staged_rename_targets = if use_stash_dance {
        stash::fetch_staged_rename_targets()
    } else {
        Vec::new()
    };

    if use_stash_dance && !stash::unstage_renames(&staged_rename_targets) {
        ui::error("failed to unstage renames before stash dance");
        print_failure_hints(hook);
        return 1;
    }

    // Capture files staged for deletion before the stash dance.
    // The stash dance restores deleted files to disk; we must re-delete them
    // in the index afterwards to preserve the user's `git rm` intent (#268).
    let staged_deletions: Vec<String> = if use_stash_dance {
        stash::fetch_staged("D")
    } else {
        Vec::new()
    };

    // Perform the stash dance if needed.
    // stash_active tracks whether a stash entry was actually created.
    let stash_active = if use_stash_dance {
        stash::stash_push()
        // If stash_push returns false (nothing to stash or error), we skip
        // the stash dance but still run commands normally.
    } else {
        false
    };

    if use_stash_dance && stash_active && !stash::stash_apply() {
        ui::error("stash apply failed — working tree has conflicting unstaged changes");
        ui::hint("commit or stash your unstaged changes first, then retry");
        stash::stash_drop();
        print_failure_hints(hook);
        return 1;
    }

    let mut results: Vec<CommandResult> = Vec::new();
    let mut json_results: Vec<CommandExecutionJson> = Vec::new();
    let mut has_failure = false;

    let is_json = format == OutputFormat::Json;

    for cmd in &commands {
        // Glob filtering: skip command if glob doesn't match any files.
        if let Some(ref glob) = cmd.glob
            && let Some(ref files) = file_list
        {
            let refs: Vec<&str> = files.iter().map(|s| s.as_str()).collect();
            if !standard_githooks::matches_any(glob, &refs) {
                if is_json {
                    json_results.push(CommandExecutionJson {
                        command: cmd.command.clone(),
                        glob: cmd.glob.clone(),
                        exit_code: None,
                        success: false,
                        advisory: cmd.prefix == Prefix::Advisory,
                        skipped: true,
                    });
                }
                continue;
            }
        }

        // Resolve `~` prefix:
        // - In pre-commit with stash dance: treat as FailFast for pass/fail logic.
        // - In other hooks: already warned above, treat as FailFast.
        let effective_prefix = if cmd.prefix == Prefix::Fix {
            Prefix::FailFast
        } else {
            cmd.prefix
        };

        // Determine the effective mode for this command.
        let effective_mode = match effective_prefix {
            Prefix::FailFast => HookMode::FailFast,
            Prefix::Advisory => HookMode::Collect, // advisory always runs
            Prefix::Default => mode,
            Prefix::Fix => unreachable!("Fix prefix resolved to FailFast above"),
        };

        // Build a temporary cmd view with the resolved prefix for execute_and_print.
        let resolved_cmd = HookCommand {
            prefix: effective_prefix,
            command: cmd.command.clone(),
            glob: cmd.glob.clone(),
        };

        let (result, failed) = execute_and_print(&resolved_cmd, msg_path, &staged_files, is_json);
        if failed {
            has_failure = true;
        }

        if is_json {
            let command_text = substitute_msg(&cmd.command, msg_path);
            json_results.push(CommandExecutionJson {
                command: command_text,
                glob: cmd.glob.clone(),
                exit_code: result.exit_code,
                success: result.exit_code == Some(0),
                advisory: result.advisory,
                skipped: false,
            });
        }

        results.push(result);

        // In fail-fast mode, abort on first non-advisory failure
        if failed && effective_mode == HookMode::FailFast {
            // Re-stage formatted files and clean up stash before returning.
            if use_stash_dance {
                if !stash::restage_files(&staged_files)
                    || !stash::restage_deletions(&staged_deletions)
                {
                    // Already returning 1 for the fail-fast failure, but
                    // ensure the stash is cleaned up before returning.
                    if stash_active {
                        stash::stash_drop();
                    }
                    ui::blank();
                    print_failure_hints(hook);
                    return 1;
                }
                if stash_active {
                    stash::stash_drop();
                }
            }

            // Print remaining commands as skipped
            let remaining = commands.len() - results.len();
            if is_json {
                // Add remaining commands as skipped
                for remaining_cmd in commands.iter().skip(results.len()) {
                    let command_text = substitute_msg(&remaining_cmd.command, msg_path);
                    json_results.push(CommandExecutionJson {
                        command: command_text,
                        glob: remaining_cmd.glob.clone(),
                        exit_code: None,
                        success: false,
                        advisory: remaining_cmd.prefix == Prefix::Advisory,
                        skipped: true,
                    });
                }
                return emit_json_result(hook, &json_results, has_failure);
            }
            if remaining > 0 {
                ui::blank();
                ui::info(&format!(
                    "{} remaining {} skipped (fail-fast)",
                    remaining,
                    if remaining == 1 {
                        "command"
                    } else {
                        "commands"
                    },
                ));
            }
            ui::blank();
            print_failure_hints(hook);
            return 1;
        }
    }

    // Complete the fix-mode finalisation after all commands have run.
    if use_stash_dance {
        // Re-stage the originally-staged files (picks up formatter changes).
        // This always runs when fix-mode is active, whether or not a stash
        // was created (no stash means no unstaged changes to protect, but
        // re-staging is still needed to pick up formatter output).
        if !stash::restage_files(&staged_files) || !stash::restage_deletions(&staged_deletions) {
            if stash_active {
                stash::stash_drop();
            }
            print_failure_hints(hook);
            return 1;
        }

        if stash_active {
            // Warn about any unstaged files that the formatter also touched.
            // These are files in `git diff --name-only` that were NOT in
            // the original staged set.
            let now_unstaged = stash::fetch_unstaged_files();
            for file in &now_unstaged {
                if !staged_files.contains(file) {
                    ui::warning(&format!("{file}: unstaged changes were also formatted"));
                }
            }

            stash::stash_drop();
        }

        // Re-stage renamed files after the stash dance completes.
        // They were unstaged before to prevent stash corruption (#387).
        if !staged_rename_targets.is_empty() {
            let mut cmd = Command::new("git");
            cmd.args(["add", "--"]);
            for f in &staged_rename_targets {
                cmd.arg(f);
            }
            match cmd.status() {
                Ok(s) if !s.success() => {
                    ui::error("failed to re-stage renamed files after formatting");
                    print_failure_hints(hook);
                    return 1;
                }
                Err(e) => {
                    ui::error(&format!("failed to re-stage renamed files: {e}"));
                    print_failure_hints(hook);
                    return 1;
                }
                _ => {}
            }
        }
    }

    // Print summary
    if is_json {
        return emit_json_result(hook, &json_results, has_failure);
    }

    let failed_count = results
        .iter()
        .filter(|r| r.exit_code != Some(0) && !r.advisory)
        .count();
    let advisory_count = results
        .iter()
        .filter(|r| r.exit_code != Some(0) && r.advisory)
        .count();

    if failed_count > 0 || advisory_count > 0 {
        ui::blank();
        let mut parts = Vec::new();
        if failed_count > 0 {
            parts.push(format!("{failed_count} failed"));
        }
        if advisory_count > 0 {
            parts.push(format!(
                "{advisory_count} advisory {}",
                if advisory_count == 1 {
                    "warning"
                } else {
                    "warnings"
                }
            ));
        }
        ui::info(&parts.join(", "));
    }

    if has_failure {
        print_failure_hints(hook);
        1
    } else {
        0
    }
}

fn emit_json_result(hook: &str, json_results: &[CommandExecutionJson], has_failure: bool) -> i32 {
    let passed = json_results
        .iter()
        .filter(|r| r.success && !r.skipped)
        .count();
    let failed = json_results
        .iter()
        .filter(|r| !r.success && !r.advisory && !r.skipped)
        .count();
    let advisory_warnings = json_results
        .iter()
        .filter(|r| !r.success && r.advisory && !r.skipped)
        .count();
    let skipped = json_results.iter().filter(|r| r.skipped).count();

    let result = HooksRunResultJson {
        hook: hook.to_string(),
        commands: json_results
            .iter()
            .map(|r| CommandExecutionJson {
                command: r.command.clone(),
                glob: r.glob.clone(),
                exit_code: r.exit_code,
                success: r.success,
                advisory: r.advisory,
                skipped: r.skipped,
            })
            .collect(),
        passed,
        failed,
        advisory_warnings,
        skipped,
    };
    println!("{}", serde_json::to_string(&result).unwrap());
    if has_failure { 1 } else { 0 }
}