1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
## git2r, R bindings to the libgit2 library.
## Copyright (C) 2013-2019 The git2r contributors
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License, version 2,
## as published by the Free Software Foundation.
##
## git2r is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License along
## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
stash_index <- function(object, index) {
if (inherits(object, "git_stash")) {
## Determine the index of the stash in the stash list
index <- match(object$sha, vapply(stash_list(object$repo),
"[[", character(1), "sha"))
}
## The stash list is zero-based
if (abs(index - round(index)) >= .Machine$double.eps^0.5)
stop("'index' must be an integer")
as.integer(index) - 1L
}
stash_object <- function(object) {
if (inherits(object, "git_stash"))
return(object$repo)
lookup_repository(object)
}
##' Apply stash
##'
##' Apply a single stashed state from the stash list.
##'
##' If local changes in the working directory conflict with changes in
##' the stash then an error will be raised. In this case, the index
##' will always remain unmodified and all files in the working
##' directory will remain unmodified. However, if you are restoring
##' untracked files or ignored files and there is a conflict when
##' applying the modified files, then those files will remain in the
##' working directory.
##' @param object path to a repository, or a \code{git_repository}
##' object, or the stash \code{object} to pop. Default is a
##' \code{path = '.'} to a reposiory.
##' @param index The index to the stash to apply. Only used when
##' \code{object} is a path to a repository or a
##' \code{git_repository} object. Default is \code{index = 1}.
##' @return invisible NULL
##' @export
##' @examples \dontrun{
##' ## Initialize a temporary repository
##' path <- tempfile(pattern="git2r-")
##' dir.create(path)
##' repo <- init(path)
##'
##' # Configure a user
##' config(repo, user.name = "Alice", user.email = "alice@@example.org")
##'
##' # Create a file, add and commit
##' writeLines("Hello world!", file.path(path, "test.txt"))
##' add(repo, 'test.txt')
##' commit(repo, "Commit message")
##'
##' # Change file
##' writeLines(c("Hello world!", "HELLO WORLD!"), file.path(path, "test.txt"))
##'
##' # Create stash in repository
##' stash(repo)
##'
##' # Change file
##' writeLines(c("Hello world!", "HeLlO wOrLd!"), file.path(path, "test.txt"))
##'
##' # Create stash in repository
##' stash(repo)
##'
##' # View stashes
##' stash_list(repo)
##'
##' # Read file
##' readLines(file.path(path, "test.txt"))
##'
##' # Apply latest git_stash object in repository
##' stash_apply(stash_list(repo)[[1]])
##'
##' # Read file
##' readLines(file.path(path, "test.txt"))
##'
##' # View stashes
##' stash_list(repo)
##' }
stash_apply <- function(object = ".", index = 1) {
.Call(git2r_stash_apply, stash_object(object), stash_index(object, index))
invisible(NULL)
}
##' Drop stash
##'
##' @param object path to a repository, or a \code{git_repository}
##' object, or the stash \code{object} to drop. Default is a
##' \code{path = '.'} to a reposiory.
##' @param index The index to the stash to drop. Only used when
##' \code{object} is a path to a repository or a
##' \code{git_repository} object. Default is \code{index = 1}.
##' @return invisible NULL
##' @export
##' @examples \dontrun{
##' ## Initialize a temporary repository
##' path <- tempfile(pattern="git2r-")
##' dir.create(path)
##' repo <- init(path)
##'
##' # Configure a user
##' config(repo, user.name = "Alice", user.email = "alice@@example.org")
##'
##' # Create a file, add and commit
##' writeLines("Hello world!", file.path(path, "test.txt"))
##' add(repo, 'test.txt')
##' commit(repo, "Commit message")
##'
##' # Change file
##' writeLines(c("Hello world!", "HELLO WORLD!"), file.path(path, "test.txt"))
##'
##' # Create stash in repository
##' stash(repo)
##'
##' # Change file
##' writeLines(c("Hello world!", "HeLlO wOrLd!"), file.path(path, "test.txt"))
##'
##' # Create stash in repository
##' stash(repo)
##'
##' # View stashes
##' stash_list(repo)
##'
##' # Drop git_stash object in repository
##' stash_drop(stash_list(repo)[[1]])
##'
##' ## Drop stash using an index to stash
##' stash_drop(repo, 1)
##'
##' # View stashes
##' stash_list(repo)
##' }
stash_drop <- function(object = ".", index = 1) {
.Call(git2r_stash_drop, stash_object(object), stash_index(object, index))
invisible(NULL)
}
##' Stash
##'
##' @template repo-param
##' @param message Optional description. Defaults to current time.
##' @param index All changes already added to the index are left
##' intact in the working directory. Default is FALSE
##' @param untracked All untracked files are also stashed and then
##' cleaned up from the working directory. Default is FALSE
##' @param ignored All ignored files are also stashed and then cleaned
##' up from the working directory. Default is FALSE
##' @param stasher Signature with stasher and time of stash
##' @return invisible \code{git_stash} object if anything to stash
##' else NULL
##' @export
##' @examples \dontrun{
##' ## Initialize a temporary repository
##' path <- tempfile(pattern="git2r-")
##' dir.create(path)
##' repo <- init(path)
##'
##' # Configure a user
##' config(repo, user.name = "Alice", user.email = "alice@@example.org")
##'
##' # Create a file, add and commit
##' writeLines("Hello world!", file.path(path, "test.txt"))
##' add(repo, 'test.txt')
##' commit(repo, "Commit message")
##'
##' # Change file
##' writeLines(c("Hello world!", "HELLO WORLD!"), file.path(path, "test.txt"))
##'
##' # Check status of repository
##' status(repo)
##'
##' # Create stash in repository
##' stash(repo)
##'
##' # Check status of repository
##' status(repo)
##'
##' # View stash
##' stash_list(repo)
##' }
stash <- function(repo = ".",
message = as.character(Sys.time()),
index = FALSE,
untracked = FALSE,
ignored = FALSE,
stasher = NULL) {
repo <- lookup_repository(repo)
if (is.null(stasher))
stasher <- default_signature(repo)
invisible(.Call(git2r_stash_save, repo, message, index,
untracked, ignored, stasher))
}
##' List stashes in repository
##'
##' @template repo-param
##' @return list of stashes in repository
##' @export
##' @examples \dontrun{
##' ## Initialize a temporary repository
##' path <- tempfile(pattern="git2r-")
##' dir.create(path)
##' repo <- init(path)
##'
##' # Configure a user
##' config(repo, user.name = "Alice", user.email = "alice@@example.org")
##'
##' # Create a file, add and commit
##' writeLines("Hello world!", file.path(path, "test-1.txt"))
##' add(repo, 'test-1.txt')
##' commit(repo, "Commit message")
##'
##' # Make one more commit
##' writeLines(c("Hello world!", "HELLO WORLD!"), file.path(path, "test-1.txt"))
##' add(repo, 'test-1.txt')
##' commit(repo, "Next commit message")
##'
##' # Create one more file
##' writeLines("Hello world!", file.path(path, "test-2.txt"))
##'
##' # Check that there are no stashes
##' stash_list(repo)
##'
##' # Stash
##' stash(repo)
##'
##' # Only untracked changes, therefore no stashes
##' stash_list(repo)
##'
##' # Stash and include untracked changes
##' stash(repo, "Stash message", untracked=TRUE)
##'
##' # View stash
##' stash_list(repo)
##' }
stash_list <- function(repo = ".") {
.Call(git2r_stash_list, lookup_repository(repo))
}
##' Pop stash
##'
##' Apply a single stashed state from the stash list and remove it
##' from the list if successful.
##' @param object path to a repository, or a \code{git_repository}
##' object, or the stash \code{object} to pop. Default is a
##' \code{path = '.'} to a reposiory.
##' @param index The index to the stash to pop. Only used when
##' \code{object} is a path to a repository or a
##' \code{git_repository} object. Default is \code{index = 1}.
##' @return invisible NULL
##' @export
##' @examples \dontrun{
##' ## Initialize a temporary repository
##' path <- tempfile(pattern="git2r-")
##' dir.create(path)
##' repo <- init(path)
##'
##' # Configure a user
##' config(repo, user.name = "Alice", user.email = "alice@@example.org")
##'
##' # Create a file, add and commit
##' writeLines("Hello world!", file.path(path, "test.txt"))
##' add(repo, 'test.txt')
##' commit(repo, "Commit message")
##'
##' # Change file
##' writeLines(c("Hello world!", "HELLO WORLD!"), file.path(path, "test.txt"))
##'
##' # Create stash in repository
##' stash(repo)
##'
##' # Change file
##' writeLines(c("Hello world!", "HeLlO wOrLd!"), file.path(path, "test.txt"))
##'
##' # Create stash in repository
##' stash(repo)
##'
##' # View stashes
##' stash_list(repo)
##'
##' # Read file
##' readLines(file.path(path, "test.txt"))
##'
##' # Pop latest git_stash object in repository
##' stash_pop(stash_list(repo)[[1]])
##'
##' # Read file
##' readLines(file.path(path, "test.txt"))
##'
##' # View stashes
##' stash_list(repo)
##' }
stash_pop <- function(object = ".", index = 1) {
.Call(git2r_stash_pop, stash_object(object), stash_index(object, index))
invisible(NULL)
}
##' @export
format.git_stash <- function(x, ...) {
x$message
}
##' @export
print.git_stash <- function(x, ...) {
cat(format(x, ...), "\n", sep = "")
invisible(x)
}
##' Summary of a stash
##'
##' @param object The stash \code{object}
##' @param ... Additional arguments affecting the summary produced.
##' @return None (invisible 'NULL').
##' @export
##' @examples \dontrun{
##' ## Initialize a temporary repository
##' path <- tempfile(pattern="git2r-")
##' dir.create(path)
##' repo <- init(path)
##'
##' # Configure a user
##' config(repo, user.name = "Alice", user.email = "alice@@example.org")
##'
##' # Create a file, add and commit
##' writeLines("Hello world!", file.path(path, "test.txt"))
##' add(repo, 'test.txt')
##' commit(repo, "Commit message")
##'
##' # Change file
##' writeLines(c("Hello world!", "HELLO WORLD!"), file.path(path, "test.txt"))
##'
##' # Create stash in repository
##' stash(repo, "Stash message")
##'
##' # View summary of stash
##' summary(stash_list(repo)[[1]])
##' }
summary.git_stash <- function(object, ...) {
cat(sprintf(paste0("message: %s\n",
"stasher: %s <%s>\n",
"when: %s\n",
"sha: %s\n\n"),
object$summary,
object$author$name,
object$author$email,
as.character(object$author$when),
object$sha))
}
|