Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMake: use git rev-parse to get GIT_COMMIT_HASH #1185

Merged
merged 1 commit into from
Jan 11, 2022

Conversation

herrgahr
Copy link
Contributor

@herrgahr herrgahr commented Jan 9, 2022

The old approach of reading .git/HEAD does not work when using git worktrees, where the folder layout looks roughly like:

solvespace.git/                      - bare clone (.git dir)
solvespace.git/work                  - example worktree containing master
solvespage.git/worktrees/work/       - .git dir of worktree
solvespage.git/worktrees/work/HEAD   - actual HEAD ref for master

First attempt was to just get GIT_ROOT from git rev-parse --git-dir but that wasn't enough, since:

  1. GIT_ROOT points to solvespage.git/worktrees/work/
  2. GIT_ROOT/HEAD points to refs/heads/master
  3. GIT_ROOT/refs/heads/master does not exist but the old implementation would want to use this to get the sha

so we need two invocations of git rev-parse

  1. git rev-parse --git-dir to get GIT_DIR needed for setting GIT_DEPENDS
  2. git rev-parse HEAD to get the sha of the worktree's HEAD

@CLAassistant
Copy link

CLAassistant commented Jan 9, 2022

CLA assistant check
All committers have signed the CLA.

@herrgahr
Copy link
Contributor Author

herrgahr commented Jan 9, 2022

Note: I don't use CMake much so this might not be the most sensible approach to this. I'm also not certain at which cmake version this was added so I'm not sure if cmake_minimum_required(VERSION 3.9...3.19) covers execute_process

The old approach of reading .git/HEAD does not work when using git
worktrees, where the folder layout looks roughly like:

solvespace.git/                      - bare clone (.git dir)
solvespace.git/work                  - example worktree containing master
solvespage.git/worktrees/work/       - .git dir of worktree
solvespage.git/worktrees/work/HEAD   - actual HEAD ref for master

First attempt was to just get GIT_ROOT from `git rev-parse --git-dir` but
that wasn't enough, since:

1. GIT_ROOT points to solvespage.git/worktrees/work/
2. GIT_ROOT/HEAD points to refs/heads/master
3. GIT_ROOT/refs/heads/master does not exist but the old implementation
   would want to use this to get the sha

so we need two invocations of git rev-parse

1. `git rev-parse --git-dir` to get GIT_DIR
    needed for setting GIT_DEPENDS
2. `git rev-parse HEAD` to get the sha of the worktree's HEAD
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${GIT_DEPENDS})

string(STRIP ${HEAD_REF} HEAD_REF)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is redundant since execute_process's OUTPUT_STRIP_TRAILING_WHITESPACE option takes care of trailing whitespace and the output of git rev-parse doesn't containg leading whitespace. But: if git rev-parse fails, ${HEAD_REF} is undefined and cmake gives a confusing error message regarding the number of arguments give to strip

@ruevs
Copy link
Member

ruevs commented Jan 9, 2022

@herrgahr welcome to the project :-)

It is rather interesting that you submit this pull request now since there is an active discussion relating to this for the last few days here #1172 :-) Take a look.

@herrgahr
Copy link
Contributor Author

herrgahr commented Jan 9, 2022

Oohhh, I wasn't aware that this problem was being discussed there, the title didn't give it away ;)

This was the typical drive-by fix, I just happened to check out the sources today after a looong time

I already suspected rummaging through .git folder and HEAD files manually was for edge cases where git is not installed or somesuch. So I guess it makes sense to close this in favor of integrating @rpavlik 's CMake magic as it seems to cover all bases.

Either way, I'd be a happy camper if the build worked with worktrees :)

Should I close the PR and open an issue for this?

@ruevs
Copy link
Member

ruevs commented Jan 9, 2022

Either way, I'd be a happy camper if the build worked with worktrees :)

Should I close the PR and open an issue for this?

I'd say leave the PR open for now, but it's up to @phkahler ultimately. And I'm not at all proficient with CMake to say whether Rpavlik's solution works with worktrees :-)

@ghost
Copy link

ghost commented Jan 10, 2022

@herrgahr welcome to the project :-)

I am second with such greetings ;)

JFTR, @herrgahr, there no need to sign CLA anymore, as it violates GPL:

@rpavlik
Copy link
Contributor

rpavlik commented Jan 10, 2022

I'm pretty sure my module works with work trees, but not 100% sure. Welcome!

@phkahler
Copy link
Member

JFTR, @herrgahr, there no need to sign CLA anymore, as it violates GPL:

No @Symbian9 it does not violate the GPL. However we do not require it anymore, the code shall stay GPLv3+

@ghost
Copy link

ghost commented Jan 10, 2022

No, it does not violate the GPL.

@phkahler, It is violate in case that any changed file licensed under GPL (SolveSpace source actually GPL, not double licensed) could NOT be double licensed as stated in CLA Assistant:

I already highlighted it here: #1098 (comment)

So, as in actual pull request proposed changes to already GPL-licensed ./cmake/GetGitCommitHash.cmake it could NOT be double licensed anymore.

@jwesthues already clarified in 2fcc933 that GPL is a single license for contributing to SolveSpace repo:

SolveSpace is licensed under the GPLv3 or later and any contributions
must be made available under the terms of that license.

And as result any pull request which authors signed CLA Assistant Agreement highly likely could NOT be merged into SolveSpace repo at all as such "double re-licensing" mentioned in signed CLA fully violates SolveSpace GPL-licensing.

@phkahler
Copy link
Member

I'd say leave the PR open for now, but it's up to @phkahler ultimately. And I'm not at all proficient with CMake to say whether Rpavlik's solution works with worktrees :-)

Oh man, don't leave stuff like that to me! If the @rpavlik solution does the job, lets go with that. Otherwise @ruevs you can merge this PR ;-)

@phkahler
Copy link
Member

On second thought, this is a fix and simplification that is available now. If it gets supplanted later that's fine too.

@phkahler phkahler merged commit f1e47e6 into solvespace:master Jan 11, 2022
@herrgahr herrgahr deleted the cmake-git-hash branch January 11, 2022 23:10
@ruevs
Copy link
Member

ruevs commented Jan 12, 2022

@phkahler @herrgahr @rpavlik unfortunately this breaks the git commit in the version string when building in the IDE in VisualStudio since (I guess) git is not in the "path" of the "build environment". I do have git and it is in the global path of the system.

So the version ends up "3.0~" :-(

@phkahler
Copy link
Member

@ruevs so I guess I'll revert it tonight.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants