git-gui: add build tab
[git-gui/bertw.git] / lib / diff.tcl
blob05256f008452cbc1d40b5187006a12bccc0be96b
1 # git-gui diff viewer
2 # Copyright (C) 2006, 2007 Shawn Pearce
4 proc clear_diff {} {
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 {
13 $i conf -state normal
14 $i delete 0.0 end
15 $i conf -state disabled
17 set diff_nl ""
18 set diff_lno 1
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
31 set is_other_diff 0
32 set is_3way_diff 0
33 update_show_line_numbers
36 proc reshow_diff {{after {}}} {
37 global file_states file_lists
38 global current_diff_path current_diff_side
39 global ui_diff
41 set p $current_diff_path
42 if {$p eq {}} {
43 # No diff is being shown.
44 } elseif {$current_diff_side eq {}} {
45 clear_diff
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]}]} {
50 next_diff $after
51 } else {
52 clear_diff
54 } else {
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
65 reshow_diff
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
78 incr diff_empty_count
79 if {$diff_empty_count > 1} return
81 info_popup [mc "No differences detected.
83 %s has no changes.
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]]
89 clear_diff
90 display_file $path __
91 rescan ui_ready 0
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
100 global is_other_diff
102 if {$diff_active || ![lock_index read]} return
104 clear_diff
105 if {$lno == {}} {
106 set lno [lsearch -sorted -exact $file_lists($w) $path]
107 if {$lno >= 0} {
108 incr lno
111 if {$lno >= 1} {
112 $w tag add in_diff $lno.0 [expr {$lno + 1}].0
113 $w see $lno.0
116 set s $file_states($path)
117 set m [lindex $s 0]
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]]
123 set is_other_diff 0
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}} {
130 set is_other_diff 1
131 update_show_line_numbers
132 show_other_diff $path $w $m $cont_info
133 } else {
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
156 global ui_diff
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
178 set diff_nl "\n"
179 incr diff_lno
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"]]
207 } else {
208 start_show_diff $cont_info
209 return
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!
244 if {$m eq {_O}} {
245 set max_sz 100000
246 set type unknown
247 if {[catch {
248 set type [file type $path]
249 switch -- $type {
250 directory {
251 set type submodule
252 set content {}
253 set sz 0
255 link {
256 set content [file readlink $path]
257 set sz [string length $content]
259 file {
260 set fd [open $path r]
261 fconfigure $fd \
262 -eofchar {} \
263 -encoding [get_path_encoding $path]
264 set content [read $fd $max_sz]
265 close $fd
266 set sz [file size $path]
268 default {
269 error "'$type' not supported"
272 } err ]} {
273 set diff_active 0
274 unlock_index
275 ui_status [mc "Unable to display %s" [escape_path $path]]
276 error_popup [strcat [mc "Error loading file:"] "\n\n$err"]
277 return
279 foreach i $ui_diff_columns {$i conf -state normal}
280 if {$type eq {submodule}} {
281 diff_append_line \
282 [append "* " [mc "Git Repository (subproject)"]] d_info \
283 [list "*" {}]
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} {
293 diff_append_line \
294 [mc "* Binary file (not showing content)."] \
295 d_info \
296 [list "*" {}]
297 } else {
298 if {$sz > $max_sz} {
299 diff_append_line \
300 [mc "* Untracked file is %d bytes." $sz] \
301 d_info \
302 [list "*" {}]
303 diff_append_line \
304 [mc "* Showing only first %d bytes." $max_sz] \
305 d_info \
306 [list "*" ""]
308 regsub -all "\r\n" $content "\n" content
309 regsub -all "\r" $content "\n" content
310 set lno 1
311 foreach line [split $content "\n"] {
312 diff_append_line $line {} [list $lno ""]
313 incr lno
315 if {$sz > $max_sz} {
316 diff_append_line \
317 [mc "* Untracked file clipped here by %s." [appname]] \
318 d_info \
319 [list "*" ""]
320 diff_append_line \
321 [mc "* To see the entire file, use an external editor."] \
322 d_info \
323 [list "*" ""]
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}
332 set diff_active 0
333 unlock_index
334 set scroll_pos [lindex $cont_info 0]
335 if {$scroll_pos ne {}} {
336 update
337 # TODO: scroll all
338 $ui_diff yview moveto $scroll_pos
340 ui_ready
341 set callback [lindex $cont_info 1]
342 if {$callback ne {}} {
343 eval $callback
345 return
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)
362 set m [lindex $s 0]
363 set is_3way_diff 0
364 set is_submodule_diff 0
365 set diff_active 1
366 set current_diff_header {}
367 set conflict_size [gitattr $path conflict-marker-size 7]
368 set diff_a_lno {}
369 set diff_b_lno {}
370 set diff_c_lno {}
371 set diff_hunk_mark {}
372 set diff_a_mod_cnt {}
373 set diff_b_mod_cnt {}
374 set diff_c_mod_cnt {}
376 set cmd [list]
377 if {$w eq $ui_index} {
378 lappend cmd diff-index
379 lappend cmd --cached
380 } elseif {$w eq $ui_workdir} {
381 if {[string first {U} $m] >= 0} {
382 lappend cmd diff
383 } else {
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
400 lappend cmd -p
401 lappend cmd --color
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} {
407 lappend cmd [PARENT]
409 if {$add_opts ne {}} {
410 eval lappend cmd $add_opts
411 } else {
412 lappend cmd --
413 lappend cmd $path
416 if {$is_submodule_diff && [git-version < "1.6.6"]} {
417 if {$w eq $ui_index} {
418 set cmd [list submodule summary --cached -- $path]
419 } else {
420 set cmd [list submodule summary --files -- $path]
424 if {[catch {set fd [eval git_read --nice $cmd]} err]} {
425 set diff_active 0
426 unlock_index
427 ui_status [mc "Unable to display %s" [escape_path $path]]
428 error_popup [strcat [mc "Error loading diff:"] "\n\n$err"]
429 return
432 set ::current_diff_inheader 1
433 set ::conflict_state {CONTEXT}
434 fconfigure $fd \
435 -blocking 0 \
436 -encoding [get_path_encoding $path] \
437 -translation lf
438 fileevent $fd readable [list read_diff $fd $conflict_size $cont_info]
441 proc parse_color_line {line} {
442 set start 0
443 set result ""
444 set markup [list]
445 set regexp {\033\[((?:\d+;)*\d+)?m(?:\033\[K)?}
446 set need_reset 0
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]]
452 set start [incr end]
453 if {$col eq "0" || $col eq ""} {
454 if {!$need_reset} continue
455 set need_reset 0
456 } else {
457 set need_reset 1
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]
483 set tags {}
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]} {
499 set is_3way_diff 1
500 update_show_line_numbers
503 set pre_lno {}
504 set pre_lno_tag {}
505 set post_lno {}
506 set post_lno_tag {}
507 set a_lno {}
508 set a_lno_tag {}
509 set b_lno {}
510 set b_lno_tag {}
511 set c_lno {}
512 set c_lno_tag {}
513 set remember_hunk 0
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
521 } else {
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]} {
533 continue
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]
544 switch -- $op {
545 {@@} {
546 regexp {^@@@ -(\d+)(?:,\d+)? -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@@} $line _g diff_c_lno diff_a_lno diff_b_lno
547 set tags d_@
549 { } {
550 set c_lno $diff_c_lno
551 incr diff_c_lno
553 set a_lno $diff_a_lno
554 incr diff_a_lno
556 set b_lno $diff_b_lno
557 incr diff_b_lno
559 set tags {}
561 { +} {
562 set c_lno $diff_c_lno
563 set c_lno_tag green
564 incr diff_c_lno
566 set b_lno $diff_b_lno
567 incr diff_b_lno
569 set tags d_s+
571 { -} {
572 if {$::conflict_state eq {CONTEXT}} {
573 incr diff_a_lno
576 set tags d_s-
578 {+ } {
579 set a_lno $diff_a_lno
580 set a_lno_tag green
581 incr diff_a_lno
583 set b_lno $diff_b_lno
584 set b_lno_tag green
585 incr diff_b_lno
587 set tags d_+s
589 {- } {
590 if {$::conflict_state eq {CONTEXT}} {
591 incr diff_c_lno
594 set tags d_-s
596 {--} {
597 set c_lno $diff_c_lno
598 set c_lno_tag red
599 incr diff_c_lno
601 set a_lno $diff_a_lno
602 set a_lno_tag red
603 incr diff_a_lno
605 set b_lno $diff_b_lno
606 #set b_lno_tag red
607 incr diff_b_lno
609 set tags d_--
611 {++} {
612 set b_lno $diff_b_lno
613 incr 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 { }]
622 set markup {}
623 set tags d$op
624 set b_lno_tag hide
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 { }]
633 set markup {}
634 set tags d|
635 set b_lno_tag hide
636 set ::conflict_state {BASE}
637 } elseif {$::conflict_state eq {BASE}} {
638 set line [string replace $line 0 1 {--}]
639 set markup {}
640 set tags d_--
641 } else {
642 set c_lno $diff_c_lno
643 set c_lno_tag green
644 incr diff_c_lno
646 set a_lno $diff_a_lno
647 set a_lno_tag green
648 incr diff_a_lno
650 set tags d_++
653 default {
654 puts "error: Unhandled 3 way diff marker: {$op}"
655 set tags {}
658 } elseif {$is_submodule_diff} {
659 if {$line == ""} continue
660 if {[regexp {^Submodule } $line]} {
661 set tags d_info
662 } elseif {[regexp {^\* } $line]} {
663 set line [string replace $line 0 1 {Submodule }]
664 set tags d_info
665 } else {
666 set op [string range $line 0 2]
667 switch -- $op {
668 { <} {set tags d_-}
669 { >} {set tags d_+}
670 { W} {set tags {}}
671 default {
672 puts "error: Unhandled submodule diff marker: {$op}"
673 set tags {}
677 } else {
678 set op [string index $line 0]
679 switch -- $op {
680 {@} {
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
690 set remember_hunk 1
691 set diff_a_mod_cnt 0
692 set diff_b_mod_cnt 0
693 set tags d_@
695 { } {
696 set a_lno $diff_a_lno
697 incr diff_a_lno
699 set b_lno $diff_b_lno
700 incr diff_b_lno
702 set tags {}
704 {-} {
705 set a_lno $diff_a_lno
706 incr diff_a_lno
707 set a_lno_tag red
708 incr diff_a_mod_cnt
710 set tags d_-
712 {+} {
713 set b_lno $diff_b_lno
714 incr diff_b_lno
715 set b_lno_tag green
716 incr diff_b_mod_cnt
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
722 set tags d$op
723 } else {
724 set tags d_+
727 default {
728 puts "error: Unhandled 2 way diff marker: {$op}"
729 set tags {}
733 set mark $diff_lno.0
734 if {$remember_hunk} {
735 set diff_hunk_mark $mark
738 if {[llength $markup] > 0} {
739 set tags {}
741 diff_append_line \
742 $line $tags \
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 {
752 set prefix clr
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}
764 if {[eof $fd]} {
765 close $fd
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
786 return
789 set diff_active 0
790 unlock_index
791 set scroll_pos [lindex $cont_info 0]
792 if {$scroll_pos ne {}} {
793 update
794 # TODO: scroll all
795 $ui_diff yview moveto $scroll_pos
797 ui_ready
799 if {[$ui_diff index end] eq {2.0}} {
800 handle_empty_diff
801 } else {
802 set diff_empty_count 0
805 set callback [lindex $cont_info 1]
806 if {$callback ne {}} {
807 eval $callback
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}} {
826 unlock_index
827 return
829 } else {
830 if {$revert} {
831 set failed_msg [mc "Failed to revert selected hunk."]
832 set apply_cmd {apply --reverse --whitespace=nowarn}
833 } else {
834 set failed_msg [mc "Failed to stage selected hunk."]
836 if {[string index $mi 1] ne {M}} {
837 unlock_index
838 return
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]
844 if {$s_lno eq {}} {
845 unlock_index
846 return
849 set e_lno [$ui_diff search -forwards -regexp ^@@ "$s_lno + 1 lines" end]
850 if {$e_lno eq {}} {
851 set e_lno end
854 if {[catch {
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]
860 close $p} err]} {
861 error_popup [append $failed_msg "\n\n$err"]
862 unlock_index
863 return
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"} {
873 set o _
874 } else {
875 set o ?
878 if {$current_diff_side eq $ui_index} {
879 set mi ${o}M
880 } elseif {$revert} {
881 set mi "[string index $mi 0]$o"
882 } elseif {[string index $mi 0] eq {_}} {
883 set mi M$o
884 } else {
885 set mi ?$o
887 unlock_index
888 display_file $current_diff_path $mi
889 # This should trigger shift to the next changed file
890 if {$o eq {_}} {
891 reshow_diff
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"]
903 set last $first
904 } else {
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"]
914 } else {
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."]
925 set to_context {+}
926 lappend apply_cmd --reverse
927 if {[string index $mi 0] ne {M}} {
928 unlock_index
929 return
931 } else {
932 if {$revert} {
933 set failed_msg [mc "Failed to revert selected line."]
934 set apply_cmd {apply --reverse --whitespace=nowarn}
935 set to_context {+}
936 } else {
937 set failed_msg [mc "Failed to stage selected line."]
938 set to_context {-}
940 if {[string index $mi 1] ne {M}} {
941 unlock_index
942 return
946 set wholepatch {}
948 while {$first_l < $last_l} {
949 set i_l [$ui_diff search -backwards -regexp ^@@ $first_l 0.0]
950 if {$i_l eq {}} {
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]
955 if {$i_l eq {}} {
956 unlock_index
957 return
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]} {
964 unlock_index
965 return
968 # There is a special situation to take care of. Consider this
969 # hunk:
971 # @@ -10,4 +10,4 @@
972 # context before
973 # -old 1
974 # -old 2
975 # +new 1
976 # +new 2
977 # context after
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:
983 # context before
984 # old 2
985 # new 1
986 # context after
988 # (By symmetry it is not possible to *un*stage "old 2" and "new
989 # 2".)
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
995 # patch:
997 # @@ -10,4 +10,5 @@
998 # context before
999 # +new 1
1000 # old 1
1001 # old 2
1002 # context after
1004 # But we do *not* treat "-" lines that are *un*staged in a
1005 # special way.
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:
1017 # @@ -10,4 +10,6 @@
1018 # context before
1019 # +new 1
1020 # +new 2
1021 # old 1
1022 # old 2
1023 # context after
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
1028 # "+" change lines.
1029 set pre_context {}
1031 set n 0
1032 set m 0
1033 set i_l [$ui_diff index "$i_l + 1 lines"]
1034 set patch {}
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]
1044 if {$c1 eq {-}} {
1045 set n [expr $n+1]
1046 set patch "$patch$pre_context$ln"
1047 set pre_context {}
1048 } else {
1049 set m [expr $m+1]
1050 set patch "$patch$ln"
1052 } elseif {$c1 ne {-} && $c1 ne {+}} {
1053 # context line
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]} {
1062 set n [expr $n+1]
1063 set m [expr $m+1]
1065 set pre_context {}
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]
1069 if {$c1 eq {-}} {
1070 set pre_context "$pre_context $ln"
1071 } else {
1072 set patch "$patch $ln"
1074 set n [expr $n+1]
1075 set m [expr $m+1]
1076 } else {
1077 # a change in the opposite direction of
1078 # to_context which is outside the range of
1079 # lines to apply.
1080 set patch "$patch$pre_context"
1081 set pre_context {}
1083 set i_l $next_l
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"]
1090 if {[catch {
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
1096 close $p} err]} {
1097 error_popup [append $failed_msg "\n\n$err"]
1100 unlock_index