【问题标题】:Fish Shell: How to interpret git response?鱼壳:如何解释 git 响应?
【发布时间】:2019-10-16 17:50:19
【问题描述】:

对于我的工作区环境,我目前正在创建一个脚本,以便在我的开发和发布分支之间轻松切换。不幸的是,repo 并不总是具有相似的分支名称,因此我检查分支是否存在以及分支是否已合并到 master。如果分支还没有合并到master,我知道我有最新的release分支。

        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 如果 STRING 的长度不为零,则返回 true。

此脚本将始终回显“Repo has changes...”,而我 100% 确定它没有更改。 如果我回显 $repo_has_changes 我会看到一个空行。此外,如果我检查字符串的长度(以防字符串返回空格?xD),它也会返回一个空行。所以我假设 $repo_has_changes 是一个字符串可能是错误的。另外我不确定我是否能够以这种方式存储 git 结果。不幸的是,我无法找到一个好的来源。

【问题讨论】:

  • 我不使用fish,也无法说出它的特定规则,但是在其他shell中,if test -n $foo必须拼写if test -n "$foo"如果$foo可以永远是空的,否则你运行test -n 而不是test -n <the-empty-string>。除此之外,请参阅git_sh_setup(与 Git 一起分发,但为 sh/bash 编写),因为它具有用于测试索引和工作树清洁度的实用功能。您的测试以查看origin/$b 是否存在可以拼写为git rev-parse -q --verify refs/remotes/origin/$b,如果远程跟踪名称存在则退出 0(并打印哈希 ID),否则退出 1。
  • 谢谢先生为我解决这个问题:)

标签: git shell fish


【解决方案1】:

不幸的是,fish 的test 是遵循 POSIX 的少数几个部分之一,它指定带有任何一个参数的 test 必须返回 true,以便于使用 test "thestring"。不幸的是,这也意味着 test -n 必须返回 true。

这确实意味着您需要引用传递给 test 的任何变量:

test -n "$branch_on_origin"

test -z "$branch_not_merged_to_master"

【讨论】:

  • fish 是否仍将命令替换的 作为数组元素存储在变量中?也许count 会是一个更好的测试。
  • 我最终将其更改为:``` test -n "$branch_not_merged_to_master" ``` 如果分支未合并,该变量将被填充 :) 感谢您的快速响应! :)
【解决方案2】:

之前的回答几乎回答了我假设的问题。但是,我最近开发了自己的自定义鱼类提示,其中包括右手提示。我把我的 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-23
    • 2015-04-24
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    • 2022-06-20
    • 2014-04-27
    • 1970-01-01
    相关资源
    最近更新 更多