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 /**
8  * License: GPL-2.0(Linking Exception)
9  */
10 module libgit2.apply;
11 
12 
13 private static import libgit2.diff;
14 private static import libgit2.types;
15 private import libgit2.common: GIT_EXTERN;
16 
17 /*
18  * @file git2/apply.h
19  * @brief Git patch application routines
20  * @defgroup git_apply Git patch application routines
21  * @ingroup Git
22  * @{
23  */
24 extern (C):
25 nothrow @nogc:
26 public:
27 
28 /**
29  * When applying a patch, callback that will be made per delta (file).
30  *
31  * When the callback:
32  * - returns < 0, the apply process will be aborted.
33  * - returns > 0, the delta will not be applied, but the apply process
34  *      continues
35  * - returns 0, the delta is applied, and the apply process continues.
36  *
37  * Returns: 0 if the delta is applied, < 0 if the apply process will be aborted or > 0 if the delta will not be applied.
38  */
39 /*
40  * Params:
41  *      delta = The delta to be applied
42  *      payload = User-specified payload
43  */
44 alias git_apply_delta_cb = int function(const (libgit2.diff.git_diff_delta)* delta, void* payload);
45 
46 /**
47  * When applying a patch, callback that will be made per hunk.
48  *
49  * When the callback:
50  * - returns < 0, the apply process will be aborted.
51  * - returns > 0, the hunk will not be applied, but the apply process
52  *      continues
53  * - returns 0, the hunk is applied, and the apply process continues.
54  *
55  * Returns: 0 if the hunk is applied, < 0 if the apply process will be aborted or > 0 if the hunk will not be applied.
56  */
57 /*
58  * Params:
59  *      hunk = The hunk to be applied
60  *      payload = User-specified payload
61  */
62 alias git_apply_hunk_cb = int function(const (libgit2.diff.git_diff_hunk)* hunk, void* payload);
63 
64 /**
65  * Flags controlling the behavior of git_apply
66  */
67 enum git_apply_flags_t
68 {
69 	/**
70 	 * Don't actually make changes, just test that the patch applies.
71 	 * This is the equivalent of `git apply --check`.
72 	 */
73 	GIT_APPLY_CHECK = 1 << 0,
74 }
75 
76 //Declaration name in C language
77 enum
78 {
79 	GIT_APPLY_CHECK = .git_apply_flags_t.GIT_APPLY_CHECK,
80 }
81 
82 /**
83  * Apply options structure
84  *
85  * Initialize with `GIT_APPLY_OPTIONS_INIT`. Alternatively, you can
86  * use `git_apply_options_init`.
87  *
88  * @see git_apply_to_tree, git_apply
89  */
90 struct git_apply_options
91 {
92 	/**
93 	 * The version
94 	 */
95 	uint version_;
96 
97 	/**
98 	 * When applying a patch, callback that will be made per delta (file).
99 	 */
100 	.git_apply_delta_cb delta_cb;
101 
102 	/**
103 	 * When applying a patch, callback that will be made per hunk.
104 	 */
105 	.git_apply_hunk_cb hunk_cb;
106 
107 	/**
108 	 * Payload passed to both delta_cb & hunk_cb.
109 	 */
110 	void* payload;
111 
112 	/**
113 	 * Bitmask of git_apply_flags_t
114 	 */
115 	uint flags;
116 }
117 
118 enum GIT_APPLY_OPTIONS_VERSION = 1;
119 
120 pragma(inline, true)
121 pure nothrow @safe @nogc @live
122 .git_apply_options GIT_APPLY_OPTIONS_INIT()
123 
124 	do
125 	{
126 		.git_apply_options OUTPUT =
127 		{
128 			version_: .GIT_APPLY_OPTIONS_VERSION,
129 		};
130 
131 		return OUTPUT;
132 	}
133 
134 /**
135  * Initialize git_apply_options structure
136  *
137  * Initialize a `git_apply_options` with default values. Equivalent to creating
138  * an instance with GIT_APPLY_OPTIONS_INIT.
139  *
140  * Params:
141  *      opts = The `git_apply_options` struct to initialize.
142  *      version_ = The struct version; pass `GIT_APPLY_OPTIONS_VERSION`
143  *
144  * Returns: 0 on success or -1 on failure.
145  */
146 @GIT_EXTERN
147 int git_apply_options_init(.git_apply_options* opts, uint version_);
148 
149 /**
150  * Apply a `git_diff` to a `git_tree`, and return the resulting image
151  * as an index.
152  *
153  * Params:
154  *      out_ = the postimage of the application
155  *      repo = the repository to apply
156  *      preimage = the tree to apply the diff to
157  *      diff = the diff to apply
158  *      options = the options for the apply (or null for defaults)
159  *
160  * Returns: 0 or an error code
161  */
162 @GIT_EXTERN
163 int git_apply_to_tree(libgit2.types.git_index** out_, libgit2.types.git_repository* repo, libgit2.types.git_tree* preimage, libgit2.diff.git_diff* diff, const (.git_apply_options)* options);
164 
165 /**
166  * Possible application locations for git_apply
167  */
168 enum git_apply_location_t
169 {
170 	/**
171 	 * Apply the patch to the workdir, leaving the index untouched.
172 	 * This is the equivalent of `git apply` with no location argument.
173 	 */
174 	GIT_APPLY_LOCATION_WORKDIR = 0,
175 
176 	/**
177 	 * Apply the patch to the index, leaving the working directory
178 	 * untouched.  This is the equivalent of `git apply --cached`.
179 	 */
180 	GIT_APPLY_LOCATION_INDEX = 1,
181 
182 	/**
183 	 * Apply the patch to both the working directory and the index.
184 	 * This is the equivalent of `git apply --index`.
185 	 */
186 	GIT_APPLY_LOCATION_BOTH = 2,
187 }
188 
189 //Declaration name in C language
190 enum
191 {
192 	GIT_APPLY_LOCATION_WORKDIR = .git_apply_location_t.GIT_APPLY_LOCATION_WORKDIR,
193 	GIT_APPLY_LOCATION_INDEX = .git_apply_location_t.GIT_APPLY_LOCATION_INDEX,
194 	GIT_APPLY_LOCATION_BOTH = .git_apply_location_t.GIT_APPLY_LOCATION_BOTH,
195 }
196 
197 /**
198  * Apply a `git_diff` to the given repository, making changes directly
199  * in the working directory, the index, or both.
200  *
201  * Params:
202  *      repo = the repository to apply to
203  *      diff = the diff to apply
204  *      location = the location to apply (workdir, index or both)
205  *      options = the options for the apply (or null for defaults)
206  *
207  * Returns: 0 or an error code
208  */
209 @GIT_EXTERN
210 int git_apply(libgit2.types.git_repository* repo, libgit2.diff.git_diff* diff, .git_apply_location_t location, const (.git_apply_options)* options);
211 
212 /* @} */