Teach `git_patch_from_diff` about parsed diffs Ensure that `git_patch_from_diff` can return the patch for parsed diffs, not just generate a patch for a generated diff.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
diff --git a/src/diff.h b/src/diff.h
index 2c0e52c..5750d2a 100644
--- a/src/diff.h
+++ b/src/diff.h
@@ -8,6 +8,7 @@
#define INCLUDE_diff_h__
#include "git2/diff.h"
+#include "git2/patch.h"
#include "git2/sys/diff.h"
#include "git2/oid.h"
@@ -44,6 +45,7 @@ struct git_diff {
int (*pfxcomp)(const char *str, const char *pfx);
int (*entrycomp)(const void *a, const void *b);
+ int (*patch_fn)(git_patch **out, git_diff *diff, size_t idx);
void (*free_fn)(git_diff *diff);
};
diff --git a/src/diff_generate.c b/src/diff_generate.c
index a996bf1..06f9b19 100644
--- a/src/diff_generate.c
+++ b/src/diff_generate.c
@@ -7,6 +7,7 @@
#include "common.h"
#include "diff.h"
#include "diff_generate.h"
+#include "patch_generate.h"
#include "fileops.h"
#include "config.h"
#include "attr_file.h"
@@ -414,6 +415,7 @@ static git_diff_generated *diff_generated_alloc(
diff->base.repo = repo;
diff->base.old_src = old_iter->type;
diff->base.new_src = new_iter->type;
+ diff->base.patch_fn = git_patch_generated_from_diff;
diff->base.free_fn = diff_generated_free;
memcpy(&diff->base.opts, &dflt, sizeof(git_diff_options));
diff --git a/src/diff_parse.c b/src/diff_parse.c
index ffdc8df..e640063 100644
--- a/src/diff_parse.c
+++ b/src/diff_parse.c
@@ -6,15 +6,10 @@
*/
#include "common.h"
#include "diff.h"
+#include "diff_parse.h"
#include "patch.h"
#include "patch_parse.h"
-typedef struct {
- struct git_diff base;
-
- git_vector patches;
-} git_diff_parsed;
-
static void diff_parsed_free(git_diff *d)
{
git_diff_parsed *diff = (git_diff_parsed *)d;
@@ -47,6 +42,7 @@ static git_diff_parsed *diff_parsed_alloc(void)
diff->base.strncomp = git__strncmp;
diff->base.pfxcomp = git__prefixcmp;
diff->base.entrycomp = git_diff__entry_cmp;
+ diff->base.patch_fn = git_patch_parsed_from_diff;
diff->base.free_fn = diff_parsed_free;
git_pool_init(&diff->base.pool, 1);
diff --git a/src/diff_parse.h b/src/diff_parse.h
new file mode 100644
index 0000000..c47d4cb
--- /dev/null
+++ b/src/diff_parse.h
@@ -0,0 +1,18 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_diff_parse_h__
+#define INCLUDE_diff_parse_h__
+
+#include "diff.h"
+
+typedef struct {
+ struct git_diff base;
+
+ git_vector patches;
+} git_diff_parsed;
+
+#endif
diff --git a/src/patch.c b/src/patch.c
index e14ffa9..ad24b07 100644
--- a/src/patch.c
+++ b/src/patch.c
@@ -194,6 +194,12 @@ int git_patch_get_line_in_hunk(
return 0;
}
+int git_patch_from_diff(git_patch **out, git_diff *diff, size_t idx)
+{
+ assert(out && diff && diff->patch_fn);
+ return diff->patch_fn(out, diff, idx);
+}
+
static void git_patch__free(git_patch *patch)
{
if (patch->free_fn)
diff --git a/src/patch_generate.c b/src/patch_generate.c
index feac4f6..927786a 100644
--- a/src/patch_generate.c
+++ b/src/patch_generate.c
@@ -746,7 +746,7 @@ int git_patch_from_buffers(
return patch_from_sources(out, &osrc, &nsrc, opts);
}
-int git_patch_from_diff(
+int git_patch_generated_from_diff(
git_patch **patch_ptr, git_diff *diff, size_t idx)
{
int error = 0;
diff --git a/src/patch_generate.h b/src/patch_generate.h
index bb26a76..2e89b5c 100644
--- a/src/patch_generate.h
+++ b/src/patch_generate.h
@@ -42,6 +42,8 @@ extern void git_patch_generated_old_data(
char **, size_t *, git_patch_generated *);
extern void git_patch_generated_new_data(
char **, size_t *, git_patch_generated *);
+extern int git_patch_generated_from_diff(
+ git_patch **, git_diff *, size_t);
typedef struct git_patch_generated_output git_patch_generated_output;
diff --git a/src/patch_parse.c b/src/patch_parse.c
index 82d2d3e..44bcf8f 100644
--- a/src/patch_parse.c
+++ b/src/patch_parse.c
@@ -7,6 +7,7 @@
#include "git2/patch.h"
#include "patch.h"
#include "patch_parse.h"
+#include "diff_parse.h"
#include "path.h"
#define parse_err(...) \
@@ -1025,6 +1026,20 @@ void git_patch_parse_ctx_free(git_patch_parse_ctx *ctx)
GIT_REFCOUNT_DEC(ctx, patch_parse_ctx_free);
}
+int git_patch_parsed_from_diff(git_patch **out, git_diff *d, size_t idx)
+{
+ git_diff_parsed *diff = (git_diff_parsed *)d;
+ git_patch *p;
+
+ if ((p = git_vector_get(&diff->patches, idx)) == NULL)
+ return -1;
+
+ GIT_REFCOUNT_INC(p);
+ *out = p;
+
+ return 0;
+}
+
static void patch_parsed__free(git_patch *p)
{
git_patch_parsed *patch = (git_patch_parsed *)p;
diff --git a/src/patch_parse.h b/src/patch_parse.h
index da56dad..99eb458 100644
--- a/src/patch_parse.h
+++ b/src/patch_parse.h
@@ -51,4 +51,6 @@ extern int git_patch_parse(
git_patch **out,
git_patch_parse_ctx *ctx);
+extern int git_patch_parsed_from_diff(git_patch **, git_diff *, size_t);
+
#endif
diff --git a/tests/diff/parse.c b/tests/diff/parse.c
index 83000a9..a06813d 100644
--- a/tests/diff/parse.c
+++ b/tests/diff/parse.c
@@ -2,6 +2,7 @@
#include "patch.h"
#include "patch_parse.h"
#include "diff_helpers.h"
+#include "../src/diff.h"
#include "../patch/patch_common.h"
@@ -151,3 +152,47 @@ void test_diff_parse__can_parse_generated_diff(void)
GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED | GIT_DIFF_FIND_EXACT_MATCH_ONLY);
}
+void test_diff_parse__get_patch_from_diff(void)
+{
+ git_repository *repo;
+ git_diff *computed, *parsed;
+ git_tree *a, *b;
+ git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
+ git_buf computed_buf = GIT_BUF_INIT;
+ git_patch *patch_computed, *patch_parsed;
+
+ repo = cl_git_sandbox_init("diff");
+
+ opts.flags = GIT_DIFF_SHOW_BINARY;
+
+ cl_assert((a = resolve_commit_oid_to_tree(repo,
+ "d70d245ed97ed2aa596dd1af6536e4bfdb047b69")) != NULL);
+ cl_assert((b = resolve_commit_oid_to_tree(repo,
+ "7a9e0b02e63179929fed24f0a3e0f19168114d10")) != NULL);
+
+ cl_git_pass(git_diff_tree_to_tree(&computed, repo, a, b, &opts));
+ cl_git_pass(git_diff_to_buf(&computed_buf,
+ computed, GIT_DIFF_FORMAT_PATCH));
+ cl_git_pass(git_patch_from_diff(&patch_computed, computed, 0));
+
+ cl_git_pass(git_diff_from_buffer(&parsed,
+ computed_buf.ptr, computed_buf.size));
+ cl_git_pass(git_patch_from_diff(&patch_parsed, parsed, 0));
+
+ cl_assert_equal_i(
+ git_patch_num_hunks(patch_computed),
+ git_patch_num_hunks(patch_parsed));
+
+ git_patch_free(patch_computed);
+ git_patch_free(patch_parsed);
+
+ git_tree_free(a);
+ git_tree_free(b);
+
+ git_diff_free(computed);
+ git_diff_free(parsed);
+
+ git_buf_free(&computed_buf);
+
+ cl_git_sandbox_cleanup();
+}