mirror of
https://github.com/sunnypilot/sunnypilot.git
synced 2026-02-18 18:53:55 +08:00
* Refactor squash and merge script for improved simplicity
Simplified the squash_and_merge.py script by replacing redundant utility functions and consolidating logic. Enhanced usability by aligning command-line arguments and leveraging streamlined git operations to improve maintainability and reliability.
* Fix argument names in squash PR script
Renamed CLI arguments from '--base' and '--source' to '--target' and '--base' to align with expected input format. This ensures the script runs correctly with proper argument mapping.
* Fix incorrect base branch argument in squash script
Updated the `--base` argument to use `source_branch` instead of `branch` to ensure the squash script processes the correct base branch. Also adjusted the command to include `branch` as a separate argument for clarity and correctness.
* Reset to a clean state after squash error.
Add a `git reset --hard` command to ensure the repository returns to a clean state after encountering errors during the squash and merge process. This prevents lingering changes from affecting subsequent operations.
* Improve error handling in squash_and_merge_prs.py
Capture and display both stdout and stderr in error cases to provide more informative feedback. Adjust the PR comment to include available output for better debugging.
* Refactor PR squash process to enhance error handling.
Modify subprocess handling to use `result.returncode` for error checks instead of relying on exceptions. Consolidate error output retrieval and logging for better clarity, while maintaining the workflow for resetting changes on failure.
* Fix incorrect return in PR processing loop
Replaced `return` with `continue` to ensure all PRs in the loop are processed before exiting. This prevents premature termination of the function and ensures accurate success count reporting.
* Simplify subprocess output handling in squash_and_merge.py
Replaced labeled print statements with direct output of stdout and stderr. This change ensures cleaner logs and remains consistent with the function's purpose of output handling during subprocess execution.
* Update subprocess.run calls to use capture_output parameter
Replaced `stdout` and `stderr` with the `capture_output` parameter for cleaner and more concise subprocess handling. Also removed extraneous whitespace for improved code readability.
* testing moving the squash script given that it's called iteratively and switching branch might miss it
* format
---------
Co-authored-by: Jason Wen <haibin.wen3@gmail.com>
55 lines
2.1 KiB
Python
Executable File
55 lines
2.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import subprocess
|
|
import sys
|
|
import argparse
|
|
|
|
|
|
def run_git_command(command, check=True):
|
|
"""
|
|
Runs a git command and returns the trimmed stdout output.
|
|
Exits the script if the command fails.
|
|
"""
|
|
print(f"Running: {' '.join(command)}")
|
|
result = subprocess.run(command, capture_output=True, text=True)
|
|
if check and result.returncode != 0:
|
|
print(result.stdout.strip())
|
|
print(result.stderr.strip())
|
|
sys.exit(result.returncode)
|
|
return result.stdout.strip()
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="Merge multiple branches with squash merges.")
|
|
parser.add_argument("--base", required=True, help="The base branch name from which the target branch will be created.")
|
|
parser.add_argument("--target", required=True, help="The target branch name to merge into.")
|
|
parser.add_argument("--title", required=False, help="Title for the commit")
|
|
|
|
parser.add_argument("branches", nargs="+", help="List of branch names to merge into the target branch.")
|
|
args = parser.parse_args()
|
|
|
|
# Checkout the base branch to ensure a common starting point.
|
|
run_git_command(["git", "checkout", args.base])
|
|
|
|
# Check if the target branch exists. If not, create it from the base branch.
|
|
branch_list = run_git_command(["git", "branch"], check=False)
|
|
branch_names = [line.strip("* ").strip() for line in branch_list.splitlines()]
|
|
if args.target in branch_names:
|
|
run_git_command(["git", "checkout", args.target])
|
|
else:
|
|
run_git_command(["git", "checkout", "-b", args.target])
|
|
|
|
# Iterate over each branch, merging it with a squash merge.
|
|
for branch in args.branches:
|
|
print(f"Merging branch '{branch}' with a squash merge.")
|
|
# Merge the branch without creating a merge commit.
|
|
run_git_command(["git", "merge", "--squash", branch])
|
|
# Commit the squashed changes with an appropriate message.
|
|
commit_message = args.title or f"Squashed merge of branch '{branch}'"
|
|
run_git_command(["git", "commit", "-m", commit_message])
|
|
|
|
print(f"All branches have been merged with squashed commits into '{args.target}'.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|