From 978b85d8175dd60b2c4a6d4894001bc1d0f2c401 Mon Sep 17 00:00:00 2001 From: "Luke W. Johnston" Date: Fri, 13 Dec 2024 14:35:21 +0100 Subject: [PATCH] refactor: :recycle: ensure that if a dir only has `.git/`, continue with creation --- R/setup_project.R | 22 ++++++++++++++++------ tests/testthat/test-projects.R | 13 +++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/R/setup_project.R b/R/setup_project.R index b391246..5d40e84 100644 --- a/R/setup_project.R +++ b/R/setup_project.R @@ -29,13 +29,17 @@ setup_project <- } if (fs::dir_exists(proj_path)) { - cli::cli_abort(c( - "The {.val {proj_path}} folder already exists, so project creation is canceled.", - "i" = "Delete the folder or use another name (not {.val {proj_name}}) for your project." - )) + if (has_only_git(proj_path)) { + cli::cli_alert_info("The project already has Git added, so we will skip setting up Git.") + } else { + cli::cli_abort(c( + "The {.val {proj_path}} folder already exists, so project creation is canceled.", + "i" = "Delete the folder or use another name (not {.val {proj_name}}) for your project." + )) + } } proj_template <- find_template("projects", "basic-analysis") - fs::dir_copy(proj_template, new_path = proj_path) + fs::dir_copy(proj_template, new_path = proj_path, overwrite = TRUE) withr::with_dir( new = proj_path, @@ -45,11 +49,17 @@ setup_project <- fs::file_delete("template-Rproj") fs::file_move("gitignore", ".gitignore") update_template("README.md", data = list(ProjectName = proj_name)) - gert::git_init() + if (!fs::dir_exists(".git")) { + gert::git_init() + } } ) } +has_only_git <- function(path) { + all(basename(fs::dir_ls(path, all = TRUE)) == ".git") +} + # Git setup functions ------------------------------------------- #' Setup Git to the project. diff --git a/tests/testthat/test-projects.R b/tests/testthat/test-projects.R index 7dff936..f34c771 100644 --- a/tests/testthat/test-projects.R +++ b/tests/testthat/test-projects.R @@ -40,3 +40,16 @@ test_that("project checks work correctly", { expect_true(fs::file_exists(sub("test new", "test-new", proj_with_space))) fs::dir_delete(sub("test new", "test-new", proj_with_space)) }) + +test_that("project still creates if Git already exists", { + new_project <- fs::path_temp("test-with-git") + fs::dir_create(new_project) + withr::with_dir(new = new_project, { + gert::git_init() + }) + expect_message( + setup_project(new_project), + regexp = ".*has [Gg]it.*" + ) + expect_true(fs::dir_exists(new_project)) +})