def __init__(self, filename, strict=False): self.filename = filename diff = PatchSet.from_filename(filename) date = None author = None with open(self.filename, 'r') as f: lines = f.read().splitlines() lines = list(takewhile(lambda line: line != '---', lines)) for line in lines: if line.startswith(DATE_PREFIX): date = parse(line[len(DATE_PREFIX):]) elif line.startswith(FROM_PREFIX): author = GitCommit.format_git_author(line[len(FROM_PREFIX):]) header = list(takewhile(lambda line: line != '', lines)) body = lines[len(header) + 1:] modified_files = [] for f in diff: if f.is_added_file: t = 'A' elif f.is_removed_file: t = 'D' else: t = 'M' modified_files.append((f.path, t)) super().__init__(None, date, author, body, modified_files, strict=strict)
def parse_git_revisions(repo_path, revisions, strict=False): repo = Repo(repo_path) parsed_commits = [] if '..' in revisions: commits = list(repo.iter_commits(revisions)) else: 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' 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) parsed_commits.append(git_commit) return parsed_commits
def __init__(self, filename): self.filename = filename diff = PatchSet.from_filename(filename) date = None author = None subject = '' subject_last = False with open(self.filename, 'r') as f: lines = f.read().splitlines() lines = list(takewhile(lambda line: line != '---', lines)) for line in lines: if line.startswith(DATE_PREFIX): date = parse(line[len(DATE_PREFIX):]) elif line.startswith(FROM_PREFIX): author = GitCommit.format_git_author(line[len(FROM_PREFIX):]) elif line.startswith(SUBJECT_PREFIX): subject = line[len(SUBJECT_PREFIX):] subject_last = True elif subject_last and line.startswith(' '): subject += line elif line == '': break else: subject_last = False if subject: subject = subject_patch_regex.sub('', subject) header = list(takewhile(lambda line: line != '', lines)) # Note: commit message consists of email subject, empty line, email body message = [subject] + lines[len(header):] modified_files = [] for f in diff: # Strip "a/" and "b/" prefixes source = decode_path(f.source_file)[2:] target = decode_path(f.target_file)[2:] if f.is_added_file: t = 'A' elif f.is_removed_file: t = 'D' elif unidiff_supports_renaming and f.is_rename: # Consider that renamed files are two operations: the deletion # of the original name and the addition of the new one. modified_files.append((source, 'D')) t = 'A' else: t = 'M' modified_files.append((target if t != 'D' else source, t)) git_info = GitInfo(None, date, author, message, modified_files) super().__init__(git_info, commit_to_info_hook=lambda x: None)
def parse_git_revisions(repo_path, revisions, strict=False): repo = Repo(repo_path) def commit_to_info(commit): try: c = repo.commit(commit) diff = repo.commit(commit + '~').diff(commit) modified_files = [] for file in diff: if hasattr(file, 'renamed_file'): is_renamed = file.renamed_file else: is_renamed = file.renamed if file.new_file: t = 'A' elif file.deleted_file: t = 'D' elif is_renamed: # 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 parsed_commits = [] if '..' in revisions: commits = list(repo.iter_commits(revisions)) else: commits = [repo.commit(revisions)] for commit in commits: 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
def __init__(self, filename, strict=False): self.filename = filename diff = PatchSet.from_filename(filename) date = None author = None with open(self.filename, 'r') as f: lines = f.read().splitlines() lines = list(takewhile(lambda line: line != '---', lines)) for line in lines: if line.startswith(DATE_PREFIX): date = parse(line[len(DATE_PREFIX):]) elif line.startswith(FROM_PREFIX): author = GitCommit.format_git_author(line[len(FROM_PREFIX):]) header = list(takewhile(lambda line: line != '', lines)) body = lines[len(header) + 1:] modified_files = [] for f in diff: # Strip "a/" and "b/" prefixes source = f.source_file[2:] target = f.target_file[2:] if f.is_added_file: t = 'A' elif f.is_removed_file: t = 'D' elif f.is_rename: # Consider that renamed files are two operations: the deletion # of the original name and the addition of the new one. modified_files.append((source, 'D')) t = 'A' 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)
def test_parse_git_name_status(self): modified_files = GitCommit.parse_git_name_status(NAME_STATUS1) assert len(modified_files) == 3 assert modified_files[1] == ('gcc/ada/libgnat/s-atopar.adb', 'D') assert modified_files[2] == ('gcc/ada/libgnat/s-aoinar.adb', 'A')