我有一个带有预提交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
把它弄干净?