def migrate_branch()

in tools/infra_scripts/migration/migration.py [0:0]


def migrate_branch(monorepo, unmerged_repo, branch_name_to_migrate):
    subdir = get_subdirectory(unmerged_repo)
    if not subdir:
        error(f"Unknown repository name: {unmerged_repo}")
        return False

    repo_name = Path(unmerged_repo).stem

    base_dir = Path.cwd()
    monorepo_dir = setup_monorepo(monorepo)
    if not monorepo_dir:
        error("Failed to setup monorepo")
        return False

    unmerged_dir = base_dir / "temp_unmerged"
    if not setup_unmerged_repo(unmerged_repo, unmerged_dir):
        error("Failed to setup unmerged repo")
        return False

    os.chdir(unmerged_dir)
    result = subprocess.run(["git", "checkout", branch_name_to_migrate], check=False)
    if result.returncode != 0:
        error(f"Failed to checkout branch {branch_name_to_migrate}")
        return False

    print("Running git-filter-repo...")
    result = subprocess.run(["git-filter-repo", "--to-subdirectory-filter", subdir, "--force"], check=False)
    if result.returncode != 0:
        error("git-filter-repo failed")
        return False

    os.chdir(base_dir / monorepo_dir)
    subprocess.run(["git", "remote", "add", "unmerged", str(unmerged_dir)], check=True)
    subprocess.run(["git", "fetch", "unmerged"], check=True)

    new_branch = f"{subdir}/{branch_name_to_migrate}"
    subprocess.run(["git", "checkout", "-b", new_branch], check=True)

    result = subprocess.run([
        "git", "merge", f"unmerged/{branch_name_to_migrate}", 
        "--allow-unrelated-histories", 
        "-m", f"Migrate {branch_name_to_migrate} from {repo_name} to {subdir}"
    ], check=False)
    if result.returncode != 0:
        error("Failed to merge branch")
        return False

    subprocess.run(["git", "remote", "remove", "unmerged"], check=True)

    print(f"Migration completed. New branch '{new_branch}' created in {monorepo_dir}")
    print("You can now push this branch to your fork of the monorepo.")

    os.chdir(base_dir)
    shutil.rmtree(unmerged_dir)

    return True