]> git.proxmox.com Git - libgit2.git/blame - src/diff_file.c
object: fix a brace
[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
RB
114 break;
115 case GIT_DELTA_MODIFIED:
116 case GIT_DELTA_COPIED:
117 case GIT_DELTA_RENAMED:
118 break;
119 default:
120 has_data = false;
121 break;
122 }
123
124 if (!has_data)
74ded024 125 fc->flags |= GIT_DIFF_FLAG__NO_DATA;
114f5a6c 126
5dc98298 127 return diff_file_content_init_common(fc, &diff->opts);
114f5a6c
RB
128}
129
6789b7a7 130int git_diff_file_content__init_from_src(
114f5a6c
RB
131 git_diff_file_content *fc,
132 git_repository *repo,
133 const git_diff_options *opts,
6789b7a7 134 const git_diff_file_content_src *src,
74ded024 135 git_diff_file *as_file)
114f5a6c
RB
136{
137 memset(fc, 0, sizeof(*fc));
138 fc->repo = repo;
74ded024 139 fc->file = as_file;
6789b7a7 140 fc->blob = src->blob;
114f5a6c 141
6789b7a7 142 if (!src->blob && !src->buf) {
74ded024 143 fc->flags |= GIT_DIFF_FLAG__NO_DATA;
114f5a6c 144 } else {
74ded024 145 fc->flags |= GIT_DIFF_FLAG__LOADED;
9950bb4e 146 fc->file->flags |= GIT_DIFF_FLAG_VALID_ID;
74ded024 147 fc->file->mode = GIT_FILEMODE_BLOB;
114f5a6c 148
6789b7a7
RB
149 if (src->blob) {
150 fc->file->size = git_blob_rawsize(src->blob);
151 git_oid_cpy(&fc->file->id, git_blob_id(src->blob));
114f5a6c 152
6789b7a7
RB
153 fc->map.len = (size_t)fc->file->size;
154 fc->map.data = (char *)git_blob_rawcontent(src->blob);
155 } else {
156 fc->file->size = src->buflen;
157 git_odb_hash(&fc->file->id, src->buf, src->buflen, GIT_OBJ_BLOB);
114f5a6c 158
6789b7a7
RB
159 fc->map.len = src->buflen;
160 fc->map.data = (char *)src->buf;
161 }
114f5a6c
RB
162 }
163
5dc98298 164 return diff_file_content_init_common(fc, opts);
114f5a6c
RB
165}
166
167static int diff_file_content_commit_to_str(
168 git_diff_file_content *fc, bool check_status)
169{
170 char oid[GIT_OID_HEXSZ+1];
171 git_buf content = GIT_BUF_INIT;
172 const char *status = "";
173
174 if (check_status) {
175 int error = 0;
176 git_submodule *sm = NULL;
177 unsigned int sm_status = 0;
178 const git_oid *sm_head;
179
a15c7802 180 if ((error = git_submodule_lookup(&sm, fc->repo, fc->file->path)) < 0) {
114f5a6c 181 /* GIT_EEXISTS means a "submodule" that has not been git added */
a15c7802
RB
182 if (error == GIT_EEXISTS) {
183 giterr_clear();
114f5a6c 184 error = 0;
a15c7802
RB
185 }
186 return error;
187 }
188
189 if ((error = git_submodule_status(&sm_status, sm)) < 0) {
190 git_submodule_free(sm);
114f5a6c
RB
191 return error;
192 }
193
194 /* update OID if we didn't have it previously */
9950bb4e 195 if ((fc->file->flags & GIT_DIFF_FLAG_VALID_ID) == 0 &&
114f5a6c
RB
196 ((sm_head = git_submodule_wd_id(sm)) != NULL ||
197 (sm_head = git_submodule_head_id(sm)) != NULL))
198 {
9950bb4e
CMN
199 git_oid_cpy(&fc->file->id, sm_head);
200 fc->file->flags |= GIT_DIFF_FLAG_VALID_ID;
114f5a6c
RB
201 }
202
203 if (GIT_SUBMODULE_STATUS_IS_WD_DIRTY(sm_status))
204 status = "-dirty";
a15c7802
RB
205
206 git_submodule_free(sm);
114f5a6c
RB
207 }
208
9950bb4e 209 git_oid_tostr(oid, sizeof(oid), &fc->file->id);
114f5a6c
RB
210 if (git_buf_printf(&content, "Subproject commit %s%s\n", oid, status) < 0)
211 return -1;
212
213 fc->map.len = git_buf_len(&content);
214 fc->map.data = git_buf_detach(&content);
74ded024 215 fc->flags |= GIT_DIFF_FLAG__FREE_DATA;
114f5a6c
RB
216
217 return 0;
218}
219
220static int diff_file_content_load_blob(git_diff_file_content *fc)
221{
222 int error = 0;
223 git_odb_object *odb_obj = NULL;
224
9950bb4e 225 if (git_oid_iszero(&fc->file->id))
114f5a6c
RB
226 return 0;
227
74ded024 228 if (fc->file->mode == GIT_FILEMODE_COMMIT)
114f5a6c
RB
229 return diff_file_content_commit_to_str(fc, false);
230
231 /* if we don't know size, try to peek at object header first */
74ded024 232 if (!fc->file->size) {
effdbeb3
RB
233 if ((error = git_diff_file__resolve_zero_size(
234 fc->file, &odb_obj, fc->repo)) < 0)
114f5a6c 235 return error;
114f5a6c
RB
236 }
237
238 if (diff_file_content_binary_by_size(fc))
239 return 0;
240
241 if (odb_obj != NULL) {
242 error = git_object__from_odb_object(
243 (git_object **)&fc->blob, fc->repo, odb_obj, GIT_OBJ_BLOB);
244 git_odb_object_free(odb_obj);
245 } else {
246 error = git_blob_lookup(
9950bb4e 247 (git_blob **)&fc->blob, fc->repo, &fc->file->id);
114f5a6c
RB
248 }
249
250 if (!error) {
74ded024 251 fc->flags |= GIT_DIFF_FLAG__FREE_BLOB;
114f5a6c
RB
252 fc->map.data = (void *)git_blob_rawcontent(fc->blob);
253 fc->map.len = (size_t)git_blob_rawsize(fc->blob);
254 }
255
256 return error;
257}
258
259static int diff_file_content_load_workdir_symlink(
260 git_diff_file_content *fc, git_buf *path)
261{
262 ssize_t alloc_len, read_len;
263
264 /* link path on disk could be UTF-16, so prepare a buffer that is
265 * big enough to handle some UTF-8 data expansion
266 */
74ded024 267 alloc_len = (ssize_t)(fc->file->size * 2) + 1;
114f5a6c
RB
268
269 fc->map.data = git__calloc(alloc_len, sizeof(char));
270 GITERR_CHECK_ALLOC(fc->map.data);
271
74ded024 272 fc->flags |= GIT_DIFF_FLAG__FREE_DATA;
114f5a6c
RB
273
274 read_len = p_readlink(git_buf_cstr(path), fc->map.data, alloc_len);
275 if (read_len < 0) {
74ded024 276 giterr_set(GITERR_OS, "Failed to read symlink '%s'", fc->file->path);
114f5a6c
RB
277 return -1;
278 }
279
280 fc->map.len = read_len;
281 return 0;
282}
283
284static int diff_file_content_load_workdir_file(
285 git_diff_file_content *fc, git_buf *path)
286{
287 int error = 0;
85d54812 288 git_filter_list *fl = NULL;
114f5a6c 289 git_file fd = git_futils_open_ro(git_buf_cstr(path));
2a7d224f 290 git_buf raw = GIT_BUF_INIT;
114f5a6c
RB
291
292 if (fd < 0)
293 return fd;
294
74ded024
RB
295 if (!fc->file->size &&
296 !(fc->file->size = git_futils_filesize(fd)))
114f5a6c
RB
297 goto cleanup;
298
299 if (diff_file_content_binary_by_size(fc))
300 goto cleanup;
301
85d54812 302 if ((error = git_filter_list_load(
4b11f25a 303 &fl, fc->repo, NULL, fc->file->path, GIT_FILTER_TO_ODB)) < 0)
114f5a6c 304 goto cleanup;
114f5a6c 305
85d54812
RB
306 /* if there are no filters, try to mmap the file */
307 if (fl == NULL) {
114f5a6c 308 if (!(error = git_futils_mmap_ro(
85d54812 309 &fc->map, fd, 0, (size_t)fc->file->size))) {
74ded024 310 fc->flags |= GIT_DIFF_FLAG__UNMAP_DATA;
114f5a6c 311 goto cleanup;
114f5a6c
RB
312 }
313
85d54812
RB
314 /* if mmap failed, fall through to try readbuffer below */
315 giterr_clear();
114f5a6c
RB
316 }
317
2a7d224f 318 if (!(error = git_futils_readbuffer_fd(&raw, fd, (size_t)fc->file->size))) {
a9f51e43 319 git_buf out = GIT_BUF_INIT;
2a7d224f 320
a9f51e43 321 error = git_filter_list_apply_to_data(&out, fl, &raw);
2a7d224f 322
591e8295
RB
323 if (out.ptr != raw.ptr)
324 git_buf_free(&raw);
85d54812 325
2a7d224f
RB
326 if (!error) {
327 fc->map.len = out.size;
328 fc->map.data = out.ptr;
329 fc->flags |= GIT_DIFF_FLAG__FREE_DATA;
330 }
331 }
85d54812 332
114f5a6c 333cleanup:
85d54812 334 git_filter_list_free(fl);
114f5a6c
RB
335 p_close(fd);
336
337 return error;
338}
339
340static int diff_file_content_load_workdir(git_diff_file_content *fc)
341{
342 int error = 0;
343 git_buf path = GIT_BUF_INIT;
344
74ded024 345 if (fc->file->mode == GIT_FILEMODE_COMMIT)
114f5a6c
RB
346 return diff_file_content_commit_to_str(fc, true);
347
74ded024 348 if (fc->file->mode == GIT_FILEMODE_TREE)
114f5a6c
RB
349 return 0;
350
351 if (git_buf_joinpath(
74ded024 352 &path, git_repository_workdir(fc->repo), fc->file->path) < 0)
114f5a6c
RB
353 return -1;
354
74ded024 355 if (S_ISLNK(fc->file->mode))
114f5a6c
RB
356 error = diff_file_content_load_workdir_symlink(fc, &path);
357 else
358 error = diff_file_content_load_workdir_file(fc, &path);
359
360 /* once data is loaded, update OID if we didn't have it previously */
9950bb4e 361 if (!error && (fc->file->flags & GIT_DIFF_FLAG_VALID_ID) == 0) {
114f5a6c 362 error = git_odb_hash(
9950bb4e
CMN
363 &fc->file->id, fc->map.data, fc->map.len, GIT_OBJ_BLOB);
364 fc->file->flags |= GIT_DIFF_FLAG_VALID_ID;
114f5a6c
RB
365 }
366
367 git_buf_free(&path);
368 return error;
369}
370
360f42f4 371int git_diff_file_content__load(git_diff_file_content *fc)
114f5a6c
RB
372{
373 int error = 0;
374
74ded024 375 if ((fc->flags & GIT_DIFF_FLAG__LOADED) != 0)
114f5a6c
RB
376 return 0;
377
74ded024 378 if ((fc->file->flags & GIT_DIFF_FLAG_BINARY) != 0)
114f5a6c
RB
379 return 0;
380
381 if (fc->src == GIT_ITERATOR_TYPE_WORKDIR)
382 error = diff_file_content_load_workdir(fc);
383 else
384 error = diff_file_content_load_blob(fc);
385 if (error)
386 return error;
387
74ded024 388 fc->flags |= GIT_DIFF_FLAG__LOADED;
114f5a6c
RB
389
390 diff_file_content_binary_by_content(fc);
391
392 return 0;
393}
394
360f42f4 395void git_diff_file_content__unload(git_diff_file_content *fc)
114f5a6c 396{
39a1a662
RB
397 if ((fc->flags & GIT_DIFF_FLAG__LOADED) == 0)
398 return;
399
74ded024 400 if (fc->flags & GIT_DIFF_FLAG__FREE_DATA) {
114f5a6c
RB
401 git__free(fc->map.data);
402 fc->map.data = "";
403 fc->map.len = 0;
74ded024 404 fc->flags &= ~GIT_DIFF_FLAG__FREE_DATA;
114f5a6c 405 }
74ded024 406 else if (fc->flags & GIT_DIFF_FLAG__UNMAP_DATA) {
114f5a6c
RB
407 git_futils_mmap_free(&fc->map);
408 fc->map.data = "";
409 fc->map.len = 0;
74ded024 410 fc->flags &= ~GIT_DIFF_FLAG__UNMAP_DATA;
114f5a6c
RB
411 }
412
74ded024 413 if (fc->flags & GIT_DIFF_FLAG__FREE_BLOB) {
114f5a6c
RB
414 git_blob_free((git_blob *)fc->blob);
415 fc->blob = NULL;
74ded024 416 fc->flags &= ~GIT_DIFF_FLAG__FREE_BLOB;
114f5a6c
RB
417 }
418
74ded024 419 fc->flags &= ~GIT_DIFF_FLAG__LOADED;
114f5a6c
RB
420}
421
360f42f4 422void git_diff_file_content__clear(git_diff_file_content *fc)
114f5a6c 423{
360f42f4 424 git_diff_file_content__unload(fc);
114f5a6c
RB
425
426 /* for now, nothing else to do */
427}