New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use_github("hadley/abc") #642

Closed
hadley opened this Issue Nov 10, 2014 · 13 comments

Comments

Projects
None yet
3 participants
@hadley
Member

hadley commented Nov 10, 2014

  • Create new repo on github
  • git init
  • git push
  • Add URL and BugReports to DESCRIPTION

hadley added a commit that referenced this issue Apr 29, 2015

hadley added a commit that referenced this issue Apr 29, 2015

@hadley

This comment has been minimized.

Member

hadley commented Jun 19, 2015

Why did I comment this code out?

  # git2r::branch_set_upstream(git2r::head(r), "origin/master")
  # git2r::push(r, "origin", "refs/heads/master")
@jennybc

This comment has been minimized.

Member

jennybc commented Aug 16, 2015

The recent conversation probably already reminded you ...

You probably commented this out because pushing and/or setting tracking branches with git2r is not completely sorted out yet: ropensci/git2r#133, ropensci/git2r#116, ropensci/git2r#152. FWIW I am using v0.11.0 of git2r and have still not managed to set the upstream remote with code like you've got above. Caveat: I'm using https. Maybe the alleged fix only applies to ssh?

@stewid

This comment has been minimized.

stewid commented Aug 16, 2015

I have managed to add an existing project to GitHub using SSH and https with v0.11.0 of git2r.

library(git2r)

## Add an existing project to GitHub using SSH and git2r. Create a new
## repository on GitHub before running the following R code.

## 1) Create a new repository
repo <- init("test-01")

## 2) Add 'README.md' to local repository
add(repo, "README.md")

## 3) Commit staged 'README.md'. Add sessionInfo to commit message
commit(repo, message = "Initial commit", session = TRUE)

## 4) Add URL for the remote repository
remote_add(repo, "origin", "git@github.com:stewid/test-01.git")

## 5) Create SSH credentials
cred <- cred_ssh_key("~/.ssh/id_rsa.pub", "~/.ssh/id_rsa")

## 6) Push changes in local repository to GitHub
push(repo, "origin", "refs/heads/master", credentials = cred)
library(git2r)

## Add an existing project to GitHub using https and git2r. Create a
## new repository on GitHub before running the following R code.

## 1) Create a new repository
repo <- init("test-02")

## 2) Add 'README.md' to local repository
add(repo, "README.md")

## 3) Commit staged 'README.md'. Add sessionInfo to commit message
commit(repo, message = "Initial commit", session = TRUE)

## 4) Add URL for the remote repository
remote_add(repo, "origin", "https://github.com/stewid/test-02.git")

## 5) Create credentials from environmental variables
cred <- cred_env("GITHUB_USER", "GITHUB_PAT")

## 6) Push changes in local repository to GitHub
push(repo, "origin", "refs/heads/master", credentials = cred)

sessionInfo:

R version 3.2.2 RC (2015-08-08 r68921)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.3 LTS

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] git2r_0.11.0
@stewid

This comment has been minimized.

stewid commented Aug 16, 2015

I have done more testing on other platforms with git2r v0.11.0 to push a local repository to GitHub.

R version Platform Protocol Repository
3.2.2 RC x86_64-pc-linux-gnu (64-bit) SSH test-01
3.2.2 RC x86_64-pc-linux-gnu (64-bit) https test-02
3.2.0 x86_64-apple-darwin13.4.0 (64-bit) SSH test-03
3.2.0 x86_64-apple-darwin13.4.0 (64-bit) https test-04
3.1.1 i386-pc-solaris2.10 (32-bit) SSH test-05
3.1.1 i386-pc-solaris2.10 (32-bit) https test-06
3.2.2 x86_64-w64-mingw32/x64 (64-bit) SSH test-07
3.2.2 x86_64-w64-mingw32/x64 (64-bit) https test-08
@jennybc

This comment has been minimized.

Member

jennybc commented Aug 16, 2015

Thanks @stewid for the explicit example using cred_env()! I will try that.

I guess my hope was that my cached credentials that allow other pushes to "just work", from command line git or RStudio or SourceTree, would get picked up by git2r. I have no idea if that is even realistic.

@hadley

This comment has been minimized.

Member

hadley commented Sep 8, 2015

Done in #901

@hadley hadley closed this Sep 8, 2015

@jennybc

This comment has been minimized.

Member

jennybc commented Sep 10, 2015

@stewid Can you help me out here?

I use git2r::branch_set_upstream() inside use_github() which was merged above.

I usually use https and can affirm that all is well with use_github(..., protocol = "https").

But the default protocol is ssh.

I've now set up SSH keys in order to test use_github(..., protocol = "ssh") and I get an error from git2r::branch_set_upstream(). Specifically, I get Failed to authenticate SSH session: Callback returned error. I can demonstrate all of this, including in a pure git2r example that has nothing to with devtools.

Before I go there ...

In your example above, are you using an SSH key without a passphrase? Or is your passphrase kept in a keychain or some such and git2r/libgit2 is able to access it? Because it's clearly not going into the credential object and that seems to be my problem. I can rescue things IFF I provide a passphrase.

Wondering if my ssh setup is somehow deficient or are we going to have handle the SSH passphrase somehow?

