0

As part of our CI build, we have a script that deploys the latest version of the project and leaves a comment on the corresponding PR with the subject from the previous commit and a link to the deploy.

However, sometimes these builds contain changes from multiple commits if a developer pushes multiple commits to the PR at the same time. I'd like to modify the script so that the message containing the link also contains the subject lines from each of these new commits.

The current implementation, that only gets the most recent commit, has a line of code similar to this:

COMMIT_MESSAGE=$(git --no-pager log --pretty=format:"%s" -1)

Is there some similar way I can interact with git to get the messages for all the commits that came from the same push as the most recent commit above?

JKillian
  • 18,061
  • 8
  • 41
  • 74

2 Answers2

1

Likely you need to configure update git hook in your team git server.

The hook receives three command-line arguments: name of the reference being pushed in, SHA-1 of its previous state, SHA-1 of its new state. So git log --pretty=format:"%s" $2..$3 within the hook will give you a desired log.

You should specifically handle cases of branch creation and deletion, the corresponding SHA-1 will be zeroed in this case:

refname="$1"
oldrev="$2"
newrev="$3"

if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then
    echo "usage: $0 <ref> <oldrev> <newrev>" >&2
    exit 1
fi

echo "Updating \"$refname\" from \"$oldrev\" to \"$newrev\"" >&2

if [ "$oldrev" = "0000000000000000000000000000000000000000" ]; then
    echo "\"$refname\" was created with rev \"$newrev\"" >&2
    # do whatever you need for a new branch or _tag_
    # you may do as described in http://stackoverflow.com/a/5720575/3159253
    # to list only commits specific for a newly created branch/tag,
    # with the only exception: the reference isn't yet created in the target repo,
    # so you don't need to filter it out
    LOG=$(git log --no-merges --pretty=oneline $newrev --not $(git for-each-ref --format="%(refname)") -- ) --
    echo -e "New reference log\n$LOG\n===" >&2
    exit 0
fi

if [ "$newrev" = "0000000000000000000000000000000000000000" ]; then
    echo "\"$refname\" was deleted (last rev was \"$oldrev\"" >&2
    # do whatever you need when a deleted branch or _tag_
    exit 0
fi

LOG=$(git log --pretty=oneline $oldrev..$newrev)
echo -e "Changed reference log\n$LOG\n===" >&2

Check Git Hooks docs for more details.

Remember that the hook is executed for both branches and tags. branch names begins with refs/heads. while tags - from refs/tags so you may distinguish them by those prefixes if you need.

user3159253
  • 16,836
  • 3
  • 30
  • 56
0

You'll need to keep track of the commit prior to the pull request, but if you can get that commit hash, you could use this updated git log command:

git --no-pager log --pretty=format:"%s" <git_hash>..HEAD

For example, here's my output from one of my repos:

> git --no-pager log --pretty=format:"%s" d8ede574fd33cac4d78bda1bce08049962c7a405..HEAD
Update .coffee file as well
Update for RN 0.25
Uodat license to 2016
2.10.0
Merge pull request #66 from Hilzu/master%

You didn't provide enough context to know if you have access to the git hash before the PR, but if you don't, I can't imagine this would be possible without it.

If you could tie into the git hooks on your CI server, that would be ideal, but if you can't you can see the commits while they're being pulled if you change your flow for pulling. See this answer. If you fetch, you can compare origin vs your current master, store the commit hash you're currently on, merge your pull, and then use that git hash to show all the messages between the two commits.

Community
  • 1
  • 1
Adam
  • 4,445
  • 1
  • 31
  • 49
  • "if you have access to the git hash before the PR"... Unfortunately not. I think some sort of git hook integration might be what's needed... – JKillian May 12 '16 at 21:29
  • You're probably right. You might want to use the client side [`post-merge` hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) on your CI server. – Adam May 12 '16 at 21:40