我知道自从被问到这个问题已经很久了,但我刚开始使用分支描述,并在寻找展示创意时遇到了这个问题。
关于 bash 脚本答案:它只打印具有描述的分支。我想模仿git branch
输出,但也有描述。
关于 perl 脚本答案:格式化失败,分支名称较长。如果您处于分离的 HEAD 状态,它也会输出螺旋度。
我尝试了python,这就是我想出的。它用以前的答案解决了我的问题。
#!/usr/bin/python
import subprocess
import sys
def git(*args):
return subprocess.check_output(['/usr/bin/git'] + list(args)).strip()
try:
branches = git('branch').split('\n')
except subprocess.CalledProcessError:
sys.exit(1)
longest = len(max(branches, key=len))
for branch in branches:
active = '*' if branch[0] == '*' else ''
branch = branch.lstrip(' *')
try:
desc = git('config', 'branch.'+branch+'.description')
except subprocess.CalledProcessError:
print '{:2}{}'.format(active, branch)
else:
print '{:2}{:{}} {}'.format(active, branch, longest, desc)
展品 A
[user|host ~/git/repo((HEAD detached at origin/master))]% git bd
* (HEAD detached at origin/master)
branch_a
delete_this_after_a_little_while_pls_thx_bye long branch description
other_branch
yay_branches_amirite PR under review
展品 B
[user|host ~/git/repo(other_branch_name)]% git bd
branch_a
delete_this_after_a_little_while_pls_thx_bye long branch description
* other_branch
yay_branches_amirite PR under review
展品 C
[user|host ~/non_git_repo]% git bd
fatal: Not a git repository (or any of the parent directories): .git