【问题标题】:Git pre-commit hook behavior with Intellij Idea使用 Intellij Idea 的 Git 预提交钩子行为
【发布时间】:2018-07-23 19:04:41
【问题描述】:

我有一个带有预提交 git 钩子的 monorepo 项目,它会在 package.json 文件中颠簸版本。该挂钩使用以下脚本:

#!/usr/bin/env bash

set -e

# See if git detects any changes for the given directory
if [[ -z `git diff --cached --shortstat ./packages/$1` ]]; then
    VERSION=`node -p -e "require('./packages/$1/package.json').version"`
    echo "$1 has not changed, old version: $VERSION"
    exit 0
fi

VERSION=$(cd packages/$1 && npm version patch --no-git-tag-version)
# Add package.json to staged
git add packages/$1/package.json > /dev/null
echo "$1 has changed, new version: ${VERSION//v}"

我在backend 包中更改了一个文件tsconfig.json 并通过Idea UI 提交它,并带有“选中运行git hooks 选项”。我只在 UI 对话框中检查文件,但钩子也应该碰撞 package.json。在 Idea 版本控制台中,我看到出现以下日志:

14:28:08.610: [myproject] git -c core.quotepath=false -c log.showSignature=false commit --only -F /private/var/folders/rf/mnfmp6xs2zjb50x0nqfrlftw0000gn/T/git-commit-msg-.txt -- packages/backend/tsconfig.json
[master c5ec828] Hooks test 24
 2 files changed, 2 insertions(+), 2 deletions(-)

运行git log -1 --stat 表示 package.json 已被钩子更改并已提交:

git log -1 --stat
commit c5ec8289afa8f15d7134b362992d4a91e31bda16 (HEAD -> master)
Author: doomsower <mail@gmail.com>
Date:   Tue Feb 13 14:28:08 2018 +0300

    Hooks test 24

 packages/backend/package.json  | 2 +-
 packages/backend/tsconfig.json | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

packages/backend/package.json 版本被钩住了,是正确的。但是,当我运行 git status 时,我看到以下内容:

On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   packages/backend/package.json

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   packages/backend/package.json

然后我运行git add packages/backend/package.json 然后git status 返回:

On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

我不太明白这里发生了什么:

1) 根据日志,Idea 运行带有--only 标志的git commit,并且在命令行中未指定package.json,但已提交。怎么可能?

2) 好的,所以提交了 2 个文件,并且提交了 package.json,但版本号有所不同。怎么可能在那之后 git 工作树不干净,我必须运行 git add 使其干净?

【问题讨论】:

    标签: git shell intellij-idea githooks


    【解决方案1】:

    所以我认为这是您上述情况的事件时间表:
    1.你更改tsconfig.json并将其添加到git索引
    2. 然后提交只有tsconfig.json 的索引。在提交运行之前,您的钩子会修改并将package.json 添加到索引中。
    3. 然后提交索引,指定只提交tsconfig.json。通常这会将package.json 保留为分阶段,但是(并且,完全披露,我在这里进行有根据的猜测)因为它被添加到钩子中 git 已经将 一些提交处理 到文件中.
    4. 新的package.json 已提交,旧的package.json 在索引中,新的package.json 在您的文件系统中。因此,将其添加回索引取消更改,因为它现在匹配提交的历史记录 - 提供一个干净的 repo。

    解决此问题的方法是从您的预提交中删除添加,并在您的提交后挂钩中运行提交,如下所示:
    git add package.json
    git commit --amend -C HEAD --no-verify
    使用--no-verify 防止死循环

    【讨论】:

    • 谢谢!您的解决方案有所帮助,只需稍作调整: --no-verify 仅禁用预提交挂钩,然后禁用提交后挂钩循环。但我能够使用以下解决方法禁用提交后循环:coderwall.com/p/dv-dgg/bypass-post-commit-hook-temporarily
    • 啊,好吧,应该检查一下。很高兴它现在为你工作:)
    猜你喜欢
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 2019-07-12
    • 2015-01-13
    • 2018-05-21
    相关资源
    最近更新 更多