Commit 62a2fc06d4af781c456bf753d6ada16df682df55

Patrick Steinhardt 2017-03-14T13:06:25

patch_generate: move `git_diff_foreach` to diff.c Now that the `git_diff_foreach` function does not depend on internals of the `git_patch_generated` structure anymore, we can easily move it to the actual diff code.

diff --git a/src/diff.c b/src/diff.c
index 317d495..a93bd4c 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -120,6 +120,41 @@ int git_diff_get_perfdata(git_diff_perfdata *out, const git_diff *diff)
 	return 0;
 }
 
+int git_diff_foreach(
+	git_diff *diff,
+	git_diff_file_cb file_cb,
+	git_diff_binary_cb binary_cb,
+	git_diff_hunk_cb hunk_cb,
+	git_diff_line_cb data_cb,
+	void *payload)
+{
+	int error = 0;
+	git_diff_delta *delta;
+	size_t idx;
+
+	assert(diff);
+
+	git_vector_foreach(&diff->deltas, idx, delta) {
+		git_patch *patch;
+
+		/* check flags against patch status */
+		if (git_diff_delta__should_skip(&diff->opts, delta))
+			continue;
+
+		if ((error = git_patch_from_diff(&patch, diff, idx)) != 0)
+			break;
+
+		error = git_patch__invoke_callbacks(patch, file_cb, binary_cb,
+						    hunk_cb, data_cb, payload);
+		git_patch_free(patch);
+
+		if (error)
+			break;
+	}
+
+	return error;
+}
+
 int git_diff_format_email__append_header_tobuf(
 	git_buf *out,
 	const git_oid *id,
diff --git a/src/patch_generate.c b/src/patch_generate.c
index bba1162..804fc0e 100644
--- a/src/patch_generate.c
+++ b/src/patch_generate.c
@@ -400,42 +400,6 @@ static int diff_required(git_diff *diff, const char *action)
 	return -1;
 }
 
-int git_diff_foreach(
-	git_diff *diff,
-	git_diff_file_cb file_cb,
-	git_diff_binary_cb binary_cb,
-	git_diff_hunk_cb hunk_cb,
-	git_diff_line_cb data_cb,
-	void *payload)
-{
-	int error = 0;
-	git_diff_delta *delta;
-	size_t idx;
-
-	if ((error = diff_required(diff, "git_diff_foreach")) < 0)
-		return error;
-
-	git_vector_foreach(&diff->deltas, idx, delta) {
-		git_patch *patch;
-
-		/* check flags against patch status */
-		if (git_diff_delta__should_skip(&diff->opts, delta))
-			continue;
-
-		if ((error = git_patch_from_diff(&patch, diff, idx)) != 0)
-			break;
-
-		error = git_patch__invoke_callbacks(patch, file_cb, binary_cb,
-						    hunk_cb, data_cb, payload);
-		git_patch_free(patch);
-
-		if (error)
-			break;
-	}
-
-	return error;
-}
-
 typedef struct {
 	git_patch_generated patch;
 	git_diff_delta delta;