From 149bc768b7583fb96de25f90d8e6fb8caf7e4907 Mon Sep 17 00:00:00 2001
From: David Anderson <dvander@alliedmods.net>
Date: Sun, 25 May 2014 02:16:03 -0700
Subject: [PATCH 1/4] Port build process to Git.

---
 AMBuildScript                        | 30 ++++++++-----------
 core-legacy/AMBuilder                |  4 +--
 core/AMBuilder                       |  2 +-
 support/buildbot/Versioning          | 11 +++++--
 support/buildbot/generate_headers.py | 44 ++++++++++++++++++++--------
 5 files changed, 56 insertions(+), 35 deletions(-)

diff --git a/AMBuildScript b/AMBuildScript
index 02281fc..69cc5b3 100644
--- a/AMBuildScript
+++ b/AMBuildScript
@@ -3,16 +3,12 @@ import os, sys
 
 class SDK(object):
   def __init__(self, sdk, ext, aDef, name, platform, dir):
-    if dir == 'ep1':
-      folder = 'hl2sdk'
-    else:
-      folder = 'hl2sdk-' + dir
+    self.folder = 'hl2sdk-' + dir
     self.envvar = sdk
     self.ext = ext
     self.code = aDef
     self.define = name
     self.platform = platform
-    self.folder = folder # Default folder name.
     self.name = dir
     self.path = None # Actual path
 
@@ -21,15 +17,15 @@ WinLinux = ['windows', 'linux']
 WinLinuxMac = ['windows', 'linux', 'mac']
 
 PossibleSDKs = {
-  'ep1':  SDK('HL2SDK', '1.ep1', '1', 'EPISODEONE', WinLinux, 'ep1'),
-  'ep2':  SDK('HL2SDKOB', '2.ep2', '3', 'ORANGEBOX', WinLinux, 'ob'),
+  'episode1':  SDK('HL2SDK', '1.ep1', '1', 'EPISODEONE', WinLinux, 'episode1'),
+  'ep2':  SDK('HL2SDKOB', '2.ep2', '3', 'ORANGEBOX', WinLinux, 'orangebox'),
   'css':  SDK('HL2SDKCSS', '2.css', '6', 'CSS', WinLinuxMac, 'css'),
   'hl2dm':  SDK('HL2SDKHL2DM', '2.hl2dm', '7', 'HL2DM', WinLinuxMac, 'hl2dm'),
   'dods': SDK('HL2SDKDODS', '2.dods', '8', 'DODS', WinLinuxMac, 'dods'),
-  'sdk2013': SDK('HL2SDK2013', '2.sdk2013', '9', 'SDK2013', WinLinuxMac, '2013'),
+  'sdk2013': SDK('HL2SDK2013', '2.sdk2013', '9', 'SDK2013', WinLinuxMac, 'sdk2013'),
   'tf2':  SDK('HL2SDKTF2', '2.tf2', '10', 'TF2', WinLinuxMac, 'tf2'),
   'l4d':  SDK('HL2SDKL4D', '2.l4d', '11', 'LEFT4DEAD', WinLinuxMac, 'l4d'),
-  'nd': SDK('HL2SDKND', '2.nd', '12', 'NUCLEARDAWN', WinLinuxMac, 'nd'),
+  'nucleardawn': SDK('HL2SDKND', '2.nd', '12', 'NUCLEARDAWN', WinLinuxMac, 'nucleardawn'),
   'l4d2': SDK('HL2SDKL4D2', '2.l4d2', '14', 'LEFT4DEAD2', WinLinuxMac, 'l4d2'),
   'darkm':  SDK('HL2SDK-DARKM', '2.darkm', '2', 'DARKMESSIAH', WinOnly, 'darkm'),
   'swarm':  SDK('HL2SDK-SWARM', '2.swarm', '15', 'ALIENSWARM', WinOnly, 'swarm'),
@@ -237,7 +233,7 @@ class MMSConfig(object):
              ['public', 'mathlib'],
              ['public', 'vstdlib'],
              ['public', 'tier0'], ['public', 'tier1']]
