Guest User

Untitled

a guest
Apr 23rd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1.  
  2. import subprocess
  3.  
  4. def git(cmd, *args):
  5. return subprocess.Popen(['git',cmd]+list(args), stdout=subprocess.PIPE).communicate()[0].decode()
  6.  
  7. def leaf(tree, pos):
  8. while True:
  9. if len(pos) == 0:
  10. return tree
  11. else:
  12. tree = tree[pos[0]]
  13. pos = pos[1:]
  14.  
  15. # Get a rev-list for each branch
  16.  
  17. branches = [x.strip() for x in git('branch').strip().split('\n')]
  18. for branch in branches:
  19. if branch.startswith('* '):
  20. head = branch.lstrip('* ')
  21. branches[branches.index('* '+head)] = head
  22.  
  23. # Build a tree (ignoring merges)
  24.  
  25. # in case there are branches with no common commits, start with a blank node
  26. tree = {'':{}}
  27.  
  28. for branch in branches:
  29. revlist = []
  30. for rev in reversed(git('rev-list','--format=%d',branch).split('commit ')):
  31. if rev == '':
  32. continue
  33. lst = rev.split('\n')
  34. r = lst[0][0:5]
  35. if len(lst) > 1 and len(lst[1]) > 0:
  36. r += ' ' + lst[1].strip()
  37. revlist.append(r)
  38. pos = ['']
  39. for rev in revlist:
  40. leaf(tree, pos)[rev] = {}
  41. pos.append(rev)
  42.  
  43. # Pretty print
  44.  
  45. tosee = []
  46. pos = ['']
  47. while True:
  48. subtree = leaf(tree, pos)
  49. for child in subtree.keys():
  50. tosee.append(pos+[child])
  51. print(('*'*len(pos)) + ' ' + pos[-1])
  52. if len(tosee) == 0:
  53. break
  54. pos = tosee.pop(0)
Add Comment
Please, Sign In to add comment