From 46390729a4689819a3b1295aef861cfd6eed651e Mon Sep 17 00:00:00 2001 From: Christos Choutouridis Date: Fri, 27 Jun 2025 13:34:18 +0300 Subject: [PATCH] branch check for checked out branch --- git-synchro.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/git-synchro.py b/git-synchro.py index c66449c..fc2c74e 100644 --- a/git-synchro.py +++ b/git-synchro.py @@ -64,7 +64,7 @@ def sync_repo(nick, from_entry, to_entry, use_mirror, dry_run=False): """ Synchronizes a repository from from_entry to to_entry. Executes git fetch from the source and git push to the destination. - If --mirror is used, performs full push of all refs. Otherwise, creates local tracking branches and pushes them without checking out. + If --mirror is used, performs full push of all refs. Otherwise, creates or updates local tracking branches and pushes them without checking out. """ _, _, from_remote, from_url, local_dir = from_entry _, _, to_remote, to_url, _ = to_entry @@ -90,13 +90,20 @@ def sync_repo(nick, from_entry, to_entry, use_mirror, dry_run=False): else: if not dry_run: try: + current_branch = subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"], cwd=local_dir).decode().strip() remotes = subprocess.check_output(["git", "branch", "-r"], cwd=local_dir).decode().splitlines() for r in remotes: if f'{from_remote}/' in r and '->' not in r: branch = r.strip().split(f'{from_remote}/')[1] - subprocess.run(["git", "branch", branch, f"{from_remote}/{branch}"], cwd=local_dir) + if branch == current_branch: + try: + subprocess.run(["git", "checkout", f"{from_remote}/{branch}"], cwd=local_dir, check=True) + except subprocess.CalledProcessError as e: + print(f"Warning: Could not update currently checked-out branch '{branch}' in {local_dir}. Possible uncommitted changes.") + else: + subprocess.run(["git", "branch", "-f", branch, f"{from_remote}/{branch}"], cwd=local_dir) except subprocess.CalledProcessError as e: - print(f"Error: Failed to create tracking branches in {local_dir}\nMessage: {e}") + print(f"Error: Failed to create or update tracking branches in {local_dir}\nMessage: {e}") sys.exit(1) run_git(["git", "push", to_remote, "--all"], cwd=local_dir, dry_run=dry_run) run_git(["git", "push", to_remote, "--tags"], cwd=local_dir, dry_run=dry_run)