Skip to content
This repository was archived by the owner on Sep 2, 2025. It is now read-only.

Commit 45f9f6a

Browse files
committed
now we use trycatch on push using git, and change remote then push again fix #59
dont cleanup files now, that was dumb, could delete users files fix #58 may want to just cleanup up files created from knitting, eg. in the future bumped to close to cran push version
1 parent 71b7223 commit 45f9f6a

File tree

3 files changed

+57
-10
lines changed

3 files changed

+57
-10
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Description: Work with 'GitHub' 'gists' from 'R' (e.g.,
99
your default browser, get embed code for a 'gist', list 'gist' 'commits', and
1010
get rate limit information when 'authenticated'. Some requests require
1111
authentication and some do not. 'Gists' website: https://gist.github.com/.
12-
Version: 0.3.1.9100
12+
Version: 0.3.3.9000
1313
Authors@R: c(
1414
person("Ramnath", "Vaidyanathan", role = "aut", email = "[email protected]"),
1515
person("Karthik", "Ram", role = "aut", email = "[email protected]"),

R/gist_create_git.R

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#' Create a gist via git instead of the GitHub Gists HTTP API
22
#'
33
#' @export
4+
#'
45
#' @template args
56
#' @param artifacts (logical/character) Include artifacts or not. If \code{TRUE},
67
#' includes all artifacts. Or you can pass in a file extension to only upload
78
#' artifacts of certain file exensions. Default: \code{FALSE}
89
#' @param git_method (character) One of ssh (default) or https. If a remote already
910
#' exists, we use that remote, and this parameter is ignored.
11+
#'
1012
#' @details Note that when \code{browse=TRUE} there is a slight delay in when
1113
#' we open up the gist in your default browser and when the data will display
1214
#' in the gist. We could have this function sleep a while and guess when it
@@ -35,9 +37,17 @@
3537
#'
3638
#' Another difference between this function and \code{\link{gist_create}} is that
3739
#' 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+
#'
3846
#' @seealso \code{\link{gist_create}}, \code{\link{gist_create_obj}}
47+
#'
3948
#' @examples \dontrun{
4049
#' # prepare a directory and a file
50+
#' unlink("~/gitgist", recursive = TRUE)
4151
#' dir.create("~/gitgist")
4252
#' file <- system.file("examples", "stuff.md", package = "gistr")
4353
#' writeLines(readLines(file), con = "~/gitgist/stuff.md")
@@ -46,36 +56,41 @@
4656
#' gist_create_git(files = "~/gitgist/stuff.md")
4757
#'
4858
#' ## more than one file can be passed in
59+
#' unlink("~/gitgist2", recursive = TRUE)
4960
#' dir.create("~/gitgist2")
5061
#' file.copy(file, "~/gitgist2/")
5162
#' cat("hello world", file = "~/gitgist2/hello_world.md")
5263
#' list.files("~/gitgist2")
5364
#' gist_create_git(c("~/gitgist2/stuff.md", "~/gitgist2/hello_world.md"))
5465
#'
5566
#' # Include all files in a directory
67+
#' unlink("~/gitgist3", recursive = TRUE)
5668
#' dir.create("~/gitgist3")
5769
#' cat("foo bar", file="~/gitgist3/foobar.txt")
5870
#' cat("hello", file="~/gitgist3/hello.txt")
5971
#' list.files("~/gitgist3")
60-
#' gist_create_git(files = "~/gitgist3")
72+
#' gist_create_git("~/gitgist3")
6173
#'
6274
#' # binary files
6375
#' png <- system.file("examples", "file.png", package = "gistr")
76+
#' unlink("~/gitgist4", recursive = TRUE)
6477
#' dir.create("~/gitgist4")
6578
#' file.copy(png, "~/gitgist4/")
6679
#' list.files("~/gitgist4")
6780
#' gist_create_git(files = "~/gitgist4/file.png")
6881
#'
6982
#' # 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
7184
#' rmd <- system.file("examples", "plots.Rmd", package = "gistr")
85+
#' unlink("~/gitgist5", recursive = TRUE)
7286
#' dir.create("~/gitgist5")
7387
#' file.copy(rmd, "~/gitgist5/")
7488
#' list.files("~/gitgist5")
75-
#' gist_create_git(files = "~/gitgist5/plots.Rmd", knit = TRUE)
89+
#' gist_create_git("~/gitgist5/plots.Rmd", knit = TRUE)
7690
#'
7791
#' # collect all/any artifacts from knitting process
7892
#' arts <- system.file("examples", "artifacts_eg1.Rmd", package = "gistr")
93+
#' unlink("~/gitgist6", recursive = TRUE)
7994
#' dir.create("~/gitgist6")
8095
#' file.copy(arts, "~/gitgist6/")
8196
#' list.files("~/gitgist6")
@@ -93,6 +108,7 @@
93108
#'
94109
#' # Use https instead of ssh
95110
#' png <- system.file("examples", "file.png", package = "gistr")
111+
#' unlink("~/gitgist7", recursive = TRUE)
96112
#' dir.create("~/gitgist7")
97113
#' file.copy(png, "~/gitgist7/")
98114
#' list.files("~/gitgist7")
@@ -161,17 +177,36 @@ gist_create_git <- function(files = NULL, description = "", public = TRUE, brows
161177
ra <- tryCatch(git2r::remote_add(git, "gistr", url), error = function(e) e)
162178
if (is(ra, "error")) message(strsplit(ra$message, ":")[[1]][[2]])
163179
# push up files
180+
push_msg <- "Old remote not found on GitHub Gists\nAdding new remote\nRe-attempting push"
164181
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+
}
166190
} else {
167191
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+
}
169200
}
201+
170202
# refresh gist metadata
171203
gst <- gist(gst$id)
172204
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+
173207
# cleanup any temporary directories
174-
unlink(dirname(allfiles[[1]]), recursive = TRUE, force = TRUE)
208+
# unlink(dirname(allfiles[[1]]), recursive = TRUE, force = TRUE)
209+
175210
# browse
176211
if (browse) browse(gst)
177212
return( gst )

