here is my code
def get_current_branch():
"returns current working branch."
init()
try:
with cd(env.repo_path):
with hide('output','running','warnings'):
cmd = 'git branch'
out = run(cmd, shell=True).split('\n')
print out
for branch in out:
if '*' in branch:
temp = branch.split(' ')[1]
out = temp
return out
except Exception as msg:
print(red("\nError!!!\n\n" + str(msg)))
output of above code comes out to be :
['RALP\x1b[m\r', ' SALP\x1b[m\r', '* \x1b[32mintegration\x1b[m']
The actual branch names are RALP, SALP and integration. But all these special characters are ruining the text processing. How can I get rid of these characters ?
Remove them using regex
import re
out = re.sub('\x1b[^m]*m', '', out)
out = re.sub('\r$', '', out)
out = re.sub('\*', '', out)
out = out.strip()
FYI, these sequences are the ANSI escape codes which are used to add color to the text in the terminal. If you run git branch --no-color
, some of the portion would be automatically removed.
Also, change temp = branch.split(' ')[1]
to temp = branch.split('*')[1]
. This should automatically remove the * in the current branch.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.