Skip to content

Commit

Permalink
Separate use_git() into two steps
Browse files Browse the repository at this point in the history
1. Initialise repo
2. Perform initial commit

Fixes #852
  • Loading branch information
hadley committed Mar 14, 2020
1 parent 6522170 commit 073a2c6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# usethis (development version)

* `use_git()` will now create initial commit if needed (#852)

* In `use_travis()`, `use_travis_badge()` and `browse_travis()`, argument `ext`
now defaults to `"com"` instead of `"ext"`, given travis-ci.com is now
recommended over travis-ci.org (#1038, @riccardoporreca).
Expand Down
36 changes: 24 additions & 12 deletions R/git.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@
#' use_git()
#' }
use_git <- function(message = "Initial commit") {
if (uses_git()) {
return(invisible())
needs_init <- !uses_git()
if (needs_init) {
ui_done("Initialising Git repo")
git_init()
}

ui_done("Initialising Git repo")
git_init()

use_git_ignore(c(".Rhistory", ".RData", ".Rproj.user"))
git_ask_commit(message, untracked = TRUE)
if (git_uncommitted(untracked = TRUE)) {
git_ask_commit(message, untracked = TRUE)
}

if (needs_init) {
restart_rstudio("A restart of RStudio is required to activate the Git pane")
}

restart_rstudio("A restart of RStudio is required to activate the Git pane")
invisible(TRUE)
}

Expand Down Expand Up @@ -49,15 +53,23 @@ git_ask_commit <- function(message, untracked = FALSE) {
))

if (ui_yeah("Is it ok to commit them?")) {
ui_done("Adding files")
repo <- git_repo()
git2r::add(repo, paths)
ui_done("Commit with message {ui_value(message)}")
git2r::commit(repo, message)
git_commit(paths, message)
}
invisible()
}

git_commit <- function(paths, message) {
ui_done("Adding files")
repo <- git_repo()
git2r::add(repo, paths)
ui_done("Commit with message {ui_value(message)}")
git2r::commit(repo, message)
}

git_has_commits <- function() {
length(git2r::commits(n = 1, repo = git_repo())) > 0
}

#' Add a git hook
#'
#' Sets up a git hook using specified script. Creates hook directory if
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-git.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
test_that("git_has_commit() changes after first commit", {
scoped_temporary_package()
git_init()

expect_true(uses_git())
expect_false(git_has_commits())

git_commit("DESCRIPTION", "test")
expect_true(git_has_commits())
})

0 comments on commit 073a2c6

Please # to comment.