summaryrefslogtreecommitdiffstats
path: root/gitlint/git.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2021-10-13 05:34:54 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2021-10-13 05:34:54 +0000
commitb8d423e7d13686d6627571d6c4adf12661d82147 (patch)
tree11d64ff26fb53c3c01ee35d062ca0c51fb883550 /gitlint/git.py
parentAdding upstream version 0.15.1. (diff)
downloadgitlint-b8d423e7d13686d6627571d6c4adf12661d82147.tar.xz
gitlint-b8d423e7d13686d6627571d6c4adf12661d82147.zip
Adding upstream version 0.16.0.upstream/0.16.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'gitlint/git.py')
-rw-r--r--gitlint/git.py17
1 files changed, 11 insertions, 6 deletions
diff --git a/gitlint/git.py b/gitlint/git.py
index a9609d0..773c7b2 100644
--- a/gitlint/git.py
+++ b/gitlint/git.py
@@ -364,22 +364,27 @@ class GitContext(PropertyCache):
return context
@staticmethod
- def from_local_repository(repository_path, refspec=None):
+ def from_local_repository(repository_path, refspec=None, commit_hash=None):
""" Retrieves the git context from a local git repository.
:param repository_path: Path to the git repository to retrieve the context from
- :param refspec: The commit(s) to retrieve
+ :param refspec: The commit(s) to retrieve (mutually exclusive with `commit_sha`)
+ :param commit_hash: Hash of the commit to retrieve (mutually exclusive with `refspec`)
"""
context = GitContext(repository_path=repository_path)
- # If no refspec is defined, fallback to the last commit on the current branch
- if refspec is None:
+ if refspec:
+ sha_list = _git("rev-list", refspec, _cwd=repository_path).split()
+ elif commit_hash: # Single commit, just pass it to `git log -1`
+ # Even though we have already been passed the commit hash, we ask git to retrieve this hash and
+ # return it to us. This way we verify that the passed hash is a valid hash for the target repo and we
+ # also convert it to the full hash format (we might have been passed a short hash).
+ sha_list = [_git("log", "-1", commit_hash, "--pretty=%H", _cwd=repository_path).replace("\n", "")]
+ else: # If no refspec is defined, fallback to the last commit on the current branch
# We tried many things here e.g.: defaulting to e.g. HEAD or HEAD^... (incl. dealing with
# repos that only have a single commit - HEAD^... doesn't work there), but then we still get into
# problems with e.g. merge commits. Easiest solution is just taking the SHA from `git log -1`.
sha_list = [_git("log", "-1", "--pretty=%H", _cwd=repository_path).replace("\n", "")]
- else:
- sha_list = _git("rev-list", refspec, _cwd=repository_path).split()
for sha in sha_list:
commit = LocalGitCommit(context, sha)