suture-core 1.1.0

A patch-based version control system with semantic merge and format-aware drivers
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
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
//! Hook system for Suture — git-compatible hook execution.
//!
//! Hooks are executable scripts found in `.suture/hooks/<hook-name>` that are
//! run at specific points in the Suture workflow. A hook exits with 0 to allow
//! the operation to proceed, or non-zero to abort.
//!
//! # Supported Hooks
//!
//! | Hook            | When                                    | Description |
//! |-----------------|-----------------------------------------|-------------|
//! | `pre-commit`    | Before `suture commit` finalizes          | Validate staged content, run linters/tests |
//! | `post-commit`   | After `suture commit` succeeds            | Send notifications, trigger CI |
//! | `pre-push`      | Before `suture push` sends to hub        | Run tests, enforce policy |
//! | `post-push`     | After `suture push` succeeds             | Send notifications, trigger deployment |
//! | `pre-merge`     | Before `suture merge` finalizes          | Validate merge safety |
//! | `post-merge`    | After a clean `suture merge` succeeds       | Send notifications |
//! | `pre-rebase`    | Before `suture rebase` replays patches    | Validate rebase safety |
//! | `post-rebase`   | After `suture rebase` completes            | Send notifications |
//! | `pre-cherry-pick`| Before `suture cherry-pick` applies a patch| Validate cherry-pick safety |
//!
//! # Hook Configuration
//!
//! - Hooks directory: `.suture/hooks/` (or override via `core.hooksPath` in `.suture/config`)
//! - Hooks are executable files named exactly by their hook type (e.g., `pre-commit`)
//! - Non-executable files or missing hooks are silently skipped
//! - Hook scripts receive environment variables with context about the operation
//!
//! # Environment Variables
//!
//! | Variable              | Description                                    |
//! |-----------------------|------------------------------------------------|
//! | `SUTURE_HOOK`          | Name of the hook being run                     |
//! | `SUTURE_REPO`          | Absolute path to the repository root           |
//! | `SUTURE_AUTHOR`        | Current author name                           |
//! | `SUTURE_BRANCH`        | Current branch name                          |
//! | `SUTURE_HEAD`           | Full hash of the current HEAD patch            |
//! | `SUTURE_OPERATION`     | Operation being performed                     |
//! | `SUTURE_HOOK_DIR`      | Path to the hooks directory                   |
//! | `SUTURE_DIFF_FILES`    | Space-separated list of changed file paths (pre-commit) |
//! | `SUTURE_PUSH_REMOTE`   | Remote name (pre-push)                         |
//! | `SUTURE_PUSH_PATCHES`  | Number of patches being pushed (pre-push)     |
//! | `SUTURE_MERGE_SOURCE`   | Source branch name (pre-merge)                |
//! | `SUTURE_MERGE_HEAD`     | HEAD patch hash at merge time (pre-merge)   |
//! | `SUTURE_REVERT_TARGET`  | Patch being reverted (pre-revert)            |

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::process::Stdio;

/// The result of running a hook.
#[derive(Debug, Clone)]
pub struct HookResult {
    pub hook_name: String,
    pub exit_code: Option<i32>,
    pub stdout: String,
    pub stderr: String,
    pub elapsed: std::time::Duration,
}

impl HookResult {
    pub fn success(&self) -> bool {
        self.exit_code == Some(0)
    }
}

/// A resolved hook script path.
#[derive(Debug, Clone)]
pub(crate) struct ResolvedHook {
    path: PathBuf,
}

/// Find the hooks directory for a repository.
///
/// Priority:
/// 1. `core.hooksPath` from `.suture/config` (if set)
/// 2. `.suture/hooks/` (default)
pub fn hooks_dir(repo_root: &Path) -> PathBuf {
    // Try to read from repo config
    let config_path = repo_root.join(".suture").join("config");
    let Some(content) = std::fs::read_to_string(&config_path).ok() else {
        return repo_root.join(".suture").join("hooks");
    };
    let Ok(config) = toml::from_str::<HashMap<String, toml::Value>>(&content) else {
        return repo_root.join(".suture").join("hooks");
    };
    let Some(toml::Value::String(path)) = config.get("core").and_then(|c| c.get("hooksPath"))
    else {
        return repo_root.join(".suture").join("hooks");
    };

    let path = PathBuf::from(&path);
    if path.is_absolute() {
        path
    } else {
        repo_root.join(&path)
    }
}

