From b453cdc9e748d796bf1cc72bebeec4cd69000bc2 Mon Sep 17 00:00:00 2001 From: Jelani Woods Date: Fri, 22 May 2020 18:38:45 -0500 Subject: [PATCH] better dry --- lib/views/graph.erb | 3 ++ lib/web_git.rb | 11 +++---- lib/web_git/graph.rb | 72 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 lib/web_git/graph.rb diff --git a/lib/views/graph.erb b/lib/views/graph.erb index c2202ab..5607c8d 100644 --- a/lib/views/graph.erb +++ b/lib/views/graph.erb @@ -1,6 +1,9 @@ <%= @full_list.to_json %>
+

+ <%= @full_list.last %> +

<%# need list of Head/branch for each commit see if it has a HEAD if so, put branch name else none diff --git a/lib/web_git.rb b/lib/web_git.rb index 418ad37..d9f75b1 100644 --- a/lib/web_git.rb +++ b/lib/web_git.rb @@ -3,6 +3,7 @@ module WebGit require "web_git/diff" + require "web_git/graph" require "web_git/string" require "sinatra" require "date" @@ -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("
") + p "—————————" p branch branch[:log] = list @@ -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 diff --git a/lib/web_git/graph.rb b/lib/web_git/graph.rb new file mode 100644 index 0000000..6d7b6a4 --- /dev/null +++ b/lib/web_git/graph.rb @@ -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