Skip to content

Instantly share code, notes, and snippets.

@hadley
Created August 22, 2012 16:17
Show Gist options
  • Select an option

  • Save hadley/3427128 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

Does on_path() require devtools?

@rmflight
Copy link
Copy Markdown

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

library(devtools)
git_prompt()

@emilmahler
Copy link
Copy Markdown

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