Commit 10549a2df166cd3b951c65ab89c33f7ef21c6b7a

Edward Thomson 2015-05-19T18:26:04

Introduce `GIT_DIFF_FLAG_EXISTS` Mark the `old_file` and `new_file` sides of a delta with a new bit, `GIT_DIFF_FLAG_EXISTS`, that introduces that a particular side of the delta exists in the diff. This is useful for indicating whether a working directory item exists or not, in the presence of a conflict. Diff users may have previously used DELETED to determine this information.

diff --git a/CHANGELOG.md b/CHANGELOG.md
index cdd1a5d..cf65fcf 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -52,6 +52,11 @@ support for HTTPS connections insead of OpenSSL.
 * `git_index_conflict_add()` will remove staged entries that exist for
   conflicted paths.
 
+* The flags for a `git_diff_file` will now have the `GIT_DIFF_FLAG_EXISTS`
+  bit set when a file exists on that side of the diff.  This is useful
+  for understanding whether a side of the diff exists in the presence of
+  a conflict.
+
 ### API additions
 
 * The `git_merge_options` gained a `file_flags` member.
diff --git a/include/git2/diff.h b/include/git2/diff.h
index 7505c08..c0d42e3 100644
--- a/include/git2/diff.h
+++ b/include/git2/diff.h
@@ -226,6 +226,7 @@ typedef enum {
 	GIT_DIFF_FLAG_BINARY     = (1u << 0), /**< file(s) treated as binary data */
 	GIT_DIFF_FLAG_NOT_BINARY = (1u << 1), /**< file(s) treated as text data */
 	GIT_DIFF_FLAG_VALID_ID   = (1u << 2), /**< `id` value is known correct */
+	GIT_DIFF_FLAG_EXISTS     = (1u << 3), /**< file exists at this side of the delta */
 } git_diff_flag_t;
 
 /**
diff --git a/src/diff.c b/src/diff.c
index d4f7d26..c9d1d87 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -127,10 +127,12 @@ static int diff_delta__from_one(
 	if (has_old) {
 		delta->old_file.mode = entry->mode;
 		delta->old_file.size = entry->file_size;
+		delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS;
 		git_oid_cpy(&delta->old_file.id, &entry->id);
 	} else /* ADDED, IGNORED, UNTRACKED */ {
 		delta->new_file.mode = entry->mode;
 		delta->new_file.size = entry->file_size;
+		delta->new_file.flags |= GIT_DIFF_FLAG_EXISTS;
 		git_oid_cpy(&delta->new_file.id, &entry->id);
 	}
 
@@ -184,13 +186,16 @@ static int diff_delta__from_two(
 		delta->old_file.size = old_entry->file_size;
 		delta->old_file.mode = old_mode;
 		git_oid_cpy(&delta->old_file.id, old_id);
-		delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID;
+		delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID |
+			GIT_DIFF_FLAG_EXISTS;
 	}
 
 	if (!git_index_entry_is_conflict(new_entry)) {
 		git_oid_cpy(&delta->new_file.id, new_id);
 		delta->new_file.size = new_entry->file_size;
 		delta->new_file.mode = new_mode;
+		delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS;
+		delta->new_file.flags |= GIT_DIFF_FLAG_EXISTS;
 
 		if (!git_oid_iszero(&new_entry->id))
 			delta->new_file.flags |= GIT_DIFF_FLAG_VALID_ID;