From 2849e2fd9882dce94f424dfa819625039621051c Mon Sep 17 00:00:00 2001
From: Christopher Bohn <bohn@unl.edu>
Date: Thu, 30 Jul 2020 16:03:44 -0500
Subject: [PATCH] Commit format sniffer now reports across all branches

---
 api/gitlab_classes.py |  9 +++++++
 commit_format_info.py | 59 +++++++++++++++++++++++++++----------------
 2 files changed, 46 insertions(+), 22 deletions(-)

diff --git a/api/gitlab_classes.py b/api/gitlab_classes.py
index efdc493..418c0a9 100644
--- a/api/gitlab_classes.py
+++ b/api/gitlab_classes.py
@@ -250,6 +250,9 @@ class GitlabCommit:
     def is_merge(self) -> bool:
         return len(self.git_commit.parent_ids) > 1
 
+    def is_revert(self) -> bool:
+        return self.get_message().startswith('Revert')
+
     def get_id(self) -> str:
         return self.git_commit.id
 
@@ -659,6 +662,12 @@ class GitlabProject:
             commits.append(GitlabCommit(commit))
         return commits
 
+    def get_branch_names(self) -> List[str]:
+        branches = self.git_project.branches.list()
+        branch_names: List[str] = list(map(lambda b: b.name, branches))
+        return branch_names
+
+
     def get_labels(self) -> Set[str]:
         """
         :return: set of label names
diff --git a/commit_format_info.py b/commit_format_info.py
index 9f8761c..4a5f38f 100644
--- a/commit_format_info.py
+++ b/commit_format_info.py
@@ -1,29 +1,44 @@
 from api.gitlab_classes import *
 from course import Course
 
+
+# noinspection PyShadowingNames
+def judge_commits(commits: List[GitlabCommit]):
+    merges = 0
+    reverts = 0
+    well_formatted_commits = 0
+    malformatted_commits = []
+    for commit in commits:
+        if commit.is_merge():
+            merges += 1
+        elif commit.is_revert():
+            reverts += 1
+        elif commit.is_well_formatted():
+            well_formatted_commits += 1
+        else:
+            malformatted_commits.append(commit)
+    print(
+        f'\t\t{merges} merges, {reverts} reverts, {well_formatted_commits} well-formatted commits, '
+        f'and {len(malformatted_commits)} malformatted commits')
+    for commit in malformatted_commits:
+        print(f'\t\t{commit.get_timestamp()} {commit.detail_formatting_problems()} ({commit.get_author()["name"]})')
+
+
 if __name__ == '__main__':
     projects = GitlabProject.get_projects_by_group(Course.gitlab_namespace)
-    projects = list(filter(lambda p: p.get_name().startswith('30pair'), projects))
+    # projects = list(filter(lambda p: p.get_name().startswith('30pair'), projects))
+    projects = list(filter(lambda p: p.get_name().startswith('36team'), projects))  # TODO: soft-code prefix
     projects.sort(key=lambda p: p.get_name())
     for project in projects:
-        commits = project.get_commits()
-        print(f'\n\n.    >>>>    {project}    <<<<')
-        print(f'{len(commits)} commits on the master branch')
-        merges = 0
-        reverts = 0
-        well_formatted_commits = 0
-        malformatted_commits = []
-        for commit in commits:
-            if commit.is_merge():
-                merges += 1
-            elif commit.get_message().startswith('Revert'):
-                reverts += 1
-            elif commit.is_well_formatted():
-                well_formatted_commits += 1
-            else:
-                malformatted_commits.append(commit)
-        print(
-            f'{merges} merges, {reverts} reverts, {well_formatted_commits} well-formatted commits, '
-            f'and {len(malformatted_commits)} malformatted commits')
-        for commit in malformatted_commits:
-            print(f'{commit.detail_formatting_problems()} ({commit.get_author()})')
+        print(f'\n\n\t>>>>    {project}    <<<<')
+        master_branch_commits = project.get_commits()
+        print(f'{len(master_branch_commits)} commits on the master branch')
+        all_branches_commits: Set[GitlabCommit] = set()
+        for branch in project.get_branch_names():
+            for commit in project.get_commits(branch):
+                all_branches_commits.add(commit)
+        print(f'{len(all_branches_commits)} commits among all branches')
+        print('\tOn the master branch:')
+        judge_commits(master_branch_commits)
+        print('\tOn all other branches:')
+        judge_commits(list(sorted(all_branches_commits - set(master_branch_commits), key=lambda c: c.get_timestamp())))
-- 
GitLab