Module: Visualisation

Defined in:
lib/application/visualisation.rb

Class Method Summary collapse

Class Method Details

.branch_author_stats(branch) ⇒ Object



155
156
157
158
159
# File 'lib/application/visualisation.rb', line 155

def self.branch_author_stats(branch)
  `git log master..#{branch} --pretty=format:%an,%ae \
    | awk '{ ++c[$0]; } END { for(cc in c) printf "%5d,%s\\n",c[cc],cc; }'\
    | sort -r`.split(/\n/).each { |c| c.strip! }
end

.branch_contains_commit(branch, commit_sha) ⇒ Object



37
38
39
# File 'lib/application/visualisation.rb', line 37

def self.branch_contains_commit(branch, commit_sha)
  `git branch --contains #{commit_sha}`.split("\n").each {|b| b.gsub!(/[*]?\s/, '')}.include?(branch)
end

.branch_diff_commit_files(commit_sha = nil) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/application/visualisation.rb', line 141

def self.branch_diff_commit_files(commit_sha = nil)
  merge_base_commit = `git merge-base master #{commit_sha}`.gsub("/\n/", '').strip!
  diff_stats = `git diff --numstat #{merge_base_commit} #{commit_sha}`.split(/\n/)
  files = {}
  additions = deletions = 0
  diff_stats.each do |line|
    cols = line.split
    additions += cols[0].to_i 
    deletions += cols[1].to_i 
    files.merge!(cols[2].to_sym => { :add => additions, :del => deletions })
  end
  files.merge!(:total => { :add => additions, :del => deletions })
end

.branch_diff_number_commits(branch) ⇒ Object



58
59
60
# File 'lib/application/visualisation.rb', line 58

def self.branch_diff_number_commits(branch)
  `git cherry master #{branch}`.split("\n").size
end

.branch_diff_size(branch) ⇒ Object

printout merge commits between base and topic branch ‘git log #branch #base –oneline –date-order –merges –reverse -1`



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/application/visualisation.rb', line 92

def self.branch_diff_size(branch)
  merge_base_commit = `git merge-base master #{branch}`.gsub("/\n/", '').strip!
  raw_diff_stats = `git diff --numstat #{merge_base_commit} #{branch}`
  diff_stats = raw_diff_stats.split(/\n/)
  additions = deletions = 0
  diff_stats.each do |line|
    cols = line.split
    additions += cols[0].to_i 
    deletions += cols[1].to_i 
  end

  return additions, deletions
end

.branch_merged_with_base?(base, branch, remotes) ⇒ Boolean



81
82
83
84
85
86
87
# File 'lib/application/visualisation.rb', line 81

def self.branch_merged_with_base?(base, branch, remotes)
  if remotes 
    `git branch -a --merged #{base} #{branch}`.length > 0
  else
    `git branch --merged #{base} #{branch}`.length > 0
  end
end

.branchesObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/application/visualisation.rb', line 5

def self.branches
  branches_arr = [] 
  total_additions = total_deletions = 0
  remotes_arr = Visualisation.remotes
  Visualisation.branches_with_remotes.each do |branch|
    puts "#{branch}"
    diff = Visualisation.branch_diff_size(branch)
    head_commit = Visualisation.head_commit_sha(branch)
    merged_with_master = Visualisation.branch_contains_commit("master", head_commit)
    total_additions += diff.first
    total_deletions += diff.last
    remote = remotes_arr.include?(branch)
    branches_arr << {:name => branch, :diff => {:add => diff.first, :del => diff.last}, 
                  :merged_with_master => merged_with_master, :hidden => false, :remote => remote}
  end
  result = {:branches => branches_arr, :diff => {:add => total_additions, :del => total_deletions}}
  result
end

.branches_containing_commit(commit_sha, remotes = true) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/application/visualisation.rb', line 41

