2 # Copyright (C) 2006, 2007 Shawn Pearce
4 proc apply_tab_size
{{firsttab
{}}} {
5 global have_tk85 repo_config ui_diff
7 set w
[font measure font_diff
"0"]
8 if {$have_tk85 && $firsttab != 0} {
9 $ui_diff configure
-tabs [list [expr {$firsttab * $w}] [expr {($firsttab + $repo_config(gui.tabsize
)) * $w}]]
10 } elseif
{$have_tk85 ||
$repo_config(gui.tabsize
) != 8} {
11 $ui_diff configure
-tabs [expr {$repo_config(gui.tabsize
) * $w}]
13 $ui_diff configure
-tabs {}
18 global ui_diff current_diff_path current_diff_header
19 global ui_index ui_workdir
21 $ui_diff conf
-state normal
22 $ui_diff delete
0.0 end
23 $ui_diff conf
-state disabled
25 set current_diff_path
{}
26 set current_diff_header
{}
28 $ui_index tag remove in_diff
0.0 end
29 $ui_workdir tag remove in_diff
0.0 end
32 proc reshow_diff
{{after {}}} {
33 global file_states file_lists
34 global current_diff_path current_diff_side
37 set p
$current_diff_path
39 # No diff is being shown.
40 } elseif
{$current_diff_side eq
{}} {
42 } elseif
{[catch {set s
$file_states($p)}]
43 ||
[lsearch -sorted -exact $file_lists($current_diff_side) $p] == -1} {
45 if {[find_next_diff
$current_diff_side $p {} {[^O
]}]} {
51 set save_pos
[lindex [$ui_diff yview
] 0]
52 show_diff
$p $current_diff_side {} $save_pos $after
56 proc force_diff_encoding
{enc
} {
57 global current_diff_path
59 if {$current_diff_path ne
{}} {
60 force_path_encoding
$current_diff_path $enc
65 proc handle_empty_diff
{} {
66 global current_diff_path file_states file_lists
67 global diff_empty_count
69 set path
$current_diff_path
70 set s
$file_states($path)
71 if {[lindex $s 0] ne
{_M
} ||
[has_textconv
$path]} return
73 # Prevent infinite rescan loops
75 if {$diff_empty_count > 1} return
77 info_popup
[mc
"No differences detected.
81 The modification date of this file was updated by another application, but the content within the file was not changed.
83 A rescan will be automatically started to find other files which may have the same state." [short_path
$path]]
90 proc show_diff
{path w
{lno
{}} {scroll_pos
{}} {callback
{}}} {
91 global file_states file_lists
92 global is_3way_diff is_conflict_diff diff_active repo_config
93 global ui_diff ui_index ui_workdir
94 global current_diff_path current_diff_side current_diff_header
95 global current_diff_queue
97 if {$diff_active ||
![lock_index
read]} return
101 set lno
[lsearch -sorted -exact $file_lists($w) $path]
107 $w tag add in_diff
$lno.0 [expr {$lno + 1}].0
111 set s
$file_states($path)
113 set is_conflict_diff
0
114 set current_diff_path
$path
115 set current_diff_side
$w
116 set current_diff_queue
{}
117 ui_status
[mc
"Loading diff of %s..." [escape_path
$path]]
119 set cont_info
[list $scroll_pos $callback]
123 if {[string first
{U
} $m] >= 0} {
124 merge_load_stages
$path [list show_unmerged_diff
$cont_info]
125 } elseif
{$m eq
{_O
}} {
126 show_other_diff
$path $w $m $cont_info
128 start_show_diff
$cont_info
132 proc show_unmerged_diff
{cont_info
} {
133 global current_diff_path current_diff_side
134 global merge_stages ui_diff is_conflict_diff
135 global current_diff_queue
137 if {$merge_stages(2) eq
{}} {
138 set is_conflict_diff
1
139 lappend current_diff_queue
\
140 [list [mc
"LOCAL: deleted\nREMOTE:\n"] d
= \
141 [list ":1:$current_diff_path" ":3:$current_diff_path"]]
142 } elseif
{$merge_stages(3) eq
{}} {
143 set is_conflict_diff
1
144 lappend current_diff_queue
\
145 [list [mc
"REMOTE: deleted\nLOCAL:\n"] d
= \
146 [list ":1:$current_diff_path" ":2:$current_diff_path"]]
147 } elseif
{[lindex $merge_stages(1) 0] eq
{120000}
148 ||
[lindex $merge_stages(2) 0] eq
{120000}
149 ||
[lindex $merge_stages(3) 0] eq
{120000}} {
150 set is_conflict_diff
1
151 lappend current_diff_queue
\
152 [list [mc
"LOCAL:\n"] d
= \
153 [list ":1:$current_diff_path" ":2:$current_diff_path"]]
154 lappend current_diff_queue
\
155 [list [mc
"REMOTE:\n"] d
= \
156 [list ":1:$current_diff_path" ":3:$current_diff_path"]]
158 start_show_diff
$cont_info
162 advance_diff_queue
$cont_info
165 proc advance_diff_queue
{cont_info
} {
166 global current_diff_queue ui_diff
168 set item
[lindex $current_diff_queue 0]
169 set current_diff_queue
[lrange $current_diff_queue 1 end
]
171 $ui_diff conf
-state normal
172 $ui_diff insert end
[lindex $item 0] [lindex $item 1]
173 $ui_diff conf
-state disabled
175 start_show_diff
$cont_info [lindex $item 2]
178 proc show_other_diff
{path w m cont_info
} {
179 global file_states file_lists
180 global is_3way_diff diff_active repo_config
181 global ui_diff ui_index ui_workdir
182 global current_diff_path current_diff_side current_diff_header
184 # - Git won't give us the diff, there's nothing to compare to!
190 set type
[file type
$path]
198 set content
[file readlink
$path]
199 set sz
[string length
$content]
202 set fd
[open $path r
]
205 -encoding [get_path_encoding
$path]
206 set content
[read $fd $max_sz]
208 set sz
[file size
$path]
211 error "'$type' not supported"
217 ui_status
[mc
"Unable to display %s" [escape_path
$path]]
218 error_popup
[strcat
[mc
"Error loading file:"] "\n\n$err"]
221 $ui_diff conf
-state normal
222 if {$type eq
{submodule
}} {
223 $ui_diff insert end
[append \
225 [mc
"Git Repository (subproject)"] \
227 } elseif
{![catch {set type
[exec file $path]}]} {
228 set n
[string length
$path]
229 if {[string equal
-length $n $path $type]} {
230 set type
[string range
$type $n end
]
231 regsub {^
:?
\s
*} $type {} type
233 $ui_diff insert end
"* $type\n" d_info
235 if {[string first
"\0" $content] != -1} {
236 $ui_diff insert end
\
237 [mc
"* Binary file (not showing content)."] \
241 $ui_diff insert end
[mc
\
242 "* Untracked file is %d bytes.
243 * Showing only first %d bytes.
244 " $sz $max_sz] d_info
246 $ui_diff insert end
$content
248 $ui_diff insert end
[mc
"
249 * Untracked file clipped here by %s.
250 * To see the entire file, use an external editor.
254 $ui_diff conf
-state disabled
257 set scroll_pos
[lindex $cont_info 0]
258 if {$scroll_pos ne
{}} {
260 $ui_diff yview moveto
$scroll_pos
263 set callback
[lindex $cont_info 1]
264 if {$callback ne
{}} {
271 proc get_conflict_marker_size
{path
} {
274 set fd_rc
[eval [list git_read check-attr
"conflict-marker-size" -- $path]]
275 set ret
[gets $fd_rc line
]
278 regexp {.
*: conflict-marker-size
: (\d
+)$} $line line size
284 proc start_show_diff
{cont_info
{add_opts
{}}} {
285 global file_states file_lists
286 global is_3way_diff is_submodule_diff diff_active repo_config
287 global ui_diff ui_index ui_workdir
288 global current_diff_path current_diff_side current_diff_header
290 set path
$current_diff_path
291 set w
$current_diff_side
293 set s
$file_states($path)
296 set is_submodule_diff
0
298 set current_diff_header
{}
299 set conflict_size
[get_conflict_marker_size
$path]
302 if {$w eq
$ui_index} {
303 lappend cmd diff-index
305 if {[git-version
>= "1.7.2"]} {
306 lappend cmd
--ignore-submodules
=dirty
308 } elseif
{$w eq
$ui_workdir} {
309 if {[string first
{U
} $m] >= 0} {
312 lappend cmd diff-files
315 if {![is_config_false gui.textconv
] && [git-version
>= 1.6.1]} {
316 lappend cmd
--textconv
319 if {[string match
{160000 *} [lindex $s 2]]
320 ||
[string match
{160000 *} [lindex $s 3]]} {
321 set is_submodule_diff
1
323 if {[git-version
>= "1.6.6"]} {
324 lappend cmd
--submodule
330 set cmd
[concat $cmd $repo_config(gui.diffopts
)]
331 if {$repo_config(gui.diffcontext
) >= 1} {
332 lappend cmd
"-U$repo_config(gui.diffcontext)"
334 if {$w eq
$ui_index} {
337 if {$add_opts ne
{}} {
338 eval lappend cmd
$add_opts
344 if {$is_submodule_diff && [git-version
< "1.6.6"]} {
345 if {$w eq
$ui_index} {
346 set cmd
[list submodule summary
--cached -- $path]
348 set cmd
[list submodule summary
--files -- $path]
352 if {[catch {set fd
[eval git_read
--nice $cmd]} err
]} {
355 ui_status
[mc
"Unable to display %s" [escape_path
$path]]
356 error_popup
[strcat
[mc
"Error loading diff:"] "\n\n$err"]
360 set ::current_diff_inheader 1
363 -encoding [get_path_encoding
$path] \
365 fileevent $fd readable
[list read_diff
$fd $conflict_size $cont_info]
368 proc parse_color_line
{line
} {
372 set regexp {\033\[((?
:\d
+;)*\d
+)?m
}
374 while {[regexp -indices -start $start $regexp $line match code
]} {
375 foreach {begin end
} $match break
376 append result
[string range
$line $start [expr {$begin - 1}]]
377 set pos
[string length
$result]
378 set col
[eval [linsert $code 0 string range
$line]]
380 if {$col eq
"0" ||
$col eq
""} {
381 if {!$need_reset} continue
386 lappend markup
$pos $col
388 append result
[string range
$line $start end
]
389 if {[llength $markup] < 4} {set markup
{}}
390 return [list $result $markup]
393 proc read_diff
{fd conflict_size cont_info
} {
394 global ui_diff diff_active is_submodule_diff
395 global is_3way_diff is_conflict_diff current_diff_header
396 global current_diff_queue
397 global diff_empty_count
399 $ui_diff conf
-state normal
400 while {[gets $fd line
] >= 0} {
401 foreach {line markup
} [parse_color_line
$line] break
402 set line
[string map
{\033 ^
} $line]
406 # -- Check for start of diff header.
407 if { [string match
{diff
--git *} $line]
408 ||
[string match
{diff
--cc *} $line]
409 ||
[string match
{diff
--combined *} $line]} {
410 set ::current_diff_inheader 1
413 # -- Check for end of diff header (any hunk line will do this).
415 if {[regexp {^
@@+ } $line]} {set ::current_diff_inheader 0}
417 # -- Automatically detect if this is a 3 way diff.
419 if {[string match
{@@@ *} $line]} {
424 if {$::current_diff_inheader} {
426 # -- These two lines stop a diff header and shouldn't be in there
427 if { [string match
{Binary files
* and
* differ
} $line]
428 ||
[regexp {^
\* Unmerged path
} $line]} {
429 set ::current_diff_inheader 0
431 append current_diff_header
$line "\n"
434 # -- Cleanup uninteresting diff header lines.
436 if { [string match
{diff
--git *} $line]
437 ||
[string match
{diff
--cc *} $line]
438 ||
[string match
{diff
--combined *} $line]
439 ||
[string match
{--- *} $line]
440 ||
[string match
{+++ *} $line]
441 ||
[string match
{index
*} $line]} {
445 # -- Name it symlink, not 120000
446 # Note, that the original line is in $current_diff_header
447 regsub {^
(deleted|new
) file mode
120000} $line {\1 symlink
} line
449 } elseif
{ $line eq
{\ No newline at end of
file}} {
450 # -- Handle some special lines
451 } elseif
{$is_3way_diff} {
452 set op
[string range
$line 0 1]
462 set regexp [string map
[list %conflict_size
$conflict_size]\
463 {^
\+\+([<>=]){%conflict_size
}(?
: |
$)}]
464 if {[regexp $regexp $line _g op
]} {
465 set is_conflict_diff
1
466 set line
[string replace
$line 0 1 { }]
473 puts "error: Unhandled 3 way diff marker: {$op}"
477 } elseif
{$is_submodule_diff} {
478 if {$line == ""} continue
479 if {[regexp {^Submodule
} $line]} {
481 } elseif
{[regexp {^
\* } $line]} {
482 set line
[string replace
$line 0 1 {Submodule
}]
485 set op
[string range
$line 0 2]
491 puts "error: Unhandled submodule diff marker: {$op}"
497 set op
[string index
$line 0]
503 set regexp [string map
[list %conflict_size
$conflict_size]\
504 {^
\+([<>=]){%conflict_size
}(?
: |
$)}]
505 if {[regexp $regexp $line _g op
]} {
506 set is_conflict_diff
1
513 puts "error: Unhandled 2 way diff marker: {$op}"
518 set mark
[$ui_diff index
"end - 1 line linestart"]
519 $ui_diff insert end
$line $tags
520 if {[string index
$line end
] eq
"\r"} {
521 $ui_diff tag add d_cr
{end
- 2c
}
523 $ui_diff insert end
"\n" $tags
525 foreach {posbegin colbegin posend colend
} $markup {
527 foreach style
[lsort -integer [split $colbegin ";"]] {
528 if {$style eq
"7"} {append prefix i
; continue}
529 if {$style != 4 && ($style < 30 ||
$style > 47)} {continue}
530 set a
"$mark linestart + $posbegin chars"
531 set b
"$mark linestart + $posend chars"
532 catch {$ui_diff tag add
$prefix$style $a $b}
536 $ui_diff conf
-state disabled
541 if {$current_diff_queue ne
{}} {
542 advance_diff_queue
$cont_info
548 set scroll_pos
[lindex $cont_info 0]
549 if {$scroll_pos ne
{}} {
551 $ui_diff yview moveto
$scroll_pos
555 if {[$ui_diff index end
] eq
{2.0}} {
558 set diff_empty_count
0
561 set callback
[lindex $cont_info 1]
562 if {$callback ne
{}} {
568 proc apply_hunk
{x y
} {
569 global current_diff_path current_diff_header current_diff_side
570 global ui_diff ui_index file_states
572 if {$current_diff_path eq
{} ||
$current_diff_header eq
{}} return
573 if {![lock_index apply_hunk
]} return
575 set apply_cmd
{apply
--cached --whitespace=nowarn
}
576 set mi
[lindex $file_states($current_diff_path) 0]
577 if {$current_diff_side eq
$ui_index} {
578 set failed_msg
[mc
"Failed to unstage selected hunk."]
579 lappend apply_cmd
--reverse
580 if {[string index
$mi 0] ne
{M
}} {
585 set failed_msg
[mc
"Failed to stage selected hunk."]
586 if {[string index
$mi 1] ne
{M
}} {
592 set s_lno
[lindex [split [$ui_diff index
@$x,$y] .
] 0]
593 set s_lno
[$ui_diff search
-backwards -regexp ^
@@ $s_lno.0 0.0]
599 set e_lno
[$ui_diff search
-forwards -regexp ^
@@ "$s_lno + 1 lines" end
]
605 set enc
[get_path_encoding
$current_diff_path]
606 set p
[eval git_write
$apply_cmd]
607 fconfigure $p -translation binary -encoding $enc
608 puts -nonewline $p $current_diff_header
609 puts -nonewline $p [$ui_diff get
$s_lno $e_lno]
611 error_popup
[append $failed_msg "\n\n$err"]
616 $ui_diff conf
-state normal
617 $ui_diff delete
$s_lno $e_lno
618 $ui_diff conf
-state disabled
620 if {[$ui_diff get
1.0 end
] eq
"\n"} {
626 if {$current_diff_side eq
$ui_index} {
628 } elseif
{[string index
$mi 0] eq
{_
}} {
634 display_file
$current_diff_path $mi
635 # This should trigger shift to the next changed file
641 proc apply_range_or_line
{x y
} {
642 global current_diff_path current_diff_header current_diff_side
643 global ui_diff ui_index file_states
645 set selected
[$ui_diff tag nextrange sel
0.0]
647 if {$selected == {}} {
648 set first
[$ui_diff index
"@$x,$y"]
651 set first
[lindex $selected 0]
652 set last
[lindex $selected 1]
655 set first_l
[$ui_diff index
"$first linestart"]
656 set last_l
[$ui_diff index
"$last lineend"]
658 if {$current_diff_path eq
{} ||
$current_diff_header eq
{}} return
659 if {![lock_index apply_hunk
]} return
661 set apply_cmd
{apply
--cached --whitespace=nowarn
}
662 set mi
[lindex $file_states($current_diff_path) 0]
663 if {$current_diff_side eq
$ui_index} {
664 set failed_msg
[mc
"Failed to unstage selected line."]
666 lappend apply_cmd
--reverse
667 if {[string index
$mi 0] ne
{M
}} {
672 set failed_msg
[mc
"Failed to stage selected line."]
674 if {[string index
$mi 1] ne
{M
}} {
682 while {$first_l < $last_l} {
683 set i_l
[$ui_diff search
-backwards -regexp ^
@@ $first_l 0.0]
685 # If there's not a @@ above, then the selected range
686 # must have come before the first_l @@
687 set i_l
[$ui_diff search
-regexp ^
@@ $first_l $last_l]
693 # $i_l is now at the beginning of a line
695 # pick start line number from hunk header
696 set hh
[$ui_diff get
$i_l "$i_l + 1 lines"]
697 set hh
[lindex [split $hh ,] 0]
698 set hln
[lindex [split $hh -] 1]
700 # There is a special situation to take care of. Consider this
711 # We used to keep the context lines in the order they appear in
712 # the hunk. But then it is not possible to correctly stage only
713 # "-old 1" and "+new 1" - it would result in this staged text:
720 # (By symmetry it is not possible to *un*stage "old 2" and "new
723 # We resolve the problem by introducing an asymmetry, namely,
724 # when a "+" line is *staged*, it is moved in front of the
725 # context lines that are generated from the "-" lines that are
726 # immediately before the "+" block. That is, we construct this
736 # But we do *not* treat "-" lines that are *un*staged in a
739 # With this asymmetry it is possible to stage the change "old
740 # 1" -> "new 1" directly, and to stage the change "old 2" ->
741 # "new 2" by first staging the entire hunk and then unstaging
742 # the change "old 1" -> "new 1".
744 # Applying multiple lines adds complexity to the special
745 # situation. The pre_context must be moved after the entire
746 # first block of consecutive staged "+" lines, so that
747 # staging both additions gives the following patch:
757 # This is non-empty if and only if we are _staging_ changes;
758 # then it accumulates the consecutive "-" lines (after
759 # converting them to context lines) in order to be moved after
765 set i_l
[$ui_diff index
"$i_l + 1 lines"]
767 while {[$ui_diff compare
$i_l < "end - 1 chars"] &&
768 [$ui_diff get
$i_l "$i_l + 2 chars"] ne
{@@}} {
769 set next_l
[$ui_diff index
"$i_l + 1 lines"]
770 set c1
[$ui_diff get
$i_l]
771 if {[$ui_diff compare
$first_l <= $i_l] &&
772 [$ui_diff compare
$i_l < $last_l] &&
773 ($c1 eq
{-} ||
$c1 eq
{+})} {
774 # a line to stage/unstage
775 set ln
[$ui_diff get
$i_l $next_l]
778 set patch
"$patch$pre_context$ln"
782 set patch
"$patch$ln"
784 } elseif
{$c1 ne
{-} && $c1 ne
{+}} {
786 set ln
[$ui_diff get
$i_l $next_l]
787 set patch
"$patch$pre_context$ln"
788 # Skip the "\ No newline at end of
789 # file". Depending on the locale setting
790 # we don't know what this line looks
791 # like exactly. The only thing we do
792 # know is that it starts with "\ "
793 if {![string match
{\\ *} $ln]} {
798 } elseif
{$c1 eq
$to_context} {
799 # turn change line into context line
800 set ln
[$ui_diff get
"$i_l + 1 chars" $next_l]
802 set pre_context
"$pre_context $ln"
804 set patch
"$patch $ln"
809 # a change in the opposite direction of
810 # to_context which is outside the range of
812 set patch
"$patch$pre_context"
817 set patch
"$patch$pre_context"
818 set wholepatch
"$wholepatch@@ -$hln,$n +$hln,$m @@\n$patch"
819 set first_l
[$ui_diff index
"$next_l + 1 lines"]
823 set enc
[get_path_encoding
$current_diff_path]
824 set p
[eval git_write
$apply_cmd]
825 fconfigure $p -translation binary -encoding $enc
826 puts -nonewline $p $current_diff_header
827 puts -nonewline $p $wholepatch
829 error_popup
[append $failed_msg "\n\n$err"]