branch check for checked out branch

This commit is contained in:
Christos Choutouridis 2025-06-27 13:34:18 +03:00
parent 2fb0b910a9
commit 46390729a4

View File

@ -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. Synchronizes a repository from from_entry to to_entry.
Executes git fetch from the source and git push to the destination. 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 _, _, from_remote, from_url, local_dir = from_entry
_, _, to_remote, to_url, _ = to_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: else:
if not dry_run: if not dry_run:
try: 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() remotes = subprocess.check_output(["git", "branch", "-r"], cwd=local_dir).decode().splitlines()
for r in remotes: for r in remotes:
if f'{from_remote}/' in r and '->' not in r: if f'{from_remote}/' in r and '->' not in r:
branch = r.strip().split(f'{from_remote}/')[1] 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: except subprocess.CalledProcessError as e:
print(f"Error: Failed to create tracking branches in {local_dir}\nMessage: {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 or update tracking branches in {local_dir}\nMessage: {e}")
sys.exit(1) sys.exit(1)
run_git(["git", "push", to_remote, "--all"], cwd=local_dir, dry_run=dry_run) 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) run_git(["git", "push", to_remote, "--tags"], cwd=local_dir, dry_run=dry_run)