Skip to content

fix: update apidiff script to install tool and set up remote repository #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
_output
67 changes: 50 additions & 17 deletions hack/apidiff.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ usage() {
echo " -t <revision>: Report changes in code up to and including this revision."
echo " Default is the current working tree instead of a revision."
echo " -r <revision>: Report changes in code added since this revision."
echo " Default is the common base of origin/master and HEAD."
echo " Default is the common base of master and HEAD."
exit 1
}

Expand Down Expand Up @@ -53,34 +53,67 @@ if [ "$#" -ge 1 ]; then
TARGET_DIR="$1"
fi

# Check for apidiff tool, install it if not found
if ! command -v "${API_DIFF_TOOL}" &> /dev/null; then
echo "Installing apidiff into ${GOBIN}."
go install golang.org/x/exp/cmd/apidiff@latest
fi
# Debug print to see all traps
trap -p

# Fetch common base if -r is not set
if [ -z "${REFERENCE_REVISION}" ]; then
echo "Determining common base with origin/master..."
REFERENCE_REVISION=$(git merge-base origin/master HEAD)
fi
# Step 1: Create a temporary directory structure under _output
mkdir -p "_output"
TMP_DIR=$(mktemp -d "_output/apidiff.XXXXXX")
TMP_DIR=$(cd "${TMP_DIR}" && pwd) # Convert to absolute path
TEMP_GOBIN="${TMP_DIR}/gobin"
TEMP_WORKTREES="${TMP_DIR}/worktrees"
mkdir -p "${TEMP_GOBIN}" "${TEMP_WORKTREES}"

# Step 1: Create a temporary directory for worktrees
TMP_DIR=$(mktemp -d)
# Single trap for cleanup
trap 'cleanup' EXIT

# shellcheck disable=SC2317
cleanup() {
# Remove all created worktrees
for worktree in "${WORKTREES[@]}"; do
git worktree remove --force "$worktree"
done

# Remove temporary directory
# Remove temporary directory with all contents
rm -rf "${TMP_DIR}"
}

# Update GOBIN to use temporary location
if ! command -v "${API_DIFF_TOOL}" &> /dev/null; then
echo "Installing apidiff into ${TEMP_GOBIN}"
GOBIN="${TEMP_GOBIN}" go install golang.org/x/exp/cmd/apidiff@latest
# Add GOBIN to PATH
export PATH=$PATH:${TEMP_GOBIN}
fi

# Set target revision: PULL_PULL_SHA > target > HEAD
if [ -z "${TARGET_REVISION}" ] && [ -n "${PULL_PULL_SHA:-}" ]; then
TARGET_REVISION="${PULL_PULL_SHA}"
elif [ -z "${TARGET_REVISION}" ]; then
TARGET_REVISION="HEAD"
fi

# Verify target commit exists
TARGET_REVISION="$(git rev-parse --verify "${TARGET_REVISION}")"

# Try to determine base revision if not explicitly set
if [ -z "${REFERENCE_REVISION}" ]; then
if [ -n "${PULL_BASE_SHA:-}" ]; then
# Use PULL_BASE_SHA directly as the base
REFERENCE_REVISION="${PULL_BASE_SHA}"
else
# Fall back to merge-base with origin/master
if ! REFERENCE_REVISION="$(git merge-base origin/master "${TARGET_REVISION}")"; then
echo "Error: Could not determine base revision. Please configure git remote 'origin' or use -r explicitly." >&2
exit 1
fi
fi
fi

# Verify base commit exists
REFERENCE_REVISION="$(git rev-parse --verify "${REFERENCE_REVISION}")"

# Step 2: Export API snapshot for the reference revision
REF_WORKTREE="${TMP_DIR}/ref"
REF_WORKTREE="${TEMP_WORKTREES}/ref"
echo "Creating Git worktree for reference revision: ${REFERENCE_REVISION}"
git worktree add "${REF_WORKTREE}" "${REFERENCE_REVISION}" --quiet
WORKTREES+=("${REF_WORKTREE}")
Expand All @@ -90,7 +123,7 @@ pushd "${REF_WORKTREE}" > /dev/null
popd > /dev/null

# Step 3: Export API snapshot for the target revision
TGT_WORKTREE="${TMP_DIR}/target"
TGT_WORKTREE="${TEMP_WORKTREES}/target"
if [ -n "${TARGET_REVISION}" ]; then
echo "Creating Git worktree for target revision: ${TARGET_REVISION}"
git worktree add "${TGT_WORKTREE}" "${TARGET_REVISION}" --quiet
Expand Down