/// Find and resolve a hook script by name.
///
/// Returns `None` if the hook doesn't exist or isn't executable.
pub(crate) fn find_hook(repo_root: &Path, hook_name: &str) -> Option<ResolvedHook> {
    let dir = hooks_dir(repo_root);
    let path = dir.join(hook_name);

    // Must exist and be executable
    if !path.exists() {
        return None;
    }

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        if let Ok(meta) = path.metadata()
            && meta.is_file()
            && (meta.permissions().mode() & 0o111) != 0
        {
            return Some(ResolvedHook { path });
        }
    }

    #[cfg(not(unix))]
    {
        // On non-Unix, just check it exists and has content
        if path.is_file()
            && std::fs::metadata(&path)
                .map(|m| m.len() > 0)
                .unwrap_or(false)
        {
            return Some(ResolvedHook { path });
        }
    }

    None
}

/// Run a hook script and capture its output.
///
/// Returns `HookResult` with the exit code and captured stdout/stderr.
pub fn run_hook(
    repo_root: &Path,
    hook_name: &str,
    env: &HashMap<String, String>,
) -> Result<HookResult, HookError> {
    let hook = find_hook(repo_root, hook_name).ok_or(HookError::NotFound(hook_name.to_string()))?;

    let start = std::time::Instant::now();

    let output = std::process::Command::new(&hook.path)
        .envs(env)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .map_err(|e| HookError::ExecFailed {
            hook: hook_name.to_string(),
            path: hook.path.display().to_string(),
            error: e.to_string(),
        })?;

    let elapsed = start.elapsed();
    let exit_code = output.status.code();

    Ok(HookResult {
        hook_name: hook_name.to_string(),
        exit_code,
        stdout: String::from_utf8_lossy(&output.stdout).to_string(),
        stderr: String::from_utf8_lossy(&output.stderr).to_string(),
        elapsed,
    })
}

/// Run all hooks of a given type from a directory (for extensibility).
///
/// If multiple scripts match (e.g., `pre-commit.d/` directory), all are run
/// in sorted order, and any failure aborts the chain.
pub fn run_hooks(
    repo_root: &Path,
    hook_name: &str,
    env: &HashMap<String, String>,
) -> Result<Vec<HookResult>, HookError> {
    let dir = hooks_dir(repo_root);
    let direct_hook = dir.join(hook_name);

    let mut results = Vec::new();

    if direct_hook.exists() {
        // Single hook file
        match run_hook(repo_root, hook_name, env) {
            Ok(result) => {
                results.push(result);
            }
            Err(HookError::NotFound(_)) => {
                // Not found, try directory
            }
            Err(e) => {
                return Err(e);
            }
        }
    }

    // Check for hook directory (e.g., pre-commit.d/)
    let hook_sub_dir = dir.join(format!("{}.d", hook_name));
    if hook_sub_dir.is_dir() {
        let mut entries: Vec<_> = std::fs::read_dir(&hook_sub_dir)
            .map_err(|e| HookError::ExecFailed {
                hook: hook_name.to_string(),
                path: hook_sub_dir.display().to_string(),
                error: e.to_string(),
            })?
            .filter_map(|entry| entry.ok())
            .filter_map(|entry| {
                let path = entry.path();
                if path.is_file() { Some(path) } else { None }
            })
            .collect::<Vec<_>>();
        entries.sort();

        for path in entries {
            let file_name = path
                .file_name()
                .map(|n| n.to_string_lossy().to_string())
                .unwrap_or_default();
            let sub_hook_name = format!("{}/{}", hook_name, file_name);

            // Check executable bit (same logic as find_hook)
            #[cfg(unix)]
            {
                use std::os::unix::fs::PermissionsExt;
                let Ok(meta) = path.metadata() else {
                    continue;
                };
                if meta.is_file() && (meta.permissions().mode() & 0o111) == 0 {
                    continue; // Skip non-executable files
                }
            }

            let start = std::time::Instant::now();
            let output = std::process::Command::new(&path)
                .envs(env)
                .env("SUTURE_HOOK", &sub_hook_name)
                .stdout(Stdio::piped())
                .stderr(Stdio::piped())
                .output()
                .map_err(|e| HookError::ExecFailed {
                    hook: sub_hook_name.clone(),
                    path: path.display().to_string(),
                    error: e.to_string(),
                })?;

            let elapsed = start.elapsed();
            let result = HookResult {
                hook_name: sub_hook_name,
                exit_code: output.status.code(),
                stdout: String::from_utf8_lossy(&output.stdout).to_string(),
                stderr: String::from_utf8_lossy(&output.stderr).to_string(),
                elapsed,
            };
            if !result.success() {
                return Err(HookError::ExecFailed {
                    hook: result.hook_name,
                    path: path.display().to_string(),
                    error: format!("hook exited with code {:?}", result.exit_code),
                });
            }
            results.push(result);
        }
    }

    Ok(results)
}

