Skip to content

Commit 1740647

Browse files
committed
feat: add function for reset --mixed
1 parent a95ddf0 commit 1740647

20 files changed

+185
-13
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Description: Holds functions creating Git messes, that users would then solve, t
1010
License: MIT + file LICENSE
1111
Encoding: UTF-8
1212
Roxygen: list(markdown = TRUE)
13-
RoxygenNote: 7.3.1
13+
RoxygenNote: 7.3.2.9000
1414
Imports:
1515
brio,
1616
cli,

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export(exo_conflict)
99
export(exo_latest_message)
1010
export(exo_one_small_change)
1111
export(exo_rebase_i)
12+
export(exo_reset)
1213
export(exo_revert_file)
1314
export(exo_split_changes)
1415
export(exo_time_machine)

R/reset.R

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#' "Hey I'd like to restart from scratch"
2+
#'
3+
#' @description
4+
#' I am working in a feature branch that's all my own.
5+
#' I made many small commits as I was figuring things out.
6+
#' Now I want the commits to tell a story for the PR reviewers,
7+
#' and not a story of how many stupid mistakes I made!
8+
#' Instead of `git base --interactive` also available as `git rebase -i`.¡,
9+
#' I can also use `git reset --mixed` and then build the commits.
10+
#' Useful links:
11+
#' - <https://github.blog/2022-06-30-write-better-commits-build-better-projects/>
12+
#' - <https://masalmon.eu/2024/06/11/rewrite-git-history/>
13+
#'
14+
#' @inheritParams exo_one_small_change
15+
#'
16+
#' @section Git commands:
17+
#' `git reset --mixed`
18+
#' @return The path to the new project
19+
#' @export
20+
#'
21+
#' @examplesIf interactive()
22+
#' parent_path <- withr::local_tempdir()
23+
#' path <- exo_reset(parent_path = parent_path)
24+
exo_reset <- function(parent_path) {
25+
26+
path <- file.path(parent_path, "reset")
27+
28+
withr::local_options(usethis.quiet = TRUE)
29+
30+
dir_create(path)
31+
original_dir <- getwd()
32+
33+
withr::local_dir(path)
34+
gert::git_init()
35+
36+
file.copy(
37+
system.file("exo_reset-Rprofile.R", package = "saperlipopette"),
38+
".Rprofile"
39+
)
40+
41+
create_project(path = getwd())
42+
# Ignore Rproj that might otherwise get edited when we open the project
43+
rproj <- fs::dir_ls(glob = "*.Rproj")
44+
usethis::local_project(getwd(), force = TRUE)
45+
usethis::use_git_ignore(rproj)
46+
usethis::use_git_ignore(".Rprofile")
47+
gert::git_add("*")
48+
git_commit("First commit")
49+
50+
gert::git_branch_create("feature")
51+
52+
ci_file1 <- "ci.yml"
53+
54+
fs::file_create(ci_file1)
55+
brio::write_lines(text = c("do: yes"), path = ci_file1)
56+
gert::git_add(ci_file1)
57+
git_commit("add ci configuration")
58+
59+
script <- "bla.R"
60+
61+
fs::file_create(script)
62+
brio::write_lines(text = c("1/0"), path = script)
63+
gert::git_add(script)
64+
git_commit("add script")
65+
66+
67+
brio::write_lines(text = c("do: true"), path = ci_file1)
68+
gert::git_add(ci_file1)
69+
git_commit("try to fix ci")
70+
brio::write_lines(text = c("do: 1"), path = ci_file1)
71+
gert::git_add(ci_file1)
72+
git_commit("try to fix ci")
73+
74+
brio::write_lines(text = c("1/Inf"), path = script)
75+
gert::git_add(script)
76+
git_commit("try to fix script")
77+
78+
brio::write_lines(text = c(c("do: 1", "save: 1")), path = ci_file1)
79+
gert::git_add(ci_file1)
80+
git_commit("add a ci thing")
81+
82+
brio::write_lines(text = c("1/2"), path = script)
83+
gert::git_add(script)
84+
git_commit("fix script")
85+
86+
usethis::local_project(original_dir, force = TRUE)
87+
88+
cli::cli_alert_info("Follow along in {path}!")
89+
90+
return(path)
91+
}

_pkgdown.yml

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ reference:
1818
- exo_clean_dir
1919
- exo_conflict
2020
- exo_rebase_i
21+
- exo_reset
2122
- exo_bisect
2223
- title: All exercises at once
2324
contents:

inst/exo_reset-Rprofile.R

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
if (file.exists("~/.Rprofile")) {
2+
base::sys.source("~/.Rprofile", envir = environment())
3+
}
4+
5+
cli::cli_alert_danger('"Hey I want the commit history of the feature branch to look smarter!"')
6+
cli::cli_alert_info("I can undo the commits and keep the changes.")
7+
cli::cli_alert_info("For more help use {.run tip()}")
8+
9+
tip <- function() {
10+
cli::cli_li(
11+
items = c(
12+
"Examine Git history.",
13+
"Find last commit before the changes.",
14+
"{.code git reset --mixed <commit-id>}.",
15+
"Do one commit per file.",
16+
"Examine Git history and your files."
17+
)
18+
)
19+
20+
}

man/exo_bisect.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/exo_clean_dir.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/exo_committed_to_main.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/exo_committed_to_wrong.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/exo_conflict.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/exo_latest_message.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/exo_one_small_change.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/exo_rebase_i.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/exo_reset.Rd

+38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/exo_revert_file.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/exo_split_changes.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/exo_time_machine.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/exo_undo_commit.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/reset.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# exo_reset() works
2+
3+
Code
4+
gert::git_log(repo = path)[["commit"]]
5+
Output
6+
[1] "3c424b0d6d98b6cf1697450b6e4cbd8a59621f82"
7+
[2] "413db3e0c49f844a7318982d727b6b42774465b9"
8+
[3] "c8468fa173677eeefd57b046272ca0cae4ae1b23"
9+
[4] "928960e78f882031ce3e08ee50dcb41778da22c5"
10+
[5] "e3f09954c724515769879d72d77e48ade0db69d9"
11+
[6] "ceb5f9c47cb6e28f0f63f668bd7e04b9a7fa87d1"
12+
[7] "0579b6610db201fc2591ebb9f7c535abd0bc70f8"
13+
[8] "e227ecc55e421f70b6e30602e6a2eee02aad42e0"
14+

tests/testthat/test-reset.R

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
test_that("exo_reset() works", {
2+
rlang::local_options(cli.default_handler = function(msg) invisible(NULL))
3+
parent_path <- withr::local_tempdir()
4+
path <- exo_reset(parent_path = parent_path)
5+
expect_equal(fs::path_file(path), "reset")
6+
expect_snapshot(gert::git_log(repo = path)[["commit"]])
7+
})

0 commit comments

Comments
 (0)