From 4d30d46345e43757af50f3fc6013a7e2d23b320e Mon Sep 17 00:00:00 2001 From: Carles Cufi Date: Mon, 16 Jan 2023 12:44:34 +0100 Subject: [PATCH] cli: Allow for a single commit in the --commits cmd-line param In order to avoid applications needing to select between `--commit` and `--commits` based on the number of commits, allow for providing a single commit with `--commits` in the format: $ gitlint --commits , The trailing comma ensures that gitlint knows this is a commit list and not a refspec. Signed-off-by: Carles Cufi --- docs/index.md | 3 +++ gitlint-core/gitlint/cli.py | 2 +- qa/test_commits.py | 5 +++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/index.md b/docs/index.md index 521242f4..b79f8d62 100644 --- a/docs/index.md +++ b/docs/index.md @@ -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 @@ -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. diff --git a/gitlint-core/gitlint/cli.py b/gitlint-core/gitlint/cli.py index 396885b1..d28d373c 100644 --- a/gitlint-core/gitlint/cli.py +++ b/gitlint-core/gitlint/cli.py @@ -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) diff --git a/qa/test_commits.py b/qa/test_commits.py index ec192e9d..11d1851e 100644 --- a/qa/test_commits.py +++ b/qa/test_commits.py @@ -110,6 +110,11 @@ def test_lint_single_commit(self): self.assertEqual(output.exit_code, 2) self.assertEqualStdout(output, expected) + # Lint using --commits , + 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 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)