878 lines
23 KiB
Tcl
878 lines
23 KiB
Tcl
# git-gui diff viewer
|
|
# Copyright (C) 2006, 2007 Shawn Pearce
|
|
|
|
proc apply_tab_size {{firsttab {}}} {
|
|
global repo_config ui_diff
|
|
|
|
set w [font measure font_diff "0"]
|
|
if {$firsttab != 0} {
|
|
$ui_diff configure -tabs [list [expr {$firsttab * $w}] [expr {($firsttab + $repo_config(gui.tabsize)) * $w}]]
|
|
} else {
|
|
$ui_diff configure -tabs [expr {$repo_config(gui.tabsize) * $w}]
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
proc reshow_diff {{after {}}} {
|
|
global file_states file_lists
|
|
global current_diff_path current_diff_side
|
|
global ui_diff
|
|
|
|
set p $current_diff_path
|
|
if {$p eq {}} {
|
|
# No diff is being shown.
|
|
} elseif {$current_diff_side eq {}} {
|
|
clear_diff
|
|
} 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]}]} {
|
|
next_diff $after
|
|
} else {
|
|
clear_diff
|
|
}
|
|
} else {
|
|
set save_pos [lindex [$ui_diff yview] 0]
|
|
show_diff $p $current_diff_side {} $save_pos $after
|
|
}
|
|
}
|
|
|
|
proc force_diff_encoding {enc} {
|
|
global current_diff_path
|
|
|
|
if {$current_diff_path ne {}} {
|
|
force_path_encoding $current_diff_path $enc
|
|
reshow_diff
|
|
}
|
|
}
|
|
|
|
proc handle_empty_diff {} {
|
|
global current_diff_path file_states
|
|
global ui_diff
|
|
|
|
set path $current_diff_path
|
|
set s $file_states($path)
|
|
if {[lindex $s 0] ne {_M} || [has_textconv $path]} return
|
|
|
|
$ui_diff conf -state normal
|
|
$ui_diff insert end [mc "* No differences detected; stage the file to de-list it from Unstaged Changes.\n"] d_info
|
|
$ui_diff insert end [mc "* Click to find other files that may have the same state.\n"] d_rescan
|
|
$ui_diff conf -state disabled
|
|
}
|
|
|
|
proc show_diff {path w {lno {}} {scroll_pos {}} {callback {}}} {
|
|
global file_states file_lists
|
|
global is_3way_diff is_conflict_diff diff_active repo_config
|
|
global ui_diff ui_index ui_workdir
|
|
global current_diff_path current_diff_side current_diff_header
|
|
global current_diff_queue
|
|
|
|
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
|
|
$w see $lno.0
|
|
}
|
|
|
|
set s $file_states($path)
|
|
set m [lindex $s 0]
|
|
set is_conflict_diff 0
|
|
set current_diff_path $path
|
|
set current_diff_side $w
|
|
set current_diff_queue {}
|
|
ui_status [mc "Loading diff of %s..." [escape_path $path]]
|
|
|
|
set cont_info [list $scroll_pos $callback]
|
|
|
|
apply_tab_size 0
|
|
|
|
if {[string first {U} $m] >= 0} {
|
|
merge_load_stages $path [list show_unmerged_diff $cont_info]
|
|
} elseif {$m eq {_O}} {
|
|
show_other_diff $path $w $m $cont_info
|
|
} else {
|
|
start_show_diff $cont_info
|
|
}
|
|
|
|
global current_diff_path selected_paths
|
|
set selected_paths($current_diff_path) 1
|
|
}
|
|
|
|
proc show_unmerged_diff {cont_info} {
|
|
global current_diff_path current_diff_side
|
|
global merge_stages ui_diff is_conflict_diff
|
|
global current_diff_queue
|
|
|
|
if {$merge_stages(2) eq {}} {
|
|
set is_conflict_diff 1
|
|
lappend current_diff_queue \
|
|
[list [mc "LOCAL: deleted\nREMOTE:\n"] d= \
|
|
[list ":1:$current_diff_path" ":3:$current_diff_path"]]
|
|
} elseif {$merge_stages(3) eq {}} {
|
|
set is_conflict_diff 1
|
|
lappend current_diff_queue \
|
|
[list [mc "REMOTE: deleted\nLOCAL:\n"] d= \
|
|
[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}} {
|
|
set is_conflict_diff 1
|
|
lappend current_diff_queue \
|
|
[list [mc "LOCAL:\n"] d= \
|
|
[list ":1:$current_diff_path" ":2:$current_diff_path"]]
|
|
lappend current_diff_queue \
|
|
[list [mc "REMOTE:\n"] d= \
|
|
[list ":1:$current_diff_path" ":3:$current_diff_path"]]
|
|
} else {
|
|
start_show_diff $cont_info
|
|
return
|
|
}
|
|
|
|
advance_diff_queue $cont_info
|
|
}
|
|
|
|
proc advance_diff_queue {cont_info} {
|
|
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
|
|
|
|
start_show_diff $cont_info [lindex $item 2]
|
|
}
|
|
|
|
proc show_other_diff {path w m cont_info} {
|
|
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
|
|
|
|
# - Git won't give us the diff, there's nothing to compare to!
|
|
#
|
|
if {$m eq {_O}} {
|
|
set max_sz 100000
|
|
set type unknown
|
|
if {[catch {
|
|
set type [file type $path]
|
|
switch -- $type {
|
|
directory {
|
|
set type submodule
|
|
set content {}
|
|
set sz 0
|
|
}
|
|
link {
|
|
set content [file readlink $path]
|
|
set sz [string length $content]
|
|
}
|
|
file {
|
|
set fd [safe_open_file $path r]
|
|
fconfigure $fd \
|
|
-encoding [get_path_encoding $path]
|
|
set content [read $fd $max_sz]
|
|
close $fd
|
|
set sz [file size $path]
|
|
}
|
|
default {
|
|
error "'$type' not supported"
|
|
}
|
|
}
|
|
} err ]} {
|
|
set diff_active 0
|
|
unlock_index
|
|
ui_status [mc "Unable to display %s" [escape_path $path]]
|
|
error_popup [strcat [mc "Error loading file:"] "\n\n$err"]
|
|
return
|
|
}
|
|
$ui_diff conf -state normal
|
|
if {$type eq {submodule}} {
|
|
$ui_diff insert end \
|
|
"* [mc "Git Repository (subproject)"]\n" \
|
|
d_info
|
|
} elseif {![catch {set type [safe_exec [list file $path]]}]} {
|
|
set n [string length $path]
|
|
if {[string equal -length $n $path $type]} {
|
|
set type [string range $type $n end]
|
|
regsub {^:?\s*} $type {} type
|
|
}
|
|
$ui_diff insert end "* $type\n" d_info
|
|
}
|
|
if {[string first "\0" $content] != -1} {
|
|
$ui_diff insert end \
|
|
[mc "* Binary fi |