5642014ff1b718df04d803b7b0457b9872b2b4f2

Author
Marc Cornellà <marc.cornella@live.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

Pull in simplified version from @wkentaro

This version uses `git status --porcelain` instead of making
multiple calls to `git status`.

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/git-prompt/gitstatus.py b/plugins/git-prompt/gitstatus.py
  2index d944fd4edb7cc018b40c152788266c5f77c66176..1065b3cb5a851bf23dd19027921f9f11a64fc456 100644
  3--- a/plugins/git-prompt/gitstatus.py
  4+++ b/plugins/git-prompt/gitstatus.py
  5@@ -4,6 +4,7 @@ from __future__ import print_function
  6 # change this symbol to whatever you prefer
  7 prehash = ':'
  8 
  9+import subprocess
 10 from subprocess import Popen, PIPE
 11 
 12 import sys
 13@@ -13,53 +14,56 @@ branch, error = gitsym.communicate()
 14 error_string = error.decode('utf-8')
 15 
 16 if 'fatal: Not a git repository' in error_string:
 17-	sys.exit(0)
 18+    sys.exit(0)
 19 
 20 branch = branch.decode("utf-8").strip()[11:]
 21 
 22-res, err = Popen(['git','diff','--name-status'], stdout=PIPE, stderr=PIPE).communicate()
 23-err_string = err.decode('utf-8')
 24-if 'fatal' in err_string:
 25-	sys.exit(0)
 26-changed_files = [namestat[0] for namestat in res.decode("utf-8").splitlines()]
 27-staged_files = [namestat[0] for namestat in Popen(['git','diff', '--staged','--name-status'], stdout=PIPE).communicate()[0].splitlines()]
 28-nb_changed = len(changed_files) - changed_files.count('U')
 29-nb_U = staged_files.count('U')
 30-nb_staged = len(staged_files) - nb_U
 31-staged = str(nb_staged)
 32-conflicts = str(nb_U)
 33-changed = str(nb_changed)
 34-nb_untracked = len([0 for status in Popen(['git','status','--porcelain',],stdout=PIPE).communicate()[0].decode("utf-8").splitlines() if status.startswith('??')])
 35-untracked = str(nb_untracked)
 36+# Get git status (staged, change, conflicts and untracked)
 37+try:
 38+    res = subprocess.check_output(['git', 'status', '--porcelain'])
 39+except subprocess.CalledProcessError:
 40+    sys.exit(0)
 41+status = [(st[0], st[1], st[2:]) for st in res.splitlines()]
 42+untracked, staged, changed, conflicts = [], [], [], []
 43+for st in status:
 44+    if st[0] == '?' and st[1] == '?':
 45+        untracked.append(st)
 46+    else:
 47+        if st[1] == 'M':
 48+            changed.append(st)
 49+        if st[0] == 'U':
 50+            conflicts.append(st)
 51+        elif st[0] != ' ':
 52+            staged.append(st)
 53 
 54 ahead, behind = 0,0
 55 
 56 if not branch: # not on any branch
 57-	branch = prehash + Popen(['git','rev-parse','--short','HEAD'], stdout=PIPE).communicate()[0].decode("utf-8")[:-1]
 58+    branch = prehash + Popen(['git','rev-parse','--short','HEAD'], stdout=PIPE).communicate()[0].decode("utf-8")[:-1]
 59 else:
 60-	remote_name = Popen(['git','config','branch.%s.remote' % branch], stdout=PIPE).communicate()[0].decode("utf-8").strip()
 61-	if remote_name:
 62-		merge_name = Popen(['git','config','branch.%s.merge' % branch], stdout=PIPE).communicate()[0].decode("utf-8").strip()
 63-		if remote_name == '.': # local
 64-			remote_ref = merge_name
 65-		else:
 66-			remote_ref = 'refs/remotes/%s/%s' % (remote_name, merge_name[11:])
 67-		revgit = Popen(['git', 'rev-list', '--left-right', '%s...HEAD' % remote_ref],stdout=PIPE, stderr=PIPE)
 68-		revlist = revgit.communicate()[0]
 69-		if revgit.poll(): # fallback to local
 70-			revlist = Popen(['git', 'rev-list', '--left-right', '%s...HEAD' % merge_name],stdout=PIPE, stderr=PIPE).communicate()[0]
 71-		behead = revlist.decode("utf-8").splitlines()
 72-		ahead = len([x for x in behead if x[0]=='>'])
 73-		behind = len(behead) - ahead
 74+    remote_name = Popen(['git','config','branch.%s.remote' % branch], stdout=PIPE).communicate()[0].decode("utf-8").strip()
 75+    if remote_name:
 76+        merge_name = Popen(['git','config','branch.%s.merge' % branch], stdout=PIPE).communicate()[0].decode("utf-8").strip()
 77+        if remote_name == '.': # local
 78+            remote_ref = merge_name
 79+        else:
 80+            remote_ref = 'refs/remotes/%s/%s' % (remote_name, merge_name[11:])
 81+        revgit = Popen(['git', 'rev-list', '--left-right', '%s...HEAD' % remote_ref],stdout=PIPE, stderr=PIPE)
 82+        revlist = revgit.communicate()[0]
 83+        if revgit.poll(): # fallback to local
 84+            revlist = Popen(['git', 'rev-list', '--left-right', '%s...HEAD' % merge_name],stdout=PIPE, stderr=PIPE).communicate()[0]
 85+        behead = revlist.decode("utf-8").splitlines()
 86+        ahead = len([x for x in behead if x[0]=='>'])
 87+        behind = len(behead) - ahead
 88 
 89 out = ' '.join([
 90-	branch,
 91-	str(ahead),
 92-	str(behind),
 93-	staged,
 94-	conflicts,
 95-	changed,
 96-	untracked,
 97-	])
 98+    branch,
 99+    str(ahead),
100+    str(behind),
101+    str(len(staged)),
102+    str(len(conflicts)),
103+    str(len(changed)),
104+    str(len(untracked)),