A git commit hash and its one-line message.
| 77 | |
| 78 | |
| 79 | class Commit: |
| 80 | """A git commit hash and its one-line message.""" |
| 81 | |
| 82 | def __init__(self, hash: str, message: str = ""): |
| 83 | """Creates a new Commit with the given hash. |
| 84 | |
| 85 | Args: |
| 86 | hash: The hexadecimal hash of the commit. |
| 87 | message: The one-line summary of the commit. If empty, this method |
| 88 | will ask git for the commit message. |
| 89 | """ |
| 90 | self.hash = hash.strip() |
| 91 | if not message: |
| 92 | # Ask git for the commit message. |
| 93 | lines = run_git(["log", "-1", "--pretty=%s", self.hash]) |
| 94 | # Should just be one line, but could be zero. |
| 95 | message = " ".join(lines) |
| 96 | self.message = message.strip() |
| 97 | |
| 98 | @staticmethod |
| 99 | def from_line(line: str) -> "Commit": |
| 100 | """Creates a Commit from a string of the form '<hash> [<message>]'.""" |
| 101 | parts = line.split(" ", maxsplit=1) |
| 102 | parts = [part.strip() for part in parts if part.strip()] |
| 103 | assert len(parts) >= 1, f"Expected at least one part in line '{line}'" |
| 104 | return Commit(hash=parts[0], message=parts[1] if len(parts) > 1 else "") |
| 105 | |
| 106 | def __repr__(self): |
| 107 | return f"Commit('{self.hash[:8]}', '{self.message}')" |
| 108 | |
| 109 | def __str__(self): |
| 110 | return f"{self.hash[:8]} {self.message}" |
| 111 | |
| 112 | |
| 113 | def is_doc_only_commit(commit: Commit) -> bool: |