Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5671e81

Browse files
author
Edward Thomson
authored
Merge pull request libgit2#3908 from libgit2/ethomson/patch_from_diff
Teach `git_patch_from_diff` about parsed diffs
2 parents c60210d + b859faa commit 5671e81

File tree

10 files changed

+95
-7
lines changed

10 files changed

+95
-7
lines changed

src/diff.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define INCLUDE_diff_h__
99

1010
#include "git2/diff.h"
11+
#include "git2/patch.h"
1112
#include "git2/sys/diff.h"
1213
#include "git2/oid.h"
1314

@@ -44,6 +45,7 @@ struct git_diff {
4445
int (*pfxcomp)(const char *str, const char *pfx);
4546
int (*entrycomp)(const void *a, const void *b);
4647

48+
int (*patch_fn)(git_patch **out, git_diff *diff, size_t idx);
4749
void (*free_fn)(git_diff *diff);
4850
};
4951

src/diff_generate.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "common.h"
88
#include "diff.h"
99
#include "diff_generate.h"
10+
#include "patch_generate.h"
1011
#include "fileops.h"
1112
#include "config.h"
1213
#include "attr_file.h"
@@ -414,6 +415,7 @@ static git_diff_generated *diff_generated_alloc(
414415
diff->base.repo = repo;
415416
diff->base.old_src = old_iter->type;
416417
diff->base.new_src = new_iter->type;
418+
diff->base.patch_fn = git_patch_generated_from_diff;
417419
diff->base.free_fn = diff_generated_free;
418420
memcpy(&diff->base.opts, &dflt, sizeof(git_diff_options));
419421

src/diff_parse.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,10 @@
66
*/
77
#include "common.h"
88
#include "diff.h"
9+
#include "diff_parse.h"
910
#include "patch.h"
1011
#include "patch_parse.h"
1112

12-
typedef struct {
13-
struct git_diff base;
14-
15-
git_vector patches;
16-
} git_diff_parsed;
17-
1813
static void diff_parsed_free(git_diff *d)
1914
{
2015
git_diff_parsed *diff = (git_diff_parsed *)d;
@@ -47,6 +42,7 @@ static git_diff_parsed *diff_parsed_alloc(void)
4742
diff->base.strncomp = git__strncmp;
4843
diff->base.pfxcomp = git__prefixcmp;
4944
diff->base.entrycomp = git_diff__entry_cmp;
45+
diff->base.patch_fn = git_patch_parsed_from_diff;
5046
diff->base.free_fn = diff_parsed_free;
5147

5248
git_pool_init(&diff->base.pool, 1);

src/diff_parse.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
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.
6+
*/
7+
#ifndef INCLUDE_diff_parse_h__
8+
#define INCLUDE_diff_parse_h__
9+
10+
#include "diff.h"
11+
12+
typedef struct {
13+
struct git_diff base;
14+
15+
git_vector patches;
16+
} git_diff_parsed;
17+
18+
#endif

src/patch.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ int git_patch_get_line_in_hunk(
194194
return 0;
195195
}
196196

197+
int git_patch_from_diff(git_patch **out, git_diff *diff, size_t idx)
198+
{
199+
assert(out && diff && diff->patch_fn);
200+
return diff->patch_fn(out, diff, idx);
201+
}
202+
197203
static void git_patch__free(git_patch *patch)
198204
{
199205
if (patch->free_fn)

src/patch_generate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ int git_patch_from_buffers(
746746
return patch_from_sources(out, &osrc, &nsrc, opts);
747747
}
748748

749-
int git_patch_from_diff(
749+
int git_patch_generated_from_diff(
750750
git_patch **patch_ptr, git_diff *diff, size_t idx)
751751
{
752752
int error = 0;

src/patch_generate.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ extern void git_patch_generated_old_data(
4242
char **, size_t *, git_patch_generated *);
4343
extern void git_patch_generated_new_data(
4444
char **, size_t *, git_patch_generated *);
45+
extern int git_patch_generated_from_diff(
46+
git_patch **, git_diff *, size_t);
4547

4648
typedef struct git_patch_generated_output git_patch_generated_output;
4749

src/patch_parse.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "git2/patch.h"
88
#include "patch.h"
99
#include "patch_parse.h"
10+
#include "diff_parse.h"
1011
#include "path.h"
1112

1213
#define parse_err(...) \
@@ -1025,6 +1026,20 @@ void git_patch_parse_ctx_free(git_patch_parse_ctx *ctx)
10251026
GIT_REFCOUNT_DEC(ctx, patch_parse_ctx_free);
10261027
}
10271028

1029+
int git_patch_parsed_from_diff(git_patch **out, git_diff *d, size_t idx)
1030+
{
1031+
git_diff_parsed *diff = (git_diff_parsed *)d;
1032+
git_patch *p;
1033+
1034+
if ((p = git_vector_get(&diff->patches, idx)) == NULL)
1035+
return -1;
1036+
1037+
GIT_REFCOUNT_INC(p);
1038+
*out = p;
1039+
1040+
return 0;
1041+
}
1042+
10281043
static void patch_parsed__free(git_patch *p)
10291044
{
10301045
git_patch_parsed *patch = (git_patch_parsed *)p;

src/patch_parse.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,6 @@ extern int git_patch_parse(
5151
git_patch **out,
5252
git_patch_parse_ctx *ctx);
5353

54+
extern int git_patch_parsed_from_diff(git_patch **, git_diff *, size_t);
55+
5456
#endif

tests/diff/parse.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "patch.h"
33
#include "patch_parse.h"
44
#include "diff_helpers.h"
5+
#include "../src/diff.h"
56

67
#include "../patch/patch_common.h"
78

@@ -151,3 +152,47 @@ void test_diff_parse__can_parse_generated_diff(void)
151152
GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED | GIT_DIFF_FIND_EXACT_MATCH_ONLY);
152153
}
153154

155+
void test_diff_parse__get_patch_from_diff(void)
156+
{
157+
git_repository *repo;
158+
git_diff *computed, *parsed;
159+
git_tree *a, *b;
160+
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
161+
git_buf computed_buf = GIT_BUF_INIT;
162+
git_patch *patch_computed, *patch_parsed;
163+
164+
repo = cl_git_sandbox_init("diff");
165+
166+
opts.flags = GIT_DIFF_SHOW_BINARY;
167+
168+
cl_assert((a = resolve_commit_oid_to_tree(repo,
169+
"d70d245ed97ed2aa596dd1af6536e4bfdb047b69")) != NULL);
170+
cl_assert((b = resolve_commit_oid_to_tree(repo,
171+
"7a9e0b02e63179929fed24f0a3e0f19168114d10")) != NULL);
172+
173+
cl_git_pass(git_diff_tree_to_tree(&computed, repo, a, b, &opts));
174+
cl_git_pass(git_diff_to_buf(&computed_buf,
175+
computed, GIT_DIFF_FORMAT_PATCH));
176+
cl_git_pass(git_patch_from_diff(&patch_computed, computed, 0));
177+
178+
cl_git_pass(git_diff_from_buffer(&parsed,
179+
computed_buf.ptr, computed_buf.size));
180+
cl_git_pass(git_patch_from_diff(&patch_parsed, parsed, 0));
181+
182+
cl_assert_equal_i(
183+
git_patch_num_hunks(patch_computed),
184+
git_patch_num_hunks(patch_parsed));
185+
186+
git_patch_free(patch_computed);
187+
git_patch_free(patch_parsed);
188+
189+
git_tree_free(a);
190+
git_tree_free(b);
191+
192+
git_diff_free(computed);
193+
git_diff_free(parsed);
194+
195+
git_buf_free(&computed_buf);
196+
197+
cl_git_sandbox_cleanup();
198+
}

0 commit comments

Comments
 (0)