return False
-class GitCommit:
- def __init__(self, hexsha, date, author, body, modified_files,
- strict=True, commit_to_date_hook=None):
+class GitInfo:
+ def __init__(self, hexsha, date, author, lines, modified_files):
self.hexsha = hexsha
- self.lines = body
+ self.date = date
+ self.author = author
+ self.lines = lines
self.modified_files = modified_files
+
+
+class GitCommit:
+ def __init__(self, info, strict=True, commit_to_info_hook=None):
+ self.info = info
self.message = None
self.changes = None
self.changelog_entries = []
self.errors = []
- self.date = date
- self.author = author
self.top_level_authors = []
self.co_authors = []
self.top_level_prs = []
self.cherry_pick_commit = None
- self.commit_to_date_hook = commit_to_date_hook
+ self.commit_to_info_hook = commit_to_info_hook
- project_files = [f for f in self.modified_files
+ project_files = [f for f in self.info.modified_files
if self.is_changelog_filename(f[0])
or f[0] in misc_files]
- ignored_files = [f for f in self.modified_files
+ ignored_files = [f for f in self.info.modified_files
if self.in_ignored_location(f[0])]
- if len(project_files) == len(self.modified_files):
+ if len(project_files) == len(self.info.modified_files):
# All modified files are only MISC files
return
elif project_files and strict:
return
all_are_ignored = (len(project_files) + len(ignored_files)
- == len(self.modified_files))
+ == len(self.info.modified_files))
self.parse_lines(all_are_ignored)
if self.changes:
self.parse_changelog()
@property
def new_files(self):
- return [x[0] for x in self.modified_files if x[1] == 'A']
+ return [x[0] for x in self.info.modified_files if x[1] == 'A']
@classmethod
def is_changelog_filename(cls, path):
return modified_files
def parse_lines(self, all_are_ignored):
- body = self.lines
+ body = self.info.lines
for i, b in enumerate(body):
if not b:
self.errors.append(Error(msg, line))
def get_file_changelog_location(self, changelog_file):
- for file in self.modified_files:
+ for file in self.info.modified_files:
if file[0] == changelog_file:
# root ChangeLog file
return ''
for pattern in entry.file_patterns:
mentioned_patterns.append(os.path.join(entry.folder, pattern))
- cand = [x[0] for x in self.modified_files
+ cand = [x[0] for x in self.info.modified_files
if not self.is_changelog_filename(x[0])]
changed_files = set(cand)
for file in sorted(mentioned_files - changed_files):
return output
def to_changelog_entries(self, use_commit_ts=False):
- current_timestamp = self.date.strftime(DATE_FORMAT)
+ current_timestamp = self.info.date.strftime(DATE_FORMAT)
for entry in self.changelog_entries:
output = ''
timestamp = entry.datetime
if self.cherry_pick_commit:
- timestamp = self.commit_to_date_hook(self.cherry_pick_commit)
+ info = self.commit_to_info_hook(self.cherry_pick_commit)
# it can happen that it is a cherry-pick for a different
# repository
- if timestamp:
- timestamp = timestamp.strftime(DATE_FORMAT)
+ if info:
+ timestamp = info.date.strftime(DATE_FORMAT)
else:
timestamp = current_timestamp
elif not timestamp or use_commit_ts:
timestamp = current_timestamp
- authors = entry.authors if entry.authors else [self.author]
+ authors = entry.authors if entry.authors else [self.info.author]
# add Co-Authored-By authors to all ChangeLog entries
for author in self.co_authors:
if author not in authors:
authors.append(author)
if self.cherry_pick_commit:
- output += self.format_authors_in_changelog([self.author],
+ output += self.format_authors_in_changelog([self.info.author],
current_timestamp)
output += '\tBackported from master:\n'
output += self.format_authors_in_changelog(authors,
from dateutil.parser import parse
-from git_commit import GitCommit
+from git_commit import GitCommit, GitInfo
from unidiff import PatchSet
else:
t = 'M'
modified_files.append((target, t))
- super().__init__(None, date, author, body, modified_files,
- strict=strict, commit_to_date_hook=lambda x: date)
+ git_info = GitInfo(None, date, author, body, modified_files)
+ super().__init__(git_info, strict=strict,
+ commit_to_info_hook=lambda x: None)
# With zero arguments, process every patch file in the ./patches directory.
print('OK')
email.print_output()
else:
- if not email.lines:
+ if not email.info.lines:
print('Error: patch contains no parsed lines', file=sys.stderr)
email.print_errors()
sys.exit(1)
print(' Debian, Ubuntu: python3-git')
exit(1)
-from git_commit import GitCommit
+from git_commit import GitCommit, GitInfo
def parse_git_revisions(repo_path, revisions, strict=False):
repo = Repo(repo_path)
- def commit_to_date(commit):
+ def commit_to_info(commit):
try:
c = repo.commit(commit)
- return datetime.utcfromtimestamp(c.committed_date)
+ diff = repo.commit(commit + '~').diff(commit)
+
+ modified_files = []
+ for file in diff:
+ if file.new_file:
+ t = 'A'
+ elif file.deleted_file:
+ t = 'D'
+ elif file.renamed_file:
+ # Consider that renamed files are two operations:
+ # the deletion of the original name
+ # and the addition of the new one.
+ modified_files.append((file.a_path, 'D'))
+ t = 'A'
+ else:
+ t = 'M'
+ modified_files.append((file.b_path, t))
+
+ date = datetime.utcfromtimestamp(c.committed_date)
+ author = '%s <%s>' % (c.author.name, c.author.email)
+ git_info = GitInfo(c.hexsha, date, author,
+ c.message.split('\n'), modified_files)
+ return git_info
except ValueError:
return None
commits = [repo.commit(revisions)]
for commit in commits:
- diff = repo.commit(commit.hexsha + '~').diff(commit.hexsha)
-
- modified_files = []
- for file in diff:
- if file.new_file:
- t = 'A'
- elif file.deleted_file:
- t = 'D'
- elif file.renamed_file:
- # Consider that renamed files are two operations: the deletion
- # of the original name and the addition of the new one.
- modified_files.append((file.a_path, 'D'))
- t = 'A'
- else:
- t = 'M'
- modified_files.append((file.b_path, t))
-
- date = datetime.utcfromtimestamp(commit.committed_date)
- author = '%s <%s>' % (commit.author.name, commit.author.email)
- git_commit = GitCommit(commit.hexsha, date, author,
- commit.message.split('\n'), modified_files,
- strict=strict,
- commit_to_date_hook=commit_to_date)
+ git_commit = GitCommit(commit_to_info(commit.hexsha), strict=strict,
+ commit_to_info_hook=commit_to_info)
parsed_commits.append(git_commit)
return parsed_commits