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