ferrous-forge 1.9.6

System-wide Rust development standards enforcer
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
//! Git hooks installation and cargo interception
//!
//! This module provides mandatory blocking hooks that prevent commits and pushes
//! when safety checks fail. Blocking is the default behavior with bypass available
//! via explicit command.
//!
//! @task T017
//! @epic T014

use crate::Result;
use std::fs;
use std::path::Path;

/// Display installation header
pub fn display_install_header() {
    println!("๐Ÿ”ง Installing Ferrous Forge Mandatory Safety Hooks");
    println!("{}", "=".repeat(55));
    println!();
    println!("These hooks will BLOCK commits/pushes that fail safety checks.");
    println!("Use 'ferrous-forge safety bypass' if you need to bypass temporarily.");
    println!();
}

/// Validate git repository and create hooks directory
///
/// # Errors
///
/// Returns an error if the project path is not a git repository or the
/// hooks directory cannot be created.
pub fn validate_git_repo_and_create_hooks_dir(project_path: &Path) -> Result<std::path::PathBuf> {
    let git_dir = project_path.join(".git");
    if !git_dir.exists() {
        return Err(crate::error::Error::Config(
            "Not a git repository. Run 'git init' first.".to_string(),
        ));
    }

    let hooks_dir = git_dir.join("hooks");
    fs::create_dir_all(&hooks_dir)?;
    Ok(hooks_dir)
}

/// Install pre-commit hook
///
/// # Errors
///
/// Returns an error if the hook file cannot be written or permissions
/// cannot be set.
pub fn install_pre_commit_hook(hooks_dir: &Path, force: bool) -> Result<()> {
    let pre_commit_path = hooks_dir.join("pre-commit");

    if pre_commit_path.exists() && !force {
        let content = fs::read_to_string(&pre_commit_path)?;
        if !content.contains("Ferrous Forge") {
            println!("โš ๏ธ  Existing pre-commit hook found. Use --force to overwrite.");
            return Ok(());
        }
    }

    let content = get_pre_commit_hook_content();
    install_hook(&pre_commit_path, &content)?;
    println!("โœ… Installed blocking pre-commit hook");
    Ok(())
}

/// Install pre-push hook
///
/// # Errors
///
/// Returns an error if the hook file cannot be written or permissions
/// cannot be set.
pub fn install_pre_push_hook(hooks_dir: &Path, force: bool) -> Result<()> {
    let pre_push_path = hooks_dir.join("pre-push");

    if pre_push_path.exists() && !force {
        let content = fs::read_to_string(&pre_push_path)?;
        if !content.contains("Ferrous Forge") {
            println!("โš ๏ธ  Existing pre-push hook found. Use --force to overwrite.");
            return Ok(());
        }
    }

    let content = get_pre_push_hook_content();
    install_hook(&pre_push_path, &content)?;
    println!("โœ… Installed blocking pre-push hook");
    Ok(())
}

/// Install cargo interception system
///
/// # Errors
///
/// Returns an error if the home directory cannot be determined, the install
/// directory cannot be created, or the wrapper script fails to write.
pub fn install_cargo_interception(force: bool) -> Result<()> {
    let home_dir = dirs::home_dir()
        .ok_or_else(|| crate::Error::Config("Unable to determine home directory".into()))?;

    let install_dir = home_dir.join(".ferrous-forge").join("bin");
    fs::create_dir_all(&install_dir)?;

    let wrapper_path = install_dir.join("cargo");

    if wrapper_path.exists() && !force {
        println!("โš ๏ธ  Cargo wrapper already exists. Use --force to overwrite.");
        return Ok(());
    }

    let wrapper_content = crate::cargo_intercept::wrapper::get_publish_wrapper_content();

    fs::write(&wrapper_path, wrapper_content)?;
    set_executable_permissions(&wrapper_path)?;

    println!("โœ… Installed cargo publish interception");
    println!("   Add {} to your PATH to enable", install_dir.display());

    Ok(())
}

/// Display installation success message
pub fn display_install_success(cargo_installed: bool) {
    println!();
    println!("๐Ÿ›ก๏ธ  Mandatory Safety Hooks Installed Successfully!");
    println!();
    println!("What happens now:");
    println!("  โ€ข git commit  โ†’ runs validation โ†’ BLOCKS if fails");
    println!("  โ€ข git push    โ†’ runs tests + validation โ†’ BLOCKS if fails");
    println!();
    println!("To bypass temporarily (requires reason):");
    println!("  ferrous-forge safety bypass --stage=pre-commit --reason=\"WIP\"");
    println!("  ferrous-forge safety bypass --stage=pre-push --reason=\"Emergency fix\"");
    println!();
    println!("To bypass for a single git operation:");
    println!("  git commit --no-verify  โš ๏ธ  Use sparingly!");
    println!();

    if cargo_installed {
        println!("To enable cargo publish interception:");
        println!("  Add ~/.ferrous-forge/bin to your PATH");
        println!();
    }

    println!("To uninstall:");
    println!("  ferrous-forge safety uninstall");
}

