Skip to contents

This function checks if the specified directory is a Git repository and, if so, executes a git log command to either print the log to the console or return it. It supports execution on Windows and Linux operating systems and provides a visually appealing graph format of the log, showing the commit hash, references, commit message, relative commit date, and author name.

Usage

git_log(Path = ".", Num = NULL, ReturnLog = FALSE)

Arguments

Path

A character string specifying the path to the directory to check. Defaults to the current working directory ".". If the path does not exist, the function will stop and throw an error. If the path is not a git repository, the function will throw a warning.

Num

An optional numeric value specifying the number of recent commits to display. If NULL (the default), the complete log is shown. If Num is not NULL or a positive number, the function will stop and throw an error.

ReturnLog

A logical value indicating whether to return the log (TRUE) or print it to the console (FALSE, default). If TRUE, the function returns a character vector containing the log lines.

Value

If ReturnLog is TRUE, returns a character vector containing the git log lines. If ReturnLog is FALSE, the function is called for its side effect of printing to the console.

Note

The function will stop and throw an error if the specified path does not exist, the operating system is not supported, the directory is not a Git repository, Git is not installed, or if the Num parameter is not NULL or a positive number.

See also

Examples

# not a git repo
git_log(Path = "C:/")
#> Error: The provided path does not exist.

# Show the most recent commit
git_log(Path = ".", Num = 1)
#> * 7896b0c - (grafted, HEAD -> main, origin/main) Add Zetero DOI (5 minutes ago) <Ahmed El-Gabbas>

# Show the most recent 5 commits
git_log(Path = ".", Num = 5)
#> * 7896b0c - (grafted, HEAD -> main, origin/main) Add Zetero DOI (5 minutes ago) <Ahmed El-Gabbas>

git_log(Path = ".", Num = 5, ReturnLog = TRUE)
#> [1] "* 7896b0c - (grafted, HEAD -> main, origin/main) Add Zetero DOI (5 minutes ago) <Ahmed El-Gabbas>"