/// Build the standard environment variables for a hook invocation.
///
/// The caller should provide `author`, `branch`, and `head_hash` from the
/// repository when available (e.g. via `repo.head()` and `repo.get_config()`).
pub fn build_env(
    repo_root: &Path,
    hook_name: &str,
    author: Option<&str>,
    branch: Option<&str>,
    head_hash: Option<&str>,
    extra: HashMap<String, String>,
) -> HashMap<String, String> {
    let mut env = HashMap::new();

    // Standard suture vars
    env.insert("SUTURE_HOOK".to_string(), hook_name.to_string());
    env.insert(
        "SUTURE_REPO".to_string(),
        repo_root.to_string_lossy().to_string(),
    );
    env.insert(
        "SUTURE_HOOK_DIR".to_string(),
        hooks_dir(repo_root).to_string_lossy().to_string(),
    );
    env.insert("SUTURE_OPERATION".to_string(), hook_name.to_string());

    // Author
    if let Some(a) = author {
        env.insert("SUTURE_AUTHOR".to_string(), a.to_string());
    }

    // Branch
    if let Some(b) = branch {
        env.insert("SUTURE_BRANCH".to_string(), b.to_string());
    }

    // HEAD hash
    if let Some(h) = head_hash {
        env.insert("SUTURE_HEAD".to_string(), h.to_string());
    }

    // Add any extra env vars
    for (k, v) in extra {
        env.insert(k, v);
    }

    env
}

/// Format hook results for display to the user.
pub fn format_hook_result(result: &HookResult) -> String {
    let status = if result.success() { "passed" } else { "FAILED" };
    format!(
        "{}: {} ({})",
        result.hook_name,
        status,
        result.exit_code.unwrap_or(-1)
    )
}

