@@ -24,7 +24,7 @@ usage() {
24
24
echo " -t <revision>: Report changes in code up to and including this revision."
25
25
echo " Default is the current working tree instead of a revision."
26
26
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."
28
28
exit 1
29
29
}
30
30
@@ -53,34 +53,67 @@ if [ "$#" -ge 1 ]; then
53
53
TARGET_DIR=" $1 "
54
54
fi
55
55
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
61
58
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} "
67
66
68
- # Step 1: Create a temporary directory for worktrees
69
- TMP_DIR=$( mktemp -d)
67
+ # Single trap for cleanup
70
68
trap ' cleanup' EXIT
71
69
70
+ # shellcheck disable=SC2317
72
71
cleanup () {
73
72
# Remove all created worktrees
74
73
for worktree in " ${WORKTREES[@]} " ; do
75
74
git worktree remove --force " $worktree "
76
75
done
77
-
78
- # Remove temporary directory
76
+ # Remove temporary directory with all contents
79
77
rm -rf " ${TMP_DIR} "
80
78
}
81
79
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
+
82
115
# Step 2: Export API snapshot for the reference revision
83
- REF_WORKTREE=" ${TMP_DIR } /ref"
116
+ REF_WORKTREE=" ${TEMP_WORKTREES } /ref"
84
117
echo " Creating Git worktree for reference revision: ${REFERENCE_REVISION} "
85
118
git worktree add " ${REF_WORKTREE} " " ${REFERENCE_REVISION} " --quiet
86
119
WORKTREES+=(" ${REF_WORKTREE} " )
@@ -90,7 +123,7 @@ pushd "${REF_WORKTREE}" > /dev/null
90
123
popd > /dev/null
91
124
92
125
# Step 3: Export API snapshot for the target revision
93
- TGT_WORKTREE=" ${TMP_DIR } /target"
126
+ TGT_WORKTREE=" ${TEMP_WORKTREES } /target"
94
127
if [ -n " ${TARGET_REVISION} " ]; then
95
128
echo " Creating Git worktree for target revision: ${TARGET_REVISION} "
96
129
git worktree add " ${TGT_WORKTREE} " " ${TARGET_REVISION} " --quiet
0 commit comments