#!/usr/bin/env Rscript

# make sure that files are not too big
maxfilesize <- function(max) {
  files <- union(system("git ls-tree -r HEAD --name-only", intern = TRUE),
                 system("git diff --cached --name-only", intern = TRUE))
  out <- data.frame(files = files, size = round(file.size(files) / 1024, 2))
  out <- out[!is.na(out$size), ]
  out <- out[out$size > max, ]
  if (nrow(out) > 0) {
    stop(nrow(out), " files with size > ", max, "kB detected: \n",
         paste0(out$files, " (", out$size, "kB)", collapse = "\n"))
  }
  message("All files are smaller than ", max, "kB\n")
}

maxfilesize(250)
