git-gui: always reset the current tag
[git/jrn.git] / lib / diff.tcl
blob91ed7c638c507421cdfc87ad923c6787d79d31a9
1 # git-gui diff viewer
2 # Copyright (C) 2006, 2007 Shawn Pearce
4 proc clear_diff {} {
5 global ui_diff current_diff_path current_diff_header
6 global ui_index ui_workdir
8 $ui_diff conf -state normal
9 $ui_diff delete 0.0 end
10 $ui_diff conf -state disabled
12 set current_diff_path {}
13 set current_diff_header {}
15 $ui_index tag remove in_diff 0.0 end
16 $ui_workdir tag remove in_diff 0.0 end
19 proc reshow_diff {{after {}}} {
20 global file_states file_lists
21 global current_diff_path current_diff_side
22 global ui_diff
24 set p $current_diff_path
25 if {$p eq {}} {
26 # No diff is being shown.
27 } elseif {$current_diff_side eq {}} {
28 clear_diff
29 } elseif {[catch {set s $file_states($p)}]
30 || [lsearch -sorted -exact $file_lists($current_diff_side) $p] == -1} {
32 if {[find_next_diff $current_diff_side $p {} {[^O]}]} {
33 next_diff $after
34 } else {
35 clear_diff
37 } else {
38 set save_pos [lindex [$ui_diff yview] 0]
39 show_diff $p $current_diff_side {} $save_pos $after
43 proc force_diff_encoding {enc} {
44 global current_diff_path
46 if {$current_diff_path ne {}} {
47 force_path_encoding $current_diff_path $enc
48 reshow_diff
52 proc handle_empty_diff {} {
53 global current_diff_path file_states file_lists
54 global diff_empty_count
56 set path $current_diff_path
57 set s $file_states($path)
58 if {[lindex $s 0] ne {_M} || [has_textconv $path]} return
60 # Prevent infinite rescan loops
61 incr diff_empty_count
62 if {$diff_empty_count > 1} return
64 info_popup [mc "No differences detected.
66 %s has no changes.
68 The modification date of this file was updated by another application, but the content within the file was not changed.
70 A rescan will be automatically started to find other files which may have the same state." [short_path $path]]
72 clear_diff
73 display_file $path __
74 rescan ui_ready 0
77 proc show_diff {path w {lno {}} {scroll_pos {}} {callback {}}} {
78 global file_states file_lists
79 global is_3way_diff is_conflict_diff diff_active repo_config
80 global ui_diff ui_index ui_workdir
81 global current_diff_path current_diff_side current_diff_header
82 global current_diff_queue
84 if {$diff_active || ![lock_index read]} return
86 clear_diff
87 if {$lno == {}} {
88 set lno [lsearch -sorted -exact $file_lists($w) $path]
89 if {$lno >= 0} {
90 incr lno
93 if {$lno >= 1} {
94 $w tag add in_diff $lno.0 [expr {$lno + 1}].0
95 $w see $lno.0
98 set s $file_states($path)
99 set m [lindex $s 0]
100 set is_conflict_diff 0
101 set current_diff_path $path
102 set current_diff_side $w
103 set current_diff_queue {}
104 ui_status [mc "Loading diff of %s..." [escape_path $path]]
106 set cont_info [list $scroll_pos $callback]
108 if {[string first {U} $m] >= 0} {
109 merge_load_stages $path [list show_unmerged_diff $cont_info]
110 } elseif {$m eq {_O}} {
111 show_other_diff $path $w $m $cont_info
112 } else {
113 start_show_diff $cont_info
117 proc show_unmerged_diff {cont_info} {
118 global current_diff_path current_diff_side
119 global merge_stages ui_diff is_conflict_diff
120 global current_diff_queue
122 if {$merge_stages(2) eq {}} {
123 set is_conflict_diff 1
124 lappend current_diff_queue \
125 [list [mc "LOCAL: deleted\nREMOTE:\n"] d= \
126 [list ":1:$current_diff_path" ":3:$current_diff_path"]]
127 } elseif {$merge_stages(3) eq {}} {
128 set is_conflict_diff 1
129 lappend current_diff_queue \
130 [list [mc "REMOTE: deleted\nLOCAL:\n"] d= \
131 [list ":1:$current_diff_path" ":2:$current_diff_path"]]
132 } elseif {[lindex $merge_stages(1) 0] eq {120000}
133 || [lindex $merge_stages(2) 0] eq {120000}
134 || [lindex $merge_stages(3) 0] eq {120000}} {
135 set is_conflict_diff 1
136 lappend current_diff_queue \
137 [list [mc "LOCAL:\n"] d= \
138 [list ":1:$current_diff_path" ":2:$current_diff_path"]]
139 lappend current_diff_queue \
140 [list [mc "REMOTE:\n"] d= \
141 [list ":1:$current_diff_path" ":3:$current_diff_path"]]
142 } else {
143 start_show_diff $cont_info
144 return
147 advance_diff_queue $cont_info
150 proc advance_diff_queue {cont_info} {
151 global current_diff_queue ui_diff
153 set item [lindex $current_diff_queue 0]
154 set current_diff_queue [lrange $current_diff_queue 1 end]
156 $ui_diff conf -state normal
157 $ui_diff insert end [lindex $item 0] [lindex $item 1]
158 $ui_diff conf -state disabled
160 start_show_diff $cont_info [lindex $item 2]
163 proc show_other_diff {path w m cont_info} {
164 global file_states file_lists
165 global is_3way_diff diff_active repo_config
166 global ui_diff ui_index ui_workdir
167 global current_diff_path current_diff_side current_diff_header
169 # - Git won't give us the diff, there's nothing to compare to!
171 if {$m eq {_O}} {
172 set max_sz 100000
173 set type unknown
174 if {[catch {
175 set type [file type $path]
176 switch -- $type {
177 directory {
178 set type submodule
179 set content {}
180 set sz 0
182 link {
183 set content [file readlink $path]
184 set sz [string length $content]
186 file {
187 set fd [open $path r]
188 fconfigure $fd \
189 -eofchar {} \
190 -encoding [get_path_encoding $path]
191 set content [read $fd $max_sz]
192 close $fd
193 set sz [file size $path]
195 default {
196 error "'$type' not supported"
199 } err ]} {
200 set diff_active 0
201 unlock_index
202 ui_status [mc "Unable to display %s" [escape_path $path]]
203 error_popup [strcat [mc "Error loading file:"] "\n\n$err"]
204 return
206 $ui_diff conf -state normal
207 if {$type eq {submodule}} {
208 $ui_diff insert end [append \
209 "* " \
210 [mc "Git Repository (subproject)"] \
211 "\n"] d_info
212 } elseif {![catch {set type [exec file $path]}]} {
213 set n [string length $path]
214 if {[string equal -length $n $path $type]} {
215 set type [string range $type $n end]
216 regsub {^:?\s*} $type {} type
218 $ui_diff insert end "* $type\n" d_info
220 if {[string first "\0" $content] != -1} {
221 $ui_diff insert end \
222 [mc "* Binary file (not showing content)."] \
223 d_info
224 } else {
225 if {$sz > $max_sz} {
226 $ui_diff insert end [mc \
227 "* Untracked file is %d bytes.
228 * Showing only first %d bytes.
229 " $sz $max_sz] d_info
231 $ui_diff insert end $content
232 if {$sz > $max_sz} {
233 $ui_diff insert end [mc "
234 * Untracked file clipped here by %s.
235 * To see the entire file, use an external editor.
236 " [appname]] d_info
239 $ui_diff conf -state disabled
240 set diff_active 0
241 unlock_index
242 set scroll_pos [lindex $cont_info 0]
243 if {$scroll_pos ne {}} {
244 update
245 $ui_diff yview moveto $scroll_pos
247 ui_ready
248 set callback [lindex $cont_info 1]
249 if {$callback ne {}} {
250 eval $callback
252 return
256 proc get_conflict_marker_size {path} {
257 set size 7
258 catch {
259 set fd_rc [eval [list git_read check-attr "conflict-marker-size" -- $path]]
260 set ret [gets $fd_rc line]
261 close $fd_rc
262 if {$ret > 0} {
263 regexp {.*: conflict-marker-size: (\d+)$} $line line size
266 return $size
269 proc start_show_diff {cont_info {add_opts {}}} {
270 global file_states file_lists
271 global is_3way_diff is_submodule_diff diff_active repo_config
272 global ui_diff ui_index ui_workdir
273 global current_diff_path current_diff_side current_diff_header
275 set path $current_diff_path
276 set w $current_diff_side
278 set s $file_states($path)
279 set m [lindex $s 0]
280 set is_3way_diff 0
281 set is_submodule_diff 0
282 set diff_active 1
283 set current_diff_header {}
284 set conflict_size [get_conflict_marker_size $path]
286 set cmd [list]
287 if {$w eq $ui_index} {
288 lappend cmd diff-index
289 lappend cmd --cached
290 } elseif {$w eq $ui_workdir} {
291 if {[string first {U} $m] >= 0} {
292 lappend cmd diff
293 } else {
294 lappend cmd diff-files
297 if {![is_config_false gui.textconv] && [git-version >= 1.6.1]} {
298 lappend cmd --textconv
301 if {[string match {160000 *} [lindex $s 2]]
302 || [string match {160000 *} [lindex $s 3]]} {
303 set is_submodule_diff 1
305 if {[git-version >= "1.6.6"]} {
306 lappend cmd --submodule
310 lappend cmd -p
311 lappend cmd --color
312 if {$repo_config(gui.diffcontext) >= 1} {
313 lappend cmd "-U$repo_config(gui.diffcontext)"
315 if {$w eq $ui_index} {
316 lappend cmd [PARENT]
318 if {$add_opts ne {}} {
319 eval lappend cmd $add_opts
320 } else {
321 lappend cmd --
322 lappend cmd $path
325 if {$is_submodule_diff && [git-version < "1.6.6"]} {
326 if {$w eq $ui_index} {
327 set cmd [list submodule summary --cached -- $path]
328 } else {
329 set cmd [list submodule summary --files -- $path]
333 if {[catch {set fd [eval git_read --nice $cmd]} err]} {
334 set diff_active 0
335 unlock_index
336 ui_status [mc "Unable to display %s" [escape_path $path]]
337 error_popup [strcat [mc "Error loading diff:"] "\n\n$err"]
338 return
341 set ::current_diff_inheader 1
342 fconfigure $fd \
343 -blocking 0 \
344 -encoding [get_path_encoding $path] \
345 -translation lf
346 fileevent $fd readable [list read_diff $fd $conflict_size $cont_info]
349 proc parse_color_line {line} {
350 set start 0
351 set result ""
352 set markup [list]
353 set regexp {\033\[((?:\d+;)*\d+)?m}
354 set need_reset 0
355 while {[regexp -indices -start $start $regexp $line match code]} {
356 foreach {begin end} $match break
357 append result [string range $line $start [expr {$begin - 1}]]
358 set pos [string length $result]
359 set col [eval [linsert $code 0 string range $line]]
360 set start [incr end]
361 if {$col eq "0" || $col eq ""} {
362 if {!$need_reset} continue
363 set need_reset 0
364 } else {
365 set need_reset 1
367 lappend markup $pos $col
369 append result [string range $line $start end]
370 if {[llength $markup] < 4} {set markup {}}
371 return [list $result $markup]
374 proc read_diff {fd conflict_size cont_info} {
375 global ui_diff diff_active is_submodule_diff
376 global is_3way_diff is_conflict_diff current_diff_header
377 global current_diff_queue
378 global diff_empty_count
380 $ui_diff conf -state normal
381 while {[gets $fd line] >= 0} {
382 foreach {line markup} [parse_color_line $line] break
383 set line [string map {\033 ^} $line]
385 set tags {}
387 # -- Check for start of diff header.
388 if { [string match {diff --git *} $line]
389 || [string match {diff --cc *} $line]
390 || [string match {diff --combined *} $line]} {
391 set ::current_diff_inheader 1
394 # -- Check for end of diff header (any hunk line will do this).
396 if {[regexp {^@@+ } $line]} {set ::current_diff_inheader 0}
398 # -- Automatically detect if this is a 3 way diff.
400 if {[string match {@@@ *} $line]} {set is_3way_diff 1}
402 if {$::current_diff_inheader} {
403 append current_diff_header $line "\n"
405 # -- Cleanup uninteresting diff header lines.
407 if { [string match {diff --git *} $line]
408 || [string match {diff --cc *} $line]
409 || [string match {diff --combined *} $line]
410 || [string match {--- *} $line]
411 || [string match {+++ *} $line]
412 || [string match {index *} $line]} {
413 continue
416 # -- Name it symlink, not 120000
417 # Note, that the original line is in $current_diff_header
418 regsub {^(deleted|new) file mode 120000} $line {\1 symlink} line
423 if {[string match {new file *} $line]
424 || [regexp {^(old|new) mode *} $line]
425 || [string match {deleted file *} $line]
426 || [string match {deleted symlink} $line]
427 || [string match {new symlink} $line]
428 || [string match {Binary files * and * differ} $line]
429 || $line eq {\ No newline at end of file}
430 || [regexp {^\* Unmerged path } $line]} {
431 } elseif {$is_3way_diff} {
432 set op [string range $line 0 1]
433 switch -- $op {
434 { } {set tags {}}
435 {@@} {set tags d_@}
436 { +} {set tags d_s+}
437 { -} {set tags d_s-}
438 {+ } {set tags d_+s}
439 {- } {set tags d_-s}
440 {--} {set tags d_--}
441 {++} {
442 set regexp [string map [list %conflict_size $conflict_size]\
443 {^\+\+([<>=]){%conflict_size}(?: |$)}]
444 if {[regexp $regexp $line _g op]} {
445 set is_conflict_diff 1
446 set line [string replace $line 0 1 { }]
447 set tags d$op
448 } else {
449 set tags d_++
452 default {
453 puts "error: Unhandled 3 way diff marker: {$op}"
454 set tags {}
457 } elseif {$is_submodule_diff} {
458 if {$line == ""} continue
459 if {[regexp {^Submodule } $line]} {
460 set tags d_info
461 } elseif {[regexp {^\* } $line]} {
462 set line [string replace $line 0 1 {Submodule }]
463 set tags d_info
464 } else {
465 set op [string range $line 0 2]
466 switch -- $op {
467 { <} {set tags d_-}
468 { >} {set tags d_+}
469 { W} {set tags {}}
470 default {
471 puts "error: Unhandled submodule diff marker: {$op}"
472 set tags {}
476 } else {
477 set op [string index $line 0]
478 switch -- $op {
479 { } {set tags {}}
480 {@} {set tags d_@}
481 {-} {set tags d_-}
482 {+} {
483 set regexp [string map [list %conflict_size $conflict_size]\
484 {^\+([<>=]){%conflict_size}(?: |$)}]
485 if {[regexp $regexp $line _g op]} {
486 set is_conflict_diff 1
487 set tags d$op
488 } else {
489 set tags d_+
492 default {
493 puts "error: Unhandled 2 way diff marker: {$op}"
494 set tags {}
498 set mark [$ui_diff index "end - 1 line linestart"]
499 $ui_diff insert end $line $tags
500 if {[string index $line end] eq "\r"} {
501 $ui_diff tag add d_cr {end - 2c}
503 $ui_diff insert end "\n" $tags
505 foreach {posbegin colbegin posend colend} $markup {
506 set prefix clr
507 foreach style [split $colbegin ";"] {
508 if {$style eq "7"} {append prefix i; continue}
509 if {$style < 30 || $style > 47} {continue}
510 set a "$mark linestart + $posbegin chars"
511 set b "$mark linestart + $posend chars"
512 catch {$ui_diff tag add $prefix$style $a $b}
516 $ui_diff conf -state disabled
518 if {[eof $fd]} {
519 close $fd
521 if {$current_diff_queue ne {}} {
522 advance_diff_queue $cont_info
523 return
526 set diff_active 0
527 unlock_index
528 set scroll_pos [lindex $cont_info 0]
529 if {$scroll_pos ne {}} {
530 update
531 $ui_diff yview moveto $scroll_pos
533 ui_ready
535 if {[$ui_diff index end] eq {2.0}} {
536 handle_empty_diff
537 } else {
538 set diff_empty_count 0
541 set callback [lindex $cont_info 1]
542 if {$callback ne {}} {
543 eval $callback
548 proc apply_hunk {x y} {
549 global current_diff_path current_diff_header current_diff_side
550 global ui_diff ui_index file_states
552 if {$current_diff_path eq {} || $current_diff_header eq {}} return
553 if {![lock_index apply_hunk]} return
555 set apply_cmd {apply --cached --whitespace=nowarn}
556 set mi [lindex $file_states($current_diff_path) 0]
557 if {$current_diff_side eq $ui_index} {
558 set failed_msg [mc "Failed to unstage selected hunk."]
559 lappend apply_cmd --reverse
560 if {[string index $mi 0] ne {M}} {
561 unlock_index
562 return
564 } else {
565 set failed_msg [mc "Failed to stage selected hunk."]
566 if {[string index $mi 1] ne {M}} {
567 unlock_index
568 return
572 set s_lno [lindex [split [$ui_diff index @$x,$y] .] 0]
573 set s_lno [$ui_diff search -backwards -regexp ^@@ $s_lno.0 0.0]
574 if {$s_lno eq {}} {
575 unlock_index
576 return
579 set e_lno [$ui_diff search -forwards -regexp ^@@ "$s_lno + 1 lines" end]
580 if {$e_lno eq {}} {
581 set e_lno end
584 if {[catch {
585 set enc [get_path_encoding $current_diff_path]
586 set p [eval git_write $apply_cmd]
587 fconfigure $p -translation binary -encoding $enc
588 puts -nonewline $p $current_diff_header
589 puts -nonewline $p [$ui_diff get $s_lno $e_lno]
590 close $p} err]} {
591 error_popup [append $failed_msg "\n\n$err"]
592 unlock_index
593 return
596 $ui_diff conf -state normal
597 $ui_diff delete $s_lno $e_lno
598 $ui_diff conf -state disabled
600 if {[$ui_diff get 1.0 end] eq "\n"} {
601 set o _
602 } else {
603 set o ?
606 if {$current_diff_side eq $ui_index} {
607 set mi ${o}M
608 } elseif {[string index $mi 0] eq {_}} {
609 set mi M$o
610 } else {
611 set mi ?$o
613 unlock_index
614 display_file $current_diff_path $mi
615 # This should trigger shift to the next changed file
616 if {$o eq {_}} {
617 reshow_diff
621 proc apply_range_or_line {x y} {
622 global current_diff_path current_diff_header current_diff_side
623 global ui_diff ui_index file_states
625 set selected [$ui_diff tag nextrange sel 0.0]
627 if {$selected == {}} {
628 set first [$ui_diff index "@$x,$y"]
629 set last $first
630 } else {
631 set first [lindex $selected 0]
632 set last [lindex $selected 1]
635 set first_l [$ui_diff index "$first linestart"]
636 set last_l [$ui_diff index "$last lineend"]
638 if {$current_diff_path eq {} || $current_diff_header eq {}} return
639 if {![lock_index apply_hunk]} return
641 set apply_cmd {apply --cached --whitespace=nowarn}
642 set mi [lindex $file_states($current_diff_path) 0]
643 if {$current_diff_side eq $ui_index} {
644 set failed_msg [mc "Failed to unstage selected line."]
645 set to_context {+}
646 lappend apply_cmd --reverse
647 if {[string index $mi 0] ne {M}} {
648 unlock_index
649 return
651 } else {
652 set failed_msg [mc "Failed to stage selected line."]
653 set to_context {-}
654 if {[string index $mi 1] ne {M}} {
655 unlock_index
656 return
660 set wholepatch {}
662 while {$first_l < $last_l} {
663 set i_l [$ui_diff search -backwards -regexp ^@@ $first_l 0.0]
664 if {$i_l eq {}} {
665 # If there's not a @@ above, then the selected range
666 # must have come before the first_l @@
667 set i_l [$ui_diff search -regexp ^@@ $first_l $last_l]
669 if {$i_l eq {}} {
670 unlock_index
671 return
673 # $i_l is now at the beginning of a line
675 # pick start line number from hunk header
676 set hh [$ui_diff get $i_l "$i_l + 1 lines"]
677 set hh [lindex [split $hh ,] 0]
678 set hln [lindex [split $hh -] 1]
680 # There is a special situation to take care of. Consider this
681 # hunk:
683 # @@ -10,4 +10,4 @@
684 # context before
685 # -old 1
686 # -old 2
687 # +new 1
688 # +new 2
689 # context after
691 # We used to keep the context lines in the order they appear in
692 # the hunk. But then it is not possible to correctly stage only
693 # "-old 1" and "+new 1" - it would result in this staged text:
695 # context before
696 # old 2
697 # new 1
698 # context after
700 # (By symmetry it is not possible to *un*stage "old 2" and "new
701 # 2".)
703 # We resolve the problem by introducing an asymmetry, namely,
704 # when a "+" line is *staged*, it is moved in front of the
705 # context lines that are generated from the "-" lines that are
706 # immediately before the "+" block. That is, we construct this
707 # patch:
709 # @@ -10,4 +10,5 @@
710 # context before
711 # +new 1
712 # old 1
713 # old 2
714 # context after
716 # But we do *not* treat "-" lines that are *un*staged in a
717 # special way.
719 # With this asymmetry it is possible to stage the change "old
720 # 1" -> "new 1" directly, and to stage the change "old 2" ->
721 # "new 2" by first staging the entire hunk and then unstaging
722 # the change "old 1" -> "new 1".
724 # Applying multiple lines adds complexity to the special
725 # situation. The pre_context must be moved after the entire
726 # first block of consecutive staged "+" lines, so that
727 # staging both additions gives the following patch:
729 # @@ -10,4 +10,6 @@
730 # context before
731 # +new 1
732 # +new 2
733 # old 1
734 # old 2
735 # context after
737 # This is non-empty if and only if we are _staging_ changes;
738 # then it accumulates the consecutive "-" lines (after
739 # converting them to context lines) in order to be moved after
740 # "+" change lines.
741 set pre_context {}
743 set n 0
744 set m 0
745 set i_l [$ui_diff index "$i_l + 1 lines"]
746 set patch {}
747 while {[$ui_diff compare $i_l < "end - 1 chars"] &&
748 [$ui_diff get $i_l "$i_l + 2 chars"] ne {@@}} {
749 set next_l [$ui_diff index "$i_l + 1 lines"]
750 set c1 [$ui_diff get $i_l]
751 if {[$ui_diff compare $first_l <= $i_l] &&
752 [$ui_diff compare $i_l < $last_l] &&
753 ($c1 eq {-} || $c1 eq {+})} {
754 # a line to stage/unstage
755 set ln [$ui_diff get $i_l $next_l]
756 if {$c1 eq {-}} {
757 set n [expr $n+1]
758 set patch "$patch$pre_context$ln"
759 set pre_context {}
760 } else {
761 set m [expr $m+1]
762 set patch "$patch$ln"
764 } elseif {$c1 ne {-} && $c1 ne {+}} {
765 # context line
766 set ln [$ui_diff get $i_l $next_l]
767 set patch "$patch$pre_context$ln"
768 set n [expr $n+1]
769 set m [expr $m+1]
770 set pre_context {}
771 } elseif {$c1 eq $to_context} {
772 # turn change line into context line
773 set ln [$ui_diff get "$i_l + 1 chars" $next_l]
774 if {$c1 eq {-}} {
775 set pre_context "$pre_context $ln"
776 } else {
777 set patch "$patch $ln"
779 set n [expr $n+1]
780 set m [expr $m+1]
781 } else {
782 # a change in the opposite direction of
783 # to_context which is outside the range of
784 # lines to apply.
785 set patch "$patch$pre_context"
786 set pre_context {}
788 set i_l $next_l
790 set patch "$patch$pre_context"
791 set wholepatch "$wholepatch@@ -$hln,$n +$hln,$m @@\n$patch"
792 set first_l [$ui_diff index "$next_l + 1 lines"]
795 if {[catch {
796 set enc [get_path_encoding $current_diff_path]
797 set p [eval git_write $apply_cmd]
798 fconfigure $p -translation binary -encoding $enc
799 puts -nonewline $p $current_diff_header
800 puts -nonewline $p $wholepatch
801 close $p} err]} {
802 error_popup [append $failed_msg "\n\n$err"]
805 unlock_index