Skip to content

Commit 02af1fc

Browse files
committed
apply: add GIT_APPLY_CHECK
This adds an option which will check if a diff is applicable without actually applying it; equivalent to git apply --check.
1 parent b246bed commit 02af1fc

File tree

4 files changed

+161
-3
lines changed

4 files changed

+161
-3
lines changed

include/git2/apply.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ typedef int GIT_CALLBACK(git_apply_hunk_cb)(
5353
const git_diff_hunk *hunk,
5454
void *payload);
5555

56+
/** Flags controlling the behavior of git_apply */
57+
typedef enum {
58+
/**
59+
* Don't actually make changes, just test that the patch applies.
60+
* This is the equivalent of `git apply --check`.
61+
*/
62+
GIT_APPLY_CHECK = (1 << 0),
63+
} git_apply_flags_t;
64+
5665
/**
5766
* Apply options structure
5867
*
@@ -72,6 +81,9 @@ typedef struct {
7281

7382
/** Payload passed to both delta_cb & hunk_cb. */
7483
void *payload;
84+
85+
/** Bitmask of git_apply_flags_t */
86+
unsigned int flags;
7587
} git_apply_options;
7688

7789
#define GIT_APPLY_OPTIONS_VERSION 1

src/apply.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -845,13 +845,17 @@ int git_apply(
845845
(error = git_reader_for_index(&post_reader, repo, postimage)) < 0)
846846
goto done;
847847

848-
if ((error = git_repository_index(&index, repo)) < 0 ||
849-
(error = git_indexwriter_init(&indexwriter, index)) < 0)
850-
goto done;
848+
if (!(opts.flags & GIT_APPLY_CHECK))
849+
if ((error = git_repository_index(&index, repo)) < 0 ||
850+
(error = git_indexwriter_init(&indexwriter, index)) < 0)
851+
goto done;
851852

852853
if ((error = apply_deltas(repo, pre_reader, preimage, post_reader, postimage, diff, &opts)) < 0)
853854
goto done;
854855

