【发布时间】: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