def self.branches_containing_commit(commit_sha, remotes = true)
  if remotes 
    return `git branch -a --contains #{commit_sha}`.split("\n").each { |b| b.gsub!(/[*]?\s/, '') and b.gsub!(/remotes\//, '') }
  else 
    return `git branch --contains #{commit_sha}`.split("\n").each { |b| b.gsub!(/[*]?\s/, '') }
  end
end

.branches_excluding_commit(commit_sha, remotes = true) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/application/visualisation.rb', line 49

def self.branches_excluding_commit(commit_sha, remotes = true)
  if remotes
    return branches_with_remotes - `git branch --contains #{commit_sha}`.split("\n").each { |b| b.gsub!(/[*]?\s/, '') }
  else
    # TODO fix this
    # return branches - `git branch --contains #{commit_sha}`.split("\n").each { |b| b.gsub!(/[*]?\s/, '') }
  end
end

.branches_with_remotesObject



24
25
26
27
# File 'lib/application/visualisation.rb', line 24

def self.branches_with_remotes
  `git branch -a`.split("\n").each { |b| b.gsub!(/[*]?\s/, '') and b.gsub!(/remotes\//, '') }
    .delete_if { |b| b.include?('->') }
end

.commit_diff_stats(commit_sha) ⇒ Object



136
137
138
# File 'lib/application/visualisation.rb', line 136

def self.commit_diff_stats(commit_sha)
  `git show #{commit_sha} --numstat --no-merges --pretty="%n"`.strip!
end

.commits_for_branch(branch_name) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/application/visualisation.rb', line 106

def self.commits_for_branch(branch_name)
  commits = []
  raw_log = `git log  master..#{branch_name} --max-count 15 --date=short --pretty="%H, %an, %ad, %s"`
  commit_lines = CSV.parse(raw_log)
  i = 1
  last_date = nil
  commit_lines.each_with_index do |commit, id|
    sha1 = commit[0]
    author = commit[1].strip!
    commit_date = Date.parse(commit[2].strip!)
    message = commit.slice(3..commit.length-1).join(",").strip!
    if !last_date.nil? && 
      (commit_date.year == last_date.year && commit_date.yday == last_date.yday)
      i += 1
    else
      i = 1
    end
    last_date = commit_date.to_date
    commit_stats = {:id => id, :date => last_date, :num => i, :sha => sha1, 
                    :author => author, :message => message}
    commits << commit_stats
    puts commits
  end
  commits
end

.head_commit_sha(branch) ⇒ Object



33
34
35
# File 'lib/application/visualisation.rb', line 33

def self.head_commit_sha(branch)
  `git rev-parse #{branch}`
end

.merge_base_file_stats(branch_name) ⇒ Object



132
133
134
# File 'lib/application/visualisation.rb', line 132

def self.merge_base_file_stats(branch_name)
  `git log master..#{branch_name} --numstat --no-merges --format="%n"`
end

.remotesObject



29
30
31
# File 'lib/application/visualisation.rb', line 29

def self.remotes
  `git branch -r`.split("\n").each { |b| b.gsub!(/[*]?\s/, '') }
end

.repo_branches_merged(show_remotes = true) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/application/visualisation.rb', line 62

def self.repo_branches_merged(show_remotes = true)
  merged_branches = {}
  compare_branches = branches_with_remotes
  compare_branches.each do |b1|
    b1_merges = {}
    compare_branches.each do |b2|
      next if b1 == b2 || b2.split("/").last == b1 || 
          (merged_branches.has_key?(b2.to_sym) && merged_branches[b2.to_sym].has_key?(b1.to_sym))
      directions = {}
      puts "comparing #{b1} with #{b2}"
      directions.merge!(:left => true) if branch_merged_with_base?(b1, b2, remotes)
      directions.merge!(:right => true) if right = branch_merged_with_base?(b2, b1, remotes)
      b1_merges.merge!(b2.to_sym => directions)
    end
    merged_branches.merge!(b1.to_sym => b1_merges)
  end
  merged_branches
end