/// Errors that can occur during hook execution.
#[derive(Debug, thiserror::Error)]
pub enum HookError {
    #[error("hook not found: {0}")]
    NotFound(String),
    #[error("hook '{hook}' exec failed: {path}: {error}")]
    ExecFailed {
        hook: String,
        path: String,
        error: String,
    },
    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),
}

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

    fn make_hook(dir: &Path, name: &str, content: &str) -> PathBuf {
        let path = dir.join(name);
        fs::write(&path, content).unwrap();
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            fs::set_permissions(&path, fs::Permissions::from_mode(0o755)).unwrap();
        }
        path
    }

    #[test]
    fn test_find_hook_exists_and_executable() {
        let tmp = tempfile::tempdir().unwrap();
        let hook_dir = tmp.path().join(".suture").join("hooks");
        fs::create_dir_all(&hook_dir).unwrap();
        make_hook(&hook_dir, "pre-commit", "#!/bin/sh\nexit 0");

        let hook = find_hook(tmp.path(), "pre-commit");
        assert!(hook.is_some());
        assert_eq!(hook.unwrap().path, hook_dir.join("pre-commit"));
    }

    #[test]
    fn test_find_hook_not_exists() {
        let tmp = tempfile::tempdir().unwrap();
        let hook = find_hook(tmp.path(), "pre-commit");
        assert!(hook.is_none());
    }

    #[test]
    fn test_find_hook_not_executable() {
        let tmp = tempfile::tempdir().unwrap();
        let hook_dir = tmp.path().join(".suture").join("hooks");
        fs::create_dir_all(&hook_dir).unwrap();
        let path = hook_dir.join("pre-commit");
        fs::write(&path, "#!/bin/sh\nexit 0").unwrap();
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            fs::set_permissions(&path, fs::Permissions::from_mode(0o644)).unwrap();
        }

        let hook = find_hook(tmp.path(), "pre-commit");
        #[cfg(unix)]
        {
            assert!(hook.is_none());
        }
        #[cfg(not(unix))]
        {
            assert!(hook.is_some());
        }
    }

    #[test]
    fn test_run_hook_success() {
        let tmp = tempfile::tempdir().unwrap();
        let hook_dir = tmp.path().join(".suture").join("hooks");
        fs::create_dir_all(&hook_dir).unwrap();
        make_hook(
            &hook_dir,
            "pre-commit",
            "#!/bin/sh\necho 'hook ran'\nexit 0",
        );

        let env = build_env(tmp.path(), "pre-commit", None, None, None, HashMap::new());
        let result = run_hook(tmp.path(), "pre-commit", &env).unwrap();
        assert!(result.success());
        assert_eq!(result.stdout.trim(), "hook ran");
    }

    #[test]
    fn test_run_hook_failure() {
        let tmp = tempfile::tempdir().unwrap();
        let hook_dir = tmp.path().join(".suture").join("hooks");
        fs::create_dir_all(&hook_dir).unwrap();
        make_hook(
            &hook_dir,
            "pre-commit",
            "#!/bin/sh\necho 'failing' >&2\nexit 1",
        );

        let env = build_env(tmp.path(), "pre-commit", None, None, None, HashMap::new());
        let result = run_hook(tmp.path(), "pre-commit", &env).unwrap();
        assert!(!result.success());
        assert_eq!(result.exit_code, Some(1));
        assert!(result.stderr.contains("failing"));
    }

    #[test]
    fn test_run_hook_not_found() {
        let tmp = tempfile::tempdir().unwrap();
        let env = build_env(tmp.path(), "pre-commit", None, None, None, HashMap::new());
        let err = run_hook(tmp.path(), "pre-commit", &env);
        assert!(matches!(err, Err(HookError::NotFound(_))));
    }

    #[test]
    fn test_build_env_basic() {
        let tmp = tempfile::tempdir().unwrap();
        let env = build_env(tmp.path(), "pre-commit", None, None, None, HashMap::new());

        assert_eq!(env.get("SUTURE_HOOK").unwrap(), "pre-commit");
        assert!(
            env.get("SUTURE_REPO")
                .unwrap()
                .contains(tmp.path().to_str().unwrap())
        );
        assert_eq!(env.get("SUTURE_OPERATION").unwrap(), "pre-commit");
        // No author/branch/head when not provided
        assert!(!env.contains_key("SUTURE_AUTHOR"));
        assert!(!env.contains_key("SUTURE_BRANCH"));
        assert!(!env.contains_key("SUTURE_HEAD"));
    }

    #[test]
    fn test_build_env_with_author_branch() {
        let tmp = tempfile::tempdir().unwrap();
        let env = build_env(
            tmp.path(),
            "pre-commit",
            Some("Alice"),
            Some("main"),
            Some("abc123"),
            HashMap::new(),
        );

        assert_eq!(env.get("SUTURE_AUTHOR").unwrap(), "Alice");
        assert_eq!(env.get("SUTURE_BRANCH").unwrap(), "main");
        assert_eq!(env.get("SUTURE_HEAD").unwrap(), "abc123");
    }

    #[test]
    fn test_build_env_with_extras() {
        let tmp = tempfile::tempdir().unwrap();
        let mut extras = HashMap::new();
        extras.insert("CUSTOM_VAR".to_string(), "value".to_string());
        let env = build_env(tmp.path(), "pre-push", None, None, None, extras);

        assert_eq!(env.get("CUSTOM_VAR").unwrap(), "value");
        assert_eq!(env.get("SUTURE_HOOK").unwrap(), "pre-push");
    }

    #[test]
    fn test_format_hook_result() {
        let result = HookResult {
            hook_name: "pre-commit".to_string(),
            exit_code: Some(0),
            stdout: "all good".to_string(),
            stderr: String::new(),
            elapsed: std::time::Duration::from_millis(5),
        };
        let formatted = format_hook_result(&result);
        assert!(formatted.contains("passed"));
    }

    #[test]
    fn test_format_hook_result_failure() {
        let result = HookResult {
            hook_name: "pre-commit".to_string(),
            exit_code: Some(1),
            stdout: String::new(),
            stderr: "error!".to_string(),
            elapsed: std::time::Duration::from_millis(3),
        };
        let formatted = format_hook_result(&result);
        assert!(formatted.contains("FAILED"));
    }

    #[test]
    fn test_hooks_dir_default() {
        let tmp = tempfile::tempdir().unwrap();
        let dir = hooks_dir(tmp.path());
        assert!(dir.to_string_lossy().contains(".suture"));
        assert!(dir.to_string_lossy().contains("hooks"));
    }

    #[test]
    fn test_hooks_dir_from_config() {
        let tmp = tempfile::tempdir().unwrap();
        let suture_dir = tmp.path().join(".suture");
        fs::create_dir_all(&suture_dir).unwrap();

        let config = r#"
[core]
hooksPath = "my-hooks"
"#;
        fs::write(suture_dir.join("config"), config).unwrap();

        let dir = hooks_dir(tmp.path());
        assert!(dir.to_string_lossy().contains("my-hooks"));
    }

    #[test]
    fn test_hooks_dir_from_config_absolute() {
        let tmp = tempfile::tempdir().unwrap();
        let suture_dir = tmp.path().join(".suture");
        fs::create_dir_all(&suture_dir).unwrap();

        let config = r#"
[core]
hooksPath = "/tmp/custom-hooks"
"#;
        fs::write(suture_dir.join("config"), config).unwrap();

        let dir = hooks_dir(tmp.path());
        assert!(dir.to_string_lossy().contains("/tmp/custom-hooks"));
    }

    #[test]
    fn test_run_hooks_directory() {
        let tmp = tempfile::tempdir().unwrap();
        let hook_dir = tmp.path().join(".suture").join("hooks");
        fs::create_dir_all(&hook_dir).unwrap();
        let hook_subdir = hook_dir.join("pre-commit.d");
        fs::create_dir_all(&hook_subdir).unwrap();

        make_hook(&hook_subdir, "01-check", "#!/bin/sh\nexit 0");
        make_hook(&hook_subdir, "02-lint", "#!/bin/sh\nexit 0");
        make_hook(&hook_subdir, "03-test", "#!/bin/sh\nexit 0");

        let env = build_env(tmp.path(), "pre-commit", None, None, None, HashMap::new());
        let results = run_hooks(tmp.path(), "pre-commit", &env).unwrap();
        assert_eq!(results.len(), 3);
        assert!(results.iter().all(|r| r.success()));
    }

    #[test]
    fn test_run_hooks_directory_failure_stops() {
        let tmp = tempfile::tempdir().unwrap();
        let hook_dir = tmp.path().join(".suture").join("hooks");
        fs::create_dir_all(&hook_dir).unwrap();
        let hook_subdir = hook_dir.join("pre-commit.d");
        fs::create_dir_all(&hook_subdir).unwrap();

        make_hook(&hook_subdir, "01-pass", "#!/bin/sh\nexit 0");
        make_hook(&hook_subdir, "02-fail", "#!/bin/sh\nexit 1");

        let env = build_env(tmp.path(), "pre-commit", None, None, None, HashMap::new());
        let err = run_hooks(tmp.path(), "pre-commit", &env);
        assert!(err.is_err());
    }
}