r-lib / gitcreds

Query git credentials from R
https://gitcreds.r-lib.org/
Other
27 stars 10 forks source link

gitcreds::gitcreds_set() does not persist #38

Closed DanChaltiel closed 2 years ago

DanChaltiel commented 2 years ago

Hi,

I'm experiencing a bug when using gitcreds::gitcreds_set(). This looks like https://github.com/r-lib/gitcreds/issues/30 but reading it unfortunately did not help me.

Coming from https://usethis.r-lib.org/articles/git-credentials.html#get-a-personal-access-token-pat, I created a PAT from the GitHub web interface and then tried to set it using gitcreds:

> gitcreds::gitcreds_set()

? Enter password or token: ghp_my_token
-> Adding new credentials...
-> Removing credetials from cache...
-> Done.

> gitcreds::gitcreds_get()
Error in throw(new_error("gitcreds_no_credentials", url = url)) : 
  Could not find any credentials
> gitcreds::gitcreds_delete() #no effect, no message

While there is no error in gitcreds_set, it seemed to have no effect.

I'm not really a git expert so I have no clue how to investigate this bug, so feel free to ask for anything.

Here are more info:

> sessionInfo()
R version 4.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)

> gitcreds::gitcreds_list_helpers()
[1] "manager"

> gitcreds::gitcreds_list()
[[1]]
<oskeyring_windows_item: generic>
 target_name: git:https://github.com
 persist: local_machine
 username: PersonalAccessToken
 credential_blob: <-- hidden -->

> usethis::git_sitrep() 
Git config (global)
* Name: 'Dan Chaltiel'
* Email: 'dan.chaltiel@gmail.com'
* Global (user-level) gitignore file: 'C:/Users/Dan/.gitignore'
* Vaccinated: TRUE
* Default Git protocol: 'https'
* Default initial branch name: <unset>
GitHub
* Default GitHub host: 'https://github.com'
* Personal access token for 'https://github.com': <unset>
* To create a personal access token, call `create_github_token()`
* To store a token for current and future use, call `gitcreds::gitcreds_set()`
i Read more in the 'Managing Git(Hub) Credentials' article:
  https://usethis.r-lib.org/articles/articles/git-credentials.html
Git repo for current project
* Active usethis project: 'F:/GITHUB/crosstable'
* Default branch: 'main'
* Current local branch -> remote tracking branch:
  'main' -> 'origin/main'
GitHub remote configuration
* Type = 'maybe_ours_or_theirs'
* Host = 'https://github.com'
* Config supports a pull request = NA
* origin = 'DanChaltiel/crosstable'
* upstream = <not configured>
* Desc = 'origin' is a GitHub repo and 'upstream' is either not configured or is not a GitHub repo.

  We may be offline or you may need to configure a GitHub personal access
  token. `gh_token_help()` can help with that.

  Read more about what this GitHub remote configurations means at:
  'https://happygitwithr.com/common-remote-setups.html'
gaborcsardi commented 2 years ago

Can you run gitcreds::gitcreds_list_helpers()?

DanChaltiel commented 2 years ago

It is already written in my post 😊

gaborcsardi commented 2 years ago

Right. What is your git version?

Can you run this from a terminal/cmd window, from the project's directory:

git credential fill
url=https://github.com

You need type in the url=https://github.com part without a prompt and then press ENTER twice. Does this print your PAT? The output should look like this:

protocol=https
host=github.com
username=PersonalAccessToken
password=<your GH PAT>
DanChaltiel commented 2 years ago

Thanks for your help, here is my version of git:

system("git --version", intern = TRUE)
[1] "git version 2.12.0.windows.1"

BTW, I'm using GitHub Desktop if it matters somehow.

For the credentials, running in a windows console, it fails:

F:\GITHUB\crosstable>git credential fill
url=https://github.com

warning: invalid credential line: ?
fatal: unable to read credential from stdin

But as with others, it works in a git Bash, even if there is still an error in my case:

Dan@DESKTOP-win10 MINGW64 /f/GITHUB/crosstable (main)
$ git credential fill
url=https://github.com

fatal: AggregateException encountered.
   Une ou plusieurs erreurs se sont produites. /*meaning "one or several errors happened"*/
protocol=https
host=github.com
username=PersonalAccessToken
password=ghp_<my GH PAT>

I guess this means that gitcreds_set() worked and that the problem lies in gitcreds_get() somehow.

EDIT:

The output of git config --list prints two lines credential.helper=manager and credential.helper=wincred while gitcreds::gitcreds_list_helpers() returns only the first, hence manager.

gaborcsardi commented 2 years ago

gitcreds needs at least git 2.12.1 to work on Windows, see "git versions" at https://gitcreds.r-lib.org/reference/gitcreds_get.html or in the manual.

If you can update git to a recent version, that is the best, 2.12.x is not supported any more even by git.

DanChaltiel commented 2 years ago

Oh OK thanks a lot for your patience! I updated git and it works now, I'm sorry I should have thought about it.

Maybe a quick check to system("git --version", intern = TRUE) might belong in gitcreds::gitcreds_set()?

gaborcsardi commented 2 years ago

Maybe a quick check to system("git --version", intern = TRUE) might belong in gitcreds::gitcreds_set()?

Parsing the git version from the output is surprisingly hard, unfortunately, there are many variations.