diff --git a/config/entrypoint.sh b/config/entrypoint.sh index 782e463..cbbfd82 100644 --- a/config/entrypoint.sh +++ b/config/entrypoint.sh @@ -51,6 +51,8 @@ create_worktree() { # Create destination directory if it doesn't exist mkdir -p "$(dirname "$WORKTREE_PATH")" + WORKTREE_BASE_SHA=$(git -C /workspace rev-parse HEAD 2>/dev/null || echo "") + # Add worktree (idempotent: if branch already exists, reuses it) if git -C /workspace worktree add "$WORKTREE_PATH" -b "$WORKTREE_BRANCH" 2>/dev/null; then echo "[entrypoint] Worktree created on new branch: ${WORKTREE_BRANCH}" @@ -124,8 +126,10 @@ run_agent() { # Collect post-run metrics local commit_count last_commit finished_at end_epoch duration_secs - commit_count=$(git -C "$WORKTREE_PATH" rev-list --count HEAD 2>/dev/null || echo 0) - last_commit=$(git -C "$WORKTREE_PATH" log --oneline -1 2>/dev/null || echo "none") + commit_count=$(git -C "$WORKTREE_PATH" -c "safe.directory=$WORKTREE_PATH" \ + rev-list --count "${WORKTREE_BASE_SHA:-HEAD}..HEAD" 2>/dev/null || echo 0) + last_commit=$(git -C "$WORKTREE_PATH" -c "safe.directory=$WORKTREE_PATH" \ + log --oneline -1 2>/dev/null || echo "none") finished_at=$(date -u +"%Y-%m-%dT%H:%M:%SZ") end_epoch=$(date +%s) duration_secs=$((end_epoch - start_epoch)) diff --git a/config/spec/entrypoint_spec.sh b/config/spec/entrypoint_spec.sh index 879f2b8..0770c0e 100644 --- a/config/spec/entrypoint_spec.sh +++ b/config/spec/entrypoint_spec.sh @@ -61,6 +61,9 @@ git() { [[ "$GIT_WORKTREE_NEW_BRANCH_SUCCEEDS" == "true" ]] && return 0 || return 1 elif [[ "$*" == *"worktree add"* ]]; then [[ "$GIT_WORKTREE_EXISTING_BRANCH_SUCCEEDS" == "true" ]] && return 0 || return 1 + elif [[ "$*" == *"rev-parse HEAD"* ]]; then + echo "basef00d" + return 0 elif [[ "$*" == *"rev-list --count"* ]]; then echo "3" return 0 @@ -486,7 +489,19 @@ WRAPPER_EOF It "collects commit count after agent finishes" When run run_entrypoint --worktree agent-br --task "do work" - The output should include "[MOCK] git -C /worktrees/agent-br rev-list --count HEAD" + The output should include "rev-list --count" + The status should equal 0 + End + + It "uses safe.directory flag when collecting git metrics" + When run run_entrypoint --worktree agent-br --task "do work" + The output should include "safe.directory=/worktrees/agent-br" + The status should equal 0 + End + + It "counts only agent commits using BASE..HEAD" + When run run_entrypoint --worktree agent-br --task "do work" + The output should include "basef00d..HEAD" The status should equal 0 End End