Commit Diff
Diff:
e450035c854c7301e281718ffd136dfe4647e681
9343b92549173e00690f24756a633423770f56d7
Commit:
9343b92549173e00690f24756a633423770f56d7
Tree:
947fb3235a72ee52bf858475c11472c1e1f9cabf
Author:
Mark Jamsek <mark@jamsek.dev>
Date:
Thu Aug 4 11:23:55 2022 UTC
Message:
collect line type metadata when generating diff

Similar to line offsets, create an array of line types (i.e., hunk, context,
minus, plus) for each line output in the diff.

ok stsp@
commit - e450035c854c7301e281718ffd136dfe4647e681
commit + 9343b92549173e00690f24756a633423770f56d7
blob - 6cd959e4159e40e5fcc467e4f69c7318fd54d3d5
blob + 304efb917050acadd79aae06eec9473ae5fdc797
--- include/diff_output.h
+++ include/diff_output.h
@@ -32,6 +32,16 @@ struct diff_output_info {
* The last offset in this array corresponds to end-of-file.
*/
ARRAYLIST(off_t) line_offsets;
+ /*
+ * Type (i.e., context, minus, plus) of each line generated by the diff.
+ * nb. 0x00 to 0x3b reserved for client-defined line types.
+ */
+ ARRAYLIST(uint8_t) line_types;
+#define DIFF_LINE_HUNK 0x3c
+#define DIFF_LINE_MINUS 0x3d
+#define DIFF_LINE_PLUS 0x3e
+#define DIFF_LINE_CONTEXT 0x3f
+#define DIFF_LINE_NONE 0x40 /* binary or no EOF newline msg, etc. */
};
void diff_output_info_free(struct diff_output_info *output_info);
blob - e44ac0a4e3f7fe9caf5b350a89c7e5c5beba00be
blob + f9f748fd298e5716a0fbfa2a0ff0277abc7a331e
--- lib/diff_output.c
+++ lib/diff_output.c
@@ -64,6 +64,7 @@ diff_output_lines(struct diff_output_info *outinfo, FI
{
struct diff_atom *atom;
off_t outoff = 0, *offp;
+ uint8_t *typep;
int rc;
if (outinfo && outinfo->line_offsets.len > 0) {
@@ -122,6 +123,12 @@ diff_output_lines(struct diff_output_info *outinfo, FI
return ENOMEM;
outoff += outlen;
*offp = outoff;
+ ARRAYLIST_ADD(typep, outinfo->line_types);
+ if (typep == NULL)
+ return ENOMEM;
+ *typep = *prefix == ' ' ? DIFF_LINE_CONTEXT :
+ *prefix == '-' ? DIFF_LINE_MINUS :
+ *prefix == '+' ? DIFF_LINE_PLUS : DIFF_LINE_NONE;
}
}
@@ -207,7 +214,9 @@ diff_output_trailing_newline_msg(struct diff_output_in
unsigned int atom_count;
int rc, ch;
off_t outoff = 0, *offp;
+ uint8_t *typep;
+
if (chunk_type == CHUNK_MINUS || chunk_type == CHUNK_SAME) {
start_atom = c->left_start;
atom_count = c->left_count;
@@ -240,6 +249,10 @@ diff_output_trailing_newline_msg(struct diff_output_in
return ENOMEM;
outoff += rc;
*offp = outoff;
+ ARRAYLIST_ADD(typep, outinfo->line_types);
+ if (typep == NULL)
+ return ENOMEM;
+ *typep = DIFF_LINE_NONE;
}
}
@@ -316,6 +329,7 @@ diff_output_info_alloc(void)
{
struct diff_output_info *output_info;
off_t *offp;
+ uint8_t *typep;
output_info = malloc(sizeof(*output_info));
if (output_info != NULL) {
@@ -326,6 +340,13 @@ diff_output_info_alloc(void)
return NULL;
}
*offp = 0;
+ ARRAYLIST_INIT(output_info->line_types, 128);
+ ARRAYLIST_ADD(typep, output_info->line_types);
+ if (typep == NULL) {
+ diff_output_info_free(output_info);
+ return NULL;
+ }
+ *typep = DIFF_LINE_NONE;
}
return output_info;
}
@@ -334,6 +355,7 @@ diff_output_info_free(struct diff_output_info *output_
diff_output_info_free(struct diff_output_info *output_info)
{
ARRAYLIST_FREE(output_info->line_offsets);
+ ARRAYLIST_FREE(output_info->line_types);
free(output_info);
}
blob - f3f4ad74c807d77b28b7b0af68f0d0ef2435d5c1
blob + 18bff74063081228c1c525cce2b35ad26a527bf8
--- lib/diff_output_unidiff.c
+++ lib/diff_output_unidiff.c
@@ -229,6 +229,7 @@ output_unidiff_chunk(struct diff_output_info *outinfo,
{
int rc, left_start, left_len, right_start, right_len;
off_t outoff = 0, *offp;
+ uint8_t *typep;
if (diff_range_empty(&cc->left) && diff_range_empty(&cc->right))
return DIFF_RC_OK;
@@ -249,7 +250,10 @@ output_unidiff_chunk(struct diff_output_info *outinfo,
return ENOMEM;
outoff += rc;
*offp = outoff;
-
+ ARRAYLIST_ADD(typep, outinfo->line_types);
+ if (typep == NULL)
+ return ENOMEM;
+ *typep = DIFF_LINE_MINUS;
}
rc = fprintf(dest, "+++ %s\n",
diff_output_get_label_right(info));
@@ -261,7 +265,10 @@ output_unidiff_chunk(struct diff_output_info *outinfo,
return ENOMEM;
outoff += rc;
*offp = outoff;
-
+ ARRAYLIST_ADD(typep, outinfo->line_types);
+ if (typep == NULL)
+ return ENOMEM;
+ *typep = DIFF_LINE_PLUS;
}
state->header_printed = true;
}
@@ -319,7 +326,10 @@ output_unidiff_chunk(struct diff_output_info *outinfo,
return ENOMEM;
outoff += rc;
*offp = outoff;
-
+ ARRAYLIST_ADD(typep, outinfo->line_types);
+ if (typep == NULL)
+ return ENOMEM;
+ *typep = DIFF_LINE_HUNK;
}
/* Got the absolute line numbers where to start printing, and the index
@@ -426,6 +436,7 @@ diff_output_unidiff(struct diff_output_info **output_i
bool force_text = (flags & DIFF_FLAG_FORCE_TEXT_DATA);
bool have_binary = (atomizer_flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
off_t outoff = 0, *offp;
+ uint8_t *typep;
int rc, i;
if (!result)
@@ -463,7 +474,10 @@ diff_output_unidiff(struct diff_output_info **output_i
return ENOMEM;
outoff += rc;
*offp = outoff;
-
+ ARRAYLIST_ADD(typep, outinfo->line_types);
+ if (typep == NULL)
+ return ENOMEM;
+ *typep = DIFF_LINE_NONE;
}
break;
}
Omar Polo