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
[mc
"No differences detected.
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." [short_path
$path]]
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
[mc
"Loading diff of %s..." [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
[mc
"Unable to display %s" [escape_path
$path]]
115 error_popup
[strcat
[mc
"Error loading file:"] "\n\n$err"]
118 $ui_diff conf
-state normal
119 if {$type eq
{submodule
}} {
120 $ui_diff insert end
[append \
122 [mc
"Git Repository (subproject)"] \
124 } elseif
{![catch {set type
[exec file $path]}]} {
125 set n
[string length
$path]
126 if {[string equal
-length $n $path $type]} {
127 set type
[string range
$type $n end
]
128 regsub {^
:?
\s
*} $type {} type
130 $ui_diff insert end
"* $type\n" d_
@
132 if {[string first
"\0" $content] != -1} {
133 $ui_diff insert end
\
134 [mc
"* Binary file (not showing content)."] \
138 $ui_diff insert end
\
139 "* Untracked file is $sz bytes.
140 * Showing only first $max_sz bytes.
143 $ui_diff insert end
$content
145 $ui_diff insert end
"
146 * Untracked file clipped here by [appname].
147 * To see the entire file, use an external editor.
151 $ui_diff conf
-state disabled
159 if {$w eq
$ui_index} {
160 lappend cmd diff-index
162 } elseif
{$w eq
$ui_workdir} {
163 if {[string index
$m 0] eq
{U
}} {
166 lappend cmd diff-files
171 lappend cmd
--no-color
172 if {$repo_config(gui.diffcontext
) >= 0} {
173 lappend cmd
"-U$repo_config(gui.diffcontext)"
175 if {$w eq
$ui_index} {
181 if {[catch {set fd
[eval git_read
--nice $cmd]} err
]} {
184 ui_status
[mc
"Unable to display %s" [escape_path
$path]]
185 error_popup
[strcat
[mc
"Error loading diff:"] "\n\n$err"]
193 fileevent $fd readable
[list read_diff
$fd]
196 proc read_diff
{fd
} {
197 global ui_diff diff_active
198 global is_3way_diff current_diff_header
200 $ui_diff conf
-state normal
201 while {[gets $fd line
] >= 0} {
202 # -- Cleanup uninteresting diff header lines.
204 if { [string match
{diff
--git *} $line]
205 ||
[string match
{diff
--cc *} $line]
206 ||
[string match
{diff
--combined *} $line]
207 ||
[string match
{--- *} $line]
208 ||
[string match
{+++ *} $line]} {
209 append current_diff_header
$line "\n"
212 if {[string match
{index
*} $line]} continue
213 if {$line eq
{deleted
file mode
120000}} {
214 set line
"deleted symlink"
217 # -- Automatically detect if this is a 3 way diff.
219 if {[string match
{@@@ *} $line]} {set is_3way_diff
1}
221 if {[string match
{mode
*} $line]
222 ||
[string match
{new
file *} $line]
223 ||
[regexp {^
(old|new
) mode
*} $line]
224 ||
[string match
{deleted
file *} $line]
225 ||
[string match
{deleted symlink
} $line]
226 ||
[string match
{Binary files
* and
* differ
} $line]
227 ||
$line eq
{\ No newline at end of
file}
228 ||
[regexp {^
\* Unmerged path
} $line]} {
230 } elseif
{$is_3way_diff} {
231 set op
[string range
$line 0 1]
241 if {[regexp {^
\+\+([<>]{7} |
={7})} $line _g op
]} {
242 set line
[string replace
$line 0 1 { }]
249 puts "error: Unhandled 3 way diff marker: {$op}"
254 set op
[string index
$line 0]
260 if {[regexp {^
\+([<>]{7} |
={7})} $line _g op
]} {
261 set line
[string replace
$line 0 0 { }]
268 puts "error: Unhandled 2 way diff marker: {$op}"
273 $ui_diff insert end
$line $tags
274 if {[string index
$line end
] eq
"\r"} {
275 $ui_diff tag add d_cr
{end
- 2c
}
277 $ui_diff insert end
"\n" $tags
279 $ui_diff conf
-state disabled
287 if {[$ui_diff index end
] eq
{2.0}} {
293 proc apply_hunk
{x y
} {
294 global current_diff_path current_diff_header current_diff_side
295 global ui_diff ui_index file_states
297 if {$current_diff_path eq
{} ||
$current_diff_header eq
{}} return
298 if {![lock_index apply_hunk
]} return
300 set apply_cmd
{apply
--cached --whitespace=nowarn
}
301 set mi
[lindex $file_states($current_diff_path) 0]
302 if {$current_diff_side eq
$ui_index} {
303 set failed_msg
[mc
"Failed to unstage selected hunk."]
304 lappend apply_cmd
--reverse
305 if {[string index
$mi 0] ne
{M
}} {
310 set failed_msg
[mc
"Failed to stage selected hunk."]
311 if {[string index
$mi 1] ne
{M
}} {
317 set s_lno
[lindex [split [$ui_diff index
@$x,$y] .
] 0]
318 set s_lno
[$ui_diff search
-backwards -regexp ^
@@ $s_lno.0 0.0]
324 set e_lno
[$ui_diff search
-forwards -regexp ^
@@ "$s_lno + 1 lines" end
]
330 set p
[eval git_write
$apply_cmd]
331 fconfigure $p -translation binary -encoding binary
332 puts -nonewline $p $current_diff_header
333 puts -nonewline $p [$ui_diff get
$s_lno $e_lno]
335 error_popup
[append $failed_msg "\n\n$err"]
340 $ui_diff conf
-state normal
341 $ui_diff delete
$s_lno $e_lno
342 $ui_diff conf
-state disabled
344 if {[$ui_diff get
1.0 end
] eq
"\n"} {
350 if {$current_diff_side eq
$ui_index} {
352 } elseif
{[string index
$mi 0] eq
{_
}} {
358 display_file
$current_diff_path $mi
362 set current_diff_path
$current_diff_path
366 proc apply_line
{x y
} {
367 global current_diff_path current_diff_header current_diff_side
368 global ui_diff ui_index file_states
370 if {$current_diff_path eq
{} ||
$current_diff_header eq
{}} return
371 if {![lock_index apply_hunk
]} return
373 set apply_cmd
{apply
--cached --whitespace=nowarn
}
374 set mi
[lindex $file_states($current_diff_path) 0]
375 if {$current_diff_side eq
$ui_index} {
376 set failed_msg
[mc
"Failed to unstage selected line."]
378 lappend apply_cmd
--reverse
379 if {[string index
$mi 0] ne
{M
}} {
384 set failed_msg
[mc
"Failed to stage selected line."]
386 if {[string index
$mi 1] ne
{M
}} {
392 set the_l
[$ui_diff index
@$x,$y]
394 # operate only on change lines
395 set c1
[$ui_diff get
"$the_l linestart"]
396 if {$c1 ne
{+} && $c1 ne
{-}} {
402 set i_l
[$ui_diff search
-backwards -regexp ^
@@ $the_l 0.0]
407 # $i_l is now at the beginning of a line
409 # pick start line number from hunk header
410 set hh
[$ui_diff get
$i_l "$i_l + 1 lines"]
411 set hh
[lindex [split $hh ,] 0]
412 set hln
[lindex [split $hh -] 1]
415 set i_l
[$ui_diff index
"$i_l + 1 lines"]
417 while {[$ui_diff compare
$i_l < "end - 1 chars"] &&
418 [$ui_diff get
$i_l "$i_l + 2 chars"] ne
{@@}} {
419 set next_l
[$ui_diff index
"$i_l + 1 lines"]
420 set c1
[$ui_diff get
$i_l]
421 if {[$ui_diff compare
$i_l <= $the_l] &&
422 [$ui_diff compare
$the_l < $next_l]} {
423 # the line to stage/unstage
424 set ln
[$ui_diff get
$i_l $next_l]
425 set patch
"$patch$ln"
426 } elseif
{$c1 ne
{-} && $c1 ne
{+}} {
428 set ln
[$ui_diff get
$i_l $next_l]
429 set patch
"$patch$ln"
431 } elseif
{$c1 eq
$to_context} {
432 # turn change line into context line
433 set ln
[$ui_diff get
"$i_l + 1 chars" $next_l]
434 set patch
"$patch $ln"
439 set patch
"@@ -$hln,$n +$hln,[eval expr $n $sign 1] @@\n$patch"
442 set p
[eval git_write
$apply_cmd]
443 fconfigure $p -translation binary -encoding binary
444 puts -nonewline $p $current_diff_header
445 puts -nonewline $p $patch
447 error_popup
[append $failed_msg "\n\n$err"]