简体   繁体   English

鱼 Shell:如何解释 git 响应?

[英]Fish Shell: How to interpret git response?

For my workspace environment I'm currently creating a script to easily switch between my development and release branches.对于我的工作区环境,我目前正在创建一个脚本,以便在我的开发和发布分支之间轻松切换。 Unfortunately the repo's do not always have similar branch name, therefor I check if the branch exists and if the branch has already been merged to master.不幸的是,repo 并不总是具有相似的分支名称,因此我检查分支是否存在以及分支是否已经合并到 master。 If the branch has not been merged to master, I know I have the latest release branch.如果分支还没有合并到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

In my ignorance I thought I store the result of my git command, and store it in a variable.在我的无知中,我以为我存储了我的 git 命令的结果,并将其存储在一个变量中。 I interpreted it as string.我将其解释为字符串。 According to the fish documentation I can test if a string is non-zero.根据fish文档,我可以测试字符串是否非零。

-n STRING returns true if the length of STRING is non-zero. -n STRING 如果 STRING 的长度不为零,则返回 true。

This script will always echo 'Repo has changes...' while I'm 100% sure it doesn't have changes.该脚本将始终回显“回购有更改...”,而我 100% 确定它没有更改。 If I echo $repo_has_changes I see an empty line.如果我回显 $repo_has_changes 我会看到一个空行。 Also if I check the length of the string (just in case string returns spaces? xD ) it also returns an empty line.此外,如果我检查字符串的长度(以防字符串返回空格?xD),它也会返回一个空行。 So my assumption that $repo_has_changes is a string might be wrong.所以我假设 $repo_has_changes 是一个字符串可能是错误的。 Also I'm not certain that I'm able to store the git result in this fashion.此外,我不确定我能否以这种方式存储 git 结果。 Unfortunately I wasn't able to find a good source for this.不幸的是,我无法找到一个好的来源。

Unfortunately, fish's test is one of the few parts that follow POSIX to the letter, and that specifies that test with any one argument has to return true, to facilitate uses like test "thestring" .不幸的是,fish 的test是遵循 POSIX 的少数几个部分之一,它指定带有任何一个参数的test必须返回 true,以便于使用类似test "thestring" Unfortunately this also means that test -n has to return true.不幸的是,这也意味着test -n必须返回 true。

Which indeed means you need to quote any variable you pass to test :这确实意味着您需要引用您传递给test的任何变量:

test -n "$branch_on_origin"

and

test -z "$branch_not_merged_to_master"

The previous response pretty much answers your question I would assume.之前的回答几乎回答了我假设的问题。 However, I recently developed my own custom prompt for fish that includes a right hand prompt.但是,我最近开发了自己的自定义鱼类提示,其中包括右手提示。 I put my git information in the right hand side.我把我的 git 信息放在右手边。 I won't attach both files, but I will provide the contents of the right side prompt function file.我不会附上这两个文件,但我会提供右侧提示 function 文件的内容。 It doesn't display exactly what you requested, but within the function there examples that cover the same information and more.它没有准确显示您所要求的内容,但在 function 中有涵盖相同信息及更多信息的示例。 If nothing else I'm hoping it will be a good example of handling git information in the fish prompt.如果不出意外,我希望这将是在鱼提示中处理 git 信息的一个很好的例子。

My right side prompt function file (~/.config/fish/functions/fish_right_prompt.fish):我的右侧提示 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