簡體   English   中英

魚 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 如果 STRING 的長度不為零,則返回 true。

該腳本將始終回顯“回購有更改...”,而我 100% 確定它沒有更改。 如果我回顯 $repo_has_changes 我會看到一個空行。 此外,如果我檢查字符串的長度(以防字符串返回空格?xD),它也會返回一個空行。 所以我假設 $repo_has_changes 是一個字符串可能是錯誤的。 此外,我不確定我能否以這種方式存儲 git 結果。 不幸的是,我無法找到一個好的來源。

不幸的是,fish 的test是遵循 POSIX 的少數幾個部分之一,它指定帶有任何一個參數的test必須返回 true,以便於使用類似test "thestring" 不幸的是,這也意味着test -n必須返回 true。

這確實意味着您需要引用您傳遞給test的任何變量:

test -n "$branch_on_origin"

test -z "$branch_not_merged_to_master"

之前的回答幾乎回答了我假設的問題。 但是,我最近開發了自己的自定義魚類提示,其中包括右手提示。 我把我的 git 信息放在右手邊。 我不會附上這兩個文件,但我會提供右側提示 function 文件的內容。 它沒有准確顯示您所要求的內容,但在 function 中有涵蓋相同信息及更多信息的示例。 如果不出意外,我希望這將是在魚提示中處理 git 信息的一個很好的例子。

我的右側提示 function 文件(~/.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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM