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_buf content = GIT_BUF_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_buf_printf(&content, "Subproject commit %s%s\n", oid, status) < 0) |
221
|
0
|
|
|
|
|
|
return -1; |
222
|
|
|
|
|
|
|
|
223
|
0
|
|
|
|
|
|
fc->map.len = git_buf_len(&content); |
224
|
0
|
|
|
|
|
|
fc->map.data = git_buf_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_buf *path) |
274
|
|
|
|
|
|
|
{ |
275
|
0
|
|
|
|
|
|
git_buf target = GIT_BUF_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_buf_len(&target); |
282
|
0
|
|
|
|
|
|
fc->map.data = git_buf_detach(&target); |
283
|
0
|
|
|
|
|
|
fc->flags |= GIT_DIFF_FLAG__FREE_DATA; |
284
|
|
|
|
|
|
|
|
285
|
0
|
|
|
|
|
|
git_buf_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_buf *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_buf_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_buf *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_buf_cstr(path)); |
330
|
4
|
|
|
|
|
|
git_buf raw = GIT_BUF_INIT; |
331
|
|
|
|
|
|
|
|
332
|
4
|
50
|
|
|
|
|
if (fd < 0) |
333
|
0
|
|
|
|
|
|
return fd; |
334
|
|
|
|
|
|
|
|
335
|
4
|
50
|
|
|
|
|
if (!fc->file->size) |
336
|
0
|
|
|
|
|
|
error = git_futils_filesize(&fc->file->size, fd); |
337
|
|
|
|
|
|
|
|
338
|
4
|
50
|
|
|
|
|
if (error < 0 || !fc->file->size) |
|
|
50
|
|
|
|
|
|
339
|
|
|
|
|
|
|
goto cleanup; |
340
|
|
|
|
|
|
|
|
341
|
8
|
|
|
|
|
|
if ((diff_opts->flags & GIT_DIFF_SHOW_BINARY) == 0 && |
342
|
4
|
|
|
|
|
|
diff_file_content_binary_by_size(fc)) |
343
|
0
|
|
|
|
|
|
goto cleanup; |
344
|
|
|
|
|
|
|
|
345
|
4
|
50
|
|
|
|
|
if ((error = git_filter_list_load( |
346
|
4
|
|
|
|
|
|
&fl, fc->repo, NULL, fc->file->path, |
347
|
|
|
|
|
|
|
GIT_FILTER_TO_ODB, GIT_FILTER_ALLOW_UNSAFE)) < 0) |
348
|
0
|
|
|
|
|
|
goto cleanup; |
349
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
/* if there are no filters, try to mmap the file */ |
351
|
4
|
50
|
|
|
|
|
if (fl == NULL) { |
352
|
4
|
50
|
|
|
|
|
if (!(error = git_futils_mmap_ro( |
353
|
4
|
|
|
|
|
|
&fc->map, fd, 0, (size_t)fc->file->size))) { |
354
|
4
|
|
|
|
|
|
fc->flags |= GIT_DIFF_FLAG__UNMAP_DATA; |
355
|
4
|
|
|
|
|
|
goto cleanup; |
356
|
|
|
|
|
|
|
} |
357
|
|
|
|
|
|
|
|
358
|
|
|
|
|
|
|
/* if mmap failed, fall through to try readbuffer below */ |
359
|
0
|
|
|
|
|
|
git_error_clear(); |
360
|
|
|
|
|
|
|
} |
361
|
|
|
|
|
|
|
|
362
|
0
|
0
|
|
|
|
|
if (!(error = git_futils_readbuffer_fd(&raw, fd, (size_t)fc->file->size))) { |
363
|
0
|
|
|
|
|
|
git_buf out = GIT_BUF_INIT; |
364
|
|
|
|
|
|
|
|
365
|
0
|
|
|
|
|
|
error = git_filter_list_apply_to_data(&out, fl, &raw); |
366
|
|
|
|
|
|
|
|
367
|
0
|
0
|
|
|
|
|
if (out.ptr != raw.ptr) |
368
|
0
|
|
|
|
|
|
git_buf_dispose(&raw); |
369
|
|
|
|
|
|
|
|
370
|
0
|
0
|
|
|
|
|
if (!error) { |
371
|
0
|
|
|
|
|
|
fc->map.len = out.size; |
372
|
0
|
|
|
|
|
|
fc->map.data = out.ptr; |
373
|
0
|
|
|
|
|
|
fc->flags |= GIT_DIFF_FLAG__FREE_DATA; |
374
|
|
|
|
|
|
|
} |
375
|
|
|
|
|
|
|
} |
376
|
|
|
|
|
|
|
|
377
|
|
|
|
|
|
|
cleanup: |
378
|
4
|
|
|
|
|
|
git_filter_list_free(fl); |
379
|
4
|
|
|
|
|
|
p_close(fd); |
380
|
|
|
|
|
|
|
|
381
|
4
|
|
|
|
|
|
return error; |
382
|
|
|
|
|
|
|
} |
383
|
|
|
|
|
|
|
|
384
|
4
|
|
|
|
|
|
static int diff_file_content_load_workdir( |
385
|
|
|
|
|
|
|
git_diff_file_content *fc, |
386
|
|
|
|
|
|
|
git_diff_options *diff_opts) |
387
|
|
|
|
|
|
|
{ |
388
|
4
|
|
|
|
|
|
int error = 0; |
389
|
4
|
|
|
|
|
|
git_buf path = GIT_BUF_INIT; |
390
|
|
|
|
|
|
|
|
391
|
4
|
50
|
|
|
|
|
if (fc->file->mode == GIT_FILEMODE_COMMIT) |
392
|
0
|
|
|
|
|
|
return diff_file_content_commit_to_str(fc, true); |
393
|
|
|
|
|
|
|
|
394
|
4
|
50
|
|
|
|
|
if (fc->file->mode == GIT_FILEMODE_TREE) |
395
|
0
|
|
|
|
|
|
return 0; |
396
|
|
|
|
|
|
|
|
397
|
4
|
50
|
|
|
|
|
if (git_buf_joinpath( |
398
|
8
|
|
|
|
|
|
&path, git_repository_workdir(fc->repo), fc->file->path) < 0) |
399
|
0
|
|
|
|
|
|
return -1; |
400
|
|
|
|
|
|
|
|
401
|
4
|
50
|
|
|
|
|
if (S_ISLNK(fc->file->mode)) |
402
|
0
|
|
|
|
|
|
error = diff_file_content_load_workdir_symlink(fc, &path); |
403
|
|
|
|
|
|
|
else |
404
|
4
|
|
|
|
|
|
error = diff_file_content_load_workdir_file(fc, &path, diff_opts); |
405
|
|
|
|
|
|
|
|
406
|
|
|
|
|
|
|
/* once data is loaded, update OID if we didn't have it previously */ |
407
|
4
|
50
|
|
|
|
|
if (!error && (fc->file->flags & GIT_DIFF_FLAG_VALID_ID) == 0) { |
|
|
50
|
|
|
|
|
|
408
|
4
|
|
|
|
|
|
error = git_odb_hash( |
409
|
4
|
|
|
|
|
|
&fc->file->id, fc->map.data, fc->map.len, GIT_OBJECT_BLOB); |
410
|
4
|
|
|
|
|
|
fc->file->flags |= GIT_DIFF_FLAG_VALID_ID; |
411
|
|
|
|
|
|
|
} |
412
|
|
|
|
|
|
|
|
413
|
4
|
|
|
|
|
|
git_buf_dispose(&path); |
414
|
4
|
|
|
|
|
|
return error; |
415
|
|
|
|
|
|
|
} |
416
|
|
|
|
|
|
|
|
417
|
100
|
|
|
|
|
|
int git_diff_file_content__load( |
418
|
|
|
|
|
|
|
git_diff_file_content *fc, |
419
|
|
|
|
|
|
|
git_diff_options *diff_opts) |
420
|
|
|
|
|
|
|
{ |
421
|
100
|
|
|
|
|
|
int error = 0; |
422
|
|
|
|
|
|
|
|
423
|
100
|
100
|
|
|
|
|
if ((fc->flags & GIT_DIFF_FLAG__LOADED) != 0) |
424
|
49
|
|
|
|
|
|
return 0; |
425
|
|
|
|
|
|
|
|
426
|
51
|
100
|
|
|
|
|
if ((fc->file->flags & GIT_DIFF_FLAG_BINARY) != 0 && |
|
|
50
|
|
|
|
|
|
427
|
2
|
|
|
|
|
|
(diff_opts->flags & GIT_DIFF_SHOW_BINARY) == 0) |
428
|
0
|
|
|
|
|
|
return 0; |
429
|
|
|
|
|
|
|
|
430
|
51
|
100
|
|
|
|
|
if (fc->src == GIT_ITERATOR_WORKDIR) |
431
|
4
|
|
|
|
|
|
error = diff_file_content_load_workdir(fc, diff_opts); |
432
|
|
|
|
|
|
|
else |
433
|
47
|
|
|
|
|
|
error = diff_file_content_load_blob(fc, diff_opts); |
434
|
51
|
50
|
|
|
|
|
if (error) |
435
|
0
|
|
|
|
|
|
return error; |
436
|
|
|
|
|
|
|
|
437
|
51
|
|
|
|
|
|
fc->flags |= GIT_DIFF_FLAG__LOADED; |
438
|
|
|
|
|
|
|
|
439
|
51
|
|
|
|
|
|
diff_file_content_binary_by_content(fc); |
440
|
|
|
|
|
|
|
|
441
|
51
|
|
|
|
|
|
return 0; |
442
|
|
|
|
|
|
|
} |
443
|
|
|
|
|
|
|
|
444
|
100
|
|
|
|
|
|
void git_diff_file_content__unload(git_diff_file_content *fc) |
445
|
|
|
|
|
|
|
{ |
446
|
100
|
50
|
|
|
|
|
if ((fc->flags & GIT_DIFF_FLAG__LOADED) == 0) |
447
|
0
|
|
|
|
|
|
return; |
448
|
|
|
|
|
|
|
|
449
|
100
|
50
|
|
|
|
|
if (fc->flags & GIT_DIFF_FLAG__FREE_DATA) { |
450
|
0
|
|
|
|
|
|
git__free(fc->map.data); |
451
|
0
|
|
|
|
|
|
fc->map.data = ""; |
452
|
0
|
|
|
|
|
|
fc->map.len = 0; |
453
|
0
|
|
|
|
|
|
fc->flags &= ~GIT_DIFF_FLAG__FREE_DATA; |
454
|
|
|
|
|
|
|
} |
455
|
100
|
100
|
|
|
|
|
else if (fc->flags & GIT_DIFF_FLAG__UNMAP_DATA) { |
456
|
4
|
|
|
|
|
|
git_futils_mmap_free(&fc->map); |
457
|
4
|
|
|
|
|
|
fc->map.data = ""; |
458
|
4
|
|
|
|
|
|
fc->map.len = 0; |
459
|
4
|
|
|
|
|
|
fc->flags &= ~GIT_DIFF_FLAG__UNMAP_DATA; |
460
|
|
|
|
|
|
|
} |
461
|
|
|
|
|
|
|
|
462
|
100
|
100
|
|
|
|
|
if (fc->flags & GIT_DIFF_FLAG__FREE_BLOB) { |
463
|
48
|
|
|
|
|
|
git_blob_free((git_blob *)fc->blob); |
464
|
48
|
|
|
|
|
|
fc->blob = NULL; |
465
|
48
|
|
|
|
|
|
fc->flags &= ~GIT_DIFF_FLAG__FREE_BLOB; |
466
|
|
|
|
|
|
|
} |
467
|
|
|
|
|
|
|
|
468
|
100
|
|
|
|
|
|
fc->flags &= ~GIT_DIFF_FLAG__LOADED; |
469
|
|
|
|
|
|
|
} |
470
|
|
|
|
|
|
|
|
471
|
100
|
|
|
|
|
|
void git_diff_file_content__clear(git_diff_file_content *fc) |
472
|
|
|
|
|
|
|
{ |
473
|
100
|
|
|
|
|
|
git_diff_file_content__unload(fc); |
474
|
|
|
|
|
|
|
|
475
|
|
|
|
|
|
|
/* for now, nothing else to do */ |
476
|
100
|
|
|
|
|
|
} |