-
Notifications
You must be signed in to change notification settings - Fork 0
/
gistory.rb
109 lines (81 loc) · 2.21 KB
/
gistory.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
abort %(Usage:
ruby gistory.rb repo_path file_path [branch_other_than_master]
) unless ARGV.size.between?(2, 3)
repo_path = File.expand_path(ARGV.first)
abort "Error: #{repo_path} does not exist" unless File.exist?(repo_path)
require 'grit'
Grit::Git.git_timeout = 60
class Grit::Actor
def name_email
"#{name} <#{email}>"
end
def ==(other)
name_email == other.name_email
end
end
repo = begin
Grit::Repo.new(repo_path)
rescue Grit::InvalidGitRepositoryError
abort "Error: #{repo_path} is not a git repo"
end
$stderr.puts "Loading commits ..."
file = ARGV[1]
branch = ARGV[2] || 'master'
require 'lib/gistory'
commits = Gistory::CommitParser.parse(repo, file, branch)
Gistory::CommitParser.index_and_generate!(commits)
puts commits.inspect
abort %(Error: Couldn't find any commits.
Are you sure this is a git repo and the file exists?) if commits.empty?
$stderr.puts "Loaded commits"
require 'sinatra'
require 'json'
require 'erb'
set :logging, false
set :host, 'localhost'
set :port, 6568
get '/' do
@delay = 1
@lps_change = 10
@lps_scroll = 50
@commits = commits
erb :index
end
get '/commits' do
@commits = commits
content_type 'text/javascript'
erb :commits, :layout => false
end
get /^\/commit\/(\d+)$/ do
@commit_index = params['captures'][0].to_i
@commits = commits
@commit_diff = commits[@commit_index]
content_type 'text/javascript'
erb :commit, :layout => false
end
#Given a commit number, returns the blob as of that commit
get '/blob/*' do
@commit_index = params[:splat].first.to_i
diff = commits[@commit_index][:diff]
# puts diff.inspect
blob = diff.deleted_file ? diff.a_blob : diff.b_blob
puts blob.data
body blob.data
end
helpers do
def j(str)
str.to_s.gsub('\\', '\\\\\\\\').gsub(/[&"><\n\r]/) { |special| { '&' => '\u0026', '>' => '\u003E', '<' => '\u003C', '"' => '\"', "\n" => "\\n", "\r" => "\\r" }[special] }
end
end
$stderr.puts "Waiting to launch gistory..."
Thread.new do
sleep(1)
#if RUBY_PLATFORM =~ /(win|w)32$/
# `start http://localhost:6568/`
if RUBY_PLATFORM =~ /darwin/
`open http://localhost:6568/`
$stderr.puts "Launched gistory"
else
puts "Please open your web browser and visit http://localhost:6568/"
end
end