-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from maelle/git-add-patch
feat: add an example for git patch
- Loading branch information
Showing
7 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
export(exo_committed_to_main) | ||
export(exo_latest_message) | ||
export(exo_one_small_change) | ||
export(exo_split_changes) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#' "Hey I'd like to split these changes to the same file in several commits!" | ||
#' | ||
#' @description | ||
#' I made many edits to a file, in different places. | ||
#' This is too much for a commit, since small commits are best practice. | ||
#' I need to add the changes to Git bit by bit. | ||
#' The tool for that is `git add --patch`, also available as `git add -p`. | ||
#' If all your changes are presented to you as one chunk by `git add --patch`, | ||
#' choose the "s" option for splitting. | ||
#' See https://git-scm.com/book/en/v2/Git-Tools-Interactive-Staging#_staging_patches. | ||
#' | ||
#' Note that patch is also an option for `git commit`, if you prefer so. | ||
#' | ||
#' | ||
#' @inheritParams exo_one_small_change | ||
#' | ||
#' @return The path to the new project | ||
#' @export | ||
#' | ||
#' @examplesIf interactive() | ||
#' parent_path <- withr::local_tempdir() | ||
#' path <- exo_split_changes(parent_path = parent_path) | ||
exo_split_changes <- function(parent_path) { | ||
|
||
path <- file.path(parent_path, "split-changes") | ||
|
||
withr::local_options(usethis.quiet = TRUE) | ||
|
||
fs::dir_create(path) | ||
original_dir <- getwd() | ||
|
||
withr::local_dir(path) | ||
gert::git_init() | ||
|
||
file.copy( | ||
system.file("exo_split_changes-Rprofile.R", package = "saperlipopette"), | ||
".Rprofile" | ||
) | ||
|
||
usethis::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") | ||
|
||
new_script <- file.path("R", "script.R") | ||
fs::file_create(new_script) | ||
script_lines <- c("a <- 1", "b <- 2") | ||
brio::write_lines(text = script_lines, path = new_script) | ||
gert::git_add(new_script) | ||
git_commit("feat: add script") | ||
|
||
script_lines <- append(script_lines, "# a comment", after = 0) | ||
script_lines <- append(script_lines, c("1/2", "1/3"), after = 2) | ||
script_lines <- append(script_lines, c("library(ggplot2)")) | ||
brio::write_lines(text = script_lines, path = new_script) | ||
|
||
usethis::local_project(original_dir, force = TRUE) | ||
|
||
cli::cli_alert_info("Follow along in {path}!") | ||
|
||
return(path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
if (file.exists("~/.Rprofile")) { | ||
base::sys.source("~/.Rprofile", envir = environment()) | ||
} | ||
cli::cli_alert_danger("Hey I'd like to split these changes to the same file in several commits!") | ||
cli::cli_alert_danger("I want to add my three changes to R/script.R in three commits not one.") | ||
cli::cli_alert_info("See {.url https://git-scm.com/book/en/v2/Git-Tools-Interactive-Staging#_staging_patches}") | ||
cli::cli_alert_info("For more help use {.run tip()}") | ||
|
||
tip <- function() { | ||
cli::cli_li( | ||
items = c( | ||
"Examine Git staging area.", | ||
"{.code git add --patch} to add a first chunk ('s' to split, 'y' to stage the first chunk, 'n' twice to discard the others)", | ||
'{.code git commit -m "First change"}', | ||
"{.code git add --patch} to add a second chunk ('s' to split, 'y' to stage the first chunk, 'n' once to discard the other)", | ||
'{.code git commit -m "Second change"}', | ||
"{.code git add *} to add the last chunk", | ||
'{.code git commit -m "Third change"}', | ||
"Examine Git history." | ||
) | ||
) | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# exo_split_changes() works | ||
|
||
Code | ||
gert::git_log(repo = path)[["commit"]] | ||
Output | ||
[1] "2a61d9da6865eb7f7272c9f901f13c1da377a01e" | ||
[2] "e227ecc55e421f70b6e30602e6a2eee02aad42e0" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
test_that("exo_split_changes() works", { | ||
rlang::local_options(cli.default_handler = function(msg) invisible(NULL)) | ||
parent_path <- withr::local_tempdir() | ||
path <- exo_split_changes(parent_path = parent_path) | ||
expect_equal(fs::path_file(path), "split-changes") | ||
expect_snapshot(gert::git_log(repo = path)[["commit"]]) | ||
}) |