Skip to content

Commit f9a4800

Browse files
committed
fix: force in git_branch_create()
1 parent 1113305 commit f9a4800

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# gert (development version)
22

3+
- `git_branch_create()`: `force` now also applies to the checkout step, allowing branch creation even when local changes would be overwritten (@MichaelChirico, #177).
34
- Improve manual pages (@olivroy, #227)
45
- Fix badge links in `README.md` (@dpprdan, #189)
56
- Use `NEWS.md` instead of `NEWS` (@olivroy, #209)

src/branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ SEXP R_git_create_branch(SEXP ptr, SEXP name, SEXP ref, SEXP checkout, SEXP forc
4949
git_reference *branch = NULL;
5050
const char *source = CHAR(STRING_ELT(ref, 0));
5151
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
52-
opts.checkout_strategy = GIT_CHECKOUT_SAFE;
52+
opts.checkout_strategy = Rf_asLogical(force) ? GIT_CHECKOUT_FORCE : GIT_CHECKOUT_SAFE;
5353
set_checkout_notify_cb(&opts);
5454
git_repository *repo = get_git_repository(ptr);
5555
git_object *revision = resolve_refish(ref, repo);

tests/testthat/test-branch.R

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
test_that("git_branch_create force applies to checkout", {
2+
repo <- git_init(tempfile("gert-tests-branch-force"))
3+
on.exit(unlink(repo, recursive = TRUE))
4+
configure_local_user(repo)
5+
6+
# First commit: hello.txt = "v1"
7+
writeLines("v1", file.path(repo, "hello.txt"))
8+
git_add("hello.txt", repo = repo)
9+
first <- git_commit("First commit", repo = repo)
10+
11+
# Second commit: hello.txt = "v2"
12+
writeLines("v2", file.path(repo, "hello.txt"))
13+
git_add("hello.txt", repo = repo)
14+
git_commit("Second commit", repo = repo)
15+
16+
# Unstaged local change: hello.txt = "v3"
17+
writeLines("v3", file.path(repo, "hello.txt"))
18+
19+
# force = FALSE: checkout to first commit conflicts with local change
20+
expect_error(
21+
git_branch_create("oldbranch", ref = first, checkout = TRUE, force = FALSE, repo = repo)
22+
)
23+
24+
# force = TRUE: checkout succeeds, overwriting local change
25+
git_branch_create("oldbranch2", ref = first, checkout = TRUE, force = TRUE, repo = repo)
26+
expect_equal(git_branch(repo = repo), "oldbranch2")
27+
expect_equal(readLines(file.path(repo, "hello.txt")), "v1")
28+
})

0 commit comments

Comments
 (0)