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
|
|
|
|
|
|
|
int error; |
164
|
1
|
50
|
|
|
|
|
if ((error = git_odb_hash(&fc->file->id, src->buf, src->buflen, GIT_OBJECT_BLOB)) < 0) |
165
|
0
|
|
|
|
|
|
return error; |
166
|
1
|
|
|
|
|
|
fc->file->size = src->buflen; |
167
|
1
|
|
|
|
|
|
fc->file->id_abbrev = GIT_OID_HEXSZ; |
168
|
|
|
|
|
|
|
|
169
|
1
|
|
|
|
|
|
fc->map.len = src->buflen; |
170
|
1
|
|
|
|
|
|
fc->map.data = (char *)src->buf; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
2
|
|
|
|
|
|
return diff_file_content_init_common(fc, opts); |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
0
|
|
|
|
|
|
static int diff_file_content_commit_to_str( |
178
|
|
|
|
|
|
|
git_diff_file_content *fc, bool check_status) |
179
|
|
|
|
|
|
|
{ |
180
|
|
|
|
|
|
|
char oid[GIT_OID_HEXSZ+1]; |
181
|
0
|
|
|
|
|
|
git_str content = GIT_STR_INIT; |
182
|
0
|
|
|
|
|
|
const char *status = ""; |
183
|
|
|
|
|
|
|
|
184
|
0
|
0
|
|
|
|
|
if (check_status) { |
185
|
0
|
|
|
|
|
|
int error = 0; |
186
|
0
|
|
|
|
|
|
git_submodule *sm = NULL; |
187
|
0
|
|
|
|
|
|
unsigned int sm_status = 0; |
188
|
|
|
|
|
|
|
const git_oid *sm_head; |
189
|
|
|
|
|
|
|
|
190
|
0
|
0
|
|
|
|
|
if ((error = git_submodule_lookup(&sm, fc->repo, fc->file->path)) < 0) { |
191
|
|
|
|
|
|
|
/* GIT_EEXISTS means a "submodule" that has not been git added */ |
192
|
0
|
0
|
|
|
|
|
if (error == GIT_EEXISTS) { |
193
|
0
|
|
|
|
|
|
git_error_clear(); |
194
|
0
|
|
|
|
|
|
error = 0; |
195
|
|
|
|
|
|
|
} |
196
|
0
|
|
|
|
|
|
return error; |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
0
|
0
|
|
|
|
|
if ((error = git_submodule_status(&sm_status, fc->repo, fc->file->path, GIT_SUBMODULE_IGNORE_UNSPECIFIED)) < 0) { |
200
|
0
|
|
|
|
|
|
git_submodule_free(sm); |
201
|
0
|
|
|
|
|
|
return error; |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
/* update OID if we didn't have it previously */ |
205
|
0
|
0
|
|
|
|
|
if ((fc->file->flags & GIT_DIFF_FLAG_VALID_ID) == 0 && |
|
|
0
|
|
|
|
|
|
206
|
0
|
0
|
|
|
|
|
((sm_head = git_submodule_wd_id(sm)) != NULL || |
207
|
0
|
|
|
|
|
|
(sm_head = git_submodule_head_id(sm)) != NULL)) |
208
|
|
|
|
|
|
|
{ |
209
|
0
|
|
|
|
|
|
git_oid_cpy(&fc->file->id, sm_head); |
210
|
0
|
|
|
|
|
|
fc->file->flags |= GIT_DIFF_FLAG_VALID_ID; |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
|
213
|
0
|
0
|
|
|
|
|
if (GIT_SUBMODULE_STATUS_IS_WD_DIRTY(sm_status)) |
214
|
0
|
|
|
|
|
|
status = "-dirty"; |
215
|
|
|
|
|
|
|
|
216
|
0
|
|
|
|
|
|
git_submodule_free(sm); |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
0
|
|
|
|
|
|
git_oid_tostr(oid, sizeof(oid), &fc->file->id); |
220
|
0
|
0
|
|
|
|
|
if (git_str_printf(&content, "Subproject commit %s%s\n", oid, status) < 0) |
221
|
0
|
|
|
|
|
|
return -1; |
222
|
|
|
|
|
|
|
|
223
|
0
|
|
|
|
|
|
fc->map.len = git_str_len(&content); |
224
|
0
|
|
|
|
|
|
fc->map.data = git_str_detach(&content); |
225
|
0
|
|
|
|
|
|
fc->flags |= GIT_DIFF_FLAG__FREE_DATA; |
226
|
|
|
|
|
|
|
|
227
|
0
|
|
|
|
|
|
return 0; |
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
|
230
|
47
|
|
|
|
|
|
static int diff_file_content_load_blob( |
231
|
|
|
|
|
|
|
git_diff_file_content *fc, |
232
|
|
|
|
|
|
|
git_diff_options *opts) |
233
|
|
|
|
|
|
|
{ |
234
|
47
|
|
|
|
|
|
int error = 0; |
235
|
47
|
|
|
|
|
|
git_odb_object *odb_obj = NULL; |
236
|
|
|
|
|
|
|
|
237
|
47
|
50
|
|
|
|
|
if (git_oid_is_zero(&fc->file->id)) |
238
|
0
|
|
|
|
|
|
return 0; |
239
|
|
|
|
|
|
|
|
240
|
47
|
50
|
|
|
|
|
if (fc->file->mode == GIT_FILEMODE_COMMIT) |
241
|
0
|
|
|
|
|
|
return diff_file_content_commit_to_str(fc, false); |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
/* if we don't know size, try to peek at object header first */ |
244
|
47
|
100
|
|
|
|
|
if (!fc->file->size) { |
245
|
13
|
50
|
|
|
|
|
if ((error = git_diff_file__resolve_zero_size( |
246
|
|
|
|
|
|
|
fc->file, &odb_obj, fc->repo)) < 0) |
247
|
0
|
|
|
|
|
|
return error; |
248
|
|
|
|
|
|
|
} |
249
|
|
|
|
|
|
|
|
250
|
92
|
|
|
|
|
|
if ((opts->flags & GIT_DIFF_SHOW_BINARY) == 0 && |
251
|
45
|
|
|
|
|
|
diff_file_content_binary_by_size(fc)) |
252
|
0
|
|
|
|
|
|
return 0; |
253
|
|
|
|
|
|
|
|
254
|
47
|
50
|
|
|
|
|
if (odb_obj != NULL) { |
255
|
0
|
|
|
|
|
|
error = git_object__from_odb_object( |
256
|
0
|
|
|
|
|
|
(git_object **)&fc->blob, fc->repo, odb_obj, GIT_OBJECT_BLOB); |
257
|
0
|
|
|
|
|
|
git_odb_object_free(odb_obj); |
258
|
|
|
|
|
|
|
} else { |
259
|
47
|
|
|
|
|
|
error = git_blob_lookup( |
260
|
94
|
|
|
|
|
|
(git_blob **)&fc->blob, fc->repo, &fc->file->id); |
261
|
|
|
|
|
|
|
} |
262
|
|
|
|
|
|
|
|
263
|
47
|
50
|
|
|
|
|
if (!error) { |
264
|
47
|
|
|
|
|
|
fc->flags |= GIT_DIFF_FLAG__FREE_BLOB; |
265
|
47
|
|
|
|
|
|
fc->map.data = (void *)git_blob_rawcontent(fc->blob); |
266
|
47
|
|
|
|
|
|
fc->map.len = (size_t)git_blob_rawsize(fc->blob); |
267
|
|
|
|
|
|
|
} |
268
|
|
|
|
|
|
|
|
269
|
47
|
|
|
|
|
|
return error; |
270
|
|
|
|
|
|
|
} |
271
|
|
|
|
|
|
|
|
272
|
0
|
|
|
|
|
|
static int diff_file_content_load_workdir_symlink_fake( |
273
|
|
|
|
|
|
|
git_diff_file_content *fc, git_str *path) |
274
|
|
|
|
|
|
|
{ |
275
|
0
|
|
|
|
|
|
git_str target = GIT_STR_INIT; |
276
|
|
|
|
|
|
|
int error; |
277
|
|
|
|
|
|
|
|
278
|
0
|
0
|
|
|
|
|
if ((error = git_futils_readbuffer(&target, path->ptr)) < 0) |
279
|
0
|
|
|
|
|
|
return error; |
280
|
|
|
|
|
|
|
|
281
|
0
|
|
|
|
|
|
fc->map.len = git_str_len(&target); |
282
|
0
|
|
|
|
|
|
fc->map.data = git_str_detach(&target); |
283
|
0
|
|
|
|
|
|
fc->flags |= GIT_DIFF_FLAG__FREE_DATA; |
284
|
|
|
|
|
|
|
|
285
|
0
|
|
|
|
|
|
git_str_dispose(&target); |
286
|
0
|
|
|
|
|
|
return error; |
287
|
|
|
|
|
|
|
} |
288
|
|
|
|
|
|
|
|
289
|
0
|
|
|
|
|
|
static int diff_file_content_load_workdir_symlink( |
290
|
|
|
|
|
|
|
git_diff_file_content *fc, git_str *path) |
291
|
|
|
|
|
|
|
{ |
292
|
|
|
|
|
|
|
ssize_t alloc_len, read_len; |
293
|
|
|
|
|
|
|
int symlink_supported, error; |
294
|
|
|
|
|
|
|
|
295
|
0
|
0
|
|
|
|
|
if ((error = git_repository__configmap_lookup( |
296
|
|
|
|
|
|
|
&symlink_supported, fc->repo, GIT_CONFIGMAP_SYMLINKS)) < 0) |
297
|
0
|
|
|
|
|
|
return -1; |
298
|
|
|
|
|
|
|
|
299
|
0
|
0
|
|
|
|
|
if (!symlink_supported) |
300
|
0
|
|
|
|
|
|
return diff_file_content_load_workdir_symlink_fake(fc, path); |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
/* link path on disk could be UTF-16, so prepare a buffer that is |
303
|
|
|
|
|
|
|
* big enough to handle some UTF-8 data expansion |
304
|
|
|
|
|
|
|
*/ |
305
|
0
|
|
|
|
|
|
alloc_len = (ssize_t)(fc->file->size * 2) + 1; |
306
|
|
|
|
|
|
|
|
307
|
0
|
|
|
|
|
|
fc->map.data = git__calloc(alloc_len, sizeof(char)); |
308
|
0
|
0
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(fc->map.data); |
309
|
|
|
|
|
|
|
|
310
|
0
|
|
|
|
|
|
fc->flags |= GIT_DIFF_FLAG__FREE_DATA; |
311
|
|
|
|
|
|
|
|
312
|
0
|
|
|
|
|
|
read_len = p_readlink(git_str_cstr(path), fc->map.data, alloc_len); |
313
|
0
|
0
|
|
|
|
|
if (read_len < 0) { |
314
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "failed to read symlink '%s'", fc->file->path); |
315
|
0
|
|
|
|
|
|
return -1; |
316
|
|
|
|
|
|
|
} |
317
|
|
|
|
|
|
|
|
318
|
0
|
|
|
|
|
|
fc->map.len = read_len; |
319
|
0
|
|
|
|
|
|
return 0; |
320
|
|
|
|
|
|
|
} |
321
|
|
|
|
|
|
|
|
322
|
4
|
|
|
|
|
|
static int diff_file_content_load_workdir_file( |
323
|
|
|
|
|
|
|
git_diff_file_content *fc, |
324
|
|
|
|
|
|
|
git_str *path, |
325
|
|
|
|
|
|
|
git_diff_options *diff_opts) |
326
|
|
|
|
|
|
|
{ |
327
|
4
|
|
|
|
|
|
int error = 0; |
328
|
4
|
|
|
|
|
|
git_filter_list *fl = NULL; |
329
|
4
|
|
|
|
|
|
git_file fd = git_futils_open_ro(git_str_cstr(path)); |
330
|
4
|
|
|
|
|
|
git_str raw = GIT_STR_INIT; |
331
|
4
|
|
|
|
|
|
git_object_size_t new_file_size = 0; |
332
|
|
|
|
|
|
|
|
333
|
4
|
50
|
|
|
|
|
if (fd < 0) |
334
|
0
|
|
|
|
|
|
return fd; |
335
|
|
|
|
|
|
|
|
336
|
4
|
|
|
|
|
|
error = git_futils_filesize(&new_file_size, fd); |
337
|
|
|
|
|
|
|
|
338
|
4
|
50
|
|
|
|
|
if (error < 0) |
339
|
0
|
|
|
|
|
|
goto cleanup; |
340
|
|
|
|
|
|
|
|
341
|
4
|
50
|
|
|
|
|
if (!(fc->file->flags & GIT_DIFF_FLAG_VALID_SIZE)) { |
342
|
0
|
|
|
|
|
|
fc->file->size = new_file_size; |
343
|
0
|
|
|
|
|
|
fc->file->flags |= GIT_DIFF_FLAG_VALID_SIZE; |
344
|
4
|
50
|
|
|
|
|
} else if (fc->file->size != new_file_size) { |
345
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_FILESYSTEM, "file changed before we could read it"); |
346
|
0
|
|
|
|
|
|
error = -1; |
347
|
0
|
|
|
|
|
|
goto cleanup; |
348
|
|
|
|
|
|
|
} |
349
|
|
|
|
|
|
|
|
350
|
8
|
|
|
|
|
|
if ((diff_opts->flags & GIT_DIFF_SHOW_BINARY) == 0 && |
351
|
4
|
|
|
|
|
|
diff_file_content_binary_by_size(fc)) |
352
|
0
|
|
|
|
|
|
goto cleanup; |
353
|
|
|
|
|
|
|
|
354
|
4
|
50
|
|
|
|
|
if ((error = git_filter_list_load( |
355
|
4
|
|
|
|
|
|
&fl, fc->repo, NULL, fc->file->path, |
356
|
|
|
|
|
|
|
GIT_FILTER_TO_ODB, GIT_FILTER_ALLOW_UNSAFE)) < 0) |
357
|
0
|
|
|
|
|
|
goto cleanup; |
358
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
/* if there are no filters, try to mmap the file */ |
360
|
4
|
50
|
|
|
|
|
if (fl == NULL) { |
361
|
4
|
50
|
|
|
|
|
if (!(error = git_futils_mmap_ro( |
362
|
4
|
|
|
|
|
|
&fc->map, fd, 0, (size_t)fc->file->size))) { |
363
|
4
|
|
|
|
|
|
fc->flags |= GIT_DIFF_FLAG__UNMAP_DATA; |
364
|
4
|
|
|
|
|
|
goto cleanup; |
365
|
|
|
|
|
|
|
} |
366
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
/* if mmap failed, fall through to try readbuffer below */ |
368
|
0
|
|
|
|
|
|
git_error_clear(); |
369
|
|
|
|
|
|
|
} |
370
|
|
|
|
|
|
|
|
371
|
0
|
0
|
|
|
|
|
if (!(error = git_futils_readbuffer_fd(&raw, fd, (size_t)fc->file->size))) { |
372
|
0
|
|
|
|
|
|
git_str out = GIT_STR_INIT; |
373
|
|
|
|
|
|
|
|
374
|
0
|
|
|
|
|
|
error = git_filter_list__convert_buf(&out, fl, &raw); |
375
|
|
|
|
|
|
|
|
376
|
0
|
0
|
|
|
|
|
if (!error) { |
377
|
0
|
|
|
|
|
|
fc->map.len = out.size; |
378
|
0
|
|
|
|
|
|
fc->map.data = out.ptr; |
379
|
0
|
|
|
|
|
|
fc->flags |= GIT_DIFF_FLAG__FREE_DATA; |
380
|
|
|
|
|
|
|
} |
381
|
|
|
|
|
|
|
} |
382
|
|
|
|
|
|
|
|
383
|
|
|
|
|
|
|
cleanup: |
384
|
4
|
|
|
|
|
|
git_filter_list_free(fl); |
385
|
4
|
|
|
|
|
|
p_close(fd); |
386
|
|
|
|
|
|
|
|
387
|
4
|
|
|
|
|
|
return error; |
388
|
|
|
|
|
|
|
} |
389
|
|
|
|
|
|
|
|
390
|
4
|
|
|
|
|
|
static int diff_file_content_load_workdir( |
391
|
|
|
|
|
|
|
git_diff_file_content *fc, |
392
|
|
|
|
|
|
|
git_diff_options *diff_opts) |
393
|
|
|
|
|
|
|
{ |
394
|
4
|
|
|
|
|
|
int error = 0; |
395
|
4
|
|
|
|
|
|
git_str path = GIT_STR_INIT; |
396
|
|
|
|
|
|
|
|
397
|
4
|
50
|
|
|
|
|
if (fc->file->mode == GIT_FILEMODE_COMMIT) |
398
|
0
|
|
|
|
|
|
return diff_file_content_commit_to_str(fc, true); |
399
|
|
|
|
|
|
|
|
400
|
4
|
50
|
|
|
|
|
if (fc->file->mode == GIT_FILEMODE_TREE) |
401
|
0
|
|
|
|
|
|
return 0; |
402
|
|
|
|
|
|
|
|
403
|
4
|
50
|
|
|
|
|
if (git_repository_workdir_path(&path, fc->repo, fc->file->path) < 0) |
404
|
0
|
|
|
|
|
|
return -1; |
405
|
|
|
|
|
|
|
|
406
|
4
|
50
|
|
|
|
|
if (S_ISLNK(fc->file->mode)) |
407
|
0
|
|
|
|
|
|
error = diff_file_content_load_workdir_symlink(fc, &path); |
408
|
|
|
|
|
|
|
else |
409
|
4
|
|
|
|
|
|
error = diff_file_content_load_workdir_file(fc, &path, diff_opts); |
410
|
|
|
|
|
|
|
|
411
|
|
|
|
|
|
|
/* once data is loaded, update OID if we didn't have it previously */ |
412
|
4
|
50
|
|
|
|
|
if (!error && (fc->file->flags & GIT_DIFF_FLAG_VALID_ID) == 0) { |
|
|
50
|
|
|
|
|
|
413
|
4
|
|
|
|
|
|
error = git_odb_hash( |
414
|
4
|
|
|
|
|
|
&fc->file->id, fc->map.data, fc->map.len, GIT_OBJECT_BLOB); |
415
|
4
|
|
|
|
|
|
fc->file->flags |= GIT_DIFF_FLAG_VALID_ID; |
416
|
|
|
|
|
|
|
} |
417
|
|
|
|
|
|
|
|
418
|
4
|
|
|
|
|
|
git_str_dispose(&path); |
419
|
4
|
|
|
|
|
|
return error; |
420
|
|
|
|
|
|
|
} |
421
|
|
|
|
|
|
|
|
422
|
100
|
|
|
|
|
|
int git_diff_file_content__load( |
423
|
|
|
|
|
|
|
git_diff_file_content *fc, |
424
|
|
|
|
|
|
|
git_diff_options *diff_opts) |
425
|
|
|
|
|
|
|
{ |
426
|
100
|
|
|
|
|
|
int error = 0; |
427
|
|
|
|
|
|
|
|
428
|
100
|
100
|
|
|
|
|
if ((fc->flags & GIT_DIFF_FLAG__LOADED) != 0) |
429
|
49
|
|
|
|
|
|
return 0; |
430
|
|
|
|
|
|
|
|
431
|
51
|
100
|
|
|
|
|
if ((fc->file->flags & GIT_DIFF_FLAG_BINARY) != 0 && |
|
|
50
|
|
|
|
|
|
432
|
2
|
|
|
|
|
|
(diff_opts->flags & GIT_DIFF_SHOW_BINARY) == 0) |
433
|
0
|
|
|
|
|
|
return 0; |
434
|
|
|
|
|
|
|
|
435
|
51
|
100
|
|
|
|
|
if (fc->src == GIT_ITERATOR_WORKDIR) |
436
|
4
|
|
|
|
|
|
error = diff_file_content_load_workdir(fc, diff_opts); |
437
|
|
|
|
|
|
|
else |
438
|
47
|
|
|
|
|
|
error = diff_file_content_load_blob(fc, diff_opts); |
439
|
51
|
50
|
|
|
|
|
if (error) |
440
|
0
|
|
|
|
|
|
return error; |
441
|
|
|
|
|
|
|
|
442
|
51
|
|
|
|
|
|
fc->flags |= GIT_DIFF_FLAG__LOADED; |
443
|
|
|
|
|
|
|
|
444
|
51
|
|
|
|
|
|
diff_file_content_binary_by_content(fc); |
445
|
|
|
|
|
|
|
|
446
|
51
|
|
|
|
|
|
return 0; |
447
|
|
|
|
|
|
|
} |
448
|
|
|
|
|
|
|
|
449
|
100
|
|
|
|
|
|
void git_diff_file_content__unload(git_diff_file_content *fc) |
450
|
|
|
|
|
|
|
{ |
451
|
100
|
50
|
|
|
|
|
if ((fc->flags & GIT_DIFF_FLAG__LOADED) == 0) |
452
|
0
|
|
|
|
|
|
return; |
453
|
|
|
|
|
|
|
|
454
|
100
|
50
|
|
|
|
|
if (fc->flags & GIT_DIFF_FLAG__FREE_DATA) { |
455
|
0
|
|
|
|
|
|
git__free(fc->map.data); |
456
|
0
|
|
|
|
|
|
fc->map.data = ""; |
457
|
0
|
|
|
|
|
|
fc->map.len = 0; |
458
|
0
|
|
|
|
|
|
fc->flags &= ~GIT_DIFF_FLAG__FREE_DATA; |
459
|
|
|
|
|
|
|
} |
460
|
100
|
100
|
|
|
|
|
else if (fc->flags & GIT_DIFF_FLAG__UNMAP_DATA) { |
461
|
4
|
|
|
|
|
|
git_futils_mmap_free(&fc->map); |
462
|
4
|
|
|
|
|
|
fc->map.data = ""; |
463
|
4
|
|
|
|
|
|
fc->map.len = 0; |
464
|
4
|
|
|
|
|
|
fc->flags &= ~GIT_DIFF_FLAG__UNMAP_DATA; |
465
|
|
|
|
|
|
|
} |
466
|
|
|
|
|
|
|
|
467
|
100
|
100
|
|
|
|
|
if (fc->flags & GIT_DIFF_FLAG__FREE_BLOB) { |
468
|
48
|
|
|
|
|
|
git_blob_free((git_blob *)fc->blob); |
469
|
48
|
|
|
|
|
|
fc->blob = NULL; |
470
|
48
|
|
|
|
|
|
fc->flags &= ~GIT_DIFF_FLAG__FREE_BLOB; |
471
|
|
|
|
|
|
|
} |
472
|
|
|
|
|
|
|
|
473
|
100
|
|
|
|
|
|
fc->flags &= ~GIT_DIFF_FLAG__LOADED; |
474
|
|
|
|
|
|
|
} |
475
|
|
|
|
|
|
|
|
476
|
100
|
|
|
|
|
|
void git_diff_file_content__clear(git_diff_file_content *fc) |
477
|
|
|
|
|
|
|
{ |
478
|
100
|
|
|
|
|
|
git_diff_file_content__unload(fc); |
479
|
|
|
|
|
|
|
|
480
|
|
|
|
|
|
|
/* for now, nothing else to do */ |
481
|
100
|
|
|
|
|
|
} |