Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add exercise for rebase -i #14

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Description: Holds functions creating Git messes, that users would then solve, t
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.0.9000
Imports:
brio,
cli,
Expand Down
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ export(exo_committed_to_main)
export(exo_committed_to_wrong)
export(exo_latest_message)
export(exo_one_small_change)
export(exo_rebase_i)
export(exo_revert_file)
export(exo_split_changes)
export(exo_time_machine)
export(exo_undo_commit)
importFrom(rlang,`%||%`)
importFrom(rlang,"%||%")
92 changes: 92 additions & 0 deletions R/rebase-i.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#' "Hey I'd like to make my commits in a branch look informative and smart!"
#'
#' @description
#' I am working in a feature branch that's all my own.
#' I made many small commits as I was figuring things out.
#' Now I want the commits to tell a story for the PR reviewers,
#' and not a story of how many stupid mistakes I made!
#' The tool for that is `git base --interactive` also available as `git rebase -i`.
#' Useful links:
#' - https://jvns.ca/blog/2023/11/06/rebasing-what-can-go-wrong-/
#' - https://wizardzines.com/comics/rules-for-rebasing/
#' - https://github.com/MikeMcQuaid/GitInPractice/blob/main/06-RewritingHistoryAndDisasterRecovery.adoc#rebase-commits-interactively-git-rebase-interactive
#'
#'
#' @inheritParams exo_one_small_change
#'
#' @section Git commands:
#' `git rebase -i`
#' @return The path to the new project
#' @export
#'
#' @examplesIf interactive()
#' parent_path <- withr::local_tempdir()
#' path <- exo_rebase_i(parent_path = parent_path)
exo_rebase_i <- function(parent_path) {

path <- file.path(parent_path, "rebase-i")

withr::local_options(usethis.quiet = TRUE)

dir_create(path)
original_dir <- getwd()

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

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

ci_file1 <- "ci.yml"

fs::file_create(ci_file1)
brio::write_lines(text = c("do: yes"), path = ci_file1)
gert::git_add(ci_file1)
git_commit("add ci configuration")

script <- "bla.R"

fs::file_create(script)
brio::write_lines(text = c("1/0"), path = script)
gert::git_add(script)
git_commit("add script")


brio::write_lines(text = c("do: true"), path = ci_file1)
gert::git_add(ci_file1)
git_commit("try to fix ci")
brio::write_lines(text = c("do: 1"), path = ci_file1)
gert::git_add(ci_file1)
git_commit("try to fix ci")

brio::write_lines(text = c("1/Inf"), path = script)
gert::git_add(script)
git_commit("try to fix script")

brio::write_lines(text = c(c("do: 1", "save: 1")), path = ci_file1)
gert::git_add(ci_file1)
git_commit("add a ci thing")

brio::write_lines(text = c("1/2"), path = script)
gert::git_add(script)
git_commit("fix script")

usethis::local_project(original_dir, force = TRUE)

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

return(path)
}
2 changes: 1 addition & 1 deletion R/saperlipopette-package.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' @keywords internal
#' @importFrom rlang `%||%`
#' @importFrom rlang %||%
"_PACKAGE"

## usethis namespace: start
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ reference:
- title: Other exercises
contents:
- exo_split_changes
- exo_rebase_i
- title: All exercises at once
contents:
- create_all_exercises
19 changes: 19 additions & 0 deletions inst/exo_rebase_i-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('"Hey I want the commit history of the feature branch to look smarter!"')
cli::cli_alert_info("See {.url https://www.digitalocean.com/community/tutorials/how-to-rebase-and-update-a-pull-request}")
cli::cli_alert_info("For more help use {.run tip()}")

tip <- function() {
cli::cli_li(
items = c(
"Examine Git history.",
"Find rebase commit with {.code git merge-base feature main}",
"{.code git rebase -i <commit-id>} as many times as you need (once to reorder, once to squash, etc).",
"Examine Git history and your files."
)
)

}
38 changes: 38 additions & 0 deletions man/exo_rebase_i.Rd

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

5 changes: 5 additions & 0 deletions man/saperlipopette-package.Rd

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

4 changes: 4 additions & 0 deletions tests/testthat/_snaps/create-all.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
+-- one-small-change
| +-- R
| \-- bla
+-- rebase-i
| +-- R
| +-- bla.R
| \-- ci.yml
+-- revert-file
| +-- R
| +-- bla.txt
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/_snaps/rebase-i.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# exo_rebase_i() works

Code
gert::git_log(repo = path)[["commit"]]
Output
[1] "3c424b0d6d98b6cf1697450b6e4cbd8a59621f82"
[2] "413db3e0c49f844a7318982d727b6b42774465b9"
[3] "c8468fa173677eeefd57b046272ca0cae4ae1b23"
[4] "928960e78f882031ce3e08ee50dcb41778da22c5"
[5] "e3f09954c724515769879d72d77e48ade0db69d9"
[6] "ceb5f9c47cb6e28f0f63f668bd7e04b9a7fa87d1"
[7] "0579b6610db201fc2591ebb9f7c535abd0bc70f8"
[8] "e227ecc55e421f70b6e30602e6a2eee02aad42e0"

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