-    if sdk.name == 'ep1' or sdk.name == 'darkm':
+    if sdk.name == 'episode1' or sdk.name == 'darkm':
       paths.append(['public', 'dlls'])
       paths.append(['game_shared'])
     else:
@@ -247,7 +243,7 @@ class MMSConfig(object):
 
     compiler.defines += ['SOURCE_ENGINE=' + sdk.code]
 
-    if sdk.name == '2013' and compiler.cxx.behavior == 'gcc':
+    if sdk.name == 'sdk2013' and compiler.cxx.behavior == 'gcc':
       # The 2013 SDK already has these in public/tier0/basetypes.h
       compiler.defines.remove('stricmp=strcasecmp')
       compiler.defines.remove('_stricmp=strcasecmp')
@@ -259,7 +255,7 @@ class MMSConfig(object):
     else:
       compiler.defines += ['COMPILER_GCC']
 
-    if sdk.name in ['css', 'hl2dm', 'dods', '2013', 'tf2', 'l4d', 'nd', 'l4d2', 'dota']:
+    if sdk.name in ['css', 'hl2dm', 'dods', 'sdk2013', 'tf2', 'l4d', 'nucleardawn', 'l4d2', 'dota']:
       if builder.target_platform in ['linux', 'mac']:
         compiler.defines += ['NO_HOOK_MALLOC', 'NO_MALLOC_OVERRIDE']
 
@@ -293,20 +289,20 @@ class MMSConfig(object):
     compiler = self.HL2Compiler(context, sdk)
 
     if builder.target_platform == 'linux':
-      if sdk.name == 'ep1':
+      if sdk.name == 'episode1':
         lib_folder = os.path.join(sdk.path, 'linux_sdk')
-      elif sdk.name == '2013':
+      elif sdk.name == 'sdk2013':
         lib_folder = os.path.join(sdk.path, 'lib', 'public', 'linux32')
       else:
         lib_folder = os.path.join(sdk.path, 'lib', 'linux')
     elif builder.target_platform == 'mac':
-      if sdk.name == '2013':
+      if sdk.name == 'sdk2013':
         lib_folder = os.path.join(sdk.path, 'lib', 'public', 'osx32')
       else:
         lib_folder = os.path.join(sdk.path, 'lib', 'mac')
 
     if builder.target_platform in ['linux', 'mac']:
-      if sdk.name == '2013':
+      if sdk.name == 'sdk2013':
         compiler.postlink += [compiler.Dep(os.path.join(lib_folder, 'tier1.a'))]
       else:
         compiler.postlink += [compiler.Dep(os.path.join(lib_folder, 'tier1_i486.a'))]
@@ -319,7 +315,7 @@ class MMSConfig(object):
     dynamic_libs = []
     if builder.target_platform == 'linux':
       compiler.linkflags[0:0] = ['-lm']
-      if sdk.name in ['css', 'hl2dm', 'dods', 'tf2', '2013', 'nd', 'l4d2']:
+      if sdk.name in ['css', 'hl2dm', 'dods', 'tf2', 'sdk2013', 'nucleardawn', 'l4d2']:
         dynamic_libs = ['libtier0_srv.so', 'libvstdlib_srv.so']
       elif sdk.name in ['l4d', 'blade', 'insurgency', 'csgo', 'dota']:
         dynamic_libs = ['libtier0.so', 'libvstdlib.so']
diff --git a/core-legacy/AMBuilder b/core-legacy/AMBuilder
index 3a2fcdc..6e55614 100644
--- a/core-legacy/AMBuilder
+++ b/core-legacy/AMBuilder
@@ -1,8 +1,8 @@
 # vim: set sts=2 ts=8 sw=2 tw=99 et ft=python: 
 import os
 
