-1

I have some specific scenario. I have raised one PR . There are multiple commits to this PR.

Now I need to get the commit hash, which is going to be generated after merging this PR to master. Is it possible to get the commit hash before merging it ?

suppose one application component is : xyz

  1. we created a PR for that feature branch to master jenkins -> PR build starts -> deploy the application on a node -> jacoco plugin in the application creates a exec file

  2. PR merge happend from master build -> no deployment for CC now ...

  3. After the deployment is done, from a separate place CC coverage job is triggered -> which is going to take the exec file and push to sonar.

  4. here I was trying to make it like .. the exec file name will have the <Future_git_commit_hash>_xyz.exec

  5. in CC coverage job i will checkout this commit_id .. and then push to sonar. (here generally we dont need the commit_id, master only we can checkout and push to sonar) ...

problem comes if : before pushing to sonar, again one more PR is raised. and new exec file gets created. so i was thinking to have the commit id so that i can differentiate.

anyway if getting the commitid before merging is not possible.. need to check other approaches.

Baitanik
  • 63
  • 1
  • 7
  • 3
    Why? This smells like an [XY Problem](https://xyproblem.info). It could theoretically be predicted if you knew a lot of details that are prone to changing (exact time the merge commit will be done, exact hash of the commit it gets merged into and exact result of the whole file tree, basically you must know the exact state of the target branch at the time of the merge). – Joachim Sauer Oct 19 '20 at 08:30
  • I need it for some processing. Actually I thought when the PR is raised , github will create a temporary id using those information like timestamp and author of PR etc. and when the PR is merged, same id will be used. but I guess it does not work that way.. at the PR merge again it will calculate the hash.. because after raising PR: if we do folloing: git fetch origin +refs/pull/601/merge git checkout FETCH_HEAD then we get one git hash.. I was thinking if we could preserve that one only for merging. – Baitanik Oct 19 '20 at 08:50
  • 2
    "some processing" doesn't really tell us anything. **Everything** in IT can be described as "some processing". If you told us what **specifically** you are trying to do, then we can guide you towards a workable solution. – Joachim Sauer Oct 19 '20 at 08:52
  • added in the description itself. – Baitanik Oct 19 '20 at 09:13
  • 1
    Does this answer your question? [Predict git commit id and commit a file which contains that commit id](https://stackoverflow.com/questions/21942694/predict-git-commit-id-and-commit-a-file-which-contains-that-commit-id) – Daemon Painter Oct 19 '20 at 09:20

2 Answers2

2

No, it isn't possible.

A commit contains the description of elements such as :

  1. the author of the commit,
  2. the content of the commit (the list of files + directories that make up this commit),
  3. the commit's parents
  4. the timestamp at which the commit is created

The hash of a commit is a hash of all this information. Since it is not possible to guess beforehand the timestamp at which the commit will occur, you cannot guess beforehand what that hash will be.


I imagine you want to use this hash for some processing : the generic way is to have the platform which handles the PR (github ? azure devops ?) trigger a script or an api call after the PR has been accepted and merged.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • Yeah i am using github. I need it for some processing... so before commit happens then it is not possible.. I thought git will create a temporary id when the PR is raised using those details timestamp and all .. the same ID will be used for merging.. timestamp etc it will take when the PR was raised . – Baitanik Oct 19 '20 at 08:44
  • as @JoachimSauer commented : can you explain what you want to do with this commit hash ? – LeGEC Oct 19 '20 at 08:46
  • because after raising PR: if we do folloing: git fetch origin +refs/pull/601/merge git checkout FETCH_HEAD then we get one git hash.. I was thinking if we could preserve that one only for merging. – Baitanik Oct 19 '20 at 08:51
1

No, this is impossible, since the exact time will be included in the hash of the merge commit.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61