From 893fc8c8df4b7a3bbbe79000c0c48a98483157b7 Mon Sep 17 00:00:00 2001 From: Chris Fenner Date: Tue, 24 Sep 2024 08:51:59 -0700 Subject: [PATCH] Base files on source branch in PR workflows (#190) * Detect merge commits (as in PRs) and switch to first parent if so * always do the unified checkout action * Only check out right parent on PRs * instead of checking out the right parent, just reference the commit --- .github/workflows/render.yml | 12 ------------ build.sh | 11 ++++++++++- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/.github/workflows/render.yml b/.github/workflows/render.yml index 9636632..20ce62e 100644 --- a/.github/workflows/render.yml +++ b/.github/workflows/render.yml @@ -48,19 +48,7 @@ jobs: echo OUTPUT_FILENAME="${filename}" >> "$GITHUB_OUTPUT" echo output filename: ${filename} - # For pull requests, check out the head ref of the source branch from the source repo. - name: Checkout - if: inputs.workflow == 'pr' - uses: actions/checkout@v4 - with: - ref: ${{ github.head_ref }} - repository: ${{ github.event.pull_request.head.repo.full_name }} - fetch-depth: 0 - fetch-tags: true - # For other workflows, check out the requested revision (which defaults to - # head of the default branch). - - name: Checkout - if: inputs.workflow != 'pr' uses: actions/checkout@v4 with: ref: ${{ inputs.revision }} diff --git a/build.sh b/build.sh index 51a2f3b..f04bc2e 100755 --- a/build.sh +++ b/build.sh @@ -245,6 +245,16 @@ fi # figure out git version and revision if needed. EXTRA_PANDOC_OPTIONS="" if test "${DO_GITVERSION}" == "yes"; then + if [ ! -z "${PR_NUMBER}" ] && $(git rev-parse HEAD^2 >/dev/null 2>/dev/null); then + # For PR workflows, base the version info on the right parent. + # In the context of a GitHub pull request, HEAD is a merge commit where + # parent1 (HEAD^1) is the target branch and parent2 (HEAD~2) is the source + GIT_COMMIT=$(git rev-parse --short HEAD^2) + else + # Otherwise, base the version info on HEAD. + GIT_COMMIT=$(git rev-parse --short HEAD) + fi + # TODO: Should we fail if dirty? raw_version="$(git describe --always --tags)" echo "Git version: ${raw_version}" @@ -263,7 +273,6 @@ if test "${DO_GITVERSION}" == "yes"; then # Where $REVISION is the number of commits since the last tag (e.g., 54) # $VERSION-$REVISION-g$COMMIT --> version without prerelease tag at a particular commit (len 3) # $VERSION-$PRERELEASE-$REVISION-g$COMMIT --> version with (len 4) - GIT_COMMIT=$(git rev-parse --short HEAD) len=${#dash_hunks[@]} case $len in 1)