Usethis: Authentication error when using pr_init()

Created on 16 Jul 2019  Â·  5Comments  Â·  Source: r-lib/usethis

I'm working through the nice new pull request helpers vignette and have run into a problem when attempting to use pr_init().

I am on a Windows 7 machine and use SSH transport protocol. git2r::cred_ssh_key() returns the correct info so I use use_git_credentials(credentials = git2r::cred_ssh_key()) prior to forking and cloning with create_from_github() to successfully clone a repo.

However, in the next step, where I make a branch with pr_init(), I get an authentication error from git2r::fetch() (after setting my credentials again with use_git_credentials()).

pr_init("makenew")
✔ Checking that local branch 'master' has the changes in 'origin/master'
Error in git2r::fetch(repo, name = remref_remote(remref), refspec = branch,  : 
  Error in 'git2r_remote_fetch': error authenticating: failed connecting agent

After poking through the innards of pr_init(), I think this error may be stemming from git_branch_compare(). This function relies on git2r::fetch() but never sets up the credentials for fetching. I can reproduce the error by running git_branch_compare() directly.

usethis:::git_branch_compare("master")
Error in git2r::fetch(repo, name = remref_remote(remref), refspec = branch,  : 
  Error in 'git2r_remote_fetch': error authenticating: failed connecting agent

If I add a line like credentials = git_credentials() within git2r::fetch() in git_branch_compare() I get output instead of an error. I don't know if credentials should need to be explicitly set in git2r::fetch() or not. If not, I may have something else going on with my credentials set-up.

bug git

Most helpful comment

I do think SSH is the best plan long term. But git2r somehow struggles to find the same SSH keys that command Git can find. So it's more prone to failure and we are very tired of helping people troubleshoot this issue.

All 5 comments

Ooh! I think you are onto something here. I think it will have to be handled more like the way it is in pr_fetch(), but yes this is a good line of thinking.

https://github.com/r-lib/usethis/blob/4fcc338b4b085ec11db08251cba0f980dd8961b1/R/pr.R#L153-L161

Yes I can verify that pr_init() (well, actually, git_branch_compare()) needs to have similar credential logic as pr_fetch() and have added it locally. I will push a fix soon.

Along the way, I learned another interesting fact about HTTPS vs. SSH, that maybe I should have known already but never thought about. You can clone and potentially fetch from a public repo, without credentials, if you use the HTTPS protocol. But if you use SSH, fetch (but not clone?) requires credentials, even though you're asking for public resources. Perhaps this should have been obvious to me before, but it was not. I think it's even more reason to recommend that people start with HTTPS.

@aosmith16 I'd very interested to confirm that this fixes your problem.

It works! :tada: Thanks so much for the quick fix.

Interesting note about HTTPS. I set up SSH before the era of Happy Git with R, and I found it pretty easy so never gave things a second thought when working with students to get their system set up. I'll def rethink that now.

I do think SSH is the best plan long term. But git2r somehow struggles to find the same SSH keys that command Git can find. So it's more prone to failure and we are very tired of helping people troubleshoot this issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xrobin picture xrobin  Â·  7Comments

jennybc picture jennybc  Â·  8Comments

hadley picture hadley  Â·  5Comments

llrs picture llrs  Â·  3Comments

jennybc picture jennybc  Â·  6Comments