git2-rs icon indicating copy to clipboard operation
git2-rs copied to clipboard

Better Docs for Fetch/Possible Fetch Bug

Open gluax opened this issue 3 years ago • 11 comments

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

gluax avatar Jan 21 '22 21:01 gluax

you simply fetch with malformed refspec, while you build the correct one for find_reference

extrawurst avatar Jan 21 '22 23:01 extrawurst

btw. you can also fetch all via an empty slice: &[] as &[&str]

extrawurst avatar Jan 21 '22 23:01 extrawurst

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.

gluax avatar Jan 21 '22 23:01 gluax

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

extrawurst avatar Jan 21 '22 23:01 extrawurst

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.

gluax avatar Jan 21 '22 23:01 gluax

git2-rs is just a thin wrapper around libgit2. their docs are pretty complete. so this can be closed, right?

extrawurst avatar Jan 21 '22 23:01 extrawurst

I would say this issue still exists in git2 since we can't reproduce the correct behavior, even if it would work libgit2.

gluax avatar Jan 21 '22 23:01 gluax

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

extrawurst avatar Jan 21 '22 23:01 extrawurst

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.

kim avatar Jan 22 '22 08:01 kim

@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.

ehuss avatar Jan 24 '22 14:01 ehuss

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.

gluax avatar Jan 24 '22 18:01 gluax