destructive_command_guard 0.4.3

A Claude Code hook that blocks destructive commands before they execute
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
//! Git branch detection for branch-aware strictness.
//!
//! This module provides reliable detection of the current git branch with fallback
//! mechanisms and per-directory caching for performance.
//!
//! # Design
//!
//! - **Primary method**: `git branch --show-current` (most reliable)
//! - **Fallback method**: Read `.git/HEAD` file directly (for environments without git CLI)
//! - **Detached HEAD**: Returns `None` for branch, or commit hash with special marker
//! - **Caching**: Per working directory cache to avoid repeated subprocess/file reads
//!
//! # Usage
//!
//! ```ignore
//! use destructive_command_guard::git::get_current_branch;
//!
//! if let Some(branch) = get_current_branch() {
//!     println!("Current branch: {}", branch);
//! } else {
//!     println!("Not on a branch (detached HEAD or not in git repo)");
//! }
//! ```

use std::cell::RefCell;
use std::path::PathBuf;
use std::process::Command;
use std::time::{Duration, Instant};

/// Cache duration before refreshing branch info.
/// 30 seconds is reasonable for a CLI tool that runs briefly.
const CACHE_TTL: Duration = Duration::from_secs(30);

/// Result of branch detection.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BranchInfo {
    /// On a named branch (e.g., "main", "feature/foo").
    Branch(String),
    /// In detached HEAD state with optional commit hash.
    DetachedHead(Option<String>),
    /// Not in a git repository.
    NotGitRepo,
}

impl BranchInfo {
    /// Returns the branch name if on a named branch, `None` otherwise.
    #[must_use]
    pub fn branch_name(&self) -> Option<&str> {
        match self {
            Self::Branch(name) => Some(name),
            Self::DetachedHead(_) | Self::NotGitRepo => None,
        }
    }

    /// Returns `true` if in a git repository (either on a branch or detached HEAD).
    #[must_use]
    pub fn is_in_git_repo(&self) -> bool {
        !matches!(self, Self::NotGitRepo)
    }

    /// Returns `true` if on a named branch.
    #[must_use]
    pub fn is_on_branch(&self) -> bool {
        matches!(self, Self::Branch(_))
    }

    /// Returns `true` if in detached HEAD state.
    #[must_use]
    pub fn is_detached(&self) -> bool {
        matches!(self, Self::DetachedHead(_))
    }
}

/// Cached branch information for a specific working directory.
#[derive(Debug)]
struct CachedBranch {
    /// The working directory this cache entry is for.
    working_dir: PathBuf,
    /// The cached branch info.
    info: BranchInfo,
    /// When this cache entry was created.
    cached_at: Instant,
}

impl CachedBranch {
    /// Returns `true` if this cache entry is still valid.
    fn is_valid(&self, current_dir: &PathBuf) -> bool {
        self.working_dir == *current_dir && self.cached_at.elapsed() < CACHE_TTL
    }
}

thread_local! {
    /// Per-thread cache for branch information.
    /// Keyed by working directory to handle directory changes.
    static BRANCH_CACHE: RefCell<Option<CachedBranch>> = const { RefCell::new(None) };
}

/// Get the current git branch, using cache if available.
///
/// Returns `None` if not on a named branch (detached HEAD) or not in a git repo.
/// Use [`get_branch_info`] for more detailed information.
///
/// # Caching
///
/// Results are cached per working directory for up to 30 seconds to avoid
/// repeated subprocess calls. The cache is automatically invalidated when
/// the working directory changes.
#[must_use]
pub fn get_current_branch() -> Option<String> {
    get_branch_info().branch_name().map(String::from)
}

/// Get detailed branch information, using cache if available.
///
/// Returns a [`BranchInfo`] enum indicating:
/// - `Branch(name)`: On a named branch
/// - `DetachedHead(hash)`: In detached HEAD state (with optional commit hash)
/// - `NotGitRepo`: Not in a git repository
#[must_use]
pub fn get_branch_info() -> BranchInfo {
    let current_dir = std::env::current_dir().unwrap_or_default();

    // Check cache first
    let cached = BRANCH_CACHE.with(|cache| {
        let borrow = cache.borrow();
        if let Some(ref entry) = *borrow {
            if entry.is_valid(&current_dir) {
                return Some(entry.info.clone());
            }
        }
        None
    });

    if let Some(info) = cached {
        return info;
    }

    // Cache miss - fetch fresh info
    let info = fetch_branch_info();

    // Update cache
    BRANCH_CACHE.with(|cache| {
        *cache.borrow_mut() = Some(CachedBranch {
            working_dir: current_dir,
            info: info.clone(),
            cached_at: Instant::now(),
        });
    });

    info
}

