Skip to content

Is it possible to have the result of git rev-list --count in a variable? #1345

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

Closed
chrbkr opened this issue Dec 11, 2017 · 13 comments
Closed
Labels

Comments

@chrbkr
Copy link

chrbkr commented Dec 11, 2017

In our project, we switched from SVN to git. The versioning scheme was <major>.<minor>.<patch>.<revision>.

For continuity, I wanted to keep this scheme, by getting the equivalent of the SVN revision via git rev-list --count HEAD.

Is there a variable in GitVersion I can use for this? Or can this command be invoked in some other way when using GitVersionTask?

@JakeGinnivan
Copy link
Contributor

This particular variable is not, we have a similar variable which is BuildMetaData, but this counts the commits since the version changed.

@chrbkr
Copy link
Author

chrbkr commented Dec 12, 2017

I see, thank you. Is there a way to call an external command in GitVersionTask or is the only possibility using GitVersion in a BeforeBuild task?

@jbaehr
Copy link
Contributor

jbaehr commented Mar 13, 2018

Please note that putting something like a svn revision or git rev-list --count HEAD in a version is not save in the long run. On Windows each of the four parts of a version can only hold 16 bits; see https://blogs.msdn.microsoft.com/msbuild/2007/01/03/why-are-build-numbers-limited-to-65535/
So after 65535 commits you get into trouble. And people already got into such trouble, just search on stack-overflow...

As I was looking for a solution for strictly monotonic increasing version numbers (to satisfy Windows Installer's requirements for "Major Upgrades") in the AssemblyFileVersionAttribute I was also tempted to simply use the total number of commits as the fourth digit. And because our repo had already more then 140k commits at that time I did some research. Indeed, it was a good idea to go another route...
(Our current approach is to use the build number from the CI-server; that can easily be reset when opening a release branch)

@asbjornu
Copy link
Member

I think #1385 might provide a solution to this by allowing the result of git rev-list --count HEAD to be exposed as an environment variable and then that variable being used by GitVersion. Would that help, @chrbkr?

@chrbkr
Copy link
Author

chrbkr commented Feb 18, 2019

Sorry for the late response. Yes, this does definitely help. In order to mitigate the 16 bit limitation pointed out by @jbaehr, it might be useful to use git rev-list HEAD --count --first-parent (on master) instead, so only releases are counted (assuming a git-flow approach).
So, the approach would be to set an environment variable in MsBuild and use that in GitVersionTask?

@jbaehr
Copy link
Contributor

jbaehr commented Feb 18, 2019

@chrbkr just be careful with GitVersion's cache. We run GitVersion.exe with the /nocache-flag prior to the build, setup a "weighted prerelease number" env-var by applying a prerelease-label based offset to the commits-since-version-source and use that for the fourth digit (#1433 is an attempt to automate this further). The first call of the GitVersionTask creates the cache with the env-var already in place. This cache is then used by all subsequent GitVerionTask invocations.
(just using build number from the CI-server does not play well with Jenkins' multi-branch
pipelines; thus we don't use what I described in an earlier comment anymore)

When you just set the env-var and only relies on GitVersionTask, you may have to find a way to tell the Task to ignore the cache (which will have a performance impact on large repos when building many projects). Or ensure the env-var is there in all cases.

Finally just use your env-var in GitVersion's format strings, see the "*-format" keys in https://gitversion.readthedocs.io/en/stable/configuration/

@stale
Copy link

stale bot commented Jun 29, 2019

This issue has been automatically marked as stale because it has not had recent activity. After 30 days from now, it will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jun 29, 2019
@stale stale bot closed this as completed Jul 29, 2019
@vincentcastagna
Copy link

So I got kind of the same question. We are migrating an old project to use gitversion. However, this project was relying on git rev-list HEAD --count. Now I update the pipelines to rely on the PreReleaseNumber, however its far off the git count. Isn't there a way to PreReleaseNumber aligned ? Because at a point this prevent us from pushing application to store as the build number must be always higher than previous pushed build.

@asbjornu
Copy link
Member

Can't you use the build counter as a parameter in the version number then, @vincentcastagna?

@vincentcastagna
Copy link

Hey @asbjornu can you elaborate please ? Where to get that build counter (you mean the git count ) ?

@asbjornu
Copy link
Member

You get that from the build system. TeamCity exposes a variable called build.counter, for instance.

@vincentcastagna
Copy link

@asbjornu any official doc about this variable ? Cause can't find.

@asbjornu
Copy link
Member

You will have to check with your build server. Which one do you use, @vincentcastagna?

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

No branches or pull requests

5 participants