Skip to content

Commit 0d5a3f5

Browse files
committed
[gitstatus.py] Use --porcelain to make faster in using python
1 parent 7d480a9 commit 0d5a3f5

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

gitstatus.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,26 @@
1717

1818
branch = branch.decode("utf-8").strip()[11:]
1919

20-
res, err = Popen(['git','diff','--name-status'], stdout=PIPE, stderr=PIPE).communicate()
20+
# Get git status (staged, change, conflicts and untracked)
21+
res, err = Popen(['git', 'status', '--porcelain'],
22+
stdout=PIPE, stderr=PIPE).communicate()
2123
err_string = err.decode('utf-8')
2224
if 'fatal' in err_string:
2325
sys.exit(0)
24-
changed_files = [namestat[0] for namestat in res.decode("utf-8").splitlines()]
25-
staged_files = [namestat[0] for namestat in Popen(['git','diff', '--staged','--name-status'], stdout=PIPE).communicate()[0].splitlines()]
26-
nb_changed = len(changed_files) - changed_files.count('U')
27-
nb_U = staged_files.count('U')
28-
nb_staged = len(staged_files) - nb_U
29-
staged = str(nb_staged)
30-
conflicts = str(nb_U)
31-
changed = str(nb_changed)
32-
nb_untracked = len([0 for status in Popen(['git','status','--porcelain',],stdout=PIPE).communicate()[0].decode("utf-8").splitlines() if status.startswith('??')])
33-
untracked = str(nb_untracked)
26+
status = [(st[0], st[1], st[2:]) for st in res.splitlines()]
27+
tracked, untracked = [], []
28+
for st in status:
29+
if st[0] == '?' and st[1] == '?':
30+
untracked.append(st)
31+
else:
32+
tracked.append(st)
33+
staged = filter(lambda x: x[0] != ' ' and x[0] != 'U', tracked)
34+
changed = filter(lambda x: x[1] == 'M', tracked)
35+
conflicts = filter(lambda x: x[0] == 'U', tracked)
36+
n_staged = len(staged)
37+
n_changed = len(changed)
38+
n_conflicts = len(conflicts)
39+
n_untracked = len(untracked)
3440

3541
ahead, behind = 0,0
3642

@@ -56,10 +62,10 @@
5662
branch,
5763
str(ahead),
5864
str(behind),
59-
staged,
60-
conflicts,
61-
changed,
62-
untracked,
65+
str(n_staged),
66+
str(n_conflicts),
67+
str(n_changed),
68+
str(n_untracked),
6369
])
6470
print(out, end='')
6571

0 commit comments

Comments
 (0)