2 # Copyright (C) 2006, 2007 Shawn Pearce
5 global ui_diff current_diff_path current_diff_header
6 global ui_index ui_workdir
7 global ui_diff_columns ui_diff_line_columns
8 global ui_diff_lno_col_width diff_lno_column_width
9 global is_other_diff is_3way_diff
10 global diff_nl diff_lno
12 foreach i
$ui_diff_columns {
15 $i conf
-state disabled
20 set diff_lno_column_width
0
21 foreach i
$ui_diff_line_columns {
22 $i conf
-width [expr $ui_diff_lno_col_width + 2]
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
33 update_show_line_numbers
36 proc reshow_diff
{{after {}}} {
37 global file_states file_lists
38 global current_diff_path current_diff_side
41 set p
$current_diff_path
43 # No diff is being shown.
44 } elseif
{$current_diff_side eq
{}} {
46 } elseif
{[catch {set s
$file_states($p)}]
47 ||
[lsearch -sorted -exact $file_lists($current_diff_side) $p] == -1} {
49 if {[find_next_diff
$current_diff_side $p {} {[^O
]}]} {
55 set save_pos
[lindex [$ui_diff yview
] 0]
56 show_diff
$p $current_diff_side {} $save_pos $after
60 proc force_diff_encoding
{enc
} {
61 global current_diff_path
63 if {$current_diff_path ne
{}} {
64 force_path_encoding
$current_diff_path $enc
69 proc handle_empty_diff
{} {
70 global current_diff_path file_states file_lists
71 global diff_empty_count
73 set path
$current_diff_path
74 set s
$file_states($path)
75 if {[lindex $s 0] ne
{_M
} ||
[has_textconv
$path]} return
77 # Prevent infinite rescan loops
79 if {$diff_empty_count > 1} return
81 info_popup
[mc
"No differences detected.
85 The modification date of this file was updated by another application, but the content within the file was not changed.
87 A rescan will be automatically started to find other files which may have the same state." [short_path
$path]]
94 proc show_diff
{path w
{lno
{}} {scroll_pos
{}} {callback
{}}} {
95 global file_states file_lists
96 global is_3way_diff is_conflict_diff diff_active repo_config
97 global ui_diff ui_index ui_workdir
98 global current_diff_path current_diff_side current_diff_header
99 global current_diff_queue
102 if {$diff_active ||
![lock_index
read]} return
106 set lno
[lsearch -sorted -exact $file_lists($w) $path]
112 $w tag add in_diff
$lno.0 [expr {$lno + 1}].0
116 set s
$file_states($path)
118 set is_conflict_diff
0
119 set current_diff_path
$path
120 set current_diff_side
$w
121 set current_diff_queue
{}
122 ui_status
[mc
"Loading diff of %s..." [escape_path
$path]]
125 set cont_info
[list $scroll_pos $callback]
127 if {[string first
{U
} $m] >= 0} {
128 merge_load_stages
$path [list show_unmerged_diff
$cont_info]
129 } elseif
{$m eq
{_O
}} {
131 update_show_line_numbers
132 show_other_diff
$path $w $m $cont_info
134 start_show_diff
$cont_info
138 proc update_lnocol_width
{txt
} {
139 global diff_lno_column_width
141 regsub {^
[-+]} $txt {} txt
142 set ln_wc
[string length
$txt]
143 if {$diff_lno_column_width < $ln_wc} {
144 set diff_lno_column_width
$ln_wc
148 proc insert_lno
{w pos txt tags
} {
149 $w insert
$pos $txt "linenumber $tags"
150 update_lnocol_width
$txt
153 proc diff_append_line
{line
{tag
{}} {blno
{}} {alno
{}} {clno
{}}} {
154 global diff_nl diff_lno
155 global ui_diff_clnos ui_diff_alnos ui_diff_blnos
158 $ui_diff_clnos insert end
"$diff_nl" linenumber
159 if {[llength $clno] > 0} {
160 $ui_diff_clnos insert end
[lindex $clno 0] "linenumber [lindex $clno 1]"
161 update_lnocol_width
[lindex $clno 0]
164 $ui_diff_alnos insert end
"$diff_nl" linenumber
165 if {[llength $alno] > 0} {
166 $ui_diff_alnos insert end
[lindex $alno 0] "linenumber [lindex $alno 1]"
167 update_lnocol_width
[lindex $alno 0]
170 $ui_diff_blnos insert end
"$diff_nl" linenumber
171 if {[llength $blno] > 0} {
172 $ui_diff_blnos insert end
[lindex $blno 0] "linenumber [lindex $blno 1]"
173 update_lnocol_width
[lindex $blno 0]
176 $ui_diff insert end
"$diff_nl$line" $tag
182 proc show_unmerged_diff
{cont_info
} {
183 global current_diff_path current_diff_side
184 global merge_stages ui_diff is_conflict_diff
185 global current_diff_queue
187 if {$merge_stages(2) eq
{}} {
188 set is_conflict_diff
1
189 lappend current_diff_queue
\
190 [list [mc
"LOCAL: deleted\nREMOTE:"] d
= \
191 [list ":1:$current_diff_path" ":3:$current_diff_path"]]
192 } elseif
{$merge_stages(3) eq
{}} {
193 set is_conflict_diff
1
194 lappend current_diff_queue
\
195 [list [mc
"REMOTE: deleted\nLOCAL:"] d
= \
196 [list ":1:$current_diff_path" ":2:$current_diff_path"]]
197 } elseif
{[lindex $merge_stages(1) 0] eq
{120000}
198 ||
[lindex $merge_stages(2) 0] eq
{120000}
199 ||
[lindex $merge_stages(3) 0] eq
{120000}} {
200 set is_conflict_diff
1
201 lappend current_diff_queue
\
202 [list [mc
"LOCAL:"] d
= \
203 [list ":1:$current_diff_path" ":2:$current_diff_path"]]
204 lappend current_diff_queue
\
205 [list [mc
"REMOTE:"] d
= \
206 [list ":1:$current_diff_path" ":3:$current_diff_path"]]
208 start_show_diff
$cont_info
212 advance_diff_queue
$cont_info
215 proc advance_diff_queue
{cont_info
} {
216 global current_diff_queue ui_diff
217 global ui_diff_columns
219 set item
[lindex $current_diff_queue 0]
220 set current_diff_queue
[lrange $current_diff_queue 1 end
]
222 set content
[lindex $item 0]
223 set tag
[lindex $item 1]
225 foreach i
$ui_diff_columns {$i conf
-state normal
}
226 foreach line
[split $content "\n"] {
227 diff_append_line
$line $tag
229 foreach i
$ui_diff_columns {$i conf
-state disabled
}
231 start_show_diff
$cont_info [lindex $item 2]
234 proc show_other_diff
{path w m cont_info
} {
235 global file_states file_lists
236 global is_3way_diff diff_active repo_config
237 global ui_diff ui_index ui_workdir
238 global current_diff_path current_diff_side current_diff_header
239 global ui_diff_columns ui_diff_line_columns
240 global ui_diff_lno_col_width diff_lno_column_width
242 # - Git won't give us the diff, there's nothing to compare to!
248 set type
[file type
$path]
256 set content
[file readlink
$path]
257 set sz
[string length
$content]
260 set fd
[open $path r
]
263 -encoding [get_path_encoding
$path]
264 set content
[read $fd $max_sz]
266 set sz
[file size
$path]
269 error "'$type' not supported"
275 ui_status
[mc
"Unable to display %s" [escape_path
$path]]
276 error_popup
[strcat
[mc
"Error loading file:"] "\n\n$err"]
279 foreach i
$ui_diff_columns {$i conf
-state normal
}
280 if {$type eq
{submodule
}} {
282 [append "* " [mc
"Git Repository (subproject)"]] d_info
\
284 } elseif
{![catch {set type
[exec file $path]}]} {
285 set n
[string length
$path]
286 if {[string equal
-length $n $path $type]} {
287 set type
[string range
$type $n end
]
288 regsub {^
:?
\s
*} $type {} type
290 diff_append_line
"* $type" d_info
[list "*" {}]
292 if {[string first
"\0" $content] != -1} {
294 [mc
"* Binary file (not showing content)."] \
300 [mc
"* Untracked file is %d bytes." $sz] \
304 [mc
"* Showing only first %d bytes." $max_sz] \
308 regsub -all "\r\n" $content "\n" content
309 regsub -all "\r" $content "\n" content
311 foreach line
[split $content "\n"] {
312 diff_append_line
$line {} [list $lno ""]
317 [mc
"* Untracked file clipped here by %s." [appname
]] \
321 [mc
"* To see the entire file, use an external editor."] \
326 if {$diff_lno_column_width > $ui_diff_lno_col_width} {
327 foreach i
$ui_diff_line_columns {
328 $i conf
-width [expr $diff_lno_column_width + 2]
331 foreach i
$ui_diff_columns {$i conf
-state disabled
}
334 set scroll_pos
[lindex $cont_info 0]
335 if {$scroll_pos ne
{}} {
338 $ui_diff yview moveto
$scroll_pos
341 set callback
[lindex $cont_info 1]
342 if {$callback ne
{}} {
349 proc start_show_diff
{cont_info
{add_opts
{}}} {
350 global file_states file_lists
351 global is_3way_diff is_submodule_diff diff_active repo_config
352 global ui_diff ui_index ui_workdir
353 global current_diff_path current_diff_side current_diff_header
354 global diff_a_lno diff_b_lno diff_c_lno
355 global diff_a_mod_cnt diff_b_mod_cnt diff_c_mod_cnt
356 global diff_hunk_mark
358 set path
$current_diff_path
359 set w
$current_diff_side
361 set s
$file_states($path)
364 set is_submodule_diff
0
366 set current_diff_header
{}
367 set conflict_size
[gitattr
$path conflict-marker-size
7]
371 set diff_hunk_mark
{}
372 set diff_a_mod_cnt
{}
373 set diff_b_mod_cnt
{}
374 set diff_c_mod_cnt
{}
377 if {$w eq
$ui_index} {
378 lappend cmd diff-index
380 } elseif
{$w eq
$ui_workdir} {
381 if {[string first
{U
} $m] >= 0} {
384 lappend cmd diff-files
387 if {![is_config_false gui.textconv
] && [git-version
>= 1.6.1]} {
388 lappend cmd
--textconv
391 if {[string match
{160000 *} [lindex $s 2]]
392 ||
[string match
{160000 *} [lindex $s 3]]} {
393 set is_submodule_diff
1
395 if {[git-version
>= "1.6.6"]} {
396 lappend cmd
--submodule
402 set cmd
[concat $cmd $repo_config(gui.diffopts
)]
403 if {$repo_config(gui.diffcontext
) >= 1} {
404 lappend cmd
"-U$repo_config(gui.diffcontext)"
406 if {$w eq
$ui_index} {
409 if {$add_opts ne
{}} {
410 eval lappend cmd
$add_opts
416 if {$is_submodule_diff && [git-version
< "1.6.6"]} {
417 if {$w eq
$ui_index} {
418 set cmd
[list submodule summary
--cached -- $path]
420 set cmd
[list submodule summary
--files -- $path]
424 if {[catch {set fd
[eval git_read
--nice $cmd]} err
]} {
427 ui_status
[mc
"Unable to display %s" [escape_path
$path]]
428 error_popup
[strcat
[mc
"Error loading diff:"] "\n\n$err"]
432 set ::current_diff_inheader 1
433 set ::conflict_state {CONTEXT
}
436 -encoding [get_path_encoding
$path] \
438 fileevent $fd readable
[list read_diff
$fd $conflict_size $cont_info]
441 proc parse_color_line
{line
} {
445 set regexp {\033\[((?
:\d
+;)*\d
+)?m
(?
:\033\[K
)?
}
447 while {[regexp -indices -start $start $regexp $line match code
]} {
448 foreach {begin end
} $match break
449 append result
[string range
$line $start [expr {$begin - 1}]]
450 set pos
[string length
$result]
451 set col
[eval [linsert $code 0 string range
$line]]
453 if {$col eq
"0" ||
$col eq
""} {
454 if {!$need_reset} continue
459 lappend markup
$pos $col
461 append result
[string range
$line $start end
]
462 if {[llength $markup] < 4} {set markup
{}}
463 return [list $result $markup]
466 proc read_diff
{fd conflict_size cont_info
} {
467 global ui_diff diff_active is_submodule_diff
468 global ui_diff_alnos ui_diff_blnos ui_diff_clnos
469 global is_3way_diff is_conflict_diff current_diff_header
470 global current_diff_queue
471 global diff_empty_count
472 global ui_diff_columns ui_diff_line_columns
473 global diff_a_lno diff_b_lno diff_c_lno
474 global diff_a_mod_cnt diff_b_mod_cnt diff_c_mod_cnt
475 global diff_lno diff_hunk_mark
476 global diff_lno_column_width ui_diff_lno_col_width
478 foreach i
$ui_diff_columns {$i conf
-state normal
}
479 while {[gets $fd line
] >= 0} {
480 foreach {line markup
} [parse_color_line
$line] break
481 set line
[string map
{\033 ^
} $line]
485 # -- Check for start of diff header.
486 if { [string match
{diff
--git *} $line]
487 ||
[string match
{diff
--cc *} $line]
488 ||
[string match
{diff
--combined *} $line]} {
489 set ::current_diff_inheader 1
492 # -- Check for end of diff header (any hunk line will do this).
494 if {[regexp {^
@@+ } $line]} {set ::current_diff_inheader 0}
496 # -- Automatically detect if this is a 3 way diff.
498 if {[string match
{@@@ *} $line]} {
500 update_show_line_numbers
515 if {$::current_diff_inheader} {
517 # -- These two lines stop a diff header and shouldn't be in there
518 if { [string match
{Binary files
* and
* differ
} $line]
519 ||
[regexp {^
\* Unmerged path
} $line]} {
520 set ::current_diff_inheader 0
522 append current_diff_header
$line "\n"
525 # -- Cleanup uninteresting diff header lines.
527 if { [string match
{diff
--git *} $line]
528 ||
[string match
{diff
--cc *} $line]
529 ||
[string match
{diff
--combined *} $line]
530 ||
[string match
{--- *} $line]
531 ||
[string match
{+++ *} $line]
532 ||
[string match
{index
*} $line]} {
536 # -- Name it symlink, not 120000
537 # Note, that the original line is in $current_diff_header
538 regsub {^
(deleted|new
) file mode
120000} $line {\1 symlink
} line
540 } elseif
{ $line eq
{\ No newline at end of
file}} {
541 # -- Handle some special lines
542 } elseif
{$is_3way_diff} {
543 set op
[string range
$line 0 1]
546 regexp {^
@@@ -(\d
+)(?
:,\d
+)?
-(\d
+)(?
:,\d
+)?
\+(\d
+)(?
:,\d
+)?
@@@} $line _g diff_c_lno diff_a_lno diff_b_lno
550 set c_lno
$diff_c_lno
553 set a_lno
$diff_a_lno
556 set b_lno
$diff_b_lno
562 set c_lno
$diff_c_lno
566 set b_lno
$diff_b_lno
572 if {$::conflict_state eq
{CONTEXT
}} {
579 set a_lno
$diff_a_lno
583 set b_lno
$diff_b_lno
590 if {$::conflict_state eq
{CONTEXT
}} {
597 set c_lno
$diff_c_lno
601 set a_lno
$diff_a_lno
605 set b_lno
$diff_b_lno
612 set b_lno
$diff_b_lno
615 set regexp [string map
[list %conflict_size
$conflict_size]\
616 {^
\+\+([<>=]){%conflict_size
}(?
: |
$)}]
617 set regexp_pre_image
[string map
[list %conflict_size
$conflict_size]\
618 {^
\+\+\|
{%conflict_size
}(?
: |
$)}]
619 if {[regexp $regexp $line _g op
]} {
620 set is_conflict_diff
1
621 set line
[string replace
$line 0 1 { }]
625 switch -exact -- $op {
626 < { set ::conflict_state {OURS
} }
627 = { set ::conflict_state {THEIRS
} }
628 > { set ::conflict_state {CONTEXT
} }
630 } elseif
{[regexp $regexp_pre_image $line]} {
631 set is_conflict_diff
1
632 set line
[string replace
$line 0 1 { }]
636 set ::conflict_state {BASE
}
637 } elseif
{$::conflict_state eq
{BASE
}} {
638 set line
[string replace
$line 0 1 {--}]
642 set c_lno
$diff_c_lno
646 set a_lno
$diff_a_lno
654 puts "error: Unhandled 3 way diff marker: {$op}"
658 } elseif
{$is_submodule_diff} {
659 if {$line == ""} continue
660 if {[regexp {^Submodule
} $line]} {
662 } elseif
{[regexp {^
\* } $line]} {
663 set line
[string replace
$line 0 1 {Submodule
}]
666 set op
[string range
$line 0 2]
672 puts "error: Unhandled submodule diff marker: {$op}"
678 set op
[string index
$line 0]
681 if {$diff_hunk_mark ne
{}} {
682 if {$diff_a_mod_cnt > 0} {
683 insert_lno
$ui_diff_alnos $diff_hunk_mark "-$diff_a_mod_cnt" red
685 if {$diff_b_mod_cnt > 0} {
686 insert_lno
$ui_diff_blnos $diff_hunk_mark "+$diff_b_mod_cnt" green
689 regexp {^
@@ -(\d
+)(?
:,\d
+)?
\+(\d
+)(?
:,\d
+)?
@@} $line _g diff_a_lno diff_b_lno
696 set a_lno
$diff_a_lno
699 set b_lno
$diff_b_lno
705 set a_lno
$diff_a_lno
713 set b_lno
$diff_b_lno
718 set regexp [string map
[list %conflict_size
$conflict_size]\
719 {^
\+([<>=]){%conflict_size
}(?
: |
$)}]
720 if {[regexp $regexp $line _g op
]} {
721 set is_conflict_diff
1
728 puts "error: Unhandled 2 way diff marker: {$op}"
734 if {$remember_hunk} {
735 set diff_hunk_mark
$mark
738 if {[llength $markup] > 0} {
743 [list "$b_lno" $b_lno_tag] \
744 [list "$a_lno" $a_lno_tag] \
745 [list "$c_lno" $c_lno_tag]
747 if {[string index
$line end
] eq
"\r"} {
748 $ui_diff tag add d_cr
{end
- 2c
}
751 foreach {posbegin colbegin posend colend
} $markup {
753 foreach style
[lsort -integer [split $colbegin ";"]] {
754 if {$style eq
"7"} {append prefix i
; continue}
755 if {$style != 4 && ($style < 30 ||
$style > 47)} {continue}
756 set a
"$mark linestart + $posbegin chars"
757 set b
"$mark linestart + $posend chars"
758 catch {$ui_diff tag add
$prefix$style $a $b}
762 foreach i
$ui_diff_columns {$i conf
-state disabled
}
767 # fill line stats for last hunk
768 foreach i
$ui_diff_columns {$i conf
-state normal
}
769 if {$diff_hunk_mark ne
{}} {
770 if {$diff_a_mod_cnt > 0} {
771 insert_lno
$ui_diff_alnos $diff_hunk_mark "-$diff_a_mod_cnt" "red"
773 if {$diff_b_mod_cnt > 0} {
774 insert_lno
$ui_diff_blnos $diff_hunk_mark "+$diff_b_mod_cnt" "green"
777 if {$diff_lno_column_width > $ui_diff_lno_col_width} {
778 foreach i
$ui_diff_line_columns {
779 $i conf
-width [expr $diff_lno_column_width + 2]
782 foreach i
$ui_diff_columns {$i conf
-state disabled
}
784 if {$current_diff_queue ne
{}} {
785 advance_diff_queue
$cont_info
791 set scroll_pos
[lindex $cont_info 0]
792 if {$scroll_pos ne
{}} {
795 $ui_diff yview moveto
$scroll_pos
799 if {[$ui_diff index end
] eq
{2.0}} {
802 set diff_empty_count
0
805 set callback
[lindex $cont_info 1]
806 if {$callback ne
{}} {
812 proc apply_hunk
{x y
{revert
0}} {
813 global current_diff_path current_diff_header current_diff_side
814 global ui_diff ui_index file_states
815 global ui_diff_columns
817 if {$current_diff_path eq
{} ||
$current_diff_header eq
{}} return
818 if {![lock_index apply_hunk
]} return
820 set apply_cmd
{apply
--cached --whitespace=nowarn
}
821 set mi
[lindex $file_states($current_diff_path) 0]
822 if {$current_diff_side eq
$ui_index} {
823 set failed_msg
[mc
"Failed to unstage selected hunk."]
824 lappend apply_cmd
--reverse
825 if {[string index
$mi 0] ne
{M
}} {
831 set failed_msg
[mc
"Failed to revert selected hunk."]
832 set apply_cmd
{apply
--reverse --whitespace=nowarn
}
834 set failed_msg
[mc
"Failed to stage selected hunk."]
836 if {[string index
$mi 1] ne
{M
}} {
842 set s_lno
[lindex [split [$ui_diff index
@$x,$y] .
] 0]
843 set s_lno
[$ui_diff search
-backwards -regexp ^
@@ $s_lno.0 0.0]
849 set e_lno
[$ui_diff search
-forwards -regexp ^
@@ "$s_lno + 1 lines" end
]
855 set enc
[get_path_encoding
$current_diff_path]
856 set p
[eval git_write
$apply_cmd]
857 fconfigure $p -translation binary -encoding $enc
858 puts -nonewline $p $current_diff_header
859 puts -nonewline $p [$ui_diff get
$s_lno $e_lno]
861 error_popup
[append $failed_msg "\n\n$err"]
866 foreach i
$ui_diff_columns {
867 $i conf
-state normal
868 $i delete
$s_lno $e_lno
869 $i conf
-state disabled
872 if {[$ui_diff get
1.0 end
] eq
"\n"} {
878 if {$current_diff_side eq
$ui_index} {
881 set mi
"[string index $mi 0]$o"
882 } elseif
{[string index
$mi 0] eq
{_
}} {
888 display_file
$current_diff_path $mi
889 # This should trigger shift to the next changed file
895 proc apply_range_or_line
{x y
{revert
0}} {
896 global current_diff_path current_diff_header current_diff_side
897 global ui_diff ui_index file_states
899 set selected
[$ui_diff tag nextrange sel
0.0]
901 if {$selected == {}} {
902 set first
[$ui_diff index
"@$x,$y"]
905 set first
[lindex $selected 0]
906 set last
[lindex $selected 1]
909 set first_l
[$ui_diff index
"$first linestart"]
910 # don't include the next line if $last points to the start of a line
911 # ie. <lno>.0 and we have a selection
912 if {$first ne
$last && [$ui_diff compare
$last == "$last linestart"]} {
913 set last_l
[$ui_diff index
"$last -1 line lineend"]
915 set last_l
[$ui_diff index
"$last lineend"]
918 if {$current_diff_path eq
{} ||
$current_diff_header eq
{}} return
919 if {![lock_index apply_hunk
]} return
921 set apply_cmd
{apply
--cached --whitespace=nowarn
}
922 set mi
[lindex $file_states($current_diff_path) 0]
923 if {$current_diff_side eq
$ui_index} {
924 set failed_msg
[mc
"Failed to unstage selected line."]
926 lappend apply_cmd
--reverse
927 if {[string index
$mi 0] ne
{M
}} {
933 set failed_msg
[mc
"Failed to revert selected line."]
934 set apply_cmd
{apply
--reverse --whitespace=nowarn
}
937 set failed_msg
[mc
"Failed to stage selected line."]
940 if {[string index
$mi 1] ne
{M
}} {
948 while {$first_l < $last_l} {
949 set i_l
[$ui_diff search
-backwards -regexp ^
@@ $first_l 0.0]
951 # If there's not a @@ above, then the selected range
952 # must have come before the first_l @@
953 set i_l
[$ui_diff search
-regexp ^
@@ $first_l $last_l]
959 # $i_l is now at the beginning of a line
961 # pick start line number from hunk header
962 if {![regexp {^
@@ -(\d
+)(?
:,\d
+)?
\+(?
:\d
+)(?
:,\d
+)?
@@(?
:\s|
$)} \
963 [$ui_diff get
$i_l "$i_l + 1 lines"] hh hln
]} {
968 # There is a special situation to take care of. Consider this
979 # We used to keep the context lines in the order they appear in
980 # the hunk. But then it is not possible to correctly stage only
981 # "-old 1" and "+new 1" - it would result in this staged text:
988 # (By symmetry it is not possible to *un*stage "old 2" and "new
991 # We resolve the problem by introducing an asymmetry, namely,
992 # when a "+" line is *staged*, it is moved in front of the
993 # context lines that are generated from the "-" lines that are
994 # immediately before the "+" block. That is, we construct this
1004 # But we do *not* treat "-" lines that are *un*staged in a
1007 # With this asymmetry it is possible to stage the change "old
1008 # 1" -> "new 1" directly, and to stage the change "old 2" ->
1009 # "new 2" by first staging the entire hunk and then unstaging
1010 # the change "old 1" -> "new 1".
1012 # Applying multiple lines adds complexity to the special
1013 # situation. The pre_context must be moved after the entire
1014 # first block of consecutive staged "+" lines, so that
1015 # staging both additions gives the following patch:
1025 # This is non-empty if and only if we are _staging_ changes;
1026 # then it accumulates the consecutive "-" lines (after
1027 # converting them to context lines) in order to be moved after
1033 set i_l
[$ui_diff index
"$i_l + 1 lines"]
1035 while {[$ui_diff compare
$i_l < "end - 1 chars"] &&
1036 [$ui_diff get
$i_l "$i_l + 2 chars"] ne
{@@}} {
1037 set next_l
[$ui_diff index
"$i_l + 1 lines"]
1038 set c1
[$ui_diff get
$i_l]
1039 if {[$ui_diff compare
$first_l <= $i_l] &&
1040 [$ui_diff compare
$i_l < $last_l] &&
1041 ($c1 eq
{-} ||
$c1 eq
{+})} {
1042 # a line to stage/unstage
1043 set ln
[$ui_diff get
$i_l $next_l]
1046 set patch
"$patch$pre_context$ln"
1050 set patch
"$patch$ln"
1052 } elseif
{$c1 ne
{-} && $c1 ne
{+}} {
1054 set ln
[$ui_diff get
$i_l $next_l]
1055 set patch
"$patch$pre_context$ln"
1056 # Skip the "\ No newline at end of
1057 # file". Depending on the locale setting
1058 # we don't know what this line looks
1059 # like exactly. The only thing we do
1060 # know is that it starts with "\ "
1061 if {![string match
{\\ *} $ln]} {
1066 } elseif
{$c1 eq
$to_context} {
1067 # turn change line into context line
1068 set ln
[$ui_diff get
"$i_l + 1 chars" $next_l]
1070 set pre_context
"$pre_context $ln"
1072 set patch
"$patch $ln"
1077 # a change in the opposite direction of
1078 # to_context which is outside the range of
1080 set patch
"$patch$pre_context"
1085 set patch
"$patch$pre_context"
1086 set wholepatch
"$wholepatch@@ -$hln,$n +$hln,$m @@\n$patch"
1087 set first_l
[$ui_diff index
"$next_l + 1 lines"]
1091 set enc
[get_path_encoding
$current_diff_path]
1092 set p
[eval git_write
$apply_cmd]
1093 fconfigure $p -translation binary -encoding $enc
1094 puts -nonewline $p $current_diff_header
1095 puts -nonewline $p $wholepatch
1097 error_popup
[append $failed_msg "\n\n$err"]