#6 `subbash/prompt`: GBranch: simplify the command

닫힘
tukusejssirs5 년 전을 오픈 · 3개의 코멘트
tukusejssirs 코멘트됨, 5 년 전

I was looking for a command to get the current branch name and I found this (src):

git rev-parse --abbrev-ref HEAD

Would it not be better than the current method?

# Current method
git status --porcelain=2 -b 2>/dev/null | tr '\n' ':' | \
awk 'match($0,/# branch.head [^ :]+/) {print substr($0,RSTART+14,RLENGTH-14)}'
I was looking for a command to get the current branch name and I found this ([src](https://stackoverflow.com/a/12142066/3408342)): ```bash git rev-parse --abbrev-ref HEAD ``` Would it not be better than the current method? ```bash # Current method git status --porcelain=2 -b 2>/dev/null | tr '\n' ':' | \ awk 'match($0,/# branch.head [^ :]+/) {print substr($0,RSTART+14,RLENGTH-14)}' ```
tukusejssirs 코멘트됨, 5 년 전
포스터

If anything, it should faster (I’ve removed the branch name from the output):

$ time git status --porcelain=2 -b 2>/dev/null | tr '\n' ':' | \
awk 'match($0,/# branch.head [^ :]+/) {print substr($0,RSTART+14,RLENGTH-14)}'

real    0m0.008s
user    0m0.007s
sys 0m0.007s

$ time git rev-parse --abbrev-ref HEAD

real    0m0.003s
user    0m0.003s
sys 0m0.000s
If anything, it should faster (I’ve removed the branch name from the output): ```bash $ time git status --porcelain=2 -b 2>/dev/null | tr '\n' ':' | \ awk 'match($0,/# branch.head [^ :]+/) {print substr($0,RSTART+14,RLENGTH-14)}' real 0m0.008s user 0m0.007s sys 0m0.007s $ time git rev-parse --abbrev-ref HEAD real 0m0.003s user 0m0.003s sys 0m0.000s ```
demure 코멘트됨, 5 년 전
소유자

Currently the awk is using a variable.
So a better test would be:

~ -> cd projects/dotfiles/
dotfiles + [M] -> GStatus="$(git status --porcelain=2 -b 2>/dev/null | tr '\n' ':')"
dotfiles [M] -> time echo ${GStatus} | awk 'match($0,/# branch.head [^ :]+/) {print substr($0,RSTART+14,RLENGTH-14)}'
master

real    0m0.007s
user    0m0.007s
sys     0m0.001s
dotfiles [M] -> time git rev-parse --abbrev-ref HEAD
master

real    0m0.005s
user    0m0.000s
sys     0m0.005s
dotfiles [M] ->

I lean a bit more towards keeping the code base uniform as the rest of the git checking also uses the git status (and it seems like minimal difference in time to me.)

Currently the awk is using a variable. So a better test would be: ``` ~ -> cd projects/dotfiles/ dotfiles + [M] -> GStatus="$(git status --porcelain=2 -b 2>/dev/null | tr '\n' ':')" dotfiles [M] -> time echo ${GStatus} | awk 'match($0,/# branch.head [^ :]+/) {print substr($0,RSTART+14,RLENGTH-14)}' master real 0m0.007s user 0m0.007s sys 0m0.001s dotfiles [M] -> time git rev-parse --abbrev-ref HEAD master real 0m0.005s user 0m0.000s sys 0m0.005s dotfiles [M] -> ``` I lean a bit more towards keeping the code base uniform as the rest of the git checking also uses the git status (and it seems like minimal difference in time to me.)
tukusejssirs 코멘트됨, 5 년 전
포스터

No problem. I just gave a suggestion which can be rejected—as always. :)

No problem. I just gave a suggestion which can be rejected—as always. :)
demure 5 년 전가 Close
로그인하여 이 대화에 참여
레이블 없음
bug
마일스톤 없음
담당자 없음
참여자 2명
로딩중...
취소
저장
아직 콘텐츠가 없습니다.