Last active
December 25, 2015 22:49
-
-
Save mizoR/7052884 to your computer and use it in GitHub Desktop.
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 | |
# -*- encoding: utf-8 -*- | |
require 'open-uri' | |
require 'json' | |
class String | |
def blue | |
"\e[34m" + self + "\e[m" | |
end | |
def yellow | |
"\e[33m" + self + "\e[m" | |
end | |
def red | |
"\e[31m" + self + "\e[m" | |
end | |
end | |
module GitStock | |
class Client | |
def initialize(options={}) | |
@store_path = options.delete(:store_path) || './gitstock.store' | |
end | |
def run(*argv) | |
self.send(*argv) | |
end | |
def open(sha) | |
database = get_database | |
data = database.detect {|row| row['sha'] =~ /^#{sha}/} | |
`open #{data['html_url']}` | |
end | |
def diff(sha) | |
database = get_database | |
data = database.detect {|row| row['sha'] =~ /^#{sha}/} | |
files = data['files'] | |
files.each do |file| | |
next if !file['patch'] | |
file['patch'].each_line do |line| | |
print \ | |
case line | |
when /^@@/ | |
line.blue | |
when /^\+/ | |
line.yellow | |
when /^\-/ | |
line.red | |
else | |
line | |
end | |
end | |
end | |
end | |
def list | |
database = get_database | |
database.each do |data| | |
puts "commit #{data['sha']}".yellow | |
puts "Author: #{data['commit']['author']['name']} <#{data['commit']['author']['email']}>" | |
puts "Date: #{data['commit']['author']['date']}" | |
puts | |
data['commit']['message'].each_line do |line| | |
puts "\t#{line}" | |
end | |
puts | |
end | |
end | |
def add(owner, repo, sha) | |
url = "https://api.github.com/repos/#{owner}/#{repo}/commits/#{sha}" | |
data = OpenURI.open_uri(url) do |io| | |
JSON.load(io) | |
end | |
create(data) | |
end | |
private | |
def get_database | |
if !File.exist?(@store_path) | |
[] | |
else | |
File.open(@store_path) { |io| JSON.load(io) } | |
end | |
end | |
def create(data) | |
database = get_database | |
if database.none? {|v| data['sha'] == v['sha']} | |
database.unshift(data.merge('created_at' => Time.now)) | |
File.open(@store_path, 'w') do |io| | |
JSON.dump(database, io) | |
end | |
end | |
end | |
end | |
end | |
if __FILE__ == $0 | |
client = GitStock::Client.new(:store_path => "#{ENV['HOME']}/Dropbox/gitstock.store") | |
regexp_github_commit_url = %r<^https://github.com/([\w]+)/([\w\.\-]+)/commit/(\w+)$> | |
case | |
when ARGV[0] == 'list' | |
client.run(ARGV[0]) | |
when (ARGV[0] == 'add') && (ARGV[1] =~ regexp_github_commit_url) | |
client.run(ARGV[0], $1, $2, $3) | |
when ARGV[0] =~ /^diff|open$/ && ARGV[1].length > 0 | |
client.run(ARGV[0], ARGV[1]) | |
else | |
puts 'git stock <command> [sha] [<args>]' | |
puts | |
puts 'Commands:' | |
puts ' list :listing stocks' | |
puts ' add <github_commit_url> :create stock' | |
puts ' diff <sha> :display stocked commit diff' | |
puts ' open <sha> :open <github_commit_url>' | |
exit 1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment