From ab26972039e9664f382d24ac25721fe56e32c65a Mon Sep 17 00:00:00 2001 From: Paul Lietar Date: Thu, 27 Jun 2024 18:04:09 +0100 Subject: [PATCH] Encode missing values as null instead of omitting them. --- R/outpack_misc.R | 14 ++++++++------ inst/schema/outpack/git.json | 4 ++-- tests/testthat/test-outpack-git.R | 5 ++++- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/R/outpack_misc.R b/R/outpack_misc.R index 3c3be020..e1fa0c3a 100644 --- a/R/outpack_misc.R +++ b/R/outpack_misc.R @@ -45,16 +45,18 @@ git_info <- function(path) { return(NULL) } + sha <- tryCatch(gert::git_commit_id(repo = repo), + error = function(e) NA) + branch <- gert::git_branch(repo = repo) - if (identical(branch, "HEAD")) { - # HEAD isn't a valid branch name, and instead is what gets returned when a + if (is.null(branch) || identical(branch, "HEAD")) { + # NULL can be returned when working in a repo that has no commits yet. + # "HEAD" isn't a valid branch name and instead is what gets returned when a # detached head was checked out. - branch <- NULL + branch <- NA } - list(sha = ignore_errors(gert::git_commit_id(repo = repo)), - branch = branch, - url = gert::git_remote_list(repo = repo)$url) + list(sha = sha, branch = branch, url = gert::git_remote_list(repo = repo)$url) } diff --git a/inst/schema/outpack/git.json b/inst/schema/outpack/git.json index e5bfbdf4..5fe44ac2 100644 --- a/inst/schema/outpack/git.json +++ b/inst/schema/outpack/git.json @@ -7,11 +7,11 @@ "type": "object", "properties": { "sha": { - "type": "string", + "type": ["string", "null"], "pattern": "^[0-9a-f]+$" }, "branch": { - "type": "string" + "type": ["string", "null"] }, "url": { "type": "array", diff --git a/tests/testthat/test-outpack-git.R b/tests/testthat/test-outpack-git.R index bdc3ae9c..0e680a57 100644 --- a/tests/testthat/test-outpack-git.R +++ b/tests/testthat/test-outpack-git.R @@ -89,6 +89,7 @@ test_that("store git information when on a detached HEAD", { meta <- orderly_metadata(id, root = root$path) expect_mapequal(meta$git, list(sha = git_info$sha, + branch = NULL, url = git_info$url)) }) @@ -108,5 +109,7 @@ test_that("handle empty git repository correctly", { }) meta <- orderly_metadata(id, root = root$path) - expect_mapequal(meta$git, list(url = "https://example.com/git")) + expect_mapequal(meta$git, list(sha = NULL, + branch = NULL, + url = "https://example.com/git")) })