如何更新Bash自定义提示以显示git状态,并在文件和文件夹更改后刷新?

huangapple go评论95阅读模式
英文:

How to update Bash custom prompt to display git status and refresh after file and folder changes?

问题

我在~/.bashrc中添加了一个PS1,对bash提示进行了更改,以便它还显示git分支和状态,但如果我不在一个包含git的文件夹中并在终端中切换到一个包含git的文件夹,它不会运行该PS1(因为我打开终端时它不在git文件夹中)。

同样,当我在git文件夹中时(显示分支和状态),如果我进行更改或提交,它不会更新git的状态。

这是我的.bashrc中相关的代码。

tput sgr0;
reset=$(tput sgr0);
white=$(tput setaf 15);
violet=$(tput setaf 61);
green=$(tput setaf 64);
purple=$(tput setaf 125);
yellow=$(tput setaf 136);

prompt_git() {
	local s='';
	local branchName='';

	# 检查当前目录是否位于Git存储库中。
	git rev-parse --is-inside-work-tree &>/dev/null || return;

	# 检查我们所在的分支。
	# 获取短符号引用。如果HEAD不是符号引用,获取跟踪远程分支或标签。否则,获取最新提交的短SHA,或放弃。
	branchName="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \
		git describe --all --exact-match HEAD 2> /dev/null || \
		git rev-parse --short HEAD 2> /dev/null || \
		echo '(unknown)')";

	# 检查索引中是否存在未提交的更改。
	if ! $(git diff --quiet --ignore-submodules --cached); then
		s+='+';
	# 检查未暂存的更改。
	elif ! $(git diff-files --quiet --ignore-submodules --); then
		s+='!';
	# 检查未跟踪的文件。
	elif [ -n "$(git ls-files --others --exclude-standard)" ]; then
		s+='?';
	# 检查是否有储藏的文件。
	elif $(git rev-parse --verify refs/stash &>/dev/null); then
		s+='$';
	else
		s+='-';
	fi;

	echo -e "[${white}] on [${violet}]${branchName} [${purple}]${s}";
}

PS1="\[\033]0;\W\007\]";
PS1+="[${green}]\w";

PS1+="$(prompt_git)";

PS1+="[${white}]: $ [${reset}]";
export PS1;

PS2="\[${yellow}]→ [${reset}]";
export PS2;

我想知道是否有一种方法可以“刷新”PS1,以便在文件夹更改、git命令和如果可能的话文件更改时更新状态和分支。

英文:

I added to the ~/.bashrc a PS1 with changes to the bash prompt so it also shows git branches and status, but if i'm in not a folder with git and goes to one in the terminal, it won't run that PS1 ( because when I opened the terminal it wasn't on a git folder )

Same goes to when i'm in a git folder ( showing the branche and status ), if I make changes or commits it doesn't change the status of git in it.

This is the relevant code from my .bashrc.

tput sgr0;
reset=$(tput sgr0);
white=$(tput setaf 15);
violet=$(tput setaf 61);
green=$(tput setaf 64);
purple=$(tput setaf 125);
yellow=$(tput setaf 136);

prompt_git() {
	local s='';
	local branchName='';

	# Check if the current directory is in a Git repository.
	git rev-parse --is-inside-work-tree &>/dev/null || return;

	# Check for what branch we’re on.
	# Get the short symbolic ref. If HEAD isn’t a symbolic ref, get a
	# tracking remote branch or tag. Otherwise, get the
	# short SHA for the latest commit, or give up.
	branchName="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \
		git describe --all --exact-match HEAD 2> /dev/null || \
		git rev-parse --short HEAD 2> /dev/null || \
		echo '(unknown)')";

	# Check for uncommitted changes in the index.
	if ! $(git diff --quiet --ignore-submodules --cached); then
		s+='+';
	# Check for unstaged changes.
	elif ! $(git diff-files --quiet --ignore-submodules --); then
		s+='!';
	# Check for untracked files.
	elif [ -n "$(git ls-files --others --exclude-standard)" ]; then
		s+='?';
	# Check for stashed files.
	elif $(git rev-parse --verify refs/stash &>/dev/null); then
		s+='$';
	else
		s+='-';
	fi;

	echo -e "\[${white}\] on \[${violet}\]${branchName} \[${purple}\][${s}]";
}

PS1="\[\033]0;\W\007\]";
PS1+="\[${green}\]\w";

PS1+="$(prompt_git)";

PS1+="\[${white}\]: $ \[${reset}\]";
export PS1;

PS2="\[${yellow}\]\[${reset}\]";
export PS2;

I would like to know if there is a way to "refresh" the PS1 when there is folder changes, git commands and if possible file changes to update the status and branches.

答案1

得分: 0

在阅读有关Bash字符串的内容后,似乎双引号在保存之前被解析,而单引号则在之后解析。

因此,考虑到这一点,我进行了如下更改,它有效:

# prompt_git
echo -e "${violet}${branchName} ${purple}[${s}]";

# after
PS1+="\[${white}\] on ";
PS1+='$(prompt_git)';

但如果文件更改也能改变状态本身,那就更酷了。

英文:

So after reading about bash strings it looks like double quotes get evaluated before its saved, and single ones evaluate later.

So with that in mind I changed to this and it works:

# prompt_git
echo -e "${violet}${branchName} ${purple}[${s}]";

# after
PS1+="\[${white}\] on ";
PS1+='$(prompt_git)';

But still would be cool if file changes could also change the status itself.

huangapple
  • 本文由 发表于 2023年6月1日 09:39:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378178.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定