Skip to contents

Retrieve the most recent commit that added or updated a file or git2rdata object. This does not imply that file still exists at the current HEAD as it ignores the deletion of files.

Use this information to document the current version of file or git2rdata object in an analysis. Since it refers to the most recent change of this file, it remains unchanged by committing changes to other files. You can also use it to track if data got updated, requiring an analysis to be rerun. See vignette("workflow", package = "git2rdata").

Usage

recent_commit(file, root, data = FALSE)

Arguments

file

the name of the git2rdata object. Git2rdata objects cannot have dots in their name. The name may include a relative path. file is a path relative to the root. Note that file must point to a location within root.

root

The root of a project. Can be a file path or a git-repository.

data

does file refers to a data object (TRUE) or to a file (FALSE)? Defaults to FALSE.

Value

a data.frame with commit, author and when for the most recent commit that adds op updates the file.

See also

Other version_control: commit(), pull(), push(), repository(), status()

Examples

# initialise a git repo using git2r
repo_path <- tempfile("git2rdata-repo")
dir.create(repo_path)
repo <- git2r::init(repo_path)
git2r::config(repo, user.name = "Alice", user.email = "alice@example.org")

# write and commit a first dataframe
# store the output of write_vc() minimize screen output
junk <- write_vc(
  iris[1:6, ], "iris", repo, sorting = "Sepal.Length", stage = TRUE,
  digits = 6
)
commit(repo, "important analysis", session = TRUE)
#> [547b9f3] 2025-01-24: important analysis
list.files(repo_path)
#> [1] "iris.tsv" "iris.yml"
Sys.sleep(1.1) # required because git doesn't handle subsecond timings

# write and commit a second dataframe
junk <- write_vc(
  iris[7:12, ], "iris2", repo, sorting = "Sepal.Length", stage = TRUE,
  digits = 6
)
commit(repo, "important analysis", session = TRUE)
#> [7a9451a] 2025-01-24: important analysis
list.files(repo_path)
#> [1] "iris.tsv"  "iris.yml"  "iris2.tsv" "iris2.yml"
Sys.sleep(1.1) # required because git doesn't handle subsecond timings

# write and commit a new version of the first dataframe
junk <- write_vc(iris[7:12, ], "iris", repo, stage = TRUE)
list.files(repo_path)
#> [1] "iris.tsv"  "iris.yml"  "iris2.tsv" "iris2.yml"
commit(repo, "important analysis", session = TRUE)
#> [9caaa6a] 2025-01-24: important analysis



# find out in which commit a file was last changed

# "iris.tsv" was last updated in the third commit
recent_commit("iris.tsv", repo)
#>                                     commit author                when
#> 1 9caaa6a8fa8c7e4c0f10fd0911ad5cc3dd4a108f  Alice 2025-01-24 08:50:11
# "iris.yml" was last updated in the first commit
recent_commit("iris.yml", repo)
#>                                     commit author                when
#> 1 9caaa6a8fa8c7e4c0f10fd0911ad5cc3dd4a108f  Alice 2025-01-24 08:50:11
# "iris2.yml" was last updated in the second commit
recent_commit("iris2.yml", repo)
#>                                     commit author                when
#> 1 7a9451ad8382476e65f961f1ab23316659698c15  Alice 2025-01-24 08:50:10
# the git2rdata object "iris" was last updated in the third commit
recent_commit("iris", repo, data = TRUE)
#>                                     commit author                when
#> 1 9caaa6a8fa8c7e4c0f10fd0911ad5cc3dd4a108f  Alice 2025-01-24 08:50:11

# remove a dataframe and commit it to see what happens with deleted files
file.remove(file.path(repo_path, "iris.tsv"))
#> [1] TRUE
prune_meta(repo, ".")
commit(repo, message = "remove iris", all = TRUE, session = TRUE)
#> [c0df679] 2025-01-24: remove iris
list.files(repo_path)
#> [1] "iris2.tsv" "iris2.yml"

# still points to the third commit as this is the latest commit in which the
# data was present
recent_commit("iris", repo, data = TRUE)
#>                                     commit author                when
#> 1 9caaa6a8fa8c7e4c0f10fd0911ad5cc3dd4a108f  Alice 2025-01-24 08:50:11