2007-05-08 05:35:48 +02:00
|
|
|
# git-gui diff viewer
|
|
|
|
# Copyright (C) 2006, 2007 Shawn Pearce
|
|
|
|
|
2012-02-12 16:55:17 +01:00
|
|
|
proc apply_tab_size {{firsttab {}}} {
|
|
|
|
global have_tk85 repo_config ui_diff
|
|
|
|
|
|
|
|
set w [font measure font_diff "0"]
|
|
|
|
if {$have_tk85 && $firsttab != 0} {
|
|
|
|
$ui_diff configure -tabs [list [expr {$firsttab * $w}] [expr {($firsttab + $repo_config(gui.tabsize)) * $w}]]
|
|
|
|
} elseif {$have_tk85 || $repo_config(gui.tabsize) != 8} {
|
|
|
|
$ui_diff configure -tabs [expr {$repo_config(gui.tabsize) * $w}]
|
|
|
|
} else {
|
|
|
|
$ui_diff configure -tabs {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-08 05:35:48 +02:00
|
|
|
proc clear_diff {} {
|
|
|
|
global ui_diff current_diff_path current_diff_header
|
|
|
|
global ui_index ui_workdir
|
|
|
|
|
|
|
|
$ui_diff conf -state normal
|
|
|
|
$ui_diff delete 0.0 end
|
|
|
|
$ui_diff conf -state disabled
|
|
|
|
|
|
|
|
set current_diff_path {}
|
|
|
|
set current_diff_header {}
|
|
|
|
|
|
|
|
$ui_index tag remove in_diff 0.0 end
|
|
|
|
$ui_workdir tag remove in_diff 0.0 end
|
|
|
|
}
|
|
|
|
|
2008-11-16 19:46:48 +01:00
|
|
|
proc reshow_diff {{after {}}} {
|
2007-07-06 05:16:13 +02:00
|
|
|
global file_states file_lists
|
2007-05-08 05:35:48 +02:00
|
|
|
global current_diff_path current_diff_side
|
2008-07-27 08:35:38 +02:00
|
|
|
global ui_diff
|
2007-05-08 05:35:48 +02:00
|
|
|
|
|
|
|
set p $current_diff_path
|
|
|
|
if {$p eq {}} {
|
|
|
|
# No diff is being shown.
|
2008-08-30 23:02:56 +02:00
|
|
|
} elseif {$current_diff_side eq {}} {
|
2007-05-08 05:35:48 +02:00
|
|
|
clear_diff
|
2008-08-30 23:02:56 +02:00
|
|
|
} elseif {[catch {set s $file_states($p)}]
|
|
|
|
|| [lsearch -sorted -exact $file_lists($current_diff_side) $p] == -1} {
|
|
|
|
|
|
|
|
if {[find_next_diff $current_diff_side $p {} {[^O]}]} {
|
2008-11-16 19:46:48 +01:00
|
|
|
next_diff $after
|
2008-08-30 23:02:56 +02:00
|
|
|
} else {
|
|
|
|
clear_diff
|
|
|
|
}
|
2007-05-08 05:35:48 +02:00
|
|
|
} else {
|
2008-07-27 08:35:38 +02:00
|
|
|
set save_pos [lindex [$ui_diff yview] 0]
|
2008-11-16 19:46:48 +01:00
|
|
|
show_diff $p $current_diff_side {} $save_pos $after
|
2007-05-08 05:35:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-17 23:07:34 +02:00
|
|
|
proc force_diff_encoding {enc} {
|
|
|
|
global current_diff_path
|
|
|
|
|
|
|
|
if {$current_diff_path ne {}} {
|
|
|
|
force_path_encoding $current_diff_path $enc
|
|
|
|
reshow_diff
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-08 05:35:48 +02:00
|
|
|
proc handle_empty_diff {} {
|
|
|
|
global current_diff_path file_states file_lists
|
git-gui: Avoid an infinite rescan loop in handle_empty_diff.
If the index update machinery and git diff happen to disagree
on whether a particular file is modified, it may cause git-gui
to enter an infinite index rescan loop, where an empty diff
starts a rescan, which finds the same set of files modified,
and tries to display the diff for the first one, which happens
to be the empty one. A current example of a possible disagreement
point is the autocrlf filter.
This patch breaks the loop by using a global counter to track
the auto-rescans. The variable is reset whenever a non-empty
diff is displayed.
Another suggested approach, which is based on giving the
--exit-code argument to git diff, cannot be used, because
diff-files seems to trust the timestamps in the index, and
returns a non-zero code even if the file is actually
unchanged, which essentially defeats the purpose of the
auto-rescan logic.
Signed-off-by: Alexander Gavrilov <angavrilov@gmail.com>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2009-02-07 17:24:01 +01:00
|
|
|
global diff_empty_count
|
2007-05-08 05:35:48 +02:00
|
|
|
|
|
|
|
set path $current_diff_path
|
|
|
|
set s $file_states($path)
|
2010-07-30 10:11:02 +02:00
|
|
|
if {[lindex $s 0] ne {_M} || [has_textconv $path]} return
|
2007-05-08 05:35:48 +02:00
|
|
|
|
git-gui: Avoid an infinite rescan loop in handle_empty_diff.
If the index update machinery and git diff happen to disagree
on whether a particular file is modified, it may cause git-gui
to enter an infinite index rescan loop, where an empty diff
starts a rescan, which finds the same set of files modified,
and tries to display the diff for the first one, which happens
to be the empty one. A current example of a possible disagreement
point is the autocrlf filter.
This patch breaks the loop by using a global counter to track
the auto-rescans. The variable is reset whenever a non-empty
diff is displayed.
Another suggested approach, which is based on giving the
--exit-code argument to git diff, cannot be used, because
diff-files seems to trust the timestamps in the index, and
returns a non-zero code even if the file is actually
unchanged, which essentially defeats the purpose of the
auto-rescan logic.
Signed-off-by: Alexander Gavrilov <angavrilov@gmail.com>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2009-02-07 17:24:01 +01:00
|
|
|
# Prevent infinite rescan loops
|
|
|
|
incr diff_empty_count
|
|
|
|
if {$diff_empty_count > 1} return
|
|
|
|
|
2007-07-21 14:21:34 +02:00
|
|
|
info_popup [mc "No differences detected.
|
2007-05-08 05:35:48 +02:00
|
|
|
|
2007-07-21 14:21:34 +02:00
|
|
|
%s has no changes.
|
2007-05-08 05:35:48 +02:00
|
|
|
|
|
|
|
The modification date of this file was updated by another application, but the content within the file was not changed.
|
|
|
|
|
2007-07-21 14:21:34 +02:00
|
|
|
A rescan will be automatically started to find other files which may have the same state." [short_path $path]]
|
2007-05-08 05:35:48 +02:00
|
|
|
|
|
|
|
clear_diff
|
|
|
|
display_file $path __
|
2007-07-06 05:16:13 +02:00
|
|
|
rescan ui_ready 0
|
2007-05-08 05:35:48 +02:00
|
|
|
}
|
|
|
|
|
2008-09-20 10:19:18 +02:00
|
|
|
proc show_diff {path w {lno {}} {scroll_pos {}} {callback {}}} {
|
2007-05-08 05:35:48 +02:00
|
|
|
global file_states file_lists
|
2008-09-20 10:19:18 +02:00
|
|
|
global is_3way_diff is_conflict_diff diff_active repo_config
|
2007-07-06 05:16:13 +02:00
|
|
|
global ui_diff ui_index ui_workdir
|
2007-05-08 05:35:48 +02:00
|
|
|
global current_diff_path current_diff_side current_diff_header
|
2008-08-30 23:05:22 +02:00
|
|
|
global current_diff_queue
|
2007-05-08 05:35:48 +02:00
|
|
|
|
|
|
|
if {$diff_active || ![lock_index read]} return
|
|
|
|
|
|
|
|
clear_diff
|
|
|
|
if {$lno == {}} {
|
|
|
|
set lno [lsearch -sorted -exact $file_lists($w) $path]
|
|
|
|
if {$lno >= 0} {
|
|
|
|
incr lno
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if {$lno >= 1} {
|
|
|
|
$w tag add in_diff $lno.0 [expr {$lno + 1}].0
|
2008-08-30 23:02:56 +02:00
|
|
|
$w see $lno.0
|
2007-05-08 05:35:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
set s $file_states($path)
|
|
|
|
set m [lindex $s 0]
|
2008-09-20 10:19:18 +02:00
|
|
|
set is_conflict_diff 0
|
2007-05-08 05:35:48 +02:00
|
|
|
set current_diff_path $path
|
|
|
|
set current_diff_side $w
|
2008-08-30 23:05:22 +02:00
|
|
|
set current_diff_queue {}
|
2007-09-13 15:19:05 +02:00
|
|
|
ui_status [mc "Loading diff of %s..." [escape_path $path]]
|
2007-05-08 05:35:48 +02:00
|
|
|
|
2008-09-20 10:19:18 +02:00
|
|
|
set cont_info [list $scroll_pos $callback]
|
|
|
|
|
2012-02-12 16:55:17 +01:00
|
|
|
apply_tab_size 0
|
|
|
|
|
2008-08-30 23:05:22 +02:00
|
|
|
if {[string first {U} $m] >= 0} {
|
2008-09-20 10:19:18 +02:00
|
|
|
merge_load_stages $path [list show_unmerged_diff $cont_info]
|
2008-08-30 23:05:22 +02:00
|
|
|
} elseif {$m eq {_O}} {
|
2008-09-20 10:19:18 +02:00
|
|
|
show_other_diff $path $w $m $cont_info
|
2008-08-30 23:05:22 +02:00
|
|
|
} else {
|
2008-09-20 10:19:18 +02:00
|
|
|
start_show_diff $cont_info
|
2008-08-30 23:05:22 +02:00
|
|
|
}
|
2016-06-28 10:59:25 +02:00
|
|
|
|
|
|
|
global current_diff_path selected_paths
|
|
|
|
set selected_paths($current_diff_path) 1
|
2008-08-30 23:05:22 +02:00
|
|
|
}
|
|
|
|
|
2008-09-20 10:19:18 +02:00
|
|
|
proc show_unmerged_diff {cont_info} {
|
2008-08-30 23:05:22 +02:00
|
|
|
global current_diff_path current_diff_side
|
2008-09-20 10:19:18 +02:00
|
|
|
global merge_stages ui_diff is_conflict_diff
|
2008-08-30 23:05:22 +02:00
|
|
|
global current_diff_queue
|
|
|
|
|
|
|
|
if {$merge_stages(2) eq {}} {
|
2008-09-20 10:19:18 +02:00
|
|
|
set is_conflict_diff 1
|
2008-08-30 23:05:22 +02:00
|
|
|
lappend current_diff_queue \
|
2010-11-29 09:21:57 +01:00
|
|
|
[list [mc "LOCAL: deleted\nREMOTE:\n"] d= \
|
2008-08-30 23:05:22 +02:00
|
|
|
[list ":1:$current_diff_path" ":3:$current_diff_path"]]
|
|
|
|
} elseif {$merge_stages(3) eq {}} {
|
2008-09-20 10:19:18 +02:00
|
|
|
set is_conflict_diff 1
|
2008-08-30 23:05:22 +02:00
|
|
|
lappend current_diff_queue \
|
2010-11-29 09:21:57 +01:00
|
|
|
[list [mc "REMOTE: deleted\nLOCAL:\n"] d= \
|
2008-08-30 23:05:22 +02:00
|
|
|
[list ":1:$current_diff_path" ":2:$current_diff_path"]]
|
|
|
|
} elseif {[lindex $merge_stages(1) 0] eq {120000}
|
|
|
|
|| [lindex $merge_stages(2) 0] eq {120000}
|
|
|
|
|| [lindex $merge_stages(3) 0] eq {120000}} {
|
2008-09-20 10:19:18 +02:00
|
|
|
set is_conflict_diff 1
|
2008-08-30 23:05:22 +02:00
|
|
|
lappend current_diff_queue \
|
2010-11-29 09:21:57 +01:00
|
|
|
[list [mc "LOCAL:\n"] d= \
|
2008-08-30 23:05:22 +02:00
|
|
|
[list ":1:$current_diff_path" ":2:$current_diff_path"]]
|
|
|
|
lappend current_diff_queue \
|
2010-11-29 09:21:57 +01:00
|
|
|
[list [mc "REMOTE:\n"] d= \
|
2008-08-30 23:05:22 +02:00
|
|
|
[list ":1:$current_diff_path" ":3:$current_diff_path"]]
|
|
|
|
} else {
|
2008-09-20 10:19:18 +02:00
|
|
|
start_show_diff $cont_info
|
2008-08-30 23:05:22 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2008-09-20 10:19:18 +02:00
|
|
|
advance_diff_queue $cont_info
|
2008-08-30 23:05:22 +02:00
|
|
|
}
|
|
|
|
|
2008-09-20 10:19:18 +02:00
|
|
|
proc advance_diff_queue {cont_info} {
|
2008-08-30 23:05:22 +02:00
|
|
|
global current_diff_queue ui_diff
|
|
|
|
|
|
|
|
set item [lindex $current_diff_queue 0]
|
|
|
|
set current_diff_queue [lrange $current_diff_queue 1 end]
|
|
|
|
|
|
|
|
$ui_diff conf -state normal
|
|
|
|
$ui_diff insert end [lindex $item 0] [lindex $item 1]
|
|
|
|
$ui_diff conf -state disabled
|
|
|
|
|
2008-09-20 10:19:18 +02:00
|
|
|
start_show_diff $cont_info [lindex $item 2]
|
2008-08-30 23:05:22 +02:00
|
|
|
}
|
|
|
|
|
2008-09-20 10:19:18 +02:00
|
|
|
proc show_other_diff {path w m cont_info} {
|
2008-08-30 23:05:22 +02:00
|
|
|
global file_states file_lists
|
|
|
|
global is_3way_diff diff_active repo_config
|
|
|
|
global ui_diff ui_index ui_workdir
|
|
|
|
global current_diff_path current_diff_side current_diff_header
|
|
|
|
|
2007-05-08 05:35:48 +02:00
|
|
|
# - Git won't give us the diff, there's nothing to compare to!
|
|
|
|
#
|
|
|
|
if {$m eq {_O}} {
|
2008-10-03 10:28:49 +02:00
|
|
|
set max_sz 100000
|
2007-09-10 02:13:10 +02:00
|
|
|
set type unknown
|
2007-05-08 05:35:48 +02:00
|
|
|
if {[catch {
|
2007-09-10 02:13:10 +02:00
|
|
|
set type [file type $path]
|
|
|
|
switch -- $type {
|
|
|
|
directory {
|
|
|
|
set type submodule
|
|
|
|
set content {}
|
|
|
|
set sz 0
|
|
|
|
}
|
|
|
|
link {
|
2007-09-09 21:04:45 +02:00
|
|
|
set content [file readlink $path]
|
|
|
|
set sz [string length $content]
|
2007-09-10 02:13:10 +02:00
|
|
|
}
|
|
|
|
file {
|
2007-09-09 21:04:45 +02:00
|
|
|
set fd [open $path r]
|
2008-01-23 06:37:10 +01:00
|
|
|
fconfigure $fd \
|
|
|
|
-eofchar {} \
|
2008-09-17 23:07:32 +02:00
|
|
|
-encoding [get_path_encoding $path]
|
2007-09-09 21:04:45 +02:00
|
|
|
set content [read $fd $max_sz]
|
|
|
|
close $fd
|
|
|
|
set sz [file size $path]
|
|
|
|
}
|
2007-09-10 02:13:10 +02:00
|
|
|
default {
|
|
|
|
error "'$type' not supported"
|
|
|
|
}
|
|
|
|
}
|
2007-05-08 05:35:48 +02:00
|
|
|
} err ]} {
|
|
|
|
set diff_active 0
|
|
|
|
unlock_index
|
2007-09-13 15:19:05 +02:00
|
|
|
ui_status [mc "Unable to display %s" [escape_path $path]]
|
2007-09-14 07:50:09 +02:00
|
|
|
error_popup [strcat [mc "Error loading file:"] "\n\n$err"]
|
2007-05-08 05:35:48 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
$ui_diff conf -state normal
|
2007-09-10 02:13:10 +02:00
|
|
|
if {$type eq {submodule}} {
|
2016-05-08 12:52:57 +02:00
|
|
|
$ui_diff insert end \
|
|
|
|
"* [mc "Git Repository (subproject)"]\n" \
|
|
|
|
d_info
|
2007-09-10 02:13:10 +02:00
|
|
|
} elseif {![catch {set type [exec file $path]}]} {
|
2007-05-08 05:35:48 +02:00
|
|
|
set n [string length $path]
|
|
|
|
if {[string equal -length $n $path $type]} {
|
|
|
|
set type [string range $type $n end]
|
|
|
|
regsub {^:?\s*} $type {} type
|
|
|
|
}
|
2010-12-06 23:01:01 +01:00
|
|
|
$ui_diff insert end "* $type\n" d_info
|
2007-05-08 05:35:48 +02:00
|
|
|
}
|
|
|
|
if {[string first "\0" $content] != -1} {
|
|
|
|
$ui_diff insert end \
|
2007-09-13 15:19:05 +02:00
|
|
|
[mc "* Binary file (not showing content)."] \
|
2010-12-06 23:01:01 +01:00
|
|
|
d_info
|
2007-05-08 05:35:48 +02:00
|
|
|
} else {
|
|
|
|
if {$sz > $max_sz} {
|
2008-10-03 13:13:42 +02:00
|
|
|
$ui_diff insert end [mc \
|
|
|
|
"* Untracked file is %d bytes.
|
|
|
|
* Showing only first %d bytes.
|
2010-12-06 23:01:01 +01:00
|
|
|
" $sz $max_sz] d_info
|
2007-05-08 05:35:48 +02:00
|
|
|
}
|
|
|
|
$ui_diff insert end $content
|
|
|
|
if {$sz > $max_sz} {
|
2008-10-03 13:13:42 +02:00
|
|
|
$ui_diff insert end [mc "
|
|
|
|
* Untracked file clipped here by %s.
|
2007-05-08 05:35:48 +02:00
|
|
|
* To see the entire file, use an external editor.
|
2010-12-06 23:01:01 +01:00
|
|
|
" [appname]] d_info
|
2007-05-08 05:35:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$ui_diff conf -state disabled
|
|
|
|
set diff_active 0
|
|
|
|
unlock_index
|
2008-09-20 10:19:18 +02:00
|
|
|
set scroll_pos [lindex $cont_info 0]
|
2008-07-27 08:35:38 +02:00
|
|
|
if {$scroll_pos ne {}} {
|
|
|
|
update
|
|
|
|
$ui_diff yview moveto $scroll_pos
|
|
|
|
}
|
2007-07-06 05:16:13 +02:00
|
|
|
ui_ready
|
2008-09-20 10:19:18 +02:00
|
|
|
set callback [lindex $cont_info 1]
|
|
|
|
if {$callback ne {}} {
|
|
|
|
eval $callback
|
|
|
|
}
|
2007-05-08 05:35:48 +02:00
|
|
|
return
|
|
|
|
}
|
2008-08-30 23:05:22 +02:00
|
|
|
}
|
|
|
|
|
2010-11-16 10:21:52 +01:00
|
|
|
proc get_conflict_marker_size {path} {
|
|
|
|
set size 7
|
|
|
|
catch {
|
|
|
|
set fd_rc [eval [list git_read check-attr "conflict-marker-size" -- $path]]
|
|
|
|
set ret [gets $fd_rc line]
|
|
|
|
close $fd_rc
|
|
|
|
if {$ret > 0} {
|
|
|
|
regexp {.*: conflict-marker-size: (\d+)$} $line line size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $size
|
|
|
|
}
|
|
|
|
|
2008-09-20 10:19:18 +02:00
|
|
|
proc start_show_diff {cont_info {add_opts {}}} {
|
2008-08-30 23:05:22 +02:00
|
|
|
global file_states file_lists
|
2009-07-21 19:32:31 +02:00
|
|
|
global is_3way_diff is_submodule_diff diff_active repo_config
|
2008-08-30 23:05:22 +02:00
|
|
|
global ui_diff ui_index ui_workdir
|
|
|
|
global current_diff_path current_diff_side current_diff_header
|
|
|
|
|
|
|
|
set path $current_diff_path
|
|
|
|
set w $current_diff_side
|
|
|
|
|
|
|
|
set s $file_states($path)
|
|
|
|
set m [lindex $s 0]
|
|
|
|
set is_3way_diff 0
|
2009-07-21 19:32:31 +02:00
|
|
|
set is_submodule_diff 0
|
2008-08-30 23:05:22 +02:00
|
|
|
set diff_active 1
|
|
|
|
set current_diff_header {}
|
2010-11-16 10:21:52 +01:00
|
|
|
set conflict_size [get_conflict_marker_size $path]
|
2007-05-08 05:35:48 +02:00
|
|
|
|
2007-07-09 07:17:09 +02:00
|
|
|
set cmd [list]
|
2007-05-08 05:35:48 +02:00
|
|
|
if {$w eq $ui_index} {
|
|
|
|
lappend cmd diff-index
|
|
|
|
lappend cmd --cached
|
2014-04-08 21:30:51 +02:00
|
|
|
if {[git-version >= "1.7.2"]} {
|
|
|
|
lappend cmd --ignore-submodules=dirty
|
|
|
|
}
|
2007-05-08 05:35:48 +02:00
|
|
|
} elseif {$w eq $ui_workdir} {
|
2008-08-30 23:00:49 +02:00
|
|
|
if {[string first {U} $m] >= 0} {
|
2007-05-08 05:35:48 +02:00
|
|
|
lappend cmd diff
|
|
|
|
} else {
|
|
|
|
lappend cmd diff-files
|
|
|
|
}
|
|
|
|
}
|
2010-07-30 10:11:02 +02:00
|
|
|
if {![is_config_false gui.textconv] && [git-version >= 1.6.1]} {
|
|
|
|
lappend cmd --textconv
|
|
|
|
}
|
2007-05-08 05:35:48 +02:00
|
|
|
|
2010-01-23 23:04:12 +01:00
|
|
|
if {[string match {160000 *} [lindex $s 2]]
|
|
|
|
|| [string match {160000 *} [lindex $s 3]]} {
|
|
|
|
set is_submodule_diff 1
|
|
|
|
|
|
|
|
if {[git-version >= "1.6.6"]} {
|
|
|
|
lappend cmd --submodule
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-08 05:35:48 +02:00
|
|
|
lappend cmd -p
|
2010-10-22 17:14:38 +02:00
|
|
|
lappend cmd --color
|
2011-01-21 11:59:45 +01:00
|
|
|
set cmd [concat $cmd $repo_config(gui.diffopts)]
|
2008-08-30 18:45:27 +02:00
|
|
|
if {$repo_config(gui.diffcontext) >= 1} {
|
2007-05-08 05:35:48 +02:00
|
|
|
lappend cmd "-U$repo_config(gui.diffcontext)"
|
|
|
|
}
|
|
|
|
if {$w eq $ui_index} {
|
|
|
|
lappend cmd [PARENT]
|
|
|
|
}
|
2008-08-30 23:05:22 +02:00
|
|
|
if {$add_opts ne {}} {
|
|
|
|
eval lappend cmd $add_opts
|
|
|
|
} else {
|
|
|
|
lappend cmd --
|
|
|
|
lappend cmd $path
|
|
|
|
}
|
2007-05-08 05:35:48 +02:00
|
|
|
|
2010-01-23 23:04:12 +01:00
|
|
|
if {$is_submodule_diff && [git-version < "1.6.6"]} {
|
2009-08-26 22:25:15 +02:00
|
|
|
if {$w eq $ui_index} {
|
2009-08-27 02:39:45 +02:00
|
|
|
set cmd [list submodule summary --cached -- $path]
|
2009-08-26 22:25:15 +02:00
|
|
|
} else {
|
2009-08-27 02:39:45 +02:00
|
|
|
set cmd [list submodule summary --files -- $path]
|
2009-08-26 22:25:15 +02:00
|
|
|
}
|
2009-07-21 19:32:31 +02:00
|
|
|
}
|
|
|
|
|
2007-07-09 07:17:09 +02:00
|
|
|
if {[catch {set fd [eval git_read --nice $cmd]} err]} {
|
2007-05-08 05:35:48 +02:00
|
|
|
set diff_active 0
|
|
|
|
unlock_index
|
2007-09-13 15:19:05 +02:00
|
|
|
ui_status [mc "Unable to display %s" [escape_path $path]]
|
2007-09-14 07:50:09 +02:00
|
|
|
error_popup [strcat [mc "Error loading diff:"] "\n\n$err"]
|
2007-05-08 05:35:48 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2008-09-05 06:46:56 +02:00
|
|
|
set ::current_diff_inheader 1
|
2007-05-08 05:35:48 +02:00
|
|
|
fconfigure $fd \
|
|
|
|
-blocking 0 \
|
2008-09-17 23:07:32 +02:00
|
|
|
-encoding [get_path_encoding $path] \
|
2008-01-23 06:37:10 +01:00
|
|
|
-translation lf
|
2010-11-16 10:21:52 +01:00
|
|
|
fileevent $fd readable [list read_diff $fd $conflict_size $cont_info]
|
2007-05-08 05:35:48 +02:00
|
|
|
}
|
|
|
|
|
2010-10-22 17:14:38 +02:00
|
|
|
proc parse_color_line {line} {
|
|
|
|
set start 0
|
|
|
|
set result ""
|
|
|
|
set markup [list]
|
|
|
|
set regexp {\033\[((?:\d+;)*\d+)?m}
|
2010-11-15 11:00:33 +01:00
|
|
|
set need_reset 0
|
2010-10-22 17:14:38 +02:00
|
|
|
while {[regexp -indices -start $start $regexp $line match code]} {
|
|
|
|
foreach {begin end} $match break
|
|
|
|
append result [string range $line $start [expr {$begin - 1}]]
|
2010-11-15 11:00:33 +01:00
|
|
|
set pos [string length $result]
|
|
|
|
set col [eval [linsert $code 0 string range $line]]
|
2010-10-22 17:14:38 +02:00
|
|
|
set start [incr end]
|
2010-11-15 11:00:33 +01:00
|
|
|
if {$col eq "0" || $col eq ""} {
|
|
|
|
if {!$need_reset} continue
|
|
|
|
set need_reset 0
|
|
|
|
} else {
|
|
|
|
set need_reset 1
|
|
|
|
}
|
|
|
|
lappend markup $pos $col
|
2010-10-22 17:14:38 +02:00
|
|
|
}
|
|
|
|
append result [string range $line $start end]
|
|
|
|
if {[llength $markup] < 4} {set markup {}}
|
|
|
|
return [list $result $markup]
|
|
|
|
}
|
|
|
|
|
2010-11-16 10:21:52 +01:00
|
|
|
proc read_diff {fd conflict_size cont_info} {
|
2009-07-21 19:32:31 +02:00
|
|
|
global ui_diff diff_active is_submodule_diff
|
2008-09-20 10:19:18 +02:00
|
|
|
global is_3way_diff is_conflict_diff current_diff_header
|
2008-08-30 23:05:22 +02:00
|
|
|
global current_diff_queue
|
git-gui: Avoid an infinite rescan loop in handle_empty_diff.
If the index update machinery and git diff happen to disagree
on whether a particular file is modified, it may cause git-gui
to enter an infinite index rescan loop, where an empty diff
starts a rescan, which finds the same set of files modified,
and tries to display the diff for the first one, which happens
to be the empty one. A current example of a possible disagreement
point is the autocrlf filter.
This patch breaks the loop by using a global counter to track
the auto-rescans. The variable is reset whenever a non-empty
diff is displayed.
Another suggested approach, which is based on giving the
--exit-code argument to git diff, cannot be used, because
diff-files seems to trust the timestamps in the index, and
returns a non-zero code even if the file is actually
unchanged, which essentially defeats the purpose of the
auto-rescan logic.
Signed-off-by: Alexander Gavrilov <angavrilov@gmail.com>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2009-02-07 17:24:01 +01:00
|
|
|
global diff_empty_count
|
2007-05-08 05:35:48 +02:00
|
|
|
|
|
|
|
$ui_diff conf -state normal
|
|
|
|
while {[gets $fd line] >= 0} {
|
2010-10-22 17:14:38 +02:00
|
|
|
foreach {line markup} [parse_color_line $line] break
|
|
|
|
set line [string map {\033 ^} $line]
|
|
|
|
|
2010-12-09 21:47:57 +01:00
|
|
|
set tags {}
|
|
|
|
|
2010-12-09 21:47:52 +01:00
|
|
|
# -- Check for start of diff header.
|
|
|
|
if { [string match {diff --git *} $line]
|
|
|
|
|| [string match {diff --cc *} $line]
|
|
|
|
|| [string match {diff --combined *} $line]} {
|
|
|
|
set ::current_diff_inheader 1
|
|
|
|
}
|
|
|
|
|
|
|
|
# -- Check for end of diff header (any hunk line will do this).
|
2007-05-08 05:35:48 +02:00
|
|
|
#
|
2010-12-09 21:47:52 +01:00
|
|
|
if {[regexp {^@@+ } $line]} {set ::current_diff_inheader 0}
|
|
|
|
|
2010-12-09 21:47:56 +01:00
|
|
|
# -- Automatically detect if this is a 3 way diff.
|
|
|
|
#
|
2012-02-12 16:55:17 +01:00
|
|
|
if {[string match {@@@ *} $line]} {
|
|
|
|
set is_3way_diff 1
|
|
|
|
apply_tab_size 1
|
|
|
|
}
|
2010-12-09 21:47:56 +01:00
|
|
|
|
2008-09-05 06:46:56 +02:00
|
|
|
if {$::current_diff_inheader} {
|
2010-12-09 21:47:58 +01:00
|
|
|
|
|
|
|
# -- These two lines stop a diff header and shouldn't be in there
|
|
|
|
if { [string match {Binary files * and * differ} $line]
|
|
|
|
|| [regexp {^\* Unmerged path } $line]} {
|
|
|
|
set ::current_diff_inheader 0
|
|
|
|
} else {
|
|
|
|
append current_diff_header $line "\n"
|
|
|
|
}
|
2010-12-09 21:47:52 +01:00
|
|
|
|
|
|
|
# -- Cleanup uninteresting diff header lines.
|
|
|
|
#
|
2008-09-05 06:46:56 +02:00
|
|
|
if { [string match {diff --git *} $line]
|
|
|
|
|| [string match {diff --cc *} $line]
|
|
|
|
|| [string match {diff --combined *} $line]
|
|
|
|
|| [string match {--- *} $line]
|
2010-12-09 21:47:53 +01:00
|
|
|
|| [string match {+++ *} $line]
|
|
|
|
|| [string match {index *} $line]} {
|
2008-09-05 06:46:56 +02:00
|
|
|
continue
|
|
|
|
}
|
2010-12-09 21:47:52 +01:00
|
|
|
|
2010-12-09 21:47:54 +01:00
|
|
|
# -- Name it symlink, not 120000
|
|
|
|
# Note, that the original line is in $current_diff_header
|
|
|
|
regsub {^(deleted|new) file mode 120000} $line {\1 symlink} line
|
2007-05-08 05:35:48 +02:00
|
|
|
|
2010-12-09 21:47:59 +01:00
|
|
|
} elseif { $line eq {\ No newline at end of file}} {
|
|
|
|
# -- Handle some special lines
|
2007-05-08 05:35:48 +02:00
|
|
|
} elseif {$is_3way_diff} {
|
|
|
|
set op [string range $line 0 1]
|
|
|
|
switch -- $op {
|
|
|
|
{ } {set tags {}}
|
|
|
|
{@@} {set tags d_@}
|
|
|
|
{ +} {set tags d_s+}
|
|
|
|
{ -} {set tags d_s-}
|
|
|
|
{+ } {set tags d_+s}
|
|
|
|
{- } {set tags d_-s}
|
|
|
|
{--} {set tags d_--}
|
|
|
|
{++} {
|
2010-11-16 10:21:52 +01:00
|
|
|
set regexp [string map [list %conflict_size $conflict_size]\
|
|
|
|
{^\+\+([<>=]){%conflict_size}(?: |$)}]
|
|
|
|
if {[regexp $regexp $line _g op]} {
|
2008-09-20 10:19:18 +02:00
|
|
|
set is_conflict_diff 1
|
2007-05-08 05:35:48 +02:00
|
|
|
set line [string replace $line 0 1 { }]
|
|
|
|
set tags d$op
|
|
|
|
} else {
|
|
|
|
set tags d_++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default {
|
|
|
|
puts "error: Unhandled 3 way diff marker: {$op}"
|
|
|
|
set tags {}
|
|
|
|
}
|
|
|
|
}
|
2009-07-21 19:32:31 +02:00
|
|
|
} elseif {$is_submodule_diff} {
|
|
|
|
if {$line == ""} continue
|
2010-01-23 23:04:12 +01:00
|
|
|
if {[regexp {^Submodule } $line]} {
|
2010-12-06 23:01:01 +01:00
|
|
|
set tags d_info
|
2010-01-23 23:04:12 +01:00
|
|
|
} elseif {[regexp {^\* } $line]} {
|
2009-07-21 19:32:31 +02:00
|
|
|
set line [string replace $line 0 1 {Submodule }]
|
2010-12-06 23:01:01 +01:00
|
|
|
set tags d_info
|
2009-07-21 19:32:31 +02:00
|
|
|
} else {
|
|
|
|
set op [string range $line 0 2]
|
|
|
|
switch -- $op {
|
|
|
|
{ <} {set tags d_-}
|
|
|
|
{ >} {set tags d_+}
|
|
|
|
{ W} {set tags {}}
|
|
|
|
default {
|
|
|
|
puts "error: Unhandled submodule diff marker: {$op}"
|
|
|
|
set tags {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-05-08 05:35:48 +02:00
|
|
|
} else {
|
|
|
|
set op [string index $line 0]
|
|
|
|
switch -- $op {
|
|
|
|
{ } {set tags {}}
|
|
|
|
{@} {set tags d_@}
|
|
|
|
{-} {set tags d_-}
|
|
|
|
{+} {
|
2010-11-16 10:21:52 +01:00
|
|
|
set regexp [string map [list %conflict_size $conflict_size]\
|
|
|
|
{^\+([<>=]){%conflict_size}(?: |$)}]
|
|
|
|
if {[regexp $regexp $line _g op]} {
|
2008-09-20 10:19:18 +02:00
|
|
|
set is_conflict_diff 1
|
2007-05-08 05:35:48 +02:00
|
|
|
set tags d$op
|
|
|
|
} else {
|
|
|
|
set tags d_+
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default {
|
|
|
|
puts "error: Unhandled 2 way diff marker: {$op}"
|
|
|
|
set tags {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-22 17:14:38 +02:00
|
|
|
set mark [$ui_diff index "end - 1 line linestart"]
|
2007-05-08 05:35:48 +02:00
|
|
|
$ui_diff insert end $line $tags
|
|
|
|
if {[string index $line end] eq "\r"} {
|
|
|
|
$ui_diff tag add d_cr {end - 2c}
|
|
|
|
}
|
|
|
|
$ui_diff insert end "\n" $tags
|
2010-10-22 17:14:38 +02:00
|
|
|
|
|
|
|
foreach {posbegin colbegin posend colend} $markup {
|
|
|
|
set prefix clr
|
2010-11-19 23:22:20 +01:00
|
|
|
foreach style [lsort -integer [split $colbegin ";"]] {
|
2010-10-22 17:14:38 +02:00
|
|
|
if {$style eq "7"} {append prefix i; continue}
|
2010-11-19 11:00:49 +01:00
|
|
|
if {$style != 4 && ($style < 30 || $style > 47)} {continue}
|
2010-10-22 17:14:38 +02:00
|
|
|
set a "$mark linestart + $posbegin chars"
|
|
|
|
set b "$mark linestart + $posend chars"
|
|
|
|
catch {$ui_diff tag add $prefix$style $a $b}
|
|
|
|
}
|
|
|
|
}
|
2007-05-08 05:35:48 +02:00
|
|
|
}
|
|
|
|
$ui_diff conf -state disabled
|
|
|
|
|
|
|
|
if {[eof $fd]} {
|
|
|
|
close $fd
|
2008-08-30 23:05:22 +02:00
|
|
|
|
|
|
|
if {$current_diff_queue ne {}} {
|
2008-09-20 10:19:18 +02:00
|
|
|
advance_diff_queue $cont_info
|
2008-08-30 23:05:22 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2007-05-08 05:35:48 +02:00
|
|
|
set diff_active 0
|
|
|
|
unlock_index
|
2008-09-20 10:19:18 +02:00
|
|
|
set scroll_pos [lindex $cont_info 0]
|
2008-07-27 08:35:38 +02:00
|
|
|
if {$scroll_pos ne {}} {
|
|
|
|
update
|
|
|
|
$ui_diff yview moveto $scroll_pos
|
|
|
|
}
|
2007-07-06 05:16:13 +02:00
|
|
|
ui_ready
|
2007-05-08 05:35:48 +02:00
|
|
|
|
|
|
|
if {[$ui_diff index end] eq {2.0}} {
|
|
|
|
handle_empty_diff
|
git-gui: Avoid an infinite rescan loop in handle_empty_diff.
If the index update machinery and git diff happen to disagree
on whether a particular file is modified, it may cause git-gui
to enter an infinite index rescan loop, where an empty diff
starts a rescan, which finds the same set of files modified,
and tries to display the diff for the first one, which happens
to be the empty one. A current example of a possible disagreement
point is the autocrlf filter.
This patch breaks the loop by using a global counter to track
the auto-rescans. The variable is reset whenever a non-empty
diff is displayed.
Another suggested approach, which is based on giving the
--exit-code argument to git diff, cannot be used, because
diff-files seems to trust the timestamps in the index, and
returns a non-zero code even if the file is actually
unchanged, which essentially defeats the purpose of the
auto-rescan logic.
Signed-off-by: Alexander Gavrilov <angavrilov@gmail.com>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2009-02-07 17:24:01 +01:00
|
|
|
} else {
|
|
|
|
set diff_empty_count 0
|
2007-05-08 05:35:48 +02:00
|
|
|
}
|
git-gui: Avoid an infinite rescan loop in handle_empty_diff.
If the index update machinery and git diff happen to disagree
on whether a particular file is modified, it may cause git-gui
to enter an infinite index rescan loop, where an empty diff
starts a rescan, which finds the same set of files modified,
and tries to display the diff for the first one, which happens
to be the empty one. A current example of a possible disagreement
point is the autocrlf filter.
This patch breaks the loop by using a global counter to track
the auto-rescans. The variable is reset whenever a non-empty
diff is displayed.
Another suggested approach, which is based on giving the
--exit-code argument to git diff, cannot be used, because
diff-files seems to trust the timestamps in the index, and
returns a non-zero code even if the file is actually
unchanged, which essentially defeats the purpose of the
auto-rescan logic.
Signed-off-by: Alexander Gavrilov <angavrilov@gmail.com>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2009-02-07 17:24:01 +01:00
|
|
|
|
2008-09-20 10:19:18 +02:00
|
|
|
set callback [lindex $cont_info 1]
|
|
|
|
if {$callback ne {}} {
|
|
|
|
eval $callback
|
|
|
|
}
|
2007-05-08 05:35:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc apply_hunk {x y} {
|
|
|
|
global current_diff_path current_diff_header current_diff_side
|
|
|
|
global ui_diff ui_index file_states
|
|
|
|
|
|
|
|
if {$current_diff_path eq {} || $current_diff_header eq {}} return
|
|
|
|
if {![lock_index apply_hunk]} return
|
|
|
|
|
2007-07-09 07:17:09 +02:00
|
|
|
set apply_cmd {apply --cached --whitespace=nowarn}
|
2007-05-08 05:35:48 +02:00
|
|
|
set mi [lindex $file_states($current_diff_path) 0]
|
|
|
|
if {$current_diff_side eq $ui_index} {
|
2007-07-21 14:21:34 +02:00
|
|
|
set failed_msg [mc "Failed to unstage selected hunk."]
|
2007-05-08 05:35:48 +02:00
|
|
|
lappend apply_cmd --reverse
|
|
|
|
if {[string index $mi 0] ne {M}} {
|
|
|
|
unlock_index
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2007-07-21 14:21:34 +02:00
|
|
|
set failed_msg [mc "Failed to stage selected hunk."]
|
2007-05-08 05:35:48 +02:00
|
|
|
if {[string index $mi 1] ne {M}} {
|
|
|
|
unlock_index
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set s_lno [lindex [split [$ui_diff index @$x,$y] .] 0]
|
|
|
|
set s_lno [$ui_diff search -backwards -regexp ^@@ $s_lno.0 0.0]
|
|
|
|
if {$s_lno eq {}} {
|
|
|
|
unlock_index
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
set e_lno [$ui_diff search -forwards -regexp ^@@ "$s_lno + 1 lines" end]
|
|
|
|
if {$e_lno eq {}} {
|
|
|
|
set e_lno end
|
|
|
|
}
|
|
|
|
|
|
|
|
if {[catch {
|
2008-09-17 23:07:32 +02:00
|
|
|
set enc [get_path_encoding $current_diff_path]
|
2007-07-09 07:17:09 +02:00
|
|
|
set p [eval git_write $apply_cmd]
|
2008-09-17 23:07:32 +02:00
|
|
|
fconfigure $p -translation binary -encoding $enc
|
2007-05-08 05:35:48 +02:00
|
|
|
puts -nonewline $p $current_diff_header
|
|
|
|
puts -nonewline $p [$ui_diff get $s_lno $e_lno]
|
|
|
|
close $p} err]} {
|
2016-05-08 12:52:57 +02:00
|
|
|
error_popup "$failed_msg\n\n$err"
|
2007-05-08 05:35:48 +02:00
|
|
|
unlock_index
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
$ui_diff conf -state normal
|
|
|
|
$ui_diff delete $s_lno $e_lno
|
|
|
|
$ui_diff conf -state disabled
|
|
|
|
|
|
|
|
if {[$ui_diff get 1.0 end] eq "\n"} {
|
|
|
|
set o _
|
|
|
|
} else {
|
|
|
|
set o ?
|
|
|
|
}
|
|
|
|
|
|
|
|
if {$current_diff_side eq $ui_index} {
|
|
|
|
set mi ${o}M
|
|
|
|
} elseif {[string index $mi 0] eq {_}} {
|
|
|
|
set mi M$o
|
|
|
|
} else {
|
|
|
|
set mi ?$o
|
|
|
|
}
|
|
|
|
unlock_index
|
|
|
|
display_file $current_diff_path $mi
|
2008-08-30 23:02:56 +02:00
|
|
|
# This should trigger shift to the next changed file
|
2007-05-08 05:35:48 +02:00
|
|
|
if {$o eq {_}} {
|
2008-08-30 23:02:56 +02:00
|
|
|
reshow_diff
|
2007-05-08 05:35:48 +02:00
|
|
|
}
|
|
|
|
}
|
2008-06-27 09:22:01 +02:00
|
|
|
|
2009-12-08 01:22:43 +01:00
|
|
|
proc apply_range_or_line {x y} {
|
2008-06-27 09:22:01 +02:00
|
|
|
global current_diff_path current_diff_header current_diff_side
|
|
|
|
global ui_diff ui_index file_states
|
|
|
|
|
2009-12-08 01:22:43 +01:00
|
|
|
set selected [$ui_diff tag nextrange sel 0.0]
|
|
|
|
|
|
|
|
if {$selected == {}} {
|
|
|
|
set first [$ui_diff index "@$x,$y"]
|
|
|
|
set last $first
|
|
|
|
} else {
|
|
|
|
set first [lindex $selected 0]
|
|
|
|
set last [lindex $selected 1]
|
|
|
|
}
|
|
|
|
|
|
|
|
set first_l [$ui_diff index "$first linestart"]
|
|
|
|
set last_l [$ui_diff index "$last lineend"]
|
|
|
|
|
2008-06-27 09:22:01 +02:00
|
|
|
if {$current_diff_path eq {} || $current_diff_header eq {}} return
|
|
|
|
if {![lock_index apply_hunk]} return
|
|
|
|
|
|
|
|
set apply_cmd {apply --cached --whitespace=nowarn}
|
|
|
|
set mi [lindex $file_states($current_diff_path) 0]
|
|
|
|
if {$current_diff_side eq $ui_index} {
|
|
|
|
set failed_msg [mc "Failed to unstage selected line."]
|
|
|
|
set to_context {+}
|
|
|
|
lappend apply_cmd --reverse
|
|
|
|
if {[string index $mi 0] ne {M}} {
|
|
|
|
unlock_index
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
set failed_msg [mc "Failed to stage selected line."]
|
|
|
|
set to_context {-}
|
|
|
|
if {[string index $mi 1] ne {M}} {
|
|
|
|
unlock_index
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-08 01:22:43 +01:00
|
|
|
set wholepatch {}
|
2008-06-27 09:22:01 +02:00
|
|
|
|
2009-12-08 01:22:43 +01:00
|
|
|
while {$first_l < $last_l} {
|
|
|
|
set i_l [$ui_diff search -backwards -regexp ^@@ $first_l 0.0]
|
|
|
|
if {$i_l eq {}} {
|
|
|
|
# If there's not a @@ above, then the selected range
|
|
|
|
# must have come before the first_l @@
|
|
|
|
set i_l [$ui_diff search -regexp ^@@ $first_l $last_l]
|
|
|
|
}
|
|
|
|
if {$i_l eq {}} {
|
|
|
|
unlock_index
|
|
|
|
return
|
|
|
|
}
|
|
|
|
# $i_l is now at the beginning of a line
|
2008-06-27 09:22:01 +02:00
|
|
|
|
2009-12-08 01:22:43 +01:00
|
|
|
# pick start line number from hunk header
|
|
|
|
set hh [$ui_diff get $i_l "$i_l + 1 lines"]
|
|
|
|
set hh [lindex [split $hh ,] 0]
|
|
|
|
set hln [lindex [split $hh -] 1]
|
2018-01-09 15:32:54 +01:00
|
|
|
set hln [lindex [split $hln " "] 0]
|
2008-06-27 09:22:01 +02:00
|
|
|
|
2009-12-08 01:22:43 +01:00
|
|
|
# There is a special situation to take care of. Consider this
|
|
|
|
# hunk:
|
|
|
|
#
|
|
|
|
# @@ -10,4 +10,4 @@
|
|
|
|
# context before
|
|
|
|
# -old 1
|
|
|
|
# -old 2
|
|
|
|
# +new 1
|
|
|
|
# +new 2
|
|
|
|
# context after
|
|
|
|
#
|
|
|
|
# We used to keep the context lines in the order they appear in
|
|
|
|
# the hunk. But then it is not possible to correctly stage only
|
|
|
|
# "-old 1" and "+new 1" - it would result in this staged text:
|
|
|
|
#
|
|
|
|
# context before
|
|
|
|
# old 2
|
|
|
|
# new 1
|
|
|
|
# context after
|
|
|
|
#
|
|
|
|
# (By symmetry it is not possible to *un*stage "old 2" and "new
|
|
|
|
# 2".)
|
|
|
|
#
|
|
|
|
# We resolve the problem by introducing an asymmetry, namely,
|
|
|
|
# when a "+" line is *staged*, it is moved in front of the
|
|
|
|
# context lines that are generated from the "-" lines that are
|
|
|
|
# immediately before the "+" block. That is, we construct this
|
|
|
|
# patch:
|
|
|
|
#
|
|
|
|
# @@ -10,4 +10,5 @@
|
|
|
|
# context before
|
|
|
|
# +new 1
|
|
|
|
# old 1
|
|
|
|
# old 2
|
|
|
|
# context after
|
|
|
|
#
|
|
|
|
# But we do *not* treat "-" lines that are *un*staged in a
|
|
|
|
# special way.
|
|
|
|
#
|
|
|
|
# With this asymmetry it is possible to stage the change "old
|
|
|
|
# 1" -> "new 1" directly, and to stage the change "old 2" ->
|
|
|
|
# "new 2" by first staging the entire hunk and then unstaging
|
|
|
|
# the change "old 1" -> "new 1".
|
|
|
|
#
|
|
|
|
# Applying multiple lines adds complexity to the special
|
|
|
|
# situation. The pre_context must be moved after the entire
|
|
|
|
# first block of consecutive staged "+" lines, so that
|
|
|
|
# staging both additions gives the following patch:
|
|
|
|
#
|
|
|
|
# @@ -10,4 +10,6 @@
|
|
|
|
# context before
|
|
|
|
# +new 1
|
|
|
|
# +new 2
|
|
|
|
# old 1
|
|
|
|
# old 2
|
|
|
|
# context after
|
|
|
|
|
|
|
|
# This is non-empty if and only if we are _staging_ changes;
|
|
|
|
# then it accumulates the consecutive "-" lines (after
|
|
|
|
# converting them to context lines) in order to be moved after
|
|
|
|
# "+" change lines.
|
|
|
|
set pre_context {}
|
|
|
|
|
|
|
|
set n 0
|
|
|
|
set m 0
|
|
|
|
set i_l [$ui_diff index "$i_l + 1 lines"]
|
|
|
|
set patch {}
|
|
|
|
while {[$ui_diff compare $i_l < "end - 1 chars"] &&
|
|
|
|
[$ui_diff get $i_l "$i_l + 2 chars"] ne {@@}} {
|
|
|
|
set next_l [$ui_diff index "$i_l + 1 lines"]
|
|
|
|
set c1 [$ui_diff get $i_l]
|
|
|
|
if {[$ui_diff compare $first_l <= $i_l] &&
|
|
|
|
[$ui_diff compare $i_l < $last_l] &&
|
|
|
|
($c1 eq {-} || $c1 eq {+})} {
|
|
|
|
# a line to stage/unstage
|
|
|
|
set ln [$ui_diff get $i_l $next_l]
|
|
|
|
if {$c1 eq {-}} {
|
|
|
|
set n [expr $n+1]
|
|
|
|
set patch "$patch$pre_context$ln"
|
|
|
|
set pre_context {}
|
|
|
|
} else {
|
|
|
|
set m [expr $m+1]
|
|
|
|
set patch "$patch$ln"
|
|
|
|
}
|
|
|
|
} elseif {$c1 ne {-} && $c1 ne {+}} {
|
|
|
|
# context line
|
|
|
|
set ln [$ui_diff get $i_l $next_l]
|
git-gui: "Stage Line": Treat independent changes in adjacent lines better
Assume that we want to commit these states:
Old state == HEAD Intermediate state New state
--------------------------------------------------------
context before context before context before
old 1 new 1 new 1
old 2 old 2 new 2
context after context after context after
that is, want to commit two changes in this order:
1. transform "old 1" into "new 1"
2. transform "old 2" into "new 2"
[This discussion and this patch is about this very case and one other case
as outlined below; any other intermediate states that one could imagine are
not affected by this patch.]
Now assume further, that we have not staged and commited anything, but we
have already changed the working file to the new state. Then we will see
this hunk in the "Unstaged Changes":
@@ -1,4 +1,4 @@
context before
-old 1
-old 2
+new 1
+new 2
context after
The obvious way to stage the intermediate state is to apply "Stage This
Line" to "-old 1" and "+new 1". Unfortunately, this resulted in this
intermediate state:
context before
old 2
new 1
context after
which is not what we wanted. In fact, it was impossible to stage the
intermediate state using "Stage Line". The crux was that if a "+" line was
staged, then the "-" lines were converted to context lines and arranged
*before* the "+" line in the forged hunk that we fed to 'git apply'.
With this patch we now treat "+" lines that are staged differently. In
particular, the "-" lines before the "+" block are moved *after* the
staged "+" line. Now it is possible to get the correct intermediate state
by staging "-old 1" and "+new 1". Problem solved.
But there is a catch.
Noticing that we didn't get the right intermediate state by staging
"-old 1" and "+new 1", we could have had the idea to stage the complete
hunk and to *unstage* "-old 2" and "+new 2". But... the result is the same.
The reason is that there is the exact symmetric problem with unstaging the
last "-" and "+" line that are in adjacent blocks of "-" and "+" lines.
This patch does *not* change the way in which "-" lines are *unstaged*.
Why? Because if we did (i.e. move "+" lines before the "-" line after
converting them to context lines), then it would be impossible to stage
this intermediate state:
context before
old 1
new 2
context after
that is, it would be impossible to stage the two independet changes in the
opposite order.
Let's look at this case a bit further: The obvious way to get this
intermediate state would be to apply "Stage This Line" to "-old 2" and
"+new 2". Before this patch, this worked as expected. With this patch, it
does not work as expected, but it can still be achieved by first staging
the entire hunk, then *unstaging* "-old 1" and "+new 1".
In summary, this patch makes a common case possible, at the expense that
a less common case is made more complicated for the user.
Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2008-07-17 15:21:51 +02:00
|
|
|
set patch "$patch$pre_context$ln"
|
2013-05-09 18:30:02 +02:00
|
|
|
# Skip the "\ No newline at end of
|
|
|
|
# file". Depending on the locale setting
|
|
|
|
# we don't know what this line looks
|
|
|
|
# like exactly. The only thing we do
|
|
|
|
# know is that it starts with "\ "
|
|
|
|
if {![string match {\\ *} $ln]} {
|
|
|
|
set n [expr $n+1]
|
|
|
|
set m [expr $m+1]
|
|
|
|
}
|
2009-12-08 01:22:43 +01:00
|
|
|
set pre_context {}
|
|
|
|
} elseif {$c1 eq $to_context} {
|
|
|
|
# turn change line into context line
|
|
|
|
set ln [$ui_diff get "$i_l + 1 chars" $next_l]
|
|
|
|
if {$c1 eq {-}} {
|
|
|
|
set pre_context "$pre_context $ln"
|
|
|
|
} else {
|
|
|
|
set patch "$patch $ln"
|
|
|
|
}
|
|
|
|
set n [expr $n+1]
|
|
|
|
set m [expr $m+1]
|
git-gui: "Stage Line": Treat independent changes in adjacent lines better
Assume that we want to commit these states:
Old state == HEAD Intermediate state New state
--------------------------------------------------------
context before context before context before
old 1 new 1 new 1
old 2 old 2 new 2
context after context after context after
that is, want to commit two changes in this order:
1. transform "old 1" into "new 1"
2. transform "old 2" into "new 2"
[This discussion and this patch is about this very case and one other case
as outlined below; any other intermediate states that one could imagine are
not affected by this patch.]
Now assume further, that we have not staged and commited anything, but we
have already changed the working file to the new state. Then we will see
this hunk in the "Unstaged Changes":
@@ -1,4 +1,4 @@
context before
-old 1
-old 2
+new 1
+new 2
context after
The obvious way to stage the intermediate state is to apply "Stage This
Line" to "-old 1" and "+new 1". Unfortunately, this resulted in this
intermediate state:
context before
old 2
new 1
context after
which is not what we wanted. In fact, it was impossible to stage the
intermediate state using "Stage Line". The crux was that if a "+" line was
staged, then the "-" lines were converted to context lines and arranged
*before* the "+" line in the forged hunk that we fed to 'git apply'.
With this patch we now treat "+" lines that are staged differently. In
particular, the "-" lines before the "+" block are moved *after* the
staged "+" line. Now it is possible to get the correct intermediate state
by staging "-old 1" and "+new 1". Problem solved.
But there is a catch.
Noticing that we didn't get the right intermediate state by staging
"-old 1" and "+new 1", we could have had the idea to stage the complete
hunk and to *unstage* "-old 2" and "+new 2". But... the result is the same.
The reason is that there is the exact symmetric problem with unstaging the
last "-" and "+" line that are in adjacent blocks of "-" and "+" lines.
This patch does *not* change the way in which "-" lines are *unstaged*.
Why? Because if we did (i.e. move "+" lines before the "-" line after
converting them to context lines), then it would be impossible to stage
this intermediate state:
context before
old 1
new 2
context after
that is, it would be impossible to stage the two independet changes in the
opposite order.
Let's look at this case a bit further: The obvious way to get this
intermediate state would be to apply "Stage This Line" to "-old 2" and
"+new 2". Before this patch, this worked as expected. With this patch, it
does not work as expected, but it can still be achieved by first staging
the entire hunk, then *unstaging* "-old 1" and "+new 1".
In summary, this patch makes a common case possible, at the expense that
a less common case is made more complicated for the user.
Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2008-07-17 15:21:51 +02:00
|
|
|
} else {
|
2009-12-08 01:22:43 +01:00
|
|
|
# a change in the opposite direction of
|
|
|
|
# to_context which is outside the range of
|
|
|
|
# lines to apply.
|
|
|
|
set patch "$patch$pre_context"
|
|
|
|
set pre_context {}
|
git-gui: "Stage Line": Treat independent changes in adjacent lines better
Assume that we want to commit these states:
Old state == HEAD Intermediate state New state
--------------------------------------------------------
context before context before context before
old 1 new 1 new 1
old 2 old 2 new 2
context after context after context after
that is, want to commit two changes in this order:
1. transform "old 1" into "new 1"
2. transform "old 2" into "new 2"
[This discussion and this patch is about this very case and one other case
as outlined below; any other intermediate states that one could imagine are
not affected by this patch.]
Now assume further, that we have not staged and commited anything, but we
have already changed the working file to the new state. Then we will see
this hunk in the "Unstaged Changes":
@@ -1,4 +1,4 @@
context before
-old 1
-old 2
+new 1
+new 2
context after
The obvious way to stage the intermediate state is to apply "Stage This
Line" to "-old 1" and "+new 1". Unfortunately, this resulted in this
intermediate state:
context before
old 2
new 1
context after
which is not what we wanted. In fact, it was impossible to stage the
intermediate state using "Stage Line". The crux was that if a "+" line was
staged, then the "-" lines were converted to context lines and arranged
*before* the "+" line in the forged hunk that we fed to 'git apply'.
With this patch we now treat "+" lines that are staged differently. In
particular, the "-" lines before the "+" block are moved *after* the
staged "+" line. Now it is possible to get the correct intermediate state
by staging "-old 1" and "+new 1". Problem solved.
But there is a catch.
Noticing that we didn't get the right intermediate state by staging
"-old 1" and "+new 1", we could have had the idea to stage the complete
hunk and to *unstage* "-old 2" and "+new 2". But... the result is the same.
The reason is that there is the exact symmetric problem with unstaging the
last "-" and "+" line that are in adjacent blocks of "-" and "+" lines.
This patch does *not* change the way in which "-" lines are *unstaged*.
Why? Because if we did (i.e. move "+" lines before the "-" line after
converting them to context lines), then it would be impossible to stage
this intermediate state:
context before
old 1
new 2
context after
that is, it would be impossible to stage the two independet changes in the
opposite order.
Let's look at this case a bit further: The obvious way to get this
intermediate state would be to apply "Stage This Line" to "-old 2" and
"+new 2". Before this patch, this worked as expected. With this patch, it
does not work as expected, but it can still be achieved by first staging
the entire hunk, then *unstaging* "-old 1" and "+new 1".
In summary, this patch makes a common case possible, at the expense that
a less common case is made more complicated for the user.
Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2008-07-17 15:21:51 +02:00
|
|
|
}
|
2009-12-08 01:22:43 +01:00
|
|
|
set i_l $next_l
|
2008-06-27 09:22:01 +02:00
|
|
|
}
|
2009-12-08 01:22:43 +01:00
|
|
|
set patch "$patch$pre_context"
|
|
|
|
set wholepatch "$wholepatch@@ -$hln,$n +$hln,$m @@\n$patch"
|
|
|
|
set first_l [$ui_diff index "$next_l + 1 lines"]
|
2008-06-27 09:22:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if {[catch {
|
2008-09-17 23:07:32 +02:00
|
|
|
set enc [get_path_encoding $current_diff_path]
|
2008-06-27 09:22:01 +02:00
|
|
|
set p [eval git_write $apply_cmd]
|
2008-09-17 23:07:32 +02:00
|
|
|
fconfigure $p -translation binary -encoding $enc
|
2008-06-27 09:22:01 +02:00
|
|
|
puts -nonewline $p $current_diff_header
|
2009-12-08 01:22:43 +01:00
|
|
|
puts -nonewline $p $wholepatch
|
2008-06-27 09:22:01 +02:00
|
|
|
close $p} err]} {
|
2016-05-08 12:52:57 +02:00
|
|
|
error_popup "$failed_msg\n\n$err"
|
2008-06-27 09:22:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unlock_index
|
|
|
|
}
|