|
| 1 | +import argparse |
| 2 | +import re |
| 3 | +import subprocess |
| 4 | +from typing import Tuple |
| 5 | + |
| 6 | +RENAME_PATTERN = re.compile(r"^R[0-9]+") |
| 7 | + |
| 8 | + |
| 9 | +def parse_args() -> Tuple[str, str]: |
| 10 | + parser = argparse.ArgumentParser() |
| 11 | + parser.add_argument("start_commit") |
| 12 | + parser.add_argument("end_commit") |
| 13 | + |
| 14 | + args = parser.parse_args() |
| 15 | + |
| 16 | + return args.start_commit, args.end_commit |
| 17 | + |
| 18 | + |
| 19 | +def main(): |
| 20 | + start_commit, end_commit = parse_args() |
| 21 | + diff_command = [ |
| 22 | + "git", |
| 23 | + "diff", |
| 24 | + "--name-status", |
| 25 | + "-M", |
| 26 | + "--stat", |
| 27 | + start_commit, |
| 28 | + end_commit, |
| 29 | + ] |
| 30 | + output = subprocess.check_output(diff_command, encoding="utf-8") |
| 31 | + renamed_files = filter(RENAME_PATTERN.match, output.splitlines()) |
| 32 | + renamed_files = map(lambda x: x.split("\t")[1:], renamed_files) |
| 33 | + renamed_markdown_files = filter(lambda x: x[0].endswith(".md"), renamed_files) |
| 34 | + renamed_markdown_files = list(renamed_markdown_files) |
| 35 | + |
| 36 | + if renamed_markdown_files: |
| 37 | + print("Detected renamed/moved files:") |
| 38 | + print("```") |
| 39 | + for file_from, file_to in renamed_markdown_files: |
| 40 | + print("{} => {}".format(file_from, file_to)) |
| 41 | + print("```") |
| 42 | + print("To prevent 404 pages, consider adding the redirects to the new pages:") |
| 43 | + print("```markdown") |
| 44 | + print("---") |
| 45 | + print("redirect_from:") |
| 46 | + for file_from, _ in renamed_markdown_files: |
| 47 | + print(" - {}".format(file_from)) |
| 48 | + print("---") |
| 49 | + print("```") |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == "__main__": |
| 53 | + main() |
0 commit comments