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 = ".", n_commits = NULL, return_log = FALSE)

Arguments

path

Character. 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.

n_commits

Integer. Number of recent commits to display. If NULL (the default), the complete log is shown. If n_commits is not NULL or a positive number, the function will stop and throw an error. This parameter is ignored if return_log is TRUE.

return_log

Logical. 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 return_log is TRUE, returns a character vector containing the git log lines. If return_log 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 n_commits parameter is not NULL or a positive number.

Author

Ahmed El-Gabbas

Examples

# Show the most recent commit
git_log(n_commits = 1)
#> * fa6b5ae - (grafted, HEAD -> main, origin/main, origin/HEAD) CV model postprocessing + fix typo in convergence plots and add a function to plot range values of beta params +  merge cpp functions into single md file (3 minutes ago) <Ahmed El-Gabbas>

# Show the most recent 5 commits
git_log(n_commits = 5)
#> * fa6b5ae - (grafted, HEAD -> main, origin/main, origin/HEAD) CV model postprocessing + fix typo in convergence plots and add a function to plot range values of beta params +  merge cpp functions into single md file (3 minutes ago) <Ahmed El-Gabbas>

# Return the log as a character vector
Log <- git_log(return_log = TRUE)

length(Log)
#> [1] 1

head(Log, 8)
#> [1] "* fa6b5ae - (grafted, HEAD -> main, origin/main, origin/HEAD) CV model postprocessing + fix typo in convergence plots and add a function to plot range values of beta params +  merge cpp functions into single md file (3 minutes ago) <Ahmed El-Gabbas>"

if (FALSE) { # \dontrun{
  # not a git repo
  git_log(path = "C:/")
  # #> Error: The provided path does not exist.
} # }