#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from __future__ import print_function

# change those symbols to whatever you prefer
symbols = {'ahead of': '↑·', 'behind': '↓·', 'prehash':':'}

from subprocess import Popen, PIPE

import sys
gitsym = Popen(['git', 'symbolic-ref', 'HEAD'], stdout=PIPE, stderr=PIPE)
branch, error = gitsym.communicate()

error_string = error.decode('utf-8')

if 'fatal: Not a git repository' in error_string:
	sys.exit(0)

branch = branch.decode('utf-8').strip()[11:]

res, err = Popen(['git','diff','--name-status'], stdout=PIPE, stderr=PIPE).communicate()
err_string = err.decode('utf-8')
if 'fatal' in err_string:
	sys.exit(0)
changed_files = [namestat[0] for namestat in res.splitlines()]
staged_files = [namestat[0] for namestat in Popen(['git','diff', '--staged','--name-status'], stdout=PIPE).communicate()[0].splitlines()]
nb_changed = len(changed_files) - changed_files.count('U')
nb_U = staged_files.count('U')
nb_staged = len(staged_files) - nb_U
staged = str(nb_staged)
conflicts = str(nb_U)
changed = str(nb_changed)
nb_untracked = len(Popen(['git','ls-files','--others','--exclude-standard'],stdout=PIPE).communicate()[0].splitlines())
untracked = str(nb_untracked)
if not nb_changed and not nb_staged and not nb_U and not nb_untracked:
	clean = '1'
else:
	clean = '0'

remote = ''

tag, tag_error = Popen(['git', 'describe', '--exact-match'], stdout=PIPE, stderr=PIPE).communicate()

if not branch: # not on any branch
	if tag: # if we are on a tag, print the tag's name
		branch = tag
	else:
		branch = symbols['prehash']+ Popen(['git','rev-parse','--short','HEAD'], stdout=PIPE).communicate()[0][:-1]
else:
	remote_name = Popen(['git','config','branch.%s.remote' % branch], stdout=PIPE).communicate()[0].strip()
	if remote_name:
		merge_name = Popen(['git','config','branch.%s.merge' % branch], stdout=PIPE).communicate()[0].strip()
	else:
		remote_name = "origin"
		merge_name = "refs/heads/%s" % branch

	if remote_name == '.': # local
		remote_ref = merge_name
	else:
		remote_ref = 'refs/remotes/%s/%s' % (remote_name, merge_name[11:])
	revgit = Popen(['git', 'rev-list', '--left-right', '%s...HEAD' % remote_ref],stdout=PIPE, stderr=PIPE)
	revlist = revgit.communicate()[0]
	if revgit.poll(): # fallback to local
		revlist = Popen(['git', 'rev-list', '--left-right', '%s...HEAD' % merge_name],stdout=PIPE, stderr=PIPE).communicate()[0]
	behead = revlist.splitlines()
	ahead = len([x for x in behead if x[0]=='>'])
	behind = len(behead) - ahead
	if behind:
		remote += '%s%s' % (symbols['behind'], behind)
	if ahead:
		remote += '%s%s' % (symbols['ahead of'], ahead)

if remote == "":
	remote = '.'

out = '\n'.join([
	str(branch),
	str(remote),
	staged,
	conflicts,
	changed,
	untracked,
	clean])
print(out)

@@ -44,46 +44,972 @@
 *-%0A%0A
-from __future__ import print_function%0A
+%22%22%22This module defines a Print function to use with python 2.x or 3.x., so we can use the prompt with older versions of Python too%0A%0AIt's interface is that of python 3.0's print. See%0Ahttp://docs.python.org/3.0/library/functions.html?highlight=print#print%0A%0AShamelessly ripped from http://www.daniweb.com/software-development/python/code/217214/a-print-function-for-different-versions-of-python%0A%22%22%22%0A%0A__all__ = %5B%22Print%22%5D%0Aimport sys%0Atry:%0A  Print = eval(%22print%22) # python 3.0 case%0Aexcept SyntaxError:%0A  try:%0A    D = dict()%0A    exec(%22from __future__ import print_function%5Cnp=print%22, D)%0A    Print = D%5B%22p%22%5D # 2.6 case%0A    del D%0A  except SyntaxError:%0A    del D%0A    def Print(*args, **kwd): # 2.4, 2.5, define our own Print function%0A      fout = kwd.get(%22file%22, sys.stdout)%0A      w = fout.write%0A      if args:%0A        w(str(args%5B0%5D))%0A        sep = kwd.get(%22sep%22, %22 %22)%0A        for a in args%5B1:%5D:%0A          w(sep)%0A          w(str(a))%0A      w(kwd.get(%22end%22, %22%5Cn%22))%0A      %0A      
 %0A# c
@@ -3669,17 +3669,17 @@
 clean%5D)%0A
-p
+P
 rint(out

Update gitstatus.py