git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexander Gavrilov <angavrilov@gmail.com>
To: git@vger.kernel.org
Cc: "Shawn O. Pearce" <spearce@spearce.org>,
	Paul Mackerras <paulus@samba.org>
Subject: [PATCH (GIT-GUI,GITK) 1/8] git-gui: Cleanup handling of the default encoding.
Date: Thu, 18 Sep 2008 01:07:32 +0400	[thread overview]
Message-ID: <1221685659-476-2-git-send-email-angavrilov@gmail.com> (raw)
In-Reply-To: <1221685659-476-1-git-send-email-angavrilov@gmail.com>

- 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>
---
 git-gui.sh       |    1 +
 lib/blame.tcl    |    2 +-
 lib/diff.tcl     |   11 ++++++-----
 lib/encoding.tcl |   14 ++++++++++++++
 lib/option.tcl   |   24 ++++++++++++++++++++++++
 5 files changed, 46 insertions(+), 6 deletions(-)

diff --git a/git-gui.sh b/git-gui.sh
index 91457a2..444990b 100755
--- a/git-gui.sh
+++ b/git-gui.sh
@@ -678,6 +678,7 @@ set default_config(merge.verbosity) 2
 set default_config(user.name) {}
 set default_config(user.email) {}
 
+set default_config(gui.encoding) [encoding system]
 set default_config(gui.matchtrackingbranch) false
 set default_config(gui.pruneduringfetch) false
 set default_config(gui.trustmtime) false
diff --git a/lib/blame.tcl b/lib/blame.tcl
index 7535adb..84d55b5 100644
--- a/lib/blame.tcl
+++ b/lib/blame.tcl
@@ -402,7 +402,7 @@ method _load {jump} {
 	fconfigure $fd \
 		-blocking 0 \
 		-translation lf \
-		-encoding [tcl_encoding [gitattr $path encoding UTF-8]]
+		-encoding [get_path_encoding $path]
 	fileevent $fd readable [cb _read_file $fd $jump]
 	set current_fd $fd
 }
