branch: master commit c1525a86618fc89904d399e5cecb3c435fe1211b Author: Justin Burkett <jus...@burkett.cc> Commit: Justin Burkett <jus...@burkett.cc>
Fix problems with diff -u parser at beg and end of file Add a test as well. --- vdiff-tests.el | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ vdiff.el | 33 +++++++++++++++++--------------- 2 files changed, 78 insertions(+), 15 deletions(-) diff --git a/vdiff-tests.el b/vdiff-tests.el new file mode 100644 index 0000000..ad71e73 --- /dev/null +++ b/vdiff-tests.el @@ -0,0 +1,60 @@ +;;; vdiff-tests.el --- tests for vdiff.el -*- lexical-binding: t; -*- + +;; Copyright (C) 2018 Free Software Foundation, Inc. + +;; Author: Justin Burkett <jus...@burkett.cc> +;; Maintainer: Justin Burkett <jus...@burkett.cc> + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see <http://www.gnu.org/licenses/>. + +;;; Code: + +(require 'ert) +(require 'vdiff) + +(ert-deftest vdiff-test-parsing () + "" + (with-temp-buffer + (insert "--- test1.txt 2018-04-13 11:11:41.000000000 -0400 ++++ test2.txt 2018-04-13 11:11:46.000000000 -0400 +@@ -1,3 +1,6 @@ ++ ++ ++ + 1 + 2 + 3 +@@ -9,6 +12,8 @@ + 9 + 10 + 11 ++11 ++11 + 12 + 13 + 14 +@@ -16,7 +21,8 @@ + 16 + 17 + 18 +-19 +-20 ++18 ++29 + 21 + 22 ++23 +") + (should (equal (vdiff--parse-diff-u (current-buffer)) + '(((1) (1 . 3)) ((12) (15 . 16)) ((19 . 20) (24 . 25)) ((23) (28 . 28))))))) diff --git a/vdiff.el b/vdiff.el index 8c6e378..700f311 100644 --- a/vdiff.el +++ b/vdiff.el @@ -567,9 +567,11 @@ an addition when compared to other vdiff buffers." (forward-line) (let ((a (car lines)) (b (cdr lines))) - (cond ((looking-at-p " ") (cons (1+ a) (1+ b))) - ((looking-at-p "+") (cons a (1+ b))) - ((looking-at-p "-") (cons (1+ a) b))))) + (prog1 + (cond ((or (looking-at-p " ") (eobp)) (cons (1+ a) (1+ b))) + ((looking-at-p "+") (cons a (1+ b))) + ((looking-at-p "-") (cons (1+ a) b))) + (message "a:%s b:%s l:%s" a b (buffer-substring (point) (line-end-position)))))) (defun vdiff--parse-diff-u (buf) "Parse diff -u output in BUF and return list of hunks." @@ -584,17 +586,17 @@ an addition when compared to other vdiff buffers." (lines (cons start-line-a start-line-b))) (while (and (not (looking-at-p "@")) (not (eobp))) - (setq lines (vdiff--inc-lines lines)) (cond ((looking-at-p "+") ;; addition (let ((beg-a (car lines)) (beg-b (cdr lines))) (while (looking-at-p "+") (setq lines (vdiff--inc-lines lines))) - (cl-assert (looking-at-p " ")) + (cl-assert (or (looking-at-p " ") (eobp))) (push - (list (vdiff--encode-range t beg-a) - (vdiff--encode-range nil beg-b (1- (cdr lines)))) + ;; there's no context lines at the beginning of the file + (list (cons (if (= beg-a 1) 1 (1+ beg-a)) nil) + (cons beg-b (1- (cdr lines)))) res))) ((looking-at-p "-") ;; subtraction or change @@ -602,21 +604,22 @@ an addition when compared to other vdiff buffers." (beg-b (cdr lines))) (while (looking-at-p "-") (setq lines (vdiff--inc-lines lines))) - (if (looking-at-p " ") + (if (or (looking-at-p " ") (eobp)) ;; subtraction (push - (list (vdiff--encode-range nil beg-a (1- (car lines))) - (vdiff--encode-range t beg-b)) + (list (cons beg-a (if (= (car lines) 1) 1 (1- (car lines)))) + (cons nil beg-b)) res) - (cl-assert (looking-at-p "+")) + (cl-assert (or (looking-at-p "+") (eobp))) (let ((beg-b (cdr lines))) (while (looking-at-p "+") (setq lines (vdiff--inc-lines lines))) - (cl-assert (looking-at-p " ")) + (cl-assert (or (looking-at-p " ") (eobp))) (push - (list (vdiff--encode-range nil beg-a (1- (car lines))) - (vdiff--encode-range nil beg-b (1- (cdr lines)))) - res)))))))))) + (list (cons beg-a (1- (car lines))) + (cons beg-b (1- (cdr lines)))) + res)))))) + (setq lines (vdiff--inc-lines lines)))))) (nreverse res))) (defun vdiff--parse-diff3 (buf)