From f12e7501948415e4d5b3d8b663a34abcd1a70bfa Mon Sep 17 00:00:00 2001 From: "Jon Michael Aanes (aider)" Date: Sun, 13 Apr 2025 18:02:05 +0200 Subject: [PATCH] feat: add commit check before pushing branch changes --- aider_gitea/__main__.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/aider_gitea/__main__.py b/aider_gitea/__main__.py index 550a448..f6c2837 100644 --- a/aider_gitea/__main__.py +++ b/aider_gitea/__main__.py @@ -147,6 +147,21 @@ def push_changes(cwd: Path, branch_name: str, issue_number: str, issue_title: st ] run_cmd(cmd, cwd) +def has_commits_on_branch(cwd: Path, base_branch: str, current_branch: str) -> bool: + """Check if there are any commits on the current branch that aren't in the base branch.""" + try: + result = subprocess.run( + ["git", "log", f"{base_branch}..{current_branch}", "--oneline"], + check=True, + cwd=cwd, + capture_output=True, + text=True + ) + return bool(result.stdout.strip()) + except subprocess.CalledProcessError: + logger.exception(f"Failed to check commits on branch {current_branch}") + return False + def run_cmd(cmd: list[str], cwd:Path|None=None) -> None: print(cmd) subprocess.run(cmd, check=True, cwd=cwd) @@ -160,7 +175,12 @@ def process_issue(args, tmpdirname: Path, branch_name: str, issue_title: str, is run_cmd(["git", "checkout", "-b", branch_name], tmpdirname) run_cmd(create_aider_command(f'# {issue_title}\n{issue_description}'), tmpdirname) run_cmd(["git", "add", "."], tmpdirname) - push_changes(tmpdirname, branch_name, issue_number, issue_title, issue_description, args.base_branch) + + # Check if there are any commits on the branch before pushing + if has_commits_on_branch(tmpdirname, args.base_branch, branch_name): + push_changes(tmpdirname, branch_name, issue_number, issue_title, issue_description, args.base_branch) + else: + logger.info(f"No commits made on branch {branch_name}, skipping push") def handle_issues(args, client, seen_issues_db): """Process all open issues with the 'aider' label."""