get_sha_commits.py 3.77 KB
Newer Older
1
#!/usr/bin/env python3
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

#
#  Copyright (C) 2018 Codethink Limited
#  Copyright (C) 2018 Bloomberg Finance LP
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2 of the License, or (at your option) any later version.
#
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library. If not, see <http://www.gnu.org/licenses/>.
#
#  Authors:
#        Lachlan Mackenzie <lachlan.mackenzie@codethink.co.uk>

import tempfile
import argparse
import os
import logging
import sys

29 30
import git

31 32 33 34 35 36 37 38 39 40 41
# This function generates a list of SHAs for a given repo, branch a start commit
# which is the last commit that was processed and is prior to the 1st commit to
# be returned and the last commit which might be current head as determined at
# the beginning of benchmark testing.
#
# repo_url - the url of the repo that should be considered
# branch - the buildstream branch that should be considered
# last_commit - the last commit that was previously processed
# latest_commit - the last commit that needs to be added to the returned
#                 list.

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
def main():
   repo = "https://gitlab.com/BuildStream/buildstream.git"
   branch = 'master'
   last_commit = None
   latest_commit = None

   parser = argparse.ArgumentParser()
   parser.add_argument("-r", "--repo_url",
                       help="URL of the repo",
                       type=str)
   parser.add_argument("-b", "--branch",
                       help="Branch to be traversed",
                       type=str)
   parser.add_argument("-s", "--last_commit",
                       help="Last commit SHA1 reference",
                       type=str)
   parser.add_argument("-t", "--latest_commit",
                       help="Latest commit SHA1 reference",
                       type=str)
   args = parser.parse_args()

   if bool(args.repo_url):
      repo = args.repo_url

   if bool(args.branch):
      branch = args.branch

   if bool(args.last_commit):
      last_commit = args.last_commit

   if bool(args.latest_commit):
      latest_commit = args.latest_commit

   try:
      commits = get_list_of_commits(repo, branch, last_commit, latest_commit)
78
   except git.exc.GitError as err:  # pylint: disable=no-member
79 80
      print("Unable to extract commits: ", err)
      sys.exit(1)
81 82
   # TODO: Come on, surely there's a million other things that could cause this?
   except Exception as ex_err:  # pylint: disable=broad-except
83 84 85 86 87 88
      print("Nothing to extract: ", ex_err)
      sys.exit(1)

   print(commits)


89
def get_list_of_commits(repo_path, branch, last_commit, latest_commit):
90 91 92 93 94 95 96 97 98

   commits = list()

   with tempfile.TemporaryDirectory(prefix='temp_staging_location') as temp_staging_area:
      try:
         if os.path.exists(repo_path):
            repo = git.Repo.init(repo_path, bare=False)
         else:
            repo = git.Repo.clone_from(repo_path, temp_staging_area)
99 100
      except git.exc.GitError as err:  # pylint: disable=no-member
         logging.error("Unable to access git repository: %s", err)
101 102 103 104
         raise

      start = False
      for commit in repo.iter_commits(branch):
105 106 107 108 109 110 111 112 113
         if commit.hexsha == latest_commit:
            commits.append(commit)
            start = True
            if commit.hexsha == last_commit:
               break
         elif commit.hexsha == last_commit:
            break
         elif start:
            commits.append(commit)
114 115 116 117 118 119

   return commits


if __name__ == "__main__":
   main()