"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Tom Jones <thj@freebsd.org>
Subject:
diff: Add api for consumers to check if diff is printable
To:
gameoftrees@openbsd.org
Date:
Thu, 29 Sep 2022 10:17:52 +0100

Download raw body.

Thread
Programs using the libdiff api they can need to know if the diff
contained anything that would be printed, or would be empty.

Expose the same check that the outputs do as a function call.

- Tom
diff --git a/include/diff_main.h b/include/diff_main.h
index 5e816ae..7b6d039 100644
--- a/include/diff_main.h
+++ b/include/diff_main.h
@@ -251,3 +251,4 @@ struct diff_result *diff_main(const struct diff_config *config,
 			      struct diff_data *left,
 			      struct diff_data *right);
 void diff_result_free(struct diff_result *result);
+int diff_printable_chunks(struct diff_result *result);
diff --git a/lib/diff_main.c b/lib/diff_main.c
index c531ad2..3043a9d 100644
--- a/lib/diff_main.c
+++ b/lib/diff_main.c
@@ -628,3 +628,19 @@ diff_result_free(struct diff_result *result)
 	ARRAYLIST_FREE(result->chunks);
 	free(result);
 }
+
+int
+diff_printable_chunks(struct diff_result *result)
+{
+	struct diff_chunk *c;
+	enum diff_chunk_type t;
+
+	for (int i = 0; i < result->chunks.len; i++) {
+		c = &result->chunks.head[i];
+		t = diff_chunk_type(c);
+		if (t == CHUNK_MINUS || t == CHUNK_PLUS)
+			return 1;
+	}
+
+	return 0;
+}