From 5ff8feb53b9f1e8cbb36b0376a966f05797a1c0b Mon Sep 17 00:00:00 2001 From: Mark Joshwel Date: Fri, 12 Jul 2024 03:40:48 +0800 Subject: [PATCH] tooling(restep): fixes --- sync.py | 78 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/sync.py b/sync.py index fca7981..a69fc3c 100644 --- a/sync.py +++ b/sync.py @@ -239,6 +239,9 @@ def generate_sotaignore(large_files: list[Path]) -> None: "# .sotaignore file generated by sota staircase ReStepper", ) + if new_sotaignore == []: + return + REPO_SOTAIGNORE.touch(exist_ok=True) REPO_SOTAIGNORE.write_text("\n".join(new_sotaignore) + "\n", encoding="utf-8") @@ -254,13 +257,7 @@ def rewrite_gitattributes(target_dir: Path) -> None: # recursively search for .gitattributes files for repo_file in target_dir.rglob(".gitattributes"): - # print(repo_file) - try: - repo_file.write_text(NEUTERED_GITATTRIBUTES, encoding="utf-8") - except Exception as exc: - print(f"error writing to {repo_file}: {exc} ({exc.__class__.__name__})") - else: - print(f"success to {repo_file}") + repo_file.write_text(NEUTERED_GITATTRIBUTES, encoding="utf-8") # helper function for running steps @@ -472,6 +469,7 @@ def main() -> None: f"critical error (whuh? internal?): not inside the temp dir '{str(Path(dir_temp).absolute())}'" ) + # check for forge and github remotes step( func=cmd("git remote -v"), post_func=post_remote_v, @@ -479,11 +477,20 @@ def main() -> None: if "remote/forge" not in r: err("critical error (whuh?): no forge remote found") + # get the current branch step( - func=cmd(f"git fetch {r['remote/forge']} --dry-run"), + func=cmd("git branch --show-current"), ) - if (not r["blank"]) and ("--dirty" not in argv): - err("critical error (whuh?): not up to date with forge... sync your changes first?") + branch = r["stdout"].strip() + if r.get("errored", "yes") or branch == "": + err("critical error (whuh?): couldn't get current branch") + + step(func=cmd(f"git fetch {r['remote/forge']}")) + step(func=cmd(f"git rev-list HEAD...{r['remote/forge']}/{branch} --count")) + if (r.get("stdout", "").strip() != "0") and ("--dirty" not in argv): + err( + "critical error (whuh?): not up to date with forge... sync your changes first?" + ) step(desc="3 lfs\tfetch lfs objects", func=cmd("git lfs fetch")) @@ -507,33 +514,31 @@ def main() -> None: "critical error (whuh? internal?): lfs objects still exist post-migrate and uninstall" ) - try: - sotaignore = ( - Path(dir_temp) - .joinpath(".sotaignore") - .read_text(encoding="utf-8") - .strip() - ) - except Exception as exc: - err("critical error: couldn't read .sotaignore file", exc=exc) + temp_sotaignore = Path(dir_temp).joinpath(".sotaignore") - sotaignore_large_files: list[str] = [ - line - for line in sotaignore.splitlines() - if not line.startswith("#") and line.strip() != "" - ] + if temp_sotaignore.exists(): + try: + sotaignore = temp_sotaignore.read_text(encoding="utf-8").strip() + except Exception as exc: + err("critical error: couldn't read .sotaignore file", exc=exc) - # FUTURE: if this becomes slow, start chunking --path arguments - # https://stackoverflow.com/questions/43762338/how-to-remove-file-from-git-history + sotaignore_large_files: list[str] = [ + line + for line in sotaignore.splitlines() + if not line.startswith("#") and line.strip() != "" + ] - for n, lf in enumerate(sotaignore_large_files, start=1): - step( - desc=f"6 lfs\tfilter ({n}/{len(sotaignore_large_files)}) - {lf}", - func=cmd(f'git filter-repo --force --invert-paths --path "{lf}"'), - ) + # FUTURE: if this becomes slow, start chunking --path arguments + # https://stackoverflow.com/questions/43762338/how-to-remove-file-from-git-history + + for n, lf in enumerate(sotaignore_large_files, start=1): + step( + desc=f"6 lfs\tfilter ({n}/{len(sotaignore_large_files)}) - {lf}", + func=cmd(f'git filter-repo --force --invert-paths --path "{lf}"'), + ) step( - desc="7 lfs\tneuter .gitattributes", + desc="7 fin\tneuter .gitattributes", func=lambda: rewrite_gitattributes(Path(dir_temp)), ) @@ -552,15 +557,10 @@ def main() -> None: err("critical error (whuh?): couldn't add github remote") r["remote/github"] = "github" - # get current branch step( - func=cmd("git branch --show-current"), - ) - - step( - desc=f"9 fin\tpushing to github/{r['stdout'].strip()}", + desc=f"9 fin\tpushing to github/{branch}", func=cmd( - f"git push {r['remote/github']} {r['stdout'].strip()} --force" + f"git push {r['remote/github']} {branch} --force" if ("--test" not in argv) else "git --version" ),