Skip to content

Commit

Permalink
feat: add exercise for "I committed to the wrong branch" (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed Jan 11, 2024
1 parent a1d5b87 commit 6764de9
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export(create_all_exercises)
export(exo_committed_to_main)
export(exo_committed_to_wrong)
export(exo_latest_message)
export(exo_one_small_change)
export(exo_split_changes)
Expand Down
64 changes: 64 additions & 0 deletions R/committed-to-wrong-branch.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#' "Oh shit, I accidentally committed to the wrong branch!"
#'
#' @description
#' To go with <https://ohshitgit.com/#accidental-commit-wrong-branch>
#'
#'
#' @inheritParams exo_one_small_change
#' @section Git commands:
#' `git cherry-pick`, `git reset`, `git checkout`
#'
#' @return The path to the new project
#' @export
#'
#' @examplesIf interactive()
#' parent_path <- withr::local_tempdir()
#' path <- exo_committed_to_wrong(parent_path = parent_path)
exo_committed_to_wrong <- function(parent_path) {

path <- file.path(parent_path, "committed-to-wrong")

withr::local_options(usethis.quiet = TRUE)

dir_create(path)
original_dir <- getwd()

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

file.copy(
system.file("exo_committed_to_wrong-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")

gert::git_branch_create("feat-bla")
gert::git_branch_checkout("main")

gert::git_branch_create("hot-fix")
brio::write_lines("lala", "fix.txt")
gert::git_add("fix.txt")
git_commit("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")

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 @@ -7,6 +7,7 @@ reference:
contents:
- exo_time_machine
- exo_committed_to_main
- exo_committed_to_wrong
- exo_latest_message
- exo_one_small_change
- title: Other exercises
Expand Down
22 changes: 22 additions & 0 deletions inst/exo_committed_to_wrong-Rprofile.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
if (file.exists("~/.Rprofile")) {
base::sys.source("~/.Rprofile", envir = environment())
}

cli::cli_alert_danger('"Oh shit, I accidentally committed to the wrong branch!"')
cli::cli_alert_danger("I wanted my last commit, that created bla, to be on the feat-bla branch not hot-fix!")
cli::cli_alert_info("See {.url https://ohshitgit.com/#accidental-commit-wrong-branch}")
cli::cli_alert_info("For more help use {.run tip()}")

tip <- function() {
cli::cli_li(
items = c(
"Examine Git history.",
"{.code git checkout feat-bla}",
"{.code git cherry-pick hot-fix}",
"{.code git checkout hot-fix}",
"{.code git reset HEAD~ --hard}",
"Examine Git history of the hot-fix and feat-bla branches."
)
)

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

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

9 changes: 9 additions & 0 deletions tests/testthat/_snaps/committed-to-wrong-branch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# exo_committed_to_wrong() works

Code
gert::git_log(repo = path)[["commit"]]
Output
[1] "83ee0e3cc5afed932cd9e3f2f34274c60bfe1166"
[2] "e48ac924412914278ed7d3ee6e82d0fe3122741a"
[3] "e227ecc55e421f70b6e30602e6a2eee02aad42e0"

4 changes: 4 additions & 0 deletions tests/testthat/_snaps/create-all.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
+-- committed-to-main
| +-- R
| \-- bla
+-- committed-to-wrong
| +-- R
| +-- bla
| \-- fix.txt
+-- latest-message
| +-- R
| \-- bla
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-committed-to-wrong-branch.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test_that("exo_committed_to_wrong() works", {
rlang::local_options(cli.default_handler = function(msg) invisible(NULL))
parent_path <- withr::local_tempdir()
path <- exo_committed_to_wrong(parent_path = parent_path)
expect_equal(fs::path_file(path), "committed-to-wrong")
expect_snapshot(gert::git_log(repo = path)[["commit"]])
})

0 comments on commit 6764de9

Please sign in to comment.