From a381f75b4b6cdd0acd58b50353028e4b4a2836cb Mon Sep 17 00:00:00 2001
From: Peter Surda <surda@economicsofbitcoin.com>
Date: Tue, 7 Feb 2017 20:46:30 +0100
Subject: [PATCH] Git head information in version

- About dialog now shows the git head
- git head check has been improved to point to the head rather than the
  previous commit
---
 src/bitmessageqt/__init__.py |  3 ++-
 src/bitmessageqt/about.py    |  7 ++++---
 src/bitmessageqt/support.py  | 10 +++-------
 src/paths.py                 | 37 ++++++++++++++++++++++++++++++++++++
 4 files changed, 46 insertions(+), 11 deletions(-)

diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py
index e944033f..32f29eaf 100644
--- a/src/bitmessageqt/__init__.py
+++ b/src/bitmessageqt/__init__.py
@@ -4004,7 +4004,8 @@ class aboutDialog(QtGui.QDialog):
         self.ui = Ui_aboutDialog()
         self.ui.setupUi(self)
         self.parent = parent
-        self.ui.labelVersion.setText('version ' + softwareVersion)
+        self.ui.label.setText("PyBitmessage " + softwareVersion)
+        self.ui.labelVersion.setText(paths.lastCommit())
 
 
 class regenerateAddressesDialog(QtGui.QDialog):
diff --git a/src/bitmessageqt/about.py b/src/bitmessageqt/about.py
index 3f4cc670..a3483675 100644
--- a/src/bitmessageqt/about.py
+++ b/src/bitmessageqt/about.py
@@ -34,16 +34,17 @@ class Ui_aboutDialog(object):
         self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Ok)
         self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
         self.label = QtGui.QLabel(aboutDialog)
-        self.label.setGeometry(QtCore.QRect(70, 126, 111, 20))
+        self.label.setGeometry(QtCore.QRect(10, 106, 341, 20))
         font = QtGui.QFont()
         font.setBold(True)
         font.setWeight(75)
         self.label.setFont(font)
-        self.label.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
+        self.label.setAlignment(QtCore.Qt.AlignCenter|QtCore.Qt.AlignVCenter)
         self.label.setObjectName(_fromUtf8("label"))
         self.labelVersion = QtGui.QLabel(aboutDialog)
-        self.labelVersion.setGeometry(QtCore.QRect(190, 126, 161, 20))
+        self.labelVersion.setGeometry(QtCore.QRect(10, 116, 341, 41))
         self.labelVersion.setObjectName(_fromUtf8("labelVersion"))
+        self.labelVersion.setAlignment(QtCore.Qt.AlignCenter|QtCore.Qt.AlignVCenter)
         self.label_2 = QtGui.QLabel(aboutDialog)
         self.label_2.setGeometry(QtCore.QRect(10, 150, 341, 41))
         self.label_2.setAlignment(QtCore.Qt.AlignCenter)
diff --git a/src/bitmessageqt/support.py b/src/bitmessageqt/support.py
index fc132453..ba750423 100644
--- a/src/bitmessageqt/support.py
+++ b/src/bitmessageqt/support.py
@@ -87,13 +87,9 @@ def createSupportMessage(myapp):
     myapp.ui.lineEditTo.setText(SUPPORT_ADDRESS)
     
     version = softwareVersion
-    githeadfile = path.join(paths.codePath(), '..', '.git', 'ORIG_HEAD')
-    if (path.isfile(githeadfile)):
-        try:
-            with open(githeadfile, 'rt') as githead:
-                version += " GIT " + githead.readline().rstrip()
-        except IOError:
-            pass
+    commit = paths.lastCommit()
+    if commit:
+        version += " GIT " + commit
 
     os = sys.platform
     if os == "win32":
diff --git a/src/paths.py b/src/paths.py
index e92116c0..0f843edf 100644
--- a/src/paths.py
+++ b/src/paths.py
@@ -68,3 +68,40 @@ def codePath():
         codePath = path.dirname(__file__)
     return codePath
 
+def tail(f, lines=20):
+    total_lines_wanted = lines
+
+    BLOCK_SIZE = 1024
+    f.seek(0, 2)
+    block_end_byte = f.tell()
+    lines_to_go = total_lines_wanted
+    block_number = -1
+    blocks = [] # blocks of size BLOCK_SIZE, in reverse order starting
+                # from the end of the file
+    while lines_to_go > 0 and block_end_byte > 0:
+        if (block_end_byte - BLOCK_SIZE > 0):
+            # read the last block we haven't yet read
+            f.seek(block_number*BLOCK_SIZE, 2)
+            blocks.append(f.read(BLOCK_SIZE))
+        else:
+            # file too small, start from begining
+            f.seek(0,0)
+            # only read what was not read
+            blocks.append(f.read(block_end_byte))
+        lines_found = blocks[-1].count('\n')
+        lines_to_go -= lines_found
+        block_end_byte -= BLOCK_SIZE
+        block_number -= 1
+    all_read_text = ''.join(reversed(blocks))
+    return '\n'.join(all_read_text.splitlines()[-total_lines_wanted:])
+
+def lastCommit():
+    githeadfile = path.join(codePath(), '..', '.git', 'logs', 'HEAD')
+    version = ""
+    if (path.isfile(githeadfile)):
+        try:
+            with open(githeadfile, 'rt') as githead:
+                version = tail(githead, 1).split()[1]
+        except IOError:
+            pass
+    return version