Class: GitHelpers::GitDiffHighlight

Inherits:
GitDiff
  • Object
show all
Defined in:
lib/git_helpers/diff.rb

Overview

stolen from diff-highlight git contrib script

Constant Summary

Constants inherited from GitDiff

GitHelpers::GitDiff::NoNewLine

Instance Attribute Summary

Attributes inherited from GitDiff

#output

Instance Method Summary collapse

Methods inherited from GitDiff

#change_mode, #detect_delete, #detect_diff_header, #detect_end_diff_header, #detect_end_hunk, #detect_filename, #detect_index, #detect_new_commit, #detect_new_diff_header, #detect_new_hunk, #detect_new_submodule_header, #detect_newfile, #detect_perm, #detect_rename_copy, #each, #end_commit, #end_diff_header, #end_meta, #end_submodule, #end_submodule_header, #get_file_name, #handle_commit, #handle_diff_header, #handle_hunk, #handle_meta, #handle_submodule, #handle_submodule_header, #initialize, #new_commit, #new_diff_header, #new_meta, #new_submodule, #new_submodule_header, #next_mode, output, #output_line, #output_lines, #parse, #parse_hunk_header, #parse_line, #prepare_new_line, #reparse, #submodule_line, #update_mode

Constructor Details

This class inherits a constructor from GitHelpers::GitDiff

Instance Method Details

#end_hunkObject



426
427
428
429
# File 'lib/git_helpers/diff.rb', line 426

def end_hunk
	super
	show_hunk
end

#handle_lineObject



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/git_helpers/diff.rb', line 464

def handle_line
	if @mode == :hunk && @hunk[:n]==2
		linemode=@line[0]
		case linemode
		when "-"
			@accumulator[0] << @orig_line
		when "+"
			@accumulator[1] << @orig_line
		else
			show_hunk
			@accumulator=[[],[]]
			output_line @orig_line
		end
	else
		output_line @orig_line
	end
end

#highlight_pair(old, new) ⇒ Object



431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/git_helpers/diff.rb', line 431

def highlight_pair(old,new)
	oldc=SimpleColor.color_entities(old).each_with_index
	newc=SimpleColor.color_entities(new).each_with_index
	seen_pm=false
	#find common prefix
	loop do
		a=oldc.grep {|c| ! SimpleColor.color?(c)}
		b=newc.grep {|c| ! SimpleColor.color?(c)}
		if !seen_pm and a=="-" and b=="+"
			seen_pm=true
		elsif a==b
		else
			last
		end
	#rescue StopIteration
	end
end

#new_hunkObject



422
423
424
425
# File 'lib/git_helpers/diff.rb', line 422

def new_hunk
	super
	@accumulator=[[],[]]
end

#show_hunkObject



449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/git_helpers/diff.rb', line 449

def show_hunk
	old,new=@accumulator
	if old.length != new.length
		output_lines(old+new)
	else
		newhunk=[]
		(0...old.length).each do |i|
			oldi,newi=highlight_pair(old[i],new[i])
			output_line oldi
			newhunk << newi
		end
		output_lines(newhunk)
	end
end