When I merge some branch to the master (in GitHub), the jenkins pipeline is triggered.
Is there a way to identify using any command of git during the current execution in Jenkins "this is a merge commit"? (not webhook - I looking another solution)
Simple: I have commit id 28c7be3b705fb517b09067e059fdlskkdjsa7ce0fd3
and I want to know if this commit of merge (PR to master).
One simple way is : check if this commit has a second parent
if git rev-parse --verify -q $commitid^2 > /dev/null; then
echo "commit $commitid is a merge commit"
else
echo "commit $commitid is a simple commit"
fi
--verify
Verify that exactly one parameter is provided, and that it can be turned into a raw 20-byte SHA-1 that can be used to access the object database. If so, emit it to the standard output; otherwise, error out.-q
Only meaningful in --verify
mode. Do not output an error message if the first argument is not a valid object name; instead exit with non-zero status silently.$commitid
Long or short SHA-1 hash of a commit. ^2
The ^
means "first parent of that commit object" with the appeded 2
this turns to "second parent of that commit object" so ^n
= "n-th parent of that commit object" and if the commit does not have n-th parent the command return value is 128
(otherwise 0
) /dev/null
-- when succesful, this command converts the input to its full sha and prints it on stdout. Full documentation for git rev-parse
available here .
If you want to check wether $commitid
is part of one of Github's Pull requests , that's a different requirement.
One part of pull request information (such as : the commit a specific PR points to) is stored in git, but you will need to use Github API ( link here ) to access other information (such as : the PR status, its author, its discussion feeed, etc ...)
Github creates references in git
for each pull request prefixed with refs/pull/
; for pull request {xx}
, the head of that pull request stored in refs/pull/{xx}/head
, and if the PR gets merged, the result of the merge gets stored in refs/pull/{xx}/merge
.
You can use that to find out if the current commit is the head of a pull request :
# fetch references starting with 'refs/pull/...',
# store them locally next to the remote branches : 'refs/remotes/origin/pull/...'
# (note: you can choose whatever pattern you want to store these references locally)
git fetch origin "+refs/pull/*:refs/remotes/origin/pull/*"
# check if one of these refs point to $commitid :
git for-each-ref --points-at "$commitid" refs/remotes/orign/pull
# you can use '--format' to customize the output :
# for example you can remove the leading 'refs/remotes/origin/pull' to have a shorter output :
$ git for-each-ref --format="%(refname:lstrip=4)" --points-at "$commitid" refs/remotes/orign/pull
42/head # <- this means it is the current head of PR #42
You can now use the API to check if PR #42 is still open :
curl \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/{myuser}/{myrepo}/pulls/42
I want to expand LeGEC's answer .
if git rev-parse --verify -q $commitid^2 > /dev/null; then
echo "commit $commitid is a merge commit"
else
echo "commit $commitid is a simple commit"
fi
--verify
Verify that exactly one parameter is provided, and that it can be turned into a raw 20-byte SHA-1 that can be used to access the object database. If so, emit it to the standard output; otherwise, error out.-q
Only meaningful in --verify
mode. Do not output an error message if the first argument is not a valid object name; instead exit with non-zero status silently.$commitid
Long or short SHA-1 hash of a commit. ^2
The ^
means "first parent of that commit object" with the appeded 2
this turns to "second parent of that commit object" so ^n
= "n-th parent of that commit object" and if the commit does not have n-th parent the command return value is 128
(otherwise 0
) Full documentation available here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.