diff --git a/R/pr.R b/R/pr.R index 8f75fe85a..3cf3ee70a 100644 --- a/R/pr.R +++ b/R/pr.R @@ -219,7 +219,7 @@ pr_init <- function(branch) { if (!is.na(remref)) { comparison <- git_branch_compare(current_branch, remref) if (comparison$remote_only > 0) { - challenge_uncommitted_changes() + challenge_uncommitted_changes(action = c("pull", "switch branches")) } ui_bullets(c("v" = "Pulling changes from {.val {remref}}.")) git_pull(remref = remref, verbose = FALSE) @@ -265,7 +265,7 @@ pr_resume <- function(branch = NULL) { )) } - challenge_uncommitted_changes() + challenge_uncommitted_changes(action = c("pull", "switch", "compare branches")) ui_bullets(c("v" = "Switching to branch {.val {branch}}.")) gert::git_branch_checkout(branch, repo = repo) @@ -290,7 +290,7 @@ pr_resume <- function(branch = NULL) { pr_fetch <- function(number = NULL, target = c("source", "primary")) { repo <- git_repo() tr <- target_repo(github_get = NA, role = target, ask = FALSE) - challenge_uncommitted_changes() + challenge_uncommitted_changes(action = c("pull", "switch branches")) if (is.null(number)) { ui_bullets(c("i" = "No PR specified ... looking up open PRs.")) @@ -384,7 +384,7 @@ pr_push <- function() { check_for_config(cfg, ok_configs = c("ours", "fork")) default_branch <- git_default_branch_(cfg) check_pr_branch(default_branch) - challenge_uncommitted_changes() + challenge_uncommitted_changes(action = "push") branch <- git_branch() remref <- git_branch_tracking(branch) @@ -432,7 +432,7 @@ pr_pull <- function() { check_for_config(cfg) default_branch <- git_default_branch_(cfg) check_pr_branch(default_branch) - challenge_uncommitted_changes() + challenge_uncommitted_changes(action = "pull") git_pull() @@ -447,7 +447,7 @@ pr_pull <- function() { #' @rdname pull-requests pr_merge_main <- function() { tr <- target_repo(github_get = TRUE, ask = FALSE) - challenge_uncommitted_changes() + challenge_uncommitted_changes(action = "pull") remref <- glue("{tr$remote}/{tr$default_branch}") ui_bullets(c("v" = "Pulling changes from {.val {remref}}.")) git_pull(remref, verbose = FALSE) @@ -511,7 +511,7 @@ pr_pause <- function() { )) return(invisible()) } - challenge_uncommitted_changes() + challenge_uncommitted_changes(action = "switch branches") # TODO: what happens here if offline? check_branch_pulled(use = "pr_pull()") diff --git a/R/utils-git.R b/R/utils-git.R index 9a3a57cb5..beb0d03f9 100644 --- a/R/utils-git.R +++ b/R/utils-git.R @@ -181,7 +181,11 @@ git_uncommitted <- function(untracked = FALSE) { nrow(git_status(untracked)) > 0 } -challenge_uncommitted_changes <- function(untracked = FALSE, msg = NULL) { +challenge_uncommitted_changes <- function( + untracked = FALSE, + msg = NULL, + action = c("push", "pull", "switch", "compare branches") +) { if (!uses_git()) { return(invisible()) } @@ -190,19 +194,31 @@ challenge_uncommitted_changes <- function(untracked = FALSE, msg = NULL) { rstudioapi::documentSaveAll() } - default_msg <- " - There are uncommitted changes, which may cause problems or be lost when \\ - we push, pull, switch, or compare branches" - msg <- glue(msg %||% default_msg) - if (git_uncommitted(untracked = untracked)) { - if (ui_yep(c( - "!" = msg, - " " = "Do you want to proceed anyway?" - ))) { - return(invisible()) - } else { - ui_abort("Uncommitted changes. Please commit before continuing.") + default_msg <- + "Uncommitted changes may cause problems or be lost when we {.or {action}}." + msg <- cli::format_inline(msg %||% default_msg) + + uncommited <- git_status(untracked) + if (nrow(uncommited) > 0) { + if (is_interactive()) { + choice <- utils::menu( + c( + "I want to proceed anyway.", + cli::format_inline( + "I want to take a closer look at {.file {uncommited$file}} first." + ) + ), + title = msg + ) + if (choice == 1) { + return(invisible()) + } } + + ui_abort( + "Uncommitted changes. Please commit before continuing.", + call = caller_env() + ) } } diff --git a/tests/testthat/test-rename-files.R b/tests/testthat/test-rename-files.R index cdfef3906..58ebefee5 100644 --- a/tests/testthat/test-rename-files.R +++ b/tests/testthat/test-rename-files.R @@ -6,7 +6,7 @@ test_that("checks uncommitted files", { use_r("foo", open = FALSE) expect_error( rename_files("foo", "bar"), - "uncommitted changes", + "Uncommitted changes", class = "usethis_error" ) })