|
| 1 | +import argparse |
| 2 | +import git |
| 3 | +import os |
| 4 | +import subprocess |
| 5 | + |
| 6 | + |
| 7 | +def make_new_dir(dir_path): |
| 8 | + if os.path.isdir(dir_path): |
| 9 | + print(f'{dir_path} already exists. Making patches ...') |
| 10 | + else: |
| 11 | + os.mkdir(dir_path) |
| 12 | + print(f'{dir_path} successfully created. Making patches ...') |
| 13 | + |
| 14 | + |
| 15 | +def is_git_repo(path): |
| 16 | + try: |
| 17 | + _ = git.Repo(path).git_dir |
| 18 | + return True |
| 19 | + except git.exc.InvalidGitRepositoryError: |
| 20 | + return False |
| 21 | + |
| 22 | + |
| 23 | +def patch_collector(dir_path, authors, output): |
| 24 | + |
| 25 | + if(is_git_repo(dir_path) is False): |
| 26 | + print(f'\n{dir_path} is not a git directory.\n') |
| 27 | + return |
| 28 | + |
| 29 | + print(f'\n**Inside {dir_path}**\n') |
| 30 | + end_hash = '' |
| 31 | + begin_hash = '' |
| 32 | + patch_count = 1 |
| 33 | + range_count = 1 |
| 34 | + |
| 35 | + author_list = '' |
| 36 | + for author in authors: |
| 37 | + author_list += f'--author={author} ' |
| 38 | + |
| 39 | + get_hashes_cmd = (f'git -C {dir_path} log --pretty=format:"%h" ' |
| 40 | + f'{author_list}') |
| 41 | + commit_hashes = subprocess.check_output(get_hashes_cmd, shell=True) |
| 42 | + commit_hashes = commit_hashes.decode(encoding='utf-8') |
| 43 | + commit_hashes = commit_hashes.split('\n') |
| 44 | + |
| 45 | + for commit_hash in commit_hashes: |
| 46 | + |
| 47 | + ancestor_hash_cmd = (f'git -C {dir_path} log --pretty=format:"%h" ' |
| 48 | + f'{author_list}{commit_hash}^^..{commit_hash}^ ' |
| 49 | + '--exit-code 1>/dev/null') |
| 50 | + |
| 51 | + if subprocess.Popen(ancestor_hash_cmd, shell=True).wait(): |
| 52 | + if end_hash == '': |
| 53 | + end_hash = commit_hash |
| 54 | + begin_hash = commit_hash |
| 55 | + range_count = range_count + 1 |
| 56 | + |
| 57 | + else: |
| 58 | + subdir_msg = ('\nPlease enter a subdirectory name for this patch' |
| 59 | + '(leave this empty\nto leave this patch out, use ./ ' |
| 60 | + 'for the root directory):') |
| 61 | + print('\n') |
| 62 | + |
| 63 | + if end_hash == '': |
| 64 | + temp = patch_count-1 |
| 65 | + print(f'Adding one patch to {temp} existing:') |
| 66 | + subprocess.Popen( |
| 67 | + f'git -C {dir_path} log {commit_hash}^..{commit_hash}', |
| 68 | + shell=True).wait() |
| 69 | + print(subdir_msg) |
| 70 | + subdir = input() |
| 71 | + |
| 72 | + if subdir != '': |
| 73 | + newdir = f'{output}/{subdir}' |
| 74 | + print(f'mkdir -p {newdir}') |
| 75 | + make_new_dir(newdir) |
| 76 | + subprocess.Popen( |
| 77 | + f'git -C {dir_path} format-patch {commit_hash}^..' |
| 78 | + f'{commit_hash} -o {newdir}', shell=True).wait() |
| 79 | + patch_count = patch_count + 1 |
| 80 | + |
| 81 | + else: |
| 82 | + print('Omitting patch...') |
| 83 | + |
| 84 | + else: |
| 85 | + temp = patch_count-1 |
| 86 | + print(f'Adding {range_count} patches to {temp} existing:') |
| 87 | + subprocess.Popen( |
| 88 | + f'git -C {dir_path} log {begin_hash}^^..{end_hash} ' |
| 89 | + f'--oneline', shell=True).wait() |
| 90 | + print(subdir_msg) |
| 91 | + subdir = input() |
| 92 | + |
| 93 | + if subdir != '': |
| 94 | + newdir = f'{output}/{subdir}' |
| 95 | + print(f'mkdir -p {newdir}') |
| 96 | + make_new_dir(newdir) |
| 97 | + subprocess.Popen( |
| 98 | + f'git -C {dir_path} format-patch {begin_hash}^^..' |
| 99 | + f'{end_hash} -o {newdir}', shell=True).wait() |
| 100 | + patch_count = patch_count + range_count |
| 101 | + |
| 102 | + else: |
| 103 | + print('Omitting patch series...') |
| 104 | + |
| 105 | + end_hash = '' |
| 106 | + range_count = 1 |
| 107 | + |
| 108 | + |
| 109 | +def main(): |
| 110 | + help_message = '''This is a simple helper script that allows collecting all |
| 111 | + patches from one author in a git repository. It will detect consequent |
| 112 | + ranges of patches and ask for each range for a subdirectory so you can |
| 113 | + distinguish the patch series.''' |
| 114 | + |
| 115 | + parser = argparse.ArgumentParser(description=help_message) |
| 116 | + parser.add_argument('-a', '--author', action='append', required=True, |
| 117 | + help="Name of contributor") |
| 118 | + parser.add_argument('-o', '--opdir', required=True, |
| 119 | + help='Output directory absolute path.') |
| 120 | + parser.add_argument('-i', '--inpdir', action='append', required=True, |
| 121 | + help="Input directory path") |
| 122 | + args = parser.parse_args() |
| 123 | + authors = args.author |
| 124 | + output = args.opdir |
| 125 | + input_dir_paths = args.inpdir |
| 126 | + |
| 127 | + for input_path in input_dir_paths: |
| 128 | + patch_collector(input_path, authors, output) |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == '__main__': |
| 132 | + main() |
0 commit comments