Skip to content

Commit

Permalink
Encode missing values as null instead of omitting them.
Browse files Browse the repository at this point in the history
  • Loading branch information
plietar committed Jun 27, 2024
1 parent 2a149b9 commit ab26972
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
14 changes: 8 additions & 6 deletions R/outpack_misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}


Expand Down
4 changes: 2 additions & 2 deletions inst/schema/outpack/git.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion tests/testthat/test-outpack-git.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})

Expand All @@ -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"))
})

0 comments on commit ab26972

Please sign in to comment.