Skip to content

Instantly share code, notes, and snippets.

@hadley
Created August 22, 2012 16:17
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hadley/3427128 to your computer and use it in GitHub Desktop.
Save hadley/3427128 to your computer and use it in GitHub Desktop.
library(memoise)
library(stringr)
git_prompt <- function() {
git <- find_git()
if (is.null(git)) stop("Git not installed", call. = FALSE)
if (!in_git_repo(git)) stop("Not in git repo", call. = FALSE)
update <- function(...) update_prompt(git)
addTaskCallback(update, name = "git")
update()
invisible(TRUE)
}
find_git <- memoise(function() {
if (on_path("git")) return("git")
if (on_path("git.exe")) return("git")
NULL
})
update_prompt <- function(git = find_git()) {
if (in_git_repo()) {
prompt <- paste("(", git_branch(git), ")",
if (is_dirty(git)) "*", "> ", sep = "")
} else {
prompt <- "> "
}
options(prompt = prompt)
invisible(TRUE)
}
git_branch <- function(git = find_git()) {
system2(git, "symbolic-ref --short -q HEAD", stdout = TRUE, stderr = FALSE)
}
is_dirty <- function(git = find_git()) {
system2(git, "diff-index HEAD --quiet", stdout = FALSE) == 1
}
in_git_repo <- function(git = find_git()) {
system2(git, "rev-parse", stdout = FALSE, stderr = FALSE) == 0
}
@jbmartin
Copy link

Does on_path() require devtools?

@rmflight
Copy link

Is there any plans to put this into the devtools package? That would be kinda cool.

library(devtools)
git_prompt()

@emilmahler
Copy link

on_path doesn't appear to be a part of the devtools package anymore. Has it been moved to another package? Is there an alternative function that can be used?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment