]> git.proxmox.com Git - libgit2.git/blob - src/diff.c
Merge branch 'pr/3809'
[libgit2.git] / src / diff.c
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 #include "git2/version.h"
8 #include "common.h"
9 #include "diff.h"
10 #include "diff_generate.h"
11 #include "patch.h"
12 #include "commit.h"
13 #include "index.h"
14
15 #define DIFF_FLAG_IS_SET(DIFF,FLAG) \
16 (((DIFF)->opts.flags & (FLAG)) != 0)
17 #define DIFF_FLAG_ISNT_SET(DIFF,FLAG) \
18 (((DIFF)->opts.flags & (FLAG)) == 0)
19 #define DIFF_FLAG_SET(DIFF,FLAG,VAL) (DIFF)->opts.flags = \
20 (VAL) ? ((DIFF)->opts.flags | (FLAG)) : ((DIFF)->opts.flags & ~(VAL))
21
22 GIT_INLINE(const char *) diff_delta__path(const git_diff_delta *delta)
23 {
24 const char *str = delta->old_file.path;
25
26 if (!str ||
27 delta->status == GIT_DELTA_ADDED ||
28 delta->status == GIT_DELTA_RENAMED ||
29 delta->status == GIT_DELTA_COPIED)
30 str = delta->new_file.path;
31
32 return str;
33 }
34
35 const char *git_diff_delta__path(const git_diff_delta *delta)
36 {
37 return diff_delta__path(delta);
38 }
39
40 int git_diff_delta__cmp(const void *a, const void *b)
41 {
42 const git_diff_delta *da = a, *db = b;
43 int val = strcmp(diff_delta__path(da), diff_delta__path(db));
44 return val ? val : ((int)da->status - (int)db->status);
45 }
46
47 int git_diff_delta__casecmp(const void *a, const void *b)
48 {
49 const git_diff_delta *da = a, *db = b;
50 int val = strcasecmp(diff_delta__path(da), diff_delta__path(db));
51 return val ? val : ((int)da->status - (int)db->status);
52 }
53
54 int git_diff__entry_cmp(const void *a, const void *b)
55 {
56 const git_index_entry *entry_a = a;
57 const git_index_entry *entry_b = b;
58
59 return strcmp(entry_a->path, entry_b->path);
60 }
61
62 int git_diff__entry_icmp(const void *a, const void *b)
63 {
64 const git_index_entry *entry_a = a;
65 const git_index_entry *entry_b = b;
66
67 return strcasecmp(entry_a->path, entry_b->path);
68 }
69
70 void git_diff_free(git_diff *diff)
71 {
72 if (!diff)
73 return;
74
75 GIT_REFCOUNT_DEC(diff, diff->free_fn);
76 }
77
78 void git_diff_addref(git_diff *diff)
79 {
80 GIT_REFCOUNT_INC(diff);
81 }
82
83 size_t git_diff_num_deltas(const git_diff *diff)
84 {
85 assert(diff);
86 return diff->deltas.length;
87 }
88
89 size_t git_diff_num_deltas_of_type(const git_diff *diff, git_delta_t type)
90 {
91 size_t i, count = 0;
92 const git_diff_delta *delta;
93
94 assert(diff);
95
96 git_vector_foreach(&diff->deltas, i, delta) {
97 count += (delta->status == type);
98 }
99
100 return count;
101 }
102
103 const git_diff_delta *git_diff_get_delta(const git_diff *diff, size_t idx)
104 {
105 assert(diff);
106 return git_vector_get(&diff->deltas, idx);
107 }
108
109 int git_diff_is_sorted_icase(const git_diff *diff)
110 {
111 return (diff->opts.flags & GIT_DIFF_IGNORE_CASE) != 0;
112 }
113
114 int git_diff_get_perfdata(git_diff_perfdata *out, const git_diff *diff)
115 {
116 assert(out);
117 GITERR_CHECK_VERSION(out, GIT_DIFF_PERFDATA_VERSION, "git_diff_perfdata");
118 out->stat_calls = diff->perf.stat_calls;
119 out->oid_calculations = diff->perf.oid_calculations;
120 return 0;
121 }
122
123 int git_diff_format_email__append_header_tobuf(
124 git_buf *out,
125 const git_oid *id,
126 const git_signature *author,
127 const char *summary,
128 const char *body,
129 size_t patch_no,
130 size_t total_patches,
131 bool exclude_patchno_marker)
132 {
133 char idstr[GIT_OID_HEXSZ + 1];
134 char date_str[GIT_DATE_RFC2822_SZ];
135 int error = 0;
136
137 git_oid_fmt(idstr, id);
138 idstr[GIT_OID_HEXSZ] = '\0';
139
140 if ((error = git__date_rfc2822_fmt(date_str, sizeof(date_str),
141 &author->when)) < 0)
142 return error;
143
144 error = git_buf_printf(out,
145 "From %s Mon Sep 17 00:00:00 2001\n" \
146 "From: %s <%s>\n" \
147 "Date: %s\n" \
148 "Subject: ",
149 idstr,
150 author->name, author->email,
151 date_str);
152
153 if (error < 0)
154 return error;
155
156 if (!exclude_patchno_marker) {
157 if (total_patches == 1) {
158 error = git_buf_puts(out, "[PATCH] ");
159 } else {
160 error = git_buf_printf(out, "[PATCH %"PRIuZ"/%"PRIuZ"] ",
161 patch_no, total_patches);
162 }
163
164 if (error < 0)
165 return error;
166 }
167
168 error = git_buf_printf(out, "%s\n\n", summary);
169
170 if (body) {
171 git_buf_puts(out, body);
172
173 if (out->ptr[out->size - 1] != '\n')
174 git_buf_putc(out, '\n');
175 }
176
177 return error;
178 }
179
180 int git_diff_format_email__append_patches_tobuf(
181 git_buf *out,
182 git_diff *diff)
183 {
184 size_t i, deltas;
185 int error = 0;
186
187 deltas = git_diff_num_deltas(diff);
188
189 for (i = 0; i < deltas; ++i) {
190 git_patch *patch = NULL;
191
192 if ((error = git_patch_from_diff(&patch, diff, i)) >= 0)
193 error = git_patch_to_buf(out, patch);
194
195 git_patch_free(patch);
196
197 if (error < 0)
198 break;
199 }
200
201 return error;
202 }
203
204 int git_diff_format_email(
205 git_buf *out,
206 git_diff *diff,
207 const git_diff_format_email_options *opts)
208 {
209 git_diff_stats *stats = NULL;
210 char *summary = NULL, *loc = NULL;
211 bool ignore_marker;
212 unsigned int format_flags = 0;
213 size_t allocsize;
214 int error;
215
216 assert(out && diff && opts);
217 assert(opts->summary && opts->id && opts->author);
218
219 GITERR_CHECK_VERSION(opts,
220 GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION,
221 "git_format_email_options");
222
223 ignore_marker = (opts->flags &
224 GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER) != 0;
225
226 if (!ignore_marker) {
227 if (opts->patch_no > opts->total_patches) {
228 giterr_set(GITERR_INVALID,
229 "patch %"PRIuZ" out of range. max %"PRIuZ,
230 opts->patch_no, opts->total_patches);
231 return -1;
232 }
233
234 if (opts->patch_no == 0) {
235 giterr_set(GITERR_INVALID,
236 "invalid patch no %"PRIuZ". should be >0", opts->patch_no);
237 return -1;
238 }
239 }
240
241 /* the summary we receive may not be clean.
242 * it could potentially contain new line characters
243 * or not be set, sanitize, */
244 if ((loc = strpbrk(opts->summary, "\r\n")) != NULL) {
245 size_t offset = 0;
246
247 if ((offset = (loc - opts->summary)) == 0) {
248 giterr_set(GITERR_INVALID, "summary is empty");
249 error = -1;
250 goto on_error;
251 }
252
253 GITERR_CHECK_ALLOC_ADD(&allocsize, offset, 1);
254 summary = git__calloc(allocsize, sizeof(char));
255 GITERR_CHECK_ALLOC(summary);
256
257 strncpy(summary, opts->summary, offset);
258 }
259
260 error = git_diff_format_email__append_header_tobuf(out,
261 opts->id, opts->author, summary == NULL ? opts->summary : summary,
262 opts->body, opts->patch_no, opts->total_patches, ignore_marker);
263
264 if (error < 0)
265 goto on_error;
266
267 format_flags = GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_INCLUDE_SUMMARY;
268
269 if ((error = git_buf_puts(out, "---\n")) < 0 ||
270 (error = git_diff_get_stats(&stats, diff)) < 0 ||
271 (error = git_diff_stats_to_buf(out, stats, format_flags, 0)) < 0 ||
272 (error = git_buf_putc(out, '\n')) < 0 ||
273 (error = git_diff_format_email__append_patches_tobuf(out, diff)) < 0)
274 goto on_error;
275
276 error = git_buf_puts(out, "--\nlibgit2 " LIBGIT2_VERSION "\n\n");
277
278 on_error:
279 git__free(summary);
280 git_diff_stats_free(stats);
281
282 return error;
283 }
284
285 int git_diff_commit_as_email(
286 git_buf *out,
287 git_repository *repo,
288 git_commit *commit,
289 size_t patch_no,
290 size_t total_patches,
291 git_diff_format_email_flags_t flags,
292 const git_diff_options *diff_opts)
293 {
294 git_diff *diff = NULL;
295 git_diff_format_email_options opts =
296 GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
297 int error;
298
299 assert (out && repo && commit);
300
301 opts.flags = flags;
302 opts.patch_no = patch_no;
303 opts.total_patches = total_patches;
304 opts.id = git_commit_id(commit);
305 opts.summary = git_commit_summary(commit);
306 opts.body = git_commit_body(commit);
307 opts.author = git_commit_author(commit);
308
309 if ((error = git_diff__commit(&diff, repo, commit, diff_opts)) < 0)
310 return error;
311
312 error = git_diff_format_email(out, diff, &opts);
313
314 git_diff_free(diff);
315 return error;
316 }
317
318 int git_diff_init_options(git_diff_options *opts, unsigned int version)
319 {
320 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
321 opts, version, git_diff_options, GIT_DIFF_OPTIONS_INIT);
322 return 0;
323 }
324
325 int git_diff_find_init_options(
326 git_diff_find_options *opts, unsigned int version)
327 {
328 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
329 opts, version, git_diff_find_options, GIT_DIFF_FIND_OPTIONS_INIT);
330 return 0;
331 }
332
333 int git_diff_format_email_init_options(
334 git_diff_format_email_options *opts, unsigned int version)
335 {
336 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
337 opts, version, git_diff_format_email_options,
338 GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT);
339 return 0;
340 }
341