/// Get branch information for a specific path.
///
/// This bypasses the cache since it's for a specific path that may differ
/// from the current working directory.
#[must_use]
pub fn get_branch_info_at_path(path: &std::path::Path) -> BranchInfo {
    fetch_branch_info_at_path(path)
}

/// Clear the branch cache.
///
/// Useful for testing or when you know the branch has changed.
pub fn clear_cache() {
    BRANCH_CACHE.with(|cache| {
        *cache.borrow_mut() = None;
    });
}

/// Fetch branch info without caching.
fn fetch_branch_info() -> BranchInfo {
    // Try primary method: git command
    if let Some(info) = get_branch_from_git_command(None) {
        return info;
    }

    // Fallback: read .git/HEAD directly
    get_branch_from_head_file(None)
}

/// Fetch branch info for a specific path without caching.
fn fetch_branch_info_at_path(path: &std::path::Path) -> BranchInfo {
    // Try primary method: git command
    if let Some(info) = get_branch_from_git_command(Some(path)) {
        return info;
    }

    // Fallback: read .git/HEAD directly
    get_branch_from_head_file(Some(path))
}

/// Primary method: Use `git branch --show-current` to get the branch name.
///
/// This is the most reliable method as it handles all edge cases correctly,
/// including worktrees, submodules, and unusual repository layouts.
fn get_branch_from_git_command(working_dir: Option<&std::path::Path>) -> Option<BranchInfo> {
    let mut cmd = Command::new("git");
    cmd.args(["branch", "--show-current"]);

    if let Some(dir) = working_dir {
        cmd.current_dir(dir);
    }

    // Suppress stderr to avoid noise when not in a git repo
    cmd.stderr(std::process::Stdio::null());

    let output = cmd.output().ok()?;

    if !output.status.success() {
        // Git command failed - might not be in a repo
        return None;
    }

    let branch = String::from_utf8(output.stdout).ok()?.trim().to_string();

    if branch.is_empty() {
        // Empty output means detached HEAD
        // Try to get the commit hash
        let hash = get_detached_head_hash(working_dir);
        Some(BranchInfo::DetachedHead(hash))
    } else {
        Some(BranchInfo::Branch(branch))
    }
}

/// Get the commit hash when in detached HEAD state.
fn get_detached_head_hash(working_dir: Option<&std::path::Path>) -> Option<String> {
    let mut cmd = Command::new("git");
    cmd.args(["rev-parse", "--short", "HEAD"]);

    if let Some(dir) = working_dir {
        cmd.current_dir(dir);
    }

    cmd.stderr(std::process::Stdio::null());

    let output = cmd.output().ok()?;

    if output.status.success() {
        let hash = String::from_utf8(output.stdout).ok()?.trim().to_string();
        if !hash.is_empty() {
            return Some(hash);
        }
    }

    None
}

/// Fallback method: Read `.git/HEAD` file directly.
///
/// Format: `ref: refs/heads/<branch-name>` for branches
/// or a commit hash for detached HEAD.
fn get_branch_from_head_file(working_dir: Option<&std::path::Path>) -> BranchInfo {
    let git_dir = find_git_dir(working_dir);
    let head_path = match git_dir {
        Some(dir) => dir.join("HEAD"),
        None => return BranchInfo::NotGitRepo,
    };

    let head_content = match std::fs::read_to_string(&head_path) {
        Ok(content) => content,
        Err(_) => return BranchInfo::NotGitRepo,
    };

    parse_head_content(&head_content)
}

fn parse_head_content(head_content: &str) -> BranchInfo {
    let trimmed = head_content.trim();

    // Symbolic ref: "ref: refs/heads/<branch>" (normal) or other refs (detached-ish state)
    if let Some(ref_path) = trimmed.strip_prefix("ref: ") {
        if let Some(branch) = ref_path.strip_prefix("refs/heads/") {
            return BranchInfo::Branch(branch.to_string());
        }
        // Non-head symbolic refs (e.g., refs/remotes/origin/main) are valid git states,
        // but not local branches.
        return BranchInfo::DetachedHead(None);
    }

    // It's a commit hash (detached HEAD)
    // Validate it looks like a hash (40 hex chars for full, or shorter for abbreviated)
    if trimmed.len() >= 7 && trimmed.len() <= 40 && trimmed.chars().all(|c| c.is_ascii_hexdigit()) {
        // Return abbreviated hash (first 7 chars)
        let short_hash = if trimmed.len() > 7 {
            trimmed[..7].to_string()
        } else {
            trimmed.to_string()
        };
        return BranchInfo::DetachedHead(Some(short_hash));
    }

    // Couldn't parse HEAD - might be corrupted or unusual format
    BranchInfo::NotGitRepo
}

