| 5 | |
| 6 | |
| 7 | class Git: |
| 8 | def __init__(self, root_dir: Path): |
| 9 | self.branch = "autofuzz_base" |
| 10 | self.root_dir = root_dir.resolve() |
| 11 | |
| 12 | self.config("user.name", "autofuzz") |
| 13 | self.config("user.email", "autofuzz@autofuzz.com") |
| 14 | self.checkout(self.branch) |
| 15 | |
| 16 | adds, mods = self.diff() |
| 17 | if adds or mods: |
| 18 | raise RuntimeError("Modified files found [" + self.branch + "]") |
| 19 | |
| 20 | def checkout(self, branch: str, create: bool = False): |
| 21 | cmd = ["git", "checkout", branch] |
| 22 | if create: |
| 23 | cmd.insert(2, "-b") |
| 24 | self.execute(cmd) |
| 25 | |
| 26 | def diff(self) -> Tuple[List[str], List[str]]: |
| 27 | cmd = ["git", "diff", "--name-status"] |
| 28 | out = self.execute(cmd) |
| 29 | adds = [] |
| 30 | mods = [] |
| 31 | for line in out.splitlines(): |
| 32 | line = line.strip() |
| 33 | if not line: |
| 34 | continue |
| 35 | tokens = line.split() |
| 36 | if tokens[0] == "A": |
| 37 | adds.append(tokens[1]) |
| 38 | elif tokens[0] == "M": |
| 39 | mods.append(tokens[1]) |
| 40 | return adds, mods |
| 41 | |
| 42 | def delete_branch(self, branch: str): |
| 43 | cmd = ["git", "branch", "-D", branch] |
| 44 | self.execute(cmd) |
| 45 | |
| 46 | def add(self, path: Path): |
| 47 | cmd = ["git", "add", path] |
| 48 | self.execute(cmd) |
| 49 | |
| 50 | def commit(self, message: str): |
| 51 | cmd = ["git", "commit", "-m", message] |
| 52 | self.execute(cmd) |
| 53 | |
| 54 | def config(self, name: str, value: str = None) -> str: |
| 55 | cmd = ["git", "config", name] |
| 56 | if value: |
| 57 | cmd.append(value) |
| 58 | return self.execute(cmd) |
| 59 | |
| 60 | def changed(self) -> List[Path]: |
| 61 | cmd = ["git", "show", "--name-status"] |
| 62 | out = self.execute(cmd) |
| 63 | changed_files = [] |
| 64 | for line in out.splitlines(): |