mirror of
https://github.com/anthropics/claude-code-action.git
synced 2026-11-22 05:49:23 +08:00
* Use the default branch's config on pull requests from authors without write access When a pull request's author does not have write access and it targets a branch other than the default one, the config that the action restores now comes from the base_branch input or the default branch instead of the pull request's base. The base is still fetched so that the two can be compared. Fetch and check out the config branch by its full ref name, so that a tag of the same name or a narrow refspec can't leave the session without config. Document that pr-N holds a fork's commits and should stay out of CI that holds secrets. Co-authored-by: mhegazy <[email protected]> * Keep the other branch's config out of commits and the base comparable The commit wrapper leaves out files that the action took from a branch other than the pull request's base. HEAD's history is fetched again if fetching the config branch cut it, and a base branch that can't be fetched fails the run as before. A bot's pull request into a pr-N branch no longer takes its config from it. Co-authored-by: mhegazy <[email protected]> --------- Co-authored-by: Claude <[email protected]>
97 lines
2.5 KiB
Bash
Executable File
97 lines
2.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
# Wrapper around `git commit` that only allows a literal message. Without it, an
|
|
# allow rule for `git commit` also permits flags that make git read a file into
|
|
# the commit, such as -F .git/config, which a later push would then publish.
|
|
#
|
|
# Usage:
|
|
# git-commit.sh -m "<message>" [-m "<message>"] [-a] [--allow-empty] [-- <paths>]
|
|
|
|
source "$(dirname "${BASH_SOURCE[0]}")/git-env.sh"
|
|
|
|
has_message=false
|
|
expect_message=false
|
|
paths_only=false
|
|
has_path=false
|
|
all=false
|
|
args=()
|
|
|
|
for arg in "$@"; do
|
|
# The arguments without -a, for when the files it adds are staged separately below.
|
|
kept="$arg"
|
|
if [[ "$expect_message" == true ]]; then
|
|
expect_message=false
|
|
has_message=true
|
|
elif [[ "$paths_only" == true ]]; then
|
|
has_path=true
|
|
else
|
|
case "$arg" in
|
|
-m | --message)
|
|
expect_message=true
|
|
;;
|
|
-am)
|
|
expect_message=true
|
|
all=true
|
|
kept="-m"
|
|
;;
|
|
--message=*)
|
|
has_message=true
|
|
;;
|
|
-a | --all)
|
|
all=true
|
|
continue
|
|
;;
|
|
--allow-empty | -q | --quiet | -s | --signoff) ;;
|
|
--)
|
|
paths_only=true
|
|
;;
|
|
-*)
|
|
echo "Error: only -m with a literal message and basic commit flags are allowed (got: $arg)" >&2
|
|
exit 1
|
|
;;
|
|
*)
|
|
has_path=true
|
|
;;
|
|
esac
|
|
fi
|
|
args+=("$kept")
|
|
done
|
|
|
|
if [[ "$expect_message" == true ]]; then
|
|
echo "Error: -m needs a message" >&2
|
|
exit 1
|
|
fi
|
|
|
|
if [[ "$has_message" != true ]]; then
|
|
echo "Error: a message is required, with -m" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# The action took these files from a branch other than the pull request's base, so a
|
|
# commit must not carry that branch's version of them. It stays in the working tree.
|
|
restored=()
|
|
if [[ -e "$script_dir/.restored-paths" ]]; then
|
|
while IFS= read -r pathspec; do
|
|
restored+=("$pathspec")
|
|
done <"$script_dir/.restored-paths"
|
|
fi
|
|
if [[ ${#restored[@]} -gt 0 ]]; then
|
|
if [[ "$has_path" == true ]]; then
|
|
for pathspec in "${restored[@]}"; do
|
|
args+=(":(exclude,${pathspec#:(}")
|
|
done
|
|
elif [[ "$all" == true ]]; then
|
|
git "${git_args[@]}" add -u :/
|
|
fi
|
|
if ! git "${git_args[@]}" diff --cached --quiet -- "${restored[@]}"; then
|
|
git "${git_args[@]}" reset -q -- "${restored[@]}"
|
|
echo "Note: these files come from another branch in this run, so their changes are not committed: ${restored[*]}" >&2
|
|
fi
|
|
if [[ "$has_path" != true || "$all" != true ]]; then
|
|
set -- "${args[@]}"
|
|
fi
|
|
fi
|
|
|
|
exec git "${git_args[@]}" commit "$@"
|