Skip to content

Commit

Permalink
WIP on 'no newline at end of file' message handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bloopletech committed Jan 14, 2010
1 parent a7efb8b commit 3652fd6
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,2 +1,3 @@
.DS_Store
**/.DS_Store
**/.DS_Store
test_data/
2 changes: 2 additions & 0 deletions gistory.rb
Expand Up @@ -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?

Expand Down
82 changes: 65 additions & 17 deletions lib/gistory/diff_to_html.rb
Expand Up @@ -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(/ /, " &nbsp;")
changes.last[:lines] << "<div>#{lt == '' ? "&nbsp;" : lt}</div>"
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
Expand All @@ -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(/ /, " &nbsp;")
"<div>#{line_space_fixed == '' ? "&nbsp;" : line_space_fixed}</div>"
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
Expand Down
3 changes: 2 additions & 1 deletion public/show_commit.js
Expand Up @@ -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())
{
Expand Down
1 change: 1 addition & 0 deletions 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 %>

0 comments on commit 3652fd6

Please sign in to comment.