-
-
Save DivineDominion/5775355 to your computer and use it in GitHub Desktop.
Logs selected git repository commits to a text file or Day One for the past day once in the morning.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>Label</key> | |
<string>com.brettterpstra.gitlogger</string> | |
<key>EnvironmentVariables</key> | |
<dict> | |
<key>LANG</key> | |
<string>en_US.UTF-8</string> | |
</dict> | |
<key>ProgramArguments</key> | |
<array> | |
<string>/usr/bin/ruby</string> | |
<string>/usr/local/bin/gitlogger.rb</string> | |
</array> | |
<key>StartCalendarInterval</key> | |
<dict> | |
<key>Hour</key> | |
<integer>08</integer> | |
<key>Minute</key> | |
<integer>00</integer> | |
</dict> | |
</dict> | |
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'date' | |
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/GitLogger.md" # set to false to disable | |
git_user = %x{git config --get user.name}.strip | |
git_user = ENV['GIT_AUTHOR_NAME'] if git_user == '' | |
git_user = '.' if git_user == '' | |
entrytext = "" | |
File.open(File.expand_path(filename), 'r') do |infile| | |
while (line = infile.gets) | |
name, path = line.strip.split(':') | |
Dir.chdir(path) | |
repo_log = '' | |
repo_log = %x{git log --first-parent --no-merges --author="#{git_user}" --since="day.before.yesterday.midnight" --until="midnight" --pretty=format:"%%NEWLINE**[#{name}]** %%%ct%%: %s (%h)%n %+b%n"}.gsub(/%(\d+)%/) { |timestamp| | |
timestamp.gsub!(/%/,'') | |
Time.at(timestamp.to_i).strftime("%H:%M").gsub(/^0/,'') | |
} | |
# 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| | |
timestamp.gsub!(/%/,'') | |
Time.at(timestamp.to_i).strftime("%H:%M").gsub(/^0/,'') | |
} if repo_log == '' | |
entrytext += repo_log | |
end | |
end | |
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 | |
end | |
entrytext = output.join | |
entrytext << "\n" | |
yesterday = Date.today.to_time - 1 | |
if dayone | |
uuid = %x{uuidgen}.gsub(/-/,'').strip | |
datestamp = yesterday.utc.iso8601 | |
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 #{yesterday.strftime("%Y-%m-%d")}:\n\n#{entrytext}") | |
template = ERB.new <<-XMLTEMPLATE | |
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>Creation Date</key> | |
<date><%= datestamp %></date> | |
<key>Entry Text</key> | |
<string><%= entry %></string> | |
<key>Starred</key> | |
<<%= starred %>/> | |
<key>UUID</key> | |
<string><%= uuid %></string> | |
</dict> | |
</plist> | |
XMLTEMPLATE | |
fh = File.new(File.expand_path(dayonepath+uuid+".doentry"),'w+') | |
fh.puts template.result(binding) | |
fh.close | |
# puts "ENTRY ADDED" | |
# puts "------------------------" | |
# puts "Time: " + datestamp | |
# puts "UUID: " + uuid | |
# puts "Starred: " + starred.to_s | |
# puts "Entry: " + entrytext | |
end | |
if textlog | |
entry = "---\n\n### #{yesterday.strftime("%D")}:\n\n#{entrytext}" | |
open(File.expand_path(textlog), 'a') { |f| | |
f.puts entry | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
According to ttscoff/Slogger#222, I had to add the following to my launchd script to make UTF characters work and not raise errors:
To debug, add this to the .plist, replacing
<<USER>>
with your Unix user name.