Skip to content

Commit

Permalink
better dry
Browse files Browse the repository at this point in the history
  • Loading branch information
jelaniwoods committed May 22, 2020
1 parent ab4bce6 commit b453cdc
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 6 deletions.
3 changes: 3 additions & 0 deletions lib/views/graph.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<%= @full_list.to_json %>

<hr>
<h2>
<%= @full_list.last %>
</h2>

<%# need list of Head/branch
for each commit see if it has a HEAD if so, put branch name else none
Expand Down
11 changes: 5 additions & 6 deletions lib/web_git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

module WebGit
require "web_git/diff"
require "web_git/graph"
require "web_git/string"
require "sinatra"
require "date"
Expand Down Expand Up @@ -37,16 +38,12 @@ class Server < Sinatra::Base
branch = { branch: branch_name }
p g.checkout(branch_name)
list = []
# g.branch.name
# p "—————————"

g.log.sort_by(&:date).each do |log|
commit = log.sha.slice(0..7)
list.push commit
end
# p g.branch.name
# p "-----"
# p list
# list.join("<br>")

p "—————————"
p branch
branch[:log] = list
Expand All @@ -73,6 +70,8 @@ class Server < Sinatra::Base
full_list.push list
# full_list.to_json
@full_list = full_list
# graph = WebGit::Graph.new(g)
# @full_list = graph.to_json
# mmm = []
# full_list.last.each do |commit|
# mmm.push commit + " — " + g.gcommit(commit).message
Expand Down
72 changes: 72 additions & 0 deletions lib/web_git/graph.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
module WebGit
require "git"
class Graph
def initialize(git)
@git = git
@full_list = []
end

def to_json

had_changes = has_untracked_changes?
if had_changes
temporarily_stash_changes
end

list = draw_graph

if had_changes
stash_pop
end

@full_list.push list
end


def has_untracked_changes?
@git.diff.size > 0
end

def temporarily_stash_changes
@git.add(all: true)
stash_count = Git::Stashes.new(@git).count
Git::Stash.new(@git, "Temporary Stash #{stash_count}")
end

def stash_pop
stashes = Git::Stashes.new(@git)
stashes.apply(0)
end

def draw_graph
branches = @git.branches.local.map(&:name)
branches.each do |branch_name|
branch = { branch: branch_name }
list = []
@git.log.sort_by(&:date).each do |log|
commit = log.sha.slice(0..7)
list.push commit
end

branch[:log] = list
branch[:head] = list.last
@full_list.push branch
end
lists = @full_list.map{|l| l[:log] }
combined_branch = { branch: "ALL", head: "_" }
@full_list.push combined_branch
# combined_branch = { branch: "ALL", head: "_" }
list = []
(lists.count - 1).times do |i|
log_hash = lists[i]

list = list | log_hash
end
# combined_branch[:log] = list
# @full_list.push combined_branch
# @full_list.push list
list
end

end
end

0 comments on commit b453cdc

Please sign in to comment.