|
| 1 | +import logging |
| 2 | +import os |
| 3 | +from dataclasses import dataclass |
| 4 | + |
| 5 | +from git import GitCommandError, Repo |
| 6 | +from pathlib3x import Path |
| 7 | + |
| 8 | +from kt.ktlib.config import Config |
| 9 | +from kt.ktlib.kernels import KernelInfo |
| 10 | +from kt.ktlib.util import Constants |
| 11 | + |
| 12 | + |
| 13 | +@dataclass |
| 14 | +class RepoWorktree: |
| 15 | + source_root: Repo |
| 16 | + folder: Path |
| 17 | + remote: str |
| 18 | + remote_branch: str |
| 19 | + local_branch: str |
| 20 | + |
| 21 | + @classmethod |
| 22 | + def load_from_filepath(cls, folder: Path): |
| 23 | + if not folder.exists(): |
| 24 | + raise RuntimeError(f"{folder} does not exist") |
| 25 | + |
| 26 | + # check if folder is a git repo |
| 27 | + repo = Repo(folder) |
| 28 | + |
| 29 | + source_root = Path(repo.git.rev_parse("--path-format=absolute", "--git-common-dir")).parent |
| 30 | + remote = repo.remotes.origin |
| 31 | + |
| 32 | + remote_branch = repo.active_branch.tracking_branch() |
| 33 | + |
| 34 | + local_branch = repo.active_branch.name |
| 35 | + |
| 36 | + return cls( |
| 37 | + source_root=source_root, |
| 38 | + folder=folder, |
| 39 | + remote=remote, |
| 40 | + remote_branch=remote_branch, |
| 41 | + local_branch=local_branch, |
| 42 | + ) |
| 43 | + |
| 44 | + def setup(self): |
| 45 | + """ |
| 46 | + First run: It will create the worktree |
| 47 | + Second run: It will update the worktree |
| 48 | + """ |
| 49 | + |
| 50 | + try: |
| 51 | + remote_ref = f"{self.remote}/{self.remote_branch}" |
| 52 | + self.source_root.git.worktree( |
| 53 | + "add", |
| 54 | + "--track", |
| 55 | + "-b", |
| 56 | + self.local_branch, |
| 57 | + self.folder, |
| 58 | + remote_ref, |
| 59 | + ) |
| 60 | + |
| 61 | + except GitCommandError as e: |
| 62 | + if "already exists" in e.stderr: |
| 63 | + self.update() |
| 64 | + else: |
| 65 | + # Make sure the worktree is properly cleaned up |
| 66 | + self.cleanup() |
| 67 | + raise e |
| 68 | + |
| 69 | + def update(self): |
| 70 | + """ |
| 71 | + It will make sure the worktree is up-to-date with remote. |
| 72 | + It assumes the worktree is already created and initialized. |
| 73 | + """ |
| 74 | + logging.info("update") |
| 75 | + repo = Repo(self.folder) |
| 76 | + |
| 77 | + repo.remotes.origin.pull(rebase=True) |
| 78 | + |
| 79 | + def cleanup(self): |
| 80 | + # remove worktree, only if it exists |
| 81 | + try: |
| 82 | + self.source_root.git.worktree("remove", self.folder, "-f") |
| 83 | + except GitCommandError as e: |
| 84 | + if f"'{self.folder}' is not a working tree" not in e.stderr: |
| 85 | + raise e |
| 86 | + |
| 87 | + # remove local branch, only if it exists |
| 88 | + try: |
| 89 | + self.source_root.delete_head(self.local_branch, force=True) |
| 90 | + except GitCommandError as e: |
| 91 | + if f"branch '{self.local_branch}' not found" not in e.stderr: |
| 92 | + raise e |
| 93 | + |
| 94 | + def push(self, force: bool = False): |
| 95 | + repo = Repo(self.folder) |
| 96 | + origin = repo.remote(name=self.remote) |
| 97 | + args = ["--set-upstream", origin.name, self.local_branch] |
| 98 | + if force: |
| 99 | + args.append("--force-with-lease") |
| 100 | + |
| 101 | + repo.git.push(*args) |
| 102 | + |
| 103 | + |
| 104 | +@dataclass |
| 105 | +class KernelWorkspace: |
| 106 | + folder: Path |
| 107 | + dist_worktree: RepoWorktree |
| 108 | + src_worktree: RepoWorktree |
| 109 | + |
| 110 | + @classmethod |
| 111 | + def load_from_filepath(cls, folder: Path): |
| 112 | + if not folder.exists(): |
| 113 | + raise RuntimeError(f"{folder} does not exists") |
| 114 | + |
| 115 | + ## Get the dist-git-tree path |
| 116 | + dist_worktree_path = folder / Constants.DIST_TREE |
| 117 | + dist_worktree = RepoWorktree.load_from_filepath(folder=dist_worktree_path) |
| 118 | + |
| 119 | + src_worktree_path = folder / Constants.SRC_TREE |
| 120 | + src_worktree = RepoWorktree.load_from_filepath(folder=src_worktree_path) |
| 121 | + |
| 122 | + return cls( |
| 123 | + folder=folder, |
| 124 | + dist_worktree=dist_worktree, |
| 125 | + src_worktree=src_worktree, |
| 126 | + ) |
| 127 | + |
| 128 | + @classmethod |
| 129 | + def load(cls, name: str, config: Config, kernel_info: KernelInfo, extra: str): |
| 130 | + if extra: |
| 131 | + name = name + "_" + extra |
| 132 | + |
| 133 | + folder = config.kernels_dir / Path(name) |
| 134 | + user = os.environ["USER"] |
| 135 | + default_remote = "origin" |
| 136 | + |
| 137 | + dist_folder = folder / Path(Constants.DIST_TREE) |
| 138 | + dist_local_branch = f"{{{user}}}_{kernel_info.dist_git_branch}" |
| 139 | + if extra: |
| 140 | + dist_local_branch += f"_{extra}" |
| 141 | + |
| 142 | + dist_worktree = RepoWorktree( |
| 143 | + source_root=Repo(kernel_info.dist_git_root.folder), |
| 144 | + folder=dist_folder, |
| 145 | + remote=default_remote, |
| 146 | + remote_branch=kernel_info.dist_git_branch, |
| 147 | + local_branch=dist_local_branch, |
| 148 | + ) |
| 149 | + |
| 150 | + src_folder = folder / Path(Constants.SRC_TREE) |
| 151 | + src_local_branch = f"{{{user}}}_{kernel_info.src_tree_branch}" |
| 152 | + if extra: |
| 153 | + src_local_branch += f"_{extra}" |
| 154 | + |
| 155 | + src_worktree = RepoWorktree( |
| 156 | + source_root=Repo(kernel_info.src_tree_root.folder), |
| 157 | + folder=src_folder, |
| 158 | + remote=default_remote, |
| 159 | + remote_branch=kernel_info.src_tree_branch, |
| 160 | + local_branch=src_local_branch, |
| 161 | + ) |
| 162 | + |
| 163 | + return cls( |
| 164 | + folder=folder, |
| 165 | + dist_worktree=dist_worktree, |
| 166 | + src_worktree=src_worktree, |
| 167 | + ) |
| 168 | + |
| 169 | + def setup(self): |
| 170 | + # Make sure the folder is created |
| 171 | + self.folder.mkdir(parents=True, exist_ok=True) |
| 172 | + |
| 173 | + self.dist_worktree.setup() |
| 174 | + self.src_worktree.setup() |
| 175 | + |
| 176 | + def cleanup(self): |
| 177 | + self.dist_worktree.cleanup() |
| 178 | + self.src_worktree.cleanup() |
| 179 | + |
| 180 | + # Remove working directory that includes the above git worktrees |
| 181 | + self.folder.rmtree(ignore_errors=True) |
0 commit comments