Skip to content

Commit

Permalink
feat: exo for undo commit (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed Jan 11, 2024
1 parent 6764de9 commit 4a44368
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export(exo_latest_message)
export(exo_one_small_change)
export(exo_split_changes)
export(exo_time_machine)
export(exo_undo_commit)
importFrom(rlang,`%||%`)
74 changes: 74 additions & 0 deletions R/revert-commit.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#' "Oh shit, I need to undo a commit from like 5 commits ago!"
#'
#' @description
#' To go with <https://ohshitgit.com/#undo-a-commit>
#'
#'
#' @inheritParams exo_one_small_change
#' @section Git commands:
#' `git log`, `git revert`
#'
#' @return The path to the new project
#' @export
#'
#' @examplesIf interactive()
#' parent_path <- withr::local_tempdir()
#' path <- exo_undo_commit(parent_path = parent_path)
exo_undo_commit <- function(parent_path) {

path <- file.path(parent_path, "undo-commit")

withr::local_options(usethis.quiet = TRUE)

dir_create(path)
original_dir <- getwd()

withr::local_dir(path)
gert::git_init()

file.copy(
system.file("exo_undo_commit-Rprofile.R", package = "saperlipopette"),
".Rprofile"
)

create_project(path = getwd())
# Ignore Rproj that might otherwise get edited when we open the project
rproj <- fs::dir_ls(glob = "*.Rproj")
usethis::local_project(getwd(), force = TRUE)
usethis::use_git_ignore(rproj)
usethis::use_git_ignore(".Rprofile")
gert::git_add("*")
git_commit("First commit")

brio::write_lines("lala", "fix.txt")
gert::git_add("fix.txt")
git_commit("fix: fix things")

fs::file_create("bla")
brio::write_lines(
text = c("thing 1", "thing 2"),
path = "bla"
)
gert::git_add("bla")
git_commit("feat: add bla")
brio::write_lines(
text = c("thing 1", "thing 3"),
path = "bla"
)
gert::git_add("bla")
git_commit("fix: edit bla")
brio::write_lines(
text = c("thing 3", "thing 3"),
path = "bla"
)
gert::git_add("bla")
git_commit("fix: amend bla")



usethis::local_project(original_dir, force = TRUE)

cli::cli_alert_info("Follow along in {path}!")

return(path)
}
19 changes: 19 additions & 0 deletions inst/exo_undo_commit-Rprofile.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
if (file.exists("~/.Rprofile")) {
base::sys.source("~/.Rprofile", envir = environment())
}

cli::cli_alert_danger('"Oh shit, I need to undo a commit from like 5 commits ago!"')
cli::cli_alert_danger("I to undo my 'fix: fix things' commit as it broke stuff!")
cli::cli_alert_info("See {.url https://ohshitgit.com/#undo-a-commit}")
cli::cli_alert_info("For more help use {.run tip()}")

tip <- function() {
cli::cli_li(
items = c(
"Examine Git history.",
"{.code git revert <hash-you-see>}",
"Examine Git history and your files."
)
)

}
28 changes: 28 additions & 0 deletions man/exo_undo_commit.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions tests/testthat/_snaps/create-all.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
+-- split-changes
| \-- R
| \-- script.R
\-- time-machine
\-- R
+-- time-machine
| \-- R
\-- undo-commit
+-- R
+-- bla
\-- fix.txt
[1] ""

11 changes: 11 additions & 0 deletions tests/testthat/_snaps/revert-commit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# exo_undo_commit() works

Code
gert::git_log(repo = path)[["commit"]]
Output
[1] "0fab6ebfa87ef3cc05a53b6db1c48eb194de054b"
[2] "efd4aff85f7b2ebe1159d54144209b65add84182"
[3] "f0f676843426c131aa325b6c24a691dcf734a3ce"
[4] "5066d095eb9ff080b9418408edaf3f33b59a2316"
[5] "e227ecc55e421f70b6e30602e6a2eee02aad42e0"

7 changes: 7 additions & 0 deletions tests/testthat/test-revert-commit.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test_that("exo_undo_commit() works", {
rlang::local_options(cli.default_handler = function(msg) invisible(NULL))
parent_path <- withr::local_tempdir()
path <- exo_undo_commit(parent_path = parent_path)
expect_equal(fs::path_file(path), "undo-commit")
expect_snapshot(gert::git_log(repo = path)[["commit"]])
})

0 comments on commit 4a44368

Please sign in to comment.