-
Notifications
You must be signed in to change notification settings - Fork 62
Description
In git2r unit tests, the working directory is outside of the Git repository being tested, and the file paths are relative to the root of the Git repository, e.g. add(repo, "test.txt")
, where test.txt
does not exist in the current working directory, but instead at file.path(workdir(repo), "test.txt")
.
Thus the following code works on Linux, macOS, and Windows:
# working directory outside of Git repo, file paths relative to root of Git repo
library(git2r)
d <- tempfile()
repo <- clone("https://github.com/ropensci/git2r.git", d)
testfile_rel <- file.path("R", "testfile.txt")
testfile_abs <- file.path(d, testfile_rel)
writeLines("this is a test", testfile_abs)
status(repo)
## Untracked files:
## Untracked: R/testfile.txt
add(repo, testfile_rel)
status(repo)
## Staged changes:
## New: R/testfile.txt
com_add <- commit(repo, "Commit testfile")
compath_add <- commits(repo, path = testfile_rel)
stopifnot(identical(compath_add[[1]]$sha, com_add$sha))
rm_file(repo, path = testfile_rel)
status(repo)
## Staged changes:
## Deleted: R/testfile.txt
com_rm <- commit(repo, "Remove testfile")
compath_rm <- commits(repo, path = testfile_rel)
stopifnot(identical(compath_rm[[1]]$sha, com_rm$sha))
However, when the working directory is set to be a subdirectory of the Git repository, the code continues to work on Linux and macOS, but unexpectedly fails on Windows:
# working directory is subdir of Git repo, file paths relative to root of Git repo
library(git2r)
d <- tempfile()
repo <- clone("https://github.com/ropensci/git2r.git", d)
cwd <- setwd(file.path(d, "man"))
testfile_rel <- file.path("R", "testfile.txt")
testfile_abs <- file.path(d, testfile_rel)
writeLines("this is a test", testfile_abs)
status(repo)
## Untracked files:
## Untracked: R/testfile.txt
add(repo, testfile_rel)
status(repo)
## Staged changes:
## New: R/testfile.txt
com_add <- commit(repo, "Commit testfile")
compath_add <- commits(repo, path = testfile_rel)
stopifnot(identical(compath_add[[1]]$sha, com_add$sha))
rm_file(repo, path = testfile_rel)
status(repo)
## Staged changes:
## Deleted: R/testfile.txt
com_rm <- commit(repo, "Remove testfile")
compath_rm <- commits(repo, path = testfile_rel)
stopifnot(identical(compath_rm[[1]]$sha, com_rm$sha))
setwd(cwd)
Thus only on Windows, when the working directory is a subdirectory of the Git repository, the absolute path to the file is required:
# working directory is subdir of Git repo, absolute file paths
# Required for Windows, but also works on Linux but not macOS
library(git2r)
d <- tempfile()
repo <- clone("https://github.com/ropensci/git2r.git", d)
cwd <- setwd(file.path(d, "man"))
testfile_rel <- file.path("R", "testfile.txt")
testfile_abs <- file.path(d, testfile_rel)
writeLines("this is a test", testfile_abs)
status(repo)
## Untracked files:
## Untracked: R/testfile.txt
add(repo, testfile_abs)
status(repo)
## Staged changes:
## New: R/testfile.txt
com_add <- commit(repo, "Commit testfile")
compath_add <- commits(repo, path = testfile_abs)
stopifnot(identical(compath_add[[1]]$sha, com_add$sha))
rm_file(repo, path = testfile_rel)
status(repo)
## Staged changes:
## Deleted: R/testfile.txt
com_rm <- commit(repo, "Remove testfile")
compath_rm <- commits(repo, path = testfile_abs)
stopifnot(identical(compath_rm[[1]]$sha, com_rm$sha))
setwd(cwd)
Confusingly, while add()
and commits(path)
require an absolute path on Windows when the current working directory is a subdirectory, rm_file()
always requires a relative path. Changing the rm_file()
call in the above example to use an absolute path causes an error (on all 3 operating systems):
rm_file(repo, path = testfile_abs)
Error in rm_file(repo, path = testfile_abs) :
pathspec '/tmp/RtmptCFrfN/file79fe61f79dbb/R/testfile.txt' did not match any files.
Furthermore, always using the absolute path to commits(path)
, even when the file no longer exists, works on Windows and Linux, but not macOS.
Is it possible to make the filepath behavior be consistent across all 3 operating systems? Also, is it possible to change rm_file()
to behave the same as add()
and commits(path)
?
Notes:
- Potentially related issue: Windows git2r sometimes can't add files to repo #315
- The above tests were performed with git2r 0.26.1.