예제 #1
0
 def GetCommitsBetweenIds(newer: str, older: str) -> list:
     """
     Get a list of commits between two Git commits.
     
     Args:
         newer (str): The newer Git commit id for the comparison.
         older (str): The older Git commit id for the comparison.
     
     Returns:
         A list of commits.
     """
     commits = list()
     output = subprocess.check_output([
         'git', 'log', '{newer}...{older}'.format(newer=newer, older=older)
     ]).decode('utf-8')
     if (len(output) < 1):
         return commits
     commitLog = str(output).split('\n')
     lineIndex = 0
     while (lineIndex < len(commitLog)):
         commitLogLine = str(commitLog[lineIndex])
         if (commitLogLine.startswith('commit')):
             # Commit log item startsexpression
             commit = Commit()
             # Get commit hash from the commit
             commit.hash = str(commitLog[lineIndex]).split(' ')[1]
             lineIndex = lineIndex + 1
             # Get "Author:", "{author's name} <{author's email}>"
             logLine = str(commitLog[lineIndex]).split(':', 1)[1].lstrip()
             user = User()
             user.name = logLine.split('<', 1)[0].rstrip(' ')
             user.email = logLine.split('<', 1)[1].lstrip().replace(
                 '\r', '').replace('\n', '')
             commit.author = user
             lineIndex = lineIndex + 1
             # Split "Date:   ", "{actual date}"
             logLine = str(commitLog[lineIndex]).split('Date:')[1]
             dateString = logLine = logLine.lstrip().replace('\r',
                                                             '').replace(
                                                                 '\n', '')
             commit.date = Date.ConvertGitStringToDate(dateString)
             # Skip extra line between date and commit title
             lineIndex = lineIndex + 2
             # Add the title to the commit but remove leading whitespace and trailing linefeed
             commit.title = str(commitLog[lineIndex]).lstrip().replace(
                 '\r', '').replace('\n', '')
             lineIndex = lineIndex + 1
             # Skip extra line between title and commit message
             lineIndex = lineIndex + 1
             # Commit message lasts until another commit message starts or the log ends
             commit.message = ""
             while (lineIndex < len(commitLog)):
                 if (str(commitLog[lineIndex]).startswith('commit')):
                     break
                 commit.message = commit.message + commitLog[
                     lineIndex].lstrip()
                 lineIndex = lineIndex + 1
             commits.append(commit)
             break
         else:
             lineIndex = lineIndex + 1
     return commits