GitVersion configuration is not incrementing
Asked Answered
L

2

8

I'm trying to setup GitVersion to handle our project's semantic versioning (GitFlow), but it's not incrementing automatically as I'm expecting. I'm also struggling with reading the project's documentation, it's not the easiest to follow.

Basically we're hoping to setup a versioning system with Major.Minor.Patch convention, where we increment Major manually, Minor automatically when a release branch gets merged into master, and Patch automatically when a feature branch gets merged into develop.

Here is my current GitVersion.yml

mode: ContinuousDeployment
branches:
  feature:
    increment: Patch
    regex: feature?[/-]
  release:
    increment: Minor
    regex: release?[/-]
  develop:
    regex: develop$
  master:
    regex: master$
ignore:
  sha: []

Also to test it I wrote a small ruby script to speed up the tedious process. I included it just to make sure I'm using GitVersion and GitFlow in the intended way.

require "git"

def new_feature_branch i
  g = Git.open(Dir.pwd)

  branch_name = "feature/number-#{i}"
  g.checkout("develop")
  g.branch(branch_name).checkout

  open('README.md', 'a') do |f|
    f.puts "\r#{i}"
  end

  g.add("README.md")
  g.commit("##{i}")

  g.branch("develop").checkout
  g.merge(branch_name)
  g.branch(branch_name).delete

  print(`gitversion`)
end


new_feature_branch(ARGV[0])

Output of gitversion

{
  "Major":1,
  "Minor":1,
  "Patch":0,
  "PreReleaseTag":"alpha.39",
  "PreReleaseTagWithDash":"-alpha.39",
  "PreReleaseLabel":"alpha",
  "PreReleaseNumber":39,
  "WeightedPreReleaseNumber":39,
  "BuildMetaData":"",
  "BuildMetaDataPadded":"",
  "FullBuildMetaData":"Branch.develop.Sha.57a536a5c6b6abb4313a2067468413447cb49c86",
  "MajorMinorPatch":"1.1.0",
  "SemVer":"1.1.0-alpha.39",
  "LegacySemVer":"1.1.0-alpha39",
  "LegacySemVerPadded":"1.1.0-alpha0039",
  "AssemblySemVer":"1.1.0.0",
  "AssemblySemFileVer":"1.1.0.0",
  "FullSemVer":"1.1.0-alpha.39",
  "InformationalVersion":"1.1.0-alpha.39+Branch.develop.Sha.57a536a5c6b6abb4313a2067468413447cb49c86",
  "BranchName":"develop",
  "Sha":"57a536a5c6b6abb4313a2067468413447cb49c86",
  "ShortSha":"57a536a",
  "NuGetVersionV2":"1.1.0-alpha0039",
  "NuGetVersion":"1.1.0-alpha0039",
  "NuGetPreReleaseTagV2":"alpha0039",
  "NuGetPreReleaseTag":"alpha0039",
  "VersionSourceSha":"27938c50fc6f364eff52bccec8dbc10297bce87b",
  "CommitsSinceVersionSource":39,
  "CommitsSinceVersionSourcePadded":"0039",
  "CommitDate":"2019-10-28"
}

The problem I'm expecting the Patch number to increment each time I merge a feature branch into develop but that isn't happening.

Launcher answered 28/10, 2019 at 16:14 Comment(4)
Where is the version number written? I guess it is in some file? Which language do you use?Turboprop
I'm not sure I understand, do you mean the output of gitversion /version or just the gitversion output. I edited the question to include the latter.Launcher
Have you checked your effective settings? gitversion /showconfig I'm guessing that develop:increment might not be what you expect (patch).Trichomoniasis
@Trichomoniasis thanks, I've updated the config settings since the OP. My answer below has the current config file that is working much more as expected.Launcher
L
3

There are still some minor issues but this config setting is working much more as I intended.

mode: Mainline
tag-prefix: '[vV]'
commit-message-incrementing: MergeMessageOnly
branches:
  feature:
    regex: feature?[/-]
    source-branches: ['develop']
  release:
    increment: Minor
    regex: release?[/-]
  develop:
    is-mainline: true
    increment: Patch
    regex: develop$
  master:
    regex: master$
Launcher answered 31/10, 2019 at 15:34 Comment(1)
a) what are these minor issues? b) i cannot find MergeMessageOnly in the docs - how did you know about this?Paralytic
T
1

This is not really an answer, as you want to use gitversion, but perhaps it shows a different way to handle your issue and it is too long for a comment:

  • Put a file "pre-commit" with the content below in /.git/hooks/
  • In our case, we are working with R and the version is "tracked" in a file called "DESCRIPTION" in the folder of a package (that is why I asked for the file and how it is called.

If you then commit, the patch is increased by +1, the minor/ major version have to be set by hand in the way x+1.0.-1 (which gets x+1.0.0). I guess you should be able to adapt the script to your needs.

Hope that helps.

#!C:/R/R-3.3.0/bin/x64/Rscript

# License: CC0 (just be nice and point others to where you got this)
# Author: Robert M Flight <[email protected]>, github.com/rmflight
#
# This is a pre-commit hook that checks that there are files to be committed, and if there are, increments the package version
# in the DESCRIPTION file.
#
# To install it, simply copy this into the ".git/hooks/pre-commit" file of your git repo, change /path/2/Rscript, and make it
# executable. Note that /path/2/Rscript is the same as your /path/2/R/bin/R, or may be in /usr/bin/Rscript depending on your
# installation. This has been tested on both Linux and Windows installations.
#
# In instances where you do NOT want the version incremented, add the environment variable inc=FALSE to your git call.
# eg "inc=FALSE git commit -m "commit message".
# This is useful when you change the major version number for example.

inc <- TRUE # default

# get the environment variable and modify if necessary
tmpEnv <- as.logical(Sys.getenv("inc"))
if (!is.na(tmpEnv)) {
  inc <- tmpEnv
}

# check that there are files that will be committed, don't want to increment version if there won't be a commit
fileDiff <- system("git diff HEAD --name-only", intern = TRUE)

if ((length(fileDiff) > 0) && inc) {

  currDir <- getwd() # this should be the top level directory of the git repo
  currDCF <- read.dcf("DESCRIPTION")
  currVersion <- currDCF[1,"Version"]
  splitVersion <- strsplit(currVersion, ".", fixed = TRUE)[[1]]
  nVer <- length(splitVersion)
  currEndVersion <- as.integer(splitVersion[nVer])
  newEndVersion <- as.character(currEndVersion + 1)
  splitVersion[nVer] <- newEndVersion
  newVersion <- paste(splitVersion, collapse = ".")
  currDCF[1,"Version"] <- newVersion
  currDCF[1, "Date"] <- strftime(as.POSIXlt(Sys.Date()), "%Y-%m-%d")
  write.dcf(currDCF, "DESCRIPTION")
  system("git add DESCRIPTION")
  cat("Incremented package version and added to commit!\n")
}
Turboprop answered 30/10, 2019 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.