代码之家  ›  专栏  ›  技术社区  ›  Konstantin Kuznetsov

使用Intellij Idea的Git预提交挂钩行为

  •  0
  • Konstantin Kuznetsov  · 技术社区  · 7 年前

    我有一个带有预提交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}"
    

    我更改了一个文件 tsconfig.json 在里面 backend 通过Idea UI打包并提交它,选中“运行git挂钩选项”。我只在UI对话框中的文件上检查这个,但钩子也应该碰撞包。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 那包裹上写着。json被hook更改并提交:

    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状态 返回值:

    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运行 git commit 具有 --only 标志和 包裹json 未在命令行中指定,但已提交。怎么可能呢?

    2) 好的,提交了2个文件 包裹json 提交的版本号不一致。在git工作树不干净之后,我必须运行 git add 把它弄干净?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Leighton    7 年前

    因此,我认为这是您上述情况下的事件时间表:
    1、你改变 tsconfig.json 并将其添加到git索引中
    2、然后提交只有 t配置。json . 在运行提交之前,钩子会修改并添加 package.json 到索引。
    然后提交索引,指定仅提交 t配置。json . 通常这会离开 包裹json 正如上演的那样,但是(而且,完全公开,我在这里进行了一个有根据的猜测),因为它被添加到了钩git has ready dome中 一些提交处理 到文件。
    这就给你留下了新的 包裹json 承诺,老 包裹json 以及文件系统上的新索引。因此将其添加回索引 取消 这一变化,因为它现在符合承诺的历史-提供了一个干净的回购。

    解决方法是从预提交中删除add,然后在提交后挂钩中运行提交,如下所示:
    git add package.json
    git commit --amend -C HEAD --no-verify
    使用 --no-verify 防止无限循环的挂钩