Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | ## |
| 4 | # Copyright c 2016 IBM Corporation |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | ## |
| 18 | |
| 19 | ############################################################################### |
| 20 | # @file commit-tracker |
| 21 | # @brief Prints out all commits on the master branch of the specified |
| 22 | # repository, as well as all commits on linked submodule |
| 23 | # repositories |
| 24 | ############################################################################### |
| 25 | |
Charles Hofer | 046fc91 | 2016-11-10 10:42:13 -0600 | [diff] [blame] | 26 | import argparse |
Charles Hofer | ff54ae9 | 2016-11-29 16:22:39 -0600 | [diff] [blame] | 27 | import git |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 28 | import logging |
| 29 | import os |
| 30 | import re |
Charles Hofer | ff54ae9 | 2016-11-29 16:22:39 -0600 | [diff] [blame] | 31 | import requests |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 32 | import sys |
| 33 | import time |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 34 | |
| 35 | ############################################################################### |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 36 | # @class CommitReport |
| 37 | # @brief A class representing information about a commit and all commits in |
| 38 | # relevant subrepos |
| 39 | ############################################################################### |
| 40 | class CommitReport: |
| 41 | def __init__(self, i_repo_uri, i_repo_name, i_sha, i_nice_name, |
Charles Hofer | ff54ae9 | 2016-11-29 16:22:39 -0600 | [diff] [blame] | 42 | i_summary, i_insertions, i_deletions, i_closed_issues): |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 43 | self.repo_uri = i_repo_uri |
| 44 | self.repo_name = i_repo_name |
| 45 | self.sha = i_sha |
| 46 | self.nice_name = i_nice_name |
| 47 | self.summary = i_summary |
| 48 | self.insertions = i_insertions |
| 49 | self.deletions = i_deletions |
Charles Hofer | ff54ae9 | 2016-11-29 16:22:39 -0600 | [diff] [blame] | 50 | self.closed_issues = i_closed_issues |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 51 | self.subreports = [] |
| 52 | |
| 53 | def to_cl_string(self, i_level=0): |
| 54 | # Define colors for the console |
| 55 | RED = '\033[31m' |
| 56 | BLUE = '\033[94m' |
| 57 | ENDC = '\033[0m' |
| 58 | # Put the string together |
| 59 | l_cl_string = (' ' * i_level) + RED + self.repo_name + ENDC + ' ' \ |
| 60 | + BLUE + self.nice_name + ENDC + ' ' \ |
| 61 | + re.sub('\s+', ' ', self.summary) |
| 62 | # Do the same for every subreport |
| 63 | for l_report in self.subreports: |
| 64 | l_cl_string += '\n' + l_report.to_cl_string(i_level + 1) |
| 65 | return l_cl_string |
| 66 | |
Charles Hofer | ec6b041 | 2016-11-29 14:15:24 -0600 | [diff] [blame^] | 67 | def to_html(self, i_level=0): |
| 68 | l_repo_url = re.sub('git://', 'http://', self.repo_uri) |
| 69 | # Get HTML for this commit |
| 70 | l_html = \ |
| 71 | '<div style="margin-left: ' + str(i_level * 20) + 'px">' \ |
| 72 | + '<a href="' + l_repo_url + '" target="_blank" ' \ |
| 73 | + 'style="color: red">' + self.repo_name + '</a> ' \ |
| 74 | + '<a href="' + l_repo_url + '/commit/' + self.sha \ |
| 75 | + '" target="_blank" style="color: blue">' + self.nice_name \ |
| 76 | + '</a> ' \ |
| 77 | + '<span>' + re.sub('\s+', ' ', self.summary) + '</span>' \ |
| 78 | + '</div>\n' |
| 79 | # Get the HTML for all subcommits |
| 80 | for l_commit in self.subreports: |
| 81 | l_html += l_commit.to_html(i_level + 1) |
| 82 | return l_html |
| 83 | |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 84 | def get_total_insertions(self): |
| 85 | l_insertions = self.insertions |
| 86 | for l_commit in self.subreports: |
| 87 | l_insertions += l_commit.get_total_insertions() |
| 88 | return l_insertions |
| 89 | |
| 90 | def get_total_deletions(self): |
| 91 | l_deletions = self.deletions |
| 92 | for l_commit in self.subreports: |
| 93 | l_deletions += l_commit.get_total_deletions() |
| 94 | return l_deletions |
| 95 | |
Charles Hofer | ff54ae9 | 2016-11-29 16:22:39 -0600 | [diff] [blame] | 96 | def get_all_closed_issues(self): |
| 97 | l_closed_issues = self.closed_issues |
| 98 | for l_commit in self.subreports: |
| 99 | l_closed_issues.extend(l_commit.get_all_closed_issues()) |
| 100 | return l_closed_issues |
| 101 | |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 102 | ############################################################################### |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 103 | # @brief Main function for the script |
| 104 | # |
| 105 | # @param i_args : Command line arguments |
| 106 | ############################################################################### |
| 107 | def main(i_args): |
Charles Hofer | 046fc91 | 2016-11-10 10:42:13 -0600 | [diff] [blame] | 108 | # Parse the arguments |
Charles Hofer | ec6b041 | 2016-11-29 14:15:24 -0600 | [diff] [blame^] | 109 | l_args = parse_arguments(i_args) |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 110 | |
Charles Hofer | ec6b041 | 2016-11-29 14:15:24 -0600 | [diff] [blame^] | 111 | # Set the logger level |
| 112 | logging.basicConfig(level=logging.ERROR) |
| 113 | |
| 114 | # Generate the commit reports |
| 115 | print 'Getting report for ' + l_args.repo_dir |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 116 | l_reports = generate_commit_reports( |
Charles Hofer | ec6b041 | 2016-11-29 14:15:24 -0600 | [diff] [blame^] | 117 | l_args.repo_uri, |
| 118 | l_args.repo_dir, |
| 119 | l_args.latest_commit, |
| 120 | l_args.earliest_commit) |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 121 | |
| 122 | # Compile issues, insertions, and deletions |
Charles Hofer | ff54ae9 | 2016-11-29 16:22:39 -0600 | [diff] [blame] | 123 | l_issues = [] |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 124 | l_total_deletions = 0 |
| 125 | l_total_insertions = 0 |
| 126 | for l_report in l_reports: |
| 127 | l_total_deletions += l_report.get_total_deletions() |
| 128 | l_total_insertions += l_report.get_total_insertions() |
Charles Hofer | ff54ae9 | 2016-11-29 16:22:39 -0600 | [diff] [blame] | 129 | l_issues.extend(l_report.get_all_closed_issues()) |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 130 | |
| 131 | # Print commit information to the console |
Charles Hofer | ec6b041 | 2016-11-29 14:15:24 -0600 | [diff] [blame^] | 132 | print 'Commits...' |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 133 | for l_report in l_reports: |
| 134 | print l_report.to_cl_string() |
Charles Hofer | ff54ae9 | 2016-11-29 16:22:39 -0600 | [diff] [blame] | 135 | print 'Closed issues...' |
| 136 | for l_issue in l_issues: |
| 137 | print ' ' + str(l_issue[0]) + ' ' + str(l_issue[1]) |
Charles Hofer | ec6b041 | 2016-11-29 14:15:24 -0600 | [diff] [blame^] | 138 | print 'Insertions and deletions...' |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 139 | print str(l_total_insertions) + ' insertions' |
| 140 | print str(l_total_deletions) + ' deletions' |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 141 | |
Charles Hofer | ec6b041 | 2016-11-29 14:15:24 -0600 | [diff] [blame^] | 142 | # Write to the HTML file if the user set the flag |
| 143 | if l_args.html_file: |
| 144 | print 'Writing to HTML file...' |
| 145 | l_html_file = open(l_args.html_file, 'w+') |
| 146 | l_html_file.write('<html><body>\n') |
| 147 | for l_report in l_reports: |
| 148 | l_html_file.write(l_report.to_html()) |
| 149 | l_html_file.write('<p>' + str(l_total_insertions) \ |
| 150 | + ' insertions and ' + str(l_total_deletions) \ |
| 151 | + ' deletions</p>') |
| 152 | l_html_file.write('<div>Closed Issues</div>') |
| 153 | for l_issue in l_issues: |
| 154 | l_html_file.write('<div><a href="http://www.github.com/' \ |
| 155 | + re.sub('#', '/issues/', l_issue[0]) \ |
| 156 | + '" target="_blank">' + l_issue[0] + '</a> ' \ |
| 157 | + l_issue[1] + '</div>') |
| 158 | l_html_file.write('</body></html>') |
| 159 | l_html_file.close() |
| 160 | |
Charles Hofer | 046fc91 | 2016-11-10 10:42:13 -0600 | [diff] [blame] | 161 | ############################################################################### |
| 162 | # @brief Parses the arguments from the command line |
| 163 | # |
| 164 | # @param i_args : The list of arguments from the command line, excluding the |
| 165 | # name of the script |
| 166 | # |
| 167 | # @return An object representin the parsed arguments |
| 168 | ############################################################################### |
| 169 | def parse_arguments(i_args): |
| 170 | l_parser = argparse.ArgumentParser( |
| 171 | description='Prints commit information from the given repo and all ' \ |
| 172 | +'sub-repos specified with SRC_REV, starting from the ' \ |
| 173 | +'most recent commit specified going back to the ' \ |
| 174 | +'earliest commit specified.') |
| 175 | l_parser.add_argument( |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 176 | 'repo_uri', |
| 177 | help='The URI of the repo to get commit information for') |
| 178 | l_parser.add_argument( |
Charles Hofer | 046fc91 | 2016-11-10 10:42:13 -0600 | [diff] [blame] | 179 | 'repo_dir', |
| 180 | help='The directory of the repo to get commit information for') |
| 181 | l_parser.add_argument( |
| 182 | 'latest_commit', |
| 183 | help='A reference (branch name, HEAD, SHA, etc.) to the most ' \ |
| 184 | +'recent commit to get information for') |
| 185 | l_parser.add_argument( |
| 186 | 'earliest_commit', |
| 187 | help='A reference to the earliest commit to get information for') |
Charles Hofer | ec6b041 | 2016-11-29 14:15:24 -0600 | [diff] [blame^] | 188 | l_parser.add_argument( |
| 189 | '--html_file', |
| 190 | default=None, |
| 191 | help='If set to a file path, this script will write an HTML ' \ |
| 192 | +'version of the console output to the file path given') |
Charles Hofer | 046fc91 | 2016-11-10 10:42:13 -0600 | [diff] [blame] | 193 | return l_parser.parse_args(i_args) |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 194 | |
| 195 | ############################################################################### |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 196 | # @brief Generates a list of CommitReport objects, each one |
| 197 | # representing a commit in the given repo URI and path, |
| 198 | # starting at the beginning commit inclusive, ending at the |
| 199 | # end commit exclusive |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 200 | # |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 201 | # @param i_repo_uri : The URI to the repo to get reports for |
| 202 | # @param i_repo_path : The path to the repo to get reports for |
| 203 | # @param i_begin_commit : A reference to the most recent commit. The |
| 204 | # most recent commit to get a report for |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 205 | # @param i_end_commit : A reference to the commit farthest in the |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 206 | # past. The next youngest commit will be |
| 207 | # the last one to get a report for |
| 208 | # |
| 209 | # @return A list of CommitReport objects in order from newest to |
| 210 | # oldest commit |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 211 | ############################################################################### |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 212 | def generate_commit_reports(i_repo_uri, i_repo_path, i_begin_commit, |
| 213 | i_end_commit): |
| 214 | # Get the repo that the user requested |
| 215 | clone_or_update(i_repo_uri, i_repo_path) |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 216 | try: |
| 217 | l_repo = git.Repo(i_repo_path) |
| 218 | except git.exc.InvalidGitRepositoryError: |
| 219 | logging.error(str(i_repo_path) + ' is not a valid git repository') |
| 220 | return |
| 221 | |
| 222 | # Get commits between the beginning and end references |
| 223 | try: |
| 224 | l_commits = l_repo.iter_commits(rev=(i_begin_commit + '...' |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 225 | + i_end_commit)) |
| 226 | # Go through each commit, generating a report |
| 227 | l_reports = [] |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 228 | for l_commit in l_commits: |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 229 | # Get the insertion and deletion line counts |
| 230 | l_insertions, l_deletions = get_line_count( |
| 231 | l_repo,str(l_commit.hexsha), |
| 232 | str(l_commit.hexsha) + '~1') |
| 233 | # Construct a new commit report |
| 234 | l_report = CommitReport( |
| 235 | i_repo_uri, |
| 236 | i_repo_path.split('/')[-1].replace('.git', ''), |
| 237 | str(l_commit.hexsha), |
| 238 | to_prefix_name_rev(l_commit.name_rev), |
| 239 | l_commit.summary, |
| 240 | l_insertions, |
Charles Hofer | ff54ae9 | 2016-11-29 16:22:39 -0600 | [diff] [blame] | 241 | l_deletions, |
| 242 | get_closed_issues(l_commit)) |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 243 | |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 244 | # Search the diffs for any bumps of submodule versions |
| 245 | l_diffs = l_commit.diff(str(l_commit.hexsha) + '~1') |
| 246 | for l_diff in l_diffs: |
| 247 | # If we have two files to compare with diff... |
| 248 | if l_diff.a_path and l_diff.b_path: |
| 249 | # ... get info about the change, log it... |
| 250 | l_subrepo_uri, l_subrepo_new_hash, l_subrepo_old_hash \ |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 251 | = get_bump_info(l_repo, str(l_commit.hexsha), |
| 252 | i_repo_path, l_diff.b_path) |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 253 | logging.debug('Found diff...') |
| 254 | logging.debug(' Subrepo URI: ' + str(l_subrepo_uri)) |
| 255 | logging.debug(' Subrepo new hash: ' |
| 256 | + str(l_subrepo_new_hash)) |
| 257 | logging.debug(' Subrepo old hash: ' |
| 258 | + str(l_subrepo_old_hash)) |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 259 | logging.debug(' Found in: ' + str(l_diff.b_path)) |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 260 | # ... and print the commits for the subrepo if this was a |
| 261 | # version bump |
| 262 | if (l_subrepo_new_hash |
| 263 | and l_subrepo_old_hash |
| 264 | and l_subrepo_uri |
| 265 | and l_subrepo_uri.startswith('git')): |
| 266 | logging.debug(' Bumped') |
| 267 | l_subrepo_path = l_subrepo_uri.split('/')[-1] |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 268 | l_subreports = generate_commit_reports( |
| 269 | l_subrepo_uri, |
| 270 | l_subrepo_path, |
| 271 | l_subrepo_new_hash, |
| 272 | l_subrepo_old_hash) |
| 273 | l_report.subreports.extend(l_subreports) |
| 274 | |
| 275 | # Put the report on the end of the list |
| 276 | l_reports.append(l_report) |
| 277 | |
| 278 | except git.exc.GitCommandError as e: |
| 279 | logging.error(e) |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 280 | logging.error(str(i_begin_commit) + ' and ' + str(i_end_commit) |
| 281 | + ' are invalid revisions') |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 282 | return l_reports |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 283 | |
| 284 | ############################################################################### |
| 285 | # @brief Gets the repo URI, the updated SHA, and the old SHA from a |
| 286 | # given repo, commit SHA and file |
| 287 | # |
| 288 | # @param i_repo : The Repo object to get version bump information |
| 289 | # from |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 290 | # @param i_hexsha : The hex hash for the commit to search for |
| 291 | # version bumps |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 292 | # @param i_repo_path : The path to the repo containing the file to |
| 293 | # get bump information from |
| 294 | # @param i_file : The path, starting at the base of the repo, |
| 295 | # to the file to get bump information from |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 296 | # |
| 297 | # @return Returns the repo URI, the updated SHA, and the old SHA in |
| 298 | # a tuple in that order |
| 299 | ############################################################################### |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 300 | def get_bump_info(i_repo, i_hexsha, i_repo_path, i_file): |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 301 | # Checkout the old repo |
| 302 | i_repo.git.checkout(i_hexsha) |
| 303 | # Get the diff text |
| 304 | l_diff_text = i_repo.git.diff(i_hexsha, i_hexsha + '~1', '--', i_file) |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 305 | logging.debug('Hash: ' + i_hexsha) |
| 306 | logging.debug('File: ' + i_repo_path + '/' + i_file) |
| 307 | logging.debug('Diff Text: ' + l_diff_text) |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 308 | |
| 309 | # SRCREV sets the SHA for the version of the other repo to use when |
| 310 | # building openbmc. SHAs should be stored in the file in a format |
| 311 | # like SRCRV =? "<SHA>". Find both the new '+' and old '-' ones |
| 312 | l_old_hash = None |
| 313 | l_new_hash = None |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 314 | l_old_hash_match = re.search('-[A-Z_]*SRCREV[+=? ]+"([a-f0-9]+)"', |
| 315 | l_diff_text) |
| 316 | l_new_hash_match = re.search('\+[A-Z_]*SRCREV[+=? ]+"([a-f0-9]+)"', |
| 317 | l_diff_text) |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 318 | if l_old_hash_match: |
| 319 | l_old_hash = l_old_hash_match.group(1) |
| 320 | if l_new_hash_match: |
| 321 | l_new_hash = l_new_hash_match.group(1) |
| 322 | |
| 323 | # Get the URI of the subrepo |
| 324 | l_uri = None |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 325 | if os.path.isfile(i_repo_path + '/' + i_file): |
| 326 | l_changed_file = open(i_repo_path + '/' + i_file, 'r') |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 327 | for l_line in l_changed_file: |
| 328 | # URIs should be stored in a format similar to |
| 329 | # SRC_URI ?= "git://github.com/<path to repo>" |
| 330 | l_uri_match = re.search('_URI[+=? ]+"([-a-zA-Z0-9/:\.]+)"', l_line) |
| 331 | if l_uri_match: |
| 332 | l_uri = l_uri_match.group(1) |
| 333 | break |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 334 | else: |
| 335 | logging.debug(i_repo_path + '/' + i_file) |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 336 | |
| 337 | # Go back to master |
| 338 | i_repo.git.checkout('master') |
| 339 | return l_uri, l_new_hash, l_old_hash |
| 340 | |
| 341 | ############################################################################### |
| 342 | # @brief Updates the repo under the given path or clones it from the |
| 343 | # uri if it doesn't yet exist |
| 344 | # |
| 345 | # @param i_uri : The URI to the remote repo to clone |
| 346 | # @param i_path : The file path to where the repo currently exists or |
| 347 | # where it will be created |
| 348 | ############################################################################### |
| 349 | def clone_or_update(i_uri, i_path): |
| 350 | # If the repo exists, just update it |
| 351 | if os.path.isdir(i_path): |
| 352 | l_repo = git.Repo(i_path) |
| 353 | l_repo.remotes[0].pull() |
| 354 | |
| 355 | # If it doesn't exist, clone it |
| 356 | else: |
| 357 | os.mkdir(i_path) |
| 358 | l_repo = git.Repo.init(i_path) |
| 359 | origin = l_repo.create_remote('origin', i_uri) |
| 360 | origin.fetch() |
| 361 | l_repo.create_head('master', origin.refs.master) \ |
| 362 | .set_tracking_branch(origin.refs.master) |
| 363 | origin.pull() |
| 364 | |
| 365 | ############################################################################### |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 366 | # @brief Gets the number of changed lines between two commits |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 367 | # |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 368 | # @param i_repo : The Repo object these commits are in |
| 369 | # @param i_begin_commit : A git reference to the beginning commit |
| 370 | # @param i_end_commit : A git reference to the end commit |
| 371 | # |
| 372 | # @return A two-tuple containing the number of insertions and the number of |
| 373 | # deletions between the begin and end commit |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 374 | ############################################################################### |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 375 | def get_line_count(i_repo, i_begin_commit, i_end_commit): |
| 376 | diff_output = i_repo.git.diff(i_end_commit, i_begin_commit, shortstat=True) |
| 377 | insertions = 0 |
| 378 | deletions = 0 |
| 379 | insertion_match = re.search('([0-9]+) insertion', diff_output) |
| 380 | deletion_match = re.search('([0-9]+) deletion', diff_output) |
| 381 | if insertion_match: |
| 382 | insertions = int(insertion_match.group(1)) |
| 383 | if deletion_match: |
| 384 | deletions = int(deletion_match.group(1)) |
| 385 | return insertions, deletions |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 386 | |
Charles Hofer | ff54ae9 | 2016-11-29 16:22:39 -0600 | [diff] [blame] | 387 | ############################################################################### |
| 388 | # @brief Gets closed issues from the commit message |
| 389 | # |
| 390 | # @param i_commit : The commit to get closed issues for |
| 391 | # |
| 392 | # @return A list of tuples, the first element being the ID of the issue, the |
| 393 | # second being the title from GitHub |
| 394 | ############################################################################### |
| 395 | def get_closed_issues(i_commit): |
| 396 | l_closed_issues = [] |
| 397 | |
| 398 | # Set up the regex |
| 399 | l_close_regex = re.compile( |
| 400 | '((F|f)ix((es|ed)?)|(C|c)lose((s|d)?)|(R|r)esolve((s|d)?)) ' |
| 401 | + '+(?P<issue>[a-zA-Z0-9#]+\/[a-zA-Z0-9#]+)') |
| 402 | l_matches = l_close_regex.finditer(i_commit.message) |
| 403 | |
| 404 | # Loop through all the matches getting each issue name |
| 405 | for l_match in l_matches: |
| 406 | l_issue_id = l_match.group('issue') |
| 407 | l_issue_title = get_issue_title(l_issue_id) |
| 408 | l_closed_issues.append((l_issue_id, l_issue_title)) |
| 409 | |
| 410 | return l_closed_issues |
| 411 | |
| 412 | ############################################################################### |
| 413 | # @brief Gets the title of an issue based on the issue ID |
| 414 | # |
| 415 | # @param i_issue_id : The ID of the issue to get the title for |
| 416 | # |
| 417 | # @return The title of the issue |
| 418 | ############################################################################### |
| 419 | def get_issue_title(i_issue_id): |
| 420 | # Construct the URL |
| 421 | l_url_tail = re.sub('#', '/issues/', i_issue_id) |
| 422 | l_full_url = 'https://api.github.com/repos/' + l_url_tail |
| 423 | l_title = '' |
| 424 | |
| 425 | # Send in the web request |
| 426 | l_response = requests.get(l_full_url) |
| 427 | if 200 == l_response.status_code: |
| 428 | l_issue = l_response.json() |
| 429 | l_title = l_issue['title'] |
| 430 | else: |
| 431 | logging.error(l_response.text) |
| 432 | logging.error('Recieved status code ' \ |
| 433 | + str(l_response.status_code) \ |
| 434 | + ' when getting issue titles.') |
| 435 | return l_title |
| 436 | |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 437 | ############################################################################## |
| 438 | # @brief Cuts the hash in commit revision names down to its 7 digit prefix |
| 439 | # |
| 440 | # @param i_name_rev : The name of the revision to change |
| 441 | # |
| 442 | # @return The same revision name but with the hash its 7 digit prefix instead |
| 443 | ############################################################################### |
| 444 | def to_prefix_name_rev(i_name_rev): |
| 445 | l_name_rev = i_name_rev |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 446 | l_hash, l_name = l_name_rev.split() |
Charles Hofer | 493cd6f | 2016-11-29 15:54:17 -0600 | [diff] [blame] | 447 | l_name_rev = l_hash[0:7] + ' ' + l_name |
| 448 | return l_name_rev |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 449 | |
Charles Hofer | ac55121 | 2016-10-20 16:33:41 -0500 | [diff] [blame] | 450 | # Only run main if run as a script |
| 451 | if __name__ == '__main__': |
| 452 | main(sys.argv[1:]) |
| 453 | |