Skip to content

Commit b453cdc

Browse files
committed
better dry
1 parent ab4bce6 commit b453cdc

File tree

3 files changed

+80
-6
lines changed

3 files changed

+80
-6
lines changed

lib/views/graph.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<%= @full_list.to_json %>
22

33
<hr>
4+
<h2>
5+
<%= @full_list.last %>
6+
</h2>
47

58
<%# need list of Head/branch
69
for each commit see if it has a HEAD if so, put branch name else none

lib/web_git.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
module WebGit
55
require "web_git/diff"
6+
require "web_git/graph"
67
require "web_git/string"
78
require "sinatra"
89
require "date"
@@ -37,16 +38,12 @@ class Server < Sinatra::Base
3738
branch = { branch: branch_name }
3839
p g.checkout(branch_name)
3940
list = []
40-
# g.branch.name
41-
# p "—————————"
41+
4242
g.log.sort_by(&:date).each do |log|
4343
commit = log.sha.slice(0..7)
4444
list.push commit
4545
end
46-
# p g.branch.name
47-
# p "-----"
48-
# p list
49-
# list.join("<br>")
46+
5047
p "—————————"
5148
p branch
5249
branch[:log] = list
@@ -73,6 +70,8 @@ class Server < Sinatra::Base
7370
full_list.push list
7471
# full_list.to_json
7572
@full_list = full_list
73+
# graph = WebGit::Graph.new(g)
74+
# @full_list = graph.to_json
7675
# mmm = []
7776
# full_list.last.each do |commit|
7877
# mmm.push commit + " — " + g.gcommit(commit).message

lib/web_git/graph.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
module WebGit
2+
require "git"
3+
class Graph
4+
def initialize(git)
5+
@git = git
6+
@full_list = []
7+
end
8+
9+
def to_json
10+
11+
had_changes = has_untracked_changes?
12+
if had_changes
13+
temporarily_stash_changes
14+
end
15+
16+
list = draw_graph
17+
18+
if had_changes
19+
stash_pop
20+
end
21+
22+
@full_list.push list
23+
end
24+
25+
26+
def has_untracked_changes?
27+
@git.diff.size > 0
28+
end
29+
30+
def temporarily_stash_changes
31+
@git.add(all: true)
32+
stash_count = Git::Stashes.new(@git).count
33+
Git::Stash.new(@git, "Temporary Stash #{stash_count}")
34+
end
35+
36+
def stash_pop
37+
stashes = Git::Stashes.new(@git)
38+
stashes.apply(0)
39+
end
40+
41+
def draw_graph
42+
branches = @git.branches.local.map(&:name)
43+
branches.each do |branch_name|
44+
branch = { branch: branch_name }
45+
list = []
46+
@git.log.sort_by(&:date).each do |log|
47+
commit = log.sha.slice(0..7)
48+
list.push commit
49+
end
50+
51+
branch[:log] = list
52+
branch[:head] = list.last
53+
@full_list.push branch
54+
end
55+
lists = @full_list.map{|l| l[:log] }
56+
combined_branch = { branch: "ALL", head: "_" }
57+
@full_list.push combined_branch
58+
# combined_branch = { branch: "ALL", head: "_" }
59+
list = []
60+
(lists.count - 1).times do |i|
61+
log_hash = lists[i]
62+
63+
list = list | log_hash
64+
end
65+
# combined_branch[:log] = list
66+
# @full_list.push combined_branch
67+
# @full_list.push list
68+
list
69+
end
70+
71+
end
72+
end

0 commit comments

Comments
 (0)