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
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."
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_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 ui_status "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 fconfigure $fd -eofchar {}
90 set content [read $fd $max_sz]
92 set sz [file size $path]
96 ui_status "Unable to display [escape_path $path]"
97 error_popup "Error loading file:\n\n$err"
100 $ui_diff conf -state normal
101 if {![catch {set type [exec file $path]}]} {
102 set n [string length $path]
103 if {[string equal -length $n $path $type]} {
104 set type [string range $type $n end]
105 regsub {^:?\s*} $type {} type
107 $ui_diff insert end "* $type\n" d_@
109 if {[string first "\0" $content] != -1} {
110 $ui_diff insert end \
111 "* Binary file (not showing content)." \
115 $ui_diff insert end \
116 "* Untracked file is $sz bytes.
117 * Showing only first $max_sz bytes.
120 $ui_diff insert end $content
122 $ui_diff insert end "
123 * Untracked file clipped here by [appname].
124 * To see the entire file, use an external editor.
128 $ui_diff conf -state disabled
136 if {$w eq $ui_index} {
137 lappend cmd diff-index
139 } elseif {$w eq $ui_workdir} {
140 if {[string index $m 0] eq {U}} {
143 lappend cmd diff-files
148 lappend cmd --no-color
149 if {$repo_config(gui.diffcontext) >= 0} {
150 lappend cmd "-U$repo_config(gui.diffcontext)"
152 if {$w eq $ui_index} {
158 if {[catch {set fd [eval git_read --nice $cmd]} err]} {
161 ui_status "Unable to display [escape_path $path]"
162 error_popup "Error loading diff:\n\n$err"
170 fileevent $fd readable [list read_diff $fd]
173 proc read_diff {fd} {
174 global ui_diff diff_active
175 global is_3way_diff current_diff_header
177 $ui_diff conf -state normal
178 while {[gets $fd line] >= 0} {
179 # -- Cleanup uninteresting diff header lines.
181 if { [string match {diff --git *} $line]
182 || [string match {diff --cc *} $line]
183 || [string match {diff --combined *} $line]
184 || [string match {--- *} $line]
185 || [string match {+++ *} $line]} {
186 append current_diff_header $line "\n"
189 if {[string match {index *} $line]} continue
190 if {$line eq {deleted file mode 120000}} {
191 set line "deleted symlink"
194 # -- Automatically detect if this is a 3 way diff.
196 if {[string match {@@@ *} $line]} {set is_3way_diff 1}
198 if {[string match {mode *} $line]
199 || [string match {new file *} $line]
200 || [string match {deleted file *} $line]
201 || [string match {Binary files * and * differ} $line]
202 || $line eq {\ No newline at end of file}
203 || [regexp {^\* Unmerged path } $line]} {
205 } elseif {$is_3way_diff} {
206 set op [string range $line 0 1]
216 if {[regexp {^\+\+([<>]{7} |={7})} $line _g op]} {
217 set line [string replace $line 0 1 { }]
224 puts "error: Unhandled 3 way diff marker: {$op}"
229 set op [string index $line 0]
235 if {[regexp {^\+([<>]{7} |={7})} $line _g op]} {
236 set line [string replace $line 0 0 { }]
243 puts "error: Unhandled 2 way diff marker: {$op}"
248 $ui_diff insert end $line $tags
249 if {[string index $line end] eq "\r"} {
250 $ui_diff tag add d_cr {end - 2c}
252 $ui_diff insert end "\n" $tags
254 $ui_diff conf -state disabled
262 if {[$ui_diff index end] eq {2.0}} {
268 proc apply_hunk {x y} {
269 global current_diff_path current_diff_header current_diff_side
270 global ui_diff ui_index file_states
272 if {$current_diff_path eq {} || $current_diff_header eq {}} return
273 if {![lock_index apply_hunk]} return
275 set apply_cmd {apply --cached --whitespace=nowarn}
276 set mi [lindex $file_states($current_diff_path) 0]
277 if {$current_diff_side eq $ui_index} {
279 lappend apply_cmd --reverse
280 if {[string index $mi 0] ne {M}} {
286 if {[string index $mi 1] ne {M}} {
292 set s_lno [lindex [split [$ui_diff index @$x,$y] .] 0]
293 set s_lno [$ui_diff search -backwards -regexp ^@@ $s_lno.0 0.0]
299 set e_lno [$ui_diff search -forwards -regexp ^@@ "$s_lno + 1 lines" end]
305 set p [eval git_write $apply_cmd]
306 fconfigure $p -translation binary -encoding binary
307 puts -nonewline $p $current_diff_header
308 puts -nonewline $p [$ui_diff get $s_lno $e_lno]
310 error_popup "Failed to $mode selected hunk.\n\n$err"
315 $ui_diff conf -state normal
316 $ui_diff delete $s_lno $e_lno
317 $ui_diff conf -state disabled
319 if {[$ui_diff get 1.0 end] eq "\n"} {
325 if {$current_diff_side eq $ui_index} {
327 } elseif {[string index $mi 0] eq {_}} {
333 display_file $current_diff_path $mi