Subject: bug#24725: 25.1.50; vc-region-history may exceed max line number of file in repository

From: Tino Calancha <[email-masked]>
Date: Thu, 20 Oct 2016 13:22:10 +0900

Dmitry Gutov <[email-masked]> writes:

> Hi!
>
> On 18.10.2016 17:27, Tino Calancha wrote:
>>
>> emacs -Q lisp/vc/vc.el
>> C-x h
>> M-x vc-region-history RET
>> fatal: file vc.el has only 2921 lines
>> ;; vc.el has 2922 lines but Git ignores the last empty line.
>
> With your patch, what will happen if the file does not end with a
> newline (which can be some people's or tools' preference)?
In that case my patch is wrong: it excludes the last line from the region.
> Currently, in that case the above scenario works fine. Maybe we need a
> different check, rather than substracting.
>
> Or try (line-number-at-pos (1- (point-max))) instead of (1- 
> (line-number-at-pos (point-max))).
Thank you.  Your suggestion handle both cases correctly.
Following is the new patch:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
>From c9ce3a54f572549ea2027108694eb556b5cbe8f3 Mon Sep 17 00:00:00 2001
From: Tino Calancha <[email-masked]>
Date: Thu, 20 Oct 2016 12:22:51 +0900
Subject: [PATCH] vc-region-history: Do not exceed the file maximum line number

The last empty line in a file is not part of the Git repository.
* lisp/vc/vc.el (vc-region-history): Exclude the last empty line
from the region (Bug#24725).
---
 lisp/vc/vc-git.el | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/lisp/vc/vc-git.el b/lisp/vc/vc-git.el
index 9eac5b2..c6a9064 100644
--- a/lisp/vc/vc-git.el
+++ b/lisp/vc/vc-git.el
@@ -1016,6 +1016,8 @@ vc-git-region-history
   ;; FIXME: Maybe this should be done in vc.el (i.e. for all backends), but
   ;; since Git is the only backend to support this operation so far, it's hard
   ;; to tell.
+  (when (> lto (line-number-at-pos (1- (point-max))))
+    (setq lto (line-number-at-pos (1- (point-max)))))
   (with-temp-buffer
     (vc-call-backend 'git 'diff file "HEAD" nil (current-buffer))
     (goto-char (point-min))
-- 
2.9.3

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
In GNU Emacs 25.1.50.1 (x86_64-pc-linux-gnu, GTK+ Version 3.22.1)
 of 2016-10-20
Repository revision: 8988327d548db7b69f30ea15496ccb0726fa4502