apply: introduce `git_apply_tree` Introduce `git_apply_tree`, which will apply a `git_diff` to a given `git_tree`, allowing an in-memory patch application for a repository.
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
diff --git a/include/git2.h b/include/git2.h
index e182ce9..9239b48 100644
--- a/include/git2.h
+++ b/include/git2.h
@@ -9,6 +9,7 @@
#define INCLUDE_git_git_h__
#include "git2/annotated_commit.h"
+#include "git2/apply.h"
#include "git2/attr.h"
#include "git2/blob.h"
#include "git2/blame.h"
diff --git a/include/git2/apply.h b/include/git2/apply.h
new file mode 100644
index 0000000..a4ec4fc
--- /dev/null
+++ b/include/git2/apply.h
@@ -0,0 +1,41 @@
+/*
+ * 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_git_apply_h__
+#define INCLUDE_git_apply_h__
+
+#include "common.h"
+#include "types.h"
+#include "oid.h"
+#include "diff.h"
+
+/**
+ * @file git2/apply.h
+ * @brief Git patch application routines
+ * @defgroup git_apply Git patch application routines
+ * @ingroup Git
+ * @{
+ */
+GIT_BEGIN_DECL
+
+/**
+ * Apply a `git_diff` to a `git_tree`, and return the resulting image
+ * as an index.
+ *
+ * @param out the postimage of the application
+ * @param repo the repository to apply
+ * @param preimage the tree to apply the diff to
+ * @param diff the diff to apply
+ */
+GIT_EXTERN(int) git_apply_to_tree(
+ git_index **out,
+ git_repository *repo,
+ git_tree *preimage,
+ git_diff *diff);
+
+/** @} */
+GIT_END_DECL
+#endif
diff --git a/src/apply.c b/src/apply.c
index 8c7bb6b..be8c7b6 100644
--- a/src/apply.c
+++ b/src/apply.c
@@ -11,6 +11,8 @@
#include "git2/patch.h"
#include "git2/filter.h"
+#include "git2/blob.h"
+#include "git2/index.h"
#include "array.h"
#include "patch.h"
#include "fileops.h"
@@ -376,3 +378,101 @@ done:
return error;
}
+
+static int apply_one(
+ git_repository *repo,
+ git_tree *preimage,
+ git_index *postimage,
+ git_diff *diff,
+ size_t i)
+{
+ git_patch *patch = NULL;
+ git_tree_entry *tree_entry = NULL;
+ git_blob *blob = NULL;
+ git_buf contents = GIT_BUF_INIT;
+ const git_diff_delta *delta;
+ char *filename = NULL;
+ unsigned int mode;
+ git_oid blob_id;
+ git_index_entry index_entry;
+ int error;
+
+ if ((error = git_patch_from_diff(&patch, diff, i)) < 0)
+ goto done;
+
+ delta = git_patch_get_delta(patch);
+
+ if ((error = git_tree_entry_bypath(&tree_entry, preimage,
+ delta->old_file.path)) < 0 ||
+ (error = git_blob_lookup(&blob, repo,
+ git_tree_entry_id(tree_entry))) < 0 ||
+ (error = git_apply__patch(&contents, &filename, &mode,
+ git_blob_rawcontent(blob), git_blob_rawsize(blob), patch)) < 0 ||
+ (error = git_blob_create_frombuffer(&blob_id, repo,
+ contents.ptr, contents.size)) < 0)
+ goto done;
+
+ memset(&index_entry, 0, sizeof(git_index_entry));
+ index_entry.path = filename;
+ index_entry.mode = mode;
+ git_oid_cpy(&index_entry.id, &blob_id);
+
+ if ((error = git_index_add(postimage, &index_entry)) < 0)
+ goto done;
+
+done:
+ git_buf_free(&contents);
+ git__free(filename);
+ git_blob_free(blob);
+ git_tree_entry_free(tree_entry);
+ git_patch_free(patch);
+
+ return error;
+}
+
+int git_apply_to_tree(
+ git_index **out,
+ git_repository *repo,
+ git_tree *preimage,
+ git_diff *diff)
+{
+ git_index *postimage = NULL;
+ const git_diff_delta *delta;
+ size_t i;
+ int error = 0;
+
+ assert(out && repo && preimage && diff);
+
+ *out = NULL;
+
+ if ((error = git_index_new(&postimage)) < 0 ||
+ (error = git_index_read_tree(postimage, preimage)) < 0)
+ goto done;
+
+ /*
+ * Remove the old paths from the index before applying diffs -
+ * we need to do a full pass to remove them before adding deltas,
+ * in order to handle rename situations.
+ */
+ for (i = 0; i < git_diff_num_deltas(diff); i++) {
+ delta = git_diff_get_delta(diff, i);
+
+ if ((error = git_index_remove(postimage,
+ delta->old_file.path, 0)) < 0)
+ goto done;
+ }
+
+ for (i = 0; i < git_diff_num_deltas(diff); i++) {
+ if ((error = apply_one(repo, preimage, postimage, diff, i)) < 0)
+ goto done;
+ }
+
+ *out = postimage;
+
+done:
+ if (error < 0)
+ git_index_free(postimage);
+
+ return error;
+}
+
diff --git a/tests/apply/tree.c b/tests/apply/tree.c
new file mode 100644
index 0000000..38c64c0
--- /dev/null
+++ b/tests/apply/tree.c
@@ -0,0 +1,58 @@
+#include "clar_libgit2.h"
+#include "../merge/merge_helpers.h"
+
+static git_repository *repo;
+
+#define TEST_REPO_PATH "merge-recursive"
+
+
+void test_apply_tree__initialize(void)
+{
+ repo = cl_git_sandbox_init(TEST_REPO_PATH);
+}
+
+void test_apply_tree__cleanup(void)
+{
+ cl_git_sandbox_cleanup();
+}
+
+void test_apply_tree__one(void)
+{
+ git_oid a_oid, b_oid;
+ git_commit *a_commit, *b_commit;
+ git_tree *a_tree, *b_tree;
+ git_diff *diff;
+ git_index *index = NULL;
+ git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
+
+ struct merge_index_entry expected[] = {
+ { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
+ };
+
+ git_oid_fromstr(&a_oid, "539bd011c4822c560c1d17cab095006b7a10f707");
+ git_oid_fromstr(&b_oid, "7c7bf85e978f1d18c0566f702d2cb7766b9c8d4f");
+
+ cl_git_pass(git_commit_lookup(&a_commit, repo, &a_oid));
+ cl_git_pass(git_commit_lookup(&b_commit, repo, &b_oid));
+
+ cl_git_pass(git_commit_tree(&a_tree, a_commit));
+ cl_git_pass(git_commit_tree(&b_tree, b_commit));
+
+ cl_git_pass(git_diff_tree_to_tree(&diff, repo, a_tree, b_tree, &opts));
+
+ cl_git_pass(git_apply_to_tree(&index, repo, a_tree, diff));
+ merge_test_index(index, expected, 6);
+
+ git_index_free(index);
+ git_diff_free(diff);
+ git_tree_free(a_tree);
+ git_tree_free(b_tree);
+ git_commit_free(a_commit);
+ git_commit_free(b_commit);
+}
+