Skip to content

Commit

Permalink
stared new fxn for git integration
Browse files Browse the repository at this point in the history
  • Loading branch information
sckott committed Apr 7, 2015
1 parent 8dfa3b5 commit 04ba6de
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Expand Up @@ -28,7 +28,8 @@ Imports:
assertthat,
knitr,
rmarkdown,
dplyr
dplyr,
git2r
Suggests:
roxygen2,
testthat
70 changes: 70 additions & 0 deletions R/gist_create_git.R
@@ -0,0 +1,70 @@
#' Create a gist via git instead of the HTTP API
#'
#' @export
#' @import git2r
#'
#' @param files Files to upload, must be in the same directory.
#' @param description (character) Brief description of gist (optional)
#' @param public (logical) Whether gist is public (default: TRUE)
#' @param browse (logical) To open newly create gist in default browser (default: TRUE)
#' @param ... Further args passed on to \code{link[httr]{POST}}
#' @examples \dontrun{
#' gist_create_git(files = "~/gitgist/stuff.md")
#' gist_create_git(files = c("~/gitgist/stuff.md", "~/gitgist/icanhazallthedata.md"))
#' }

gist_create_git <- function(files, description = "", public = TRUE, browse = TRUE) {

stopifnot(all(file.exists(files)))
stopifnot(length(unique(dirname(files))) == 1)
git <- git2r::init(dirname(files[[1]]))

if (knit) {
allfiles <- list()
for (i in seq_along(files)) {
ff <- files[[i]]
dirpath <- dirname(ff)
orig_files <- ff
ff <- knit_render(ff, knitopts, renderopts)
if (artifacts) {
file_artifacts <- get_artifacts(ff, dirpath)
files <- c(ff, file_artifacts)
}
if (include_source) ff <- c(orig_files, ff)
allfiles[[i]] <- ff
}
} else {
allfiles <- path.expand(files)
}

# add files
git2r::add(git, basename(allfiles))
# commit files
git2r::commit(git, message = "added files from gistr")
# create gist
gst <- cgist(description, public)
# add remote
git2r::remote_add(git, "gistr", sprintf("git@gist.github.com:/%s.git", gst$id))
# push up files
git2r::push(git, "gistr", "refs/heads/master")
# browse
if (browse) browse(gist)
return( gist )
}

cgist <- function(description, public) {
res <- httr::POST(paste0(ghbase(), '/gists'),
gist_auth(),
encode = "json",
body = jsonlite::toJSON(list(
description = description,
public = public,
files = list(
".gistr" = list(
content = "gistr"
)
)
), auto_unbox = TRUE)
)
jsonlite::fromJSON(httr::content(res, "text"), FALSE)
}

0 comments on commit 04ba6de

Please sign in to comment.