/// Get pre-commit hook content with mandatory blocking
fn get_pre_commit_hook_content() -> &'static str {
    r#"#!/bin/bash
# Ferrous Forge Mandatory Safety Pipeline - Pre-Commit Hook
# @task T017 @epic T014
# 
# This hook BLOCKS commits if safety checks fail.
# Use 'ferrous-forge safety bypass --stage=pre-commit --reason="..."' to bypass.

set -e

echo ""
echo "๐Ÿ›ก๏ธ  Ferrous Forge Safety Pipeline - Pre-Commit"
echo "โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•"

# Check if ferrous-forge is installed
if ! command -v ferrous-forge >/dev/null 2>&1; then
    echo ""
    echo "โš ๏ธ  Ferrous Forge not found in PATH"
    echo "   Install with: cargo install ferrous-forge"
    echo "   Skipping safety checks..."
    exit 0
fi

# Check for active bypass
BYPASS_CHECK=$(ferrous-forge safety check-bypass --stage=pre-commit 2>/dev/null || echo "none")
if [ "$BYPASS_CHECK" = "active" ]; then
    echo ""
    echo "โš ๏ธ  Safety checks bypassed (active bypass found)"
    exit 0
fi

# Run Ferrous Forge validation
echo ""
echo "๐Ÿ” Running mandatory validation checks..."
echo ""

if ! ferrous-forge validate --quiet 2>&1; then
    echo ""
    echo "โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•"
    echo "๐Ÿ›ก๏ธ  FERROUS FORGE BLOCKED COMMIT"
    echo "โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•"
    echo ""
    echo "Validation failed. Fix the issues above before committing."
    echo ""
    echo "How to fix:"
    echo "  1. Run 'ferrous-forge validate' to see all errors"
    echo "  2. Fix the reported violations"
    echo "  3. Try committing again"
    echo ""
    echo "To bypass (requires reason):"
    echo "  ferrous-forge safety bypass --stage=pre-commit --reason=\"WIP commit\""
    echo ""
    echo "To bypass for this commit only:"
    echo "  git commit --no-verify"
    echo ""
    exit 1
fi

# Run clippy checks
echo "๐Ÿ“Ž Running clippy checks..."
if ! cargo clippy -- -D warnings 2>/dev/null; then
    echo ""
    echo "โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•"
    echo "๐Ÿ›ก๏ธ  FERROUS FORGE BLOCKED COMMIT"
    echo "โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•"
    echo ""
    echo "Clippy checks failed. Fix the warnings before committing."
    echo ""
    echo "How to fix:"
    echo "  Run 'cargo clippy -- -D warnings' to see all issues"
    echo ""
    exit 1
fi

echo ""
echo "โœ… All safety checks passed! Commit allowed."
echo ""
exit 0
"#
}

/// Get pre-push hook content with mandatory blocking
fn get_pre_push_hook_content() -> &'static str {
    r#"#!/bin/bash
# Ferrous Forge Mandatory Safety Pipeline - Pre-Push Hook
# @task T017 @epic T014
#
# This hook BLOCKS pushes if safety checks fail.
# Use 'ferrous-forge safety bypass --stage=pre-push --reason="..."' to bypass.

set -e

echo ""
echo "๐Ÿ›ก๏ธ  Ferrous Forge Safety Pipeline - Pre-Push"
echo "โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•"

# Check if ferrous-forge is installed
if ! command -v ferrous-forge >/dev/null 2>&1; then
    echo ""
    echo "โš ๏ธ  Ferrous Forge not found in PATH"
    echo "   Install with: cargo install ferrous-forge"
    echo "   Skipping safety checks..."
    exit 0
fi

# Check for active bypass
BYPASS_CHECK=$(ferrous-forge safety check-bypass --stage=pre-push 2>/dev/null || echo "none")
if [ "$BYPASS_CHECK" = "active" ]; then
    echo ""
    echo "โš ๏ธ  Safety checks bypassed (active bypass found)"
    exit 0
fi

# Run comprehensive checks
echo ""
echo "๐Ÿ” Running comprehensive safety checks..."
echo ""

