bug: failure in GitLab with "Error: fatal: Invalid revision range"
Steps to Reproduce
I am not sure if it is dup of #885 or not, but I've got this failure here https://gitlab.com/gitlab-community/modelops/applied-ml/code-suggestions/ai-assist/-/jobs/7430104908
$ npm install --save-dev @commitlint/config-conventional @commitlint/cli
added 125 packages in 9s
28 packages are looking for funding
run `npm fund` for details
$ echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js
$ npx commitlint --from="$CI_MERGE_REQUEST_DIFF_BASE_SHA" --help-url 'https://www.conventionalcommits.org/en'
file:///builds/gitlab-community/modelops/applied-ml/code-suggestions/ai-assist/node_modules/@commitlint/cli/lib/cli.js:127
throw err;
^
Error: fatal: Invalid revision range 6e97dc894774a3d16b9d6abf5bf762ee240abd08..HEAD
at Transform.transform [as _transform] (/builds/gitlab-community/modelops/applied-ml/code-suggestions/ai-assist/node_modules/git-raw-commits/index.js:100:36)
at Transform._write (node:internal/streams/transform:171:8)
at writeOrBuffer (node:internal/streams/writable:570:12)
at _write (node:internal/streams/writable:499:10)
at Writable.write (node:internal/streams/writable:508:10)
at Socket.ondata (node:internal/streams/readable:1007:22)
at Socket.emit (node:events:532:35)
at addChunk (node:internal/streams/readable:559:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
at Readable.push (node:internal/streams/readable:390:5)
Node.js v22.5.1
The commit from "Invalid revision range" is here https://gitlab.com/gitlab-community/modelops/applied-ml/code-suggestions/ai-assist/-/commit/6e97dc894774a3d16b9d6abf5bf762ee240abd08 and it precedes the MR, so I am not sure what this error is about.
Current Behavior
No response
Expected Behavior
Expected actionable error message.
Affected packages
- [X] cli
- [ ] core
- [ ] prompt
- [ ] config-angular
Possible Solution
No response
Context
No response
commitlint --version
i don't know
git --version
i don't know
node --version
v22.5.1
Okay. https://github.com/conventional-changelog/commitlint/issues/885#issuecomment-1092958759 pinpoints the problem.
The default git depth in GitLab pipelines is 20 (https://docs.gitlab.com/ee/ci/pipelines/settings.html#limit-the-number-of-changes-fetched-during-clone).
And there are exactly 20 commits in my MR.
So the solution to my problem is to remove git depth limitation in the lint job (https://gitlab.com/gitlab-org/modelops/applied-ml/code-suggestions/ai-assist/-/merge_requests/1143).
variables:
GIT_DEPTH: 0
The proper fix on commitlint side is to detect this condition, and show user the fix. On GitLab side - to ensure that MR default clone depth always includes parent commit.
Might be already good to add a comment to the docs first
@escapedcat https://github.com/conventional-changelog/commitlint/pull/4105
Opened an issue in GitLab for long term fix https://gitlab.com/gitlab-org/gitlab/-/issues/474701
@abitrolly I think this issue is "resolved", right? There's nothing we could do to prevent this issue on GitLab except the documentation change which happened in https://github.com/conventional-changelog/commitlint/commit/a2f27fa19b789471928889ddfca98d0c662e9b71.
@janbiasi is there is a smart way to detect how many commits does MR contain, that would be the best fix to put into docs. The chances that GitLab issue will fix itself are low.
@abitrolly according to your comment and the GitLab Docs it is 20 - this is also already stated in the docs (see below). Hence my comment to close this issue as we can't do more than document that.
https://github.com/conventional-changelog/commitlint/blob/0c589976977b74c112405becc57c70af8dc6ab3d/docs/guides/ci-setup.md?plain=1#L139-L141
cc @escapedcat
@janbiasi the commitlint can request any revision length. Chances that users will discover this issue are low, so regardless of documentation, the unnecessary traction will still disrupt people workflow.
If it is impossible to detect the exact number of commits in GitLab MR, commitlint can fail gracefully. But I believe the git branchpoint information is still present in some CI environment variable. So the logic could be:
if err.Error() == "Error: fatal: Invalid revision range" && ci == "gitlab"{
if undefined(branch_commits) and len(log) == 20 {
exit("Error: commit XXXXX is out of default GitLab fetch range (20), see https://github.com/conventional-changelog/commitlint/issues/4103 how to increase it")
}
if len(log) < len(branch_commits) {
exec(`git fetch --depth ' + len(branch_commits))
retry
}
}
@abitrolly each CI handles this differently, so you would need to implement workarounds for each CI and their corresponding git implementation (= not commitlint). As an example; with GitHub you can't request a further depth in commits than specified within checkout action unless your providing a custom GH token with specific access. This would lead to inconsistencies in terms of CI usage, which is more confusing and prone to invalid setups than the current state.
Additionaly - at least in my opinion - this is not within the commitlint scope, as the commits are a prerequisite outside of commitlint itself and provided by git.
I'd be happy to hear what your opinion is @escapedcat
Let's close this for now. I more people run into this we can look into it again.
So far we're good with docs I guess.