Skip to content

Commit

Permalink
feat: add reflog exercise (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed Jan 11, 2024
1 parent c757154 commit 9f28085
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export(exo_committed_to_main)
export(exo_latest_message)
export(exo_one_small_change)
export(exo_split_changes)
export(exo_time_machine)
importFrom(rlang,`%||%`)
57 changes: 57 additions & 0 deletions R/time-machine.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#' "Oh shit, I did something terribly wrong, please tell me git has a magic time machine!?!"
#'
#' @description
#' To go with <https://ohshitgit.com/#magic-time-machine>
#'
#'
#' @inheritParams exo_one_small_change
#' @section Git commands:
#' `git reset`, `git reflog`
#'
#' @return The path to the new project
#' @export
#'
#' @examplesIf interactive()
#' parent_path <- withr::local_tempdir()
#' path <- exo_time_machine(parent_path = parent_path)
exo_time_machine <- function(parent_path) {

path <- file.path(parent_path, "time-machine")

withr::local_options(usethis.quiet = TRUE)

dir_create(path)
original_dir <- getwd()

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

file.copy(
system.file("exo_time_machine-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("*")
first <- git_commit("First commit")

fs::file_create("bla")
brio::write_lines(
text = c("thing 1", "thing 2"),
path = "bla"
)
gert::git_add("bla")
git_commit("feat: add bla")
gert::git_reset_hard(first)

usethis::local_project(original_dir, force = TRUE)

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

return(path)
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ template:
reference:
- title: Exercises inspired from Oh shit Git!
contents:
- exo_time_machine
- exo_committed_to_main
- exo_latest_message
- exo_one_small_change
Expand Down
14 changes: 14 additions & 0 deletions inst/exo_time_machine-Rprofile.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
if (file.exists("~/.Rprofile")) {
base::sys.source("~/.Rprofile", envir = environment())
}
cli::cli_alert_danger("I mistakenly run {.code git reset hard} so lost my feature commit.")
cli::cli_alert_danger("I want to get it back!")
cli::cli_alert_info("For more help use {.run tip()}")

tip <- function() {
cli::cli_li("{.code git reflog} to add a first chunk")
cli::cli_li('{.code git reset --hard HEAD@{1}}')
cli::cli_li('Note that --hard does not always make sense, but git reset is always the command')
cli::cli_li('Examine Git history.')

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

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

6 changes: 4 additions & 2 deletions tests/testthat/_snaps/create-all.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
+-- one-small-change
| +-- R
| \-- bla
\-- split-changes
+-- split-changes
| \-- R
| \-- script.R
\-- time-machine
\-- R
\-- script.R
[1] ""

7 changes: 7 additions & 0 deletions tests/testthat/_snaps/time-machine.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# exo_time_machine() works

Code
gert::git_log(repo = path)[["commit"]]
Output
[1] "e227ecc55e421f70b6e30602e6a2eee02aad42e0"

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

0 comments on commit 9f28085

Please sign in to comment.