Created
June 25, 2025 20:27
-
-
Save RonnyPfannschmidt/e90dec6540d6be45d63793493fe0b811 to your computer and use it in GitHub Desktop.
small python script to walk the git reflog to find a working version after rebase failure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import subprocess | |
| import sys | |
| from pathlib import Path | |
| def get_reflog_entries(): | |
| """Get list of reflog entries (commit hashes) for current branch""" | |
| # Get current branch name | |
| branch_result = subprocess.run( | |
| ["git", "branch", "--show-current"], | |
| capture_output=True, | |
| text=True, | |
| check=True | |
| ) | |
| current_branch = branch_result.stdout.strip() | |
| # Get reflog entries for current branch only | |
| result = subprocess.run( | |
| ["git", "reflog", current_branch, "--format=%H"], | |
| capture_output=True, | |
| text=True, | |
| check=True | |
| ) | |
| return result.stdout.strip().split('\n') | |
| def test_commit(commit_hash, base_path): | |
| """Test a commit by creating worktree and running checks""" | |
| worktree_path = base_path / f"test-{commit_hash[:8]}" | |
| try: | |
| # Create worktree | |
| subprocess.run( | |
| ["git", "worktree", "add", str(worktree_path), commit_hash], | |
| check=True, | |
| capture_output=True | |
| ) | |
| # Run ruff check | |
| ruff_result = subprocess.run( | |
| ["uv", "run", "ruff", "check", "--output-format=concise"], | |
| cwd=worktree_path, | |
| capture_output=True, | |
| text=True | |
| ) | |
| if ruff_result.returncode != 0: | |
| print(f"{commit_hash[:8]}: ruff failed") | |
| if ruff_result.stdout: | |
| print(ruff_result.stdout) | |
| return False | |
| # Run pytest | |
| pytest_result = subprocess.run( | |
| ["uv", "run", "pytest", "-q"], | |
| cwd=worktree_path, | |
| capture_output=True, | |
| text=True | |
| ) | |
| if pytest_result.returncode != 0: | |
| print(f"{commit_hash[:8]}: pytest failed") | |
| if pytest_result.stdout: | |
| print(pytest_result.stdout) | |
| return False | |
| print(f"{commit_hash[:8]}: SUCCESS") | |
| return True | |
| finally: | |
| # Cleanup worktree | |
| if worktree_path.exists(): | |
| subprocess.run( | |
| ["git", "worktree", "remove", str(worktree_path)], | |
| capture_output=True | |
| ) | |
| def main(): | |
| base_path = Path.cwd() | |
| reflog_entries = get_reflog_entries() | |
| for commit_hash in reflog_entries: | |
| if test_commit(commit_hash, base_path): | |
| print(f"First working commit: {commit_hash}") | |
| sys.exit(0) | |
| print("No working commit found") | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment