1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
# SPDX-License-Identifier: GPL-2.0-or-later
# Simple module for inspecting git commits
import os
import subprocess
class GitCommit:
__slots__ = (
"sha1",
# to extract more info
"_git_dir",
# cached values
"_author",
"_date",
"_body",
"_files",
"_files_status",
)
def __init__(self, sha1, git_dir):
self.sha1 = sha1
self._git_dir = git_dir
self._author = \
self._date = \
self._body = \
self._files = \
self._files_status = \
None
def cache(self):
""" Cache all properties
"""
self.author
self.date
self.body
self.files
self.files_status
def _log_format(self, format, args=()):
# sha1 = self.sha1.decode('ascii')
cmd = (
"git",
"--git-dir",
self._git_dir,
"log",
"-1", # only this rev
self.sha1,
"--format=" + format,
) + args
# print(" ".join(cmd))
p = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
)
return p.stdout.read()
@property
def sha1_short(self):
cmd = (
"git",
"--git-dir",
self._git_dir,
"rev-parse",
"--short",
self.sha1,
)
p = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
)
return p.stdout.read().strip().decode('ascii')
@property
def author(self):
ret = self._author
if ret is None:
content = self._log_format("%an")[:-1]
ret = content.decode("utf8", errors="ignore")
self._author = ret
return ret
@property
def date(self):
ret = self._date
if ret is None:
import datetime
ret = datetime.datetime.fromtimestamp(int(self._log_format("%ct")))
self._date = ret
return ret
@property
def body(self):
ret = self._body
if ret is None:
content = self._log_format("%B")[:-1]
ret = content.decode("utf8", errors="ignore")
self._body = ret
return ret
@property
def subject(self):
return self.body.lstrip().partition("\n")[0]
@property
def files(self):
ret = self._files
if ret is None:
ret = [f for f in self._log_format("format:", args=("--name-only",)).split(b"\n") if f]
self._files = ret
return ret
@property
def files_status(self):
ret = self._files_status
if ret is None:
ret = [f.split(None, 1) for f in self._log_format("format:", args=("--name-status",)).split(b"\n") if f]
self._files_status = ret
return ret
class GitCommitIter:
__slots__ = (
"_path",
"_git_dir",
"_sha1_range",
"_process",
)
def __init__(self, path, sha1_range):
self._path = path
self._git_dir = os.path.join(path, ".git")
self._sha1_range = sha1_range
self._process = None
def __iter__(self):
cmd = (
"git",
"--git-dir",
self._git_dir,
"log",
self._sha1_range,
"--format=%H",
)
# print(" ".join(cmd))
self._process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
)
return self
def __next__(self):
sha1 = self._process.stdout.readline()[:-1]
if sha1:
return GitCommit(sha1, self._git_dir)
else:
raise StopIteration
class GitRepo:
__slots__ = (
"_path",
"_git_dir",
)
def __init__(self, path):
self._path = path
self._git_dir = os.path.join(path, ".git")
@property
def branch(self):
cmd = (
"git",
"--git-dir",
self._git_dir,
"rev-parse",
"--abbrev-ref",
"HEAD",
)
# print(" ".join(cmd))
p = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
)
return p.stdout.read()
|