gitstatus.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. # change those symbols to whatever you prefer
  4. symbols = {'ahead of': '↑', 'behind': '↓', 'staged':'♦', 'changed':'‣', 'untracked':'…', 'clean':'⚡', 'unmerged':'≠', 'sha1':':'}
  5. from subprocess import Popen, PIPE
  6. output,error = Popen(['git','status'], stdout=PIPE, stderr=PIPE).communicate()
  7. if error:
  8. import sys
  9. sys.exit(0)
  10. lines = output.splitlines()
  11. import re
  12. behead_re = re.compile(r"^# Your branch is (ahead of|behind) '(.*)' by (\d+) commit")
  13. diverge_re = re.compile(r"^# and have (\d+) and (\d+) different")
  14. status = ''
  15. staged = re.compile(r'^# Changes to be committed:$', re.MULTILINE)
  16. changed = re.compile(r'^# Changed but not updated:$', re.MULTILINE)
  17. untracked = re.compile(r'^# Untracked files:$', re.MULTILINE)
  18. unmerged = re.compile(r'^# Unmerged paths:$', re.MULTILINE)
  19. def execute(*command):
  20. out, err = Popen(stdout=PIPE, stderr=PIPE, *command).communicate()
  21. if not err:
  22. nb = len(out.splitlines())
  23. else:
  24. nb = '?'
  25. return nb
  26. if staged.search(output):
  27. nb = execute(['git','diff','--staged','--name-only','--diff-filter=ACDMRT'])
  28. status += '%s%s' % (symbols['staged'], nb)
  29. if unmerged.search(output):
  30. nb = execute(['git','diff', '--staged','--name-only', '--diff-filter=U'])
  31. status += '%s%s' % (symbols['unmerged'], nb)
  32. if changed.search(output):
  33. nb = execute(['git','diff','--name-only', '--diff-filter=ACDMRT'])
  34. status += '%s%s' % (symbols['changed'], nb)
  35. if untracked.search(output):
  36. ## nb = len(Popen(['git','ls-files','--others','--exclude-standard'],stdout=PIPE).communicate()[0].splitlines())
  37. ## status += "%s" % (symbols['untracked']*(nb//3 + 1), )
  38. status += symbols['untracked']
  39. if status == '':
  40. status = symbols['clean']
  41. remote = ''
  42. bline = lines[0]
  43. if bline.find('Not currently on any branch') != -1:
  44. branch = symbols['sha1']+ Popen(['git','rev-parse','--short','HEAD'], stdout=PIPE).communicate()[0][:-1]
  45. else:
  46. branch = bline.split(' ')[3]
  47. bstatusline = lines[1]
  48. match = behead_re.match(bstatusline)
  49. if match:
  50. remote = symbols[match.groups()[0]]
  51. remote += match.groups()[2]
  52. elif lines[2:]:
  53. div_match = diverge_re.match(lines[2])
  54. if div_match:
  55. remote = "{behind}{1}{ahead of}{0}".format(*div_match.groups(), **symbols)
  56. print '\n'.join([branch,remote,status])