|
1 | 1 | #' Create a gist via git instead of the GitHub Gists HTTP API
|
2 | 2 | #'
|
3 | 3 | #' @export
|
| 4 | +#' |
4 | 5 | #' @template args
|
5 | 6 | #' @param artifacts (logical/character) Include artifacts or not. If \code{TRUE},
|
6 | 7 | #' includes all artifacts. Or you can pass in a file extension to only upload
|
7 | 8 | #' artifacts of certain file exensions. Default: \code{FALSE}
|
8 | 9 | #' @param git_method (character) One of ssh (default) or https. If a remote already
|
9 | 10 | #' exists, we use that remote, and this parameter is ignored.
|
| 11 | +#' |
10 | 12 | #' @details Note that when \code{browse=TRUE} there is a slight delay in when
|
11 | 13 | #' we open up the gist in your default browser and when the data will display
|
12 | 14 | #' in the gist. We could have this function sleep a while and guess when it
|
|
35 | 37 | #'
|
36 | 38 | #' Another difference between this function and \code{\link{gist_create}} is that
|
37 | 39 | #' this function can collect all artifacts coming out of a knit process.
|
| 40 | +#' |
| 41 | +#' If a gist is somehow deleted, or the remote changes, when you try to push to the |
| 42 | +#' same gist again, everything should be fine. We now use \code{tryCatch} on the |
| 43 | +#' push attempt, and if it fails, we'll add a new remote (which means a new gist), |
| 44 | +#' and push again. |
| 45 | +#' |
38 | 46 | #' @seealso \code{\link{gist_create}}, \code{\link{gist_create_obj}}
|
| 47 | +#' |
39 | 48 | #' @examples \dontrun{
|
40 | 49 | #' # prepare a directory and a file
|
| 50 | +#' unlink("~/gitgist", recursive = TRUE) |
41 | 51 | #' dir.create("~/gitgist")
|
42 | 52 | #' file <- system.file("examples", "stuff.md", package = "gistr")
|
43 | 53 | #' writeLines(readLines(file), con = "~/gitgist/stuff.md")
|
|
46 | 56 | #' gist_create_git(files = "~/gitgist/stuff.md")
|
47 | 57 | #'
|
48 | 58 | #' ## more than one file can be passed in
|
| 59 | +#' unlink("~/gitgist2", recursive = TRUE) |
49 | 60 | #' dir.create("~/gitgist2")
|
50 | 61 | #' file.copy(file, "~/gitgist2/")
|
51 | 62 | #' cat("hello world", file = "~/gitgist2/hello_world.md")
|
52 | 63 | #' list.files("~/gitgist2")
|
53 | 64 | #' gist_create_git(c("~/gitgist2/stuff.md", "~/gitgist2/hello_world.md"))
|
54 | 65 | #'
|
55 | 66 | #' # Include all files in a directory
|
| 67 | +#' unlink("~/gitgist3", recursive = TRUE) |
56 | 68 | #' dir.create("~/gitgist3")
|
57 | 69 | #' cat("foo bar", file="~/gitgist3/foobar.txt")
|
58 | 70 | #' cat("hello", file="~/gitgist3/hello.txt")
|
59 | 71 | #' list.files("~/gitgist3")
|
60 |
| -#' gist_create_git(files = "~/gitgist3") |
| 72 | +#' gist_create_git("~/gitgist3") |
61 | 73 | #'
|
62 | 74 | #' # binary files
|
63 | 75 | #' png <- system.file("examples", "file.png", package = "gistr")
|
| 76 | +#' unlink("~/gitgist4", recursive = TRUE) |
64 | 77 | #' dir.create("~/gitgist4")
|
65 | 78 | #' file.copy(png, "~/gitgist4/")
|
66 | 79 | #' list.files("~/gitgist4")
|
67 | 80 | #' gist_create_git(files = "~/gitgist4/file.png")
|
68 | 81 | #'
|
69 | 82 | #' # knit files first, then push up
|
70 |
| -#' # note: by default we don't upload images, but you can do that, see examples |
| 83 | +#' # note: by default we don't upload images, but you can do that, see next example |
71 | 84 | #' rmd <- system.file("examples", "plots.Rmd", package = "gistr")
|
| 85 | +#' unlink("~/gitgist5", recursive = TRUE) |
72 | 86 | #' dir.create("~/gitgist5")
|
73 | 87 | #' file.copy(rmd, "~/gitgist5/")
|
74 | 88 | #' list.files("~/gitgist5")
|
75 |
| -#' gist_create_git(files = "~/gitgist5/plots.Rmd", knit = TRUE) |
| 89 | +#' gist_create_git("~/gitgist5/plots.Rmd", knit = TRUE) |
76 | 90 | #'
|
77 | 91 | #' # collect all/any artifacts from knitting process
|
78 | 92 | #' arts <- system.file("examples", "artifacts_eg1.Rmd", package = "gistr")
|
| 93 | +#' unlink("~/gitgist6", recursive = TRUE) |
79 | 94 | #' dir.create("~/gitgist6")
|
80 | 95 | #' file.copy(arts, "~/gitgist6/")
|
81 | 96 | #' list.files("~/gitgist6")
|
|
93 | 108 | #'
|
94 | 109 | #' # Use https instead of ssh
|
95 | 110 | #' png <- system.file("examples", "file.png", package = "gistr")
|
| 111 | +#' unlink("~/gitgist7", recursive = TRUE) |
96 | 112 | #' dir.create("~/gitgist7")
|
97 | 113 | #' file.copy(png, "~/gitgist7/")
|
98 | 114 | #' list.files("~/gitgist7")
|
@@ -161,17 +177,36 @@ gist_create_git <- function(files = NULL, description = "", public = TRUE, brows
|
161 | 177 | ra <- tryCatch(git2r::remote_add(git, "gistr", url), error = function(e) e)
|
162 | 178 | if (is(ra, "error")) message(strsplit(ra$message, ":")[[1]][[2]])
|
163 | 179 | # push up files
|
| 180 | + push_msg <- "Old remote not found on GitHub Gists\nAdding new remote\nRe-attempting push" |
164 | 181 | if (git_method == "ssh") {
|
165 |
| - git2r::push(git, "gistr", "refs/heads/master", force = TRUE) |
| 182 | + trypush <- tryCatch(git2r::push(git, "gistr", "refs/heads/master", force = TRUE), |
| 183 | + error = function(e) e) |
| 184 | + if (is(trypush, "error")) { |
| 185 | + message(push_msg) |
| 186 | + git2r::remote_remove(git, "gistr") |
| 187 | + git2r::remote_add(git, "gistr", url) |
| 188 | + git2r::push(git, "gistr", "refs/heads/master", force = TRUE) |
| 189 | + } |
166 | 190 | } else {
|
167 | 191 | cred <- git2r::cred_env("GITHUB_USERNAME", "GITHUB_PAT")
|
168 |
| - git2r::push(git, "gistr", "refs/heads/master", force = TRUE, credentials = cred) |
| 192 | + trypush <- tryCatch(git2r::push(git, "gistr", "refs/heads/master", force = TRUE, credentials = cred), |
| 193 | + error = function(e) e) |
| 194 | + if (is(trypush, "error")) { |
| 195 | + message(push_msg) |
| 196 | + git2r::remote_remove(git, "gistr") |
| 197 | + git2r::remote_add(git, "gistr", url) |
| 198 | + git2r::push(git, "gistr", "refs/heads/master", force = TRUE, credentials = cred) |
| 199 | + } |
169 | 200 | }
|
| 201 | + |
170 | 202 | # refresh gist metadata
|
171 | 203 | gst <- gist(gst$id)
|
172 | 204 | message("The file list for your gist may not be accurate if you are uploading a lot of files")
|
| 205 | + message("Refresh the gist page if your files aren't there") |
| 206 | + |
173 | 207 | # cleanup any temporary directories
|
174 |
| - unlink(dirname(allfiles[[1]]), recursive = TRUE, force = TRUE) |
| 208 | + # unlink(dirname(allfiles[[1]]), recursive = TRUE, force = TRUE) |
| 209 | + |
175 | 210 | # browse
|
176 | 211 | if (browse) browse(gst)
|
177 | 212 | return( gst )
|
|
0 commit comments