Class: Diff
- Inherits:
-
Object
- Object
- 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
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 232 233 |
# File 'lib/web_git/diff.rb', line 191 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
83 84 85 |
# File 'lib/web_git/diff.rb', line 83 def self.file_diffs(diff) diff.scan(/diff --git.*?(?=diff --git|\z)/m) end |
.get_diff ⇒ Object
6 7 8 9 10 |
# File 'lib/web_git/diff.rb', line 6 def self.get_diff # Dir.chdir(Rails.root) do `git diff` # end end |
.get_each_left(diff) ⇒ Object
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 40 41 42 |
# File 'lib/web_git/diff.rb', line 12 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[0] != "+" 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[0].split(" ")[0]. split(",")[0].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
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 73 74 75 |
# File 'lib/web_git/diff.rb', line 44 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[0] != "-" 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[0].split(" ")[1]. split(",")[0].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
101 102 103 104 105 106 107 108 109 |
# File 'lib/web_git/diff.rb', line 101 def self.get_file_names(commit) git = Git.open(Dir.pwd) if commit.empty? filenames = git.status.changed.keys else filenames = git.diff(commit, "HEAD").map(&:path) end filenames end |
.get_last_commit_hash ⇒ Object
77 78 79 80 81 |
# File 'lib/web_git/diff.rb', line 77 def self.get_last_commit_hash working_dir = Dir.pwd git = Git.open(working_dir) git.log.first.sha.slice(0, 7) end |
.get_last_diff ⇒ Object
111 112 113 114 115 |
# File 'lib/web_git/diff.rb', line 111 def self.get_last_diff # Dir.chdir(Rails.root) do `git diff -M HEAD~1` # end end |
.get_last_left(diff) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/web_git/diff.rb', line 117 def self.get_last_left(diff) filenames = get_file_names("HEAD~1") 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[0] != "+" 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
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/web_git/diff.rb', line 141 def self.get_last_right(diff) filenames = get_file_names("HEAD~1") 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[0] != "+" elsif line[0] == "+" 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
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/web_git/diff.rb', line 164 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" style="overflow-y: scroll;max-height:400px">' left_hash.keys.each do |file| html_output += '<div class="file mb-4 p-1">' html_output += '<h4>' + file + '</h4>' html_output += Diffy::Diff.new( left_hash[file], right_hash[file], :include_plus_and_minus_in_html => true, :allow_empty_diff => false ).to_s(:html) html_output += '</div>' end html_output += '</div>' html_output end |
.match_other_files(line, file, filenames) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/web_git/diff.rb', line 87 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 |