Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ gitlint --commits "origin..HEAD"
gitlint --commits 019cf40,c50eb150,d6bc75a
# These can include special references as well
gitlint --commits HEAD~1,mybranch-name,origin/main,d6bc75a
# You can also lint a single commit with --commits:
gitling --commits 019cf40,
```

The `--commits` flag takes a **single** refspec argument or commit range. Basically, any range that is understood
Expand All @@ -332,6 +334,7 @@ by [git rev-list](https://git-scm.com/docs/git-rev-list) as a single argument wi
Alternatively, you can pass `--commits` a comma-separated list of commit hashes (both short and full-length SHAs work,
as well as special references such as `HEAD` and branch names).
Gitlint will treat these as pointers to **single** commits and lint these in the order you passed.
`--commits` also accepts a single commit SHA with a trailing comma.

For cases where the `--commits` option doesn't provide the flexibility you need, you can always use a simple shell
script to lint an arbitrary set of commits, like shown in the example below.
Expand Down
2 changes: 1 addition & 1 deletion gitlint-core/gitlint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def from_commit_msg(message):
if refspec:
# 3.1.1 Not real refspec, but comma-separated list of commit hashes
if "," in refspec:
commit_hashes = [hash.strip() for hash in refspec.split(",")]
commit_hashes = [hash.strip() for hash in refspec.split(",") if hash]
return GitContext.from_local_repository(lint_config.target, commit_hashes=commit_hashes)
# 3.1.2 Real refspec
return GitContext.from_local_repository(lint_config.target, refspec=refspec)
Expand Down
5 changes: 5 additions & 0 deletions qa/test_commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ def test_lint_single_commit(self):
self.assertEqual(output.exit_code, 2)
self.assertEqualStdout(output, expected)

# Lint using --commits <commit sha>,
output = gitlint("--commits", f"{commit_sha},", _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[2])
self.assertEqual(output.exit_code, 2)
self.assertEqualStdout(output, expected)

# Lint a single commit using --commits <refspec> pointing to the single commit
output = gitlint("--commits", refspec, _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[2])
self.assertEqual(output.exit_code, 2)
Expand Down