]>
Commit | Line | Data |
---|---|---|
ceab4e26 BS |
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 "blame.h" | |
eae0bfdc | 9 | |
ceab4e26 BS |
10 | #include "git2/commit.h" |
11 | #include "git2/revparse.h" | |
12 | #include "git2/revwalk.h" | |
13 | #include "git2/tree.h" | |
14 | #include "git2/diff.h" | |
15 | #include "git2/blob.h" | |
c1ca2b67 | 16 | #include "git2/signature.h" |
ac3d33df | 17 | #include "git2/mailmap.h" |
ceab4e26 BS |
18 | #include "util.h" |
19 | #include "repository.h" | |
20 | #include "blame_git.h" | |
21 | ||
22 | ||
f0c9d8ba | 23 | static int hunk_byfinalline_search_cmp(const void *key, const void *entry) |
ceab4e26 | 24 | { |
f0c9d8ba BS |
25 | git_blame_hunk *hunk = (git_blame_hunk*)entry; |
26 | ||
79aa0302 | 27 | size_t lineno = *(size_t*)key; |
cb1cb24c PS |
28 | size_t lines_in_hunk = hunk->lines_in_hunk; |
29 | size_t final_start_line_number = hunk->final_start_line_number; | |
79aa0302 JP |
30 | |
31 | if (lineno < final_start_line_number) | |
ceab4e26 | 32 | return -1; |
79aa0302 | 33 | if (lineno >= final_start_line_number + lines_in_hunk) |
ceab4e26 BS |
34 | return 1; |
35 | return 0; | |
36 | } | |
f0c9d8ba BS |
37 | |
38 | static int paths_cmp(const void *a, const void *b) { return git__strcmp((char*)a, (char*)b); } | |
39 | static int hunk_cmp(const void *_a, const void *_b) | |
ceab4e26 BS |
40 | { |
41 | git_blame_hunk *a = (git_blame_hunk*)_a, | |
42 | *b = (git_blame_hunk*)_b; | |
43 | ||
ac3d33df JK |
44 | if (a->final_start_line_number > b->final_start_line_number) |
45 | return 1; | |
46 | else if (a->final_start_line_number < b->final_start_line_number) | |
47 | return -1; | |
48 | else | |
49 | return 0; | |
ceab4e26 BS |
50 | } |
51 | ||
52 | static bool hunk_ends_at_or_before_line(git_blame_hunk *hunk, size_t line) | |
53 | { | |
cb1cb24c | 54 | return line >= (hunk->final_start_line_number + hunk->lines_in_hunk - 1); |
ceab4e26 BS |
55 | } |
56 | ||
57 | static bool hunk_starts_at_or_after_line(git_blame_hunk *hunk, size_t line) | |
58 | { | |
59 | return line <= hunk->final_start_line_number; | |
60 | } | |
61 | ||
c25aa7cd | 62 | static git_blame_hunk *new_hunk( |
cb1cb24c PS |
63 | size_t start, |
64 | size_t lines, | |
65 | size_t orig_start, | |
f0c9d8ba | 66 | const char *path) |
ceab4e26 BS |
67 | { |
68 | git_blame_hunk *hunk = git__calloc(1, sizeof(git_blame_hunk)); | |
69 | if (!hunk) return NULL; | |
70 | ||
71 | hunk->lines_in_hunk = lines; | |
72 | hunk->final_start_line_number = start; | |
73 | hunk->orig_start_line_number = orig_start; | |
74 | hunk->orig_path = path ? git__strdup(path) : NULL; | |
75 | ||
76 | return hunk; | |
77 | } | |
78 | ||
c25aa7cd PP |
79 | static void free_hunk(git_blame_hunk *hunk) |
80 | { | |
81 | git__free((void*)hunk->orig_path); | |
82 | git_signature_free(hunk->final_signature); | |
83 | git_signature_free(hunk->orig_signature); | |
84 | git__free(hunk); | |
85 | } | |
86 | ||
87 | static git_blame_hunk *dup_hunk(git_blame_hunk *hunk) | |
ceab4e26 | 88 | { |
f0c9d8ba BS |
89 | git_blame_hunk *newhunk = new_hunk( |
90 | hunk->final_start_line_number, | |
91 | hunk->lines_in_hunk, | |
92 | hunk->orig_start_line_number, | |
93 | hunk->orig_path); | |
392702ee ET |
94 | |
95 | if (!newhunk) | |
96 | return NULL; | |
97 | ||
ceab4e26 BS |
98 | git_oid_cpy(&newhunk->orig_commit_id, &hunk->orig_commit_id); |
99 | git_oid_cpy(&newhunk->final_commit_id, &hunk->final_commit_id); | |
089297b2 | 100 | newhunk->boundary = hunk->boundary; |
ceab4e26 | 101 | |
c25aa7cd PP |
102 | if (git_signature_dup(&newhunk->final_signature, hunk->final_signature) < 0 || |
103 | git_signature_dup(&newhunk->orig_signature, hunk->orig_signature) < 0) { | |
104 | free_hunk(newhunk); | |
105 | return NULL; | |
106 | } | |
107 | ||
108 | return newhunk; | |
ceab4e26 BS |
109 | } |
110 | ||
111 | /* Starting with the hunk that includes start_line, shift all following hunks' | |
112 | * final_start_line by shift_by lines */ | |
f0c9d8ba | 113 | static void shift_hunks_by(git_vector *v, size_t start_line, int shift_by) |
ceab4e26 BS |
114 | { |
115 | size_t i; | |
116 | ||
79aa0302 | 117 | if (!git_vector_bsearch2(&i, v, hunk_byfinalline_search_cmp, &start_line)) { |
ceab4e26 BS |
118 | for (; i < v->length; i++) { |
119 | git_blame_hunk *hunk = (git_blame_hunk*)v->contents[i]; | |
120 | hunk->final_start_line_number += shift_by; | |
121 | } | |
122 | } | |
123 | } | |
f0c9d8ba | 124 | |
c25aa7cd | 125 | git_blame *git_blame__alloc( |
ceab4e26 BS |
126 | git_repository *repo, |
127 | git_blame_options opts, | |
128 | const char *path) | |
129 | { | |
96869a4e RB |
130 | git_blame *gbr = git__calloc(1, sizeof(git_blame)); |
131 | if (!gbr) | |
ceab4e26 | 132 | return NULL; |
96869a4e | 133 | |
ceab4e26 BS |
134 | gbr->repository = repo; |
135 | gbr->options = opts; | |
96869a4e RB |
136 | |
137 | if (git_vector_init(&gbr->hunks, 8, hunk_cmp) < 0 || | |
138 | git_vector_init(&gbr->paths, 8, paths_cmp) < 0 || | |
139 | (gbr->path = git__strdup(path)) == NULL || | |
140 | git_vector_insert(&gbr->paths, git__strdup(path)) < 0) | |
141 | { | |
142 | git_blame_free(gbr); | |
96869a4e RB |
143 | return NULL; |
144 | } | |
145 | ||
ac3d33df JK |
146 | if (opts.flags & GIT_BLAME_USE_MAILMAP && |
147 | git_mailmap_from_repository(&gbr->mailmap, repo) < 0) { | |
148 | git_blame_free(gbr); | |
149 | return NULL; | |
150 | } | |
151 | ||
ceab4e26 BS |
152 | return gbr; |
153 | } | |
154 | ||
155 | void git_blame_free(git_blame *blame) | |
156 | { | |
157 | size_t i; | |
158 | git_blame_hunk *hunk; | |
ceab4e26 BS |
159 | |
160 | if (!blame) return; | |
161 | ||
162 | git_vector_foreach(&blame->hunks, i, hunk) | |
163 | free_hunk(hunk); | |
164 | git_vector_free(&blame->hunks); | |
165 | ||
9cfce273 | 166 | git_vector_free_deep(&blame->paths); |
ceab4e26 | 167 | |
f0c9d8ba BS |
168 | git_array_clear(blame->line_index); |
169 | ||
ac3d33df JK |
170 | git_mailmap_free(blame->mailmap); |
171 | ||
96869a4e | 172 | git__free(blame->path); |
ceab4e26 BS |
173 | git_blob_free(blame->final_blob); |
174 | git__free(blame); | |
175 | } | |
176 | ||
177 | uint32_t git_blame_get_hunk_count(git_blame *blame) | |
178 | { | |
c25aa7cd | 179 | GIT_ASSERT_ARG(blame); |
aad5403f | 180 | return (uint32_t)blame->hunks.length; |
ceab4e26 BS |
181 | } |
182 | ||
183 | const git_blame_hunk *git_blame_get_hunk_byindex(git_blame *blame, uint32_t index) | |
184 | { | |
c25aa7cd | 185 | GIT_ASSERT_ARG_WITH_RETVAL(blame, NULL); |
ceab4e26 BS |
186 | return (git_blame_hunk*)git_vector_get(&blame->hunks, index); |
187 | } | |
188 | ||
cb1cb24c | 189 | const git_blame_hunk *git_blame_get_hunk_byline(git_blame *blame, size_t lineno) |
ceab4e26 | 190 | { |
cb1cb24c | 191 | size_t i, new_lineno = lineno; |
c25aa7cd PP |
192 | |
193 | GIT_ASSERT_ARG_WITH_RETVAL(blame, NULL); | |
ceab4e26 | 194 | |
79aa0302 | 195 | if (!git_vector_bsearch2(&i, &blame->hunks, hunk_byfinalline_search_cmp, &new_lineno)) { |
aad5403f | 196 | return git_blame_get_hunk_byindex(blame, (uint32_t)i); |
ceab4e26 BS |
197 | } |
198 | ||
199 | return NULL; | |
200 | } | |
201 | ||
f17ed637 | 202 | static int normalize_options( |
ceab4e26 BS |
203 | git_blame_options *out, |
204 | const git_blame_options *in, | |
205 | git_repository *repo) | |
206 | { | |
207 | git_blame_options dummy = GIT_BLAME_OPTIONS_INIT; | |
208 | if (!in) in = &dummy; | |
209 | ||
210 | memcpy(out, in, sizeof(git_blame_options)); | |
211 | ||
212 | /* No newest_commit => HEAD */ | |
22a2d3d5 | 213 | if (git_oid_is_zero(&out->newest_commit)) { |
f17ed637 PS |
214 | if (git_reference_name_to_id(&out->newest_commit, repo, "HEAD") < 0) { |
215 | return -1; | |
216 | } | |
ceab4e26 | 217 | } |
d1228f1c BS |
218 | |
219 | /* min_line 0 really means 1 */ | |
220 | if (!out->min_line) out->min_line = 1; | |
221 | /* max_line 0 really means N, but we don't know N yet */ | |
364d800b BS |
222 | |
223 | /* Fix up option implications */ | |
224 | if (out->flags & GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES) | |
225 | out->flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES; | |
226 | if (out->flags & GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES) | |
227 | out->flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES; | |
228 | if (out->flags & GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES) | |
229 | out->flags |= GIT_BLAME_TRACK_COPIES_SAME_FILE; | |
f17ed637 PS |
230 | |
231 | return 0; | |
ceab4e26 BS |
232 | } |
233 | ||
234 | static git_blame_hunk *split_hunk_in_vector( | |
235 | git_vector *vec, | |
236 | git_blame_hunk *hunk, | |
237 | size_t rel_line, | |
238 | bool return_new) | |
239 | { | |
240 | size_t new_line_count; | |
241 | git_blame_hunk *nh; | |
242 | ||
243 | /* Don't split if already at a boundary */ | |
244 | if (rel_line <= 0 || | |
245 | rel_line >= hunk->lines_in_hunk) | |
246 | { | |
247 | return hunk; | |
248 | } | |
249 | ||
250 | new_line_count = hunk->lines_in_hunk - rel_line; | |
cb1cb24c PS |
251 | nh = new_hunk(hunk->final_start_line_number + rel_line, new_line_count, |
252 | hunk->orig_start_line_number + rel_line, hunk->orig_path); | |
392702ee ET |
253 | |
254 | if (!nh) | |
255 | return NULL; | |
256 | ||
ceab4e26 BS |
257 | git_oid_cpy(&nh->final_commit_id, &hunk->final_commit_id); |
258 | git_oid_cpy(&nh->orig_commit_id, &hunk->orig_commit_id); | |
259 | ||
260 | /* Adjust hunk that was split */ | |
cb1cb24c | 261 | hunk->lines_in_hunk -= new_line_count; |
ceab4e26 BS |
262 | git_vector_insert_sorted(vec, nh, NULL); |
263 | { | |
264 | git_blame_hunk *ret = return_new ? nh : hunk; | |
265 | return ret; | |
266 | } | |
267 | } | |
268 | ||
ceab4e26 | 269 | /* |
f0c9d8ba BS |
270 | * Construct a list of char indices for where lines begin |
271 | * Adapted from core git: | |
272 | * https://github.com/gitster/git/blob/be5c9fb9049ed470e7005f159bb923a5f4de1309/builtin/blame.c#L1760-L1789 | |
ceab4e26 | 273 | */ |
f0c9d8ba | 274 | static int index_blob_lines(git_blame *blame) |
ceab4e26 | 275 | { |
f0c9d8ba | 276 | const char *buf = blame->final_buf; |
22a2d3d5 | 277 | size_t len = blame->final_buf_size; |
f0c9d8ba BS |
278 | int num = 0, incomplete = 0, bol = 1; |
279 | size_t *i; | |
a06474f8 | 280 | |
f0c9d8ba BS |
281 | if (len && buf[len-1] != '\n') |
282 | incomplete++; /* incomplete line at the end */ | |
283 | while (len--) { | |
284 | if (bol) { | |
285 | i = git_array_alloc(blame->line_index); | |
ac3d33df | 286 | GIT_ERROR_CHECK_ALLOC(i); |
f0c9d8ba BS |
287 | *i = buf - blame->final_buf; |
288 | bol = 0; | |
289 | } | |
290 | if (*buf++ == '\n') { | |
291 | num++; | |
292 | bol = 1; | |
293 | } | |
294 | } | |
295 | i = git_array_alloc(blame->line_index); | |
ac3d33df | 296 | GIT_ERROR_CHECK_ALLOC(i); |
f0c9d8ba BS |
297 | *i = buf - blame->final_buf; |
298 | blame->num_lines = num + incomplete; | |
299 | return blame->num_lines; | |
ceab4e26 | 300 | } |
a06474f8 | 301 | |
c25aa7cd | 302 | static git_blame_hunk *hunk_from_entry(git_blame__entry *e, git_blame *blame) |
ceab4e26 BS |
303 | { |
304 | git_blame_hunk *h = new_hunk( | |
305 | e->lno+1, e->num_lines, e->s_lno+1, e->suspect->path); | |
392702ee ET |
306 | |
307 | if (!h) | |
308 | return NULL; | |
309 | ||
ceab4e26 | 310 | git_oid_cpy(&h->final_commit_id, git_commit_id(e->suspect->commit)); |
a06474f8 | 311 | git_oid_cpy(&h->orig_commit_id, git_commit_id(e->suspect->commit)); |
ac3d33df JK |
312 | git_commit_author_with_mailmap( |
313 | &h->final_signature, e->suspect->commit, blame->mailmap); | |
314 | git_signature_dup(&h->orig_signature, h->final_signature); | |
25c47aae | 315 | h->boundary = e->is_boundary ? 1 : 0; |
ceab4e26 BS |
316 | return h; |
317 | } | |
318 | ||
77db6ff5 BS |
319 | static int load_blob(git_blame *blame) |
320 | { | |
321 | int error; | |
322 | ||
b6f60a4d BS |
323 | if (blame->final_blob) return 0; |
324 | ||
77db6ff5 BS |
325 | error = git_commit_lookup(&blame->final, blame->repository, &blame->options.newest_commit); |
326 | if (error < 0) | |
327 | goto cleanup; | |
328 | error = git_object_lookup_bypath((git_object**)&blame->final_blob, | |
ac3d33df | 329 | (git_object*)blame->final, blame->path, GIT_OBJECT_BLOB); |
77db6ff5 BS |
330 | |
331 | cleanup: | |
332 | return error; | |
333 | } | |
334 | ||
f0c9d8ba | 335 | static int blame_internal(git_blame *blame) |
ceab4e26 BS |
336 | { |
337 | int error; | |
a121e580 | 338 | git_blame__entry *ent = NULL; |
a121e580 | 339 | git_blame__origin *o; |
ceab4e26 | 340 | |
b6f60a4d BS |
341 | if ((error = load_blob(blame)) < 0 || |
342 | (error = git_blame__get_origin(&o, blame, blame->final, blame->path)) < 0) | |
ceab4e26 | 343 | goto cleanup; |
22a2d3d5 UG |
344 | |
345 | if (git_blob_rawsize(blame->final_blob) > SIZE_MAX) { | |
346 | git_error_set(GIT_ERROR_NOMEMORY, "blob is too large to blame"); | |
347 | error = -1; | |
348 | goto cleanup; | |
349 | } | |
350 | ||
b6f60a4d | 351 | blame->final_buf = git_blob_rawcontent(blame->final_blob); |
22a2d3d5 | 352 | blame->final_buf_size = (size_t)git_blob_rawsize(blame->final_blob); |
ceab4e26 | 353 | |
f0c9d8ba | 354 | ent = git__calloc(1, sizeof(git_blame__entry)); |
ac3d33df | 355 | GIT_ERROR_CHECK_ALLOC(ent); |
392702ee | 356 | |
f0c9d8ba | 357 | ent->num_lines = index_blob_lines(blame); |
d1228f1c BS |
358 | ent->lno = blame->options.min_line - 1; |
359 | ent->num_lines = ent->num_lines - blame->options.min_line + 1; | |
f0c9d8ba | 360 | if (blame->options.max_line > 0) |
d1228f1c | 361 | ent->num_lines = blame->options.max_line - blame->options.min_line + 1; |
d1228f1c | 362 | ent->s_lno = ent->lno; |
ceab4e26 | 363 | ent->suspect = o; |
d1228f1c | 364 | |
0a0f0558 | 365 | blame->ent = ent; |
ceab4e26 | 366 | |
ae195a71 | 367 | error = git_blame__like_git(blame, blame->options.flags); |
ceab4e26 | 368 | |
ceab4e26 | 369 | cleanup: |
0a0f0558 | 370 | for (ent = blame->ent; ent; ) { |
a121e580 | 371 | git_blame__entry *e = ent->next; |
ac3d33df | 372 | git_blame_hunk *h = hunk_from_entry(ent, blame); |
ceab4e26 | 373 | |
392702ee | 374 | git_vector_insert(&blame->hunks, h); |
ceab4e26 | 375 | |
b6f60a4d | 376 | git_blame__free_entry(ent); |
ceab4e26 BS |
377 | ent = e; |
378 | } | |
379 | ||
ceab4e26 BS |
380 | return error; |
381 | } | |
382 | ||
f0c9d8ba BS |
383 | /******************************************************************************* |
384 | * File blaming | |
385 | ******************************************************************************/ | |
386 | ||
ceab4e26 BS |
387 | int git_blame_file( |
388 | git_blame **out, | |
389 | git_repository *repo, | |
390 | const char *path, | |
391 | git_blame_options *options) | |
392 | { | |
393 | int error = -1; | |
394 | git_blame_options normOptions = GIT_BLAME_OPTIONS_INIT; | |
395 | git_blame *blame = NULL; | |
396 | ||
c25aa7cd PP |
397 | GIT_ASSERT_ARG(out); |
398 | GIT_ASSERT_ARG(repo); | |
399 | GIT_ASSERT_ARG(path); | |
400 | ||
f17ed637 PS |
401 | if ((error = normalize_options(&normOptions, options, repo)) < 0) |
402 | goto on_error; | |
ceab4e26 BS |
403 | |
404 | blame = git_blame__alloc(repo, normOptions, path); | |
ac3d33df | 405 | GIT_ERROR_CHECK_ALLOC(blame); |
ceab4e26 | 406 | |
77db6ff5 | 407 | if ((error = load_blob(blame)) < 0) |
ceab4e26 BS |
408 | goto on_error; |
409 | ||
f0c9d8ba | 410 | if ((error = blame_internal(blame)) < 0) |
ceab4e26 BS |
411 | goto on_error; |
412 | ||
413 | *out = blame; | |
414 | return 0; | |
415 | ||
416 | on_error: | |
417 | git_blame_free(blame); | |
418 | return error; | |
419 | } | |
420 | ||
421 | /******************************************************************************* | |
422 | * Buffer blaming | |
423 | *******************************************************************************/ | |
424 | ||
425 | static bool hunk_is_bufferblame(git_blame_hunk *hunk) | |
426 | { | |
22a2d3d5 | 427 | return hunk && git_oid_is_zero(&hunk->final_commit_id); |
ceab4e26 BS |
428 | } |
429 | ||
430 | static int buffer_hunk_cb( | |
431 | const git_diff_delta *delta, | |
7dcb1c45 | 432 | const git_diff_hunk *hunk, |
ceab4e26 BS |
433 | void *payload) |
434 | { | |
435 | git_blame *blame = (git_blame*)payload; | |
aad5403f | 436 | uint32_t wedge_line; |
ceab4e26 BS |
437 | |
438 | GIT_UNUSED(delta); | |
ceab4e26 | 439 | |
7dcb1c45 | 440 | wedge_line = (hunk->old_lines == 0) ? hunk->new_start : hunk->old_start; |
ceab4e26 BS |
441 | blame->current_diff_line = wedge_line; |
442 | ||
ceab4e26 | 443 | blame->current_hunk = (git_blame_hunk*)git_blame_get_hunk_byline(blame, wedge_line); |
9db56cc4 BS |
444 | if (!blame->current_hunk) { |
445 | /* Line added at the end of the file */ | |
446 | blame->current_hunk = new_hunk(wedge_line, 0, wedge_line, blame->path); | |
ac3d33df | 447 | GIT_ERROR_CHECK_ALLOC(blame->current_hunk); |
392702ee | 448 | |
9db56cc4 BS |
449 | git_vector_insert(&blame->hunks, blame->current_hunk); |
450 | } else if (!hunk_starts_at_or_after_line(blame->current_hunk, wedge_line)){ | |
451 | /* If this hunk doesn't start between existing hunks, split a hunk up so it does */ | |
ceab4e26 BS |
452 | blame->current_hunk = split_hunk_in_vector(&blame->hunks, blame->current_hunk, |
453 | wedge_line - blame->current_hunk->orig_start_line_number, true); | |
ac3d33df | 454 | GIT_ERROR_CHECK_ALLOC(blame->current_hunk); |
ceab4e26 BS |
455 | } |
456 | ||
457 | return 0; | |
458 | } | |
459 | ||
460 | static int ptrs_equal_cmp(const void *a, const void *b) { return a<b ? -1 : a>b ? 1 : 0; } | |
461 | static int buffer_line_cb( | |
462 | const git_diff_delta *delta, | |
7dcb1c45 BS |
463 | const git_diff_hunk *hunk, |
464 | const git_diff_line *line, | |
ceab4e26 BS |
465 | void *payload) |
466 | { | |
467 | git_blame *blame = (git_blame*)payload; | |
468 | ||
469 | GIT_UNUSED(delta); | |
7dcb1c45 BS |
470 | GIT_UNUSED(hunk); |
471 | GIT_UNUSED(line); | |
ceab4e26 | 472 | |
7dcb1c45 | 473 | if (line->origin == GIT_DIFF_LINE_ADDITION) { |
ceab4e26 BS |
474 | if (hunk_is_bufferblame(blame->current_hunk) && |
475 | hunk_ends_at_or_before_line(blame->current_hunk, blame->current_diff_line)) { | |
476 | /* Append to the current buffer-blame hunk */ | |
477 | blame->current_hunk->lines_in_hunk++; | |
f0c9d8ba | 478 | shift_hunks_by(&blame->hunks, blame->current_diff_line+1, 1); |
ceab4e26 BS |
479 | } else { |
480 | /* Create a new buffer-blame hunk with this line */ | |
f0c9d8ba | 481 | shift_hunks_by(&blame->hunks, blame->current_diff_line, 1); |
cb1cb24c | 482 | blame->current_hunk = new_hunk(blame->current_diff_line, 1, 0, blame->path); |
ac3d33df | 483 | GIT_ERROR_CHECK_ALLOC(blame->current_hunk); |
392702ee | 484 | |
ceab4e26 BS |
485 | git_vector_insert_sorted(&blame->hunks, blame->current_hunk, NULL); |
486 | } | |
487 | blame->current_diff_line++; | |
488 | } | |
489 | ||
7dcb1c45 | 490 | if (line->origin == GIT_DIFF_LINE_DELETION) { |
ceab4e26 BS |
491 | /* Trim the line from the current hunk; remove it if it's now empty */ |
492 | size_t shift_base = blame->current_diff_line + blame->current_hunk->lines_in_hunk+1; | |
493 | ||
494 | if (--(blame->current_hunk->lines_in_hunk) == 0) { | |
495 | size_t i; | |
496 | shift_base--; | |
497 | if (!git_vector_search2(&i, &blame->hunks, ptrs_equal_cmp, blame->current_hunk)) { | |
498 | git_vector_remove(&blame->hunks, i); | |
499 | free_hunk(blame->current_hunk); | |
aad5403f | 500 | blame->current_hunk = (git_blame_hunk*)git_blame_get_hunk_byindex(blame, (uint32_t)i); |
ceab4e26 BS |
501 | } |
502 | } | |
f0c9d8ba | 503 | shift_hunks_by(&blame->hunks, shift_base, -1); |
ceab4e26 BS |
504 | } |
505 | return 0; | |
506 | } | |
507 | ||
508 | int git_blame_buffer( | |
509 | git_blame **out, | |
510 | git_blame *reference, | |
511 | const char *buffer, | |
e9d5e5f3 | 512 | size_t buffer_len) |
ceab4e26 BS |
513 | { |
514 | git_blame *blame; | |
515 | git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; | |
516 | size_t i; | |
517 | git_blame_hunk *hunk; | |
518 | ||
519 | diffopts.context_lines = 0; | |
520 | ||
c25aa7cd PP |
521 | GIT_ASSERT_ARG(out); |
522 | GIT_ASSERT_ARG(reference); | |
523 | GIT_ASSERT_ARG(buffer && buffer_len); | |
ceab4e26 BS |
524 | |
525 | blame = git_blame__alloc(reference->repository, reference->options, reference->path); | |
ac3d33df | 526 | GIT_ERROR_CHECK_ALLOC(blame); |
ceab4e26 BS |
527 | |
528 | /* Duplicate all of the hunk structures in the reference blame */ | |
529 | git_vector_foreach(&reference->hunks, i, hunk) { | |
392702ee | 530 | git_blame_hunk *h = dup_hunk(hunk); |
ac3d33df | 531 | GIT_ERROR_CHECK_ALLOC(h); |
392702ee ET |
532 | |
533 | git_vector_insert(&blame->hunks, h); | |
ceab4e26 BS |
534 | } |
535 | ||
536 | /* Diff to the reference blob */ | |
537 | git_diff_blob_to_buffer(reference->final_blob, blame->path, | |
8147b1af ET |
538 | buffer, buffer_len, blame->path, &diffopts, |
539 | NULL, NULL, buffer_hunk_cb, buffer_line_cb, blame); | |
ceab4e26 BS |
540 | |
541 | *out = blame; | |
542 | return 0; | |
543 | } | |
b9f81997 | 544 | |
22a2d3d5 | 545 | int git_blame_options_init(git_blame_options *opts, unsigned int version) |
b9f81997 | 546 | { |
702efc89 RB |
547 | GIT_INIT_STRUCTURE_FROM_TEMPLATE( |
548 | opts, version, git_blame_options, GIT_BLAME_OPTIONS_INIT); | |
549 | return 0; | |
b9f81997 | 550 | } |
22a2d3d5 UG |
551 | |
552 | #ifndef GIT_DEPRECATE_HARD | |
553 | int git_blame_init_options(git_blame_options *opts, unsigned int version) | |
554 | { | |
555 | return git_blame_options_init(opts, version); | |
556 | } | |
557 | #endif |