Skip to content

Commit c0eba37

Browse files
committed
diff_parse: correctly set options for parsed diffs
The function `diff_parsed_alloc` allocates and initializes a `git_diff_parsed` structure. This structure also contains diff options. While we initialize its flags, we fail to do a real initialization of its values. This bites us when we want to actually use the generated diff as we do not se the option's version field, which is required to operate correctly. Fix the issue by executing `git_diff_init_options` on the embedded struct.
1 parent ad5a909 commit c0eba37

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/diff_parse.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,16 @@ static git_diff_parsed *diff_parsed_alloc(void)
3737

3838
GIT_REFCOUNT_INC(diff);
3939
diff->base.type = GIT_DIFF_TYPE_PARSED;
40-
diff->base.opts.flags &= ~GIT_DIFF_IGNORE_CASE;
4140
diff->base.strcomp = git__strcmp;
4241
diff->base.strncomp = git__strncmp;
4342
diff->base.pfxcomp = git__prefixcmp;
4443
diff->base.entrycomp = git_diff__entry_cmp;
4544
diff->base.patch_fn = git_patch_parsed_from_diff;
4645
diff->base.free_fn = diff_parsed_free;
4746

47+
git_diff_init_options(&diff->base.opts, GIT_DIFF_OPTIONS_VERSION);
48+
diff->base.opts.flags &= ~GIT_DIFF_IGNORE_CASE;
49+
4850
git_pool_init(&diff->base.pool, 1);
4951

5052
if (git_vector_init(&diff->patches, 0, NULL) < 0 ||

tests/diff/parse.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,24 @@ void test_diff_parse__parsing_minimal_patch_succeeds(void)
246246
git_diff_free(diff);
247247
git_buf_free(&buf);
248248
}
249+
250+
void test_diff_parse__patch_roundtrip_succeeds(void)
251+
{
252+
const char buf1[] = "a\n", buf2[] = "b\n";
253+
git_buf patchbuf = GIT_BUF_INIT, diffbuf = GIT_BUF_INIT;
254+
git_patch *patch;
255+
git_diff *diff;
256+
257+
cl_git_pass(git_patch_from_buffers(&patch, buf1, strlen(buf1), "obj1", buf2, strlen(buf2), "obj2", NULL));
258+
cl_git_pass(git_patch_to_buf(&patchbuf, patch));
259+
260+
cl_git_pass(git_diff_from_buffer(&diff, patchbuf.ptr, patchbuf.size));
261+
cl_git_pass(git_diff_to_buf(&diffbuf, diff, GIT_DIFF_FORMAT_PATCH));
262+
263+
cl_assert_equal_s(patchbuf.ptr, diffbuf.ptr);
264+
265+
git_patch_free(patch);
266+
git_diff_free(diff);
267+
git_buf_free(&patchbuf);
268+
git_buf_free(&diffbuf);
269+
}

0 commit comments

Comments
 (0)