Bladeren bron

refactor: Improve Git context gathering and backup

This commit refactors the Git context gathering process for
clarity and adds a safety measure.

The changes include:

- Moved the backup branch creation earlier, before detailed Git
  context gathering, to ensure a backup exists even if context
  gathering fails.
- Consolidated Git context gathering into two phases: initial
  and detailed, for better organization and error handling.
- Ensured backup branch is created only if commits are found.
seno 3 dagen geleden
bovenliggende
commit
f49aed5a37
1 gewijzigde bestanden met toevoegingen van 19 en 17 verwijderingen
  1. 19 17
      git_rebase_ai.py

+ 19 - 17
git_rebase_ai.py

@@ -1043,8 +1043,22 @@ def rebase(args):
     upstream_ref = args.upstream_ref
     logging.info(f"Comparing against reference: {upstream_ref}")
 
-    # --- Safety: Create Backup Branch ---
-    # Always create backup, especially if attempting auto-rebase
+    # --- Gather Initial Git Context ---
+    print("\nGathering initial Git context...")
+    commit_range, merge_base = get_commit_range(upstream_ref, current_branch)
+    if not commit_range:
+        sys.exit(1)  # Error message already printed by get_commit_range
+
+    logging.info(f"Analyzing commit range: {commit_range} (Merge Base: {merge_base})")
+
+    commits = get_commits_in_range(commit_range)
+    if not commits:
+        print(
+            f"\n✅ No commits found between '{merge_base}' ({upstream_ref}) and '{current_branch}'. Nothing to rebase."
+        )
+        sys.exit(0)
+
+    # --- Safety: Create Backup Branch (only if commits exist) ---
     backup_branch = create_backup_branch(current_branch)
     if not backup_branch:
         try:
@@ -1067,21 +1081,9 @@ def rebase(args):
         print(f"     git reset --hard {backup_branch}")
         print("-" * 40)
 
-    # --- Gather Git Context ---
-    print("\nGathering Git context...")
-    commit_range, merge_base = get_commit_range(upstream_ref, current_branch)
-    if not commit_range:
-        sys.exit(1)
-
-    logging.info(f"Analyzing commit range: {commit_range} (Merge Base: {merge_base})")
-
-    commits = get_commits_in_range(commit_range)
-    if not commits:
-        logging.info(
-            f"No commits found between '{merge_base}' and '{current_branch}'. Nothing to do."
-        )
-        sys.exit(0)
-
+    # --- Gather Remaining Git Context ---
+    print("\nGathering detailed Git context for AI...")
+    # We already have commits, commit_range, merge_base from the initial check
     file_structure, changed_files_list = get_changed_files_in_range(commit_range)
     diff = get_diff_in_range(commit_range)