2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
8 #include "diff_parse.h"
12 #include "patch_parse.h"
14 static void diff_parsed_free(git_diff
*d
)
16 git_diff_parsed
*diff
= (git_diff_parsed
*)d
;
20 git_vector_foreach(&diff
->patches
, i
, patch
)
21 git_patch_free(patch
);
23 git_vector_free(&diff
->patches
);
25 git_vector_free(&diff
->base
.deltas
);
26 git_pool_clear(&diff
->base
.pool
);
28 git__memzero(diff
, sizeof(*diff
));
32 static git_diff_parsed
*diff_parsed_alloc(void)
34 git_diff_parsed
*diff
;
36 if ((diff
= git__calloc(1, sizeof(git_diff_parsed
))) == NULL
)
39 GIT_REFCOUNT_INC(&diff
->base
);
40 diff
->base
.type
= GIT_DIFF_TYPE_PARSED
;
41 diff
->base
.strcomp
= git__strcmp
;
42 diff
->base
.strncomp
= git__strncmp
;
43 diff
->base
.pfxcomp
= git__prefixcmp
;
44 diff
->base
.entrycomp
= git_diff__entry_cmp
;
45 diff
->base
.patch_fn
= git_patch_parsed_from_diff
;
46 diff
->base
.free_fn
= diff_parsed_free
;
48 if (git_diff_init_options(&diff
->base
.opts
, GIT_DIFF_OPTIONS_VERSION
) < 0) {
53 diff
->base
.opts
.flags
&= ~GIT_DIFF_IGNORE_CASE
;
55 git_pool_init(&diff
->base
.pool
, 1);
57 if (git_vector_init(&diff
->patches
, 0, NULL
) < 0 ||
58 git_vector_init(&diff
->base
.deltas
, 0, git_diff_delta__cmp
) < 0) {
59 git_diff_free(&diff
->base
);
63 git_vector_set_cmp(&diff
->base
.deltas
, git_diff_delta__cmp
);
68 int git_diff_from_buffer(
73 git_diff_parsed
*diff
;
75 git_patch_parse_ctx
*ctx
= NULL
;
80 diff
= diff_parsed_alloc();
81 GIT_ERROR_CHECK_ALLOC(diff
);
83 ctx
= git_patch_parse_ctx_init(content
, content_len
, NULL
);
84 GIT_ERROR_CHECK_ALLOC(ctx
);
86 while (ctx
->parse_ctx
.remain_len
) {
87 if ((error
= git_patch_parse(&patch
, ctx
)) < 0)
90 git_vector_insert(&diff
->patches
, patch
);
91 git_vector_insert(&diff
->base
.deltas
, patch
->delta
);
94 if (error
== GIT_ENOTFOUND
&& git_vector_length(&diff
->patches
) > 0) {
99 git_patch_parse_ctx_free(ctx
);
102 git_diff_free(&diff
->base
);