4

I'm a minecraft modder, and I use the ForgeGradle plugin to build my mods.

I'm currently attempting to setup a versioning scheme based on my git revisions and hash. Under arch linux's PKGBUILD system I would use:

pkgver() {
  cd $_pkgbase
  printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

which would end up with something along the lines of r392.2cc2ebc

I'm attempting with the following:

ext.revision = 'git rev-list --count HEAD'.execute()
ext.hash     = 'git rev-parse --short HEAD'.execute()
version      = "r${revision.text}.${hash.text}"

which gets me almost what I need, r70?.11ae542?; not sure how to get rid of the ? in each portion of the version. Gradle 2.0, suggestions?

Further investigation due to Peter Niederwieser's comment lead me to run build with the info flag, and it does seem newlines are getting stuck into the file name:

Executing task ':reobf' (up-to-date check took 0.004 secs) due to:
  Output file build/libs/CreepyPastaCraft-1.7.x-r70
.11ae542
-universal.jar has changed.
7
  • What's the question about? How to remove question marks from a string? Jul 21, 2014 at 4:26
  • That would work, yes, but I'm looking for something cleaner than .execute().text.remove? or whatever the actual code would be.
    – hanetzer
    Jul 21, 2014 at 4:28
  • On my machine, both strings end in a newline. Not sure why they come out as ? for you. Looks like some encoding problem. Jul 21, 2014 at 4:49
  • maybe its because the newlines get stuck into the filename and it may not be valid?
    – hanetzer
    Jul 21, 2014 at 5:02
  • Of course you don't want to have newlines in the version string. You can try something like version = version.replace("\n", "").replace("\r", ""). Jul 21, 2014 at 5:09

2 Answers 2

13

Well, as Peter has declined to turn his comment into an answer, I shall answer my own question for the benefit of others looking to do the same:

ext.revision = 'git rev-list --count HEAD'.execute().text.trim()
ext.hash = 'git rev-parse --short HEAD'.execute().text.trim()
version = "r${revision}.${hash}"

This yeilds the same result as the bash expression in my question.

1
  • We use something similar to this. The problem we're facing is that when the HEAD changes (ie a pull, merge, commit, whatever) this value isn't updated and we need to manually sync the project.
    – Xiao
    Jul 22, 2015 at 1:53
1

If you'd like the version to be the currently checked-out reference abbreviation (branch name or tag name) , then this is all it takes:

version 'git rev-parse --abbrev-ref HEAD'.execute().getText().trim()

However, this will set version as "HEAD" if head is not a branch or a tag. If you'd like it that the version will be based on the short hash under these circumstances, then you can do the following:

version 'git rev-parse --abbrev-ref HEAD'.execute().getText().trim()
if (version.equals('HEAD')) {
    version 'git rev-parse --short HEAD'.execute().getText().trim()
}
1
  • I get > No signature of method: build_9web9znalz2b16fdzxcfh6kna.publishing() is applicable for argument types: (build_9web9znalz2b16fdzxcfh6kna$_run_closure8) values: [build_9web9znalz2b16fdzxcfh6kna$_run_closure8@19fac889]
    – Antonin
    Dec 16, 2021 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.