/// Find the .git directory for a repository.
///
/// Handles both regular repositories (.git as directory) and worktrees
/// (.git as file pointing to the actual git directory).
fn find_git_dir(working_dir: Option<&std::path::Path>) -> Option<PathBuf> {
    let start_dir = working_dir
        .map(PathBuf::from)
        .or_else(|| std::env::current_dir().ok())?;

    let mut current = start_dir.as_path();

    loop {
        let git_path = current.join(".git");

        if git_path.is_dir() {
            // Regular git directory
            return Some(git_path);
        }

        if git_path.is_file() {
            // Worktree: .git file contains "gitdir: <path>"
            if let Ok(content) = std::fs::read_to_string(&git_path) {
                if let Some(gitdir) = parse_gitdir_from_dot_git_file(&content) {
                    let gitdir_path = PathBuf::from(gitdir);
                    // Handle relative paths
                    let resolved = if gitdir_path.is_absolute() {
                        gitdir_path
                    } else {
                        current.join(gitdir_path)
                    };
                    if resolved.is_dir() {
                        return Some(resolved);
                    }
                }
            }
        }

        // Move up to parent directory
        current = current.parent()?;
    }
}

fn parse_gitdir_from_dot_git_file(content: &str) -> Option<&str> {
    content.lines().find_map(|line| {
        line.trim()
            .strip_prefix("gitdir:")
            .map(str::trim)
            .filter(|value| !value.is_empty())
    })
}

/// Check if the current directory is in a git repository.
#[must_use]
pub fn is_in_git_repo() -> bool {
    get_branch_info().is_in_git_repo()
}

