다음 스크립트를 사용하여 bash 프롬프트를 다음과 같이 설정하려고합니다. 모든 것이 작동하지만 Git 저장소의 브랜치 이름과 브랜치의 상태를 컬러로 인쇄하는 부분입니다. 색상은 다소 임의적이지만, 파일이 커밋되지 않은 경우 빨간색, 파일이 스테이지되지 않은 경우 노란색, 다른 것이 녹색 인 경우에는 말할 필요도 없습니다. 흰 생각에 내가 원하는 부분을 인쇄하고있어. 내가 마지막 부분으로 $branchStyle
을 개별적으로 정의하는 스크립트의 일부를 실행하면 작동하지만 여기에는 포함되지 않습니다. 내가 도대체 뭘 잘못하고있는 겁니까?bash 프롬프트에서 git 상태의 색상 설정
prompt_git() {
local s=""
local branchName=""
# check if the current directory is in a git repository
if [ $(git rev-parse --is-inside-work-tree &>/dev/null; printf "%s" $?) == 0 ]; then
# check if the current directory is in .git before running git checks
if [ "$(git rev-parse --is-inside-git-dir 2> /dev/null)" == "false" ]; then
# ensure index is up to date
git update-index --really-refresh -q &>/dev/null
# check for uncommitted changes in the index
if ! $(git diff --quiet --ignore-submodules --cached); then
s="$s+";
fi
# check for unstaged changes
if ! $(git diff-files --quiet --ignore-submodules --); then
s="$s!";
fi
# check for untracked files
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
s="$s?";
fi
# check for stashed files
if $(git rev-parse --verify refs/stash &>/dev/null); then
s="$s$";
fi
fi
# get the short symbolic ref
# if HEAD isn't a symbolic ref, get the short SHA
# otherwise, just give up
branchName="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \
git rev-parse --short HEAD 2> /dev/null || \
printf "(unknown)")"
[ -n "$s" ] && s=" [$s]"
printf "%s" "$1$branchName$s"
else
return
fi
}
set_prompts() {
local bold=$(tput bold)
local reset=$(tput sgr0)
local base05=$(tput setaf 188) # light grey
local base08=$(tput setaf 210) # red
local base0A=$(tput setaf 221) # yellow
local base0B=$(tput setaf 114) # green
if git rev-parse --git-dir >/dev/null 2>&1; then
# check for uncommitted changes in the index
if ! git diff-index --quiet --cached HEAD --ignore-submodules -- >&2; then
branchStyle=$base08
# check for unstaged changes
elif ! git diff-files --quiet --ignore-submodules -- >&2; then
branchStyle=$base0A
else
branchStyle=$base0B
fi
fi
PS1+="\$(prompt_git \"$bold$base05 on $branchStyle\")" # git repository details
export PS1
}
set_prompts
unset set_prompts
문제를 나타내는 짧은 테스트 사례가 있습니까? [bash 태그 위키] (http : // stackoverflow.co.kr/tags/bash/info)에는 게시 된 코드 크기를 줄이는 방법에 대한 팁이 있습니다. –
@thatotherguy, k, 나는 스크립트에 필요한 것 같지 않은 모든 것을 꺼 냈습니다. 본질적으로'prompt_git' 함수는 repo에서 변경된 것을 찾고 문자열을 추가하여 프롬프트에서 볼 수 있도록 설계되었습니다. 그리고 커밋되지 않은 변경이나 unstaged 변경이 있는지를 결정하는'set_prompts' 함수에서 다른 검사를합니다 그에 따라 색상을 설정합니다. 나는이 모든 것이 1 또는 많은 함수에서 행해졌으므로, 최선의 결과는 무엇이든 상관 없다. – chrisopedia
스크립트에 필요없는 내용 만 제거하지 마십시오. 게시하려는 특정 색상 문제를 재현하지 않아도되는 내용은 모두 제거하십시오. 예를 들어 문제를 재연하는 데 충분합니까? 'echo $ (tput setaf 210) 왜이 텍스트는 핑크색이 아닌 흰색 " –