gitcreds

Query git credentials from R

R build status Codecov test coverage R-CMD-check

Features

Installation

Install the package from CRAN:

install.packages("gitcreds")

Usage

gitcreds is typically used upstream, in R packages that need to authenticate to git or GitHub. End users of these packages might still find it useful to call gitcreds directly, to set up their credentials, or check that they have been set up correctly.

You can also use gitcreds in an R script. In this case you are both the end user and the upstream developer.

Usage as an end user

library(gitcreds)

Use gitcreds_get() to check your GitHub or other git credentials. It returns a named list, with a password entry. The password is not printed by default:

gitcreds_get()
#> <gitcreds>
#>   protocol: https
#>   host    : github.com
#>   username: gaborcsardi
#>   password: <-- hidden -->

Use gitcreds_set() to add new credentials, or replace existing ones. It always asks you before replacing existing credentials:

gitcreds_set()
#> -> Your current credentials for 'https://github.com':
#> 
#>   protocol: https
#>   host    : github.com
#>   username: gaborcsardi
#>   password: <-- hidden -->
#> 
#> -> What would you like to do?
#> 
#> 1: Keep these credentials
#> 2: Replace these credentials
#> 3: See the password / token
#> 
#> Selection: 2
#> 
#> ? Enter new password or token: secret
#> -> Removing current credentials...
#> -> Adding new credentials...
#> -> Removing credentials from cache...
#> -> Done.

Use gitcreds_delete() to delete credentials. It always asks you before actually deleting any credentials:

gitcreds_delete()
#> -> Your current credentials for 'https://github.com':
#> 
#>   protocol: https
#>   host    : github.com
#>   username: token
#>   password: <-- hidden -->
#> 
#> -> What would you like to do?
#> 
#> 1: Keep these credentials
#> 2: Delete these credentials
#> 3: See the password / token
#> 
#> Selection: 2
#> -> Removing current credentials...
#> -> Removing credentials from cache...
#> -> Done.

Usage as a package author

If you want to use git’s credentials in your package, call gitcreds_get(). You probably want to handle the various errors it can return. Here is an example for a function that optionally neeeds a GitHub token. It searches the code of a GitHub repository:

github_search <- function(query, repo = "wch/r-source") {
  token <- tryCatch(
    gitcreds::gitcreds_get(),
    error = function(e) NULL
  )

  url <- "https://api.github.com/search/code"
  q <- list(q = paste0(query, "+repo:", repo))
  token <- paste0("token ", token$password)

  httr::GET(url, query = q, httr::add_headers(Authorization = token))
}

The next example always needs a GitHub token, so it fails without one. It lists the public repositories of the current user:

msg <- function(wh) {
  msgs <- c(
    no_git = paste0(
      "No git installation found. You need to install git and set up ",
      "your GitHub Personal Access token using `gitcreds::gitcreds_set()`."),
    no_creds = paste0(
      "No git credentials found. Please set up your GitHub Personal Access ",
      "token using `gitcreds::gitcreds_set()`.")
    )
  msgs[wh]
}

my_private_repos <- function() {
  token <- tryCatch(
    gitcreds::gitcreds_get(),
    gitcreds_nogit_error = function(e) stop(msg("no_git")),
    gitcreds_no_credentials = function(e) stop(msg("no_creds"))    
  )

  url <- "https://api.github.com/user/repos"
  q <- list(visibility = "public")
  token <- paste0("token ", token$password)

  httr::GET(url, query = q, httr::add_headers(Authorization = token))
}

Point your users to gitcreds_set() for adding/updating their credentials, or write your own wrapper for this.

If you want more control or a different UI, take a look at the lower level gitcreds_fill(), gitcreds_approve() and gitcreds_reject() functions.

See also gitcreds for package authors.

Code of Conduct

Please note that the gitcreds project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

License

MIT © RStudio