Skip to content

Commit 63c8e7f

Browse files
committed
Feature upgrade for git_checkout()
- added 2 helpers to run the 2 options for "branch" checkout and "file" checkout - added new stop() catches for edge-cases - added unit tests to cover the new options for `git_checkout()` - closes #2
1 parent 53829a9 commit 63c8e7f

8 files changed

Lines changed: 168 additions & 40 deletions

File tree

‎DESCRIPTION‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Collate:
4040
'gitr-package.R'
4141
'git.R'
4242
'git-branch.R'
43+
'git-checkout.R'
4344
'git-commit.R'
4445
'git-lint-commit.R'
4546
'git-pull-request.R'

‎R/git-checkout.R‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#' Git Checkout
2+
#'
3+
#' Checks out as a branch if doesn't exist. Branch
4+
#' oriented workflow for switching between branches.
5+
#' If `file` is passed, checks out the file. A common
6+
#' shortcut to undoing local changes to a file(s).
7+
#' Can be a vector of multiple files.
8+
#'
9+
#' @name checkout
10+
#'
11+
#' @inheritParams params
12+
#'
13+
#' @return `NULL` ... invisibly.
14+
#'
15+
#' @examples
16+
#' \dontrun{
17+
#' git_checkout("feature-br")
18+
#' git_checkout(file = "DESCRIPTION")
19+
#' }
20+
#' @export
21+
git_checkout <- function(branch = NULL, file = NULL) {
22+
if ( !is_git() ) {
23+
return(invisible())
24+
}
25+
26+
if ( is.null(branch) && is.null(file) ) {
27+
stop("You must pass a either `branch` or a `file` to checkout.",
28+
call. = FALSE)
29+
} else if ( is.null(branch) ) {
30+
gitr_checkout_file(file)
31+
} else if ( is.null(file) ) {
32+
gitr_checkout_branch(branch)
33+
} else {
34+
stop("You cannot pass *both* a `branch` and a `file` to checkout.",
35+
call. = FALSE)
36+
}
37+
38+
invisible()
39+
}
40+
41+
42+
# helpers ----
43+
gitr_checkout_file <- function(file) {
44+
tracked_files <- git("ls-files", echo_cmd = FALSE)$stdout
45+
stopifnot(
46+
"the `file` must be tracked by git." = file %in% tracked_files
47+
)
48+
out <- git("checkout", file)
49+
if ( getOption("gitr_echo_cmd", TRUE) ) {
50+
cat(out$stdout, sep = "\n")
51+
#cat(out$stderr, sep = "\n")
52+
}
53+
}
54+
55+
gitr_checkout_branch <- function(branch) {
56+
br_exists <- branch %in% git_local_br()
57+
tracked_files <- git("ls-files", echo_cmd = FALSE)$stdout
58+
59+
if ( branch %in% tracked_files ) {
60+
stop("`branch` cannot have the same name as a tracked file.",
61+
call. = FALSE)
62+
}
63+
64+
if ( br_exists ) {
65+
out <- git("checkout", branch)
66+
} else {
67+
out <- git("checkout", "-b", branch)
68+
}
69+
70+
if ( getOption("gitr_echo_cmd", TRUE) ) {
71+
cat(out$stdout, sep = "\n")
72+
#cat(out$stderr, sep = "\n")
73+
}
74+
}

‎R/git.R‎

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -88,31 +88,3 @@ git_version <- function() {
8888
gsub(".*([1-3]\\.[0-9]{1,3}\\.[0-9]{1,3}).*", "\\1", ver)
8989
}
9090

