Commit accd78486523ef875a941ed65b02c56902074d76

Gregory Herrero 2019-11-07T13:02:38

diff_print: add a new 'print_index' flag when printing diff. Add a new 'print_index' flag to let the caller decide whether or not 'index <oid>..<oid>' should be printed. Since patch id needs not to have index when hashing a patch, it will be useful soon. Signed-off-by: Gregory Herrero <gregory.herrero@oracle.com>

diff --git a/src/diff.h b/src/diff.h
index 93374b9..1a4ee47 100644
--- a/src/diff.h
+++ b/src/diff.h
@@ -57,7 +57,8 @@ extern int git_diff_delta__format_file_header(
 	const git_diff_delta *delta,
 	const char *oldpfx,
 	const char *newpfx,
-	int oid_strlen);
+	int oid_strlen,
+	bool print_index);
 
 extern int git_diff_delta__cmp(const void *a, const void *b);
 extern int git_diff_delta__casecmp(const void *a, const void *b);
diff --git a/src/diff_print.c b/src/diff_print.c
index f1aac5e..4124bcb 100644
--- a/src/diff_print.c
+++ b/src/diff_print.c
@@ -269,7 +269,8 @@ static int diff_print_modes(
 }
 
 static int diff_print_oid_range(
-	git_buf *out, const git_diff_delta *delta, int id_strlen)
+	git_buf *out, const git_diff_delta *delta, int id_strlen,
+	bool print_index)
 {
 	char start_oid[GIT_OID_HEXSZ+1], end_oid[GIT_OID_HEXSZ+1];
 
@@ -293,8 +294,9 @@ static int diff_print_oid_range(
 	git_oid_tostr(end_oid, id_strlen + 1, &delta->new_file.id);
 
 	if (delta->old_file.mode == delta->new_file.mode) {
-		git_buf_printf(out, "index %s..%s %o\n",
-			start_oid, end_oid, delta->old_file.mode);
+		if (print_index)
+			git_buf_printf(out, "index %s..%s %o\n",
+				start_oid, end_oid, delta->old_file.mode);
 	} else {
 		if (delta->old_file.mode == 0)
 			git_buf_printf(out, "new file mode %o\n", delta->new_file.mode);
@@ -303,7 +305,8 @@ static int diff_print_oid_range(
 		else
 			diff_print_modes(out, delta);
 
-		git_buf_printf(out, "index %s..%s\n", start_oid, end_oid);
+		if (print_index)
+			git_buf_printf(out, "index %s..%s\n", start_oid, end_oid);
 	}
 
 	return git_buf_oom(out) ? -1 : 0;
@@ -400,7 +403,8 @@ int git_diff_delta__format_file_header(
 	const git_diff_delta *delta,
 	const char *oldpfx,
 	const char *newpfx,
-	int id_strlen)
+	int id_strlen,
+	bool print_index)
 {
 	git_buf old_path = GIT_BUF_INIT, new_path = GIT_BUF_INIT;
 	bool unchanged = delta_is_unchanged(delta);
@@ -431,7 +435,8 @@ int git_diff_delta__format_file_header(
 	}
 
 	if (!unchanged) {
-		if ((error = diff_print_oid_range(out, delta, id_strlen)) < 0)
+		if ((error = diff_print_oid_range(out, delta,
+						  id_strlen, print_index)) < 0)
 			goto done;
 
 		if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0)
@@ -566,6 +571,7 @@ static int diff_print_patch_file(
 		(pi->flags & GIT_DIFF_FORCE_BINARY);
 	bool show_binary = !!(pi->flags & GIT_DIFF_SHOW_BINARY);
 	int id_strlen = pi->id_strlen;
+	bool print_index = true;
 
 	if (binary && show_binary)
 		id_strlen = delta->old_file.id_abbrev ? delta->old_file.id_abbrev :
@@ -582,7 +588,8 @@ static int diff_print_patch_file(
 		return 0;
 
 	if ((error = git_diff_delta__format_file_header(
-			pi->buf, delta, oldpfx, newpfx, id_strlen)) < 0)
+			pi->buf, delta, oldpfx, newpfx,
+			id_strlen, print_index)) < 0)
 		return error;
 
 	pi->line.origin      = GIT_DIFF_LINE_FILE_HDR;
diff --git a/src/patch.c b/src/patch.c
index d19e683..82181bb 100644
--- a/src/patch.c
+++ b/src/patch.c
@@ -79,7 +79,7 @@ size_t git_patch_size(
 		git_buf file_header = GIT_BUF_INIT;
 
 		if (git_diff_delta__format_file_header(
-			&file_header, patch->delta, NULL, NULL, 0) < 0)
+			&file_header, patch->delta, NULL, NULL, 0, true) < 0)
 			git_error_clear();
 		else
 			out += git_buf_len(&file_header);