545
## diff
546
547
proc clear_diff {} {
548
- global ui_diff current_diff_path ui_index ui_workdir
548
+ global ui_diff current_diff_path current_diff_header
549
+ global ui_index ui_workdir
550
551
$ui_diff conf -state normal
552
$ui_diff delete 0.0 end
553
$ui_diff conf -state disabled
554
555
set current_diff_path {}
556
+ set current_diff_header {}
557
558
$ui_index tag remove in_diff 0.0 end
559
$ui_workdir tag remove in_diff 0.0 end
601
global file_states file_lists
602
global is_3way_diff diff_active repo_config
603
global ui_diff ui_status_value ui_index ui_workdir
602
- global current_diff_path current_diff_side
604
+ global current_diff_path current_diff_side current_diff_header
605
606
if {$diff_active || ![lock_index read]} return
607
625
set diff_active 1
626
set current_diff_path $path
627
set current_diff_side $w
628
+ set current_diff_header {}
629
set ui_status_value "Loading diff of [escape_path $path]..."
630
631
# - Git won't give us the diff, there's nothing to compare to!
710
return
711
}
712
710
- fconfigure $fd -blocking 0 -translation auto
713
+ fconfigure $fd \
714
+ -blocking 0 \
715
+ -encoding binary \
716
+ -translation binary
717
fileevent $fd readable [list read_diff $fd]
718
}
719
720
proc read_diff {fd} {
715
- global ui_diff ui_status_value is_3way_diff diff_active
721
+ global ui_diff ui_status_value diff_active
722
+ global is_3way_diff current_diff_header
723
724
$ui_diff conf -state normal
725
while {[gets $fd line] >= 0} {
726
# -- Cleanup uninteresting diff header lines.
727
#
721
- if {[string match {diff --git *} $line]} continue
722
- if {[string match {diff --cc *} $line]} continue
723
- if {[string match {diff --combined *} $line]} continue
724
- if {[string match {--- *} $line]} continue
725
- if {[string match {+++ *} $line]} continue
728
+ if { [string match {diff --git *} $line]
729
+ || [string match {diff --cc *} $line]
730
+ || [string match {diff --combined *} $line]
731
+ || [string match {--- *} $line]
732
+ || [string match {+++ *} $line]} {
733
+ append current_diff_header $line "\n"
734
+ continue
735
+ }
736
+ if {[string match {index *} $line]} continue
737
if {$line eq {deleted file mode 120000}} {
738
set line "deleted symlink"
739
}
742
#
743
if {[string match {@@@ *} $line]} {set is_3way_diff 1}
744
734
- if {[string match {index *} $line]
735
- || [string match {mode *} $line]
745
+ if {[string match {mode *} $line]
746
|| [string match {new file *} $line]
747
|| [string match {deleted file *} $line]
748
|| [string match {Binary files * and * differ} $line]
809
}
810
}
811
812
+proc apply_hunk {x y} {
813
+ global current_diff_path current_diff_header current_diff_side
814
+ global ui_diff ui_index file_states
815
+
816
+ if {$current_diff_path eq {} || $current_diff_header eq {}} return
817
+ if {![lock_index apply_hunk]} return
818
+
819
+ set apply_cmd {git apply --cached --whitespace=nowarn}
820
+ set mi [lindex $file_states($current_diff_path) 0]
821
+ if {$current_diff_side eq $ui_index} {
822
+ set mode unstage
823
+ lappend apply_cmd --reverse
824
+ if {[string index $mi 0] ne {M}} {
825
+ unlock_index
826
+ return
827
+ }
828
+ } else {
829
+ set mode stage
830
+ if {[string index $mi 1] ne {M}} {
831
+ unlock_index
832
+ return
833
+ }
834
+ }
835
+
836
+ set s_lno [lindex [split [$ui_diff index @$x,$y] .] 0]
837
+ set s_lno [$ui_diff search -backwards -regexp ^@@ $s_lno.0 0.0]
838
+ if {$s_lno eq {}} {
839
+ unlock_index
840
+ return
841
+ }
842
+
843
+ set e_lno [$ui_diff search -forwards -regexp ^@@ "$s_lno + 1 lines" end]
844
+ if {$e_lno eq {}} {
845
+ set e_lno end
846
+ }
847
+
848
+ if {[catch {
849
+ set p [open "| $apply_cmd" w]
850
+ fconfigure $p -translation binary -encoding binary
851
+ puts -nonewline $p $current_diff_header
852
+ puts -nonewline $p [$ui_diff get $s_lno $e_lno]
853
+ close $p} err]} {
854
+ error_popup "Failed to $mode selected hunk.\n\n$err"
855
+ unlock_index
856
+ return
857
+ }
858
+
859
+ $ui_diff conf -state normal
860
+ $ui_diff delete $s_lno $e_lno
861
+ $ui_diff conf -state disabled
862
+
863
+ if {[$ui_diff get 1.0 end] eq "\n"} {
864
+ set o _
865
+ } else {
866
+ set o ?
867
+ }
868
+
869
+ if {$current_diff_side eq $ui_index} {
870
+ set mi ${o}M
871
+ } elseif {[string index $mi 0] eq {_}} {
872
+ set mi M$o
873
+ } else {
874
+ set mi ?$o
875
+ }
876
+ unlock_index
877
+ display_file $current_diff_path $mi
878
+ if {$o eq {_}} {
879
+ clear_diff
880
+ }
881
+}
882
+
883
######################################################################
884
##
885
## commit
4223
# -- Diff Header
4224
#
4225
set current_diff_path {}
4226
+set current_diff_side {}
4227
set diff_actions [list]
4228
proc trace_current_diff_path {varname args} {
4229
global current_diff_path diff_actions file_states
4364
}
4365
lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
4366
$ctxm add separator
4367
+$ctxm add command \
4368
+ -label {Apply/Reverse Hunk} \
4369
+ -font font_ui \
4370
+ -command {apply_hunk $cursorX $cursorY}
4371
+set ui_diff_applyhunk [$ctxm index last]
4372
+lappend diff_actions [list $ctxm entryconf $ui_diff_applyhunk -state]
4373
+$ctxm add separator
4374
$ctxm add command \
4375
-label {Decrease Font Size} \
4376
-font font_ui \
4402
$ctxm add command -label {Options...} \
4403
-font font_ui \
4404
-command do_options
4316
-bind_button3 $ui_diff "tk_popup $ctxm %X %Y"
4405
+bind_button3 $ui_diff "
4406
+ set cursorX %x
4407
+ set cursorY %y
4408
+ if {\$ui_index eq \$current_diff_side} {
4409
+ $ctxm entryconf $ui_diff_applyhunk -label {Unstage Hunk From Commit}
4410
+ } else {
4411
+ $ctxm entryconf $ui_diff_applyhunk -label {Stage Hunk For Commit}
4412
+ }
4413
+ tk_popup $ctxm %X %Y
4414
+"
4415
4416
# -- Status Bar
4417
#