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 ui_status_value file_states file_lists
21 global current_diff_path current_diff_side
23 set p $current_diff_path
25 # No diff is being shown.
26 } elseif {$current_diff_side eq {}
27 || [catch {set s $file_states($p)}]
28 || [lsearch -sorted -exact $file_lists($current_diff_side) $p] == -1} {
31 show_diff $p $current_diff_side
35 proc handle_empty_diff {} {
36 global current_diff_path file_states file_lists
38 set path $current_diff_path
39 set s $file_states($path)
40 if {[lindex $s 0] ne {_M}} return
42 info_popup "No differences detected.
44 [short_path $path] has no changes.
46 The modification date of this file was updated by another application, but the content within the file was not changed.
48 A rescan will be automatically started to find other files which may have the same state."
52 rescan {set ui_status_value {Ready.}} 0
55 proc show_diff {path w {lno {}}} {
56 global file_states file_lists
57 global is_3way_diff diff_active repo_config
58 global ui_diff ui_status_value ui_index ui_workdir
59 global current_diff_path current_diff_side current_diff_header
61 if {$diff_active || ![lock_index read]} return
65 set lno [lsearch -sorted -exact $file_lists($w) $path]
71 $w tag add in_diff $lno.0 [expr {$lno + 1}].0
74 set s $file_states($path)
78 set current_diff_path $path
79 set current_diff_side $w
80 set current_diff_header {}
81 set ui_status_value "Loading diff of [escape_path $path]..."
83 # - Git won't give us the diff, there's nothing to compare to!
86 set max_sz [expr {128 * 1024}]
89 set content [read $fd $max_sz]
91 set sz [file size $path]
95 set ui_status_value "Unable to display [escape_path $path]"
96 error_popup "Error loading file:\n\n$err"
99 $ui_diff conf -state normal
100 if {![catch {set type [exec file $path]}]} {
101 set n [string length $path]
102 if {[string equal -length $n $path $type]} {
103 set type [string range $type $n end]
104 regsub {^:?\s*} $type {} type
106 $ui_diff insert end "* $type\n" d_@
108 if {[string first "\0" $content] != -1} {
109 $ui_diff insert end \
110 "* Binary file (not showing content)." \
114 $ui_diff insert end \
115 "* Untracked file is $sz bytes.
116 * Showing only first $max_sz bytes.
119 $ui_diff insert end $content
121 $ui_diff insert end "
122 * Untracked file clipped here by [appname].
123 * To see the entire file, use an external editor.
127 $ui_diff conf -state disabled
130 set ui_status_value {Ready.}
135 if {$w eq $ui_index} {
136 lappend cmd diff-index
138 } elseif {$w eq $ui_workdir} {
139 if {[string index $m 0] eq {U}} {
142 lappend cmd diff-files
147 lappend cmd --no-color
148 if {$repo_config(gui.diffcontext) > 0} {
149 lappend cmd "-U$repo_config(gui.diffcontext)"
151 if {$w eq $ui_index} {
157 if {[catch {set fd [open $cmd r]} err]} {
160 set ui_status_value "Unable to display [escape_path $path]"
161 error_popup "Error loading diff:\n\n$err"
169 fileevent $fd readable [list read_diff $fd]
172 proc read_diff {fd} {
173 global ui_diff ui_status_value diff_active
174 global is_3way_diff current_diff_header
176 $ui_diff conf -state normal
177 while {[gets $fd line] >= 0} {
178 # -- Cleanup uninteresting diff header lines.
180 if { [string match {diff --git *} $line]
181 || [string match {diff --cc *} $line]
182 || [string match {diff --combined *} $line]
183 || [string match {--- *} $line]
184 || [string match {+++ *} $line]} {
185 append current_diff_header $line "\n"
188 if {[string match {index *} $line]} continue
189 if {$line eq {deleted file mode 120000}} {
190 set line "deleted symlink"
193 # -- Automatically detect if this is a 3 way diff.
195 if {[string match {@@@ *} $line]} {set is_3way_diff 1}
197 if {[string match {mode *} $line]
198 || [string match {new file *} $line]
199 || [string match {deleted file *} $line]
200 || [string match {Binary files * and * differ} $line]
201 || $line eq {\ No newline at end of file}
202 || [regexp {^\* Unmerged path } $line]} {
204 } elseif {$is_3way_diff} {
205 set op [string range $line 0 1]
215 if {[regexp {^\+\+([<>]{7} |={7})} $line _g op]} {
216 set line [string replace $line 0 1 { }]
223 puts "error: Unhandled 3 way diff marker: {$op}"
228 set op [string index $line 0]
234 if {[regexp {^\+([<>]{7} |={7})} $line _g op]} {
235 set line [string replace $line 0 0 { }]
242 puts "error: Unhandled 2 way diff marker: {$op}"
247 $ui_diff insert end $line $tags
248 if {[string index $line end] eq "\r"} {
249 $ui_diff tag add d_cr {end - 2c}
251 $ui_diff insert end "\n" $tags
253 $ui_diff conf -state disabled
259 set ui_status_value {Ready.}
261 if {[$ui_diff index end] eq {2.0}} {
267 proc apply_hunk {x y} {
268 global current_diff_path current_diff_header current_diff_side
269 global ui_diff ui_index file_states
271 if {$current_diff_path eq {} || $current_diff_header eq {}} return
272 if {![lock_index apply_hunk]} return
274 set apply_cmd {git apply --cached --whitespace=nowarn}
275 set mi [lindex $file_states($current_diff_path) 0]
276 if {$current_diff_side eq $ui_index} {
278 lappend apply_cmd --reverse
279 if {[string index $mi 0] ne {M}} {
285 if {[string index $mi 1] ne {M}} {
291 set s_lno [lindex [split [$ui_diff index @$x,$y] .] 0]
292 set s_lno [$ui_diff search -backwards -regexp ^@@ $s_lno.0 0.0]
298 set e_lno [$ui_diff search -forwards -regexp ^@@ "$s_lno + 1 lines" end]
304 set p [open "| $apply_cmd" w]
305 fconfigure $p -translation binary -encoding binary
306 puts -nonewline $p $current_diff_header
307 puts -nonewline $p [$ui_diff get $s_lno $e_lno]
309 error_popup "Failed to $mode selected hunk.\n\n$err"
314 $ui_diff conf -state normal
315 $ui_diff delete $s_lno $e_lno
316 $ui_diff conf -state disabled
318 if {[$ui_diff get 1.0 end] eq "\n"} {
324 if {$current_diff_side eq $ui_index} {
326 } elseif {[string index $mi 0] eq {_}} {
332 display_file $current_diff_path $mi