gcc-changelog: come up with GitInfo wrapper.
authorMartin Liska <mliska@suse.cz>
Tue, 30 Jun 2020 08:12:45 +0000 (10:12 +0200)
committerMartin Liska <mliska@suse.cz>
Tue, 30 Jun 2020 08:56:05 +0000 (10:56 +0200)
contrib/ChangeLog:

* gcc-changelog/git_check_commit.py: Use GitInfo
* gcc-changelog/git_commit.py: Add GitInfo class.
* gcc-changelog/git_email.py: Use GitInfo class.
* gcc-changelog/git_repository.py: Likewise.

contrib/gcc-changelog/git_check_commit.py
contrib/gcc-changelog/git_commit.py
contrib/gcc-changelog/git_email.py
contrib/gcc-changelog/git_repository.py

index 2601ae4f613d3a2e205d0d5102c3872a05f28d08..ab6da05744a7f3bbbf1e03cef521429668d77ba5 100755 (executable)
@@ -37,7 +37,7 @@ retval = 0
 for git_commit in parse_git_revisions(args.git_path, args.revisions,
                                       not args.non_strict_mode):
     res = 'OK' if git_commit.success else 'FAILED'
-    print('Checking %s: %s' % (git_commit.hexsha, res))
+    print('Checking %s: %s' % (git_commit.info.hexsha, res))
     if git_commit.success:
         if args.print_changelog:
             git_commit.print_output()
index 4a78793b87e8eb45e32d632d64caf31802bff1f8..9d821a8940dd28e77cab8bab9a7a830e2c493319 100755 (executable)
@@ -245,30 +245,34 @@ class ChangeLogEntry:
         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:
@@ -278,7 +282,7 @@ class GitCommit:
             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()
@@ -296,7 +300,7 @@ class GitCommit:
 
     @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):
@@ -331,7 +335,7 @@ class GitCommit:
         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:
@@ -475,7 +479,7 @@ class GitCommit:
                     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 ''
@@ -538,7 +542,7 @@ class GitCommit:
             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):
@@ -609,28 +613,28 @@ class GitCommit:
         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,
index 2083d7b7ec9af3904b4f2ab3299ccac33b4933bf..014fdd1004b4b04ba67fb87a72a9feb59debcc31 100755 (executable)
@@ -22,7 +22,7 @@ from itertools import takewhile
 
 from dateutil.parser import parse
 
-from git_commit import GitCommit
+from git_commit import GitCommit, GitInfo
 
 from unidiff import PatchSet
 
@@ -66,8 +66,9 @@ class GitEmail(GitCommit):
             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.
@@ -100,7 +101,7 @@ if __name__ == '__main__':
             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)
index a419bd97701b3598d792ef704b2c4dfd37c98312..4f0d21af03965e7376402080edf4aec1fbc82e65 100755 (executable)
@@ -26,16 +26,38 @@ except ImportError:
     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
 
@@ -46,28 +68,7 @@ def parse_git_revisions(repo_path, revisions, strict=False):
         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