Adding git branch on the Bash command prompt

前端 未结 13 2094
感情败类
感情败类 2020-11-30 16:15

I tried adding the git branch I\'m currently working on (checked-out) on the bash prompt without success.. (while keeping my current path which shows the active dir

13条回答
  •  情书的邮戳
    2020-11-30 16:57

    I have tried a small script in python that goes in a bin folder.... 'gitprompt' file

    #!/usr/bin/env python
    import subprocess, os
    s = os.path.join(os.getcwd(), '.git')
    def cut(cmd):
        ret=''
        half=0
        record = False
        for c in cmd:
            if c == "\n":
                if not (record):
                    pass
                else:
                    break
            if (record) and c!="\n":
                ret = ret + c
            if c=='*':
                half=0.5
            if c==' ':
                if half == 0.5:
                    half = 1
            if half == 1:
                record = True
        return ret
    if (os.path.isdir(s)):
        out = subprocess.check_output("git branch",shell=True)
        print cut(out)
    else:
        print "-"
    

    Make it executable and stuff

    Then adjust the bash prompt accordingly like :

    \u:\w--[$(gitprompt)] \$ 
    

提交回复
热议问题