Commit Diff


commit - 4861c9da86ada01cffa2ce3801f670587e896ce2
commit + 486215cf28522983f77b83d7a2a5effc522fea5f
blob - 13fb82c4b8e75e74ec16e8b30fea6c29fd41efb3
blob + 3707ff39d6fbd6d28afc2a01bc56bd367ffec06c
--- lib/diff_main.c
+++ lib/diff_main.c
@@ -225,10 +225,12 @@ diff_state_add_solved_chunk(struct diff_state *state,
 	 * never directly follows a plus chunk. */
 	result = &state->result->chunks;
 
-	last_t = diff_chunk_type(&result->head[result->len - 1]);
+	last_t = result->len ? diff_chunk_type(&result->head[result->len - 1])
+		: CHUNK_EMPTY;
 	new_t = diff_chunk_type(chunk);
 
-	debug("Add %s chunk:\n", chunk->solved ? "solved" : "UNSOLVED");
+	debug("ADD %s chunk #%u:\n", chunk->solved ? "solved" : "UNSOLVED",
+	      result->len);
 	debug("L\n");
 	debug_dump_atoms(&state->left, chunk->left_start, chunk->left_count);
 	debug("R\n");
@@ -298,7 +300,6 @@ diff_state_add_chunk(struct diff_state *state, bool so
 		     struct diff_atom *left_start, unsigned int left_count,
 		     struct diff_atom *right_start, unsigned int right_count)
 {
-	diff_chunk_arraylist_t *result = NULL;
 	struct diff_chunk *new_chunk;
 	struct diff_chunk chunk = {
 		.solved = solved,
@@ -308,26 +309,21 @@ diff_state_add_chunk(struct diff_state *state, bool so
 		.right_count = right_count,
 	};
 
+	/* An unsolved chunk means store as intermediate result for later
+	 * re-iteration.
+	 * If there already are intermediate results, that means even a
+	 * following solved chunk needs to go to intermediate results, so that
+	 * it is later put in the final correct position in solved chunks.
+	 */
 	if (!solved || state->temp_result.len) {
 		/* Append to temp_result */
-		result = &state->temp_result;
-		debug("append to temp_result %p L %u  R %u\n", result,
-		      left_count, right_count);
-		debug("L\n");
-		debug_dump_atoms(&state->left, left_start, left_count);
-		debug("R\n");
-		debug_dump_atoms(&state->right, right_start, right_count);
-	} else if (!state->result->chunks.len) {
-		/* Append to final result */
-		result = &state->result->chunks;
-		debug("Add first chunk:\n");
+		debug("ADD %s chunk to temp result:\n",
+		      chunk.solved ? "solved" : "UNSOLVED");
 		debug("L\n");
 		debug_dump_atoms(&state->left, left_start, left_count);
 		debug("R\n");
 		debug_dump_atoms(&state->right, right_start, right_count);
-	}
-	if (result) {
-		ARRAYLIST_ADD(new_chunk, *result);
+		ARRAYLIST_ADD(new_chunk, state->temp_result);
 		if (!new_chunk)
 			return NULL;
 		*new_chunk = chunk;