jenkinsci / jenkinsci/git-plugin

[JENKINS-61067] Wrong value for GIT_COMMIT after merge?

Open
#3,522 3 comments 0 reactions 0 assignees View on GitHub
component:git-plugin component:gitlab-branch-source-plugin imported-jira-issue priority:major resolution:unresolved
Dominant language
Java
Stars
694
Forks
1.1k
Avg merge
1h 29m
Merged PRs (30d)
3

Description

This issue is just for documentation, since the issue went away with the update to gitlab-branch-source 1.4.4. Before that, a merge request would do a "fat" instead a lightweight checkout to read the Jenkinsfile and I cannot reproduce the issue when just a lightweight checkout is done.

Pipeline sample code to have something to talk about:

def repo = checkout scm

echo repo.GIT_COMMIT

This works correctly for branch builds, but not for merge request builds and the strategy "Merging the merge request with the current target branch revision" - when using that strategy, GIT_COMMIT points to a revision which doesn't exist after the merge, but instead to the merge commit from the "temporary" chechout which was done for reading the Jenkinsfile. Relevant log snippets:

```
Branch indexing
Querying the current revision of merge request #3...
Current revision of merge request #3 is 367f5bc9835cecaf5929cf9a6a8d3600ad1dae32
Checking out git https://.git into /var/lib/jenkins/workspace/SOMETHING_MR-3@​script to read Jenkinsfile
[...]
> git fetch --no-tags --force --progress -- https://.git +refs/merge-requests/3/head:refs/remotes/origin/MR-3 +refs/heads/master:refs/remotes/origin/master # timeout=10
Merging remotes/origin/master commit eb6a5804b1a07157304d00562a489437342049e1 into MR head commit 367f5bc9835cecaf5929cf9a6a8d3600ad1dae32
[...]
Merge succeeded, producing a9473d5a2d79ace679affe37853e74950602b07b
Checking out Revision a9473d5a2d79ace679affe37853e74950602b07b (feature/update-slf4j)
[...]
Commit message: "Merge commit 'eb6a5804b1a07157304d00562a489437342049e1' into HEAD"
[GitLab Pipeline Status] Notifying merge request build status: RUNNING tgruetzm/versions/ci/MR-3: Build started...
[GitLab Pipeline Status] Notified
Running in Durability level: PERFORMANCE_OPTIMIZED
[...]
[GitLab Pipeline Status] Notifying merge request build status: RUNNING tgruetzm/versions/ci/MR-3: Build started...
[Pipeline] Start of Pipeline
[...]
[Pipeline] checkout
[...]
Merging remotes/origin/master commit eb6a5804b1a07157304d00562a489437342049e1 into MR head commit 367f5bc9835cecaf5929cf9a6a8d3600ad1dae32
[...]
Merge succeeded, producing bdabb0071fa91fe128c037a9f3352b459fcfcde8
Checking out Revision bdabb0071fa91fe128c037a9f3352b459fcfcde8 (feature/update-slf4j)
[...]
Commit message: "Merge commit 'eb6a5804b1a07157304d00562a489437342049e1' into HEAD"
First time build. Skipping changelog.
[...]
[Pipeline] echo
{GIT_AUTHOR_EMAIL=xxx, GIT_AUTHOR_NAME=xxx, GIT_BRANCH=feature/update-slf4j, GIT_COMMIT=a9473d5a2d79ace679affe37853e74950602b07b, GIT_COMMITTER_EMAIL=xxx, GIT_COMMITTER_NAME=xxx, GIT_LOCAL_BRANCH=feature/update-slf4j, GIT_PREVIOUS_COMMIT=873ac16b7ac3b44ad9b45cdde7479a3dd73a4beb, GIT_PREVIOUS_SUCCESSFUL_COMMIT=367f5bc9835cecaf5929cf9a6a8d3600ad1dae32, GIT_URL=https://.git}
[...]
+ git log --oneline -2
bdabb00 Merge commit 'eb6a5804b1a07157304d00562a489437342049e1' into HEAD
eb6a580 Update test output
[...]

```

Now, with a lightweight checkout:

