Class: GitHelpers::GitFancyDiff

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

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_hunk, #end_meta, #end_submodule, #get_file_name, #handle_commit, #handle_diff_header, #handle_hunk, #handle_meta, #handle_submodule, #handle_submodule_header, #new_hunk, #new_meta, #new_submodule, #next_mode, output, #output_line, #output_lines, #parse, #parse_hunk_header, #parse_line, #prepare_new_line, #reparse, #submodule_line, #update_mode

Constructor Details

#initialize(*args, **kw, &b) ⇒ GitFancyDiff

Returns a new instance of GitFancyDiff.



485
486
487
488
489
490
491
492
# File 'lib/git_helpers/diff.rb', line 485

def initialize(*args,**kw,&b)
	super
	#when run inside a pager I get one more column so the line overflow
	#I don't know why
	cols=`tput cols`.to_i
	cols==0 && cols=80 #if TERM is not defined `tput cols` returns ''
	@cols=cols-1
end

Instance Method Details

#binary_file_differObject



628
629
630
631
# File 'lib/git_helpers/diff.rb', line 628

def binary_file_differ
	@file and (@file[:mode]==:new && @line =~ %r{^Binary files /dev/null and ./#{@file[:name]} differ$} or
					 	 @file[:mode]==:delete && @line =~ %r{^Binary files ./#{@file[:old_name]} and /dev/null differ$})
end

#clean_hunk_colObject



602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# File 'lib/git_helpers/diff.rb', line 602

def clean_hunk_col
	if @opts[:color] && @mode==:hunk && !@start_mode && @hunk[:n]==2
		bcolor,ecolor,line=SimpleColor.current_colors(@orig_line)
		m=line.scrub.match(/^([+-])?(.*)/)
		mode=m[1]
		cline=m[2]
		if mode && cline !~ /[^[:space:]]/ #detect blank line
			output_line SimpleColor.color(bcolor.to_s + (cline.empty? ? " ": cline)+ecolor.to_s,:inverse)
		else
			cline.sub!(/^\s/,'') unless mode #strip one blank character
			output_line bcolor.to_s+cline+ecolor.to_s
		end
		true
	end
end

#diff_header_summaryObject



532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
# File 'lib/git_helpers/diff.rb', line 532

def diff_header_summary
	r=case @file[:mode]
		when :modify
			"modified: #{@file[:name]}"
		when :rewrite
			"rewrote: #{@file[:name]} (dissimilarity: #{@file[:dissimilarity]})"
		when :new
			"added#{perm_mode(@file[:new_perm])}: #{@file[:name]}"
		when :delete
			"deleted#{perm_mode(@file[:old_perm])}: #{@file[:old_name]}"
		when :rename
			"renamed: #{@file[:old_name]} to #{@file[:name]} (similarity: #{@file[:similarity]})"
		when :copy
			"copied: #{@file[:old_name]} to #{@file[:name]} (similarity: #{@file[:similarity]})"
		end
	r<<" [#{short_perm_mode(@file[:old_perm],prefix:'-')}#{short_perm_mode(@file[:new_perm])}]" if @file[:old_perm] && @file[:new_perm]
	r
end

#end_commitObject



597
598
599
600
# File 'lib/git_helpers/diff.rb', line 597

def end_commit
	super
	output_line meta_colorize(hhline)
end

#end_diff_headerObject



564
565
566
567
568
# File 'lib/git_helpers/diff.rb', line 564

def end_diff_header
	super
	output_line meta_colorize(diff_header_summary)
	output_line meta_colorize(hline)
end

#end_submodule_headerObject



583
584
585
586
587
# File 'lib/git_helpers/diff.rb', line 583

def end_submodule_header
	super
	output_line meta_colorize(submodule_header_summary)
	output_line meta_colorize(hline)
end

#handle_lineObject



633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# File 'lib/git_helpers/diff.rb', line 633

def handle_line
	super
	#:diff_header and submodule_header are handled at end_*
	case @mode
	when :meta
		if binary_file_differ
		else output_line @orig_line
		end
	when :hunk
		if hunk_header
		elsif nonewline_clean
		elsif clean_hunk_col
		else output_line @orig_line
		end
	when :submodule,:commit
		output_line @orig_line
	end
end

#hhlineObject



497
498
499
500
501
# File 'lib/git_helpers/diff.rb', line 497

def hhline
	#'⬛'*@cols
	#"━"*@cols
	""*@cols
end

#hlineObject



494
495
496
# File 'lib/git_helpers/diff.rb', line 494

def hline
	''*@cols
end

#hunk_headerObject



618
619
620
621
622
623
624
625
626
# File 'lib/git_helpers/diff.rb', line 618

def hunk_header
	if @mode==:hunk && @start_mode
		if @hunk[:lines][0][1] && @hunk[:lines][0][1] != 0
			header="#{@file[:name]}:#{@hunk[:lines][0][1]}"
			output_line @orig_line.sub(/(@@+\s)(.*)(\s@@+)/,"\\1#{header}\\3")
		end
		true
	end
end

#meta_colorize(l) ⇒ Object



551
552
553
554
555
556
557
# File 'lib/git_helpers/diff.rb', line 551

def meta_colorize(l)
	if @opts[:color]
		l.color(*@colors[:meta])
	else
		l
	end
end

#new_commitObject



593
594
595
596
# File 'lib/git_helpers/diff.rb', line 593

def new_commit
	super
	output_line meta_colorize(hhline)
end

#new_diff_headerObject



559
560
561
562
# File 'lib/git_helpers/diff.rb', line 559

def new_diff_header
	super
	output_line meta_colorize(hline)
end

#new_submodule_headerObject



578
579
580
581
# File 'lib/git_helpers/diff.rb', line 578

def new_submodule_header
	super
	output_line meta_colorize(hline)
end

#nonewline_cleanObject



589
590
591
# File 'lib/git_helpers/diff.rb', line 589

def nonewline_clean
		@mode==:hunk && @file && (@file[:perm]=="120000" or @file[:old_perm]=="120000" or @file[:new_perm]=="120000") && @line==NoNewLine
end

#perm_mode(m, prefix: ' ') ⇒ Object



517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/git_helpers/diff.rb', line 517

def perm_mode(m, prefix: ' ')
	case m
	when "040000"
		prefix+"directory"
	when "100644"
		"" #file
	when "100755"
		prefix+"executable"
	when "120000"
		prefix+"symlink"
	when "160000"
		prefix+"gitlink"
	end
end

#short_perm_mode(m, prefix: '+') ⇒ Object



503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/git_helpers/diff.rb', line 503

def short_perm_mode(m, prefix: '+')
	case m
	when "040000"
		prefix+"d" #directory
	when "100644"
		"" #file
	when "100755"
		prefix+"x" #executable
	when "120000"
		prefix+"l" #symlink
	when "160000"
		prefix+"g" #gitlink
	end
end

#submodule_header_summaryObject



570
571
572
573
574
575
576
# File 'lib/git_helpers/diff.rb', line 570

def submodule_header_summary
	r="Submodule #{@submodule[:name]}"
	extra=[@submodule[:modified] && "modified", @submodule[:untracked] && "untracked"].compact.join("+")
	r<<" [#{extra}]" unless extra.empty?
	r << " #{@submodule[:info]}" if @submodule[:info]
	r
end