Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python
6.3 KB
Raw Blame
//! Tests for destructive_git.zig (split out under the 600-line ceiling).
const std = @import("std");
const isDestructiveGit = @import("destructive_git.zig").isDestructiveGit;

test "#1269: destructive git in every common spelling is flagged" {
    const cases = [_][]const u8{
        // Spellings the substring scan already caught.
        "git reset --hard HEAD~3",
        "git clean -fd",
        "git push --force origin main",
        "git push -f",
        "git branch -D worktree-docs",
        "git checkout -- src/main.zig",
        "git checkout .",
        "git stash clear",
        "git stash drop",
        "git restore .",
        "cd sub && git reset --hard",
        "git -C /repo reset --hard",
        // The spellings it missed.
        "git clean -df",
        "git clean -xdf",
        "git clean -d -f",
        "git clean --force",
        "git restore -- .",
        "git restore :/",
        "git restore src/main.zig",
        "git restore --staged --worktree .",
        "git restore -SW src/a.zig",
        "git restore --source=HEAD~1 src/a.zig",
        "git checkout -f",
        "git checkout --force main",
        "git checkout :/",
        "git checkout .gitignore",
        "git checkout ./src/a.zig",
        "git checkout '*.zig'",
        "git checkout HEAD -- .",
        "git checkout --ours conflicted.txt",
        "git switch --discard-changes main",
        "git switch -f main",
        "git -C dir reset --hard",
        "git reset  --hard",
        "git reset\t--hard origin/main",
        "git -c core.editor=true reset --hard",
        "git --git-dir=.git --work-tree=. reset --hard",
        "git --work-tree /tmp/x clean -f",
        "git -C \"$dir\" reset --hard",
        "git -C $(pwd) reset --hard",
        "git \"reset\" --hard",
        "cd x; git clean -fdx",
        "make || git reset --hard",
        "(cd x && git stash drop stash@{1})",
        "sh -c \"git reset --hard\"",
        "bash -c 'cd x && git clean -fd'",
        "echo $(git reset --hard)",
        "echo `git clean -f`",
        "sudo git push --force",
        "GIT_DIR=x git reset --hard",
        "/usr/bin/git reset --hard",
        "git reset --hard 2>&1 | tail -3",
        "git reset --hard>/dev/null",
        "git 2>/dev/null clean -fd",
        "cat <<EOF | sh\ngit reset --hard\nEOF",
        "git push --force-with-lease",
        "git push -uf origin main",
        "git push origin +main",
        "git push origin --delete old",
        "git push origin :old",
        "git push --mirror backup",
        "git branch --delete --force x",
        "git branch -d -f x",
        "git branch -df x",
        "git reset \\\n  --hard",
        // Quotes and backslashes that spell git without the literal letters.
        "gi''t reset --hard",
        "g\"i\"t reset --hard",
        "\"g\"it clean -fdx",
        "g\\it push -f",
        "sh -c 'g\\it reset --hard'",
        // `$(...)` inside double quotes, with quotes of its own.
        "out=\"$(git -C \"$dir\" reset --hard)\"",
        "msg=\"$(git -C \"$d\" clean -fd 2>&1)\"",
        "echo \"$(git reset \"--hard\")\"",
        "echo $(echo \")\"; git reset --hard)",
        // Abbreviated long options.
        "git reset --har",
        "git clean --for",
        "git branch --del --force z",
        "git switch --discard main",
        "git push --mirro x",
        "git push --for",
        "git push origin --del old",
        "git restore --staged --wor .",
        "git checkout --for main",
        // Two positionals: `<tree-ish> <path>`.
        "git checkout main src/main.zig",
        "git checkout HEAD~1 src/a.zig",
        // `&>` / `&>>` redirect, not a separator.
        "git reset &>/dev/null --hard",
        "git clean &>/dev/null -fd",
        "git clean &>>log -fd",
        // Aliases whose value is destructive.
        "git config alias.x 'reset --hard' && git x",
        "git config --global alias.nuke '!git clean -fdx'",
        "git config set alias.x \"push --force\"",
        "git -c alias.x='reset --hard' x",
        "git -c 'alias.x=!git reset --hard' x",
        // Case-insensitive git word (Windows).
        "Git reset --hard",
        "GIT.EXE clean -fd",
    };
    for (cases) |c| std.testing.expect(isDestructiveGit(c)) catch |err| {
        std.debug.print("not flagged: {s}\n", .{c});
        return err;
    };
}

test "#1269: read-only and branch-only git is not flagged" {
    const cases = [_][]const u8{
        "git status",
        "git log --oneline -5",
        "git log -S\"reset --hard\"",
        "git diff HEAD",
        "git show HEAD:src/main.zig",
        "git commit -m wip",
        "git reset --soft HEAD~1",
        "git reset HEAD src/a.zig",
        "git checkout main",
        "git checkout -b feature",
        "git checkout -b feature origin/main",
        "git checkout --track origin/feature",
        "git checkout --detach",
        "git switch main",
        "git switch -c new-branch",
        "git restore --staged f.txt",
        "git restore --staged .",
        "git restore",
        "git clean -n",
        "git clean -nd",
        "git push",
        "git push -u origin feature",
        "git push origin main",
        "git push origin HEAD:refs/heads/x",
        "git branch -d merged-feature",
        "git branch -u origin/main",
        "git stash",
        "git stash pop",
        "git stash list",
        "git stash push -m drop",
        "git restore --staged f.txt 2>/dev/null",
        "git stash list >stashes.txt",
        "git fetch --prune",
        "git -C /repo status",
        "git -c color.ui=always diff",
        "gitk --all",
        "ls -la",
        "echo reset --hard",
        "",
        "git checkout --det",
        "git checkout --detach HEAD~1",
        "git checkout --orphan gh-pages main",
        "git log --grep 'reset --hard'",
        "git push --follow-tags",
        "git restore --staged --source=HEAD f",
        "git restore --sta f",
        "git config alias.st status",
        "git config --global alias.lg 'log --oneline'",
        "git -c alias.x=status x",
        "git status &>/dev/null && echo ok",
        "git status 2>&1 & git log",
        "echo \"$(git status)\"",
        "gitk --all &>/dev/null",
    };
    for (cases) |c| std.testing.expect(!isDestructiveGit(c)) catch |err| {
        std.debug.print("flagged: {s}\n", .{c});
        return err;
    };
}