-1

recently I wanted to copy files from a repo, in rust using Repository::clone, but this function does not support branches. I wanted to ask how to copy files from a branch.

I am currently using git2 version 0.17.1, and rust version 1.69.0 (84c898d65 2023-04-16).

    let repo = match Repository::clone(repo_url, temp_dir.path()) {
        Ok(repo) => repo,
        Err(e) => panic!("failed to clone: {}", e),
    };

    println!("Cloned {} to {}", repo_url, temp_dir.path().display());
    repo.remote_anonymous(repo_url)?
        .fetch(&[branch_name], None, None)?;

    let head = repo.head().unwrap();
    let oid = head.target().unwrap();
    let commit = repo.find_commit(oid).unwrap();
    let branch = repo
        .branch(branch_name, &commit, false)
        .expect("Faild to find branch");

    let obj = repo
        .revparse_single(&("refs/heads/".to_owned() + branch_name))
        .unwrap();

    repo.set_head(&("refs/heads/".to_owned() + branch_name));

    repo.checkout_tree(&obj, None);

This is the code I tried. The branch has been changed. But I don't know how to swap the files from that branch with the ones that have already been copied.

malezjaa
  • 7
  • 2

1 Answers1

2

If you read the documentation for clone, it tells you this:

See the RepoBuilder struct for more information.

Then if you go to RepoBuilder, there's a branch method.

git2::build::RepoBuilder::new()
    .branch(branch_name)
    .clone(repo_url, temp_dir.path())
drewtato
  • 6,783
  • 1
  • 12
  • 17