Skip to content

Commit

Permalink
Simplify and speed up fetch_merge_commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Mar 10, 2021
1 parent 6b60438 commit 6171d55
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
3 changes: 1 addition & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ commands:
name: 'Save Merge Commit'
key: git-{{ .Branch }}-{{ .Revision }}
paths:
- .git/FETCH_HEAD
- .circleci/merge_sha
restore_merge_commit:
steps:
- restore_cache:
Expand Down Expand Up @@ -78,7 +78,6 @@ jobs:
executor:
name: amphtml-medium-executor
steps:
- checkout
- run:
name: 'Fetch Merge Commit'
command: ./.circleci/fetch_merge_commit.sh fetch
Expand Down
23 changes: 7 additions & 16 deletions .circleci/fetch_merge_commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ GREEN() { echo -e "\n\033[0;32m$1\033[0m"; }
YELLOW() { echo -e "\n\033[0;33m$1\033[0m"; }
RED() { echo -e "\n\033[0;31m$1\033[0m"; }

GET_MERGE_SHA() {
awk '{print $1}' .git/FETCH_HEAD
}

if [[ -z "$1" ]]; then
echo "Usage: fetch_merge_commit.sh [fetch | merge]"
fi
Expand Down Expand Up @@ -62,24 +58,19 @@ if [[ -z "$PR_NUMBER" ]]; then
fi

if [[ "$1" == "fetch" ]]; then
# GitHub provides refs/pull/<PR_NUMBER>/merge, an up-to-date merge branch for
# every PR branch that can be cleanly merged to master. For more details, see:
# https://discuss.circleci.com/t/show-test-results-for-prospective-merge-of-a-github-pr/1662
MERGE_BRANCH="refs/pull/$PR_NUMBER/merge"
echo $(GREEN "Fetching merge SHA from $MERGE_BRANCH...")

(set -x && git fetch origin "$MERGE_BRANCH") || err=$?
if [[ "$err" -ne "0" ]]; then
echo $(RED "Failed to fetch merge commit between $CIRCLE_BRANCH and master.")
echo $(RED "Please rebase your PR branch.")
exit $err
fi

echo $(GREEN "Fetched merge SHA $(GET_MERGE_SHA)...")
MERGE_SHA="$(git ls-remote https://github.com/ampproject/amphtml.git "$MERGE_BRANCH" | awk '{print $1}')"
echo $(GREEN "Fetched merge SHA $(MERGE_SHA)...")
echo "$MERGE_SHA" > .circleci/merge_sha
exit 0
fi

# GitHub provides refs/pull/<PR_NUMBER>/merge, an up-to-date merge branch for
# every PR branch that can be cleanly merged to master. For more details, see:
# https://discuss.circleci.com/t/show-test-results-for-prospective-merge-of-a-github-pr/1662
MERGE_SHA="$(GET_MERGE_SHA)"
MERGE_SHA="$(cat .circleci/merge_sha)"
echo $(GREEN "Fetching merge commit $MERGE_SHA...")
(set -x && git pull --ff-only origin "$MERGE_SHA") || err=$?

Expand Down

0 comments on commit 6171d55

Please sign in to comment.