From 9f28085b80cf676701323aed25a4a38229778d64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Thu, 11 Jan 2024 15:38:00 +0100 Subject: [PATCH] feat: add reflog exercise (#11) --- NAMESPACE | 1 + R/time-machine.R | 57 +++++++++++++++++++++++++++ _pkgdown.yml | 1 + inst/exo_time_machine-Rprofile.R | 14 +++++++ man/exo_time_machine.Rd | 28 +++++++++++++ tests/testthat/_snaps/create-all.md | 6 ++- tests/testthat/_snaps/time-machine.md | 7 ++++ tests/testthat/test-time-machine.R | 7 ++++ 8 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 R/time-machine.R create mode 100644 inst/exo_time_machine-Rprofile.R create mode 100644 man/exo_time_machine.Rd create mode 100644 tests/testthat/_snaps/time-machine.md create mode 100644 tests/testthat/test-time-machine.R diff --git a/NAMESPACE b/NAMESPACE index f2ec6ff..97c5764 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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,`%||%`) diff --git a/R/time-machine.R b/R/time-machine.R new file mode 100644 index 0000000..0776204 --- /dev/null +++ b/R/time-machine.R @@ -0,0 +1,57 @@ +#' "Oh shit, I did something terribly wrong, please tell me git has a magic time machine!?!" +#' +#' @description +#' To go with +#' +#' +#' @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) +} diff --git a/_pkgdown.yml b/_pkgdown.yml index b1d1e1c..dacf658 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -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 diff --git a/inst/exo_time_machine-Rprofile.R b/inst/exo_time_machine-Rprofile.R new file mode 100644 index 0000000..1c9e252 --- /dev/null +++ b/inst/exo_time_machine-Rprofile.R @@ -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.') + +} diff --git a/man/exo_time_machine.Rd b/man/exo_time_machine.Rd new file mode 100644 index 0000000..b500192 --- /dev/null +++ b/man/exo_time_machine.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/time-machine.R +\name{exo_time_machine} +\alias{exo_time_machine} +\title{"Oh shit, I did something terribly wrong, please tell me git has a magic time machine!?!"} +\usage{ +exo_time_machine(parent_path) +} +\arguments{ +\item{parent_path}{Path where to create the exercise repo} +} +\value{ +The path to the new project +} +\description{ +To go with \url{https://ohshitgit.com/#magic-time-machine} +} +\section{Git commands}{ + +\verb{git reset}, \verb{git reflog} +} + +\examples{ +\dontshow{if (interactive()) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +parent_path <- withr::local_tempdir() +path <- exo_time_machine(parent_path = parent_path) +\dontshow{\}) # examplesIf} +} diff --git a/tests/testthat/_snaps/create-all.md b/tests/testthat/_snaps/create-all.md index da4d8ff..3266a5c 100644 --- a/tests/testthat/_snaps/create-all.md +++ b/tests/testthat/_snaps/create-all.md @@ -13,8 +13,10 @@ +-- one-small-change | +-- R | \-- bla - \-- split-changes + +-- split-changes + | \-- R + | \-- script.R + \-- time-machine \-- R - \-- script.R [1] "" diff --git a/tests/testthat/_snaps/time-machine.md b/tests/testthat/_snaps/time-machine.md new file mode 100644 index 0000000..ffb6365 --- /dev/null +++ b/tests/testthat/_snaps/time-machine.md @@ -0,0 +1,7 @@ +# exo_time_machine() works + + Code + gert::git_log(repo = path)[["commit"]] + Output + [1] "e227ecc55e421f70b6e30602e6a2eee02aad42e0" + diff --git a/tests/testthat/test-time-machine.R b/tests/testthat/test-time-machine.R new file mode 100644 index 0000000..7a7529d --- /dev/null +++ b/tests/testthat/test-time-machine.R @@ -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"]]) +})