diff --git a/lib/diff.tcl b/lib/diff.tcl
index b0ecfbc..8fefc5d 100644
--- a/lib/diff.tcl
+++ b/lib/diff.tcl
@@ -164,11 +164,10 @@ proc show_other_diff {path w m scroll_pos} {
 					set sz [string length $content]
 				}
 				file {
-					set enc [gitattr $path encoding UTF-8]
 					set fd [open $path r]
 					fconfigure $fd \
 						-eofchar {} \
-						-encoding [tcl_encoding $enc]
+						-encoding [get_path_encoding $path]
 					set content [read $fd $max_sz]
 					close $fd
 					set sz [file size $path]
@@ -282,7 +281,7 @@ proc start_show_diff {scroll_pos {add_opts {}}} {
 	set ::current_diff_inheader 1
 	fconfigure $fd \
 		-blocking 0 \
-		-encoding [tcl_encoding [gitattr $path encoding UTF-8]] \
+		-encoding [get_path_encoding $path] \
 		-translation lf
 	fileevent $fd readable [list read_diff $fd $scroll_pos]
 }
@@ -435,8 +434,9 @@ proc apply_hunk {x y} {
 	}
 
 	if {[catch {
+		set enc [get_path_encoding $current_diff_path]
 		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 [$ui_diff get $s_lno $e_lno]
 		close $p} err]} {
@@ -604,8 +604,9 @@ proc apply_line {x y} {
 	set patch "@@ -$hln,$n +$hln,[eval expr $n $sign 1] @@\n$patch"
 
 	if {[catch {
+		set enc [get_path_encoding $current_diff_path]
 		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 $patch
 		close $p} err]} {
diff --git a/lib/encoding.tcl b/lib/encoding.tcl
index 7f06b0d..e186b0c 100644
--- a/lib/encoding.tcl
+++ b/lib/encoding.tcl
@@ -274,3 +274,17 @@ proc tcl_encoding {enc} {
     }
     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
+}
diff --git a/lib/option.tcl b/lib/option.tcl
index 9b865f6..40af44e 100644
--- a/lib/option.tcl
+++ b/lib/option.tcl
@@ -1,6 +1,28 @@
 # git-gui options editor
 # 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 {} {
 	global default_config font_descs
 	global repo_config global_config
@@ -130,6 +152,7 @@ proc do_options {} {
 		{i-1..99 gui.diffcontext {mc "Number of Diff Context Lines"}}
 		{i-0..99 gui.commitmsgwidth {mc "Commit Message Text Width"}}
 		{t gui.newbranchtemplate {mc "New Branch Name Template"}}
+		{t gui.encoding {mc "Default File Contents Encoding"}}
 		} {
 		set type [lindex $option 0]
 		set name [lindex $option 1]
@@ -275,6 +298,7 @@ proc do_restore_defaults {} {
 }
 
 proc do_save_config {w} {
+	if {![config_check_encodings]} return
 	if {[catch {save_config} err]} {
 		error_popup [strcat [mc "Failed to completely save options:"] "\n\n$err"]
 	}
-- 
1.6.0.20.g6148bc

  reply	other threads:[~2008-09-17 21:10 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-09-17 21:07 [PATCH (GIT-GUI,GITK) 0/8] Encoding support in GUI Alexander Gavrilov
2008-09-17 21:07 ` Alexander Gavrilov [this message]
2008-09-17 21:07   ` [PATCH (GIT-GUI,GITK) 2/8] git-gui: Add a menu of available encodings Alexander Gavrilov
2008-09-17 21:07     ` [PATCH (GIT-GUI,GITK) 3/8] git-gui: Allow forcing display encoding for diffs using a submenu Alexander Gavrilov
2008-09-17 21:07       ` [PATCH (GIT-GUI,GITK) 4/8] git-gui: Optimize encoding name resolution using a lookup table Alexander Gavrilov
2008-09-17 21:07         ` [PATCH (GIT-GUI,GITK) 5/8] git-gui: Support the encoding menu in gui blame Alexander Gavrilov
2008-09-17 21:07           ` [PATCH (GIT-GUI,GITK) 6/8] gitk: Port new encoding logic from git-gui Alexander Gavrilov
2008-09-17 21:07             ` [PATCH (GIT-GUI,GITK) 7/8] gitk: Implement file contents encoding support Alexander Gavrilov
2008-09-17 21:07               ` [PATCH (GIT-GUI,GITK) 8/8] gitk: Support filenames in the locale encoding Alexander Gavrilov
2008-09-19 12:10             ` [PATCH (GIT-GUI,GITK) 6/8] gitk: Port new encoding logic from git-gui Johannes Sixt
2008-09-19 12:38               ` Alexander Gavrilov
2008-09-19 13:04                 ` Johannes Sixt
2008-09-21 18:52                   ` Alexander Gavrilov
2008-09-22  7:25                     ` Johannes Sixt
2008-09-22  7:46                       ` Johannes Sixt
2008-09-22  8:01                       ` Alexander Gavrilov
2008-09-22  8:20                         ` Johannes Sixt
2008-09-22  9:02                           ` Alexander Gavrilov
2008-09-22  9:18                             ` Johannes Sixt
2008-09-22 10:18                               ` Alexander Gavrilov
2008-09-22  9:01                     ` Dmitry Potapov
2008-09-18 15:02   ` [PATCH (GIT-GUI,GITK) 1/8] git-gui: Cleanup handling of the default encoding Dmitry Potapov
2008-09-18 15:14     ` Alexander Gavrilov
2008-09-18 16:29     ` Johannes Sixt
2008-09-18 16:50       ` Dmitry Potapov
2008-09-18 17:00         ` Alexander Gavrilov
2008-09-18 17:19           ` Dmitry Potapov
2008-09-17 21:45 ` [PATCH (GIT-GUI,GITK) 0/8] Encoding support in GUI Paul Mackerras
2008-09-18 11:12   ` Alexander Gavrilov
2008-09-21 22:55 ` Paul Mackerras
2008-09-22 10:12   ` Alexander Gavrilov
2008-10-05  2:30     ` [PATCH 1/2] check-attr: add an internal check_attr() function Dmitry Potapov
2008-10-05  2:30       ` [PATCH 2/2] check-attr: Add --stdin-paths option Dmitry Potapov
2008-10-06  7:09         ` Johannes Sixt
2008-10-07  0:14           ` [PATCH 1/2 v2] check-attr: add an internal check_attr() function Dmitry Potapov
2008-10-07  0:16           ` [PATCH 2/2 v2] check-attr: Add --stdin-paths option Dmitry Potapov
2008-10-08 15:24             ` Shawn O. Pearce
2008-10-12 14:19               ` Dmitry Potapov
2008-10-12 15:04                 ` Jakub Narebski
2008-10-12 16:35                   ` Dmitry Potapov
2008-10-10 22:39             ` Paul Mackerras
2008-10-12 14:30               ` Dmitry Potapov
2008-10-01 11:35   ` [PATCH (GIT-GUI,GITK) 0/8] Encoding support in GUI Johannes Sixt
2008-10-10 10:46     ` Paul Mackerras

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1221685659-476-2-git-send-email-angavrilov@gmail.com \
    --to=angavrilov@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=paulus@samba.org \
    --cc=spearce@spearce.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).