```
Started by user Tobias Gruetzmacher
Querying the current revision of merge request #3...
Current revision of merge request #3 is 367f5bc9835cecaf5929cf9a6a8d3600ad1dae32
Obtained Jenkinsfile from eb6a5804b1a07157304d00562a489437342049e1+367f5bc9835cecaf5929cf9a6a8d3600ad1dae32
Running in Durability level: PERFORMANCE_OPTIMIZED
[...]
[GitLab Pipeline Status] Notifying merge request build status: RUNNING tgruetzm/versions/ci/MR-3: Build started...
[GitLab Pipeline Status] Notified
[Pipeline] Start of Pipeline
[...]
[Pipeline] checkout
[...]
Merging remotes/origin/master commit eb6a5804b1a07157304d00562a489437342049e1 into MR head commit 367f5bc9835cecaf5929cf9a6a8d3600ad1dae32
[...]
Merge succeeded, producing 793df539d53a54cb84316c39f81939c344255e4b
Checking out Revision 793df539d53a54cb84316c39f81939c344255e4b (feature/update-slf4j)
[...]
Commit message: "Merge commit 'eb6a5804b1a07157304d00562a489437342049e1' into HEAD"
First time build. Skipping changelog.
[...]
[Pipeline] echo
{GIT_AUTHOR_EMAIL=xxx, GIT_AUTHOR_NAME=xxx, GIT_BRANCH=feature/update-slf4j, GIT_COMMIT=793df539d53a54cb84316c39f81939c344255e4b, GIT_COMMITTER_EMAIL=xxx, GIT_COMMITTER_NAME=xxx, GIT_LOCAL_BRANCH=feature/update-slf4j, GIT_PREVIOUS_COMMIT=a9473d5a2d79ace679affe37853e74950602b07b, GIT_PREVIOUS_SUCCESSFUL_COMMIT=367f5bc9835cecaf5929cf9a6a8d3600ad1dae32, GIT_URL=https://.git}
[...]
+ git log --oneline -2
793df53 Merge commit 'eb6a5804b1a07157304d00562a489437342049e1' into HEAD
eb6a580 Update test output
[...]

```

---
Originally reported by tgr, imported from: Wrong value for GIT_COMMIT after merge?


  • status: Open
  • priority: Major
  • component(s): git-plugin, gitlab-branch-source-plugin
  • resolution: Unresolved
  • votes: 1
  • watchers: 3
  • imported: 2025-12-02

Raw content of original issue

This issue is just for documentation, since the issue went away with the update to gitlab-branch-source 1.4.4. Before that, a merge request would do a "fat" instead a lightweight checkout to read the Jenkinsfile and I cannot reproduce the issue when just a lightweight checkout is done.

Pipeline sample code to have something to talk about:


def repo = checkout scm

echo repo.GIT_COMMIT

This works correctly for branch builds, but not for merge request builds and the strategy "Merging the merge request with the current target branch revision" - when using that strategy, GIT_COMMIT points to a revision which doesn't exist after the merge, but instead to the merge commit from the "temporary" chechout which was done for reading the Jenkinsfile. Relevant log snippets:


Branch indexing

