fn set_up_remotes()

in focus/operations/src/clone.rs [732:842]


fn set_up_remotes(
    dense_repo: &Repository,
    sparse_repo: &Repository,
    main_branch_name: &str,
    app: Arc<App>,
) -> Result<()> {
    let remotes = dense_repo
        .remotes()
        .context("Failed to read remotes from dense repo")?;

    let sparse_workdir = sparse_repo
        .workdir()
        .expect("Could not determine sparse repo workdir");

    for remote_name in remotes.iter() {
        let remote_name = match remote_name {
            Some(name) => name,
            None => continue,
        };

        let dense_remote = dense_repo.find_remote(remote_name)?;
        let maybe_fetch_url = if let Some(maybe_url) = dense_remote.url() {
            maybe_url
        } else {
            bail!("Dense remote '{}' has no URL", remote_name);
        };

        let maybe_push_url = dense_remote.pushurl().unwrap_or(maybe_fetch_url);
        debug!(?remote_name, fetch_url = ?maybe_fetch_url, push_url = ?maybe_push_url, "Setting up remote");

        let maybe_fetch_url = match Url::parse(maybe_fetch_url) {
            Ok(mut url) => {
                if let Some(host) = url.host() {
                    if cfg!(feature = "twttr") {
                        // Apply Twitter-specific remote treatment.
                        if host.to_string().eq_ignore_ascii_case("git.twitter.biz") {
                            // If the path for the fetch URL does not begin with '/ro', add that prefix.
                            if !url.path().starts_with("/ro") {
                                url.set_path(&format!("/ro{}", url.path()));
                            }
                        }
                    }
                }
                url.as_str().to_owned()
            }
            Err(_) => {
                info!(
                    "Fetch URL ('{}') for remote {} is not a URL",
                    maybe_fetch_url, remote_name
                );
                maybe_fetch_url.to_owned()
            }
        };
        if Url::parse(maybe_push_url).is_err() {
            info!(
                "Push URL ('{}') for remote {} is not a URL",
                maybe_push_url, remote_name
            )
        }

        // Delete existing remote in the sparse repo if it exists. This is a workaround because `remote_delete` is not working correctly.
        if sparse_repo.find_remote(remote_name).is_ok() {
            let (mut cmd, scmd) = git_helper::git_command(app.clone())?;
            let _ = scmd.ensure_success_or_log(
                cmd.current_dir(sparse_workdir)
                    .arg("remote")
                    .arg("remove")
                    .arg(remote_name),
                SandboxCommandOutput::Stderr,
            )?;
        }

        // Add the remote to the sparse repo
        info!(
            "Setting up remote {} fetch:{} push:{}",
            remote_name, maybe_fetch_url, maybe_push_url
        );
        sparse_repo
            .remote_with_fetch(
                remote_name,
                maybe_fetch_url.as_str(),
                &format!(
                    "refs/heads/{main_branch_name}:refs/remotes/{remote_name}/{main_branch_name}"
                ),
            )
            .with_context(|| {
                format!(
                    "Configuring fetch URL remote {} in the sparse repo failed",
                    &remote_name
                )
            })?;

        sparse_repo
            .config()?
            .set_str(
                format!("remote.{}.tagOpt", remote_name).as_str(),
                "--no-tags",
            )
            .with_context(|| format!("setting remote.{}.tagOpt = --no-tags", &remote_name))?;

        sparse_repo
            .remote_set_pushurl(remote_name, Some(maybe_push_url))
            .with_context(|| {
                format!(
                    "Configuring push URL for remote {} in the sparse repo failed",
                    &remote_name
                )
            })?;
    }
    Ok(())
}