introduce got_diff_blob_file_lines_changed()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
diff --git a/lib/diff.c b/lib/diff.c
index d572937..cab65a3 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -140,8 +140,19 @@ got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
NULL);
}
-const struct got_error *
-got_diff_blob_file(struct got_blob_object *blob1, FILE *f2, size_t size2,
+static const struct got_error *
+alloc_changes(struct got_diff_changes **changes)
+{
+ *changes = calloc(1, sizeof(**changes));
+ if (*changes == NULL)
+ return got_error_from_errno("calloc");
+ SIMPLEQ_INIT(&(*changes)->entries);
+ return NULL;
+}
+
+static const struct got_error *
+diff_blob_file(struct got_diff_changes **changes,
+ struct got_blob_object *blob1, FILE *f2, size_t size2,
const char *label2, int diff_context, FILE *outfile)
{
struct got_diff_state ds;
@@ -153,6 +164,9 @@ got_diff_blob_file(struct got_blob_object *blob1, FILE *f2, size_t size2,
size_t size1;
int res, flags = 0;
+ if (changes)
+ *changes = NULL;
+
size1 = 0;
if (blob1) {
f1 = got_opentemp();
@@ -189,9 +203,18 @@ got_diff_blob_file(struct got_blob_object *blob1, FILE *f2, size_t size2,
args.diff_context = diff_context;
flags |= D_PROTOTYPE;
- fprintf(outfile, "blob - %s\n", idstr1);
- fprintf(outfile, "file + %s\n", f2 == NULL ? "/dev/null" : label2);
- err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, NULL);
+ if (outfile) {
+ fprintf(outfile, "blob - %s\n", idstr1);
+ fprintf(outfile, "file + %s\n",
+ f2 == NULL ? "/dev/null" : label2);
+ }
+ if (changes) {
+ err = alloc_changes(changes);
+ if (err)
+ return err;
+ }
+ err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile,
+ changes ? *changes : NULL);
done:
if (f1 && fclose(f1) != 0 && err == NULL)
err = got_error_from_errno("fclose");
@@ -199,15 +222,31 @@ done:
}
const struct got_error *
+got_diff_blob_file(struct got_blob_object *blob1, FILE *f2, size_t size2,
+ const char *label2, int diff_context, FILE *outfile)
+{
+ return diff_blob_file(NULL, blob1, f2, size2, label2, diff_context,
+ outfile);
+}
+
+const struct got_error *
+got_diff_blob_file_lines_changed(struct got_diff_changes **changes,
+ struct got_blob_object *blob1, FILE *f2, size_t size2,
+ const char *label2, int diff_context)
+{
+ return diff_blob_file(changes, blob1, f2, size2, label2, diff_context,
+ NULL);
+}
+
+const struct got_error *
got_diff_blob_lines_changed(struct got_diff_changes **changes,
struct got_blob_object *blob1, struct got_blob_object *blob2)
{
const struct got_error *err = NULL;
- *changes = calloc(1, sizeof(**changes));
- if (*changes == NULL)
- return got_error_from_errno("calloc");
- SIMPLEQ_INIT(&(*changes)->entries);
+ err = alloc_changes(changes);
+ if (err)
+ return err;
err = diff_blobs(blob1, blob2, NULL, NULL, 3, NULL, *changes);
if (err) {
diff --git a/lib/got_lib_diff.h b/lib/got_lib_diff.h
index b8fe79b..c9a2662 100644
--- a/lib/got_lib_diff.h
+++ b/lib/got_lib_diff.h
@@ -140,6 +140,8 @@ const struct got_error *got_diffreg(int *, FILE *,
FILE *, int, struct got_diff_args *, struct got_diff_state *, FILE *,
struct got_diff_changes *);
+const struct got_error *got_diff_blob_file_lines_changed(struct got_diff_changes **,
+ struct got_blob_object *, FILE *, size_t, const char *, int);
const struct got_error *got_diff_blob_lines_changed(struct got_diff_changes **,
struct got_blob_object *, struct got_blob_object *);
void got_diff_free_changes(struct got_diff_changes *);