Subject: bug#14516: 24.3.50; vc-diff ignores the encoding of its input

From: Dima Kogan <[email-masked]>
Date: Thu, 30 May 2013 14:50:04 -0700

I'm using a very recent build of the emacs source repo on a Linux box.

I have a file that uses DOS-style line termination. This file is in git
and has some modifications. I use (vc-diff) to get a buffer that shows
those modifications. Currently this buffer is NOT set to use the DOS
encoding, and emacs does a conversion, so this buffer is missing the
extra CR characters the DOS encoding had. Because of this, if I save
this buffer to a file, external tools can no longer be used to apply the
patch.

I'm attaching a tiny git repository that demonstrates the issue:

 dima@shorty:/tmp$ tar xvfz vc_encoding_error_test_repo.tar.gz
 vc_encoding_error_test_repo/
 vc_encoding_error_test_repo/file
 <snip>

 dima@shorty:/tmp$ cd vc_encoding_error_test_repo

 dima@shorty:/tmp/vc_encoding_error_test_repo$ emacs --batch --eval '(progn (find-file "file") (vc-diff) (with-current-buffer "*vc-diff*" (write-file "/tmp/patch")))' -Q
 Finding changes in /tmp/vc_encoding_error_test_repo/file...
 Saving file /tmp/patch...
 Wrote /tmp/patch

 dima@shorty:/tmp/vc_encoding_error_test_repo$ git apply --cached /tmp/patch
 error: patch failed: file:1
 error: file: patch does not apply

Here we see git fail to apply the saved patch. This is due to the
stripped CR characters.

VC already has code in it to detect the encoding of its input. I'm
attaching a small patch that sets this detected encoding to the
*vc-diff* buffer. This fixes the issue.

>From fd04b0d03f61344a4c9a37cafc86495c79a46d19 Mon Sep 17 00:00:00 2001
From: Dima Kogan <[email-masked]>
Date: Thu, 30 May 2013 14:17:08 -0700
Subject: [PATCH] vc buffers now use the encoding of their input

---
 lisp/vc/vc-dispatcher.el |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lisp/vc/vc-dispatcher.el b/lisp/vc/vc-dispatcher.el
index 309cf50..1bada72 100644
--- a/lisp/vc/vc-dispatcher.el
+++ b/lisp/vc/vc-dispatcher.el
@@ -178,7 +178,9 @@ Another is that undo information is not kept."
     (setq default-directory olddir)
     (let ((buffer-undo-list t)
           (inhibit-read-only t))
-      (erase-buffer))))
+      (erase-buffer))
+    (when coding-system-for-read
+      (setq buffer-file-coding-system coding-system-for-read))))
 
 (defvar vc-sentinel-movepoint)          ;Dynamically scoped.
 
-- 
1.7.10.4