Class: WebGit::Diff
- Inherits:
-
Object
- Object
- WebGit::Diff
- Defined in:
- lib/web_git/diff.rb
Class Method Summary collapse
- .diff_to_html(diff) ⇒ Object
- .file_diffs(diff) ⇒ Object
- .get_diff ⇒ Object
- .get_each_left(diff) ⇒ Object
- .get_each_right(diff) ⇒ Object
- .get_file_names(commit) ⇒ Object
- .get_last_commit_hash ⇒ Object
- .get_last_diff ⇒ Object
- .get_last_left(diff) ⇒ Object
- .get_last_right(diff) ⇒ Object
- .last_to_html(diff) ⇒ Object
- .match_other_files(line, file, filenames) ⇒ Object
Class Method Details
.diff_to_html(diff) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/web_git/diff.rb', line 189 def self.diff_to_html(diff) left_hash = get_each_left(diff) right_hash = get_each_right(diff) html_output = '<div style="overflow-y: scroll;height:400px">' html_output += '<style>' html_output += Diffy::CSS html_output += '</style>' html_output += '<div class="row mb-3">' html_output += '<div class="col-md-12 offset" style="overflow-y: scroll;">' left_hash.keys.each do |file| html_output += '<div class="row text-center"> <div class="col-12"> <h4>' html_output+= file.to_s html_output += '</h4> </div> </div> <div class="row mb-4"> <div class="col-6">' html_output += Diffy::SplitDiff.new( left_hash[file], right_hash[file], :format => :html ).left html_output += '</div> <div class="col-6">' html_output += Diffy::SplitDiff.new( left_hash[file], right_hash[file], :format => :html ).right html_output += '</div></div>' end html_output += "</div>" html_output += "</div>" html_output += "</div>" html_output end |
.file_diffs(diff) ⇒ Object
81 82 83 |
# File 'lib/web_git/diff.rb', line 81 def self.file_diffs(diff) diff.scan(/diff --git.*?(?=diff --git|\z)/m) end |
.get_diff ⇒ Object
3 4 5 6 7 |
# File 'lib/web_git/diff.rb', line 3 def self.get_diff Dir.chdir(Rails.root) do `git diff` end end |
.get_each_left(diff) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/web_git/diff.rb', line 9 def self.get_each_left(diff) filenames = get_file_names("") files = file_diffs(diff) lefts = {} files.each_with_index do |file, i| file_content = "" lines = file.split("\n").drop(4) start_line = 0 current_line_index = 0 line_number = start_line + current_line_index lines.each do |line| if !line.match?(/@@ ([-]\d+,\d+\s[+]\d+,\d+) @@/) if line.first != "+" file_content += "#{line_number}| " + line + "\n" line_number += 1 end else current_line_index = 0 # The line numbers in the output of a git diff match this regex numbers = line.scan(/@@ ([-]\d+,\d+\s[+]\d+,\d+) @@/).map(&:join) # If left, starting line number is the first one in a split Array start_line = numbers.first.split(" ").first. split(",").first.to_i.abs line_number = start_line + current_line_index file_content += "\n" end end lefts[filenames[i]] = file_content.chomp "\n" end lefts end |
.get_each_right(diff) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/web_git/diff.rb', line 41 def self.get_each_right(diff) filenames = get_file_names("") files = file_diffs(diff) rights = {} files.each_with_index do |file, i| file_content = "" lines = file.split("\n").drop(4) start_line = 0 current_line_index = 0 line_number = start_line + current_line_index lines.each do |line| # The line numbers in the output of a git diff match this regex # @@ -61,18 +61,15 @@ if !line.match?(/@@ ([-]\d+,\d+\s[+]\d+,\d+) @@/) if line.first != "-" file_content += "#{line_number}| " + line + "\n" line_number += 1 end else current_line_index = 0 numbers = line.scan(/@@ ([-]\d+,\d+\s[+]\d+,\d+) @@/).map(&:join) # If right, start line is the second in a split Array start_line = numbers.first.split(" ").second. split(",").first.to_i.abs line_number = start_line + current_line_index file_content += "\n" end end rights[filenames[i]] = file_content.chomp "\n" end rights end |
.get_file_names(commit) ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/web_git/diff.rb', line 99 def self.get_file_names(commit) Dir.chdir(Rails.root) do if commit.blank? filenames = `git diff --name-only` else filenames = `git diff-tree --no-commit-id --name-only -r #{commit}` end filenames.split("\n") end end |
.get_last_commit_hash ⇒ Object
74 75 76 77 78 79 |
# File 'lib/web_git/diff.rb', line 74 def self.get_last_commit_hash Dir.chdir(Rails.root) do log = `git log -1 --oneline` log.split.first end end |
.get_last_diff ⇒ Object
110 111 112 113 114 |
# File 'lib/web_git/diff.rb', line 110 def self.get_last_diff Dir.chdir(Rails.root) do `git diff -M HEAD~1` end end |
.get_last_left(diff) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/web_git/diff.rb', line 116 def self.get_last_left(diff) filenames = get_file_names(get_last_commit_hash) files = file_diffs(diff) ones = {} files.each_with_index do |file, i| file_content = "" lines = file.split("\n").drop(4) lines.each do |line| # The line numbers in the output of a git diff match this regex # @@ -61,18 +61,15 @@ if !line.match?(/@@ ([-]\d+,\d+\s[+]\d+,\d+) @@/) if line.first != "+" line.slice!(0) file_content += line + "\n" end else file_content += "\n" end end ones[filenames[i]] = file_content.chomp "\n" end ones end |
.get_last_right(diff) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/web_git/diff.rb', line 140 def self.get_last_right(diff) filenames = get_file_names(get_last_commit_hash) files = file_diffs(diff) ones = {} files.each_with_index do |file, i| file_content = "" lines = file.split("\n").drop(4) lines.each do |line| if !line.match?(/@@ ([-]\d+,\d+\s[+]\d+,\d+) @@/) if line.first != "+" elsif line.first == "+" line.slice!(0) file_content += line + "\n" end else file_content += "\n" end end ones[filenames[i]] = file_content.chomp "\n" end ones end |
.last_to_html(diff) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/web_git/diff.rb', line 163 def self.last_to_html(diff) left_hash = get_last_left(diff) right_hash = get_last_right(diff) html_output = '<link rel="stylesheet"' + 'href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/' + 'bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/' + '1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">' html_output += '<style>' html_output += Diffy::CSS html_output += '</style>' html_output += '<div class="card">' left_hash.keys.each do |file| html_output += '<div class="file mb-4 p-3">' html_output += '<h4>' + file + '</h4>' html_output += Diffy::Diff.new( left_hash[file], right_hash[file], :include_plus_and_minus_in_html => true ).to_s(:html) html_output += '</div>' end html_output += '</div>' html_output end |
.match_other_files(line, file, filenames) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/web_git/diff.rb', line 85 def self.match_other_files(line, file, filenames) filenames.each do |other_file| if file != other_file # It looks like: # --- a/<path-to-file> # +++ b/<path-to-file> if line.include?('diff --git a/' + other_file + ' b/' + other_file) return true end end end false end |