856+
if ((opts.flags & GIT_APPLY_CHECK))
857+
goto done;
858+
855859
switch (location) {
856860
case GIT_APPLY_LOCATION_BOTH:
857861
error = git_apply__to_workdir(repo, diff, preimage, postimage, location, &opts);

tests/apply/apply_helpers.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,27 @@
2121
"-longer. take out the slices of ham, and skim off the grease if any\n" \
2222
"+longer; take out the slices of ham, and skim off the grease if any\n"
2323

24+
/* This is the binary equivalent of DIFF_MODIFY_TWO_FILES */
25+
#define DIFF_MODIFY_TWO_FILES_BINARY \
26+
"diff --git a/asparagus.txt b/asparagus.txt\n" \
27+
"index f51658077d85f2264fa179b4d0848268cb3475c3..ffb36e513f5fdf8a6ba850a20142676a2ac4807d 100644\n" \
28+
"GIT binary patch\n" \
29+
"delta 24\n" \
30+
"fcmX@ja+-zTF*v|6$k9DCSRvRyG(c}7zYP-rT_OhP\n" \
31+
"\n" \
32+
"delta 24\n" \
33+
"fcmX@ja+-zTF*v|6$k9DCSRvRyG(d49zYP-rT;T@W\n" \
34+
"\n" \
35+
"diff --git a/veal.txt b/veal.txt\n" \
36+
"index 94d2c01087f48213bd157222d54edfefd77c9bba..a7b066537e6be7109abfe4ff97b675d4e077da20 100644\n" \
37+
"GIT binary patch\n" \
38+
"delta 26\n" \
39+
"hcmX@kah!uI%+=9HA=p1OKyM?L03)OIW@$zpW&mXg25bNT\n" \
40+
"\n" \
41+
"delta 26\n" \
42+
"hcmX@kah!uI%+=9HA=p1OKyf3N03)N`W@$zpW&mU#22ub3\n" \
43+
"\n"
44+
2445
#define DIFF_DELETE_FILE \
2546
"diff --git a/gravy.txt b/gravy.txt\n" \
2647
"deleted file mode 100644\n" \

tests/apply/check.c

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include "clar_libgit2.h"
2+
#include "apply_helpers.h"
3+
4+
static git_repository *repo;
5+
6+
#define TEST_REPO_PATH "merge-recursive"
7+
8+
void test_apply_check__initialize(void)
9+
{
10+
git_oid oid;
11+
git_commit *commit;
12+
13+
repo = cl_git_sandbox_init(TEST_REPO_PATH);
14+
15+
git_oid_fromstr(&oid, "539bd011c4822c560c1d17cab095006b7a10f707");
16+
cl_git_pass(git_commit_lookup(&commit, repo, &oid));
17+
cl_git_pass(git_reset(repo, (git_object *)commit, GIT_RESET_HARD, NULL));
18+
git_commit_free(commit);
19+
}
20+
21+
void test_apply_check__cleanup(void)
22+
{
23+
cl_git_sandbox_cleanup();
24+
}
25+
26+
void test_apply_check__generate_diff(void)
27+
{
28+
git_oid a_oid, b_oid;
29+
git_commit *a_commit, *b_commit;
30+
git_tree *a_tree, *b_tree;
31+
git_diff *diff;
32+
git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
33+
git_apply_options opts = GIT_APPLY_OPTIONS_INIT;
34+
35+
cl_git_pass(git_oid_fromstr(&a_oid, "539bd011c4822c560c1d17cab095006b7a10f707"));
36+
cl_git_pass(git_oid_fromstr(&b_oid, "7c7bf85e978f1d18c0566f702d2cb7766b9c8d4f"));
37+
cl_git_pass(git_commit_lookup(&a_commit, repo, &a_oid));
38+
cl_git_pass(git_commit_lookup(&b_commit, repo, &b_oid));
39+
40+
cl_git_pass(git_commit_tree(&a_tree, a_commit));
41+
cl_git_pass(git_commit_tree(&b_tree, b_commit));
42+
43+
opts.flags |= GIT_APPLY_CHECK;
44+
cl_git_pass(git_diff_tree_to_tree(&diff, repo, a_tree, b_tree, &diff_opts));
45+
cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, &opts));
46+
47+
validate_index_unchanged(repo);
48+
validate_workdir_unchanged(repo);
49+
50+
git_diff_free(diff);
51+
git_tree_free(a_tree);
52+
git_tree_free(b_tree);
53+
git_commit_free(a_commit);
54+
git_commit_free(b_commit);
55+
}
56+
57+
void test_apply_check__parsed_diff(void)
58+
{
59+
git_diff *diff;
60+
git_apply_options opts = GIT_APPLY_OPTIONS_INIT;
61+
62+
opts.flags |= GIT_APPLY_CHECK;
63+
cl_git_pass(git_diff_from_buffer(&diff,
64+
DIFF_MODIFY_TWO_FILES, strlen(DIFF_MODIFY_TWO_FILES)));
65+
cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_INDEX, &opts));
66+
67+
validate_index_unchanged(repo);
68+
validate_workdir_unchanged(repo);
69+
70+
git_diff_free(diff);
71+
}
72+
73+
void test_apply_check__binary(void)
74+
{
75+
git_diff *diff;
76+
git_apply_options opts = GIT_APPLY_OPTIONS_INIT;
77+
78+
opts.flags |= GIT_APPLY_CHECK;
79+
cl_git_pass(git_diff_from_buffer(&diff,
80+
DIFF_MODIFY_TWO_FILES_BINARY,
81+
strlen(DIFF_MODIFY_TWO_FILES_BINARY)));
82+
cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_INDEX, &opts));
83+
84+
validate_index_unchanged(repo);
85+
validate_workdir_unchanged(repo);
86+
87+
git_diff_free(diff);
88+
}
89+
90+
void test_apply_check__does_not_apply(void)
91+
{
92+
git_diff *diff;
93+
git_index *index;
94+
git_apply_options opts = GIT_APPLY_OPTIONS_INIT;
95+
96+
const char *diff_file = DIFF_MODIFY_TWO_FILES;
97+
98+
struct merge_index_entry index_expected[] = {
99+
{ 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
100+
{ 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
101+
{ 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
102+
{ 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
103+
{ 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
104+
};
105+
size_t index_expected_cnt = sizeof(index_expected) /
106+
sizeof(struct merge_index_entry);
107+
108+
/* mutate the index */
109+
cl_git_pass(git_repository_index(&index, repo));
110+
cl_git_pass(git_index_remove(index, "veal.txt", 0));
111+
cl_git_pass(git_index_write(index));
112+
git_index_free(index);
113+
114+
opts.flags |= GIT_APPLY_CHECK;
115+
cl_git_pass(git_diff_from_buffer(&diff, diff_file, strlen(diff_file)));
116+
cl_git_fail_with(GIT_EAPPLYFAIL, git_apply(repo, diff, GIT_APPLY_LOCATION_INDEX, &opts));
117+
118+
validate_apply_index(repo, index_expected, index_expected_cnt);
119+
120+
git_diff_free(diff);
121+
}

0 commit comments

Comments
 (0)