diff --git a/gitstatus.py b/gitstatus.py index d944fd4e..7db1cef2 100755 --- a/gitstatus.py +++ b/gitstatus.py @@ -17,20 +17,25 @@ branch = branch.decode("utf-8").strip()[11:] -res, err = Popen(['git','diff','--name-status'], stdout=PIPE, stderr=PIPE).communicate() +# Get git status (staged, change, conflicts and untracked) +res, err = Popen(['git', 'status', '--porcelain'], 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.decode("utf-8").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([0 for status in Popen(['git','status','--porcelain',],stdout=PIPE).communicate()[0].decode("utf-8").splitlines() if status.startswith('??')]) -untracked = str(nb_untracked) +status = [(st[0], st[1], st[2:]) for st in res.splitlines()] +tracked, untracked = [], [] +for st in status: + if st[0] == '?' and st[1] == '?': + untracked.append(st) + else: + tracked.append(st) +staged = filter(lambda x: x[0] != ' ' and x[0] != 'U', tracked) +changed = filter(lambda x: x[1] == 'M', tracked) +conflicts = filter(lambda x: x[0] == 'U', tracked) +n_staged = len(staged) +n_changed = len(changed) +n_conflicts = len(conflicts) +n_untracked = len(untracked) ahead, behind = 0,0 @@ -56,10 +61,10 @@ branch, str(ahead), str(behind), - staged, - conflicts, - changed, - untracked, + str(n_staged), + str(n_conflicts), + str(n_changed), + str(n_untracked), ]) print(out, end='')