Skip to content

Instantly share code, notes, and snippets.

@amoeba
Created November 29, 2023 03:39
Show Gist options
  • Save amoeba/4e26c064d1a0d0227cd8c2260cf0072a to your computer and use it in GitHub Desktop.
Save amoeba/4e26c064d1a0d0227cd8c2260cf0072a to your computer and use it in GitHub Desktop.
#' new_contributors.R
#'
#' Produce a list of names of new contributors between two git refs. The method
#' this uses is to first get the list of unique contrbutors referancable from
#' the first ref, then the second ref, and then compute the set difference and
#' return that.
#'
#' Usage
#'
#' Launch an R session from the directory containing the git repo you want to
#' query against.
#'
#' Run this to get a list of new contributors between the refs
#' "apache-arrow-13.0.0" and "apache-arrow-14.0.0" for commits that touched
#' the "r" subdirectory":
#
#' print_new_contributors(
#' "apache-arrow-13.0.0",
#' "apache-arrow-14.0.0",
#' "r"
#' )
#'
#' Note that the third argument, subdirectory, is optional. If omitted, it
#' will use the root directory.
stopunlesscommand <- function(command, arguments) {
out <- tryCatch({
system2("git", arguments, stdout = TRUE)
},
warning = function(w) {
stop(w)
},
error = function(e) {
stop(e)
})
TRUE
}
stopfinotinrepo <- function() {
stopunlesscommand("git", "reflog main")
}
stopifnotref <- function(ref) {
stopifnot(is.character(ref))
stopifnot(nchar(ref) > 0)
stopunlesscommand("git", paste("reflog", ref))
}
make_git_log_prev_args <- function(ref_from, subdirectory) {
paste0("log --pretty='format: %an' ", ref_from, " ", subdirectory, " | sort | uniq")
}
make_git_log_next_args <- function(ref_from, ref_to, subdirectory) {
paste0("log --pretty='format: %an' ", ref_from, "..", ref_to, " ", subdirectory, " | sort | uniq")
}
print_new_contributors <- function(ref_from, ref_to, subdirectory = ".") {
stopfinotinrepo()
stopifnotref(ref_from)
stopifnotref(ref_to)
stopifnot(file.exists(subdirectory))
prev_out <- trimws(system2("git", make_git_log_prev_args(ref_from, subdirectory), stdout = TRUE))
new_out <- trimws(system2("git", make_git_log_next_args(ref_from, ref_to, subdirectory), stdout = TRUE))
setdiff(new_out, prev_out)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment