0

Assume:

The latest commit's commit message of the branch feature/xyz is add feature xyz with SHA number 1234. The newest commit's SHA number of the main branch is 3456.

When I push the branch feature/xyz to the repo and open a pull request from feature/xyz to main a workflow pipeline will be triggered, how can I get the commit message add feature xyz?

note: the pull request is open and not merged

name: Dev deploy

on:
    
  pull_request:
      types: [opened, reopened, edited, synchronize]

jobs:
  deploy-to-dev:
      name: deploy to dev
      runs-on: ubuntu-22.04
      steps:

        - name: checkout repo
          uses: actions/checkout@v3

        - name: Set env var
          run: |
            echo commit=$(git log --format=%B -n 1) >> $GITHUB_ENV

git log --format=%B -n 1 would give me something like Merge 1234 into 3456.

How can I get the commit message of the last commit from the branch feature/xyz, aka, add feature xyz in this case?

SLN
  • 4,772
  • 2
  • 38
  • 79
  • Before or after it has been merged to master? Does the pipeline run on a temporary merge of the would-be result? – knittl Oct 18 '22 at 21:28
  • @knittl the pull request is open, before merge to master – SLN Oct 18 '22 at 21:30
  • Yes, but from your output it looks like it is running on a temporary merge? The second head of the head would be `HEAD^2` then. – knittl Oct 18 '22 at 21:33
  • @knittl I really don't know how I made a temporary merge in github :( , I just clicked the pull request button and keep push for new commit after a peer review – SLN Oct 18 '22 at 21:38
  • In any case, `HEAD^2` should be the second parent of this merge commit. First parent very likely being the target branch, second parent being your PR branch. – knittl Oct 18 '22 at 21:39
  • Note that this is specifically described [here](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows) under the `pull_request` action: the `GITHUB_SHA` field is set to the "Last merge commit on the `GITHUB_REF` branch" (which in turn is specified as `refs/pull//merge`). So [hlovdal's answer](https://stackoverflow.com/a/74118184/1256452) is correct. (I also agree that depending on the commit message in the `HEAD^2` commit is unwise.) – torek Oct 19 '22 at 01:11

1 Answers1

2

I think it is a really bad idea to depend on the last commit on a feature branch to be special and contain some extra valuable commit message, so I would stongly recommend against doing this. But the below is an answer to what was asked.


Assuming the following: The main branch contains commits "m1".."m5". The feature/xyz branch contains commits "f1".."f2", starting from "m2", e.g.

$ git log --graph --all --oneline
* 4d5b4ee (feature/xyz) f2
* 62f9a15 f1
| * 6cf050a (HEAD -> main) m5
| * f9bd7c6 m4
| * b7860f8 m3
|/  
* 39fee03 m2
* 1c1d702 m1
* e167ed6 .gitignore
$

then running

$ git checkout main
$ git merge feature/xyz

would create a merge commit, e.g.

$ git log --graph --all --oneline
*   7a8474a (HEAD -> main) Merge branch 'feature/xyz'
|\  
| * 4d5b4ee (feature/xyz) f2
| * 62f9a15 f1
* | 6cf050a m5
...

A merge commit is a commit with two (or more) parents. You probably know that you can use ^ as a postfix to a branch/commit/ref to indicate its parent, but what when there are multiple? Just a single ^ is the same as the first parent which can be explicitly specified as ^1 and correspondingly ^2 refers to the second parent.

$ git rev-parse HEAD^
6cf050af75c8c71d441364906673690bb7f0b63d
$ git rev-parse HEAD^1
6cf050af75c8c71d441364906673690bb7f0b63d
$ git rev-parse HEAD^2
4d5b4ee433a8232300de0b62c97f613d2d812f6b
$

So what you are asking for sounds like the commit message for the second parent to the merge commit which you can get by running

git show -s --format=format:%s%n%n%b HEAD^2

when the current commit is the merge commit.

hlovdal
  • 26,565
  • 10
  • 94
  • 165