有条件的语句检查您是否位于Shell中的GIT存储库中

发布于 2025-02-13 04:01:50 字数 2665 浏览 0 评论 0 原文

我正在尝试在 c:/program Files/git/etc/profile.d/git-prompt.sh 目录的Windows上编辑我的git bash终端目录是GIT存储库。

以下是我当前的代码:

if test -f /etc/profile.d/git-sdk.sh
then
    TITLEPREFIX=SDK-${MSYSTEM#MINGW}
else
    TITLEPREFIX=$MSYSTEM
fi

if test -f ~/.config/git/git-prompt.sh
then
    . ~/.config/git/git-prompt.sh
else
    PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window title
    PS1="$PS1"'\[\033[1;32m\]'       # change to green
    PS1="$PS1"'➜  '                 # arrow
    PS1="$PS1"'\[\033[1;36m\]'      # change color to bold cyan
    PS1="$PS1"'\W'                  # current working directory
    if test -z "$WINELOADERNOEXEC"
    then
        GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"
        COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"
        COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"
        COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"
        # if test -z "$COMPLETION_PATH/git-prompt.sh"
        # then
        . "$COMPLETION_PATH/git-completion.bash"
        . "$COMPLETION_PATH/git-prompt.sh"
        # if test -n `git branch --show-current` 
        # then
        PS1="$PS1"'\[\033[1;34m\]'                  # change color to cyan
        PS1="$PS1"' git:('                          # bash function
        PS1="$PS1"'\[\033[1;31m\]'                  # change color to cyan
        PS1="$PS1"'`git branch --show-current`'     # get current working branch
        PS1="$PS1"'\[\033[1;34m\]'                  # change color to bold blue
        PS1="$PS1"') '                              # close brackets

        PS1="$PS1"'\n'                  # new line
        PS1="$PS1"'\[\033[1;33m\]'      # change color to bold yellow
        PS1="$PS1"'$ '                  # prompt: always $
        PS1="$PS1"'\[\033[0m\]'         # change color to default
        # fi
        # fi
    fi
fi

MSYS2_PS1="$PS1"                    # for detection by MSYS2 SDK's bash.basrc

# Evaluate all user-specific Bash completion scripts (if any)
if test -z "$WINELOADERNOEXEC"
then
    for c in "$HOME"/bash_completion.d/*.bash
    do
        # Handle absence of any scripts (or the folder) gracefully
        test ! -f "$c" ||
        . "$c"
    done
fi

我正在尝试创建一个有条件的语句,以测试我的当前目录是否为git存储库,如果是的,请显示正确的格式。

如果我在GIT存储库中打开终端:

➜  test-repo git:(main)
$ 

如果我打开端子而没有 git存储库:

fatal: not a git repository (or any of the parent directories): .git
➜  test-repo git:() 
$ 

Intension是删除 fatal:不是git存储库(或任何父级目录):。 git git:()如果我不在git存储库中。

I'm trying to edit my git bash terminal on Windows in the C:/Program Files/Git/etc/profile.d/git-prompt.sh directory and I want to check if my current working directory is a Git repository.

Below is my current code:

if test -f /etc/profile.d/git-sdk.sh
then
    TITLEPREFIX=SDK-${MSYSTEM#MINGW}
else
    TITLEPREFIX=$MSYSTEM
fi

if test -f ~/.config/git/git-prompt.sh
then
    . ~/.config/git/git-prompt.sh
else
    PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window title
    PS1="$PS1"'\[\033[1;32m\]'       # change to green
    PS1="$PS1"'➜  '                 # arrow
    PS1="$PS1"'\[\033[1;36m\]'      # change color to bold cyan
    PS1="$PS1"'\W'                  # current working directory
    if test -z "$WINELOADERNOEXEC"
    then
        GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"
        COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"
        COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"
        COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"
        # if test -z "$COMPLETION_PATH/git-prompt.sh"
        # then
        . "$COMPLETION_PATH/git-completion.bash"
        . "$COMPLETION_PATH/git-prompt.sh"
        # if test -n `git branch --show-current` 
        # then
        PS1="$PS1"'\[\033[1;34m\]'                  # change color to cyan
        PS1="$PS1"' git:('                          # bash function
        PS1="$PS1"'\[\033[1;31m\]'                  # change color to cyan
        PS1="$PS1"'`git branch --show-current`'     # get current working branch
        PS1="$PS1"'\[\033[1;34m\]'                  # change color to bold blue
        PS1="$PS1"') '                              # close brackets

        PS1="$PS1"'\n'                  # new line
        PS1="$PS1"'\[\033[1;33m\]'      # change color to bold yellow
        PS1="$PS1"'$ '                  # prompt: always $
        PS1="$PS1"'\[\033[0m\]'         # change color to default
        # fi
        # fi
    fi
fi

MSYS2_PS1="$PS1"                    # for detection by MSYS2 SDK's bash.basrc

# Evaluate all user-specific Bash completion scripts (if any)
if test -z "$WINELOADERNOEXEC"
then
    for c in "$HOME"/bash_completion.d/*.bash
    do
        # Handle absence of any scripts (or the folder) gracefully
        test ! -f "$c" ||
        . "$c"
    done
fi

I'm trying to create a conditional statement to test if my current directory is a Git repository and if so, display the correct format.

If I open my terminal in a Git repository:

➜  test-repo git:(main)
$ 

If I open my terminal without Git repository:

fatal: not a git repository (or any of the parent directories): .git
➜  test-repo git:() 
$ 

The intension is to remove fatal: not a git repository (or any of the parent directories): .git and git:() if I'm not in a Git repository.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生寂 2025-02-20 04:01:50

更新如何

if git status -s 1>&2 2>/dev/null
then
  : You are in a git repo
else
  : You are not
fi

git状态的更快替代品-s

git rev-parse -q --short HEAD

How about

if git status -s 1>&2 2>/dev/null
then
  : You are in a git repo
else
  : You are not
fi

UPDATE : A faster alternative to git status -s would be

git rev-parse -q --short HEAD
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文