|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import click |
| 4 | +from pygit2.enums import FileStatus, ResetMode |
| 5 | +from pygit2.repository import Repository |
| 6 | + |
| 7 | +from codegen.cli.auth.constants import CODEGEN_DIR |
| 8 | +from codegen.cli.git.repo import get_git_repo |
| 9 | + |
| 10 | + |
| 11 | +def is_codegen_file(filepath: Path) -> bool: |
| 12 | + """Check if a file is in the .codegen directory.""" |
| 13 | + return CODEGEN_DIR in filepath.parents |
| 14 | + |
| 15 | + |
| 16 | +def backup_codegen_files(repo: Repository) -> dict[str, tuple[bytes | None, bool]]: |
| 17 | + """Backup .codegen files and track if they were staged. |
| 18 | +
|
| 19 | + Returns: |
| 20 | + Dict mapping filepath to (content, was_staged) tuple. |
| 21 | + content is None for deleted files. |
| 22 | + """ |
| 23 | + codegen_changes = {} |
| 24 | + for filepath, status in repo.status().items(): |
| 25 | + if not is_codegen_file(Path(filepath)): |
| 26 | + continue |
| 27 | + |
| 28 | + was_staged = bool(status & (FileStatus.INDEX_MODIFIED | FileStatus.INDEX_NEW | FileStatus.INDEX_DELETED | FileStatus.INDEX_RENAMED)) |
| 29 | + |
| 30 | + # Handle deleted files |
| 31 | + if status & (FileStatus.WT_DELETED | FileStatus.INDEX_DELETED): |
| 32 | + codegen_changes[filepath] = (None, was_staged) |
| 33 | + continue |
| 34 | + # Handle modified, new, or renamed files |
| 35 | + if status & (FileStatus.WT_MODIFIED | FileStatus.WT_NEW | FileStatus.INDEX_MODIFIED | FileStatus.INDEX_NEW | FileStatus.INDEX_RENAMED): |
| 36 | + file_path = Path(repo.workdir) / filepath |
| 37 | + if file_path.exists(): # Only read if file exists |
| 38 | + codegen_changes[filepath] = (file_path.read_bytes(), was_staged) |
| 39 | + |
| 40 | + return codegen_changes |
| 41 | + |
| 42 | + |
| 43 | +def restore_codegen_files(repo: Repository, codegen_changes: dict[str, tuple[bytes | None, bool]]) -> None: |
| 44 | + """Restore backed up .codegen files and their staged status.""" |
| 45 | + for filepath, (content, was_staged) in codegen_changes.items(): |
| 46 | + file_path = Path(repo.workdir) / filepath |
| 47 | + |
| 48 | + if content is None: # Handle deleted files |
| 49 | + if file_path.exists(): |
| 50 | + file_path.unlink() |
| 51 | + if was_staged: |
| 52 | + repo.index.remove(filepath) |
| 53 | + else: # Handle existing files |
| 54 | + file_path.parent.mkdir(parents=True, exist_ok=True) |
| 55 | + file_path.write_bytes(content) |
| 56 | + if was_staged: |
| 57 | + repo.index.add(filepath) |
| 58 | + |
| 59 | + if codegen_changes: |
| 60 | + repo.index.write() |
| 61 | + |
| 62 | + |
| 63 | +def remove_untracked_files(repo: Repository) -> None: |
| 64 | + """Remove untracked files except those in .codegen directory.""" |
| 65 | + for filepath, status in repo.status().items(): |
| 66 | + if not is_codegen_file(Path(filepath)) and status & FileStatus.WT_NEW: |
| 67 | + file_path = Path(repo.workdir) / filepath |
| 68 | + if file_path.exists(): # Only try to remove if file exists |
| 69 | + if file_path.is_file(): |
| 70 | + file_path.unlink() |
| 71 | + elif file_path.is_dir(): |
| 72 | + file_path.rmdir() |
| 73 | + |
| 74 | + |
| 75 | +@click.command(name="reset") |
| 76 | +def reset_command() -> None: |
| 77 | + """Reset git repository while preserving all files in .codegen directory""" |
| 78 | + repo = get_git_repo() |
| 79 | + if not repo: |
| 80 | + click.echo("Not a git repository", err=True) |
| 81 | + return |
| 82 | + |
| 83 | + try: |
| 84 | + # Backup .codegen files and their staged status |
| 85 | + codegen_changes = backup_codegen_files(repo) |
| 86 | + |
| 87 | + # Reset everything |
| 88 | + repo.reset(repo.head.target, ResetMode.HARD) |
| 89 | + |
| 90 | + # Restore .codegen files and their staged status |
| 91 | + restore_codegen_files(repo, codegen_changes) |
| 92 | + |
| 93 | + # Remove untracked files except .codegen |
| 94 | + remove_untracked_files(repo) |
| 95 | + |
| 96 | + click.echo(f"Reset complete. Repository has been restored to HEAD (preserving {CODEGEN_DIR}) and untracked files have been removed (except {CODEGEN_DIR})") |
| 97 | + except Exception as e: |
| 98 | + click.echo(f"Error: {e}", err=True) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + reset_command() |
0 commit comments