1#' GitHub Wrappers
2#'
3#' Fetch and checkout pull requests.
4#'
5#' By default `git_fetch_pull_requests` will download all PR branches. To
6#' remove these again simply use `git_fetch(prune = TRUE)`.
7#'
8#' @export
9#' @rdname github
10#' @inheritParams git_fetch
11#' @param pr number with PR to fetch or check out. Use `"*"` to fetch all
12#' pull requests.
13git_checkout_pull_request <- function(pr = 1, remote = NULL, repo = '.'){
14  pr <- as.character(pr)
15  if(!length(remote))
16    remote <- git_info(repo)$remote
17  local_branch <- sprintf("pr/%s", pr)
18  remote_branch <- sprintf("%s/pr/%s", remote, pr)
19  git_fetch_pull_requests(pr = pr, remote = remote, repo = repo)
20  if(git_branch_exists(local_branch)){
21    inform("Continuing on existing pr branch: %s", local_branch)
22    git_branch_checkout(local_branch, repo = repo)
23    git_pull(repo = repo)
24  } else {
25    inform("Creating new branch: %s", local_branch)
26    git_branch_create(local_branch, remote_branch, checkout = TRUE, repo = repo)
27  }
28}
29
30#' @export
31#' @rdname github
32git_fetch_pull_requests <- function(pr = '*', remote = NULL, repo = '.'){
33  pr <- as.character(pr)
34  if(!length(remote))
35    remote <- git_info(repo)$remote
36  refspec <- sprintf('+refs/pull/%s/head:refs/remotes/%s/pr/%s', pr, remote, pr)
37  git_fetch(remote = remote, refspec = refspec, repo = repo)
38  invisible(refspec)
39}
40