@jennybc

This comment has been minimized.

Member

jennybc commented Sep 10, 2015

OK here is the threatened/promised reprex. I assume you both use ssh? Like all the time? And your key has a passphrase? What happens for you?

path <- "foo"
dir.create(path)
repo <- init(path)
writeLines("yada yada yada", file.path(path, "example.txt"))
add(repo, "example.txt")
commit(repo, "First commit message")
#> [b3de976] 2015-09-10: First commit message
req <-
  httr::POST("https://api.github.com/user/repos",
             config = httr::authenticate(Sys.getenv("GITHUB_PAT"),
                                         "x-oauth-basic", "basic"),
             encode = "json",
             body = list(
               name = jsonlite::unbox("foo"),
               description = jsonlite::unbox("test branch_set_upstream"),
               private = jsonlite::unbox(TRUE)
             ))
httr::http_status(req)
#> $category
#> [1] "success"
#> 
#> $message
#> [1] "success: (201) Created"
httr::content(req)$ssh_url
#> [1] "git@github.com:jennybc/foo.git"
remote_add(repo, "origin", httr::content(req)$ssh_url)
cred <- cred_ssh_key("~/.ssh/id_rsa.pub", "~/.ssh/id_rsa")
push(repo, "origin", "refs/heads/master", credentials = cred)
#> Error in .local(object, ...): Error in 'git2r_push': Failed to authenticate SSH session: Callback returned error
cred <- cred_ssh_key("~/.ssh/id_rsa.pub", "~/.ssh/id_rsa", "<PASSPHRASE GOES HERE>")
push(repo, "origin", "refs/heads/master", credentials = cred)
branch_get_upstream(head(repo))
#> NULL
branch_set_upstream(git2r::head(repo), "origin/master")
branch_get_upstream(head(repo))
#> [b3de97] (origin @ git@github.com:jennybc/foo.git) master

In use_github() I use GITHUB_PAT and https to push, so the ssh error first arises on branch_set_upstream(). But the above makes the point that I can't complete the set up w/o providing the passphrase. And yes git push works when I use git from the command line: ssh-agent is configured properly and I don't need to explicitly provide my passphrase.

@stewid

This comment has been minimized.

stewid commented Sep 10, 2015

Hi @jennybc, I'm testing, will return in a moment.

@stewid

This comment has been minimized.

stewid commented Sep 10, 2015

In your example above, are you using an SSH key without a passphrase?

Yes

Or is your passphrase kept in a keychain or some such and git2r/libgit2 is able to access it?

No

I can rescue things IFF I provide a passphrase

Great

I assume you both use ssh?

Yes, I use ssh

Like all the time?

Yes

And your key has a passphrase?

Yes, but I did not use keys with a passphrase in the example above.


The ssh-agent method is only used if the credentials is NULL and authentication with ssh keys is ok.
https://github.com/ropensci/git2r/blob/master/src/git2r_cred.c#L232
Skip the credentials argument in git2r to use the ssh-agent.

push(repo, "origin", "refs/heads/master")

Does that work?

@jennybc

This comment has been minimized.

Member

jennybc commented Sep 10, 2015

OK yes that works. If I do the push via ssh, with a NULL credential object, I can subsequently set remote tracking branch. Sadly, the converse also true. I can ONLY set remote tracking branch with an ssh remote URL if I have previously pushed to said remote via ssh (not https). I can and will change use_github() logic to honor this requirement.

Is it obvious that this should be the case? Is this what you mean by "... and authentication with ssh key is ok"? Does a successful ssh push create some persistent state in git2r that affects downstream commands with no explicit credentialling? This pre-requisite and the need to use NULL credentials if the SSH key has a passphrase would be great to document in git2r (unless that's well-known and I'm just learning hard lessons about git here).

What's the intended workflow if public and private keys live in non-default location and we can't use NULL credentials?

@hadley I will make a new PR shortly to deal with this.

@stewid

This comment has been minimized.

stewid commented Sep 14, 2015

Is this what you mean by "... and authentication with ssh key is ok"?

For operations that might need credentials, e.g. push, git2r supplies the callback function git2r_cred_acquire_cb to libgit2. If the remote host requires authentication in order to connect to it, the callback git2r_cred_acquire_cb is called from libgit2. One argument to this function is the allowed credential type. If that argument says that it's OK with ssh keys but no credentials object was supplied to the operation, than git2r instructs libgit2 to try the ssh-agent for the keys.

Does a successful ssh push create some persistent state in git2r that affects downstream commands with no explicit credentialling?

No

This pre-requisite and the need to use NULL credentials if the SSH key has a passphrase would be great to document in git2r

The reason for NULL credentials is not about the passphrase. It tells git2r to tell libgit2 to try to get the keys from the ssh-agent. I will improve the documentation.

What's the intended workflow if public and private keys live in non-default location and we can't use NULL credentials?

To use the git2r cred_ssh_key function.

@jennybc thanks for your feedback. Much of this functionality is very recently implemented in git2r, your testing and suggestions to improve workflow and documentation are much appreciated.

@lock

This comment has been minimized.

lock bot commented Sep 18, 2018

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Sep 18, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.