2 # Copyright (C) 2006, 2007 Shawn Pearce
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
20 global file_states file_lists
21 global current_diff_path current_diff_side
24 set p $current_diff_path
26 # No diff is being shown.
27 } elseif {$current_diff_side eq {}} {
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]}]} {
38 set save_pos [lindex [$ui_diff yview] 0]
39 show_diff $p $current_diff_side {} $save_pos
43 proc handle_empty_diff {} {
44 global current_diff_path file_states file_lists
46 set path $current_diff_path
47 set s $file_states($path)
48 if {[lindex $s 0] ne {_M}} return
50 info_popup [mc "No differences detected.
54 The modification date of this file was updated by another application, but the content within the file was not changed.
56 A rescan will be automatically started to find other files which may have the same state." [short_path $path]]
63 proc show_diff {path w {lno {}} {scroll_pos {}}} {
64 global file_states file_lists
65 global is_3way_diff diff_active repo_config
66 global ui_diff ui_index ui_workdir
67 global current_diff_path current_diff_side current_diff_header
68 global current_diff_queue
70 if {$diff_active || ![lock_index read]} return
74 set lno [lsearch -sorted -exact $file_lists($w) $path]
80 $w tag add in_diff $lno.0 [expr {$lno + 1}].0
84 set s $file_states($path)
86 set current_diff_path $path
87 set current_diff_side $w
88 set current_diff_queue {}
89 ui_status [mc "Loading diff of %s..." [escape_path $path]]
91 if {[string first {U} $m] >= 0} {
92 merge_load_stages $path [list show_unmerged_diff $scroll_pos]
93 } elseif {$m eq {_O}} {
94 show_other_diff $path $w $m $scroll_pos
96 start_show_diff $scroll_pos
100 proc show_unmerged_diff {scroll_pos} {
101 global current_diff_path current_diff_side
102 global merge_stages ui_diff
103 global current_diff_queue
105 if {$merge_stages(2) eq {}} {
106 lappend current_diff_queue \
107 [list "LOCAL: deleted\nREMOTE:\n" d======= \
108 [list ":1:$current_diff_path" ":3:$current_diff_path"]]
109 } elseif {$merge_stages(3) eq {}} {
110 lappend current_diff_queue \
111 [list "REMOTE: deleted\nLOCAL:\n" d======= \
112 [list ":1:$current_diff_path" ":2:$current_diff_path"]]
113 } elseif {[lindex $merge_stages(1) 0] eq {120000}
114 || [lindex $merge_stages(2) 0] eq {120000}
115 || [lindex $merge_stages(3) 0] eq {120000}} {
116 lappend current_diff_queue \
117 [list "LOCAL:\n" d======= \
118 [list ":1:$current_diff_path" ":2:$current_diff_path"]]
119 lappend current_diff_queue \
120 [list "REMOTE:\n" d======= \
121 [list ":1:$current_diff_path" ":3:$current_diff_path"]]
123 start_show_diff $scroll_pos
127 advance_diff_queue $scroll_pos
130 proc advance_diff_queue {scroll_pos} {
131 global current_diff_queue ui_diff
133 set item [lindex $current_diff_queue 0]
134 set current_diff_queue [lrange $current_diff_queue 1 end]
136 $ui_diff conf -state normal
137 $ui_diff insert end [lindex $item 0] [lindex $item 1]
138 $ui_diff conf -state disabled
140 start_show_diff $scroll_pos [lindex $item 2]
143 proc show_other_diff {path w m scroll_pos} {
144 global file_states file_lists
145 global is_3way_diff diff_active repo_config
146 global ui_diff ui_index ui_workdir
147 global current_diff_path current_diff_side current_diff_header
149 # - Git won't give us the diff, there's nothing to compare to!
152 set max_sz [expr {128 * 1024}]
155 set type [file type $path]
163 set content [file readlink $path]
164 set sz [string length $content]
167 set fd [open $path r]
170 -encoding [get_path_encoding $path]
171 set content [read $fd $max_sz]
173 set sz [file size $path]
176 error "'$type' not supported"
182 ui_status [mc "Unable to display %s" [escape_path $path]]
183 error_popup [strcat [mc "Error loading file:"] "\n\n$err"]
186 $ui_diff conf -state normal
187 if {$type eq {submodule}} {
188 $ui_diff insert end [append \
190 [mc "Git Repository (subproject)"] \
192 } elseif {![catch {set type [exec file $path]}]} {
193 set n [string length $path]
194 if {[string equal -length $n $path $type]} {
195 set type [string range $type $n end]
196 regsub {^:?\s*} $type {} type
198 $ui_diff insert end "* $type\n" d_@
200 if {[string first "\0" $content] != -1} {
201 $ui_diff insert end \
202 [mc "* Binary file (not showing content)."] \
206 $ui_diff insert end \
207 "* Untracked file is $sz bytes.
208 * Showing only first $max_sz bytes.
211 $ui_diff insert end $content
213 $ui_diff insert end "
214 * Untracked file clipped here by [appname].
215 * To see the entire file, use an external editor.
219 $ui_diff conf -state disabled
222 if {$scroll_pos ne {}} {
224 $ui_diff yview moveto $scroll_pos
231 proc start_show_diff {scroll_pos {add_opts {}}} {
232 global file_states file_lists
233 global is_3way_diff diff_active repo_config
234 global ui_diff ui_index ui_workdir
235 global current_diff_path current_diff_side current_diff_header
237 set path $current_diff_path
238 set w $current_diff_side
240 set s $file_states($path)
244 set current_diff_header {}
247 if {$w eq $ui_index} {
248 lappend cmd diff-index
250 } elseif {$w eq $ui_workdir} {
251 if {[string first {U} $m] >= 0} {
254 lappend cmd diff-files
259 lappend cmd --no-color
260 if {$repo_config(gui.diffcontext) >= 1} {
261 lappend cmd "-U$repo_config(gui.diffcontext)"
263 if {$w eq $ui_index} {
266 if {$add_opts ne {}} {
267 eval lappend cmd $add_opts
273 if {[catch {set fd [eval git_read --nice $cmd]} err]} {
276 ui_status [mc "Unable to display %s" [escape_path $path]]
277 error_popup [strcat [mc "Error loading diff:"] "\n\n$err"]
281 set ::current_diff_inheader 1
284 -encoding [get_path_encoding $path] \
286 fileevent $fd readable [list read_diff $fd $scroll_pos]
289 proc read_diff {fd scroll_pos} {
290 global ui_diff diff_active
291 global is_3way_diff current_diff_header
292 global current_diff_queue
294 $ui_diff conf -state normal
295 while {[gets $fd line] >= 0} {
296 # -- Cleanup uninteresting diff header lines.
298 if {$::current_diff_inheader} {
299 if { [string match {diff --git *} $line]
300 || [string match {diff --cc *} $line]
301 || [string match {diff --combined *} $line]
302 || [string match {--- *} $line]
303 || [string match {+++ *} $line]} {
304 append current_diff_header $line "\n"
308 if {[string match {index *} $line]} continue
309 if {$line eq {deleted file mode 120000}} {
310 set line "deleted symlink"
312 set ::current_diff_inheader 0
314 # -- Automatically detect if this is a 3 way diff.
316 if {[string match {@@@ *} $line]} {set is_3way_diff 1}
318 if {[string match {mode *} $line]
319 || [string match {new file *} $line]
320 || [regexp {^(old|new) mode *} $line]
321 || [string match {deleted file *} $line]
322 || [string match {deleted symlink} $line]
323 || [string match {Binary files * and * differ} $line]
324 || $line eq {\ No newline at end of file}
325 || [regexp {^\* Unmerged path } $line]} {
327 } elseif {$is_3way_diff} {
328 set op [string range $line 0 1]
338 if {[regexp {^\+\+([<>]{7} |={7})} $line _g op]} {
339 set line [string replace $line 0 1 { }]
346 puts "error: Unhandled 3 way diff marker: {$op}"
351 set op [string index $line 0]
357 if {[regexp {^\+([<>]{7} |={7})} $line _g op]} {
358 set line [string replace $line 0 0 { }]
365 puts "error: Unhandled 2 way diff marker: {$op}"
370 $ui_diff insert end $line $tags
371 if {[string index $line end] eq "\r"} {
372 $ui_diff tag add d_cr {end - 2c}
374 $ui_diff insert end "\n" $tags
376 $ui_diff conf -state disabled
381 if {$current_diff_queue ne {}} {
382 advance_diff_queue $scroll_pos
388 if {$scroll_pos ne {}} {
390 $ui_diff yview moveto $scroll_pos
394 if {[$ui_diff index end] eq {2.0}} {
400 proc apply_hunk {x y} {
401 global current_diff_path current_diff_header current_diff_side
402 global ui_diff ui_index file_states
404 if {$current_diff_path eq {} || $current_diff_header eq {}} return
405 if {![lock_index apply_hunk]} return
407 set apply_cmd {apply --cached --whitespace=nowarn}
408 set mi [lindex $file_states($current_diff_path) 0]
409 if {$current_diff_side eq $ui_index} {
410 set failed_msg [mc "Failed to unstage selected hunk."]
411 lappend apply_cmd --reverse
412 if {[string index $mi 0] ne {M}} {
417 set failed_msg [mc "Failed to stage selected hunk."]
418 if {[string index $mi 1] ne {M}} {
424 set s_lno [lindex [split [$ui_diff index @$x,$y] .] 0]
425 set s_lno [$ui_diff search -backwards -regexp ^@@ $s_lno.0 0.0]
431 set e_lno [$ui_diff search -forwards -regexp ^@@ "$s_lno + 1 lines" end]
437 set enc [get_path_encoding $current_diff_path]
438 set p [eval git_write $apply_cmd]
439 fconfigure $p -translation binary -encoding $enc
440 puts -nonewline $p $current_diff_header
441 puts -nonewline $p [$ui_diff get $s_lno $e_lno]
443 error_popup [append $failed_msg "\n\n$err"]
448 $ui_diff conf -state normal
449 $ui_diff delete $s_lno $e_lno
450 $ui_diff conf -state disabled
452 if {[$ui_diff get 1.0 end] eq "\n"} {
458 if {$current_diff_side eq $ui_index} {
460 } elseif {[string index $mi 0] eq {_}} {
466 display_file $current_diff_path $mi
467 # This should trigger shift to the next changed file
473 proc apply_line {x y} {
474 global current_diff_path current_diff_header current_diff_side
475 global ui_diff ui_index file_states
477 if {$current_diff_path eq {} || $current_diff_header eq {}} return
478 if {![lock_index apply_hunk]} return
480 set apply_cmd {apply --cached --whitespace=nowarn}
481 set mi [lindex $file_states($current_diff_path) 0]
482 if {$current_diff_side eq $ui_index} {
483 set failed_msg [mc "Failed to unstage selected line."]
485 lappend apply_cmd --reverse
486 if {[string index $mi 0] ne {M}} {
491 set failed_msg [mc "Failed to stage selected line."]
493 if {[string index $mi 1] ne {M}} {
499 set the_l [$ui_diff index @$x,$y]
501 # operate only on change lines
502 set c1 [$ui_diff get "$the_l linestart"]
503 if {$c1 ne {+} && $c1 ne {-}} {
509 set i_l [$ui_diff search -backwards -regexp ^@@ $the_l 0.0]
514 # $i_l is now at the beginning of a line
516 # pick start line number from hunk header
517 set hh [$ui_diff get $i_l "$i_l + 1 lines"]
518 set hh [lindex [split $hh ,] 0]
519 set hln [lindex [split $hh -] 1]
521 # There is a special situation to take care of. Consider this hunk:
531 # We used to keep the context lines in the order they appear in the
532 # hunk. But then it is not possible to correctly stage only
533 # "-old 1" and "+new 1" - it would result in this staged text:
540 # (By symmetry it is not possible to *un*stage "old 2" and "new 2".)
542 # We resolve the problem by introducing an asymmetry, namely, when
543 # a "+" line is *staged*, it is moved in front of the context lines
544 # that are generated from the "-" lines that are immediately before
545 # the "+" block. That is, we construct this patch:
554 # But we do *not* treat "-" lines that are *un*staged in a special
557 # With this asymmetry it is possible to stage the change
558 # "old 1" -> "new 1" directly, and to stage the change
559 # "old 2" -> "new 2" by first staging the entire hunk and
560 # then unstaging the change "old 1" -> "new 1".
562 # This is non-empty if and only if we are _staging_ changes;
563 # then it accumulates the consecutive "-" lines (after converting
564 # them to context lines) in order to be moved after the "+" change
569 set i_l [$ui_diff index "$i_l + 1 lines"]
571 while {[$ui_diff compare $i_l < "end - 1 chars"] &&
572 [$ui_diff get $i_l "$i_l + 2 chars"] ne {@@}} {
573 set next_l [$ui_diff index "$i_l + 1 lines"]
574 set c1 [$ui_diff get $i_l]
575 if {[$ui_diff compare $i_l <= $the_l] &&
576 [$ui_diff compare $the_l < $next_l]} {
577 # the line to stage/unstage
578 set ln [$ui_diff get $i_l $next_l]
581 set patch "$patch$pre_context$ln"
583 set patch "$patch$ln$pre_context"
586 } elseif {$c1 ne {-} && $c1 ne {+}} {
588 set ln [$ui_diff get $i_l $next_l]
589 set patch "$patch$pre_context$ln"
592 } elseif {$c1 eq $to_context} {
593 # turn change line into context line
594 set ln [$ui_diff get "$i_l + 1 chars" $next_l]
596 set pre_context "$pre_context $ln"
598 set patch "$patch $ln"
604 set patch "@@ -$hln,$n +$hln,[eval expr $n $sign 1] @@\n$patch"
607 set enc [get_path_encoding $current_diff_path]
608 set p [eval git_write $apply_cmd]
609 fconfigure $p -translation binary -encoding $enc
610 puts -nonewline $p $current_diff_header
611 puts -nonewline $p $patch
613 error_popup [append $failed_msg "\n\n$err"]