# Run tests
echo "๐Ÿงช Running tests..."
if ! cargo test --quiet 2>&1; then
    echo ""
    echo "โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•"
    echo "๐Ÿ›ก๏ธ  FERROUS FORGE BLOCKED PUSH"
    echo "โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•"
    echo ""
    echo "Tests failed. Fix failing tests before pushing."
    echo ""
    echo "How to fix:"
    echo "  Run 'cargo test' to see detailed test failures"
    echo ""
    echo "To bypass (requires reason):"
    echo "  ferrous-forge safety bypass --stage=pre-push --reason=\"Emergency fix\""
    echo ""
    exit 1
fi

# Run full validation
echo "๐Ÿ” Running validation..."
if ! ferrous-forge validate --quiet 2>&1; then
    echo ""
    echo "โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•"
    echo "๐Ÿ›ก๏ธ  FERROUS FORGE BLOCKED PUSH"
    echo "โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•"
    echo ""
    echo "Validation failed. Fix the issues before pushing."
    echo ""
    echo "How to fix:"
    echo "  1. Run 'ferrous-forge validate' to see all errors"
    echo "  2. Fix the reported violations"
    echo "  3. Try pushing again"
    echo ""
    echo "To bypass (requires reason):"
    echo "  ferrous-forge safety bypass --stage=pre-push --reason=\"Emergency fix\""
    echo ""
    exit 1
fi

# Run security audit
echo "๐Ÿ”’ Running security audit..."
if command -v cargo-audit >/dev/null 2>&1; then
    if ! cargo audit 2>/dev/null; then
        echo ""
        echo "โš ๏ธ  Security audit found vulnerabilities"
        echo "   Run 'cargo audit' for details"
        echo ""
    fi
else
    echo "   (cargo-audit not installed, skipping)"
fi

echo ""
echo "โœ… All safety checks passed! Push allowed."
echo ""
exit 0
"#
}

/// Install a hook file with proper permissions
fn install_hook(hook_path: &Path, content: &str) -> Result<()> {
    fs::write(hook_path, content)?;
    set_executable_permissions(hook_path)?;
    Ok(())
}

/// Set executable permissions on Unix systems
fn set_executable_permissions(#[allow(unused)] path: &Path) -> Result<()> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = fs::metadata(path)?.permissions();
        perms.set_mode(0o755);
        fs::set_permissions(path, perms)?;
    }
    Ok(())
}

/// Uninstall git hooks from a project
///
/// # Errors
///
/// Returns an error if the project path is not a git repository or hook
/// files cannot be removed.
pub fn uninstall_hooks(project_path: &Path) -> Result<()> {
    let hooks_dir = project_path.join(".git").join("hooks");

    for hook_name in &["pre-commit", "pre-push"] {
        let hook_path = hooks_dir.join(hook_name);
        if hook_path.exists() {
            let content = fs::read_to_string(&hook_path)?;
            if content.contains("Ferrous Forge") {
                fs::remove_file(&hook_path)?;
                println!("โœ… Removed {} hook", hook_name);
            }
        }
    }

    println!("\n๐Ÿ—‘๏ธ  Safety hooks uninstalled");
    Ok(())
}

/// Check if hooks are installed and active
///
/// Returns a tuple of (`pre_commit_installed`, `pre_push_installed`)
pub fn check_hooks_status(project_path: &Path) -> (bool, bool) {
    let hooks_dir = project_path.join(".git").join("hooks");

    let pre_commit_installed = if let Ok(content) = fs::read_to_string(hooks_dir.join("pre-commit"))
    {
        content.contains("Ferrous Forge")
    } else {
        false
    };

    let pre_push_installed = if let Ok(content) = fs::read_to_string(hooks_dir.join("pre-push")) {
        content.contains("Ferrous Forge")
    } else {
        false
    };

    (pre_commit_installed, pre_push_installed)
}

/// Get detailed hook status for display
pub fn get_hook_status_display(project_path: &Path) -> String {
    let (pre_commit, pre_push) = check_hooks_status(project_path);

    let mut status = String::from("Hook Status:\n");
    status.push_str(&format!(
        "  Pre-commit: {}\n",
        if pre_commit {
            "โœ… installed"
        } else {
            "โŒ not installed"
        }
    ));
    status.push_str(&format!(
        "  Pre-push:   {}\n",
        if pre_push {
            "โœ… installed"
        } else {
            "โŒ not installed"
        }
    ));

    if pre_commit && pre_push {
        status.push_str("\n๐Ÿ›ก๏ธ  Mandatory blocking is ACTIVE\n");
    } else if pre_commit || pre_push {
        status.push_str("\nโš ๏ธ  Partial installation detected\n");
    } else {
        status.push_str("\nโš ๏ธ  Safety hooks are NOT installed\n");
        status.push_str("   Run: ferrous-forge safety install\n");
    }

    status
}