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
{}} {callback
{}}} {
73 global file_states file_lists
74 global is_3way_diff is_conflict_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 is_conflict_diff
0
96 set current_diff_path
$path
97 set current_diff_side
$w
98 set current_diff_queue
{}
99 ui_status
[mc
"Loading diff of %s..." [escape_path
$path]]
101 set cont_info
[list $scroll_pos $callback]
103 if {[string first
{U
} $m] >= 0} {
104 merge_load_stages
$path [list show_unmerged_diff
$cont_info]
105 } elseif
{$m eq
{_O
}} {
106 show_other_diff
$path $w $m $cont_info
108 start_show_diff
$cont_info
112 proc show_unmerged_diff
{cont_info
} {
113 global current_diff_path current_diff_side
114 global merge_stages ui_diff is_conflict_diff
115 global current_diff_queue
117 if {$merge_stages(2) eq
{}} {
118 set is_conflict_diff
1
119 lappend current_diff_queue
\
120 [list "LOCAL: deleted\nREMOTE:\n" d
======= \
121 [list ":1:$current_diff_path" ":3:$current_diff_path"]]
122 } elseif
{$merge_stages(3) eq
{}} {
123 set is_conflict_diff
1
124 lappend current_diff_queue
\
125 [list "REMOTE: deleted\nLOCAL:\n" d
======= \
126 [list ":1:$current_diff_path" ":2:$current_diff_path"]]
127 } elseif
{[lindex $merge_stages(1) 0] eq
{120000}
128 ||
[lindex $merge_stages(2) 0] eq
{120000}
129 ||
[lindex $merge_stages(3) 0] eq
{120000}} {
130 set is_conflict_diff
1
131 lappend current_diff_queue
\
132 [list "LOCAL:\n" d
======= \
133 [list ":1:$current_diff_path" ":2:$current_diff_path"]]
134 lappend current_diff_queue
\
135 [list "REMOTE:\n" d
======= \
136 [list ":1:$current_diff_path" ":3:$current_diff_path"]]
138 start_show_diff
$cont_info
142 advance_diff_queue
$cont_info
145 proc advance_diff_queue
{cont_info
} {
146 global current_diff_queue ui_diff
148 set item
[lindex $current_diff_queue 0]
149 set current_diff_queue
[lrange $current_diff_queue 1 end
]
151 $ui_diff conf
-state normal
152 $ui_diff insert end
[lindex $item 0] [lindex $item 1]
153 $ui_diff conf
-state disabled
155 start_show_diff
$cont_info [lindex $item 2]
158 proc show_other_diff
{path w m cont_info
} {
159 global file_states file_lists
160 global is_3way_diff diff_active repo_config
161 global ui_diff ui_index ui_workdir
162 global current_diff_path current_diff_side current_diff_header
164 # - Git won't give us the diff, there's nothing to compare to!
167 set max_sz
[expr {128 * 1024}]
170 set type
[file type
$path]
178 set content
[file readlink
$path]
179 set sz
[string length
$content]
182 set fd
[open $path r
]
185 -encoding [get_path_encoding
$path]
186 set content
[read $fd $max_sz]
188 set sz
[file size
$path]
191 error "'$type' not supported"
197 ui_status
[mc
"Unable to display %s" [escape_path
$path]]
198 error_popup
[strcat
[mc
"Error loading file:"] "\n\n$err"]
201 $ui_diff conf
-state normal
202 if {$type eq
{submodule
}} {
203 $ui_diff insert end
[append \
205 [mc
"Git Repository (subproject)"] \
207 } elseif
{![catch {set type
[exec file $path]}]} {
208 set n
[string length
$path]
209 if {[string equal
-length $n $path $type]} {
210 set type
[string range
$type $n end
]
211 regsub {^
:?
\s
*} $type {} type
213 $ui_diff insert end
"* $type\n" d_
@
215 if {[string first
"\0" $content] != -1} {
216 $ui_diff insert end
\
217 [mc
"* Binary file (not showing content)."] \
221 $ui_diff insert end
\
222 "* Untracked file is $sz bytes.
223 * Showing only first $max_sz bytes.
226 $ui_diff insert end
$content
228 $ui_diff insert end
"
229 * Untracked file clipped here by [appname].
230 * To see the entire file, use an external editor.
234 $ui_diff conf
-state disabled
237 set scroll_pos
[lindex $cont_info 0]
238 if {$scroll_pos ne
{}} {
240 $ui_diff yview moveto
$scroll_pos
243 set callback
[lindex $cont_info 1]
244 if {$callback ne
{}} {
251 proc start_show_diff
{cont_info
{add_opts
{}}} {
252 global file_states file_lists
253 global is_3way_diff diff_active repo_config
254 global ui_diff ui_index ui_workdir
255 global current_diff_path current_diff_side current_diff_header
257 set path
$current_diff_path
258 set w
$current_diff_side
260 set s
$file_states($path)
264 set current_diff_header
{}
267 if {$w eq
$ui_index} {
268 lappend cmd diff-index
270 } elseif
{$w eq
$ui_workdir} {
271 if {[string first
{U
} $m] >= 0} {
274 lappend cmd diff-files
279 lappend cmd
--no-color
280 if {$repo_config(gui.diffcontext
) >= 1} {
281 lappend cmd
"-U$repo_config(gui.diffcontext)"
283 if {$w eq
$ui_index} {
286 if {$add_opts ne
{}} {
287 eval lappend cmd
$add_opts
293 if {[catch {set fd
[eval git_read
--nice $cmd]} err
]} {
296 ui_status
[mc
"Unable to display %s" [escape_path
$path]]
297 error_popup
[strcat
[mc
"Error loading diff:"] "\n\n$err"]
301 set ::current_diff_inheader 1
304 -encoding [get_path_encoding
$path] \
306 fileevent $fd readable
[list read_diff
$fd $cont_info]
309 proc read_diff
{fd cont_info
} {
310 global ui_diff diff_active
311 global is_3way_diff is_conflict_diff current_diff_header
312 global current_diff_queue
314 $ui_diff conf
-state normal
315 while {[gets $fd line
] >= 0} {
316 # -- Cleanup uninteresting diff header lines.
318 if {$::current_diff_inheader} {
319 if { [string match
{diff
--git *} $line]
320 ||
[string match
{diff
--cc *} $line]
321 ||
[string match
{diff
--combined *} $line]
322 ||
[string match
{--- *} $line]
323 ||
[string match
{+++ *} $line]} {
324 append current_diff_header
$line "\n"
328 if {[string match
{index
*} $line]} continue
329 if {$line eq
{deleted
file mode
120000}} {
330 set line
"deleted symlink"
332 set ::current_diff_inheader 0
334 # -- Automatically detect if this is a 3 way diff.
336 if {[string match
{@@@ *} $line]} {set is_3way_diff
1}
338 if {[string match
{mode
*} $line]
339 ||
[string match
{new
file *} $line]
340 ||
[regexp {^
(old|new
) mode
*} $line]
341 ||
[string match
{deleted
file *} $line]
342 ||
[string match
{deleted symlink
} $line]
343 ||
[string match
{Binary files
* and
* differ
} $line]
344 ||
$line eq
{\ No newline at end of
file}
345 ||
[regexp {^
\* Unmerged path
} $line]} {
347 } elseif
{$is_3way_diff} {
348 set op
[string range
$line 0 1]
358 if {[regexp {^
\+\+([<>]{7} |
={7})} $line _g op
]} {
359 set is_conflict_diff
1
360 set line
[string replace
$line 0 1 { }]
367 puts "error: Unhandled 3 way diff marker: {$op}"
372 set op
[string index
$line 0]
378 if {[regexp {^
\+([<>]{7} |
={7})} $line _g op
]} {
379 set is_conflict_diff
1
380 set line
[string replace
$line 0 0 { }]
387 puts "error: Unhandled 2 way diff marker: {$op}"
392 $ui_diff insert end
$line $tags
393 if {[string index
$line end
] eq
"\r"} {
394 $ui_diff tag add d_cr
{end
- 2c
}
396 $ui_diff insert end
"\n" $tags
398 $ui_diff conf
-state disabled
403 if {$current_diff_queue ne
{}} {
404 advance_diff_queue
$cont_info
410 set scroll_pos
[lindex $cont_info 0]
411 if {$scroll_pos ne
{}} {
413 $ui_diff yview moveto
$scroll_pos
417 if {[$ui_diff index end
] eq
{2.0}} {
420 set callback
[lindex $cont_info 1]
421 if {$callback ne
{}} {
427 proc apply_hunk
{x y
} {
428 global current_diff_path current_diff_header current_diff_side
429 global ui_diff ui_index file_states
431 if {$current_diff_path eq
{} ||
$current_diff_header eq
{}} return
432 if {![lock_index apply_hunk
]} return
434 set apply_cmd
{apply
--cached --whitespace=nowarn
}
435 set mi
[lindex $file_states($current_diff_path) 0]
436 if {$current_diff_side eq
$ui_index} {
437 set failed_msg
[mc
"Failed to unstage selected hunk."]
438 lappend apply_cmd
--reverse
439 if {[string index
$mi 0] ne
{M
}} {
444 set failed_msg
[mc
"Failed to stage selected hunk."]
445 if {[string index
$mi 1] ne
{M
}} {
451 set s_lno
[lindex [split [$ui_diff index
@$x,$y] .
] 0]
452 set s_lno
[$ui_diff search
-backwards -regexp ^
@@ $s_lno.0 0.0]
458 set e_lno
[$ui_diff search
-forwards -regexp ^
@@ "$s_lno + 1 lines" end
]
464 set enc
[get_path_encoding
$current_diff_path]
465 set p
[eval git_write
$apply_cmd]
466 fconfigure $p -translation binary -encoding $enc
467 puts -nonewline $p $current_diff_header
468 puts -nonewline $p [$ui_diff get
$s_lno $e_lno]
470 error_popup
[append $failed_msg "\n\n$err"]
475 $ui_diff conf
-state normal
476 $ui_diff delete
$s_lno $e_lno
477 $ui_diff conf
-state disabled
479 if {[$ui_diff get
1.0 end
] eq
"\n"} {
485 if {$current_diff_side eq
$ui_index} {
487 } elseif
{[string index
$mi 0] eq
{_
}} {
493 display_file
$current_diff_path $mi
494 # This should trigger shift to the next changed file
500 proc apply_line
{x y
} {
501 global current_diff_path current_diff_header current_diff_side
502 global ui_diff ui_index file_states
504 if {$current_diff_path eq
{} ||
$current_diff_header eq
{}} return
505 if {![lock_index apply_hunk
]} return
507 set apply_cmd
{apply
--cached --whitespace=nowarn
}
508 set mi
[lindex $file_states($current_diff_path) 0]
509 if {$current_diff_side eq
$ui_index} {
510 set failed_msg
[mc
"Failed to unstage selected line."]
512 lappend apply_cmd
--reverse
513 if {[string index
$mi 0] ne
{M
}} {
518 set failed_msg
[mc
"Failed to stage selected line."]
520 if {[string index
$mi 1] ne
{M
}} {
526 set the_l
[$ui_diff index
@$x,$y]
528 # operate only on change lines
529 set c1
[$ui_diff get
"$the_l linestart"]
530 if {$c1 ne
{+} && $c1 ne
{-}} {
536 set i_l
[$ui_diff search
-backwards -regexp ^
@@ $the_l 0.0]
541 # $i_l is now at the beginning of a line
543 # pick start line number from hunk header
544 set hh
[$ui_diff get
$i_l "$i_l + 1 lines"]
545 set hh
[lindex [split $hh ,] 0]
546 set hln
[lindex [split $hh -] 1]
548 # There is a special situation to take care of. Consider this hunk:
558 # We used to keep the context lines in the order they appear in the
559 # hunk. But then it is not possible to correctly stage only
560 # "-old 1" and "+new 1" - it would result in this staged text:
567 # (By symmetry it is not possible to *un*stage "old 2" and "new 2".)
569 # We resolve the problem by introducing an asymmetry, namely, when
570 # a "+" line is *staged*, it is moved in front of the context lines
571 # that are generated from the "-" lines that are immediately before
572 # the "+" block. That is, we construct this patch:
581 # But we do *not* treat "-" lines that are *un*staged in a special
584 # With this asymmetry it is possible to stage the change
585 # "old 1" -> "new 1" directly, and to stage the change
586 # "old 2" -> "new 2" by first staging the entire hunk and
587 # then unstaging the change "old 1" -> "new 1".
589 # This is non-empty if and only if we are _staging_ changes;
590 # then it accumulates the consecutive "-" lines (after converting
591 # them to context lines) in order to be moved after the "+" change
596 set i_l
[$ui_diff index
"$i_l + 1 lines"]
598 while {[$ui_diff compare
$i_l < "end - 1 chars"] &&
599 [$ui_diff get
$i_l "$i_l + 2 chars"] ne
{@@}} {
600 set next_l
[$ui_diff index
"$i_l + 1 lines"]
601 set c1
[$ui_diff get
$i_l]
602 if {[$ui_diff compare
$i_l <= $the_l] &&
603 [$ui_diff compare
$the_l < $next_l]} {
604 # the line to stage/unstage
605 set ln
[$ui_diff get
$i_l $next_l]
608 set patch
"$patch$pre_context$ln"
610 set patch
"$patch$ln$pre_context"
613 } elseif
{$c1 ne
{-} && $c1 ne
{+}} {
615 set ln
[$ui_diff get
$i_l $next_l]
616 set patch
"$patch$pre_context$ln"
619 } elseif
{$c1 eq
$to_context} {
620 # turn change line into context line
621 set ln
[$ui_diff get
"$i_l + 1 chars" $next_l]
623 set pre_context
"$pre_context $ln"
625 set patch
"$patch $ln"
631 set patch
"@@ -$hln,$n +$hln,[eval expr $n $sign 1] @@\n$patch"
634 set enc
[get_path_encoding
$current_diff_path]
635 set p
[eval git_write
$apply_cmd]
636 fconfigure $p -translation binary -encoding $enc
637 puts -nonewline $p $current_diff_header
638 puts -nonewline $p $patch
640 error_popup
[append $failed_msg "\n\n$err"]