Skip to content

Commit 5988527

Browse files
committed
git-gui: Handle commit encoding better.
Git prefers that all log messages are encoding in UTF-8. So now when git-gui generates the commit message it converts the commit message text from the internal Tcl Unicode representation into a UTF-8 file. The file is then fed as stdin to git-commit-tree. I had to start using a file here rather than feeding the message in with << as << uses the system encoding, which we may not want. When we reload a commit message via git-cat-file we are getting the raw byte stream, with no encoding performed by Git itself. So unless the new 'encoding' header appears in the message we should probably assume it is utf-8 encoded; but if the header is present we need to use whatever it claims. Signed-off-by: Shawn O. Pearce <[email protected]>
1 parent 51a989b commit 5988527

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

git-gui.sh

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,7 @@ proc read_diff {fd} {
805805

806806
proc load_last_commit {} {
807807
global HEAD PARENT MERGE_HEAD commit_type ui_comm
808+
global repo_config
808809

809810
if {[llength $PARENT] == 0} {
810811
error_popup {There is nothing to amend.
@@ -831,11 +832,18 @@ current merge activity.
831832
set parents [list]
832833
if {[catch {
833834
set fd [open "| git cat-file commit $curHEAD" r]
835+
fconfigure $fd -encoding binary -translation lf
836+
if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
837+
set enc utf-8
838+
}
834839
while {[gets $fd line] > 0} {
835840
if {[string match {parent *} $line]} {
836841
lappend parents [string range $line 7 end]
842+
} elseif {[string match {encoding *} $line]} {
843+
set enc [string tolower [string range $line 9 end]]
837844
}
838845
}
846+
fconfigure $fd -encoding $enc
839847
set msg [string trim [read $fd]]
840848
close $fd
841849
} err]} {
@@ -1027,6 +1035,7 @@ proc commit_committree {fd_wt curHEAD msg} {
10271035
global single_commit all_heads current_branch
10281036
global ui_status_value ui_comm selected_commit_type
10291037
global file_states selected_paths rescan_active
1038+
global repo_config
10301039

10311040
gets $fd_wt tree_id
10321041
if {$tree_id eq {} || [catch {close $fd_wt} err]} {
@@ -1036,6 +1045,17 @@ proc commit_committree {fd_wt curHEAD msg} {
10361045
return
10371046
}
10381047

1048+
# -- Build the message.
1049+
#
1050+
set msg_p [gitdir COMMIT_EDITMSG]
1051+
set msg_wt [open $msg_p w]
1052+
if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
1053+
set enc utf-8
1054+
}
1055+
fconfigure $msg_wt -encoding $enc -translation binary
1056+
puts -nonewline $msg_wt $msg
1057+
close $msg_wt
1058+
10391059
# -- Create the commit.
10401060
#
10411061
set cmd [list git commit-tree $tree_id]
@@ -1048,7 +1068,7 @@ proc commit_committree {fd_wt curHEAD msg} {
10481068
# git commit-tree writes to stderr during initial commit.
10491069
lappend cmd 2>/dev/null
10501070
}
1051-
lappend cmd << $msg
1071+
lappend cmd <$msg_p
10521072
if {[catch {set cmt_id [eval exec $cmd]} err]} {
10531073
error_popup "commit-tree failed:\n\n$err"
10541074
set ui_status_value {Commit failed.}
@@ -1086,6 +1106,7 @@ proc commit_committree {fd_wt curHEAD msg} {
10861106

10871107
# -- Cleanup after ourselves.
10881108
#
1109+
catch {file delete $msg_p}
10891110
catch {file delete [gitdir MERGE_HEAD]}
10901111
catch {file delete [gitdir MERGE_MSG]}
10911112
catch {file delete [gitdir SQUASH_MSG]}

0 commit comments

Comments
 (0)