static int diff_file_content_load_workdir_file( git_diff_file_content *fc, git_buf *path, git_diff_options *diff_opts) { int error = 0; git_filter_list *fl = NULL; git_file fd = git_futils_open_ro(git_buf_cstr(path)); git_buf raw = GIT_BUF_INIT; if (fd < 0) return fd; if (!fc->file->size && !(fc->file->size = git_futils_filesize(fd))) goto cleanup; if ((diff_opts->flags & GIT_DIFF_SHOW_BINARY) == 0 && diff_file_content_binary_by_size(fc)) goto cleanup; if ((error = git_filter_list_load( &fl, fc->repo, NULL, fc->file->path, GIT_FILTER_TO_ODB, GIT_FILTER_ALLOW_UNSAFE)) < 0) goto cleanup; /* if there are no filters, try to mmap the file */ if (fl == NULL) { if (!(error = git_futils_mmap_ro( &fc->map, fd, 0, (size_t)fc->file->size))) { fc->flags |= GIT_DIFF_FLAG__UNMAP_DATA; goto cleanup; } /* if mmap failed, fall through to try readbuffer below */ giterr_clear(); } if (!(error = git_futils_readbuffer_fd(&raw, fd, (size_t)fc->file->size))) { git_buf out = GIT_BUF_INIT; error = git_filter_list_apply_to_data(&out, fl, &raw); if (out.ptr != raw.ptr) git_buf_dispose(&raw); if (!error) { fc->map.len = out.size; fc->map.data = out.ptr; fc->flags |= GIT_DIFF_FLAG__FREE_DATA; } } cleanup: git_filter_list_free(fl); p_close(fd); return error; }
static int diff_file_content_load_workdir_file( git_diff_file_content *fc, git_buf *path) { int error = 0; git_vector filters = GIT_VECTOR_INIT; git_buf raw = GIT_BUF_INIT, filtered = GIT_BUF_INIT; git_file fd = git_futils_open_ro(git_buf_cstr(path)); if (fd < 0) return fd; if (!fc->file->size && !(fc->file->size = git_futils_filesize(fd))) goto cleanup; if (diff_file_content_binary_by_size(fc)) goto cleanup; if ((error = git_filters_load( &filters, fc->repo, fc->file->path, GIT_FILTER_TO_ODB)) < 0) goto cleanup; /* error >= is a filter count */ if (error == 0) { if (!(error = git_futils_mmap_ro( &fc->map, fd, 0, (size_t)fc->file->size))) fc->flags |= GIT_DIFF_FLAG__UNMAP_DATA; else /* fall through to try readbuffer below */ giterr_clear(); } if (error != 0) { error = git_futils_readbuffer_fd(&raw, fd, (size_t)fc->file->size); if (error < 0) goto cleanup; if (!filters.length) git_buf_swap(&filtered, &raw); else error = git_filters_apply(&filtered, &raw, &filters); if (!error) { fc->map.len = git_buf_len(&filtered); fc->map.data = git_buf_detach(&filtered); fc->flags |= GIT_DIFF_FLAG__FREE_DATA; } git_buf_free(&raw); git_buf_free(&filtered); } cleanup: git_filters_free(&filters); p_close(fd); return error; }
static int diff_file_content_load_blob(git_diff_file_content *fc) { int error = 0; git_odb_object *odb_obj = NULL; if (git_oid_iszero(&fc->file->oid)) return 0; if (fc->file->mode == GIT_FILEMODE_COMMIT) return diff_file_content_commit_to_str(fc, false); /* if we don't know size, try to peek at object header first */ if (!fc->file->size) { git_odb *odb; size_t len; git_otype type; if (!(error = git_repository_odb__weakptr(&odb, fc->repo))) { error = git_odb__read_header_or_object( &odb_obj, &len, &type, odb, &fc->file->oid); git_odb_free(odb); } if (error) return error; fc->file->size = len; } if (diff_file_content_binary_by_size(fc)) return 0; if (odb_obj != NULL) { error = git_object__from_odb_object( (git_object **)&fc->blob, fc->repo, odb_obj, GIT_OBJ_BLOB); git_odb_object_free(odb_obj); } else { error = git_blob_lookup( (git_blob **)&fc->blob, fc->repo, &fc->file->oid); } if (!error) { fc->flags |= GIT_DIFF_FLAG__FREE_BLOB; fc->map.data = (void *)git_blob_rawcontent(fc->blob); fc->map.len = (size_t)git_blob_rawsize(fc->blob); } return error; }
static int diff_file_content_init_common( git_diff_file_content *fc, const git_diff_options *opts) { fc->opts_flags = opts ? opts->flags : GIT_DIFF_NORMAL; if (opts && opts->max_size >= 0) fc->opts_max_size = opts->max_size ? opts->max_size : DIFF_MAX_FILESIZE; if (fc->src == GIT_ITERATOR_TYPE_EMPTY) fc->src = GIT_ITERATOR_TYPE_TREE; if (!fc->driver && git_diff_driver_lookup(&fc->driver, fc->repo, NULL, fc->file->path) < 0) return -1; /* give driver a chance to modify options */ git_diff_driver_update_options(&fc->opts_flags, fc->driver); /* make sure file is conceivable mmap-able */ if ((git_off_t)((size_t)fc->file->size) != fc->file->size) fc->file->flags |= GIT_DIFF_FLAG_BINARY; /* check if user is forcing text diff the file */ else if (fc->opts_flags & GIT_DIFF_FORCE_TEXT) { fc->file->flags &= ~GIT_DIFF_FLAG_BINARY; fc->file->flags |= GIT_DIFF_FLAG_NOT_BINARY; } /* check if user is forcing binary diff the file */ else if (fc->opts_flags & GIT_DIFF_FORCE_BINARY) { fc->file->flags &= ~GIT_DIFF_FLAG_NOT_BINARY; fc->file->flags |= GIT_DIFF_FLAG_BINARY; } diff_file_content_binary_by_size(fc); if ((fc->flags & GIT_DIFF_FLAG__NO_DATA) != 0) { fc->flags |= GIT_DIFF_FLAG__LOADED; fc->map.len = 0; fc->map.data = ""; } if ((fc->flags & GIT_DIFF_FLAG__LOADED) != 0) diff_file_content_binary_by_content(fc); return 0; }
static int diff_file_content_load_blob( git_diff_file_content *fc, git_diff_options *opts) { int error = 0; git_odb_object *odb_obj = NULL; if (git_oid_iszero(&fc->file->id)) return 0; if (fc->file->mode == GIT_FILEMODE_COMMIT) return diff_file_content_commit_to_str(fc, false); /* if we don't know size, try to peek at object header first */ if (!fc->file->size) { if ((error = git_diff_file__resolve_zero_size( fc->file, &odb_obj, fc->repo)) < 0) return error; } if ((opts->flags & GIT_DIFF_SHOW_BINARY) == 0 && diff_file_content_binary_by_size(fc)) return 0; if (odb_obj != NULL) { error = git_object__from_odb_object( (git_object **)&fc->blob, fc->repo, odb_obj, GIT_OBJECT_BLOB); git_odb_object_free(odb_obj); } else { error = git_blob_lookup( (git_blob **)&fc->blob, fc->repo, &fc->file->id); } if (!error) { fc->flags |= GIT_DIFF_FLAG__FREE_BLOB; fc->map.data = (void *)git_blob_rawcontent(fc->blob); fc->map.len = (size_t)git_blob_rawsize(fc->blob); } return error; }