鱼 Shell:如何解释 git 响应?
Fish Shell: How to interpret git response?
对于我的工作区环境,我目前正在创建一个脚本以在我的开发和发布分支之间轻松切换。不幸的是,repo 并不总是有相似的分支名称,因此我检查分支是否存在以及分支是否已经合并到 master。如果分支还没有合并到master,我知道我有最新发布的分支。
set repo_has_changes (git status --porcelain --untracked-files=no)
if test -n $repo_has_changes
echo 'Repo has changes...'
else
git checkout master
for b in $releaseVersions
set branch_on_origin (git branch -r --list origin/$b)
set branch_not_merged_to_master (git branch -r --list --no-merged master origin/$b)
if test -n $branch_on_origin
if test -z $branch_not_merged_to_master
git checkout $b
git pull
break
end
end
end
end
在我的无知中,我以为我存储了我的 git 命令的结果,并将其存储在一个变量中。我将其解释为字符串。根据 fish 文档,我可以测试字符串是否非零。
-n STRING returns true if the length of STRING is non-zero.
此脚本将始终回显 'Repo has changes...',而我 100% 确定它没有更改。
如果我回显 $repo_has_changes 我会看到一个空行。另外,如果我检查字符串的长度(以防万一 string returns spaces? xD ),它也会 returns 一个空行。所以我假设 $repo_has_changes 是一个字符串可能是错误的。此外,我不确定我是否能够以这种方式存储 git 结果。不幸的是,我找不到合适的资源。
不幸的是,fish 的 test
是 POSIX 后面的少数几个部分之一,它指定 test
与任何一个参数必须 return是的,以便于像 test "thestring"
这样的使用。不幸的是,这也意味着 test -n
必须 return 为真。
这确实意味着您需要引用传递给 test
:
的任何变量
test -n "$branch_on_origin"
和
test -z "$branch_not_merged_to_master"
之前的回复几乎回答了我假设的你的问题。但是,我最近开发了自己的自定义鱼提示,其中包括右手提示。我把我的 git 信息放在右边。这两个文件我就不一一附上了,我会提供右侧提示功能文件的内容。它没有准确显示您所请求的内容,但在函数中有涵盖相同信息和更多信息的示例。如果不出意外,我希望它能成为处理鱼提示中 git 信息的一个很好的例子。
我的右侧提示功能文件(~/.config/fish/functions/fish_right_prompt.fish):
function fish_right_prompt -d "Write out the right prompt"
set -l last_status $status # Special variable for storing last `$status` returned by terminal.
set -l is_git_repository (git rev-parse --is-inside-work-tree ^/dev/null) # Special variable indicating git.
# ==============
# Status Segment
# ==============
# This segment is not visible in the prompt by default. It checks the `$last_status` variable to see if the
# terminal's last executed command or function returned an error status. If it did then the status segment is
# generated based on the returned status code, formatted and colored accordingly, and printed to prompt.
# --------------
if not test $last_status -eq 0 # Test if `$last_status` is not equal to `0`.
set_color -o red # Color the prompt bold red.
# Eventually it would be cool to include conditional programming here to generate unique output for any
# error cases to print to the prompt. For now, I'm just going to have this segment print a generic
# formatted string to the prompt in the event of any errors.
printf "%b" "\U203C Error Code $last_status \U203C"
set_color normal
end
# ===========
# Git Segment
# ===========
# An optional segment that is generated and printed to the prompt if the PWD is a git repository. If the
# segment is generated it displays a variety of indicators and information depending on the current state of
# repo.
# -----------
if test -n "$is_git_repository" # Test if the PWD is an active git repository.
set_color A3A3A3 # Color the prompt grey. (Uses a hex color.)
echo -n "[git:" # Echo the git repo indicator.
set -l branch (git symbolic-ref --short HEAD ^/dev/null; or git show-ref --head -s --abbrev | head -n1 ^/dev/null)
git diff-files --quiet --ignore-submodules ^/dev/null; or set -l has_unstaged_files
git diff-index --quiet --ignore-submodules --cached HEAD ^/dev/null; or set -l has_staged_files
if set -q has_unstaged_files # Check if the repo is unstaged.
set_color red # Color the prompt red.
else if set -q has_staged_files # Check if the repo is staged.
set_color yellow # Color the prompt yellow.
else # If the repo is clean then proceed.
set_color green # Color the prompt green.
end
echo -n "$branch]" # Echo the git branch into the prompt.
git rev-parse --abbrev-ref '@{upstream}' >/dev/null ^&1; and set -l has_upstream # Set `$has_upstream`.
if set -q has_upstream # Check if the repository has any upstream.
set -l commit_counts (git rev-list --left-right --count 'HEAD...@{upstream}' ^/dev/null) # Commit counts.
set -l commits_to_push (echo $commit_counts | cut -f 1 ^/dev/null) # Commits to push.
set -l commits_to_pull (echo $commit_counts | cut -f 2 ^/dev/null) # Commits to pull.
if test $commits_to_push != 0 # Check if there any commits to push.
if test $commits_to_pull -ne 0 # Check if there are any commits to pull.
set_color red # Color the prompt red.
else if test $commits_to_push -gt 3 # Check if there are more than 3 commits to push.
set_color yellow # Color the prompt yellow.
else # If there are no commits to pull, and no more than 3 commits to push, then proceed.
set_color green # Color the prompt green.
end
echo -n " ⇡ " # Echo the upstream push symbol.
end
if test $commits_to_pull != 0 # Check if there are any commits to pull.
if test $commits_to_push -ne 0 # Check if there are any commits to push
set_color red # Color the prompt red.
else if test $commits_to_pull -gt 3 # Check if there are more than 3 commits to pull.
set_color yellow # Color the prompt yellow.
else # If no commits to push, and no more than 3 commits to pull, then proceed.
set_color green # Color the prompt green.
end
echo -n " ⇣ " # Echo the upstream pull symbol.
end
set_color normal # Reset color and text decoration.
end
if test (git stash list | wc -l) -gt 0 # Check if there are any stashed changes.
set_color A3A3A3 # Color the prompt grey. (Uses a hex color.)
echo -n " ☰ " # Echo the stacked changes symbol.
end
set_color normal # Reset color and text decoration.
end
# -------------------------------------------------------------------------------------------------------------
# That's all there is to right side secondary prompt. All other prompt segments can be found in the fish
# function file for the primary prompt: `fish_prompt.fish`.
# -------------------------------------------------------------------------------------------------------------
end
对于我的工作区环境,我目前正在创建一个脚本以在我的开发和发布分支之间轻松切换。不幸的是,repo 并不总是有相似的分支名称,因此我检查分支是否存在以及分支是否已经合并到 master。如果分支还没有合并到master,我知道我有最新发布的分支。
set repo_has_changes (git status --porcelain --untracked-files=no)
if test -n $repo_has_changes
echo 'Repo has changes...'
else
git checkout master
for b in $releaseVersions
set branch_on_origin (git branch -r --list origin/$b)
set branch_not_merged_to_master (git branch -r --list --no-merged master origin/$b)
if test -n $branch_on_origin
if test -z $branch_not_merged_to_master
git checkout $b
git pull
break
end
end
end
end
在我的无知中,我以为我存储了我的 git 命令的结果,并将其存储在一个变量中。我将其解释为字符串。根据 fish 文档,我可以测试字符串是否非零。
-n STRING returns true if the length of STRING is non-zero.
此脚本将始终回显 'Repo has changes...',而我 100% 确定它没有更改。 如果我回显 $repo_has_changes 我会看到一个空行。另外,如果我检查字符串的长度(以防万一 string returns spaces? xD ),它也会 returns 一个空行。所以我假设 $repo_has_changes 是一个字符串可能是错误的。此外,我不确定我是否能够以这种方式存储 git 结果。不幸的是,我找不到合适的资源。
不幸的是,fish 的 test
是 POSIX 后面的少数几个部分之一,它指定 test
与任何一个参数必须 return是的,以便于像 test "thestring"
这样的使用。不幸的是,这也意味着 test -n
必须 return 为真。
这确实意味着您需要引用传递给 test
:
test -n "$branch_on_origin"
和
test -z "$branch_not_merged_to_master"
之前的回复几乎回答了我假设的你的问题。但是,我最近开发了自己的自定义鱼提示,其中包括右手提示。我把我的 git 信息放在右边。这两个文件我就不一一附上了,我会提供右侧提示功能文件的内容。它没有准确显示您所请求的内容,但在函数中有涵盖相同信息和更多信息的示例。如果不出意外,我希望它能成为处理鱼提示中 git 信息的一个很好的例子。
我的右侧提示功能文件(~/.config/fish/functions/fish_right_prompt.fish):
function fish_right_prompt -d "Write out the right prompt"
set -l last_status $status # Special variable for storing last `$status` returned by terminal.
set -l is_git_repository (git rev-parse --is-inside-work-tree ^/dev/null) # Special variable indicating git.
# ==============
# Status Segment
# ==============
# This segment is not visible in the prompt by default. It checks the `$last_status` variable to see if the
# terminal's last executed command or function returned an error status. If it did then the status segment is
# generated based on the returned status code, formatted and colored accordingly, and printed to prompt.
# --------------
if not test $last_status -eq 0 # Test if `$last_status` is not equal to `0`.
set_color -o red # Color the prompt bold red.
# Eventually it would be cool to include conditional programming here to generate unique output for any
# error cases to print to the prompt. For now, I'm just going to have this segment print a generic
# formatted string to the prompt in the event of any errors.
printf "%b" "\U203C Error Code $last_status \U203C"
set_color normal
end
# ===========
# Git Segment
# ===========
# An optional segment that is generated and printed to the prompt if the PWD is a git repository. If the
# segment is generated it displays a variety of indicators and information depending on the current state of
# repo.
# -----------
if test -n "$is_git_repository" # Test if the PWD is an active git repository.
set_color A3A3A3 # Color the prompt grey. (Uses a hex color.)
echo -n "[git:" # Echo the git repo indicator.
set -l branch (git symbolic-ref --short HEAD ^/dev/null; or git show-ref --head -s --abbrev | head -n1 ^/dev/null)
git diff-files --quiet --ignore-submodules ^/dev/null; or set -l has_unstaged_files
git diff-index --quiet --ignore-submodules --cached HEAD ^/dev/null; or set -l has_staged_files
if set -q has_unstaged_files # Check if the repo is unstaged.
set_color red # Color the prompt red.
else if set -q has_staged_files # Check if the repo is staged.
set_color yellow # Color the prompt yellow.
else # If the repo is clean then proceed.
set_color green # Color the prompt green.
end
echo -n "$branch]" # Echo the git branch into the prompt.
git rev-parse --abbrev-ref '@{upstream}' >/dev/null ^&1; and set -l has_upstream # Set `$has_upstream`.
if set -q has_upstream # Check if the repository has any upstream.
set -l commit_counts (git rev-list --left-right --count 'HEAD...@{upstream}' ^/dev/null) # Commit counts.
set -l commits_to_push (echo $commit_counts | cut -f 1 ^/dev/null) # Commits to push.
set -l commits_to_pull (echo $commit_counts | cut -f 2 ^/dev/null) # Commits to pull.
if test $commits_to_push != 0 # Check if there any commits to push.
if test $commits_to_pull -ne 0 # Check if there are any commits to pull.
set_color red # Color the prompt red.
else if test $commits_to_push -gt 3 # Check if there are more than 3 commits to push.
set_color yellow # Color the prompt yellow.
else # If there are no commits to pull, and no more than 3 commits to push, then proceed.
set_color green # Color the prompt green.
end
echo -n " ⇡ " # Echo the upstream push symbol.
end
if test $commits_to_pull != 0 # Check if there are any commits to pull.
if test $commits_to_push -ne 0 # Check if there are any commits to push
set_color red # Color the prompt red.
else if test $commits_to_pull -gt 3 # Check if there are more than 3 commits to pull.
set_color yellow # Color the prompt yellow.
else # If no commits to push, and no more than 3 commits to pull, then proceed.
set_color green # Color the prompt green.
end
echo -n " ⇣ " # Echo the upstream pull symbol.
end
set_color normal # Reset color and text decoration.
end
if test (git stash list | wc -l) -gt 0 # Check if there are any stashed changes.
set_color A3A3A3 # Color the prompt grey. (Uses a hex color.)
echo -n " ☰ " # Echo the stacked changes symbol.
end
set_color normal # Reset color and text decoration.
end
# -------------------------------------------------------------------------------------------------------------
# That's all there is to right side secondary prompt. All other prompt segments can be found in the fish
# function file for the primary prompt: `fish_prompt.fish`.
# -------------------------------------------------------------------------------------------------------------
end