Skip to content

Instantly share code, notes, and snippets.

@wpetry
Last active July 25, 2019 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpetry/8a3334c6917010775dbac2d5ef608fb7 to your computer and use it in GitHub Desktop.
Save wpetry/8a3334c6917010775dbac2d5ef608fb7 to your computer and use it in GitHub Desktop.
Checks status of all git repos in a directory
#################################################-
## Functions to check status of git repos in a given folder ----
## W.K. Petry
##
## inspired by @djnavarro's workbch::view_git_status
#################################################-
## Helper fxn to fail gracefully for local-only repos ----
## with no upstream
#################################################-
ab_poss <- purrr::possibly(.f = ~as.data.frame(as.list(git2r::ahead_behind(local = .x, upstream = .y)),
col.names = c("ahead", "behind")),
otherwise = data.frame(ahead = NA_integer_,
behind = NA_integer_))
#################################################-
## Fetch git status of all repo ----
#################################################-
git_status <- function(path = getwd(), recursive = TRUE,
show_clean = FALSE, keep_paths = FALSE,
keep_untethered = TRUE){
require(dplyr)
require(tidyr)
require(tibble)
require(purrr)
require(git2r)
path <- gsub("\\/$", "", path) # remove trailing "/" if needed
dirs <- dir(path = path, pattern = "^.git$", recursive = recursive,
full.names = TRUE, all.files = TRUE, include.dirs = TRUE)
tbl <- tibble(local_name = basename(dirname(dirs)),
local_path = dirname(dirs)) %>%
mutate(repo_head = purrr::map(.x = local_path,
.f = git2r::repository_head),
upstream_head = purrr::map(.x = repo_head,
.f = git2r::branch_get_upstream),
status = purrr::map(.x = local_path,
.f = ~as.data.frame(lapply(unclass(git2r::status(.x)), length))),
ab = purrr::map2(.x = repo_head, .y = upstream_head,
.f = ab_poss),
has_upstream = map(.x = upstream_head,
.f = ~!is.null(.x))) %>%
dplyr::select(-contains("_head"), -local_path, local_path) %>%
tidyr::unnest(cols = c("status", "ab")) %>%
filter_if(is.integer, any_vars(. != 0 | is.na(.)))
return(tbl)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment