Class: WebGit::Graph

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::DateHelper
Defined in:
lib/web_git/graph.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(git) ⇒ Graph

Returns a new instance of Graph.



11
12
13
14
15
# File 'lib/web_git/graph.rb', line 11

def initialize(git)
  @git = git
  @full_list = []
  @heads = {}
end

Instance Attribute Details

#headsObject

Returns the value of attribute heads.



9
10
11
# File 'lib/web_git/graph.rb', line 9

def heads
  @heads
end

Class Method Details

.project_rootObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/web_git/graph.rb', line 32

def self.project_root
  if defined?(Rails) && Rails.respond_to?("root")
    return Rails.root
  end

  if defined?(Bundler)
    return Bundler.root
  end

  Dir.pwd
end

Instance Method Details

#build_array_of_commit_hashesObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/web_git/graph.rb', line 99

def build_array_of_commit_hashes
  log_commits = []
  @git.log.sort_by(&:date).each do |git_commit_object|
    commit = {}
    commit[:sha] = git_commit_object.sha.slice(0..7)
    commit[:date] = git_commit_object.date
    commit[:formatted_date] = time_ago_in_words(git_commit_object.date)
    commit[:message] = git_commit_object.message
    commit[:author] = git_commit_object.author.name
    commit[:heads] = []
    log_commits.push commit
  end
  log_commits
end

#cli_graphObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/web_git/graph.rb', line 44

def cli_graph
  Dir.chdir(Graph.project_root) do
    @cli_graph = `git log --oneline --decorate --graph --all --color`
    all_commits = `git log  --all --format=format:%H`.split("\n").map{|a| a.slice(0,7)}

    @cli_graph = Ansispan.convert(@cli_graph)
    all_commits.each do |sha|
      sha_button = "<span class=\"commit\"><button class=\"btn btn-link sha\">#{sha}</button></span>"
      @cli_graph.gsub!(sha, sha_button)
    end
  end
  @cli_graph
end

#draw_graphObject



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
# File 'lib/web_git/graph.rb', line 73

def draw_graph
  starting_branch = @git.current_branch
  branches = @git.branches.local.map(&:name)
  branches.each do |branch_name|
    branch = { branch: branch_name }
    @git.checkout(branch_name)
    log_commits = build_array_of_commit_hashes
    branch[:log] = log_commits
    branch[:head] = log_commits.last[:sha]
    @full_list.push branch
  end
  @git.checkout(starting_branch)

  @full_list.each do |branch_hash|
    head_sha = branch_hash[:head]
    branch_name = branch_hash[:branch]

    if @heads[head_sha].nil?
      @heads[head_sha] = [branch_name]
    else
      @heads[head_sha].push branch_name
    end
  end

end

#has_untracked_changes?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/web_git/graph.rb', line 58

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

#stash_popObject



68
69
70
71
# File 'lib/web_git/graph.rb', line 68

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

#temporarily_stash_changesObject



62
63
64
65
66
# File 'lib/web_git/graph.rb', line 62

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

#to_hashObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/web_git/graph.rb', line 17

def to_hash
  
  has_changes = has_untracked_changes?
  if has_changes
    temporarily_stash_changes
  end

  draw_graph

  if has_changes
    stash_pop
  end
  @full_list
end