Skip to content

Commit 8641cf8

Browse files
committed
Simplify a little bit
1 parent 96cecf7 commit 8641cf8

3 files changed

Lines changed: 21 additions & 40 deletions

File tree

R/commit.R

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ git_stat_files <- function(files, ref = "HEAD", max = NULL, repo = '.') {
244244
#' Applies the inverse of the changes introduced by a given commit, equivalent
245245
#' to `git revert <commit>`. The commit must be reachable from the current HEAD.
246246
#'
247-
#' By default, a new revert commit is created immediately. Set `no_commit = TRUE`
247+
#' By default, a new revert commit is created immediately. Set `commit = FALSE`
248248
#' to only stage the reverted changes without committing, leaving you free to
249249
#' amend or combine them before calling [git_commit()].
250250
#'
@@ -253,11 +253,11 @@ git_stat_files <- function(files, ref = "HEAD", max = NULL, repo = '.') {
253253
#' @rdname git_revert
254254
#' @family git
255255
#' @inheritParams git_open
256-
#' @inheritParams git_commit
257256
#' @inheritParams git_commit_info
258-
#' @param no_commit if `TRUE`, stage the reverted changes without creating a
259-
#' commit. Default is `FALSE`, that is to say, by default a commit is made.
257+
#' @param commit if `FALSE`, stage the reverted changes without creating a
258+
#' commit. Default is `TRUE`, that is to say, by default a commit is made.
260259
#' @param message a commit message. Similar default to `git revert`.
260+
#' @param ... other parameters passed to `git_commit`
261261
#' @return The SHA of the new revert commit (invisibly), or `NULL` when
262262
#' `no_commit = TRUE`.
263263
#' @examplesIf interactive()
@@ -300,23 +300,21 @@ git_stat_files <- function(files, ref = "HEAD", max = NULL, repo = '.') {
300300
#' @useDynLib gert R_git_revert
301301
git_revert <- function(
302302
ref,
303+
commit = TRUE,
303304
message = NULL,
304-
author = NULL,
305-
committer = NULL,
306-
no_commit = FALSE,
305+
...,
307306
repo = '.'
308307
) {
309308
repo <- git_open(repo)
310309
assert_string(ref)
311-
stopifnot(is.logical(no_commit), length(no_commit) == 1)
310+
stopifnot(is.logical(commit), length(commit) == 1)
312311

313-
sha <- try(git_commit_id(ref, repo = repo), silent = TRUE)
314-
if (inherits(sha, "try-error")) {
312+
sha <- tryCatch(git_commit_id(ref, repo = repo), error = function(e){
315313
stop(sprintf(
316314
"Can't find reference/commit '%s' in the current branch history",
317315
ref
318316
))
319-
}
317+
})
320318

321319
head_sha <- git_commit_id("HEAD", repo = repo)
322320
sha_descends_from_head <- git_commit_descendant_of(
@@ -330,20 +328,12 @@ git_revert <- function(
330328

331329
.Call(R_git_revert, repo, sha)
332330

333-
if (no_commit) {
334-
return(NULL)
335-
}
336-
337-
if (is.null(message)) {
338-
message <- revert_message(sha, repo)
331+
if (isTRUE(commit)) {
332+
if (is.null(message)) {
333+
message <- revert_message(sha, repo)
334+
}
335+
git_commit(message, ..., repo = repo)
339336
}
340-
341-
invisible(git_commit(
342-
message,
343-
author = author,
344-
committer = committer,
345-
repo = repo
346-
))
347337
}
348338

349339
assert_string <- function(x) {

man/git_revert.Rd

Lines changed: 6 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-commit.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ test_that("reverting a commit", {
222222
writeLines("again", file.path(repo, "hello.txt"))
223223
git_add("hello.txt", repo = repo)
224224
third <- git_commit("Third commit", repo = repo)
225-
result <- git_revert(third, no_commit = TRUE, repo = repo)
225+
result <- git_revert(third, commit = FALSE, repo = repo)
226226
expect_null(result)
227227
status <- git_status(repo = repo)
228228
expect_true(any(status$staged))

0 commit comments

Comments
 (0)