Querying the current revision of merge request #3...
Current revision of merge request #3 is 367f5bc9835cecaf5929cf9a6a8d3600ad1dae32
Checking out git https://<REPO>.git into /var/lib/jenkins/workspace/SOMETHING_MR-3@script to read Jenkinsfile
[...]
> git fetch --no-tags --force --progress -- https://<REPO>.git +refs/merge-requests/3/head:refs/remotes/origin/MR-3 +refs/heads/master:refs/remotes/origin/master # timeout=10
Merging remotes/origin/master commit eb6a5804b1a07157304d00562a489437342049e1 into MR head commit 367f5bc9835cecaf5929cf9a6a8d3600ad1dae32
[...]
Merge succeeded, producing a9473d5a2d79ace679affe37853e74950602b07b
Checking out Revision a9473d5a2d79ace679affe37853e74950602b07b (feature/update-slf4j)
[...]
Commit message: "Merge commit 'eb6a5804b1a07157304d00562a489437342049e1' into HEAD"
[GitLab Pipeline Status] Notifying merge request build status: RUNNING tgruetzm/versions/ci/MR-3: Build started...
[GitLab Pipeline Status] Notified
Running in Durability level: PERFORMANCE_OPTIMIZED
[...]
[GitLab Pipeline Status] Notifying merge request build status: RUNNING tgruetzm/versions/ci/MR-3: Build started...
[Pipeline] Start of Pipeline
[...]
[Pipeline] checkout
[...]
Merging remotes/origin/master commit eb6a5804b1a07157304d00562a489437342049e1 into MR head commit 367f5bc9835cecaf5929cf9a6a8d3600ad1dae32
[...]
Merge succeeded, producing bdabb0071fa91fe128c037a9f3352b459fcfcde8
Checking out Revision bdabb0071fa91fe128c037a9f3352b459fcfcde8 (feature/update-slf4j)
[...]
Commit message: "Merge commit 'eb6a5804b1a07157304d00562a489437342049e1' into HEAD"
First time build. Skipping changelog.
[...]
[Pipeline] echo
{GIT_AUTHOR_EMAIL=xxx, GIT_AUTHOR_NAME=xxx, GIT_BRANCH=feature/update-slf4j, GIT_COMMIT=a9473d5a2d79ace679affe37853e74950602b07b, GIT_COMMITTER_EMAIL=xxx, GIT_COMMITTER_NAME=xxx, GIT_LOCAL_BRANCH=feature/update-slf4j, GIT_PREVIOUS_COMMIT=873ac16b7ac3b44ad9b45cdde7479a3dd73a4beb, GIT_PREVIOUS_SUCCESSFUL_COMMIT=367f5bc9835cecaf5929cf9a6a8d3600ad1dae32, GIT_URL=https://<REPO>.git}
[...]
+ git log --oneline -2
bdabb00 Merge commit 'eb6a5804b1a07157304d00562a489437342049e1' into HEAD
eb6a580 Update test output
[...]

Now, with a lightweight checkout:



Started by user Tobias Gruetzmacher

Querying the current revision of merge request #3...
Current revision of merge request #3 is 367f5bc9835cecaf5929cf9a6a8d3600ad1dae32
Obtained Jenkinsfile from eb6a5804b1a07157304d00562a489437342049e1+367f5bc9835cecaf5929cf9a6a8d3600ad1dae32
Running in Durability level: PERFORMANCE_OPTIMIZED
[...]
[GitLab Pipeline Status] Notifying merge request build status: RUNNING tgruetzm/versions/ci/MR-3: Build started...
[GitLab Pipeline Status] Notified
[Pipeline] Start of Pipeline
[...]
[Pipeline] checkout
[...]
Merging remotes/origin/master commit eb6a5804b1a07157304d00562a489437342049e1 into MR head commit 367f5bc9835cecaf5929cf9a6a8d3600ad1dae32
[...]
Merge succeeded, producing 793df539d53a54cb84316c39f81939c344255e4b
Checking out Revision 793df539d53a54cb84316c39f81939c344255e4b (feature/update-slf4j)
[...]
Commit message: "Merge commit 'eb6a5804b1a07157304d00562a489437342049e1' into HEAD"
First time build. Skipping changelog.
[...]
[Pipeline] echo
{GIT_AUTHOR_EMAIL=xxx, GIT_AUTHOR_NAME=xxx, GIT_BRANCH=feature/update-slf4j, GIT_COMMIT=793df539d53a54cb84316c39f81939c344255e4b, GIT_COMMITTER_EMAIL=xxx, GIT_COMMITTER_NAME=xxx, GIT_LOCAL_BRANCH=feature/update-slf4j, GIT_PREVIOUS_COMMIT=a9473d5a2d79ace679affe37853e74950602b07b, GIT_PREVIOUS_SUCCESSFUL_COMMIT=367f5bc9835cecaf5929cf9a6a8d3600ad1dae32, GIT_URL=https://<REPO>.git}
[...]
+ git log --oneline -2
793df53 Merge commit 'eb6a5804b1a07157304d00562a489437342049e1' into HEAD
eb6a580 Update test output
[...]

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.