git2r
git2r copied to clipboard
Error handling when push is rejected by remote host
GitHub has a new feature where it will reject a git push
signed with the user's email if they have enabled the setting Block command line pushes that expose my email.
When running Git in the terminal, it is clear that the push failed and why. Here is the error message when I temporarily enable this privacy setting:
$ git push origin master
Counting objects: 24, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (17/17), done.
Writing objects: 100% (24/24), 2.99 KiB | 611.00 KiB/s, done.
Total 24 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
remote: error: GH007: Your push would publish a private email address.
remote: You can make your email public or disable this protection by visiting:
remote: http://github.com/settings/emails
To https://github.com/jdblischak/test-email.git
! [remote rejected] master -> master (push declined due to email privacy restrictions)
error: failed to push some refs to 'https://github.com/jdblischak/test-email.git'
However, when I run git2r::push()
there is no error message at all. The returned value is NULL
, which is also what is returned for a successful push.
library(git2r)
repo <- repository()
credentials <- cred_user_pass("jdblischak")
result <- push(repo, name = "origin", refspec = "refs/heads/master", credentials = credentials)
str(result)
## NULL
But then it does fail when trying to configure the branch to track the remote branch:
branch_set_upstream(branch = repository_head(repo), name = "origin/master")
## Error in branch_set_upstream(branch = repository_head(repo), name = "origin/master") :
## Error in 'git2r_branch_set_upstream': cannot set upstream for branch 'master'
Ideally the call to git2r::push()
would trigger an error that my code could catch with tryCatch()
, and the error message would include the reason "remote: error: GH007: Your push would publish a private email address."
Is it possible to make git2r::push()
behave more like git push
when the push is rejected by the remote host?
Thanks, I wasn't aware of that feature.
However, when I run git2r::push() there is no error message at all. The returned value is NULL, which is also what is returned for a successful push.
So the push is rejected, but without an error message?
Is it possible to make git2r::push() behave more like git push when the push is rejected by the remote host?
Unfortunately I have no idea if it's possible, for example, to change the libgit2 push options (https://libgit2.org/libgit2/#HEAD/type/git_push_options) in the code below to trigger the reject error. One of the option parameters is used to pass callbacks for the push operation, and maybe one of the callbacks can be used to identify the reject and trigger an error? One interesting callback is the git_push_update_reference_cb (https://github.com/libgit2/libgit2/blob/HEAD/include/git2/remote.h#L464)
https://github.com/ropensci/git2r/blob/e2d2cd6add39741ae969f864551ed95489738b34/src/git2r_push.c#L97
The git2r
error is triggered by the return value from the libgit2 function git_remote_push
(https://libgit2.org/libgit2/#HEAD/group/remote/git_remote_push), so the key is to understand how to trigger it to return an error code
Interesting that git2r_branch_set_upstream
triggers an error.
Thanks, I wasn't aware of that feature.
Yeah, I was also stumped when I first saw the error during a workshop. I was able to figure it out only after searching the error message afterwards.
So the push is rejected, but without an error message?
Correct. The call to git2r::push()
completes and returns invisible(NULL)
. But no new commits have been pushed to GitHub.
Unfortunately I have no idea if it's possible, for example, to change the libgit2 push options (https://libgit2.org/libgit2/#HEAD/type/git_push_options) in the code below to trigger the reject error.
And sadly I can't be of much help investigating the C code. But if you try to implement something, I'm happy to test it out.