git-gui: Use gitattribute "encoding" for file content display
Most folks using git-gui on internationalized files have complained that it doesn't recognize UTF-8 correctly. In the past we have just ignored the problem and showed the file contents as binary/US-ASCII, which is wrong no matter how you look at it. This really should be a per-file attribute, managed by .gitattributes, so we now pull the "encoding" attribute data for the given path from the .gitattributes (if available) and use that, falling back to UTF-8 if the attributes are unavailable, git-check-attr is broken, or an encoding for this path not specified. We apply the encoding anytime we show file content, which currently is limited to only the diff viewer and the blame viewer. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Shawn O. Pearce committed
Jan 23, 2008 at 00:37 UTC
1ffca60f0b0395e1e593e64d66e7ed3c47d8517e
4 files changed
+26
-4
.gitattributes
new
+3
@@ -0,0 +1,3 @@
1
+* encoding=US-ASCII
2
+git-gui.sh encoding=UTF-8
3
+/po/*.po encoding=UTF-8
git-gui.sh
+13
@@ -521,6 +521,19 @@ proc kill_file_process {fd} {
521
}
522
}
523
524
+proc gitattr {path attr default} {
525
+ if {[catch {set r [git check-attr $attr -- $path]}]} {
526
+ set r unspecified
527
+ } else {
528
+ set r [join [lrange [split $r :] 2 end] :]
529
+ regsub {^ } $r {} r
530
+ }
531
+ if {$r eq {unspecified}} {
532
+ return $default
533
+ }
534
+ return $r
535
+}
536
+
537
proc sq {value} {
538
regsub -all ' $value "'\\''" value
539
return "'$value'"
lib/blame.tcl
+4
-1
@@ -399,7 +399,10 @@ method _load {jump} {
399
} else {
400
set fd [git_read cat-file blob "$commit:$path"]
401
}
402
- fconfigure $fd -blocking 0 -translation lf -encoding binary
402
+ fconfigure $fd \
403
+ -blocking 0 \
404
+ -translation lf \
405
+ -encoding [tcl_encoding [gitattr $path encoding UTF-8]]
406
fileevent $fd readable [cb _read_file $fd $jump]
407
set current_fd $fd
408
}
lib/diff.tcl
+6
-3
@@ -164,8 +164,11 @@ 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]
168
set fd [open $path r]
168
- fconfigure $fd -eofchar {}
169
+ fconfigure $fd \
170
+ -eofchar {} \
171
+ -encoding [tcl_encoding $enc]
172
set content [read $fd $max_sz]
173
close $fd
174
set sz [file size $path]
@@ -279,8 +282,8 @@ proc start_show_diff {scroll_pos {add_opts {}}} {
282
set ::current_diff_inheader 1
283
fconfigure $fd \
284
-blocking 0 \
282
- -encoding binary \
283
- -translation binary
285
+ -encoding [tcl_encoding [gitattr $path encoding UTF-8]] \
286
+ -translation lf
287
fileevent $fd readable [list read_diff $fd $scroll_pos]
288
}
289