diff --git a/.gitignore b/.gitignore index bfba4b6..1e3c167 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .DS_Store -**/.DS_Store \ No newline at end of file +**/.DS_Store +test_data/ \ No newline at end of file diff --git a/gistory.rb b/gistory.rb index b0b1c29..0190905 100644 --- a/gistory.rb +++ b/gistory.rb @@ -35,6 +35,8 @@ def ==(other) commits = Gistory::CommitParser.parse(repo, file, branch) Gistory::CommitParser.index_and_generate!(commits) +puts commits.inspect + abort %(Error: Couldn't find any commits. Are you sure this is a git repo and the file exists?) if commits.empty? diff --git a/lib/gistory/diff_to_html.rb b/lib/gistory/diff_to_html.rb index ae4a120..ed91355 100644 --- a/lib/gistory/diff_to_html.rb +++ b/lib/gistory/diff_to_html.rb @@ -19,33 +19,28 @@ def self.diff_to_html_binary(diff) end end - #TODO: REFACTOR THIS SHIT SOMETHING FURIOUS - def self.diff_to_html_textual(diff) - content_lines = diff.diff.split(/\n/)[2..-1] - line_offset = 1 + + + def self.lines_to_raw_changes(lines) changes = [] + + line_offset = 1 should_change = false - content_lines.each do |l| - #puts "line_offset: #{line_offset}, l: #{l}" + lines.each do |l| + #puts "line_offset: #{line_offset}, l: #{l}, changes: #{changes.inspect}" if l =~ /^(\@\@ \-(\d+),(\d+) \+(\d+),(\d+) \@\@)/ line_offset = $4.to_i == 0 ? 1 : $4.to_i else if l == '\ No newline at end of file' - #logic here needs checking and docs - if changes.length >= 2 && changes.last[:mode] == :add && changes[-2][:mode] == :remove && changes.last[:times] == 1 && changes[-2][:times] == 1 - changes.pop - changes.pop - end - + changes.last[:nl_notice] = true if !changes.empty? line_offset -= 1 elsif l =~ /^\+/ - changes << { :start => line_offset, :lines => "", :times => 0, :mode => :add } if should_change or changes.empty? or changes.last[:mode] != :add + changes << { :start => line_offset, :lines => [], :times => 0, :mode => :add, :nl_notice => false } if should_change or changes.empty? or changes.last[:mode] != :add should_change = false changes.last[:times] += 1 - lt = h(l[1..-1]).gsub(/ /, "  ") - changes.last[:lines] << "
#{lt == '' ? " " : lt}
" + changes.last[:lines] << l[1..-1] elsif l =~ /^-/ - changes << { :start => line_offset, :times => 0, :mode => :remove } if should_change or changes.empty? or changes.last[:mode] != :remove + changes << { :start => line_offset, :times => 0, :mode => :remove, :nl_notice => false } if should_change or changes.empty? or changes.last[:mode] != :remove should_change = false changes.last[:times] += 1 line_offset -= 1 @@ -55,8 +50,61 @@ def self.diff_to_html_textual(diff) line_offset += 1 end end - #puts changes.inspect + changes + end + + def self.process_newline_warnings_on_changes(changes) + return changes if changes.length <= 1 || !changes.detect { |change| change[:nl_notice] } #Remove edge cases + + puts "Reached processing stage of process_newline_warnings_on_changes: #{changes.inspect}" + if changes[-2][:mode] == :remove && changes[-1][:mode] == :add + if changes[-2][:nl_notice] && changes[-1][:nl_notice] + if changes[-2][:times] > 1 && changes[-1][:lines].length == 1 && changes[-2][:lines].first == changes[-1][:lines].first #3rd condition here is invalid + changes[-2][:start] += 1 #Handles removing and adding where there's no end newline on both + changes.pop + end + end + end + puts "Finished processing stage: #{changes.inspect}" + + changes.each { |change| change.delete :nl_notice } + +=begin + This line will be in the diff in the following circumstances: + * Adding a newline to the last line in the file #No action required + * Removing the newline from the last line in a file + * Adding multiple lines, where the last line did not have a new line and now does + * Adding multiple lines, where the last line did not have a new line and still doesn't + * Removing multiple lines, where the last line did not have a new line and now does + * Removing multiple lines, where the last line did not have a new line and still doesn't + * Removing and adding multiple lines, where the last line never has a \n #You'll see 2 \ lines, one at the end of the remove and one at the end of the add +=end + + changes + end + + + + def self.changes_to_html(changes) + changes.select { |change| change.key?(:lines) }.each do |change| + change[:lines] = change[:lines].map do |line| + line_space_fixed = h(line).gsub(/ /, "  ") + "
#{line_space_fixed == '' ? " " : line_space_fixed}
" + end.join + end + + changes + end + + #TODO: REFACTOR THIS SHIT SOMETHING FURIOUS + def self.diff_to_html_textual(diff) + content_lines = diff.diff.split(/\n/)[2..-1] + + changes = self.lines_to_raw_changes(content_lines) + changes = self.process_newline_warnings_on_changes(changes) + changes = self.changes_to_html(changes) + if diff.new_file { :type => 'new', :message => "Created file #{h diff.b_path}", :content => changes } elsif diff.deleted_file diff --git a/public/show_commit.js b/public/show_commit.js index e2a58a3..bc289be 100644 --- a/public/show_commit.js +++ b/public/show_commit.js @@ -19,7 +19,8 @@ function addHunk(startIndex, lines) { insertPoint.insert({ 'after': wrapper }); - alert(wrapper.viewportOffset().top + ", " + blindDistance(wrapper) + ", " + (wrapper.viewportOffset().top + blindDistance(wrapper)) + ", " + document.viewport.getHeight()); + console.log(wrapper); + console.log(wrapper.viewportOffset().top + ", " + blindDistance(wrapper) + ", " + (wrapper.viewportOffset().top + blindDistance(wrapper)) + ", " + document.viewport.getHeight()); //TODO: bug here if adding a few lines at end of file, should issue a scroll down here but doesn't if((wrapper.viewportOffset().top + blindDistance(wrapper)) > document.viewport.getHeight()) { diff --git a/views/commits.erb b/views/commits.erb index 7725c72..94af349 100644 --- a/views/commits.erb +++ b/views/commits.erb @@ -1,3 +1,4 @@ <% @commits.each_with_index do |commit_diff, commit_index| %> + <% puts commit_diff.inspect %> <%= erb :_commit, :layout => false, :locals => { :commit_diff => commit_diff, :commit_index => commit_index } %> <% end %> \ No newline at end of file