git-gui: Cleanup handling of the default encoding.
- Make diffs and blame default to the system (locale) encoding instead of hard-coding UTF-8. - Add a gui.encoding option to allow overriding it. - gitattributes still have the final word. The rationale for this is Windows support: 1) Windows people are accustomed to using legacy encodings for text files. For many of them defaulting to utf-8 will be counter-intuitive. 2) Windows doesn't support utf-8 locales, and switching the system encoding is a real pain. Thus the option. This patch also adds proper encoding conversion to Apply Hunk/Line. Signed-off-by: Alexander Gavrilov <angavrilov@gmail.com> Tested-by: Johannes Sixt <johannes.sixt@telecom.at> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
This commit is contained in:
parent
696235c6c1
commit
72e6b00202
@ -678,6 +678,7 @@ set default_config(merge.verbosity) 2
|
|||||||
set default_config(user.name) {}
|
set default_config(user.name) {}
|
||||||
set default_config(user.email) {}
|
set default_config(user.email) {}
|
||||||
|
|
||||||
|
set default_config(gui.encoding) [encoding system]
|
||||||
set default_config(gui.matchtrackingbranch) false
|
set default_config(gui.matchtrackingbranch) false
|
||||||
set default_config(gui.pruneduringfetch) false
|
set default_config(gui.pruneduringfetch) false
|
||||||
set default_config(gui.trustmtime) false
|
set default_config(gui.trustmtime) false
|
||||||
|
@ -402,7 +402,7 @@ method _load {jump} {
|
|||||||
fconfigure $fd \
|
fconfigure $fd \
|
||||||
-blocking 0 \
|
-blocking 0 \
|
||||||
-translation lf \
|
-translation lf \
|
||||||
-encoding [tcl_encoding [gitattr $path encoding UTF-8]]
|
-encoding [get_path_encoding $path]
|
||||||
fileevent $fd readable [cb _read_file $fd $jump]
|
fileevent $fd readable [cb _read_file $fd $jump]
|
||||||
set current_fd $fd
|
set current_fd $fd
|
||||||
}
|
}
|
||||||
|
11
lib/diff.tcl
11
lib/diff.tcl
@ -164,11 +164,10 @@ proc show_other_diff {path w m scroll_pos} {
|
|||||||
set sz [string length $content]
|
set sz [string length $content]
|
||||||
}
|
}
|
||||||
file {
|
file {
|
||||||
set enc [gitattr $path encoding UTF-8]
|
|
||||||
set fd [open $path r]
|
set fd [open $path r]
|
||||||
fconfigure $fd \
|
fconfigure $fd \
|
||||||
-eofchar {} \
|
-eofchar {} \
|
||||||
-encoding [tcl_encoding $enc]
|
-encoding [get_path_encoding $path]
|
||||||
set content [read $fd $max_sz]
|
set content [read $fd $max_sz]
|
||||||
close $fd
|
close $fd
|
||||||
set sz [file size $path]
|
set sz [file size $path]
|
||||||
@ -282,7 +281,7 @@ proc start_show_diff {scroll_pos {add_opts {}}} {
|
|||||||
set ::current_diff_inheader 1
|
set ::current_diff_inheader 1
|
||||||
fconfigure $fd \
|
fconfigure $fd \
|
||||||
-blocking 0 \
|
-blocking 0 \
|
||||||
-encoding [tcl_encoding [gitattr $path encoding UTF-8]] \
|
-encoding [get_path_encoding $path] \
|
||||||
-translation lf
|
-translation lf
|
||||||
fileevent $fd readable [list read_diff $fd $scroll_pos]
|
fileevent $fd readable [list read_diff $fd $scroll_pos]
|
||||||
}
|
}
|
||||||
@ -435,8 +434,9 @@ proc apply_hunk {x y} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if {[catch {
|
if {[catch {
|
||||||
|
set enc [get_path_encoding $current_diff_path]
|
||||||
set p [eval git_write $apply_cmd]
|
set p [eval git_write $apply_cmd]
|
||||||
fconfigure $p -translation binary -encoding binary
|
fconfigure $p -translation binary -encoding $enc
|
||||||
puts -nonewline $p $current_diff_header
|
puts -nonewline $p $current_diff_header
|
||||||
puts -nonewline $p [$ui_diff get $s_lno $e_lno]
|
puts -nonewline $p [$ui_diff get $s_lno $e_lno]
|
||||||
close $p} err]} {
|
close $p} err]} {
|
||||||
@ -604,8 +604,9 @@ proc apply_line {x y} {
|
|||||||
set patch "@@ -$hln,$n +$hln,[eval expr $n $sign 1] @@\n$patch"
|
set patch "@@ -$hln,$n +$hln,[eval expr $n $sign 1] @@\n$patch"
|
||||||
|
|
||||||
if {[catch {
|
if {[catch {
|
||||||
|
set enc [get_path_encoding $current_diff_path]
|
||||||
set p [eval git_write $apply_cmd]
|
set p [eval git_write $apply_cmd]
|
||||||
fconfigure $p -translation binary -encoding binary
|
fconfigure $p -translation binary -encoding $enc
|
||||||
puts -nonewline $p $current_diff_header
|
puts -nonewline $p $current_diff_header
|
||||||
puts -nonewline $p $patch
|
puts -nonewline $p $patch
|
||||||
close $p} err]} {
|
close $p} err]} {
|
||||||
|
@ -274,3 +274,17 @@ proc tcl_encoding {enc} {
|
|||||||
}
|
}
|
||||||
return {}
|
return {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
proc get_path_encoding {path} {
|
||||||
|
set tcl_enc [tcl_encoding [get_config gui.encoding]]
|
||||||
|
if {$tcl_enc eq {}} {
|
||||||
|
set tcl_enc [encoding system]
|
||||||
|
}
|
||||||
|
if {$path ne {}} {
|
||||||
|
set enc2 [tcl_encoding [gitattr $path encoding $tcl_enc]]
|
||||||
|
if {$enc2 ne {}} {
|
||||||
|
set tcl_enc $enc2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $tcl_enc
|
||||||
|
}
|
||||||
|
@ -1,6 +1,28 @@
|
|||||||
# git-gui options editor
|
# git-gui options editor
|
||||||
# Copyright (C) 2006, 2007 Shawn Pearce
|
# Copyright (C) 2006, 2007 Shawn Pearce
|
||||||
|
|
||||||
|
proc config_check_encodings {} {
|
||||||
|
global repo_config_new global_config_new
|
||||||
|
|
||||||
|
set enc $global_config_new(gui.encoding)
|
||||||
|
if {$enc eq {}} {
|
||||||
|
set global_config_new(gui.encoding) [encoding system]
|
||||||
|
} elseif {[tcl_encoding $enc] eq {}} {
|
||||||
|
error_popup [mc "Invalid global encoding '%s'" $enc]
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
set enc $repo_config_new(gui.encoding)
|
||||||
|
if {$enc eq {}} {
|
||||||
|
set repo_config_new(gui.encoding) [encoding system]
|
||||||
|
} elseif {[tcl_encoding $enc] eq {}} {
|
||||||
|
error_popup [mc "Invalid repo encoding '%s'" $enc]
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
proc save_config {} {
|
proc save_config {} {
|
||||||
global default_config font_descs
|
global default_config font_descs
|
||||||
global repo_config global_config
|
global repo_config global_config
|
||||||
@ -130,6 +152,7 @@ proc do_options {} {
|
|||||||
{i-1..99 gui.diffcontext {mc "Number of Diff Context Lines"}}
|
{i-1..99 gui.diffcontext {mc "Number of Diff Context Lines"}}
|
||||||
{i-0..99 gui.commitmsgwidth {mc "Commit Message Text Width"}}
|
{i-0..99 gui.commitmsgwidth {mc "Commit Message Text Width"}}
|
||||||
{t gui.newbranchtemplate {mc "New Branch Name Template"}}
|
{t gui.newbranchtemplate {mc "New Branch Name Template"}}
|
||||||
|
{t gui.encoding {mc "Default File Contents Encoding"}}
|
||||||
} {
|
} {
|
||||||
set type [lindex $option 0]
|
set type [lindex $option 0]
|
||||||
set name [lindex $option 1]
|
set name [lindex $option 1]
|
||||||
@ -275,6 +298,7 @@ proc do_restore_defaults {} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
proc do_save_config {w} {
|
proc do_save_config {w} {
|
||||||
|
if {![config_check_encodings]} return
|
||||||
if {[catch {save_config} err]} {
|
if {[catch {save_config} err]} {
|
||||||
error_popup [strcat [mc "Failed to completely save options:"] "\n\n$err"]
|
error_popup [strcat [mc "Failed to completely save options:"] "\n\n$err"]
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user