Git钩子在Source Tree上无法工作

26

通过终端一切正常,但在Source Tree中无法工作

这是我的pre-commit钩子

#!/bin/bash
#
# hook script for swiftlint. It will triggered when you make a commit.
#
# If you want to use, type commands in your console.
# $ ln -s ../../pre-commit-swiftlint.sh .git/hooks/pre-commit
# $ chmod +x .git/hooks/pre-commit

LINT=$(which swiftlint)

if [[ -e "${LINT}" ]]; then
   echo "SwiftLint Start..."
else
echo "SwiftLint does not exist, download from https://github.com/realm/SwiftLint"
exit 1
fi

RESULT=$($LINT lint --quiet)

if [ "$RESULT" == '' ]; then
printf "\e[32mSwiftLint Finished.\e[39m\n"
else
echo ""
printf "\e[41mSwiftLint Failed.\e[49m Please check below:\n"

while read -r line; do

    FILEPATH=$(echo $line | cut -d : -f 1)
    L=$(echo $line | cut -d : -f 2)
    C=$(echo $line | cut -d : -f 3)
    TYPE=$(echo $line | cut -d : -f 4 | cut -c 2-)
    MESSAGE=$(echo $line | cut -d : -f 5 | cut -c 2-)
    DESCRIPTION=$(echo $line | cut -d : -f 6 | cut -c 2-)
    if [ "$TYPE" == 'error' ]; then
        printf "\n  \e[31m$TYPE\e[39m\n"
    else
        printf "\n  \e[33m$TYPE\e[39m\n"
    fi
    printf "    \e[90m$FILEPATH:$L:$C\e[39m\n"
    printf "    $MESSAGE - $DESCRIPTION\n"
done <<< "$RESULT"

printf "\nCOMMIT ABORTED. Please fix them before commiting.\n"

exit 1
fi

什么没有效果?没有被执行?还是会执行并出现错误信息? - VonC
通过此问题已经解决。 - Islam Temirbek
通过添加export PATH=/usr/local/bin:$PATH - Islam Temirbek
太好了!将其作为答案接受即可。这将有助于其他人。 - VonC
8个回答

18

那么每次我都必须通过终端打开SourceTree而不是直接通过系统打开吗? - Shoeb Mirza
@ShoebMirza 这是对我有效的方法。也许还有更好的方式。我很快就习惯了。 - svkaka
只有在我通过终端打开SourceTree而不是直接通过系统打开时才能工作。 - Shoeb Mirza
您还可以安装SourceTree命令行工具(在SourceTree应用程序中作为菜单选项可用),然后从命令行运行“stree”命令。(请参见此评论。) - Josh Kelley
有趣,可行,但这怎么解决问题呢? - spierala
这在sourcetree v4.1.3中不起作用。他们似乎已经将一些常见路径(如/usr/local/bin)添加到环境中,但没有在终端中添加确切的$PATH值。在我的情况下,需要/Users/<username>/go/bin路径,但sourcetree找不到它。我的解决方法是创建符号链接,ln -s /Users/<username>/go/bin/<binary> /usr/local/bin/<binary> - Michael

15

通过添加export PATH=/usr/local/bin:$PATH来解决问题。

Source Tree问题


我将这个字符串直接添加到了 pre-commit 文件的顶部。 - Paul T.

5

如果你在MacOS上使用nvmhusky@>=5,以下是2022年的解决方案:

只需在终端中运行此命令:

echo 'export NVM_DIR="$HOME/.nvm"\n[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' > ~/.huskyrc

如果这个解决方案对您有帮助,请评价它,以便将其提升。


2

要么在~/.bash_profile~/.zshrc中添加export PATH=/usr/local/bin:$PATH

或者

直接进入Sourcetree首选项,在git设置下使用系统git,而不是内嵌的git。


2

通过执行 git config --global core.hooksPath '~/.githooks' 命令进行修复

我将 core.hooksPath 设置为错误的目录。 使用 git config --global core.hooksPath '~/.githooks' 进行重置。


1
这适用于OSX,更新~/.bash_profile:


export PATH="/usr/local/bin:$PATH"

0
在我的情况下,导出路径并没有起作用。我不得不重新安装husky并重新启动sourcetree。然后一切都恢复正常了。

0

安装命令行工具后,您现在可以从终端调用以下内容:

stree

通过命令行打开Sourcetree。

如果在您的终端中起作用,Git Hooks现在将在Sourcetree中运行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接