From 4090ff3db59228aabf5a57db4c2afad9c9d23132 Mon Sep 17 00:00:00 2001 From: s1lentq Date: Mon, 7 Nov 2016 19:27:06 +0700 Subject: [PATCH] Refactoring of versioning --- build.gradle | 14 +- buildSrc/build.gradle | 33 +-- .../groovy/gradlecpp/VelocityUtils.groovy | 4 - .../src/main/groovy/versioning/GitInfo.groovy | 6 +- .../groovy/versioning/GitVersioner.groovy | 93 ++++++- .../versioning/RehldsVersionInfo.groovy | 37 ++- gradle.properties | 5 +- rehlds/build.gradle | 14 +- rehlds/engine/buildnum.cpp | 9 +- rehlds/engine/common.h | 12 + rehlds/engine/host.cpp | 12 +- rehlds/engine/host_cmd.cpp | 17 +- rehlds/engine/sys_dll2.cpp | 2 +- rehlds/msvc/PreBuild.bat | 257 +++++++++--------- rehlds/msvc/ReHLDS.vcxproj | 20 +- rehlds/version/appversion.vm | 12 +- 16 files changed, 334 insertions(+), 213 deletions(-) diff --git a/build.gradle b/build.gradle index dfa6e1e..de12d30 100644 --- a/build.gradle +++ b/build.gradle @@ -26,14 +26,22 @@ if (gitInfo && gitInfo.tag && gitInfo.tag[0] == 'v') { majorVersion: m.group(1) as int, minorVersion: m.group(2) as int, maintenanceVersion: m.group(4) ? (m.group(4) as int) : null, - lastCommitDate: gitInfo.lastCommitDate + localChanges: gitInfo.localChanges, + commitDate: gitInfo.commitDate, + commitSHA: gitInfo.commitSHA, + commitURL: gitInfo.commitURL ) } else { versionInfo = new RehldsVersionInfo( majorVersion: project.majorVersion as int, minorVersion: project.minorVersion as int, - suffix: 'SNAPSHOT', - lastCommitDate: gitInfo ? gitInfo.lastCommitDate : new DateTime() + maintenanceVersion: project.maintenanceVersion as int, + suffix: 'dev', + localChanges: gitInfo ? gitInfo.localChanges : true, + commitDate: gitInfo ? gitInfo.commitDate : new DateTime(), + commitSHA: gitInfo ? gitInfo.commitSHA : "", + commitURL: gitInfo ? gitInfo.commitURL : "", + commitCount: gitInfo ? (gitInfo.commitCount as int) : null ) } diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle index 2226751..8b5cf00 100644 --- a/buildSrc/build.gradle +++ b/buildSrc/build.gradle @@ -1,27 +1,24 @@ apply plugin: 'groovy' repositories { - //mavenLocal() - mavenCentral() - maven { - url 'http://nexus.rehlds.org/nexus/content/repositories/rehlds-releases/' - } - maven { - url 'http://nexus.rehlds.org/nexus/content/repositories/rehlds-snapshots/' - } - + //mavenLocal() + mavenCentral() + maven { + url 'http://nexus.rehlds.org/nexus/content/repositories/rehlds-releases/' + } + maven { + url 'http://nexus.rehlds.org/nexus/content/repositories/rehlds-snapshots/' + } } dependencies { compile gradleApi() - compile localGroovy() - compile 'commons-io:commons-io:2.4' - compile 'commons-lang:commons-lang:2.6' - compile 'joda-time:joda-time:2.7' + compile localGroovy() + compile 'commons-io:commons-io:2.4' + compile 'commons-lang:commons-lang:2.6' + compile 'joda-time:joda-time:2.7' - compile 'org.doomedsociety.gradlecpp:gradle-cpp-plugin:1.2' - - compile 'org.eclipse.jgit:org.eclipse.jgit:3.7.0.201502260915-r' - - compile 'org.apache.velocity:velocity:1.7' + compile 'org.doomedsociety.gradlecpp:gradle-cpp-plugin:1.2' + compile 'org.eclipse.jgit:org.eclipse.jgit:3.7.0.201502260915-r' + compile 'org.apache.velocity:velocity:1.7' } diff --git a/buildSrc/src/main/groovy/gradlecpp/VelocityUtils.groovy b/buildSrc/src/main/groovy/gradlecpp/VelocityUtils.groovy index 38d0d56..55eca36 100644 --- a/buildSrc/src/main/groovy/gradlecpp/VelocityUtils.groovy +++ b/buildSrc/src/main/groovy/gradlecpp/VelocityUtils.groovy @@ -3,7 +3,6 @@ package gradlecpp import org.apache.velocity.Template import org.apache.velocity.VelocityContext import org.apache.velocity.app.Velocity -import org.joda.time.format.DateTimeFormat class VelocityUtils { @@ -26,10 +25,7 @@ class VelocityUtils { throw new RuntimeException("Failed to load velocity template ${tplFile.absolutePath}: not found") } - def velocityContext = new VelocityContext(ctx) - velocityContext.put("_DateTimeFormat", DateTimeFormat) - def sw = new StringWriter() tpl.merge(velocityContext, sw) diff --git a/buildSrc/src/main/groovy/versioning/GitInfo.groovy b/buildSrc/src/main/groovy/versioning/GitInfo.groovy index 6514be6..7532e0b 100644 --- a/buildSrc/src/main/groovy/versioning/GitInfo.groovy +++ b/buildSrc/src/main/groovy/versioning/GitInfo.groovy @@ -6,7 +6,11 @@ import org.joda.time.DateTime @CompileStatic @TypeChecked class GitInfo { - DateTime lastCommitDate + boolean localChanges + DateTime commitDate String branch String tag + String commitSHA + String commitURL + Integer commitCount } diff --git a/buildSrc/src/main/groovy/versioning/GitVersioner.groovy b/buildSrc/src/main/groovy/versioning/GitVersioner.groovy index fb7f480..8370411 100644 --- a/buildSrc/src/main/groovy/versioning/GitVersioner.groovy +++ b/buildSrc/src/main/groovy/versioning/GitVersioner.groovy @@ -1,9 +1,15 @@ package versioning +import java.util.Set; + import groovy.transform.CompileStatic import groovy.transform.TypeChecked +import org.eclipse.jgit.api.Git +import org.eclipse.jgit.api.Status; import org.eclipse.jgit.lib.ObjectId import org.eclipse.jgit.lib.Repository +import org.eclipse.jgit.lib.StoredConfig +import org.eclipse.jgit.revwalk.RevCommit import org.eclipse.jgit.revwalk.RevWalk import org.eclipse.jgit.storage.file.FileRepositoryBuilder import org.joda.time.DateTime @@ -15,7 +21,52 @@ class GitVersioner { static GitInfo versionForDir(String dir) { versionForDir(new File(dir)) } + static int getCountCommit(Repository repo) { + Iterable commits = Git.wrap(repo).log().call() + int count = 0; + commits.each { + count++; + } + return count; + } + static String prepareUrlToCommits(String url) { + if (url == null) { + // default remote url + return "https://github.com/dreamstalker/rehlds/commit/"; + } + + StringBuilder sb = new StringBuilder(); + String childPath; + int pos = url.indexOf('@'); + if (pos != -1) { + childPath = url.substring(pos + 1, url.lastIndexOf('.git')).replace(':', '/'); + sb.append('https://'); + } else { + pos = url.lastIndexOf('.git'); + childPath = (pos == -1) ? url : url.substring(0, pos); + } + + // support for different links to history of commits + if (url.indexOf('bitbucket.org') != -1) { + sb.append(childPath).append('/commits/'); + } else { + sb.append(childPath).append('/commit/'); + } + return sb.toString(); + } + // check uncommited changes + static boolean getUncommittedChanges(Repository repo) { + Git git = new Git(repo); + Status status = git.status().call(); + + Set uncommittedChanges = status.getUncommittedChanges(); + for(String uncommitted : uncommittedChanges) { + return true; + } + + return false; + } static GitInfo versionForDir(File dir) { FileRepositoryBuilder builder = new FileRepositoryBuilder() Repository repo = builder.setWorkTree(dir) @@ -27,15 +78,49 @@ class GitVersioner { return null } + final StoredConfig cfg = repo.getConfig(); + def commit = new RevWalk(repo).parseCommit(head) + if (!commit) { + throw new RuntimeException("Can't find last commit.") + } + + def localChanges = getUncommittedChanges(repo); + def commitDate = new DateTime(1000L * commit.commitTime, DateTimeZone.UTC); + if (localChanges) { + commitDate = new DateTime(); + } + def branch = repo.getBranch() - def commitDate = new DateTime(1000L * commit.commitTime, DateTimeZone.UTC) + + String url = null; + String remote_name = cfg.getString("branch", branch, "remote"); + + if (remote_name == null) { + for (String remotes : cfg.getSubsections("remote")) { + if (url != null) { + println 'Found a second remote: (' + remotes + '), url: (' + cfg.getString("remote", remotes, "url") + ')' + continue; + } + + url = cfg.getString("remote", remotes, "url"); + } + } else { + url = cfg.getString("remote", remote_name, "url"); + } + + String commitURL = prepareUrlToCommits(url); String tag = repo.tags.find { kv -> kv.value.objectId == commit.id }?.key + String commitSHA = commit.getId().abbreviate(7).name(); return new GitInfo( - lastCommitDate: commitDate, - branch: branch, - tag: tag + localChanges: localChanges, + commitDate: commitDate, + branch: branch, + tag: tag, + commitSHA: commitSHA, + commitURL: commitURL, + commitCount: getCountCommit(repo) ) } } diff --git a/buildSrc/src/main/groovy/versioning/RehldsVersionInfo.groovy b/buildSrc/src/main/groovy/versioning/RehldsVersionInfo.groovy index 00630fc..7b2a5c6 100644 --- a/buildSrc/src/main/groovy/versioning/RehldsVersionInfo.groovy +++ b/buildSrc/src/main/groovy/versioning/RehldsVersionInfo.groovy @@ -3,6 +3,7 @@ package versioning import groovy.transform.CompileStatic import groovy.transform.ToString import groovy.transform.TypeChecked +import org.joda.time.format.DateTimeFormat import org.joda.time.DateTime @CompileStatic @TypeChecked @@ -13,23 +14,43 @@ class RehldsVersionInfo { Integer maintenanceVersion String suffix - DateTime lastCommitDate + boolean localChanges + DateTime commitDate + String commitSHA + String commitURL + Integer commitCount - String format(String versionSeparator, String suffixSeparator, boolean includeSuffix) { + String asMavenVersion() { StringBuilder sb = new StringBuilder() - sb.append(majorVersion).append(versionSeparator).append(minorVersion) + sb.append(majorVersion).append('.' + minorVersion); if (maintenanceVersion != null) { - sb.append(versionSeparator).append(maintenanceVersion) + sb.append('.' + maintenanceVersion); } - if (suffix && includeSuffix) { - sb.append(suffixSeparator).append(suffix) + if (commitCount != null) { + sb.append('.' + commitCount) + } + + if (suffix) { + sb.append('-' + suffix) + } + + // do mark for this build like a modified version + if (localChanges) { + sb.append('+m'); } return sb.toString() } + String asCommitDate() { + String pattern = "MMM d yyyy"; + if (commitDate.getDayOfMonth() >= 10) { + pattern = "MMM d yyyy"; + } - String asMavenVersion() { - format('.', '-', true) + return DateTimeFormat.forPattern(pattern).withLocale(Locale.ENGLISH).print(commitDate); + } + String asCommitTime() { + return DateTimeFormat.forPattern('HH:mm:ss').withLocale(Locale.ENGLISH).print(commitDate); } } diff --git a/gradle.properties b/gradle.properties index 5bf150a..cdf5124 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,3 @@ -majorVersion=0 -minorVersion=2 +majorVersion=3 +minorVersion=13 +maintenanceVersion=0 diff --git a/rehlds/build.gradle b/rehlds/build.gradle index 88dec9c..118d013 100644 --- a/rehlds/build.gradle +++ b/rehlds/build.gradle @@ -1,6 +1,7 @@ import gradlecpp.RehldsPlayTestPlugin import gradlecpp.RehldsPlayTestTask import gradlecpp.VelocityUtils + import org.doomedsociety.gradlecpp.GradleCppUtils import org.doomedsociety.gradlecpp.LazyNativeDepSet import org.doomedsociety.gradlecpp.cfg.ToolchainConfig @@ -386,22 +387,29 @@ tasks.clean.doLast { task generateAppVersion { RehldsVersionInfo verInfo = (RehldsVersionInfo) rootProject.rehldsVersionInfo + def tplFile = project.file('version/appversion.vm') def renderedFile = project.file('version/appversion.h') + // check to up-to-date inputs.file tplFile inputs.file project.file('gradle.properties') outputs.file renderedFile - inputs.property('version', verInfo.asMavenVersion()) - inputs.property('lastCommitDate', verInfo.lastCommitDate.toString()) + + // this will ensure that this task is redone when the versions change + inputs.property('version', rootProject.version) + inputs.property('commitDate', verInfo.asCommitDate()) doLast { + def templateCtx = [ - verInfo : verInfo + verInfo : verInfo ] def content = VelocityUtils.renderTemplate(tplFile, templateCtx) renderedFile.delete() renderedFile.write(content, 'utf-8') + + println 'The current ReHLDS maven version is ' + rootProject.version + ', url: (' + verInfo.commitURL + '' + verInfo.commitSHA + ')'; } } diff --git a/rehlds/engine/buildnum.cpp b/rehlds/engine/buildnum.cpp index a71c3ea..1a41aa7 100644 --- a/rehlds/engine/buildnum.cpp +++ b/rehlds/engine/buildnum.cpp @@ -28,19 +28,12 @@ #include "precompiled.h" -//TODO: use last commit date there -#ifdef REHLDS_FIXES -static char *date = __DATE__; -#else // REHLDS_FIXES -static char *date = "Aug 8 2013"; -#endif // REHLDS_FIXES - +static char *date = __BUILD_DATE__; static char *mon[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; static char mond[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; - int build_number(void) { int m = 0; diff --git a/rehlds/engine/common.h b/rehlds/engine/common.h index 300dbd5..599512d 100644 --- a/rehlds/engine/common.h +++ b/rehlds/engine/common.h @@ -34,6 +34,18 @@ #include "info.h" #include "com_model.h" +#ifndef REHLDS_FIXES +#ifdef _WIN32 + #define __BUILD_TIME__ "11:17:23" + #define __BUILD_DATE__ "Aug 8 2013" +#else + #define __BUILD_TIME__ "13:14:09" + #define __BUILD_DATE__ "Aug 29 2013" +#endif +#else + #define __BUILD_TIME__ APP_COMMIT_TIME + #define __BUILD_DATE__ APP_COMMIT_DATE +#endif #ifdef HOOK_ENGINE #define serverinfo (*pserverinfo) diff --git a/rehlds/engine/host.cpp b/rehlds/engine/host.cpp index a010094..d4c5136 100644 --- a/rehlds/engine/host.cpp +++ b/rehlds/engine/host.cpp @@ -1123,20 +1123,12 @@ void Host_Version(void) if (g_pcls.state != ca_dedicated) { Con_DPrintf("Protocol version %i\nExe version %s (%s)\n", PROTOCOL_VERSION, gpszVersionString, gpszProductString); -#ifdef REHLDS_FIXES - Con_DPrintf("Exe build: " __TIME__ " " __DATE__ " (%i)\n", build_number()); -#else - Con_DPrintf("Exe build: 13:14:09 Aug 29 2013 (%i)\n", build_number()); -#endif // REHLDS_FIXES + Con_DPrintf("Exe build: " __BUILD_TIME__ " " __BUILD_DATE__ " (%i)\n", build_number()); } else { Con_Printf("Protocol version %i\nExe version %s (%s)\n", PROTOCOL_VERSION, gpszVersionString, gpszProductString); -#ifdef REHLDS_FIXES - Con_Printf("Exe build: " __TIME__ " " __DATE__ " (%i)\n", build_number()); -#else - Con_Printf("Exe build: 13:14:09 Aug 29 2013 (%i)\n", build_number()); -#endif // REHLDS_FIXES + Con_Printf("Exe build: " __BUILD_TIME__ " " __BUILD_DATE__ " (%i)\n", build_number()); } } diff --git a/rehlds/engine/host_cmd.cpp b/rehlds/engine/host_cmd.cpp index a71c03f..d3cc85d 100644 --- a/rehlds/engine/host_cmd.cpp +++ b/rehlds/engine/host_cmd.cpp @@ -2432,16 +2432,15 @@ void Host_Changelevel2_f(void) void Host_Version_f(void) { Con_Printf("Protocol version %i\nExe version %s (%s)\n", PROTOCOL_VERSION, gpszVersionString, gpszProductString); + #ifdef REHLDS_FIXES - Con_Printf("Exe build: " __TIME__ " " __DATE__ " (%i)\n", build_number()); -#else // REHLDS_FIXES - #ifdef _WIN32 - Con_Printf("Exe build: 11:17:24 Aug 8 2013 (%i)\n", build_number()); - #else // _WIN32 - Con_Printf("Exe build: 10:03:21 Aug 8 2013 (%i)\n", build_number()); - #endif // _WIN32 -#endif // REHLDS_FIXES - Con_Printf("ReHLDS API version %i.%i\n", REHLDS_API_VERSION_MAJOR, REHLDS_API_VERSION_MINOR); + Con_Printf("ReHLDS version: " APP_VERSION "\n"); + Con_Printf("Build date: " __BUILD_TIME__ " " __BUILD_DATE__ " (%i)\n", build_number()); + Con_Printf("Build from: "APP_COMMIT_URL APP_COMMIT_SHA"\n"); +#else + Con_Printf("Exe build: " __BUILD_TIME__ " " __BUILD_DATE__ " (%i)\n", build_number()); +#endif + } /* <3d516> ../engine/host_cmd.c:3382 */ diff --git a/rehlds/engine/sys_dll2.cpp b/rehlds/engine/sys_dll2.cpp index 1ea1ccc..88e6e87 100644 --- a/rehlds/engine/sys_dll2.cpp +++ b/rehlds/engine/sys_dll2.cpp @@ -705,7 +705,7 @@ bool CDedicatedServerAPI::Init_noVirt(char *basedir, char *cmdline, CreateInterf #endif if (!Q_strstr(cmdline, "-nobreakpad")) { - CRehldsPlatformHolder::get()->SteamAPI_UseBreakpadCrashHandler(va("%d", build_number()), "Aug 8 2013", "11:17:26", 0, 0, 0); + CRehldsPlatformHolder::get()->SteamAPI_UseBreakpadCrashHandler(va("%d", build_number()), __BUILD_DATE__, __BUILD_TIME__, 0, 0, 0); } TraceInit("Sys_InitArgv( m_OrigCmd )", "Sys_ShutdownArgv()", 0); Sys_InitArgv(m_OrigCmd); diff --git a/rehlds/msvc/PreBuild.bat b/rehlds/msvc/PreBuild.bat index 5ec0c3e..a950c49 100644 --- a/rehlds/msvc/PreBuild.bat +++ b/rehlds/msvc/PreBuild.bat @@ -1,27 +1,37 @@ -@echo OFF +@setlocal enableextensions enabledelayedexpansion +@echo off :: :: Pre-build auto-versioning script :: -SET srcdir=%~1 -SET repodir=%~2 +set srcdir=%~1 +set repodir=%~2 set old_version= -set old_specialbuild="" -set version_revision=0 -set version_specialbuild= -set version_date=? -set version_pdate= -set version_pdate_1= -set version_pdate_2= -set version_major= -set version_minor= -set version_maintenance= +set version_major=0 +set version_minor=0 +set version_maintenance=0 +set version_modifed= + +set commitSHA= +set commitURL= +set commitCount=0 +set branch_name=master + +for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set "dt=%%a" +set "YYYY=%dt:~0,4%" +set "MM=%dt:~4,2%" +set "DD=%dt:~6,2%" +set "hour=%dt:~8,2%" +set "min=%dt:~10,2%" +set "sec=%dt:~12,2%" + +for /f "tokens=%MM%" %%I in ("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec") do set "month=%%I" :: -:: Check for SubWCRev.exe presence +:: Check for git.exe presence :: -SubWCRev.exe 2>NUL >NUL +CALL git.exe describe >NUL 2>&1 set errlvl="%ERRORLEVEL%" :: @@ -30,127 +40,144 @@ set errlvl="%ERRORLEVEL%" IF EXIST "%srcdir%\appversion.h" ( FOR /F "usebackq tokens=1,2,3" %%i in ("%srcdir%\appversion.h") do ( IF %%i==#define ( - IF %%j==APP_VERSION_C SET old_version=%%k - IF %%j==APP_VERSION_SPECIALBUILD SET old_specialbuild=%%k + IF %%j==APP_VERSION ( + :: Remove quotes + set v=%%k + set old_version=!v:"=! + ) ) ) ) -IF NOT %errlvl% == "1" ( - echo can't locate SubWCRev.exe - auto-versioning step won't be performed +IF %errlvl% == "1" ( + echo can't locate git.exe - auto-versioning step won't be performed :: if we haven't appversion.h, we need to create it - IF "%old_version%" == "" ( - set version_revision=0 - set version_date=? - goto _readVersionH + IF NOT "%old_version%" == "" ( + set commitCount=0 ) - exit /B 0 ) :: -:: Create template file for SubWCRev +:: Read major, minor and maintenance version components from Version.h :: - -:GETTEMPNAME -:: Use current path, current time and random number to create unique file name -SET TMPFILE=svn-%CD:~-15%-%RANDOM%-%TIME:~-5%-%RANDOM% -:: Remove bad characters -SET TMPFILE=%TMPFILE:\=% -SET TMPFILE=%TMPFILE:.=% -SET TMPFILE=%TMPFILE:,=% -SET TMPFILE=%TMPFILE: =% -:: Will put in a temporary directory -SET TMPFILE=%TMP%.\%TMPFILE% -IF EXIST "%TMPFILE%" GOTO :GETTEMPNAME - -echo #define SVNV_REVISION ^$WCREV^$ >"%TMPFILE%.templ" -echo #define SVNV_DATE ^$WCDATE=^%%Y-^%%m-^%%d__^%%H-^%%M-^%%S^$ >>"%TMPFILE%.templ" -echo #define SVNV_PDATE_1 ^$WCDATE=^%%Y-^%%m-^%%d^$ >>"%TMPFILE%.templ" -echo #define SVNV_PDATE_2 ^$WCDATE=^%%H:^%%M:^%%S^$ >>"%TMPFILE%.templ" -echo . >>"%TMPFILE%.templ" - -:: -:: Process template -:: -SubWCRev.exe "%repodir%\." "%TMPFILE%.templ" "%TMPFILE%.h" >NUL - -IF NOT "%ERRORLEVEL%" == "0" ( - echo SubWCRev.exe done with errors [%ERRORLEVEL%]. - echo Check if you have correct SVN repository at '%repodir%' - echo Auto-versioning step will not be performed. - - DEL /F /Q "%TMPFILE%.templ" 2>NUL - DEL /F /Q "%TMPFILE%.h" 2>NUL - - :: if we haven't appversion.h, we need to create it - IF "%old_version%" == "" ( - set version_revision=0 - set version_date=? - goto _readVersionH +IF EXIST "%srcdir%\version.h" ( + FOR /F "usebackq tokens=1,2,3" %%i in ("%srcdir%\version.h") do ( + IF %%i==#define ( + IF %%j==VERSION_MAJOR set version_major=%%k + IF %%j==VERSION_MINOR set version_minor=%%k + IF %%j==VERSION_MAINTENANCE set version_maintenance=%%k + ) + ) +) ELSE ( + FOR /F "usebackq tokens=1,2,3,* delims==" %%i in ("%repodir%..\gradle.properties") do ( + IF NOT [%%j] == [] ( + IF %%i==majorVersion set version_major=%%j + IF %%i==minorVersion set version_minor=%%j + IF %%i==maintenanceVersion set version_maintenance=%%j + ) ) - exit /B 0 ) -DEL /F /Q "%TMPFILE%.templ" 2>NUL - :: :: Read revision and release date from it :: -FOR /F "usebackq tokens=1,2,3" %%i in ("%TMPFILE%.h") do ( - IF %%i==#define ( - IF %%j==SVNV_REVISION SET version_revision=%%k - IF %%j==SVNV_DATE SET version_date=%%k - IF %%j==SVNV_PDATE_1 SET version_pdate_1=%%k - IF %%j==SVNV_PDATE_2 SET version_pdate_2=%%k +IF NOT %errlvl% == "1" ( + :: Get current branch + FOR /F "tokens=*" %%i IN ('"git -C "%repodir%\." rev-parse --abbrev-ref HEAD"') DO ( + set branch_name=%%i + ) + + FOR /F "tokens=*" %%i IN ('"git -C "%repodir%\." rev-list --count !branch_name!"') DO ( + IF NOT [%%i] == [] ( + set commitCount=%%i + ) ) ) -DEL /F /Q "%TMPFILE%.h" 2>NUL -SET version_pdate=%version_pdate_1% %version_pdate_2% +:: +:: Get remote url repository +:: +IF NOT %errlvl% == "1" ( + + set branch_remote=origin + :: Get remote name by current branch + FOR /F "tokens=*" %%i IN ('"git -C "%repodir%\." config branch.!branch_name!.remote"') DO ( + set branch_remote=%%i + ) + :: Get remote url + FOR /F "tokens=2 delims=@" %%i IN ('"git -C "%repodir%\." config remote.!branch_remote!.url"') DO ( + set commitURL=%%i + ) + :: Get commit id + FOR /F "tokens=*" %%i IN ('"git -C "%repodir%\." rev-parse --verify HEAD"') DO ( + set shafull=%%i + set commitSHA=!shafull:~0,+7! + ) + + IF [!commitURL!] == [] ( + + FOR /F "tokens=1" %%i IN ('"git -C "%repodir%\." config remote.!branch_remote!.url"') DO ( + set commitURL=%%i + ) + + :: strip .git + if "x!commitURL:~-4!"=="x.git" ( + set commitURL=!commitURL:~0,-4! + ) + + :: append extra string + If NOT "%commitURL%"=="%commitURL:bitbucket.org=%" ( + set commitURL=!commitURL!/commit/ + ) ELSE ( + set commitURL=!commitURL!/commits/ + ) + + ) ELSE ( + :: strip .git + if "x!commitURL:~-4!"=="x.git" ( + set commitURL=!commitURL:~0,-4! + ) + :: replace : to / + set commitURL=!commitURL::=/! + + :: append extra string + If NOT "%commitURL%"=="%commitURL:bitbucket.org=%" ( + set commitURL=https://!commitURL!/commit/ + ) ELSE ( + set commitURL=https://!commitURL!/commits/ + ) + ) +) :: :: Detect local modifications :: -SubWCRev.exe "%repodir%\." -nm >NUL - -IF "%ERRORLEVEL%" == "7" ( - set version_specialbuild=m -) ELSE ( - set version_specialbuild= +set localChanged=0 +IF NOT %errlvl% == "1" ( + FOR /F "tokens=*" %%i IN ('"git -C "%repodir%\." ls-files -m"') DO ( + set localChanged=1 + ) ) -:_readVersionH -:: -:: Read major, minor and maintenance version components from Version.h -:: -FOR /F "usebackq tokens=1,2,3" %%i in ("%srcdir%\version.h") do ( - IF %%i==#define ( - IF %%j==VERSION_MAJOR SET version_major=%%k - IF %%j==VERSION_MINOR SET version_minor=%%k - IF %%j==VERSION_MAINTENANCE SET version_maintenance=%%k - ) +IF [%localChanged%]==[1] ( + set version_modifed=+m ) :: :: Now form full version string like 1.0.0.1 :: -IF "%version_maintenance%" == "" ( - set new_version=%version_major%,%version_minor%,0,%version_revision% -) ELSE ( - set new_version=%version_major%,%version_minor%,%version_maintenance%,%version_revision% -) + +set new_version=%version_major%.%version_minor%.%version_maintenance%.%commitCount%-dev%version_modifed% :: :: Update appversion.h if version has changed or modifications/mixed revisions detected :: IF NOT "%new_version%"=="%old_version%" goto _update -IF NOT "%version_specialbuild%"==%old_specialbuild% goto _update goto _exit :_update -echo Updating appversion.h, new version is "%new_version%", the old one was "%old_version%" -echo new special build is "%version_specialbuild%", the old one was %old_specialbuild% +echo Updating appversion.h, new version is "%new_version%", the old one was %old_version% echo #ifndef __APPVERSION_H__>"%srcdir%\appversion.h" echo #define __APPVERSION_H__>>"%srcdir%\appversion.h" @@ -161,36 +188,15 @@ echo // Don't edit it.>>"%srcdir%\appversion.h" echo // >>"%srcdir%\appversion.h" echo.>>"%srcdir%\appversion.h" echo // Version defines>>"%srcdir%\appversion.h" - -IF "%version_maintenance%" == "" ( - echo #define APP_VERSION_D %version_major%.%version_minor%.%version_revision% >>"%srcdir%\appversion.h" - echo #define APP_VERSION_STRD "%version_major%.%version_minor%.%version_revision%">>"%srcdir%\appversion.h" - echo #define APP_VERSION_C %version_major%,%version_minor%,0,%version_revision% >>"%srcdir%\appversion.h" - echo #define APP_VERSION_STRCS "%version_major%, %version_minor%, 0, %version_revision%">>"%srcdir%\appversion.h" -) ELSE ( - echo #define APP_VERSION_D %version_major%.%version_minor%.%version_maintenance%.%version_revision% >>"%srcdir%\appversion.h" - echo #define APP_VERSION_STRD "%version_major%.%version_minor%.%version_maintenance%.%version_revision%">>"%srcdir%\appversion.h" - echo #define APP_VERSION_C %version_major%,%version_minor%,%version_maintenance%,%version_revision% >>"%srcdir%\appversion.h" - echo #define APP_VERSION_STRCS "%version_major%, %version_minor%, %version_maintenance%, %version_revision%">>"%srcdir%\appversion.h" -) +echo #define APP_VERSION "%new_version%">>"%srcdir%\appversion.h" echo.>>"%srcdir%\appversion.h" -echo #define APP_VERSION_DATE %version_date%>>"%srcdir%\appversion.h" -echo #define APP_VERSION_DATE_STR "%version_date%">>"%srcdir%\appversion.h" -echo.>>"%srcdir%\appversion.h" -echo #define APP_VERSION_PDATE_STR "%version_pdate%">>"%srcdir%\appversion.h" -echo.>>"%srcdir%\appversion.h" -echo #define APP_VERSION_YMD_STR "%version_pdate_1%">>"%srcdir%\appversion.h" -echo.>>"%srcdir%\appversion.h" +echo #define APP_COMMIT_DATE "%month% %DD% %YYYY%">>"%srcdir%\appversion.h" +echo #define APP_COMMIT_TIME "%hour%:%min%:%sec%">>"%srcdir%\appversion.h" -IF NOT "%version_specialbuild%" == "" ( - echo #define APP_VERSION_FLAGS VS_FF_SPECIALBUILD>>"%srcdir%\appversion.h" - echo #define APP_VERSION_SPECIALBUILD "%version_specialbuild%">>"%srcdir%\appversion.h" - echo #define APP_VERSION APP_VERSION_STRD "" APP_VERSION_SPECIALBUILD "" VERSION_POSTFIX>>"%srcdir%\appversion.h" -) ELSE ( - echo #define APP_VERSION_FLAGS 0x0L>>"%srcdir%\appversion.h" - echo #define APP_VERSION APP_VERSION_STRD "" VERSION_POSTFIX>>"%srcdir%\appversion.h" -) +echo.>>"%srcdir%\appversion.h" +echo #define APP_COMMIT_SHA "%commitSHA%">>"%srcdir%\appversion.h" +echo #define APP_COMMIT_URL "%commitURL%">>"%srcdir%\appversion.h" echo.>>"%srcdir%\appversion.h" echo #endif //__APPVERSION_H__>>"%srcdir%\appversion.h" @@ -200,6 +206,7 @@ echo.>>"%srcdir%\appversion.h" :: Do update of version.cpp file last modify time to force it recompile :: copy /b "%srcdir%\version.cpp"+,, "%srcdir%\version.cpp" +endlocal :_exit -exit /B 0 \ No newline at end of file +exit /B 0 diff --git a/rehlds/msvc/ReHLDS.vcxproj b/rehlds/msvc/ReHLDS.vcxproj index 8c79de5..6abe453 100644 --- a/rehlds/msvc/ReHLDS.vcxproj +++ b/rehlds/msvc/ReHLDS.vcxproj @@ -720,7 +720,7 @@ IF EXIST "$(ProjectDir)PreBuild.bat" (CALL "$(ProjectDir)PreBuild.bat" "$(ProjectDir)..\version\" "$(ProjectDir)..\") - Setup version from SVN revision + Setup version from Git revision $(ProjectDir)\..\;$(ProjectDir)\..\hookers\;$(ProjectDir)\..\metamod\include\;$(ProjectDir)\..\public\rehlds\;$(ProjectDir)\..\common;$(ProjectDir)\..\engine;$(ProjectDir)\..\public;$(ProjectDir)\..\pm_shared;$(ProjectDir)\..\rehlds\;$(ProjectDir)\..\testsuite\;$(VCInstallDir)UnitTest\include;$(SolutionDir)..\dep\bzip2\include\;$(SolutionDir)..\dep\cppunitlite\include\;%(AdditionalIncludeDirectories) @@ -758,7 +758,7 @@ IF EXIST "$(ProjectDir)PreBuild.bat" (CALL "$(ProjectDir)PreBuild.bat" "$(ProjectDir)..\version\" "$(ProjectDir)..\") - Setup version from SVN revision + Setup version from Git revision $(ProjectDir)\..\;$(ProjectDir)\..\hookers\;$(ProjectDir)\..\metamod\include\;$(ProjectDir)\..\public\rehlds\;$(ProjectDir)\..\common;$(ProjectDir)\..\engine;$(ProjectDir)\..\public;$(ProjectDir)\..\pm_shared;$(ProjectDir)\..\rehlds\;$(ProjectDir)\..\testsuite\;$(VCInstallDir)UnitTest\include;$(SolutionDir)..\dep\bzip2\include\;$(SolutionDir)..\dep\cppunitlite\include\;%(AdditionalIncludeDirectories) @@ -795,7 +795,7 @@ IF EXIST "$(ProjectDir)PreBuild.bat" (CALL "$(ProjectDir)PreBuild.bat" "$(ProjectDir)..\version\" "$(ProjectDir)..\") - Setup version from SVN revision + Setup version from Git revision $(ProjectDir)\..\;$(ProjectDir)\..\hookers\;$(ProjectDir)\..\metamod\include\;$(ProjectDir)\..\public\rehlds\;$(ProjectDir)\..\common;$(ProjectDir)\..\engine;$(ProjectDir)\..\public;$(ProjectDir)\..\pm_shared;$(ProjectDir)\..\rehlds\;$(ProjectDir)\..\testsuite\;$(VCInstallDir)UnitTest\include;$(SolutionDir)..\dep\bzip2\include\;$(SolutionDir)..\dep\cppunitlite\include\;%(AdditionalIncludeDirectories) @@ -831,7 +831,7 @@ IF EXIST "$(ProjectDir)PreBuild.bat" (CALL "$(ProjectDir)PreBuild.bat" "$(ProjectDir)..\version\" "$(ProjectDir)..\") - Setup version from SVN revision + Setup version from Git revision $(ProjectDir)\..\;$(ProjectDir)\..\hookers\;$(ProjectDir)\..\metamod\include\;$(ProjectDir)\..\public\rehlds\;$(ProjectDir)\..\common;$(ProjectDir)\..\engine;$(ProjectDir)\..\public;$(ProjectDir)\..\pm_shared;$(ProjectDir)\..\rehlds\;$(ProjectDir)\..\testsuite\;$(VCInstallDir)UnitTest\include;$(SolutionDir)..\dep\bzip2\include\;$(SolutionDir)..\dep\cppunitlite\include\;%(AdditionalIncludeDirectories) @@ -870,7 +870,7 @@ IF EXIST "$(ProjectDir)PreBuild.bat" (CALL "$(ProjectDir)PreBuild.bat" "$(ProjectDir)..\version\" "$(ProjectDir)..\") - Setup version from SVN revision + Setup version from Git revision $(ProjectDir)\..\;$(ProjectDir)\..\hookers\;$(ProjectDir)\..\metamod\include\;$(ProjectDir)\..\public\rehlds\;$(ProjectDir)\..\common;$(ProjectDir)\..\engine;$(ProjectDir)\..\public;$(ProjectDir)\..\pm_shared;$(ProjectDir)\..\rehlds\;$(ProjectDir)\..\testsuite\;$(VCInstallDir)UnitTest\include;$(SolutionDir)..\dep\bzip2\include\;$(SolutionDir)..\dep\cppunitlite\include\;%(AdditionalIncludeDirectories) @@ -906,7 +906,7 @@ IF EXIST "$(ProjectDir)PreBuild.bat" (CALL "$(ProjectDir)PreBuild.bat" "$(ProjectDir)..\version\" "$(ProjectDir)..\") - Setup version from SVN revision + Setup version from Git revision $(ProjectDir)\..\;$(ProjectDir)\..\hookers\;$(ProjectDir)\..\metamod\include\;$(ProjectDir)\..\public\rehlds\;$(ProjectDir)\..\common;$(ProjectDir)\..\engine;$(ProjectDir)\..\public;$(ProjectDir)\..\pm_shared;$(ProjectDir)\..\rehlds\;$(ProjectDir)\..\testsuite\;$(VCInstallDir)UnitTest\include;$(SolutionDir)..\dep\bzip2\include\;$(SolutionDir)..\dep\cppunitlite\include\;%(AdditionalIncludeDirectories) @@ -942,7 +942,7 @@ IF EXIST "$(ProjectDir)PreBuild.bat" (CALL "$(ProjectDir)PreBuild.bat" "$(ProjectDir)..\version\" "$(ProjectDir)..\") - Setup version from SVN revision + Setup version from Git revision $(ProjectDir)\..\;$(ProjectDir)\..\hookers\;$(ProjectDir)\..\metamod\include\;$(ProjectDir)\..\public\rehlds\;$(ProjectDir)\..\common;$(ProjectDir)\..\engine;$(ProjectDir)\..\public;$(ProjectDir)\..\pm_shared;$(ProjectDir)\..\rehlds\;$(ProjectDir)\..\testsuite\;$(VCInstallDir)UnitTest\include;$(SolutionDir)..\dep\bzip2\include\;$(SolutionDir)..\dep\cppunitlite\include\;%(AdditionalIncludeDirectories) @@ -977,7 +977,7 @@ IF EXIST "$(ProjectDir)PreBuild.bat" (CALL "$(ProjectDir)PreBuild.bat" "$(ProjectDir)..\version\" "$(ProjectDir)..\") - Setup version from SVN revision + Setup version from Git revision $(ProjectDir)\..\;$(ProjectDir)\..\hookers\;$(ProjectDir)\..\metamod\include\;$(ProjectDir)\..\public\rehlds\;$(ProjectDir)\..\common;$(ProjectDir)\..\engine;$(ProjectDir)\..\public;$(ProjectDir)\..\pm_shared;$(ProjectDir)\..\rehlds\;$(ProjectDir)\..\testsuite\;$(VCInstallDir)UnitTest\include;$(SolutionDir)..\dep\bzip2\include\;$(SolutionDir)..\dep\cppunitlite\include\;%(AdditionalIncludeDirectories) @@ -1016,7 +1016,7 @@ IF EXIST "$(ProjectDir)PreBuild.bat" (CALL "$(ProjectDir)PreBuild.bat" "$(ProjectDir)..\version\" "$(ProjectDir)..\") - Setup version from SVN revision + Setup version from Git revision $(ProjectDir)\..\;$(ProjectDir)\..\hookers\;$(ProjectDir)\..\metamod\include\;$(ProjectDir)\..\public\rehlds\;$(ProjectDir)\..\common;$(ProjectDir)\..\engine;$(ProjectDir)\..\public;$(ProjectDir)\..\pm_shared;$(ProjectDir)\..\rehlds\;$(ProjectDir)\..\testsuite\;$(VCInstallDir)UnitTest\include;$(SolutionDir)..\dep\bzip2\include\;$(SolutionDir)..\dep\cppunitlite\include\;%(AdditionalIncludeDirectories) @@ -1056,7 +1056,7 @@ IF EXIST "$(ProjectDir)PreBuild.bat" (CALL "$(ProjectDir)PreBuild.bat" "$(ProjectDir)..\version\" "$(ProjectDir)..\") - Setup version from SVN revision + Setup version from Git revision $(ProjectDir)\..\;$(ProjectDir)\..\hookers\;$(ProjectDir)\..\metamod\include\;$(ProjectDir)\..\public\rehlds\;$(ProjectDir)\..\common;$(ProjectDir)\..\engine;$(ProjectDir)\..\public;$(ProjectDir)\..\pm_shared;$(ProjectDir)\..\rehlds\;$(ProjectDir)\..\testsuite\;$(VCInstallDir)UnitTest\include;$(SolutionDir)..\dep\bzip2\include\;$(SolutionDir)..\dep\cppunitlite\include\;%(AdditionalIncludeDirectories) diff --git a/rehlds/version/appversion.vm b/rehlds/version/appversion.vm index 37e63ae..6fe6c2e 100644 --- a/rehlds/version/appversion.vm +++ b/rehlds/version/appversion.vm @@ -7,14 +7,12 @@ // // Version defines -\#define VERSION_MAJOR ${verInfo.majorVersion} -\#define VERSION_MINOR ${verInfo.minorVersion} +\#define APP_VERSION "$verInfo.asMavenVersion()" -\#define APP_VERSION_D ${verInfo.format('.', '-', true)} -\#define APP_VERSION_STRD "${verInfo.format('.', '-', true)}" +\#define APP_COMMIT_DATE "$verInfo.asCommitDate()" +\#define APP_COMMIT_TIME "$verInfo.asCommitTime()" -#set ( $commitYMD = $_DateTimeFormat.forPattern('yyyy-MM-dd').print($verInfo.lastCommitDate) ) - -\#define APP_VERSION_YMD_STR "${commitYMD}" +\#define APP_COMMIT_SHA "$verInfo.commitSHA" +\#define APP_COMMIT_URL "$verInfo.commitURL" #endif //__APPVERSION_H__