Commit b921964b25c14c8dfeb26d1a5efedb28ee9e7284

Gregory Herrero 2019-11-07T13:08:51

diff_print: add support for GIT_DIFF_FORMAT_PATCH_ID. Git is generating patch-id using a stripped down version of a patch where hunk header and index information are not present. Signed-off-by: Gregory Herrero <gregory.herrero@oracle.com>

diff --git a/include/git2/diff.h b/include/git2/diff.h
index 73c5e55..89733a7 100644
--- a/include/git2/diff.h
+++ b/include/git2/diff.h
@@ -1093,6 +1093,7 @@ typedef enum {
 	GIT_DIFF_FORMAT_RAW          = 3u, /**< like git diff --raw */
 	GIT_DIFF_FORMAT_NAME_ONLY    = 4u, /**< like git diff --name-only */
 	GIT_DIFF_FORMAT_NAME_STATUS  = 5u, /**< like git diff --name-status */
+	GIT_DIFF_FORMAT_PATCH_ID     = 6u, /**< git diff as used by git patch-id */
 } git_diff_format_t;
 
 /**
diff --git a/src/diff_print.c b/src/diff_print.c
index 4124bcb..369e5c1 100644
--- a/src/diff_print.c
+++ b/src/diff_print.c
@@ -571,7 +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;
+	bool print_index = (pi->format != GIT_DIFF_FORMAT_PATCH_ID);
 
 	if (binary && show_binary)
 		id_strlen = delta->old_file.id_abbrev ? delta->old_file.id_abbrev :
@@ -677,6 +677,11 @@ int git_diff_print(
 		print_hunk = diff_print_patch_hunk;
 		print_line = diff_print_patch_line;
 		break;
+	case GIT_DIFF_FORMAT_PATCH_ID:
+		print_file = diff_print_patch_file;
+		print_binary = diff_print_patch_binary;
+		print_line = diff_print_patch_line;
+		break;
 	case GIT_DIFF_FORMAT_PATCH_HEADER:
 		print_file = diff_print_patch_file;
 		break;