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 set type
[file type
$path]
97 set content
[file readlink
$path]
98 set sz
[string length
$content]
101 set fd
[open $path r
]
102 fconfigure $fd -eofchar {}
103 set content
[read $fd $max_sz]
105 set sz
[file size
$path]
108 error "'$type' not supported"
114 ui_status
"Unable to display [escape_path $path]"
115 error_popup
"Error loading file:\n\n$err"
118 $ui_diff conf
-state normal
119 if {$type eq
{submodule
}} {
120 $ui_diff insert end
"* Git Repository (subproject)\n" d_
@
121 } elseif
{![catch {set type
[exec file $path]}]} {
122 set n
[string length
$path]
123 if {[string equal
-length $n $path $type]} {
124 set type
[string range
$type $n end
]
125 regsub {^
:?
\s
*} $type {} type
127 $ui_diff insert end
"* $type\n" d_
@
129 if {[string first
"\0" $content] != -1} {
130 $ui_diff insert end
\
131 "* Binary file (not showing content)." \
135 $ui_diff insert end
\
136 "* Untracked file is $sz bytes.
137 * Showing only first $max_sz bytes.
140 $ui_diff insert end
$content
142 $ui_diff insert end
"
143 * Untracked file clipped here by [appname].
144 * To see the entire file, use an external editor.
148 $ui_diff conf
-state disabled
156 if {$w eq
$ui_index} {
157 lappend cmd diff-index
159 } elseif
{$w eq
$ui_workdir} {
160 if {[string index
$m 0] eq
{U
}} {
163 lappend cmd diff-files
168 lappend cmd
--no-color
169 if {$repo_config(gui.diffcontext
) >= 0} {
170 lappend cmd
"-U$repo_config(gui.diffcontext)"
172 if {$w eq
$ui_index} {
178 if {[catch {set fd
[eval git_read
--nice $cmd]} err
]} {
181 ui_status
"Unable to display [escape_path $path]"
182 error_popup
"Error loading diff:\n\n$err"
190 fileevent $fd readable
[list read_diff
$fd]
193 proc read_diff
{fd
} {
194 global ui_diff diff_active
195 global is_3way_diff current_diff_header
197 $ui_diff conf
-state normal
198 while {[gets $fd line
] >= 0} {
199 # -- Cleanup uninteresting diff header lines.
201 if { [string match
{diff
--git *} $line]
202 ||
[string match
{diff
--cc *} $line]
203 ||
[string match
{diff
--combined *} $line]
204 ||
[string match
{--- *} $line]
205 ||
[string match
{+++ *} $line]} {
206 append current_diff_header
$line "\n"
209 if {[string match
{index
*} $line]} continue
210 if {$line eq
{deleted
file mode
120000}} {
211 set line
"deleted symlink"
214 # -- Automatically detect if this is a 3 way diff.
216 if {[string match
{@@@ *} $line]} {set is_3way_diff
1}
218 if {[string match
{mode
*} $line]
219 ||
[string match
{new
file *} $line]
220 ||
[string match
{deleted
file *} $line]
221 ||
[string match
{deleted symlink
} $line]
222 ||
[string match
{Binary files
* and
* differ
} $line]
223 ||
$line eq
{\ No newline at end of
file}
224 ||
[regexp {^
\* Unmerged path
} $line]} {
226 } elseif
{$is_3way_diff} {
227 set op
[string range
$line 0 1]
237 if {[regexp {^
\+\+([<>]{7} |
={7})} $line _g op
]} {
238 set line
[string replace
$line 0 1 { }]
245 puts "error: Unhandled 3 way diff marker: {$op}"
250 set op
[string index
$line 0]
256 if {[regexp {^
\+([<>]{7} |
={7})} $line _g op
]} {
257 set line
[string replace
$line 0 0 { }]
264 puts "error: Unhandled 2 way diff marker: {$op}"
269 $ui_diff insert end
$line $tags
270 if {[string index
$line end
] eq
"\r"} {
271 $ui_diff tag add d_cr
{end
- 2c
}
273 $ui_diff insert end
"\n" $tags
275 $ui_diff conf
-state disabled
283 if {[$ui_diff index end
] eq
{2.0}} {
289 proc apply_hunk
{x y
} {
290 global current_diff_path current_diff_header current_diff_side
291 global ui_diff ui_index file_states
293 if {$current_diff_path eq
{} ||
$current_diff_header eq
{}} return
294 if {![lock_index apply_hunk
]} return
296 set apply_cmd
{apply
--cached --whitespace=nowarn
}
297 set mi
[lindex $file_states($current_diff_path) 0]
298 if {$current_diff_side eq
$ui_index} {
300 lappend apply_cmd
--reverse
301 if {[string index
$mi 0] ne
{M
}} {
307 if {[string index
$mi 1] ne
{M
}} {
313 set s_lno
[lindex [split [$ui_diff index
@$x,$y] .
] 0]
314 set s_lno
[$ui_diff search
-backwards -regexp ^
@@ $s_lno.0 0.0]
320 set e_lno
[$ui_diff search
-forwards -regexp ^
@@ "$s_lno + 1 lines" end
]
326 set p
[eval git_write
$apply_cmd]
327 fconfigure $p -translation binary -encoding binary
328 puts -nonewline $p $current_diff_header
329 puts -nonewline $p [$ui_diff get
$s_lno $e_lno]
331 error_popup
"Failed to $mode selected hunk.\n\n$err"
336 $ui_diff conf
-state normal
337 $ui_diff delete
$s_lno $e_lno
338 $ui_diff conf
-state disabled
340 if {[$ui_diff get
1.0 end
] eq
"\n"} {
346 if {$current_diff_side eq
$ui_index} {
348 } elseif
{[string index
$mi 0] eq
{_
}} {
354 display_file
$current_diff_path $mi