git2r (version 0.10.1)

discover_repository: Find path to repository for any file

Description

libgit's git_discover_repository is used to identify the location of the repository. The path will therefore be terminated by a file separator.

Usage

discover_repository(path)

## S3 method for class 'character': discover_repository(path)

Arguments

path
A character vector specifying the path to a file or folder

Value

  • Character vector with path to repository or NULL if this cannot be established.

Examples

Run this code
## Initialize a temporary repository
path <- tempfile(pattern="git2r-")
dir.create(path)
repo <- init(path)

## Create a user and commit a file
config(repo, user.name="Alice", user.email="alice@example.org")
writeLines("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do",
           file.path(path, "example-1.txt"))
add(repo, "example-1.txt")
commit(repo, "First commit message")

## Create a second file. The file is not added for version control
## in the repository.
dir.create(file.path(path, "example"))
file_2 <- file.path(path, "example/example-2.txt")
writeLines("Not under version control", file_2)

## Find the path to the repository using the path to the second file
discover_repository(file_2)

Run the code above in your browser using DataCamp Workspace