commitizen / commitizen/cz-cli

Fails in hook mode when package is a subdirectory of git repo

Đang mở
#683 4 bình luận 12 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
JavaScript
Star
17.5k
Fork
566
Merge trung bình
8 giờ 16 phút
Pull request đã merge (30 ngày)
1

Mô tả

### Scenario:

Everything Node.JS lives within a subfolder e.g myGitRepo/source/package.json:
```
"devDependencies": {
"commitizen": "^4.0.3",
"cz-conventional-changelog": "^3.0.2",
"husky": "^3.0.9"
},
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
}
},
"husky": {
"hooks": {
"prepare-commit-msg": "exec < /dev/tty && npx git cz --hook || true"
}
}
```

Overriding `config.commitizen.path` to the plain npm module name `cz-conventional-changelog` (as per #469) is sufficient to get `npx git cz` working, BUT...

### Problem:

Trying to use the husky hook (which is from the README) will throw:
```
myGitRepo/source $ git commit
# (Prompting works fine, but then...)
Error: ENOENT: no such file or directory, open '/myCoolParentFolder/myGitRepo/source/.git/COMMIT_EDITMSG
```

Note that it's always taking the package.json folder, not the current working directory e.g:
```
myGitRepo/source/packages $ git commit
# (Also prompts fine, but tries to write to same wrong .git folder...)
Error: ENOENT: no such file or directory, open '/myCoolParentFolder/myGitRepo/source/.git/COMMIT_EDITMSG
```

It doesn't seem to be possible to override via environment variables (e.g. by messing with `GIT_DIR` in the husky hook statement like they do [here](https://github.com/typicode/husky/issues/364) or [here](https://github.com/typicode/husky/issues/584)), because [cli/strategies/git-cz.js](https://github.com/commitizen/cz-cli/blob/master/src/cli/strategies/git-cz.js) explicitly calls `commitizen.commit` with `repoPath = process.cwd()`:

```
commit(sh, inquirer, process.cwd(), prompter, {
args: parsedGitCzArgs,
disableAppendPaths: true,
emitData: true,
quiet: false,
retryLastCommit,
hookMode
}, function (error) {
if (error) {
throw error;
}
});
```

...and [git/commit.js](https://github.com/commitizen/cz-cli/blob/master/src/git/commit.js) uses that repoPath to store the captured message:
```
const commitFilePath = path.join(repoPath, '/.git/COMMIT_EDITMSG');
```

This issue only affects running in hook mode, because in standard `git cz` we just pass the message through as an argument to a spawned git process, which manages to figure out that it's in a subfolder of a repository.

### Fix Investigation:

I had a look around to try and figure what a fix might look like, but I don't really understand why `cliPath` and `repoPath` are tracked as separate variables when the CLI strategy seems to just force `repoPath` to the package root?

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.