]> git.proxmox.com Git - libgit2.git/blame - src/diff_file.c
buffer: introduce git_buf_attach_notowned
[libgit2.git] / src / diff_file.c
CommitLineData
114f5a6c
RB
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 "common.h"
8#include "git2/blob.h"
9#include "git2/submodule.h"
10#include "diff.h"
11#include "diff_file.h"
12#include "odb.h"
13#include "fileops.h"
14#include "filter.h"
15
16#define DIFF_MAX_FILESIZE 0x20000000
17
18static bool diff_file_content_binary_by_size(git_diff_file_content *fc)
19{
20 /* if we have diff opts, check max_size vs file size */
74ded024 21 if ((fc->file->flags & DIFF_FLAGS_KNOWN_BINARY) == 0 &&
5dc98298 22 fc->opts_max_size > 0 &&
74ded024
RB
23 fc->file->size > fc->opts_max_size)
24 fc->file->flags |= GIT_DIFF_FLAG_BINARY;
114f5a6c 25
74ded024 26 return ((fc->file->flags & GIT_DIFF_FLAG_BINARY) != 0);
114f5a6c
RB
27}
28
29static void diff_file_content_binary_by_content(git_diff_file_content *fc)
30{
74ded024 31 if ((fc->file->flags & DIFF_FLAGS_KNOWN_BINARY) != 0)
114f5a6c
RB
32 return;
33
34 switch (git_diff_driver_content_is_binary(
35 fc->driver, fc->map.data, fc->map.len)) {
74ded024
RB
36 case 0: fc->file->flags |= GIT_DIFF_FLAG_NOT_BINARY; break;
37 case 1: fc->file->flags |= GIT_DIFF_FLAG_BINARY; break;
114f5a6c
RB
38 default: break;
39 }
40}
41
5dc98298
RB
42static int diff_file_content_init_common(
43 git_diff_file_content *fc, const git_diff_options *opts)
114f5a6c 44{
5dc98298
RB
45 fc->opts_flags = opts ? opts->flags : GIT_DIFF_NORMAL;
46
47 if (opts && opts->max_size >= 0)
48 fc->opts_max_size = opts->max_size ?
49 opts->max_size : DIFF_MAX_FILESIZE;
114f5a6c 50
74ded024 51 if (fc->src == GIT_ITERATOR_TYPE_EMPTY)
114f5a6c 52 fc->src = GIT_ITERATOR_TYPE_TREE;
74ded024
RB
53
54 if (!fc->driver &&
55 git_diff_driver_lookup(&fc->driver, fc->repo, fc->file->path) < 0)
56 return -1;
114f5a6c 57
5dc98298
RB
58 /* give driver a chance to modify options */
59 git_diff_driver_update_options(&fc->opts_flags, fc->driver);
60
114f5a6c 61 /* make sure file is conceivable mmap-able */
74ded024
RB
62 if ((git_off_t)((size_t)fc->file->size) != fc->file->size)
63 fc->file->flags |= GIT_DIFF_FLAG_BINARY;
5dc98298
RB
64 /* check if user is forcing text diff the file */
65 else if (fc->opts_flags & GIT_DIFF_FORCE_TEXT) {
74ded024
RB
66 fc->file->flags &= ~GIT_DIFF_FLAG_BINARY;
67 fc->file->flags |= GIT_DIFF_FLAG_NOT_BINARY;
5dc98298
RB
68 }
69 /* check if user is forcing binary diff the file */
70 else if (fc->opts_flags & GIT_DIFF_FORCE_BINARY) {
74ded024
RB
71 fc->file->flags &= ~GIT_DIFF_FLAG_NOT_BINARY;
72 fc->file->flags |= GIT_DIFF_FLAG_BINARY;
5dc98298 73 }
114f5a6c
RB
74
75 diff_file_content_binary_by_size(fc);
76
74ded024
RB
77 if ((fc->flags & GIT_DIFF_FLAG__NO_DATA) != 0) {
78 fc->flags |= GIT_DIFF_FLAG__LOADED;
114f5a6c
RB
79 fc->map.len = 0;
80 fc->map.data = "";
81 }
82
74ded024 83 if ((fc->flags & GIT_DIFF_FLAG__LOADED) != 0)
114f5a6c
RB
84 diff_file_content_binary_by_content(fc);
85
86 return 0;
87}
88
360f42f4 89int git_diff_file_content__init_from_diff(
114f5a6c 90 git_diff_file_content *fc,
3ff1d123 91 git_diff *diff,
114f5a6c
RB
92 size_t delta_index,
93 bool use_old)
94{
95 git_diff_delta *delta = git_vector_get(&diff->deltas, delta_index);
114f5a6c
RB
96 bool has_data = true;
97
98 memset(fc, 0, sizeof(*fc));
99 fc->repo = diff->repo;
74ded024 100 fc->file = use_old ? &delta->old_file : &delta->new_file;
114f5a6c 101 fc->src = use_old ? diff->old_src : diff->new_src;
114f5a6c 102
74ded024 103 if (git_diff_driver_lookup(&fc->driver, fc->repo, fc->file->path) < 0)
114f5a6c
RB
104 return -1;
105
106 switch (delta->status) {
107 case GIT_DELTA_ADDED:
108 has_data = !use_old; break;
109 case GIT_DELTA_DELETED:
110 has_data = use_old; break;
111 case GIT_DELTA_UNTRACKED:
112 has_data = !use_old &&
10672e3e 113 (diff->opts.flags & GIT_DIFF_SHOW_UNTRACKED_CONTENT) != 0;
114f5a6c 114 break;
61bef72d 115 case GIT_DELTA_UNREADABLE:
114f5a6c
RB
116 case GIT_DELTA_MODIFIED:
117 case GIT_DELTA_COPIED:
118 case GIT_DELTA_RENAMED:
119 break;
120 default:
121 has_data = false;
122 break;
123 }
124
125 if (!has_data)
74ded024 126 fc->flags |= GIT_DIFF_FLAG__NO_DATA;
114f5a6c 127
5dc98298 128 return diff_file_content_init_common(fc, &diff->opts);
114f5a6c
RB
129}
130
6789b7a7 131int git_diff_file_content__init_from_src(
114f5a6c
RB
132 git_diff_file_content *fc,
133 git_repository *repo,
134 const git_diff_options *opts,
6789b7a7 135 const git_diff_file_content_src *src,
74ded024 136 git_diff_file *as_file)
114f5a6c
RB
137{
138 memset(fc, 0, sizeof(*fc));
139 fc->repo = repo;
74ded024 140 fc->file = as_file;
6789b7a7 141 fc->blob = src->blob;
114f5a6c 142
6789b7a7 143 if (!src->blob && !src->buf) {
74ded024 144 fc->flags |= GIT_DIFF_FLAG__NO_DATA;
114f5a6c 145 } else {
74ded024 146 fc->flags |= GIT_DIFF_FLAG__LOADED;
9950bb4e 147 fc->file->flags |= GIT_DIFF_FLAG_VALID_ID;
74ded024 148 fc->file->mode = GIT_FILEMODE_BLOB;
114f5a6c 149
6789b7a7
RB
150 if (src->blob) {
151 fc->file->size = git_blob_rawsize(src->blob);
152 git_oid_cpy(&fc->file->id, git_blob_id(src->blob));
114f5a6c 153
6789b7a7
RB
154 fc->map.len = (size_t)fc->file->size;
155 fc->map.data = (char *)git_blob_rawcontent(src->blob);
156 } else {
157 fc->file->size = src->buflen;
158 git_odb_hash(&fc->file->id, src->buf, src->buflen, GIT_OBJ_BLOB);
114f5a6c 159
6789b7a7
RB
160 fc->map.len = src->buflen;
161 fc->map.data = (char *)src->buf;
162 }
114f5a6c
RB
163 }
164
5dc98298 165 return diff_file_content_init_common(fc, opts);
114f5a6c
RB
166}
167
168static int diff_file_content_commit_to_str(
169 git_diff_file_content *fc, bool check_status)
170{
171 char oid[GIT_OID_HEXSZ+1];
172 git_buf content = GIT_BUF_INIT;
173 const char *status = "";
174
175 if (check_status) {
176 int error = 0;
177 git_submodule *sm = NULL;
178 unsigned int sm_status = 0;
179 const git_oid *sm_head;
180
a15c7802 181 if ((error = git_submodule_lookup(&sm, fc->repo, fc->file->path)) < 0) {
114f5a6c 182 /* GIT_EEXISTS means a "submodule" that has not been git added */
a15c7802
RB
183 if (error == GIT_EEXISTS) {
184 giterr_clear();
114f5a6c 185 error = 0;
a15c7802
RB
186 }
187 return error;
188 }
189
190 if ((error = git_submodule_status(&sm_status, sm)) < 0) {
191 git_submodule_free(sm);
114f5a6c
RB
192 return error;
193 }
194
195 /* update OID if we didn't have it previously */
9950bb4e 196 if ((fc->file->flags & GIT_DIFF_FLAG_VALID_ID) == 0 &&
114f5a6c
RB
197 ((sm_head = git_submodule_wd_id(sm)) != NULL ||
198 (sm_head = git_submodule_head_id(sm)) != NULL))
199 {
9950bb4e
CMN
200 git_oid_cpy(&fc->file->id, sm_head);
201 fc->file->flags |= GIT_DIFF_FLAG_VALID_ID;
114f5a6c
RB
202 }
203
204 if (GIT_SUBMODULE_STATUS_IS_WD_DIRTY(sm_status))
205 status = "-dirty";
a15c7802
RB
206
207 git_submodule_free(sm);
114f5a6c
RB
208 }
209
9950bb4e 210 git_oid_tostr(oid, sizeof(oid), &fc->file->id);
114f5a6c
RB
211 if (git_buf_printf(&content, "Subproject commit %s%s\n", oid, status) < 0)
212 return -1;
213
214 fc->map.len = git_buf_len(&content);
215 fc->map.data = git_buf_detach(&content);
74ded024 216 fc->flags |= GIT_DIFF_FLAG__FREE_DATA;
114f5a6c
RB
217
218 return 0;
219}
220
221static int diff_file_content_load_blob(git_diff_file_content *fc)
222{
223 int error = 0;
224 git_odb_object *odb_obj = NULL;
225
9950bb4e 226 if (git_oid_iszero(&fc->file->id))
114f5a6c
RB
227 return 0;
228
74ded024 229 if (fc->file->mode == GIT_FILEMODE_COMMIT)
114f5a6c
RB
230 return diff_file_content_commit_to_str(fc, false);
231
232 /* if we don't know size, try to peek at object header first */
74ded024 233 if (!fc->file->size) {
effdbeb3
RB
234 if ((error = git_diff_file__resolve_zero_size(
235 fc->file, &odb_obj, fc->repo)) < 0)
114f5a6c 236 return error;
114f5a6c
RB
237 }
238
239 if (diff_file_content_binary_by_size(fc))
240 return 0;
241
242 if (odb_obj != NULL) {
243 error = git_object__from_odb_object(
244 (git_object **)&fc->blob, fc->repo, odb_obj, GIT_OBJ_BLOB);
245 git_odb_object_free(odb_obj);
246 } else {
247 error = git_blob_lookup(
9950bb4e 248 (git_blob **)&fc->blob, fc->repo, &fc->file->id);
114f5a6c
RB
249 }
250
251 if (!error) {
74ded024 252 fc->flags |= GIT_DIFF_FLAG__FREE_BLOB;
114f5a6c
RB
253 fc->map.data = (void *)git_blob_rawcontent(fc->blob);
254 fc->map.len = (size_t)git_blob_rawsize(fc->blob);
255 }
256
257 return error;
258}
259
260static int diff_file_content_load_workdir_symlink(
261 git_diff_file_content *fc, git_buf *path)
262{
263 ssize_t alloc_len, read_len;
264
265 /* link path on disk could be UTF-16, so prepare a buffer that is
266 * big enough to handle some UTF-8 data expansion
267 */
74ded024 268 alloc_len = (ssize_t)(fc->file->size * 2) + 1;
114f5a6c
RB
269
270 fc->map.data = git__calloc(alloc_len, sizeof(char));
271 GITERR_CHECK_ALLOC(fc->map.data);
272
74ded024 273 fc->flags |= GIT_DIFF_FLAG__FREE_DATA;
114f5a6c
RB
274
275 read_len = p_readlink(git_buf_cstr(path), fc->map.data, alloc_len);
276 if (read_len < 0) {
74ded024 277 giterr_set(GITERR_OS, "Failed to read symlink '%s'", fc->file->path);
114f5a6c
RB
278 return -1;
279 }
280
281 fc->map.len = read_len;
282 return 0;
283}
284
285static int diff_file_content_load_workdir_file(
286 git_diff_file_content *fc, git_buf *path)
287{
288 int error = 0;
85d54812 289 git_filter_list *fl = NULL;
114f5a6c 290 git_file fd = git_futils_open_ro(git_buf_cstr(path));
2a7d224f 291 git_buf raw = GIT_BUF_INIT;
114f5a6c
RB
292
293 if (fd < 0)
294 return fd;
295
74ded024
RB
296 if (!fc->file->size &&
297 !(fc->file->size = git_futils_filesize(fd)))
114f5a6c
RB
298 goto cleanup;
299
300 if (diff_file_content_binary_by_size(fc))
301 goto cleanup;
302
85d54812 303 if ((error = git_filter_list_load(
5269008c
RB
304 &fl, fc->repo, NULL, fc->file->path,
305 GIT_FILTER_TO_ODB, GIT_FILTER_OPT_ALLOW_UNSAFE)) < 0)
114f5a6c 306 goto cleanup;
114f5a6c 307
85d54812
RB
308 /* if there are no filters, try to mmap the file */
309 if (fl == NULL) {
114f5a6c 310 if (!(error = git_futils_mmap_ro(
85d54812 311 &fc->map, fd, 0, (size_t)fc->file->size))) {
74ded024 312 fc->flags |= GIT_DIFF_FLAG__UNMAP_DATA;
114f5a6c 313 goto cleanup;
114f5a6c
RB
314 }
315
85d54812
RB
316 /* if mmap failed, fall through to try readbuffer below */
317 giterr_clear();
114f5a6c
RB
318 }
319
2a7d224f 320 if (!(error = git_futils_readbuffer_fd(&raw, fd, (size_t)fc->file->size))) {
a9f51e43 321 git_buf out = GIT_BUF_INIT;
2a7d224f 322
a9f51e43 323 error = git_filter_list_apply_to_data(&out, fl, &raw);
2a7d224f 324
591e8295
RB
325 if (out.ptr != raw.ptr)
326 git_buf_free(&raw);
85d54812 327
2a7d224f
RB
328 if (!error) {
329 fc->map.len = out.size;
330 fc->map.data = out.ptr;
331 fc->flags |= GIT_DIFF_FLAG__FREE_DATA;
332 }
333 }
85d54812 334
114f5a6c 335cleanup:
85d54812 336 git_filter_list_free(fl);
114f5a6c
RB
337 p_close(fd);
338
339 return error;
340}
341
342static int diff_file_content_load_workdir(git_diff_file_content *fc)
343{
344 int error = 0;
345 git_buf path = GIT_BUF_INIT;
346
74ded024 347 if (fc->file->mode == GIT_FILEMODE_COMMIT)
114f5a6c
RB
348 return diff_file_content_commit_to_str(fc, true);
349
74ded024 350 if (fc->file->mode == GIT_FILEMODE_TREE)
114f5a6c
RB
351 return 0;
352
353 if (git_buf_joinpath(
74ded024 354 &path, git_repository_workdir(fc->repo), fc->file->path) < 0)
114f5a6c
RB
355 return -1;
356
74ded024 357 if (S_ISLNK(fc->file->mode))
114f5a6c
RB
358 error = diff_file_content_load_workdir_symlink(fc, &path);
359 else
360 error = diff_file_content_load_workdir_file(fc, &path);
361
362 /* once data is loaded, update OID if we didn't have it previously */
9950bb4e 363 if (!error && (fc->file->flags & GIT_DIFF_FLAG_VALID_ID) == 0) {
114f5a6c 364 error = git_odb_hash(
9950bb4e
CMN
365 &fc->file->id, fc->map.data, fc->map.len, GIT_OBJ_BLOB);
366 fc->file->flags |= GIT_DIFF_FLAG_VALID_ID;
114f5a6c
RB
367 }
368
369 git_buf_free(&path);
370 return error;
371}
372
360f42f4 373int git_diff_file_content__load(git_diff_file_content *fc)
114f5a6c
RB
374{
375 int error = 0;
376
74ded024 377 if ((fc->flags & GIT_DIFF_FLAG__LOADED) != 0)
114f5a6c
RB
378 return 0;
379
74ded024 380 if ((fc->file->flags & GIT_DIFF_FLAG_BINARY) != 0)
114f5a6c
RB
381 return 0;
382
383 if (fc->src == GIT_ITERATOR_TYPE_WORKDIR)
384 error = diff_file_content_load_workdir(fc);
385 else
386 error = diff_file_content_load_blob(fc);
387 if (error)
388 return error;
389
74ded024 390 fc->flags |= GIT_DIFF_FLAG__LOADED;
114f5a6c
RB
391
392 diff_file_content_binary_by_content(fc);
393
394 return 0;
395}
396
360f42f4 397void git_diff_file_content__unload(git_diff_file_content *fc)
114f5a6c 398{
39a1a662
RB
399 if ((fc->flags & GIT_DIFF_FLAG__LOADED) == 0)
400 return;
401
74ded024 402 if (fc->flags & GIT_DIFF_FLAG__FREE_DATA) {
114f5a6c
RB
403 git__free(fc->map.data);
404 fc->map.data = "";
405 fc->map.len = 0;
74ded024 406 fc->flags &= ~GIT_DIFF_FLAG__FREE_DATA;
114f5a6c 407 }
74ded024 408 else if (fc->flags & GIT_DIFF_FLAG__UNMAP_DATA) {
114f5a6c
RB
409 git_futils_mmap_free(&fc->map);
410 fc->map.data = "";
411 fc->map.len = 0;
74ded024 412 fc->flags &= ~GIT_DIFF_FLAG__UNMAP_DATA;
114f5a6c
RB
413 }
414
74ded024 415 if (fc->flags & GIT_DIFF_FLAG__FREE_BLOB) {
114f5a6c
RB
416 git_blob_free((git_blob *)fc->blob);
417 fc->blob = NULL;
74ded024 418 fc->flags &= ~GIT_DIFF_FLAG__FREE_BLOB;
114f5a6c
RB
419 }
420
74ded024 421 fc->flags &= ~GIT_DIFF_FLAG__LOADED;
114f5a6c
RB
422}
423
360f42f4 424void git_diff_file_content__clear(git_diff_file_content *fc)
114f5a6c 425{
360f42f4 426 git_diff_file_content__unload(fc);
114f5a6c
RB
427
428 /* for now, nothing else to do */
429}