Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git Log script using r, a spin-off from watching https://www.youtube.com/watch?v=CB9p8n3gugM, no libs allowed. see also: https://gist.github.com/neerajt/4f04f3d7dd93b51dabbcac5e4a0b0456
GIT_PATH <- '.git'
PARENT_SEP <- 'parent '
get_log <- function() {
current_hash <- get_current_hash()
current_commit <- get_commit(current_hash)
all_commits <- c(format_commit(current_hash, current_commit))
while(has_parent(current_commit)){
current_hash <- get_parent_of_commit(current_commit)
current_commit <- get_commit(current_hash)
all_commits <- append(all_commits, format_commit(current_hash, current_commit))
}
print(cat(paste(all_commits, collapse='\n')))
}
format_commit <- function(hash, commit){
commit_keeps <- !sapply(commit, is_parent_or_tree)
return(append(c('', paste0('\033[1;37m','commit ', hash, '\033[0m')), commit[commit_keeps]))
}
has_parent <- function(commit){
return(any(sapply(commit, is_parent)))
}
is_parent_or_tree <- function(commitLine){
return(is_tree(commitLine) | is_parent(commitLine))
}
is_tree <- function(commitLine){
return(regexpr('tree ', commitLine)[1] == 1)
}
is_parent <- function(commitLine){
return(regexpr(PARENT_SEP, commitLine)[1] == 1)
}
get_parent_of_commit <- function(commit){
return(gsub(PARENT_SEP, '', commit[2]))
}
get_commit <- function(hash){
GIT_COMMAND <- 'git cat-file -p '
return(system(paste0(GIT_COMMAND, hash), intern=TRUE))
}
get_current_hash <- function() {
REF_SEP <- 'ref: '
ref <- readLines(get_git_dir('HEAD'))
ref_directory <- gsub(REF_SEP, '', ref)
return(readLines(get_git_dir(ref_directory)))
}
get_git_dir <- function(subdir) {
return(paste0(GIT_PATH, '/', subdir))
}
get_log()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment