]> git.leopard-lacewing.eu Git - tex_tools.git/commitdiff
new Hashing, resolve Parents works
authorPeter Schaefer <schaeferpm@gmail.com>
Fri, 15 Feb 2019 10:47:24 +0000 (11:47 +0100)
committerPeter Schaefer <schaeferpm@gmail.com>
Fri, 15 Feb 2019 10:47:24 +0000 (11:47 +0100)
gitLog2tikz.py

index 8b017e045db066920289beae1c8a1cb12a6f2bad..66542699ad868d45212797f343580d8fb1b0dfe6 100644 (file)
@@ -5,8 +5,6 @@ from re import compile, match
 # Importer fast fertig
 # Aber die light Variante
 
-hashpat7 = compile(r"[a-f0-9]{7}")
-hashpat32 = compile(r"[a-f0-9]{32}")
 linepat = compile(r'[^|\/\\]')
 
 
@@ -14,11 +12,34 @@ def color(numb):
     colors = ["black", "blue", "brown", "cyan", "darkgray", "gray", "green", "lightgray", "lime", "magenta", "olive", "orange", "pink", "purple", "red", "teal", "violet", "white", "yellow"]
     return colors[numb % colors.__len__()]
 
+class Hash:
+    hashpat7 = compile(r"[a-f0-9]{7}")
+    hashpat40 = compile(r"[a-f0-9]{40}")
+    def __init__(self,hash):
+        if Hash.hashpat7.match(hash) or hashpat40.match(hash):
+            self._hash: str = hash
+        else:
+            raise ValueError('Wrong Hash Format')
+
+    def hash(self):
+        return self._hash[0:7]
+    
+    def hashLong(self):
+        return self._hash
+
+    def __str__(self):
+        return self._hash[0:7]
+
+    def __len__(self):
+        return len(self._hash)
+
+    def __eq__(self,other):
+        return self.hash() == other.hash()
 
 class Commit:
     def __init__(self,line):
         self._line = None
-        self._hash = None
+        self._hash: Hash = None
         self._children = {}
         self._parents = {}
         self._message= None
@@ -46,7 +67,6 @@ class Commit:
         if self._hash:
             self._message = self._message.replace("!","")
 
-
     def __init__(self,hashes,node_pos,message_pos):
         self._children = {}
         self._parents = {}
@@ -54,11 +74,14 @@ class Commit:
         self._node_color = None
         self._author = None
         self._date = None
-        self._hash = hashes[0]
+        self._hash = Hash(hashes[0])
         self._node_pos = node_pos
         self._message_pos = message_pos
         for parent in hashes[1:]:
-            self._parents[parent] = NotImplemented
+            self._parents[Hash(parent).hash()] = NotImplemented
+    
+    def hash(self):
+        return self._hash.hash()
 
     def AddInfo(self,line):
         line = line[self._message_pos:]
@@ -76,15 +99,17 @@ class Commit:
         return
 
     def update_parent (self, child):
-        self._parents[child._hash] = child
-        child._children[self._hash] = self
+        self._parents[child.hash()] = child
+        child._children[self.hash()] = self
 
     def export_to_tikz(self, ypos):
         print("\\node[commit, " + color(self._node_pos) + ", fill=" +  color(self._node_pos) + "] (" + self._hash + ") at (" + str(.5 * self._node_pos) + "," + str(ypos) + ") {};")
         print("\\node[right,xshift=10] (label_" + self._hash + ") at (" + self._hash + ".east) {\\verb!" + self._hash + ": " + self._message + "!};")
         #for child in self._children.itervalues():
         #    print("\\path[" + "blue" +"] (" + self._hash + ") to[out=90,in=-90] (" + child._hash + ");") 
-
+    
+    def __eq__(self,other):
+        return self.hash() == other.hash()
 
 class Branch:
     def __init__(self, line):
@@ -110,12 +135,15 @@ class Repo:
         self._branches = []
 
     def add_commit(self, commit):
-        if commit._hash:
-            self._commits[commit._hash] = commit
+        if commit.hash():
+            self._commits[commit.hash()] = commit
+        else:
+            raise ValueError('Commit has no hash')
 
     def resolve_parents(self):
         for commit in self._commits.values():
             for key_p in commit._parents.keys():
+
                 commit.update_parent(self._commits[key_p])
 
     def add_branch(self, branch):
@@ -142,12 +170,17 @@ class Repo:
             print("\\node[branch,right,xshift=10] (" + branch._name + ") at (label_" + branch._hash + ".east) {\\lstinline{" + branch._name +"}};")
         print("\\end{tikzpicture}")
 
+
+#new Repo
+r =  Repo()
+
 # extract Commits
 cmd = "git log --branches --graph --parents"
 
 return_output = check_output(cmd, shell=True, encoding='utf-8')
 #print(return_output.split("\n"))
 
+comm = None
 
 for line in return_output.split("\n"):
     if not line == "":
@@ -170,10 +203,12 @@ for line in return_output.split("\n"):
             com = line[message_pos:].split(' ')
             if com[0] == 'commit':
                 comm = Commit(com[1:],node_pos,message_pos)
+                r.add_commit(comm)
             else:
-                print("something went wrong at:" + line)
+                raise ValueError('looks like a new commit but isn\'t: ' + line)
         else: #still old commit
             comm.AddInfo(line)
         
+r.resolve_parents()
 
 print(node_pos + message_pos)