Commit Diff


commit - 41ff30f35eb8d2ee0688753ce9be23cb2727b92a
commit + 2146cf12c5afb7faa0de37de5465f09b6b604ce5
blob - e1c91e207ea111f2b335b80d5614b68e6413b480
blob + 2f436da2ef8207a4c86d757974d9937918bea73e
--- lib/diff_main.c
+++ lib/diff_main.c
@@ -364,6 +364,7 @@ diff_algo_none(const struct diff_algo_config *algo_con
 	struct diff_atom *r = state->right.atoms.head;
 	unsigned int r_len = state->right.atoms.len;
 	unsigned int equal_atoms_start = 0;
+	unsigned int equal_atoms_end = 0;
 
 	while (equal_atoms_start < l_len
 	       && equal_atoms_start < r_len) {
@@ -377,6 +378,20 @@ diff_algo_none(const struct diff_algo_config *algo_con
 			break;
 		equal_atoms_start++;
 	}
+	while (equal_atoms_end < (l_len - equal_atoms_start)
+	       && equal_atoms_end < (r_len - equal_atoms_start)) {
+		int err;
+		bool same;
+		err = diff_atom_same(&same, &l[l_len - 1 - equal_atoms_end],
+				   &r[r_len - 1 - equal_atoms_end]);
+		if (err)
+			return err;
+		if (!same)
+			break;
+		equal_atoms_end++;
+	}
+
+	/* Add a chunk of equal lines at the start */
 	if (equal_atoms_start) {
 		if (!diff_state_add_chunk(state, true,
 					  &l[0],
@@ -387,22 +402,33 @@ diff_algo_none(const struct diff_algo_config *algo_con
 	}
 
 	/* Add a "minus" chunk with all lines from the left. */
-	if (equal_atoms_start < l_len) {
+	if (equal_atoms_start + equal_atoms_end < l_len) {
 		if (!diff_state_add_chunk(state, true,
 					  &l[equal_atoms_start],
-					  l_len - equal_atoms_start,
+					  l_len - equal_atoms_start - equal_atoms_end,
 					  NULL, 0))
 		    return ENOMEM;
 	}
 
 	/* Add a "plus" chunk with all lines from the right. */
-	if (equal_atoms_start < r_len) {
+	if (equal_atoms_start + equal_atoms_end < r_len) {
 		if (!diff_state_add_chunk(state, true,
 					  NULL, 0,
 					  &r[equal_atoms_start],
-					  r_len - equal_atoms_start))
+					  r_len - equal_atoms_start - equal_atoms_end))
 		return ENOMEM;
 	}
+
+	/* Add a chunk of equal lines at the end */
+	if (equal_atoms_end) {
+		if (!diff_state_add_chunk(state, true,
+					  &l[l_len - equal_atoms_end],
+					  equal_atoms_end,
+					  &r[r_len - equal_atoms_end],
+					  equal_atoms_end))
+			return ENOMEM;
+	}
+
 	return DIFF_RC_OK;
 }