99import sys
1010
1111from socket import gethostname
12- from os import path
13- from git import Repo
12+ from os import path
1413
1514import subprocess
1615
@@ -89,25 +88,38 @@ def get_vcs_subdir(current_dir):
8988 return ""
9089
9190
92- def branch_name (repo_path , vcs_subdir ):
91+ def branch_info (repo_path , vcs_subdir ):
9392 vcs_obj = path .join (repo_path , vcs_subdir )
9493
9594 if vcs_subdir == ".git" :
96- return git_branch_name (repo_path , vcs_subdir )
95+ return git_branch_info (repo_path , vcs_subdir )
9796 else :
98- return "SVN"
97+ return "SVN" , ""
9998
10099
101- def git_branch_name (repo_path , vcs_subdir ):
102- repo = Repo (repo_path )
103- cmd_output = subprocess .run (["git" , "status" , "-s" ], stdout = subprocess .PIPE )
100+ def git_branch_info (repo_path , vcs_subdir ):
101+ cmd_output = subprocess .run (
102+ ["git" , "status" , "--branch" , "--porcelain" ],
103+ stdout = subprocess .PIPE
104+ )
105+ lines = cmd_output .stdout .decode (encoding = "utf-8" ).splitlines ()
104106
105- if len (cmd_output .stdout ) > 0 :
106- fmt = f"{ repo .active_branch .name } *"
107- else :
108- fmt = f"{ repo .active_branch .name } "
107+ branch = lines [0 ][3 :]
108+
109+ try :
110+ local , rest = branch .split ("..." )
111+ except ValueError :
112+ local ,rest = branch , ""
113+
114+ divergence = rest [rest .find ("[" ):rest .find ("]" ) + 1 ]
115+
116+ changes = lines [1 :]
117+ dirty = len (changes ) > 0
118+
119+ if dirty :
120+ local += "*"
109121
110- return fmt . format ()
122+ return local , divergence
111123
112124
113125def virtual_env (pwd ):
@@ -185,7 +197,7 @@ def main():
185197 hostname = gethostname ()
186198 username = os .getenv ('USER' )
187199 repo , repo_path , vcs_subdir = repo_information (pwd )
188- branch = branch_name (repo_path , vcs_subdir ) if repo else ""
200+ branch , divergence = branch_info (repo_path , vcs_subdir ) if repo else ( "" , "" )
189201 virtualenv = virtual_env (pwd )
190202
191203 # Format pieces for display
@@ -195,7 +207,21 @@ def main():
195207 branch = format_branch_name (branch )
196208
197209 # Combine into display
198- prompt = '' .join ([os .linesep , identity , repo , FGRN , pwd , RS , branch , virtualenv , os .linesep , FYEL , CURSOR , RS ])
210+ prompt = '' .join ([
211+ os .linesep ,
212+ identity ,
213+ repo ,
214+ FGRN ,
215+ pwd ,
216+ RS ,
217+ branch ,
218+ divergence ,
219+ virtualenv ,
220+ os .linesep ,
221+ FYEL ,
222+ CURSOR ,
223+ RS
224+ ])
199225 print (prompt )
200226
201227
0 commit comments