git2-rs
git2-rs copied to clipboard
Better Docs for Fetch/Possible Fetch Bug
Fetching a remote branch does not seem to work in any capacity. If it does the docs really need to better document this behavior as it does not seem to work from any way we've tested.
repo.find_remote("origin")
.unwrap()
.fetch(&[branch.to_string()], None, None)
.unwrap();
let fetch_head = repo.find_reference("FETCH_HEAD").unwrap();
let fetch_commit = repo.reference_to_annotated_commit(&fetch_head).unwrap();
let mut reference = repo.find_reference(&format!("refs/heads/{}", branch)).unwrap(); // this line panics which means it did not properly fetch
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { code: -3, klass: 4, message: "reference 'refs/heads/staging' not found" }', leo-commands\src\repo_commands.rs:53:80
you simply fetch with malformed refspec, while you build the correct one for find_reference
btw. you can also fetch all via an empty slice: &[] as &[&str]
What does that mean? The string inside the branch variable has not been mutated. They should both be correct.
Yea we tried &[] as &[&str] still results in the same error.
Yea we tried &[] as &[&str] still results in the same error.
that should work. works for me. I suggest not relying on git2-rs docs alone. the upstream libgit2 docs are more complete
We just winded up writing a wrapper around the git exec that works.
Just wanted to report we could not reproduce fetching all nor fetching a specific branch.
git2-rs is just a thin wrapper around libgit2. their docs are pretty complete. so this can be closed, right?
I would say this issue still exists in git2 since we can't reproduce the correct behavior, even if it would work libgit2.
I can attest that it works with git2-rs, see an example in gitui: https://github.com/extrawurst/gitui/blob/master/asyncgit/src/sync/remotes/mod.rs#L82 and https://github.com/extrawurst/gitui/blob/master/asyncgit/src/sync/remotes/mod.rs#L143
we generally never fall back to the git cli for anything and pull/fetch works great. you would probably need to share way more code and the structure of your concrete case to investigate what breaks it
I suppose the confusion stems from trying to pass a branch name instead of a fetchspec (src:dst). Also, an empty slice implies that the remote has to be set up in the repo config.
@gluax Can you provide a complete example that doesn't work? I don't think the issue here is necessarily related to refspecs, but instead perhaps the repository was initialized without a local branch. I don't think fetch won't create one automatically, even with a refspec.
Either your last line should be .find_reference(&format!("refs/remotes/origin/{}", branch)), or you need to create the local branch that tracks the remote branch.
You all can close the issue if you want. We already removed git2 as a dep in favor of just using rust exec calls, so I don't have the example code anymore.