Created
November 9, 2023 14:36
-
-
Save mgaitan/3201a42c72a8e8560b34c6fd559fdb90 to your computer and use it in GitHub Desktop.
Replaces a target commit with multiple smaller commits
This file contains 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 | |
""" | |
Replaces a target commit with multiple smaller commits and then reapplies | |
the subsequent commits on top of it. The original commit is split by each changed file, and then subsequent | |
commits are cherry-picked on top of the new split commits. | |
It's useful to fine-grain a `git bisect` to find a problematic change. | |
Usage: | |
git split-commit <commit> | |
""" | |
import subprocess | |
import argparse | |
def split_commit(commit_hash): | |
# Find all commits after the target commit up to the original HEAD. | |
original_head = subprocess.check_output(['git', 'rev-parse', 'HEAD'], text=True).strip() | |
subsequent_commits = subprocess.check_output( | |
['git', 'log', '--first-parent', '--format=%H', f"{commit_hash}..{original_head}"], text=True).strip().split("\n") | |
subsequent_commits = reversed([commit for commit in subsequent_commits if commit]) | |
# Checkout to a new temporary branch based on the commit before the target commit. | |
subprocess.run(['git', 'reset', '--hard', f"{commit_hash}^"], check=True) | |
start_range = subprocess.check_output(['git', 'rev-parse', 'HEAD'], text=True).strip() | |
# Cherry-pick the target commit but do not commit, keeping changes in the index. | |
subprocess.run(['git', 'cherry-pick', '--no-commit', commit_hash], check=True) | |
# Get the list of files that were part of the target commit. | |
files_changed = subprocess.check_output(['git', 'diff', '--cached', '--name-only'], text=True).strip().split('\n') | |
# Commit each file individually. | |
for file in files_changed: | |
print(f"Committing {file}...") | |
subprocess.run(['git', 'reset', '--', '.'], check=True) | |
subprocess.run(['git', 'add', '--', file], check=True) | |
subprocess.run(['git', 'commit', '-nm', f"Split change from {commit_hash}: {file}"], check=True) | |
# Cherry-pick each subsequent commit. | |
for commit in subsequent_commits: | |
try: | |
subprocess.run(['git', 'cherry-pick', commit], check=True) | |
except subprocess.CalledProcessError: | |
# is a merge? | |
subprocess.run(['git', 'cherry-pick', '-m', '1', commit], check=True) | |
print(f"Replaced commit {commit_hash} with multiple smaller commits and reapplied subsequent commits.") | |
print(f"You can start git bisect in {start_range} ") | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description=__doc__) | |
parser.add_argument('commit_hash', type=str, help='The hash of the commit to split.') | |
args = parser.parse_args() | |
# Call the function with the provided commit hash. | |
split_commit(args.commit_hash) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment