Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Archive marked git repositories daily commits to Day One and/or text file. Improved by @DivineDominion
#!/usr/bin/env ruby
require 'time'
require 'erb'
require 'cgi'
filename = "~/.gitlogger"
## File format, One per line
# Repo Name:/path/to/base
dayone = false # log to day one? (true or false)
textlog = false # "~/Dropbox/nvALT2.2/" # set to false to disable
git_user = %x{git config --get}.strip
git_user = ENV['GIT_AUTHOR_NAME'] if git_user == ''
git_user = '.' if git_user == ''
entrytext = "", 'r') do |infile|
while (line = infile.gets)
name, path = line.strip.split(':')
repo_log = ''
repo_log = %x{git log --first-parent --no-merges --author="#{git_user}" --pretty=format:"%%NEWLINE**[#{name}]** %%%ct%%: %s (%h)%n %+b%n" --since="yesterday"}.gsub(/%(\d+)%/) { |timestamp|
# if no remote repository is specified, `git fetch` will output
# error messages; so we silence them
repo_log = %x{git fetch 2>/dev/null && git log --remotes --first-parent --no-merges --author="#{git_user}" --pretty=format:"* **[#{name}]** %%%ct%%: %s (%h)%n %+b%n" --since="yesterday"}.gsub(/%(\d+)%/) { |timestamp|
} if repo_log == ''
entrytext += repo_log
exit if entrytext.strip == ""
# remove the weird empty lines at the beginning
entrytext = entrytext.gsub(/^\s{4}\n/,"").gsub(/\n{3,}/m,"\n\n")
entries = entrytext.split('%NEWLINE')
output = []
# the first line is always empty gibberish and will be omitted
entries[1..-1].each do |entry|
entry.gsub!(/^(.+)$/, ' \1')
entry[0] = "*"
output << entry
entrytext = output.join
entrytext << "\n"
if dayone
uuid = %x{uuidgen}.gsub(/-/,'').strip
datestamp =
starred = false
dayonedir = %x{ls ~/Library/Mobile\\ Documents/|grep dayoneapp}.strip
dayonepath = "~/Dropbox/Apps/Day\ One/Journal.dayone/entries/"
#"~/Library/Mobile\ Documents/#{dayonedir}/Documents/Journal_dayone/entries/"
entry = CGI.escapeHTML("Git Log #{"%Y-%m-%d")}:\n\n#{entrytext}")
template = <<-XMLTEMPLATE
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">
<key>Creation Date</key>
<date><%= datestamp %></date>
<key>Entry Text</key>
<string><%= entry %></string>
<<%= starred %>/>
<string><%= uuid %></string>
fh =".doentry"),'w+')
fh.puts template.result(binding)
# puts "ENTRY ADDED"
# puts "------------------------"
# puts "Time: " + datestamp
# puts "UUID: " + uuid
# puts "Starred: " + starred.to_s
# puts "Entry: " + entrytext
if textlog
entry = "---\n\n### #{"%D")}:\n\n#{entrytext}"
open(File.expand_path(textlog), 'a') { |f|
f.puts entry

This comment has been minimized.

Copy link

@DivineDominion DivineDominion commented Jan 11, 2014

I updated my fork to read the past day's complete history instead of the last 24hrs. The old behavior was unreliable when the Mac was asleep since before LaunchAgent triggers script execution.

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