man/gist_create_git.Rd

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,16 @@ for use cases involving:
8080
8181
Another difference between this function and \code{\link{gist_create}} is that
8282
this function can collect all artifacts coming out of a knit process.
83+
84+
If a gist is somehow deleted, or the remote changes, when you try to push to the
85+
same gist again, everything should be fine. We now use \code{tryCatch} on the
86+
push attempt, and if it fails, we'll add a new remote (which means a new gist),
87+
and push again.
8388
}
8489
\examples{
8590
\dontrun{
8691
# prepare a directory and a file
92+
unlink("~/gitgist", recursive = TRUE)
8793
dir.create("~/gitgist")
8894
file <- system.file("examples", "stuff.md", package = "gistr")
8995
writeLines(readLines(file), con = "~/gitgist/stuff.md")
@@ -92,36 +98,41 @@ writeLines(readLines(file), con = "~/gitgist/stuff.md")
9298
gist_create_git(files = "~/gitgist/stuff.md")
9399

94100
## more than one file can be passed in
101+
unlink("~/gitgist2", recursive = TRUE)
95102
dir.create("~/gitgist2")
96103
file.copy(file, "~/gitgist2/")
97104
cat("hello world", file = "~/gitgist2/hello_world.md")
98105
list.files("~/gitgist2")
99106
gist_create_git(c("~/gitgist2/stuff.md", "~/gitgist2/hello_world.md"))
100107

101108
# Include all files in a directory
109+
unlink("~/gitgist3", recursive = TRUE)
102110
dir.create("~/gitgist3")
103111
cat("foo bar", file="~/gitgist3/foobar.txt")
104112
cat("hello", file="~/gitgist3/hello.txt")
105113
list.files("~/gitgist3")
106-
gist_create_git(files = "~/gitgist3")
114+
gist_create_git("~/gitgist3")
107115

108116
# binary files
109117
png <- system.file("examples", "file.png", package = "gistr")
118+
unlink("~/gitgist4", recursive = TRUE)
110119
dir.create("~/gitgist4")
111120
file.copy(png, "~/gitgist4/")
112121
list.files("~/gitgist4")
113122
gist_create_git(files = "~/gitgist4/file.png")
114123

115124
# knit files first, then push up
116-
# note: by default we don't upload images, but you can do that, see examples
125+
# note: by default we don't upload images, but you can do that, see next example
117126
rmd <- system.file("examples", "plots.Rmd", package = "gistr")
127+
unlink("~/gitgist5", recursive = TRUE)
118128
dir.create("~/gitgist5")
119129
file.copy(rmd, "~/gitgist5/")
120130
list.files("~/gitgist5")
121-
gist_create_git(files = "~/gitgist5/plots.Rmd", knit = TRUE)
131+
gist_create_git("~/gitgist5/plots.Rmd", knit = TRUE)
122132

123133
# collect all/any artifacts from knitting process
124134
arts <- system.file("examples", "artifacts_eg1.Rmd", package = "gistr")
135+
unlink("~/gitgist6", recursive = TRUE)
125136
dir.create("~/gitgist6")
126137
file.copy(arts, "~/gitgist6/")
127138
list.files("~/gitgist6")
@@ -139,6 +150,7 @@ numbers
139150

140151
# Use https instead of ssh
141152
png <- system.file("examples", "file.png", package = "gistr")
153+
unlink("~/gitgist7", recursive = TRUE)
142154
dir.create("~/gitgist7")
143155
file.copy(png, "~/gitgist7/")
144156
list.files("~/gitgist7")

0 commit comments

Comments
 (0)