|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || true)" |
| 5 | +if [[ -z "$branch" ]]; then |
| 6 | + exit 0 |
| 7 | +fi |
| 8 | + |
| 9 | +if [[ "${ALLOW_COMMIT_ON_PROTECTED_BRANCH:-0}" == "1" ]]; then |
| 10 | + exit 0 |
| 11 | +fi |
| 12 | + |
| 13 | +is_vscode_git_context=0 |
| 14 | +if [[ -n "${VSCODE_GIT_IPC_HANDLE:-}" || -n "${VSCODE_GIT_ASKPASS_NODE:-}" || -n "${VSCODE_IPC_HOOK_CLI:-}" ]]; then |
| 15 | + is_vscode_git_context=1 |
| 16 | +fi |
| 17 | + |
| 18 | +allow_vscode_protected_branch_raw="${MUSAFETY_ALLOW_VSCODE_PROTECTED_BRANCH:-$(git config --get multiagent.protectedBranches.allowVSCode || true)}" |
| 19 | +allow_vscode_protected_branch_raw="$(printf '%s' "${allow_vscode_protected_branch_raw:-}" | tr '[:upper:]' '[:lower:]')" |
| 20 | +allow_vscode_protected_branch=0 |
| 21 | +case "$allow_vscode_protected_branch_raw" in |
| 22 | + 1|true|yes|on) allow_vscode_protected_branch=1 ;; |
| 23 | + *) allow_vscode_protected_branch=0 ;; |
| 24 | +esac |
| 25 | + |
| 26 | +protected_branches_raw="${MUSAFETY_PROTECTED_BRANCHES:-$(git config --get multiagent.protectedBranches || true)}" |
| 27 | +if [[ -z "$protected_branches_raw" ]]; then |
| 28 | + protected_branches_raw="dev main master" |
| 29 | +fi |
| 30 | +protected_branches_raw="${protected_branches_raw//,/ }" |
| 31 | + |
| 32 | +is_protected_branch=0 |
| 33 | +for protected_branch in $protected_branches_raw; do |
| 34 | + if [[ "$branch" == "$protected_branch" ]]; then |
| 35 | + is_protected_branch=1 |
| 36 | + break |
| 37 | + fi |
| 38 | +done |
| 39 | + |
| 40 | +if [[ "$is_protected_branch" == "1" ]]; then |
| 41 | + if [[ "$is_vscode_git_context" == "1" && "$allow_vscode_protected_branch" == "1" ]]; then |
| 42 | + exit 0 |
| 43 | + fi |
| 44 | + |
| 45 | + git_dir="$(git rev-parse --git-dir)" |
| 46 | + if [[ -f "$git_dir/MERGE_HEAD" ]]; then |
| 47 | + exit 0 |
| 48 | + fi |
| 49 | + |
| 50 | + cat >&2 <<'MSG' |
| 51 | +[agent-branch-guard] Direct commits on protected branches are blocked. |
| 52 | +Use an agent branch first: |
| 53 | + bash scripts/agent-branch-start.sh "<task-or-plan>" "<agent-name>" |
| 54 | +After finishing work: |
| 55 | + bash scripts/agent-branch-finish.sh |
| 56 | +
|
| 57 | +Optional repo override to allow VS Code Source Control commits: |
| 58 | + git config multiagent.protectedBranches.allowVSCode true |
| 59 | +
|
| 60 | +Temporary bypass (not recommended): |
| 61 | + ALLOW_COMMIT_ON_PROTECTED_BRANCH=1 git commit ... |
| 62 | +MSG |
| 63 | + exit 1 |
| 64 | +fi |
| 65 | + |
| 66 | +if [[ "$branch" == agent/* ]]; then |
| 67 | + if ! python3 scripts/agent-file-locks.py validate --branch "$branch" --staged; then |
| 68 | + cat >&2 <<'MSG' |
| 69 | +[agent-branch-guard] Agent branch commits require file ownership locks. |
| 70 | +Claim files first: |
| 71 | + python3 scripts/agent-file-locks.py claim --branch "$(git rev-parse --abbrev-ref HEAD)" <file...> |
| 72 | +MSG |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | + |
| 76 | + require_sync_before_commit_raw="$(git config --get multiagent.sync.requireBeforeCommit || true)" |
| 77 | + if [[ -z "$require_sync_before_commit_raw" ]]; then |
| 78 | + require_sync_before_commit_raw="false" |
| 79 | + fi |
| 80 | + require_sync_before_commit="$(printf '%s' "$require_sync_before_commit_raw" | tr '[:upper:]' '[:lower:]')" |
| 81 | + |
| 82 | + should_require_sync=0 |
| 83 | + case "$require_sync_before_commit" in |
| 84 | + 1|true|yes|on) should_require_sync=1 ;; |
| 85 | + 0|false|no|off) should_require_sync=0 ;; |
| 86 | + *) should_require_sync=0 ;; |
| 87 | + esac |
| 88 | + |
| 89 | + if [[ "$should_require_sync" == "1" ]]; then |
| 90 | + base_branch="$(git config --get multiagent.baseBranch || true)" |
| 91 | + if [[ -z "$base_branch" ]]; then |
| 92 | + base_branch="dev" |
| 93 | + fi |
| 94 | + |
| 95 | + max_behind_raw="$(git config --get multiagent.sync.maxBehindCommits || true)" |
| 96 | + if [[ -z "$max_behind_raw" ]]; then |
| 97 | + max_behind_raw="0" |
| 98 | + fi |
| 99 | + if [[ ! "$max_behind_raw" =~ ^[0-9]+$ ]]; then |
| 100 | + echo "[agent-sync-guard] Invalid multiagent.sync.maxBehindCommits value: ${max_behind_raw}" >&2 |
| 101 | + echo "[agent-sync-guard] Expected non-negative integer. Example: git config multiagent.sync.maxBehindCommits 0" >&2 |
| 102 | + exit 1 |
| 103 | + fi |
| 104 | + |
| 105 | + if ! git fetch origin "$base_branch" --quiet >/dev/null 2>&1; then |
| 106 | + echo "[agent-sync-guard] Unable to fetch origin/${base_branch} while commit sync gate is enabled." >&2 |
| 107 | + echo "[agent-sync-guard] Disable gate temporarily with: git config multiagent.sync.requireBeforeCommit false" >&2 |
| 108 | + exit 1 |
| 109 | + fi |
| 110 | + |
| 111 | + if ! git show-ref --verify --quiet "refs/remotes/origin/${base_branch}"; then |
| 112 | + echo "[agent-sync-guard] Remote base branch not found: origin/${base_branch}" >&2 |
| 113 | + exit 1 |
| 114 | + fi |
| 115 | + |
| 116 | + behind_count="$(git rev-list --left-right --count "${branch}...origin/${base_branch}" 2>/dev/null | awk '{print $2}')" |
| 117 | + behind_count="${behind_count:-0}" |
| 118 | + max_behind="${max_behind_raw}" |
| 119 | + |
| 120 | + if [[ "$behind_count" -gt "$max_behind" ]]; then |
| 121 | + cat >&2 <<MSG |
| 122 | +[agent-sync-guard] Commit blocked: '${branch}' is behind origin/${base_branch} by ${behind_count} commit(s) (max allowed: ${max_behind}). |
| 123 | +Run: |
| 124 | + musafety sync --base ${base_branch} |
| 125 | +Or relax threshold: |
| 126 | + git config multiagent.sync.maxBehindCommits <n> |
| 127 | +MSG |
| 128 | + exit 1 |
| 129 | + fi |
| 130 | + fi |
| 131 | +fi |
| 132 | + |
| 133 | +if command -v pre-commit >/dev/null 2>&1 && [[ -f .pre-commit-config.yaml ]]; then |
| 134 | + pre-commit run --hook-stage pre-commit |
| 135 | +fi |
0 commit comments