-if 'ep1' in MMS.sdks:
-  sdk = MMS.sdks['ep1']
+if 'episode1' in MMS.sdks:
+  sdk = MMS.sdks['episode1']
   name = 'metamod.' + sdk.ext
   binary = MMS.HL2Library(builder, name, sdk)
   binary.sources += [
diff --git a/core/AMBuilder b/core/AMBuilder
index 454f7f4..5b15887 100644
--- a/core/AMBuilder
+++ b/core/AMBuilder
@@ -3,7 +3,7 @@ import os
 
 for sdk_name in MMS.sdks:
   sdk = MMS.sdks[sdk_name]
-  if sdk.name == 'ep1':
+  if sdk.name == 'episode1':
     continue
 
   name = 'metamod.' + sdk.ext
diff --git a/support/buildbot/Versioning b/support/buildbot/Versioning
index 5d14a2c..2144f28 100644
--- a/support/buildbot/Versioning
+++ b/support/buildbot/Versioning
@@ -15,11 +15,18 @@ outputs = [
   os.path.join(builder.buildFolder, 'includes', 'metamod_version_auto.h')
 ]
 
+with open(os.path.join(builder.sourcePath, '.git', 'HEAD')) as fp:
+ git_state = fp.read().strip().split(':')[1].strip()
+
+git_head_path = os.path.join(builder.sourcePath, '.git', git_state)
+if not os.path.exists(git_head_path):
+  git_head_path = os.path.join(builder.sourcePath, '.git', 'HEAD')
+
 sources = [
   os.path.join(builder.sourcePath, 'product.version'),
 
-  # This is a hack, but we need some way to only run this script when HG changes.
-  os.path.join(builder.sourcePath, '.hg', 'dirstate'),
+  # This is a hack, but we need some way to only run this script when Git changes.
+  git_head_path,
 
   # The script source is a dependency, of course...
   argv[1]
diff --git a/support/buildbot/generate_headers.py b/support/buildbot/generate_headers.py
index ff73c8b..0c051a1 100644
--- a/support/buildbot/generate_headers.py
+++ b/support/buildbot/generate_headers.py
@@ -11,9 +11,19 @@ if len(argv) < 2:
 SourceFolder = os.path.abspath(os.path.normpath(argv[0]))
 OutputFolder = os.path.normpath(argv[1])
 
-def get_hg_version():
-  argv = ['hg', 'parent', '-R', SourceFolder]
+class FolderChanger:
+  def __init__(self, folder):
+    self.old = os.getcwd()
+    self.new = folder
 
+  def __enter__(self):
+    if self.new:
+      os.chdir(self.new)
+
+  def __exit__(self, type, value, traceback):
+    os.chdir(self.old)
+
+def run_and_return(argv):
   # Python 2.6 doesn't have check_output.
   if hasattr(subprocess, 'check_output'):
     text = subprocess.check_output(argv)
@@ -26,33 +36,41 @@ def get_hg_version():
     if rval:
       raise subprocess.CalledProcessError(rval, argv)
     text = output.decode('utf8')
+  return text.strip()
 
-  m = re.match('changeset:\s+(\d+):(.+)', text)
-  if m == None:
-    raise Exception('Could not determine repository version')
-  return m.groups()
+def get_git_version():
+  revision_count = run_and_return(['git', 'rev-list', '--count', 'HEAD'])
+  revision_hash = run_and_return(['git', 'log', '--pretty=format:%h:%H', '-n', '1'])
+  shorthash, longhash = revision_hash.split(':')
+
+  return revision_count, shorthash, longhash
 
 def output_version_header():
-  rev, cset = get_hg_version()
+  with FolderChanger(SourceFolder):
+    count, shorthash, longhash = get_git_version()
+
   with open(os.path.join(SourceFolder, 'product.version')) as fp:
-    productContents = fp.read()
-  m = re.match('(\d+)\.(\d+)\.(\d+)(.*)', productContents)
+    contents = fp.read()
+  m = re.match('(\d+)\.(\d+)\.(\d+)-?(.*)', contents)
   if m == None:
     raise Exception('Could not detremine product version')
   major, minor, release, tag = m.groups()
 
+  if tag:
+    tag = '-' + tag
+
   with open(os.path.join(OutputFolder, 'metamod_version_auto.h'), 'w') as fp:
     fp.write("""#ifndef _METAMOD_AUTO_VERSION_INFORMATION_H_
 #define _METAMOD_AUTO_VERSION_INFORMATION_H_
 
 #define MMS_BUILD_STRING     \"{0}\"
-#define MMS_BUILD_UNIQUEID    \"{1}:{2}\" MMS_BUILD_STRING
-#define MMS_FULL_VERSION    \"{3}.{4}.{5}\" MMS_BUILD_STRING
-#define MMS_FILE_VERSION    {6},{7},{8},0
+#define MMS_BUILD_UNIQUEID    \"{1}\" MMS_BUILD_STRING
+#define MMS_FULL_VERSION    \"{2}.{3}.{4}\" MMS_BUILD_STRING
+#define MMS_FILE_VERSION    {5},{6},{7},0
 
 #endif /* _METAMOD_AUTO_VERSION_INFORMATION_H_ */
 
-""".format(tag, rev, cset, major, minor, release, major, minor, release))
+""".format(tag, shorthash, major, minor, release, major, minor, release))
 
 output_version_header()
 

From 1817478b0e5800bc9fefa655d30068d82fcbc853 Mon Sep 17 00:00:00 2001
From: David Anderson <dvander@alliedmods.net>
Date: Sun, 25 May 2014 03:26:08 -0700
Subject: [PATCH 2/4] Add rev count back.

---
 core/metamod_console.cpp             | 3 ++-
 support/buildbot/generate_headers.py | 8 +++++---
 versionlib/versionlib.cpp            | 3 ++-
 versionlib/versionlib.h              | 3 ++-
 4 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/core/metamod_console.cpp b/core/metamod_console.cpp
index bdedd8f..047a3f8 100644
--- a/core/metamod_console.cpp
+++ b/core/metamod_console.cpp
@@ -70,7 +70,8 @@ bool Command_Meta(IMetamodSourceCommandInfo *info)
 		else if (strcmp(command, "version") == 0)
 		{
 			CONMSG("Metamod:Source version %s\n", METAMOD_VERSION);
-			CONMSG("Build ID: %s\n", METAMOD_BUILD_ID);
+			CONMSG("Built from: https://github.com/alliedmodders/metamod-source/commit/%s\n", METAMOD_BUILD_SHA);
+			CONMSG("Build ID: %s:%s\n", METAMOD_BUILD_LOCAL_REV, METAMOD_BUILD_SHA);
 
 			if (g_Metamod.IsLoadedAsGameDLL())
 			{
diff --git a/support/buildbot/generate_headers.py b/support/buildbot/generate_headers.py
index 0c051a1..0135d2c 100644
--- a/support/buildbot/generate_headers.py
+++ b/support/buildbot/generate_headers.py
@@ -64,13 +64,15 @@ def output_version_header():
 #define _METAMOD_AUTO_VERSION_INFORMATION_H_
 
 #define MMS_BUILD_STRING     \"{0}\"
-#define MMS_BUILD_UNIQUEID    \"{1}\" MMS_BUILD_STRING
+#define MMS_BUILD_LOCAL_REV   \"{5}\"
+#define MMS_BUILD_SHA         \"{1}\"
+#define MMS_BUILD_UNIQUEID    \"{5}:{1}\" MMS_BUILD_STRING
 #define MMS_FULL_VERSION    \"{2}.{3}.{4}\" MMS_BUILD_STRING
-#define MMS_FILE_VERSION    {5},{6},{7},0
+#define MMS_FILE_VERSION    {2},{3},{4},0
 
 #endif /* _METAMOD_AUTO_VERSION_INFORMATION_H_ */
 
-""".format(tag, shorthash, major, minor, release, major, minor, release))
+""".format(tag, shorthash, major, minor, release, count))
 
 output_version_header()
 
diff --git a/versionlib/versionlib.cpp b/versionlib/versionlib.cpp
index e4f3b57..a30505f 100644
--- a/versionlib/versionlib.cpp
+++ b/versionlib/versionlib.cpp
@@ -26,6 +26,7 @@
 #include <metamod_version.h>
 #include <versionlib.h>
 
-const char *METAMOD_BUILD_ID = MMS_BUILD_UNIQUEID;
+const char *METAMOD_BUILD_LOCAL_REV = MMS_BUILD_LOCAL_REV;
+const char *METAMOD_BUILD_SHA = MMS_BUILD_SHA;
 const char *METAMOD_VERSION = MMS_FULL_VERSION;
 
diff --git a/versionlib/versionlib.h b/versionlib/versionlib.h
index 0044416..47bb91f 100644
--- a/versionlib/versionlib.h
+++ b/versionlib/versionlib.h
@@ -25,7 +25,8 @@
 #ifndef _INCLUDE_METAMOD_VERSIONLIB_H_
 #define _INCLUDE_METAMOD_VERSIONLIB_H_
 
-extern "C" const char *METAMOD_BUILD_ID;
+extern "C" const char *METAMOD_BUILD_LOCAL_REV;
+extern "C" const char *METAMOD_BUILD_SHA;
 extern "C" const char *METAMOD_VERSION;
 
 #endif // _INCLUDE_METAMOD_VERSIONLIB_H_

From 66127f6d9831b8da883b67dc8b9ef7f561fe626f Mon Sep 17 00:00:00 2001
From: David Anderson <dvander@alliedmods.net>
Date: Sun, 25 May 2014 03:29:54 -0700
Subject: [PATCH 3/4] Fix build.

---
 core-legacy/concommands.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/core-legacy/concommands.cpp b/core-legacy/concommands.cpp
index 370610b..801a2c6 100644
--- a/core-legacy/concommands.cpp
+++ b/core-legacy/concommands.cpp
@@ -142,7 +142,8 @@ CON_COMMAND(meta, "Metamod:Source Menu")
 			return;
 		} else if (strcmp(command, "version") == 0) {
 			CONMSG("Metamod:Source version %s\n", METAMOD_VERSION);
-			CONMSG("Build ID: %s\n", METAMOD_BUILD_ID);
+			CONMSG("Built from: https://github.com/alliedmodders/metamod-source/commit/%s\n", METAMOD_BUILD_SHA);
+			CONMSG("Build ID: %s:%s\n", METAMOD_BUILD_LOCAL_REV, METAMOD_BUILD_SHA);
 			if (g_GameDll.loaded)
 			{
 				CONMSG("Loaded As: GameDLL (gameinfo.txt)\n");

From 5f1f405956cc6db34817318cd0b28f82dbe4e9b3 Mon Sep 17 00:00:00 2001
From: David Anderson <dvander@alliedmods.net>
Date: Sun, 25 May 2014 03:41:32 -0700
Subject: [PATCH 4/4] Fix.

---
 core-legacy/concommands.cpp | 2 ++
 core/metamod_console.cpp    | 2 ++
 2 files changed, 4 insertions(+)

diff --git a/core-legacy/concommands.cpp b/core-legacy/concommands.cpp
index 801a2c6..56fe107 100644
--- a/core-legacy/concommands.cpp
+++ b/core-legacy/concommands.cpp
@@ -142,7 +142,9 @@ CON_COMMAND(meta, "Metamod:Source Menu")
 			return;
 		} else if (strcmp(command, "version") == 0) {
 			CONMSG("Metamod:Source version %s\n", METAMOD_VERSION);
+#if defined(MMS_GENERATED_BUILD)
 			CONMSG("Built from: https://github.com/alliedmodders/metamod-source/commit/%s\n", METAMOD_BUILD_SHA);
+#endif
 			CONMSG("Build ID: %s:%s\n", METAMOD_BUILD_LOCAL_REV, METAMOD_BUILD_SHA);
 			if (g_GameDll.loaded)
 			{
diff --git a/core/metamod_console.cpp b/core/metamod_console.cpp
index 047a3f8..4f42e83 100644
--- a/core/metamod_console.cpp
+++ b/core/metamod_console.cpp
@@ -70,7 +70,9 @@ bool Command_Meta(IMetamodSourceCommandInfo *info)
 		else if (strcmp(command, "version") == 0)
 		{
 			CONMSG("Metamod:Source version %s\n", METAMOD_VERSION);
+#if defined(MMS_GENERATED_BUILD)
 			CONMSG("Built from: https://github.com/alliedmodders/metamod-source/commit/%s\n", METAMOD_BUILD_SHA);
+#endif
 			CONMSG("Build ID: %s:%s\n", METAMOD_BUILD_LOCAL_REV, METAMOD_BUILD_SHA);
 
 			if (g_Metamod.IsLoadedAsGameDLL())