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 force_diff_encoding {enc} {
44 global current_diff_path
46 if {$current_diff_path ne {}} {
47 force_path_encoding $current_diff_path $enc
52 proc handle_empty_diff {} {
53 global current_diff_path file_states file_lists
55 set path $current_diff_path
56 set s $file_states($path)
57 if {[lindex $s 0] ne {_M}} return
59 info_popup [mc "No differences detected.
63 The modification date of this file was updated by another application, but the content within the file was not changed.
65 A rescan will be automatically started to find other files which may have the same state." [short_path $path]]
72 proc show_diff {path w {lno {}} {scroll_pos {}}} {
73 global file_states file_lists
74 global is_3way_diff diff_active repo_config
75 global ui_diff ui_index ui_workdir
76 global current_diff_path current_diff_side current_diff_header
77 global current_diff_queue
79 if {$diff_active || ![lock_index read]} return
83 set lno [lsearch -sorted -exact $file_lists($w) $path]
89 $w tag add in_diff $lno.0 [expr {$lno + 1}].0
93 set s $file_states($path)
95 set current_diff_path $path
96 set current_diff_side $w
97 set current_diff_queue {}
98 ui_status [mc "Loading diff of %s..." [escape_path $path]]
100 if {[string first {U} $m] >= 0} {
101 merge_load_stages $path [list show_unmerged_diff $scroll_pos]
102 } elseif {$m eq {_O}} {
103 show_other_diff $path $w $m $scroll_pos
105 start_show_diff $scroll_pos
109 proc show_unmerged_diff {scroll_pos} {
110 global current_diff_path current_diff_side
111 global merge_stages ui_diff
112 global current_diff_queue
114 if {$merge_stages(2) eq {}} {
115 lappend current_diff_queue \
116 [list "LOCAL: deleted\nREMOTE:\n" d======= \
117 [list ":1:$current_diff_path" ":3:$current_diff_path"]]
118 } elseif {$merge_stages(3) eq {}} {
119 lappend current_diff_queue \
120 [list "REMOTE: deleted\nLOCAL:\n" d======= \
121 [list ":1:$current_diff_path" ":2:$current_diff_path"]]
122 } elseif {[lindex $merge_stages(1) 0] eq {120000}
123 || [lindex $merge_stages(2) 0] eq {120000}
124 || [lindex $merge_stages(3) 0] eq {120000}} {
125 lappend current_diff_queue \
126 [list "LOCAL:\n" d======= \
127 [list ":1:$current_diff_path" ":2:$current_diff_path"]]
128 lappend current_diff_queue \
129 [list "REMOTE:\n" d======= \
130 [list ":1:$current_diff_path" ":3:$current_diff_path"]]
132 start_show_diff $scroll_pos
136 advance_diff_queue $scroll_pos
139 proc advance_diff_queue {scroll_pos} {
140 global current_diff_queue ui_diff
142 set item [lindex $current_diff_queue 0]
143 set current_diff_queue [lrange $current_diff_queue 1 end]
145 $ui_diff conf -state normal
146 $ui_diff insert end [lindex $item 0] [lindex $item 1]
147 $ui_diff conf -state disabled
149 start_show_diff $scroll_pos [lindex $item 2]
152 proc show_other_diff {path w m scroll_pos} {
153 global file_states file_lists
154 global is_3way_diff diff_active repo_config
155 global ui_diff ui_index ui_workdir
156 global current_diff_path current_diff_side current_diff_header
158 # - Git won't give us the diff, there's nothing to compare to!
161 set max_sz [expr {128 * 1024}]
164 set type [file type $path]
172 set content [file readlink $path]
173 set sz [string length $content]
176 set fd [open $path r]
179 -encoding [get_path_encoding $path]
180 set content [read $fd $max_sz]
182 set sz [file size $path]
185 error "'$type' not supported"
191 ui_status [mc "Unable to display %s" [escape_path $path]]
192 error_popup [strcat [mc "Error loading file:"] "\n\n$err"]
195 $ui_diff conf -state normal
196 if {$type eq {submodule}} {
197 $ui_diff insert end [append \
199 [mc "Git Repository (subproject)"] \
201 } elseif {![catch {set type [exec file $path]}]} {
202 set n [string length $path]
203 if {[string equal -length $n $path $type]} {
204 set type [string range $type $n end]
205 regsub {^:?\s*} $type {} type
207 $ui_diff insert end "* $type\n" d_@
209 if {[string first "\0" $content] != -1} {
210 $ui_diff insert end \
211 [mc "* Binary file (not showing content)."] \
215 $ui_diff insert end \
216 "* Untracked file is $sz bytes.
217 * Showing only first $max_sz bytes.
220 $ui_diff insert end $content
222 $ui_diff insert end "
223 * Untracked file clipped here by [appname].
224 * To see the entire file, use an external editor.
228 $ui_diff conf -state disabled
231 if {$scroll_pos ne {}} {
233 $ui_diff yview moveto $scroll_pos
240 proc start_show_diff {scroll_pos {add_opts {}}} {
241 global file_states file_lists
242 global is_3way_diff diff_active repo_config
243 global ui_diff ui_index ui_workdir
244 global current_diff_path current_diff_side current_diff_header
246 set path $current_diff_path
247 set w $current_diff_side
249 set s $file_states($path)
253 set current_diff_header {}
256 if {$w eq $ui_index} {
257 lappend cmd diff-index
259 } elseif {$w eq $ui_workdir} {
260 if {[string first {U} $m] >= 0} {
263 lappend cmd diff-files
268 lappend cmd --no-color
269 if {$repo_config(gui.diffcontext) >= 1} {
270 lappend cmd "-U$repo_config(gui.diffcontext)"
272 if {$w eq $ui_index} {
275 if {$add_opts ne {}} {
276 eval lappend cmd $add_opts
282 if {[catch {set fd [eval git_read --nice $cmd]} err]} {
285 ui_status [mc "Unable to display %s" [escape_path $path]]
286 error_popup [strcat [mc "Error loading diff:"] "\n\n$err"]
290 set ::current_diff_inheader 1
293 -encoding [get_path_encoding $path] \
295 fileevent $fd readable [list read_diff $fd $scroll_pos]
298 proc read_diff {fd scroll_pos} {
299 global ui_diff diff_active
300 global is_3way_diff current_diff_header
301 global current_diff_queue
303 $ui_diff conf -state normal
304 while {[gets $fd line] >= 0} {
305 # -- Cleanup uninteresting diff header lines.
307 if {$::current_diff_inheader} {
308 if { [string match {diff --git *} $line]
309 || [string match {diff --cc *} $line]
310 || [string match {diff --combined *} $line]
311 || [string match {--- *} $line]
312 || [string match {+++ *} $line]} {
313 append current_diff_header $line "\n"
317 if {[string match {index *} $line]} continue
318 if {$line eq {deleted file mode 120000}} {
319 set line "deleted symlink"
321 set ::current_diff_inheader 0
323 # -- Automatically detect if this is a 3 way diff.
325 if {[string match {@@@ *} $line]} {set is_3way_diff 1}
327 if {[string match {mode *} $line]
328 || [string match {new file *} $line]
329 || [regexp {^(old|new) mode *} $line]
330 || [string match {deleted file *} $line]
331 || [string match {deleted symlink} $line]
332 || [string match {Binary files * and * differ} $line]
333 || $line eq {\ No newline at end of file}
334 || [regexp {^\* Unmerged path } $line]} {
336 } elseif {$is_3way_diff} {
337 set op [string range $line 0 1]
347 if {[regexp {^\+\+([<>]{7} |={7})} $line _g op]} {
348 set line [string replace $line 0 1 { }]
355 puts "error: Unhandled 3 way diff marker: {$op}"
360 set op [string index $line 0]
366 if {[regexp {^\+([<>]{7} |={7})} $line _g op]} {
367 set line [string replace $line 0 0 { }]
374 puts "error: Unhandled 2 way diff marker: {$op}"
379 $ui_diff insert end $line $tags
380 if {[string index $line end] eq "\r"} {
381 $ui_diff tag add d_cr {end - 2c}
383 $ui_diff insert end "\n" $tags
385 $ui_diff conf -state disabled
390 if {$current_diff_queue ne {}} {
391 advance_diff_queue $scroll_pos
397 if {$scroll_pos ne {}} {
399 $ui_diff yview moveto $scroll_pos
403 if {[$ui_diff index end] eq {2.0}} {
409 proc apply_hunk {x y} {
410 global current_diff_path current_diff_header current_diff_side
411 global ui_diff ui_index file_states
413 if {$current_diff_path eq {} || $current_diff_header eq {}} return
414 if {![lock_index apply_hunk]} return
416 set apply_cmd {apply --cached --whitespace=nowarn}
417 set mi [lindex $file_states($current_diff_path) 0]
418 if {$current_diff_side eq $ui_index} {
419 set failed_msg [mc "Failed to unstage selected hunk."]
420 lappend apply_cmd --reverse
421 if {[string index $mi 0] ne {M}} {
426 set failed_msg [mc "Failed to stage selected hunk."]
427 if {[string index $mi 1] ne {M}} {
433 set s_lno [lindex [split [$ui_diff index @$x,$y] .] 0]
434 set s_lno [$ui_diff search -backwards -regexp ^@@ $s_lno.0 0.0]
440 set e_lno [$ui_diff search -forwards -regexp ^@@ "$s_lno + 1 lines" end]
446 set enc [get_path_encoding $current_diff_path]
447 set p [eval git_write $apply_cmd]
448 fconfigure $p -translation binary -encoding $enc
449 puts -nonewline $p $current_diff_header
450 puts -nonewline $p [$ui_diff get $s_lno $e_lno]
452 error_popup [append $failed_msg "\n\n$err"]
457 $ui_diff conf -state normal
458 $ui_diff delete $s_lno $e_lno
459 $ui_diff conf -state disabled
461 if {[$ui_diff get 1.0 end] eq "\n"} {
467 if {$current_diff_side eq $ui_index} {
469 } elseif {[string index $mi 0] eq {_}} {
475 display_file $current_diff_path $mi
476 # This should trigger shift to the next changed file
482 proc apply_line {x y} {
483 global current_diff_path current_diff_header current_diff_side
484 global ui_diff ui_index file_states
486 if {$current_diff_path eq {} || $current_diff_header eq {}} return
487 if {![lock_index apply_hunk]} return
489 set apply_cmd {apply --cached --whitespace=nowarn}
490 set mi [lindex $file_states($current_diff_path) 0]
491 if {$current_diff_side eq $ui_index} {
492 set failed_msg [mc "Failed to unstage selected line."]
494 lappend apply_cmd --reverse
495 if {[string index $mi 0] ne {M}} {
500 set failed_msg [mc "Failed to stage selected line."]
502 if {[string index $mi 1] ne {M}} {
508 set the_l [$ui_diff index @$x,$y]
510 # operate only on change lines
511 set c1 [$ui_diff get "$the_l linestart"]
512 if {$c1 ne {+} && $c1 ne {-}} {
518 set i_l [$ui_diff search -backwards -regexp ^@@ $the_l 0.0]
523 # $i_l is now at the beginning of a line
525 # pick start line number from hunk header
526 set hh [$ui_diff get $i_l "$i_l + 1 lines"]
527 set hh [lindex [split $hh ,] 0]
528 set hln [lindex [split $hh -] 1]
530 # There is a special situation to take care of. Consider this hunk:
540 # We used to keep the context lines in the order they appear in the
541 # hunk. But then it is not possible to correctly stage only
542 # "-old 1" and "+new 1" - it would result in this staged text:
549 # (By symmetry it is not possible to *un*stage "old 2" and "new 2".)
551 # We resolve the problem by introducing an asymmetry, namely, when
552 # a "+" line is *staged*, it is moved in front of the context lines
553 # that are generated from the "-" lines that are immediately before
554 # the "+" block. That is, we construct this patch:
563 # But we do *not* treat "-" lines that are *un*staged in a special
566 # With this asymmetry it is possible to stage the change
567 # "old 1" -> "new 1" directly, and to stage the change
568 # "old 2" -> "new 2" by first staging the entire hunk and
569 # then unstaging the change "old 1" -> "new 1".
571 # This is non-empty if and only if we are _staging_ changes;
572 # then it accumulates the consecutive "-" lines (after converting
573 # them to context lines) in order to be moved after the "+" change
578 set i_l [$ui_diff index "$i_l + 1 lines"]
580 while {[$ui_diff compare $i_l < "end - 1 chars"] &&
581 [$ui_diff get $i_l "$i_l + 2 chars"] ne {@@}} {
582 set next_l [$ui_diff index "$i_l + 1 lines"]
583 set c1 [$ui_diff get $i_l]
584 if {[$ui_diff compare $i_l <= $the_l] &&
585 [$ui_diff compare $the_l < $next_l]} {
586 # the line to stage/unstage
587 set ln [$ui_diff get $i_l $next_l]
590 set patch "$patch$pre_context$ln"
592 set patch "$patch$ln$pre_context"
595 } elseif {$c1 ne {-} && $c1 ne {+}} {
597 set ln [$ui_diff get $i_l $next_l]
598 set patch "$patch$pre_context$ln"
601 } elseif {$c1 eq $to_context} {
602 # turn change line into context line
603 set ln [$ui_diff get "$i_l + 1 chars" $next_l]
605 set pre_context "$pre_context $ln"
607 set patch "$patch $ln"
613 set patch "@@ -$hln,$n +$hln,[eval expr $n $sign 1] @@\n$patch"
616 set enc [get_path_encoding $current_diff_path]
617 set p [eval git_write $apply_cmd]
618 fconfigure $p -translation binary -encoding $enc
619 puts -nonewline $p $current_diff_header
620 puts -nonewline $p $patch
622 error_popup [append $failed_msg "\n\n$err"]