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
19 proc reshow_diff
{{after {}}} {
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 $after
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
54 global diff_empty_count
56 set path
$current_diff_path
57 set s
$file_states($path)
58 if {[lindex $s 0] ne
{_M
} ||
[has_textconv
$path]} return
60 # Prevent infinite rescan loops
62 if {$diff_empty_count > 1} return
64 info_popup
[mc
"No differences detected.
68 The modification date of this file was updated by another application, but the content within the file was not changed.
70 A rescan will be automatically started to find other files which may have the same state." [short_path
$path]]
77 proc show_diff
{path w
{lno
{}} {scroll_pos
{}} {callback
{}}} {
78 global file_states file_lists
79 global is_3way_diff is_conflict_diff diff_active repo_config
80 global ui_diff ui_index ui_workdir
81 global current_diff_path current_diff_side current_diff_header
82 global current_diff_queue
84 if {$diff_active ||
![lock_index
read]} return
88 set lno
[lsearch -sorted -exact $file_lists($w) $path]
94 $w tag add in_diff
$lno.0 [expr {$lno + 1}].0
98 set s
$file_states($path)
100 set is_conflict_diff
0
101 set current_diff_path
$path
102 set current_diff_side
$w
103 set current_diff_queue
{}
104 ui_status
[mc
"Loading diff of %s..." [escape_path
$path]]
106 set cont_info
[list $scroll_pos $callback]
108 if {[string first
{U
} $m] >= 0} {
109 merge_load_stages
$path [list show_unmerged_diff
$cont_info]
110 } elseif
{$m eq
{_O
}} {
111 show_other_diff
$path $w $m $cont_info
113 start_show_diff
$cont_info
117 proc show_unmerged_diff
{cont_info
} {
118 global current_diff_path current_diff_side
119 global merge_stages ui_diff is_conflict_diff
120 global current_diff_queue
122 if {$merge_stages(2) eq
{}} {
123 set is_conflict_diff
1
124 lappend current_diff_queue
\
125 [list [mc
"LOCAL: deleted\nREMOTE:\n"] d
= \
126 [list ":1:$current_diff_path" ":3:$current_diff_path"]]
127 } elseif
{$merge_stages(3) eq
{}} {
128 set is_conflict_diff
1
129 lappend current_diff_queue
\
130 [list [mc
"REMOTE: deleted\nLOCAL:\n"] d
= \
131 [list ":1:$current_diff_path" ":2:$current_diff_path"]]
132 } elseif
{[lindex $merge_stages(1) 0] eq
{120000}
133 ||
[lindex $merge_stages(2) 0] eq
{120000}
134 ||
[lindex $merge_stages(3) 0] eq
{120000}} {
135 set is_conflict_diff
1
136 lappend current_diff_queue
\
137 [list [mc
"LOCAL:\n"] d
= \
138 [list ":1:$current_diff_path" ":2:$current_diff_path"]]
139 lappend current_diff_queue
\
140 [list [mc
"REMOTE:\n"] d
= \
141 [list ":1:$current_diff_path" ":3:$current_diff_path"]]
143 start_show_diff
$cont_info
147 advance_diff_queue
$cont_info
150 proc advance_diff_queue
{cont_info
} {
151 global current_diff_queue ui_diff
153 set item
[lindex $current_diff_queue 0]
154 set current_diff_queue
[lrange $current_diff_queue 1 end
]
156 $ui_diff conf
-state normal
157 $ui_diff insert end
[lindex $item 0] [lindex $item 1]
158 $ui_diff conf
-state disabled
160 start_show_diff
$cont_info [lindex $item 2]
163 proc show_other_diff
{path w m cont_info
} {
164 global file_states file_lists
165 global is_3way_diff diff_active repo_config
166 global ui_diff ui_index ui_workdir
167 global current_diff_path current_diff_side current_diff_header
169 # - Git won't give us the diff, there's nothing to compare to!
175 set type
[file type
$path]
183 set content
[file readlink
$path]
184 set sz
[string length
$content]
187 set fd
[open $path r
]
190 -encoding [get_path_encoding
$path]
191 set content
[read $fd $max_sz]
193 set sz
[file size
$path]
196 error "'$type' not supported"
202 ui_status
[mc
"Unable to display %s" [escape_path
$path]]
203 error_popup
[strcat
[mc
"Error loading file:"] "\n\n$err"]
206 $ui_diff conf
-state normal
207 if {$type eq
{submodule
}} {
208 $ui_diff insert end
[append \
210 [mc
"Git Repository (subproject)"] \
212 } elseif
{![catch {set type
[exec file $path]}]} {
213 set n
[string length
$path]
214 if {[string equal
-length $n $path $type]} {
215 set type
[string range
$type $n end
]
216 regsub {^
:?
\s
*} $type {} type
218 $ui_diff insert end
"* $type\n" d_info
220 if {[string first
"\0" $content] != -1} {
221 $ui_diff insert end
\
222 [mc
"* Binary file (not showing content)."] \
226 $ui_diff insert end
[mc
\
227 "* Untracked file is %d bytes.
228 * Showing only first %d bytes.
229 " $sz $max_sz] d_info
231 $ui_diff insert end
$content
233 $ui_diff insert end
[mc
"
234 * Untracked file clipped here by %s.
235 * To see the entire file, use an external editor.
239 $ui_diff conf
-state disabled
242 set scroll_pos
[lindex $cont_info 0]
243 if {$scroll_pos ne
{}} {
245 $ui_diff yview moveto
$scroll_pos
248 set callback
[lindex $cont_info 1]
249 if {$callback ne
{}} {
256 proc start_show_diff
{cont_info
{add_opts
{}}} {
257 global file_states file_lists
258 global is_3way_diff is_submodule_diff diff_active repo_config
259 global ui_diff ui_index ui_workdir
260 global current_diff_path current_diff_side current_diff_header
262 set path
$current_diff_path
263 set w
$current_diff_side
265 set s
$file_states($path)
268 set is_submodule_diff
0
270 set current_diff_header
{}
271 set conflict_size
[gitattr
$path conflict-marker-size
7]
274 if {$w eq
$ui_index} {
275 lappend cmd diff-index
277 } elseif
{$w eq
$ui_workdir} {
278 if {[string first
{U
} $m] >= 0} {
281 lappend cmd diff-files
284 if {![is_config_false gui.textconv
] && [git-version
>= 1.6.1]} {
285 lappend cmd
--textconv
288 if {[string match
{160000 *} [lindex $s 2]]
289 ||
[string match
{160000 *} [lindex $s 3]]} {
290 set is_submodule_diff
1
292 if {[git-version
>= "1.6.6"]} {
293 lappend cmd
--submodule
299 set cmd
[concat $cmd $repo_config(gui.diffopts
)]
300 if {$repo_config(gui.diffcontext
) >= 1} {
301 lappend cmd
"-U$repo_config(gui.diffcontext)"
303 if {$w eq
$ui_index} {
306 if {$add_opts ne
{}} {
307 eval lappend cmd
$add_opts
313 if {$is_submodule_diff && [git-version
< "1.6.6"]} {
314 if {$w eq
$ui_index} {
315 set cmd
[list submodule summary
--cached -- $path]
317 set cmd
[list submodule summary
--files -- $path]
321 if {[catch {set fd
[eval git_read
--nice $cmd]} err
]} {
324 ui_status
[mc
"Unable to display %s" [escape_path
$path]]
325 error_popup
[strcat
[mc
"Error loading diff:"] "\n\n$err"]
329 set ::current_diff_inheader 1
330 set ::conflict_state {CONTEXT
}
333 -encoding [get_path_encoding
$path] \
335 fileevent $fd readable
[list read_diff
$fd $conflict_size $cont_info]
338 proc parse_color_line
{line
} {
342 set regexp {\033\[((?
:\d
+;)*\d
+)?m
}
344 while {[regexp -indices -start $start $regexp $line match code
]} {
345 foreach {begin end
} $match break
346 append result
[string range
$line $start [expr {$begin - 1}]]
347 set pos
[string length
$result]
348 set col
[eval [linsert $code 0 string range
$line]]
350 if {$col eq
"0" ||
$col eq
""} {
351 if {!$need_reset} continue
356 lappend markup
$pos $col
358 append result
[string range
$line $start end
]
359 if {[llength $markup] < 4} {set markup
{}}
360 return [list $result $markup]
363 proc read_diff
{fd conflict_size cont_info
} {
364 global ui_diff diff_active is_submodule_diff
365 global is_3way_diff is_conflict_diff current_diff_header
366 global current_diff_queue
367 global diff_empty_count
369 $ui_diff conf
-state normal
370 while {[gets $fd line
] >= 0} {
371 foreach {line markup
} [parse_color_line
$line] break
372 set line
[string map
{\033 ^
} $line]
376 # -- Check for start of diff header.
377 if { [string match
{diff
--git *} $line]
378 ||
[string match
{diff
--cc *} $line]
379 ||
[string match
{diff
--combined *} $line]} {
380 set ::current_diff_inheader 1
383 # -- Check for end of diff header (any hunk line will do this).
385 if {[regexp {^
@@+ } $line]} {set ::current_diff_inheader 0}
387 # -- Automatically detect if this is a 3 way diff.
389 if {[string match
{@@@ *} $line]} {set is_3way_diff
1}
391 if {$::current_diff_inheader} {
393 # -- These two lines stop a diff header and shouldn't be in there
394 if { [string match
{Binary files
* and
* differ
} $line]
395 ||
[regexp {^
\* Unmerged path
} $line]} {
396 set ::current_diff_inheader 0
398 append current_diff_header
$line "\n"
401 # -- Cleanup uninteresting diff header lines.
403 if { [string match
{diff
--git *} $line]
404 ||
[string match
{diff
--cc *} $line]
405 ||
[string match
{diff
--combined *} $line]
406 ||
[string match
{--- *} $line]
407 ||
[string match
{+++ *} $line]
408 ||
[string match
{index
*} $line]} {
412 # -- Name it symlink, not 120000
413 # Note, that the original line is in $current_diff_header
414 regsub {^
(deleted|new
) file mode
120000} $line {\1 symlink
} line
416 } elseif
{ $line eq
{\ No newline at end of
file}} {
417 # -- Handle some special lines
418 } elseif
{$is_3way_diff} {
419 set op
[string range
$line 0 1]
429 set regexp [string map
[list %conflict_size
$conflict_size]\
430 {^
\+\+([<>=]){%conflict_size
}(?
: |
$)}]
431 set regexp_pre_image
[string map
[list %conflict_size
$conflict_size]\
432 {^
\+\+\|
{%conflict_size
}(?
: |
$)}]
433 if {[regexp $regexp $line _g op
]} {
434 set is_conflict_diff
1
435 set line
[string replace
$line 0 1 { }]
438 switch -exact -- $op {
439 < { set ::conflict_state {OURS
} }
440 = { set ::conflict_state {THEIRS
} }
441 > { set ::conflict_state {CONTEXT
} }
443 } elseif
{[regexp $regexp_pre_image $line]} {
444 set is_conflict_diff
1
445 set line
[string replace
$line 0 1 { }]
448 set ::conflict_state {BASE
}
449 } elseif
{$::conflict_state eq
{BASE
}} {
450 set line
[string replace
$line 0 1 {--}]
458 puts "error: Unhandled 3 way diff marker: {$op}"
462 } elseif
{$is_submodule_diff} {
463 if {$line == ""} continue
464 if {[regexp {^Submodule
} $line]} {
466 } elseif
{[regexp {^
\* } $line]} {
467 set line
[string replace
$line 0 1 {Submodule
}]
470 set op
[string range
$line 0 2]
476 puts "error: Unhandled submodule diff marker: {$op}"
482 set op
[string index
$line 0]
488 set regexp [string map
[list %conflict_size
$conflict_size]\
489 {^
\+([<>=]){%conflict_size
}(?
: |
$)}]
490 if {[regexp $regexp $line _g op
]} {
491 set is_conflict_diff
1
498 puts "error: Unhandled 2 way diff marker: {$op}"
503 set mark
[$ui_diff index
"end - 1 line linestart"]
504 if {[llength $markup] > 0} {
507 $ui_diff insert end
$line $tags
508 if {[string index
$line end
] eq
"\r"} {
509 $ui_diff tag add d_cr
{end
- 2c
}
511 $ui_diff insert end
"\n" $tags
513 foreach {posbegin colbegin posend colend
} $markup {
515 foreach style
[lsort -integer [split $colbegin ";"]] {
516 if {$style eq
"7"} {append prefix i
; continue}
517 if {$style != 4 && ($style < 30 ||
$style > 47)} {continue}
518 set a
"$mark linestart + $posbegin chars"
519 set b
"$mark linestart + $posend chars"
520 catch {$ui_diff tag add
$prefix$style $a $b}
524 $ui_diff conf
-state disabled
529 if {$current_diff_queue ne
{}} {
530 advance_diff_queue
$cont_info
536 set scroll_pos
[lindex $cont_info 0]
537 if {$scroll_pos ne
{}} {
539 $ui_diff yview moveto
$scroll_pos
543 if {[$ui_diff index end
] eq
{2.0}} {
546 set diff_empty_count
0
549 set callback
[lindex $cont_info 1]
550 if {$callback ne
{}} {
556 proc apply_hunk
{x y
{revert
0}} {
557 global current_diff_path current_diff_header current_diff_side
558 global ui_diff ui_index file_states
560 if {$current_diff_path eq
{} ||
$current_diff_header eq
{}} return
561 if {![lock_index apply_hunk
]} return
563 set apply_cmd
{apply
--cached --whitespace=nowarn
}
564 set mi
[lindex $file_states($current_diff_path) 0]
565 if {$current_diff_side eq
$ui_index} {
566 set failed_msg
[mc
"Failed to unstage selected hunk."]
567 lappend apply_cmd
--reverse
568 if {[string index
$mi 0] ne
{M
}} {
574 set failed_msg
[mc
"Failed to revert selected hunk."]
575 set apply_cmd
{apply
--reverse --whitespace=nowarn
}
577 set failed_msg
[mc
"Failed to stage selected hunk."]
579 if {[string index
$mi 1] ne
{M
}} {
585 set s_lno
[lindex [split [$ui_diff index
@$x,$y] .
] 0]
586 set s_lno
[$ui_diff search
-backwards -regexp ^
@@ $s_lno.0 0.0]
592 set e_lno
[$ui_diff search
-forwards -regexp ^
@@ "$s_lno + 1 lines" end
]
598 set enc
[get_path_encoding
$current_diff_path]
599 set p
[eval git_write
$apply_cmd]
600 fconfigure $p -translation binary -encoding $enc
601 puts -nonewline $p $current_diff_header
602 puts -nonewline $p [$ui_diff get
$s_lno $e_lno]
604 error_popup
[append $failed_msg "\n\n$err"]
609 $ui_diff conf
-state normal
610 $ui_diff delete
$s_lno $e_lno
611 $ui_diff conf
-state disabled
613 if {[$ui_diff get
1.0 end
] eq
"\n"} {
619 if {$current_diff_side eq
$ui_index} {
622 set mi
"[string index $mi 0]$o"
623 } elseif
{[string index
$mi 0] eq
{_
}} {
629 display_file
$current_diff_path $mi
630 # This should trigger shift to the next changed file
636 proc apply_range_or_line
{x y
{revert
0}} {
637 global current_diff_path current_diff_header current_diff_side
638 global ui_diff ui_index file_states
640 set selected
[$ui_diff tag nextrange sel
0.0]
642 if {$selected == {}} {
643 set first
[$ui_diff index
"@$x,$y"]
646 set first
[lindex $selected 0]
647 set last
[lindex $selected 1]
650 set first_l
[$ui_diff index
"$first linestart"]
651 # don't include the next line if $last points to the start of a line
652 # ie. <lno>.0 and we have a selection
653 if {$first ne
$last && [$ui_diff compare
$last == "$last linestart"]} {
654 set last_l
[$ui_diff index
"$last -1 line lineend"]
656 set last_l
[$ui_diff index
"$last lineend"]
659 if {$current_diff_path eq
{} ||
$current_diff_header eq
{}} return
660 if {![lock_index apply_hunk
]} return
662 set apply_cmd
{apply
--cached --whitespace=nowarn
}
663 set mi
[lindex $file_states($current_diff_path) 0]
664 if {$current_diff_side eq
$ui_index} {
665 set failed_msg
[mc
"Failed to unstage selected line."]
667 lappend apply_cmd
--reverse
668 if {[string index
$mi 0] ne
{M
}} {
674 set failed_msg
[mc
"Failed to revert selected line."]
675 set apply_cmd
{apply
--reverse --whitespace=nowarn
}
678 set failed_msg
[mc
"Failed to stage selected line."]
681 if {[string index
$mi 1] ne
{M
}} {
689 while {$first_l < $last_l} {
690 set i_l
[$ui_diff search
-backwards -regexp ^
@@ $first_l 0.0]
692 # If there's not a @@ above, then the selected range
693 # must have come before the first_l @@
694 set i_l
[$ui_diff search
-regexp ^
@@ $first_l $last_l]
700 # $i_l is now at the beginning of a line
702 # pick start line number from hunk header
703 if {![regexp {^
@@ -(\d
+)(?
:,\d
+)?
\+(?
:\d
+)(?
:,\d
+)?
@@(?
:\s|
$)} \
704 [$ui_diff get
$i_l "$i_l + 1 lines"] hh hln
]} {
709 # There is a special situation to take care of. Consider this
720 # We used to keep the context lines in the order they appear in
721 # the hunk. But then it is not possible to correctly stage only
722 # "-old 1" and "+new 1" - it would result in this staged text:
729 # (By symmetry it is not possible to *un*stage "old 2" and "new
732 # We resolve the problem by introducing an asymmetry, namely,
733 # when a "+" line is *staged*, it is moved in front of the
734 # context lines that are generated from the "-" lines that are
735 # immediately before the "+" block. That is, we construct this
745 # But we do *not* treat "-" lines that are *un*staged in a
748 # With this asymmetry it is possible to stage the change "old
749 # 1" -> "new 1" directly, and to stage the change "old 2" ->
750 # "new 2" by first staging the entire hunk and then unstaging
751 # the change "old 1" -> "new 1".
753 # Applying multiple lines adds complexity to the special
754 # situation. The pre_context must be moved after the entire
755 # first block of consecutive staged "+" lines, so that
756 # staging both additions gives the following patch:
766 # This is non-empty if and only if we are _staging_ changes;
767 # then it accumulates the consecutive "-" lines (after
768 # converting them to context lines) in order to be moved after
774 set i_l
[$ui_diff index
"$i_l + 1 lines"]
776 while {[$ui_diff compare
$i_l < "end - 1 chars"] &&
777 [$ui_diff get
$i_l "$i_l + 2 chars"] ne
{@@}} {
778 set next_l
[$ui_diff index
"$i_l + 1 lines"]
779 set c1
[$ui_diff get
$i_l]
780 if {[$ui_diff compare
$first_l <= $i_l] &&
781 [$ui_diff compare
$i_l < $last_l] &&
782 ($c1 eq
{-} ||
$c1 eq
{+})} {
783 # a line to stage/unstage
784 set ln
[$ui_diff get
$i_l $next_l]
787 set patch
"$patch$pre_context$ln"
791 set patch
"$patch$ln"
793 } elseif
{$c1 ne
{-} && $c1 ne
{+}} {
795 set ln
[$ui_diff get
$i_l $next_l]
796 set patch
"$patch$pre_context$ln"
797 # Skip the "\ No newline at end of
798 # file". Depending on the locale setting
799 # we don't know what this line looks
800 # like exactly. The only thing we do
801 # know is that it starts with "\ "
802 if {![string match
{\\ *} $ln]} {
807 } elseif
{$c1 eq
$to_context} {
808 # turn change line into context line
809 set ln
[$ui_diff get
"$i_l + 1 chars" $next_l]
811 set pre_context
"$pre_context $ln"
813 set patch
"$patch $ln"
818 # a change in the opposite direction of
819 # to_context which is outside the range of
821 set patch
"$patch$pre_context"
826 set patch
"$patch$pre_context"
827 set wholepatch
"$wholepatch@@ -$hln,$n +$hln,$m @@\n$patch"
828 set first_l
[$ui_diff index
"$next_l + 1 lines"]
832 set enc
[get_path_encoding
$current_diff_path]
833 set p
[eval git_write
$apply_cmd]
834 fconfigure $p -translation binary -encoding $enc
835 puts -nonewline $p $current_diff_header
836 puts -nonewline $p $wholepatch
838 error_popup
[append $failed_msg "\n\n$err"]