/// Check if the specified path is in a git repository.
#[must_use]
pub fn is_in_git_repo_at_path(path: &std::path::Path) -> bool {
    get_branch_info_at_path(path).is_in_git_repo()
}

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

    fn run_git(repo_path: &Path, args: &[&str]) {
        let output = Command::new("git")
            .current_dir(repo_path)
            .args(args)
            .output()
            .expect("failed to run git command");
        assert!(
            output.status.success(),
            "git {:?} failed: {}",
            args,
            String::from_utf8_lossy(&output.stderr)
        );
    }

    fn init_git_repo(repo_path: &Path) {
        run_git(repo_path, &["init"]);
        run_git(
            repo_path,
            &["config", "user.email", "dcg-tests@example.com"],
        );
        run_git(repo_path, &["config", "user.name", "DCG Tests"]);
    }

    fn create_commit(repo_path: &Path, file_name: &str) {
        fs::write(repo_path.join(file_name), "test data").expect("write commit fixture file");
        run_git(repo_path, &["add", file_name]);
        run_git(repo_path, &["commit", "-m", "test commit"]);
    }

    #[test]
    fn test_branch_info_methods() {
        let branch = BranchInfo::Branch("main".to_string());
        assert_eq!(branch.branch_name(), Some("main"));
        assert!(branch.is_in_git_repo());
        assert!(branch.is_on_branch());
        assert!(!branch.is_detached());

        let detached = BranchInfo::DetachedHead(Some("abc1234".to_string()));
        assert_eq!(detached.branch_name(), None);
        assert!(detached.is_in_git_repo());
        assert!(!detached.is_on_branch());
        assert!(detached.is_detached());

        let not_repo = BranchInfo::NotGitRepo;
        assert_eq!(not_repo.branch_name(), None);
        assert!(!not_repo.is_in_git_repo());
        assert!(!not_repo.is_on_branch());
        assert!(!not_repo.is_detached());
    }

    #[test]
    fn test_cache_validity() {
        let current_dir = PathBuf::from("/test/path");
        let other_dir = PathBuf::from("/other/path");

        let cache = CachedBranch {
            working_dir: current_dir.clone(),
            info: BranchInfo::Branch("main".to_string()),
            cached_at: Instant::now(),
        };

        // Same directory, fresh cache
        assert!(cache.is_valid(&current_dir));

        // Different directory
        assert!(!cache.is_valid(&other_dir));
    }

    #[test]
    fn test_head_file_parsing_branch() {
        let info = parse_head_content("ref: refs/heads/feature/my-branch");
        assert_eq!(info, BranchInfo::Branch("feature/my-branch".to_string()));
    }

    #[test]
    fn test_head_file_parsing_non_head_ref_is_detached() {
        let info = parse_head_content("ref: refs/remotes/origin/main");
        assert_eq!(info, BranchInfo::DetachedHead(None));
    }

    #[test]
    fn test_head_file_parsing_detached() {
        // Test commit hash detection
        let hash = "abc1234def5678";
        assert!(hash.len() >= 7 && hash.len() <= 40);
        assert!(hash.chars().all(|c| c.is_ascii_hexdigit()));
        let info = parse_head_content(hash);
        assert_eq!(info, BranchInfo::DetachedHead(Some("abc1234".to_string())));
    }

    #[test]
    fn test_parse_gitdir_from_dot_git_file_multiline() {
        let content = "gitdir: ../.git/worktrees/demo\nworktree: /tmp/demo\n";
        assert_eq!(
            parse_gitdir_from_dot_git_file(content),
            Some("../.git/worktrees/demo")
        );
    }

    #[test]
    fn test_find_git_dir_resolves_worktree_pointer_file() {
        let temp = tempfile::tempdir().expect("tempdir");
        let repo_root = temp.path().join("repo");
        let worktree = repo_root.join("nested").join("worktree");
        let git_dir = repo_root.join(".real-git-dir");

        fs::create_dir_all(&worktree).expect("create worktree");
        fs::create_dir_all(&git_dir).expect("create git dir");

        let dot_git = repo_root.join(".git");
        fs::write(
            &dot_git,
            "gitdir: .real-git-dir\nworktree: nested/worktree\n",
        )
        .expect("write .git file");

        let resolved = find_git_dir(Some(&worktree)).expect("resolve git dir");
        assert_eq!(resolved, git_dir);
    }

    #[test]
    fn test_clear_cache() {
        // Just verify clear_cache doesn't panic
        clear_cache();

        // And that we can still get branch info after clearing
        let _ = get_branch_info();
    }

    #[test]
    fn test_get_current_branch_returns_some_in_git_repo() {
        // This test runs in the dcg repo, so should return a branch
        // unless we're in a weird CI state
        let result = get_current_branch();
        // We don't assert the specific value since it could be any branch
        // Just verify the function doesn't panic
        drop(result);
    }

    #[test]
    fn test_is_in_git_repo() {
        // This test runs in the dcg repo
        let result = is_in_git_repo();
        // Should be true since we're in a git repo
        assert!(result, "Expected to be in a git repo");
    }

    #[test]
    fn test_branch_info_at_temp_path() {
        // Test with a path that's definitely not a git repo
        let temp_dir = std::env::temp_dir();
        let result = get_branch_info_at_path(&temp_dir);
        // Temp dir might or might not be in a git repo depending on system
        // Just verify it doesn't panic
        drop(result);
    }

    #[test]
    fn test_get_branch_info_at_path_detects_named_branch() {
        let temp = tempfile::tempdir().expect("tempdir");
        init_git_repo(temp.path());
        run_git(temp.path(), &["checkout", "-b", "feature/test"]);

        let info = get_branch_info_at_path(temp.path());
        assert_eq!(info, BranchInfo::Branch("feature/test".to_string()));
    }

    #[test]
    fn test_get_branch_info_at_path_detects_detached_head() {
        let temp = tempfile::tempdir().expect("tempdir");
        init_git_repo(temp.path());
        create_commit(temp.path(), "detached.txt");
        run_git(temp.path(), &["checkout", "--detach"]);

        let info = get_branch_info_at_path(temp.path());
        assert!(
            matches!(info, BranchInfo::DetachedHead(_)),
            "Expected detached HEAD, got {info:?}"
        );
    }

    #[test]
    fn test_get_branch_info_at_path_returns_not_git_repo_for_plain_directory() {
        let temp = tempfile::tempdir().expect("tempdir");
        let info = get_branch_info_at_path(temp.path());
        assert_eq!(info, BranchInfo::NotGitRepo);
    }
}