git-cinnabar
60 строк · 1.8 Кб
1# This Source Code Form is subject to the terms of the Mozilla Public
2# License, v. 2.0. If a copy of the MPL was not distributed with this
3# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5import sys6
7
8class DataCommand:9def __init__(self, data):10self.data = data11
12def write_to(self, out):13out.write(b"data %d\n" % len(self.data))14out.write(self.data)15
16
17def iter_commands(input=sys.stdin.buffer):18for line in input:19if line.startswith(b"data "):20_, length = line.split()21length = int(length)22data = input.read(length)23assert len(data) == length24yield DataCommand(data)25else:26yield line27
28
29def write_command(command, out=sys.stdout.buffer):30if isinstance(command, DataCommand):31command.write_to(out)32else:33out.write(command)34
35
36if __name__ == "__main__":37args = sys.argv[1:]38for arg in args:39if arg not in ["--commits", "--roots"]:40print(f"Unsupported options: {args}")41sys.exit(1)42
43commands = iter_commands()44for command in commands:45if isinstance(command, DataCommand):46if "--commits" in args:47command.data += b"\n"48if "--roots" in args:49write_command(command)50while True:51command = next(commands)52# No "from" command, so this is a root, remove all the53# files from it.54if not command.startswith((b"deleteall", b"M ")):55break56
57elif command.startswith((b"author <", b"committer <")):58cmd, email = command.split(b"<", 1)59command = cmd[:-1] + email.split(b"@", 1)[0] + b" <" + email60write_command(command)61