@matheusbsbs / matheus-bagliano / commits / 72e6b00202
Fork 0

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>

Alexander Gavrilov committed Sep 18, 2008 at 01:07 UTC 72e6b002021e45255f568fc0c885d82de75ae935
5 files changed +46 -6
git-gui.sh
+1
@@ -678,6 +678,7 @@ set default_config(merge.verbosity) 2
678 set default_config(user.name) {}
679 set default_config(user.email) {}
680
681 +set default_config(gui.encoding) [encoding system]
682 set default_config(gui.matchtrackingbranch) false
683 set default_config(gui.pruneduringfetch) false
684 set default_config(gui.trustmtime) false
lib/blame.tcl
+1 -1
@@ -402,7 +402,7 @@ method _load {jump} {
402 fconfigure $fd \
403 -blocking 0 \
404 -translation lf \
405 - -encoding [tcl_encoding [gitattr $path encoding UTF-8]]
405 + -encoding [get_path_encoding $path]
406 fileevent $fd readable [cb _read_file $fd $jump]
407 set current_fd $fd
408 }
lib/diff.tcl
+6 -5
@@ -164,11 +164,10 @@ proc show_other_diff {path w m scroll_pos} {
164 set sz [string length $content]
165 }
166 file {
167 - set enc [gitattr $path encoding UTF-8]
167 set fd [open $path r]
168 fconfigure $fd \
169 -eofchar {} \
171 - -encoding [tcl_encoding $enc]
170 + -encoding [get_path_encoding $path]
171 set content [read $fd $max_sz]
172 close $fd
173 set sz [file size $path]
@@ -282,7 +281,7 @@ proc start_show_diff {scroll_pos {add_opts {}}} {
281 set ::current_diff_inheader 1
282 fconfigure $fd \
283 -blocking 0 \
285 - -encoding [tcl_encoding [gitattr $path encoding UTF-8]] \
284 + -encoding [get_path_encoding $path] \
285 -translation lf
286 fileevent $fd readable [list read_diff $fd $scroll_pos]
287 }
@@ -435,8 +434,9 @@ proc apply_hunk {x y} {
434 }
435
436 if {[catch {
437 + set enc [get_path_encoding $current_diff_path]
438 set p [eval git_write $apply_cmd]
439 - fconfigure $p -translation binary -encoding binary
439 + fconfigure $p -translation binary -encoding $enc
440 puts -nonewline $p $current_diff_header
441 puts -nonewline $p [$ui_diff get $s_lno $e_lno]
442 close $p} err]} {
@@ -604,8 +604,9 @@ proc apply_line {x y} {
604 set patch "@@ -$hln,$n +$hln,[eval expr $n $sign 1] @@\n$patch"
605
606 if {[catch {
607 + set enc [get_path_encoding $current_diff_path]
608 set p [eval git_write $apply_cmd]
608 - fconfigure $p -translation binary -encoding binary
609 + fconfigure $p -translation binary -encoding $enc
610 puts -nonewline $p $current_diff_header
611 puts -nonewline $p $patch
612 close $p} err]} {
lib/encoding.tcl
+14
@@ -274,3 +274,17 @@ proc tcl_encoding {enc} {
274 }
275 return {}
276 }
277 +
278 +proc get_path_encoding {path} {
279 + set tcl_enc [tcl_encoding [get_config gui.encoding]]
280 + if {$tcl_enc eq {}} {
281 + set tcl_enc [encoding system]
282 + }
283 + if {$path ne {}} {
284 + set enc2 [tcl_encoding [gitattr $path encoding $tcl_enc]]
285 + if {$enc2 ne {}} {
286 + set tcl_enc $enc2
287 + }
288 + }
289 + return $tcl_enc
290 +}
lib/option.tcl
+24
@@ -1,6 +1,28 @@
1 # git-gui options editor
2 # Copyright (C) 2006, 2007 Shawn Pearce
3
4 +proc config_check_encodings {} {
5 + global repo_config_new global_config_new
6 +
7 + set enc $global_config_new(gui.encoding)
8 + if {$enc eq {}} {
9 + set global_config_new(gui.encoding) [encoding system]
10 + } elseif {[tcl_encoding $enc] eq {}} {
11 + error_popup [mc "Invalid global encoding '%s'" $enc]
12 + return 0
13 + }
14 +
15 + set enc $repo_config_new(gui.encoding)
16 + if {$enc eq {}} {
17 + set repo_config_new(gui.encoding) [encoding system]
18 + } elseif {[tcl_encoding $enc] eq {}} {
19 + error_popup [mc "Invalid repo encoding '%s'" $enc]
20 + return 0
21 + }
22 +
23 + return 1
24 +}
25 +
26 proc save_config {} {
27 global default_config font_descs
28 global repo_config global_config
@@ -130,6 +152,7 @@ proc do_options {} {
152 {i-1..99 gui.diffcontext {mc "Number of Diff Context Lines"}}
153 {i-0..99 gui.commitmsgwidth {mc "Commit Message Text Width"}}
154 {t gui.newbranchtemplate {mc "New Branch Name Template"}}
155 + {t gui.encoding {mc "Default File Contents Encoding"}}
156 } {
157 set type [lindex $option 0]
158 set name [lindex $option 1]
@@ -275,6 +298,7 @@ proc do_restore_defaults {} {
298 }
299
300 proc do_save_config {w} {
301 + if {![config_check_encodings]} return
302 if {[catch {save_config} err]} {
303 error_popup [strcat [mc "Failed to completely save options:"] "\n\n$err"]
304 }