@@ -24,7 +24,7 @@ usage() {
2424 echo " -t <revision>: Report changes in code up to and including this revision."
2525 echo " Default is the current working tree instead of a revision."
2626 echo " -r <revision>: Report changes in code added since this revision."
27- echo " Default is the common base of origin/ master and HEAD."
27+ echo " Default is the common base of master and HEAD."
2828 exit 1
2929}
3030
@@ -53,34 +53,67 @@ if [ "$#" -ge 1 ]; then
5353 TARGET_DIR=" $1 "
5454fi
5555
56- # Check for apidiff tool, install it if not found
57- if ! command -v " ${API_DIFF_TOOL} " & > /dev/null; then
58- echo " Installing apidiff into ${GOBIN} ."
59- go install golang.org/x/exp/cmd/apidiff@latest
60- fi
56+ # Debug print to see all traps
57+ trap -p
6158
62- # Fetch common base if -r is not set
63- if [ -z " ${REFERENCE_REVISION} " ]; then
64- echo " Determining common base with origin/master..."
65- REFERENCE_REVISION=$( git merge-base origin/master HEAD)
66- fi
59+ # Step 1: Create a temporary directory structure under _output
60+ mkdir -p " _output"
61+ TMP_DIR=$( mktemp -d " _output/apidiff.XXXXXX" )
62+ TMP_DIR=$( cd " ${TMP_DIR} " && pwd) # Convert to absolute path
63+ TEMP_GOBIN=" ${TMP_DIR} /gobin"
64+ TEMP_WORKTREES=" ${TMP_DIR} /worktrees"
65+ mkdir -p " ${TEMP_GOBIN} " " ${TEMP_WORKTREES} "
6766
68- # Step 1: Create a temporary directory for worktrees
69- TMP_DIR=$( mktemp -d)
67+ # Single trap for cleanup
7068trap ' cleanup' EXIT
7169
70+ # shellcheck disable=SC2317
7271cleanup () {
7372 # Remove all created worktrees
7473 for worktree in " ${WORKTREES[@]} " ; do
7574 git worktree remove --force " $worktree "
7675 done
77-
78- # Remove temporary directory
76+ # Remove temporary directory with all contents
7977 rm -rf " ${TMP_DIR} "
8078}
8179
80+ # Update GOBIN to use temporary location
81+ if ! command -v " ${API_DIFF_TOOL} " & > /dev/null; then
82+ echo " Installing apidiff into ${TEMP_GOBIN} "
83+ GOBIN=" ${TEMP_GOBIN} " go install golang.org/x/exp/cmd/apidiff@latest
84+ # Add GOBIN to PATH
85+ export PATH=$PATH :${TEMP_GOBIN}
86+ fi
87+
88+ # Set target revision: PULL_PULL_SHA > target > HEAD
89+ if [ -z " ${TARGET_REVISION} " ] && [ -n " ${PULL_PULL_SHA:- } " ]; then
90+ TARGET_REVISION=" ${PULL_PULL_SHA} "
91+ elif [ -z " ${TARGET_REVISION} " ]; then
92+ TARGET_REVISION=" HEAD"
93+ fi
94+
95+ # Verify target commit exists
96+ TARGET_REVISION=" $( git rev-parse --verify " ${TARGET_REVISION} " ) "
97+
98+ # Try to determine base revision if not explicitly set
99+ if [ -z " ${REFERENCE_REVISION} " ]; then
100+ if [ -n " ${PULL_BASE_SHA:- } " ]; then
101+ # Use PULL_BASE_SHA directly as the base
102+ REFERENCE_REVISION=" ${PULL_BASE_SHA} "
103+ else
104+ # Fall back to merge-base with origin/master
105+ if ! REFERENCE_REVISION=" $( git merge-base origin/master " ${TARGET_REVISION} " ) " ; then
106+ echo " Error: Could not determine base revision. Please configure git remote 'origin' or use -r explicitly." >&2
107+ exit 1
108+ fi
109+ fi
110+ fi
111+
112+ # Verify base commit exists
113+ REFERENCE_REVISION=" $( git rev-parse --verify " ${REFERENCE_REVISION} " ) "
114+
82115# Step 2: Export API snapshot for the reference revision
83- REF_WORKTREE=" ${TMP_DIR } /ref"
116+ REF_WORKTREE=" ${TEMP_WORKTREES } /ref"
84117echo " Creating Git worktree for reference revision: ${REFERENCE_REVISION} "
85118git worktree add " ${REF_WORKTREE} " " ${REFERENCE_REVISION} " --quiet
86119WORKTREES+=(" ${REF_WORKTREE} " )
@@ -90,7 +123,7 @@ pushd "${REF_WORKTREE}" > /dev/null
90123popd > /dev/null
91124
92125# Step 3: Export API snapshot for the target revision
93- TGT_WORKTREE=" ${TMP_DIR } /target"
126+ TGT_WORKTREE=" ${TEMP_WORKTREES } /target"
94127if [ -n " ${TARGET_REVISION} " ]; then
95128 echo " Creating Git worktree for target revision: ${TARGET_REVISION} "
96129 git worktree add " ${TGT_WORKTREE} " " ${TARGET_REVISION} " --quiet
0 commit comments