From f45ce136a3d7dba589054ea710062b179da8e4cb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Latner?= <matyas.latner@nic.cz>
Date: Fri, 7 Aug 2015 13:48:38 +0200
Subject: [PATCH] New gradle versioning pattern

---
 build.gradle | 42 ++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 40 insertions(+), 2 deletions(-)

diff --git a/build.gradle b/build.gradle
index 6281f1ab0..e10ded077 100644
--- a/build.gradle
+++ b/build.gradle
@@ -193,11 +193,40 @@ task processAssets(dependsOn: [':util:checksum:runChecksum', ':util:checksum:run
 
 def getVersionNameFromGit() {
     def stdout = new ByteArrayOutputStream()
+    String branchName = getBrancheNameFromGit()
+
+    // get last tag in current branch
     exec {
-        commandLine 'git', 'describe', '--tags', '--always'
+        commandLine 'git', 'describe', '--abbrev=0', '--tags', '--first-parent'
         standardOutput = stdout
     }
-    return stdout.toString().trim()
+    String result = stdout.toString().trim();
+
+    // for non release branches add number of commits and branch name
+    if (!"master".equals(branchName)) {
+        stdout = new ByteArrayOutputStream()
+        exec {
+            commandLine 'git', 'rev-list', '--tags', '--no-walk', '--max-count=1', '--first-parent'
+            standardOutput = stdout
+        }
+        String revision = stdout.toString().trim()
+
+        stdout = new ByteArrayOutputStream()
+        exec {
+            commandLine 'git', 'rev-list', "${revision}..HEAD", '--count', '--first-parent'
+            standardOutput = stdout
+        }
+        String numberOfCommits = stdout.toString().trim()
+
+        if ("devel".equals(branchName)) {
+            // add only number of commits for devel branch
+            result = "${result}-${numberOfCommits}"
+        } else {
+            // add branch name and number of commits
+            result = "${result}-${branchName}-${numberOfCommits}"
+        }
+    }
+    return result
 }
 
 def getVersionCodeFromGit() {
@@ -209,6 +238,15 @@ def getVersionCodeFromGit() {
     return stdout.toString().trim().toInteger()
 }
 
+def getBrancheNameFromGit() {
+    def stdout = new ByteArrayOutputStream()
+    exec {
+        commandLine 'git', 'rev-parse', '--abbrev-ref', 'HEAD'
+        standardOutput = stdout
+    }
+    return stdout.toString().trim().toLowerCase()
+}
+
 def getMapConvertedToString(Map map) {
     String result = "";
     map.each { key, value ->
-- 
GitLab