91-
#' @describeIn git
92-
#' `git checkout` as a branch if doesn't exist. Branch
93-
#' oriented workflow for switching between branches.
94-
#'
95-
#' @inheritParams params
96-
#'
97-
#' @return `git_checkout()`: `NULL` ... invisibly.
98-
#'
99-
#' @export
100-
git_checkout <- function(branch = NULL) {
101-
if ( is.null(branch) ) {
102-
stop("You must pass a branch to checkout.", call. = FALSE)
103-
}
104-
if ( is_git() ) {
105-
br <- git("branch", "--list", branch, echo_cmd = FALSE)
106-
files <- git("ls-files", echo_cmd = FALSE)$stdout
107-
if ( !(branch %in% files) && identical(br$stdout, "") ) {
108-
out <- git("checkout", "-b", branch) # branch doesn't yet exist
109-
} else {
110-
out <- git("checkout", branch)
111-
}
112-
if ( getOption("gitr_echo_cmd", TRUE) ) {
113-
cat(out$stdout, sep = "\n")
114-
cat(out$stderr, sep = "\n")
115-
}
116-
}
117-
invisible()
118-
}

‎man/checkout.Rd‎

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎man/git.Rd‎

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# `git_checkout()` checks out a file when asked
2+
3+
Code
4+
git_checkout(file = "DESCRIPTION")
5+
Output
6+
Running git ls-files
7+
Running git checkout DESCRIPTION
8+
Updated 0 paths from the index
9+
10+
---
11+
12+
Code
13+
git_checkout(file = files)
14+
Output
15+
Running git ls-files
16+
Running git checkout NAMESPACE DESCRIPTION NEWS.md README.Rmd
17+
Updated 4 paths from the index
18+

‎tests/testthat/test-git-checkout.R‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
# Setup ----
3+
skip_if_not(is_git())
4+
dir <- local_create_worktree()
5+
6+
7+
# Testing ----
8+
test_that("`git_checkout()` errors when params are bad", {
9+
expect_error(
10+
git_checkout(),
11+
"You must pass a either `branch` or a `file` to checkout."
12+
)
13+
expect_error(
14+
git_checkout("DESCRIPTION"), # common error; param order
15+
"`branch` cannot have the same name as a tracked file."
16+
)
17+
expect_error(
18+
git_checkout("foo", "bar"),
19+
"You cannot pass *both* a `branch` and a `file` to checkout.",
20+
fixed = TRUE
21+
)
22+
})
23+
24+
test_that("`git_checkout()` checks out a branch when asked", {
25+
new_br <- "feature-wip"
26+
withr::defer(git("branch", "-D", new_br)) # delete the 'new_br' branch
27+
git_checkout(new_br)
28+
expect_true(new_br %in% git_local_br()) # new br is created
29+
expect_equal(git_current_br(), new_br) # switched to new br
30+
git_checkout("gitr-test-br") # go back to default br
31+
expect_equal(git_current_br(), "gitr-test-br")
32+
})
33+
34+
test_that("`git_checkout()` checks out a file when asked", {
35+
withr::local_options(list(gitr_echo_cmd = TRUE)) # turn on for snapshot
36+
# no changes; 0 paths updated from index
37+
expect_snapshot(git_checkout(file = "DESCRIPTION"))
38+
39+
files <- c("NAMESPACE", "DESCRIPTION", "NEWS.md", "README.Rmd")
40+
unlink(files, force = TRUE) # delete 4 files
41+
# 4 deletions; 4 paths updated from index
42+
expect_snapshot(git_checkout(file = files))
43+
})

‎tests/testthat/test-git-commit.R‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ test_that("`git_uncommit()` pops the current commit off the commit stack", {
7676

7777
test_that("`git_unstage()` unstages a file from the staging area", {
7878
writeLines("This is a unit test for `git_unstage()`", con = "DESCRIPTION")
79-
withr::defer(git_checkout("DESCRIPTION")) # cleanup
79+
withr::defer(git_checkout(file = "DESCRIPTION")) # cleanup
8080
git("add", "DESCRIPTION")
8181
staged <- git("diff", "--name-only", "--cached")$stdout
8282
expect_equal(staged, "DESCRIPTION") # check staged

0 commit comments

Comments
 (0)