def _setup_azure_cli_repo()

in azdev/operations/setup.py [0:0]


def _setup_azure_cli_repo(cli_path):
    if cli_path and cli_path != 'EDGE':
        # Store original directory
        original_dir = os.getcwd()
        try:
            # Change to CLI repo root directory
            os.chdir(cli_path)

            display(f"\nSetting up Azure CLI repo: {cli_path}\n")
            # Change git hooks path
            _change_git_hooks_path(cli_path)

            # Check existing remotes
            remotes = subprocess.check_output(['git', 'remote', '-v'], text=True)

            # If upstream already exists, nothing to do
            if 'upstream' in remotes:
                return

            # Check origin remote URL
            origin_url = None
            for line in remotes.splitlines():
                if line.startswith('origin') and '(fetch)' in line:
                    origin_url = line.split()[1]
                    break

            # Only add upstream if origin is an azure-cli fork
            if origin_url and origin_url.endswith('/azure-cli.git'):
                upstream_url = 'https://github.com/Azure/azure-cli.git'
                subprocess.check_call(['git', 'remote', 'add', 'upstream', upstream_url])
                display(f"Added upstream remote: {upstream_url}")
                # fetch the upstream/dev branch
                subprocess.check_call(['git', 'fetch', 'upstream', 'dev'])
                display(f"Fetched upstream/dev branch for CLI in {cli_path}")
        except subprocess.CalledProcessError as e:
            logger.warning("Failed to add upstream remote: %s", str(e))
        finally:
            # Always return to original directory
            os.chdir(original_dir)