Hash :
8c35ff14
Author :
Date :
2020-11-19T15:37:46
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
/*
* Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
* Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/queue.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <errno.h>
#include <sha1.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
#include <util.h>
#include <zlib.h>
#include "got_error.h"
#include "got_object.h"
#include "got_cancel.h"
#include "got_blame.h"
#include "got_commit_graph.h"
#include "got_opentemp.h"
#include "got_lib_inflate.h"
#include "got_lib_delta.h"
#include "got_lib_object.h"
#include "got_lib_diff.h"
#ifndef MAX
#define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
#endif
struct got_blame_line {
int annotated;
struct got_object_id id;
};
struct got_blame {
struct diff_config *cfg;
int nlines; /* number of lines in file being blamed */
int nannotated; /* number of lines already annotated */
struct got_blame_line *lines; /* one per line */
int ncommits;
/*
* These change with every traversed commit. After diffing
* commits N:N-1, in preparation for diffing commits N-1:N-2,
* data for commit N is retained and flipped into data for N-1.
*
*/
FILE *f1; /* older version from commit N-1. */
FILE *f2; /* newer version from commit N. */
unsigned char *map1;
unsigned char *map2;
off_t size1;
off_t size2;
int nlines1;
int nlines2;
off_t *line_offsets1;
off_t *line_offsets2;
/*
* Map line numbers of an older version of the file to valid line
* numbers in the version of the file being blamed. This map is
* updated with each commit we traverse throughout the file's history.
* Lines mapped to -1 do not correspond to any line in the version
* being blamed.
*/
int *linemap1;
int *linemap2;
struct diff_data *data1;
struct diff_data *data2;
};
static const struct got_error *
annotate_line(struct got_blame *blame, int lineno, struct got_object_id *id,
const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
void *arg)
{
const struct got_error *err = NULL;
struct got_blame_line *line;
if (lineno < 0 || lineno >= blame->nlines)
return NULL;
line = &blame->lines[lineno];
if (line->annotated)
return NULL;
memcpy(&line->id, id, sizeof(line->id));
line->annotated = 1;
blame->nannotated++;
if (cb)
err = cb(arg, blame->nlines, lineno + 1, id);
return err;
}
static const struct got_error *
blame_changes(struct got_blame *blame, struct diff_result *diff_result,
struct got_object_id *commit_id,
const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
void *arg)
{
const struct got_error *err = NULL;
int i;
int idx1 = 0, idx2 = 0;
for (i = 0; i < diff_result->chunks.len &&
blame->nannotated < blame->nlines; i++) {
struct diff_chunk *c = diff_chunk_get(diff_result, i);
unsigned int left_start, left_count;
unsigned int right_start, right_count;
int j;
/*
* We do not need to worry about idx1/idx2 growing out
* of bounds because the diff implementation ensures
* that chunk ranges never exceed the number of lines
* in the left/right input files.
*/
left_start = diff_chunk_get_left_start(c, diff_result, 0);
left_count = diff_chunk_get_left_count(c);
right_start = diff_chunk_get_right_start(c, diff_result, 0);
right_count = diff_chunk_get_right_count(c);
if (left_count == right_count) {
for (j = 0; j < left_count; j++) {
blame->linemap1[idx1++] =
blame->linemap2[idx2++];
}
continue;
}
if (right_count == 0) {
for (j = 0; j < left_count; j++) {
blame->linemap1[idx1++] = -1;
}
continue;
}
for (j = 0; j < right_count; j++) {
int ln = blame->linemap2[idx2++];
err = annotate_line(blame, ln, commit_id, cb, arg);
if (err)
return err;
if (blame->nlines == blame->nannotated)
break;
}
}
return NULL;
}
static const struct got_error *
blame_prepare_file(FILE *f, unsigned char **p, off_t *size,
int *nlines, off_t **line_offsets, struct diff_data *diff_data,
const struct diff_config *cfg, struct got_blob_object *blob)
{
const struct got_error *err = NULL;
int rc;
err = got_object_blob_dump_to_file(size, nlines, line_offsets,
f, blob);
if (err)
return err;
#ifndef GOT_DIFF_NO_MMAP
*p = mmap(NULL, *size, PROT_READ, MAP_PRIVATE, fileno(f), 0);
if (*p == MAP_FAILED)
#endif
*p = NULL; /* fall back on file I/O */
rc = diff_atomize_file(diff_data, cfg, f, *p, *size, 0);
if (rc)
return got_error_set_errno(rc, "diff_atomize_file");
return NULL;
}
static const struct got_error *
blame_commit(struct got_blame *blame, struct got_object_id *id,
const char *path, struct got_repository *repo,
const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
void *arg)
{
const struct got_error *err = NULL;
struct got_commit_object *commit = NULL;
struct got_object_qid *pid = NULL;
struct got_object_id *pblob_id = NULL;
struct got_blob_object *pblob = NULL;
struct diff_result *diff_result = NULL;
err = got_object_open_as_commit(&commit, repo, id);
if (err)
return err;
pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
if (pid == NULL) {
got_object_commit_close(commit);
return NULL;
}
err = got_object_id_by_path(&pblob_id, repo, pid->id, path);
if (err) {
if (err->code == GOT_ERR_NO_TREE_ENTRY)
err = NULL;
goto done;
}
err = got_object_open_as_blob(&pblob, repo, pblob_id, 8192);
if (err)
goto done;
blame->f1 = got_opentemp();
if (blame->f1 == NULL) {
err = got_error_from_errno("got_opentemp");
goto done;
}
err = blame_prepare_file(blame->f1, &blame->map1, &blame->size1,
&blame->nlines1, &blame->line_offsets1, blame->data1,
blame->cfg, pblob);
if (err)
goto done;
diff_result = diff_main(blame->cfg, blame->data1, blame->data2);
if (diff_result == NULL) {
err = got_error_set_errno(ENOMEM, "malloc");
goto done;
}
if (diff_result->rc != DIFF_RC_OK) {
err = got_error_set_errno(diff_result->rc, "diff");
goto done;
}
if (diff_result->chunks.len > 0) {
if (blame->nlines1 > 0) {
blame->linemap1 = calloc(blame->nlines1,
sizeof(*blame->linemap1));
if (blame->linemap1 == NULL) {
err = got_error_from_errno("malloc");
goto done;
}
}
err = blame_changes(blame, diff_result, id, cb, arg);
if (err)
goto done;
} else if (cb)
err = cb(arg, blame->nlines, -1, id);
done:
if (diff_result)
diff_result_free(diff_result);
if (commit)
got_object_commit_close(commit);
free(pblob_id);
if (pblob)
got_object_blob_close(pblob);
return err;
}
static const struct got_error *
blame_close(struct got_blame *blame)
{
const struct got_error *err = NULL;
diff_data_free(blame->data1);
free(blame->data1);
diff_data_free(blame->data2);
free(blame->data2);
if (blame->map1) {
if (munmap(blame->map1, blame->size1) == -1 && err == NULL)
err = got_error_from_errno("munmap");
}
if (blame->map2) {
if (munmap(blame->map2, blame->size2) == -1 && err == NULL)
err = got_error_from_errno("munmap");
}
if (blame->f1 && fclose(blame->f1) != 0 && err == NULL)
err = got_error_from_errno("fclose");
if (blame->f2 && fclose(blame->f2) != 0 && err == NULL)
err = got_error_from_errno("fclose");
free(blame->lines);
free(blame->line_offsets1);
free(blame->line_offsets2);
free(blame->linemap1);
free(blame->linemap2);
free(blame->cfg);
free(blame);
return err;
}
static int
atomize_file(struct diff_data *d, FILE *f, off_t filesize, int nlines,
off_t *line_offsets)
{
int i, rc = DIFF_RC_OK;
ARRAYLIST_INIT(d->atoms, nlines);
for (i = 0; i < nlines; i++) {
struct diff_atom *atom;
off_t len, pos = line_offsets[i];
unsigned int hash = 0;
int j;
ARRAYLIST_ADD(atom, d->atoms);
if (atom == NULL) {
rc = errno;
break;
}
if (i < nlines - 1)
len = line_offsets[i + 1] - pos;
else
len = filesize - pos;
if (fseeko(f, pos, SEEK_SET) == -1) {
rc = errno;
break;
}
for (j = 0; j < len; j++) {
int c = fgetc(f);
if (c == EOF) {
if (feof(f))
rc = EIO; /* unexpected EOF */
else
rc = errno;
goto done;
}
hash = diff_atom_hash_update(hash, (unsigned char)c);
}
*atom = (struct diff_atom){
.root = d,
.pos = pos,
.at = NULL, /* atom data is not memory-mapped */
.len = len,
.hash = hash,
};
}
done:
if (rc)
ARRAYLIST_FREE(d->atoms);
return rc;
}
static int
atomize_file_mmap(struct diff_data *d, unsigned char *p,
off_t filesize, int nlines, off_t *line_offsets)
{
int i, rc = DIFF_RC_OK;
ARRAYLIST_INIT(d->atoms, nlines);
for (i = 0; i < nlines; i++) {
struct diff_atom *atom;
off_t len, pos = line_offsets[i];
unsigned int hash = 0;
int j;
ARRAYLIST_ADD(atom, d->atoms);
if (atom == NULL) {
rc = errno;
break;
}
if (i < nlines - 1)
len = line_offsets[i + 1] - pos;
else
len = filesize - pos;
for (j = 0; j < len; j++)
hash = diff_atom_hash_update(hash, p[pos + j]);
*atom = (struct diff_atom){
.root = d,
.pos = pos,
.at = &p[pos],
.len = len,
.hash = hash,
};
}
if (rc)
ARRAYLIST_FREE(d->atoms);
return rc;
}
/* Implements diff_atomize_func_t */
static int
blame_atomize_file(void *arg, struct diff_data *d)
{
struct got_blame *blame = arg;
if (d->f == blame->f1) {
if (blame->map1)
return atomize_file_mmap(d, blame->map1,
blame->size1, blame->nlines1,
blame->line_offsets1);
else
return atomize_file(d, blame->f1, blame->size1,
blame->nlines1, blame->line_offsets1);
} else if (d->f == blame->f2) {
if (d->atoms.len > 0) {
/* Re-use data from previous commit. */
return DIFF_RC_OK;
}
if (blame->map2)
return atomize_file_mmap(d, blame->map2,
blame->size2, blame->nlines2,
blame->line_offsets2);
else
return atomize_file(d, blame->f2, blame->size2,
blame->nlines2, blame->line_offsets2);
}
return DIFF_RC_OK;
}
static const struct got_error *
close_file2_and_reuse_file1(struct got_blame *blame)
{
struct diff_data *d;
free(blame->line_offsets2);
blame->line_offsets2 = blame->line_offsets1;
blame->line_offsets1 = NULL;
free(blame->linemap2);
blame->linemap2 = blame->linemap1;
blame->linemap1 = NULL;
if (blame->map2) {
if (munmap(blame->map2, blame->size2) == -1)
return got_error_from_errno("munmap");
blame->map2 = blame->map1;
blame->map2 = NULL;
}
blame->size2 = blame->size1;
blame->size1 = 0;
if (fclose(blame->f2) == EOF)
return got_error_from_errno("fclose");
blame->f2 = blame->f1;
blame->f1 = NULL;
blame->nlines2 = blame->nlines1;
blame->nlines1 = 0;
free(blame->line_offsets2);
blame->line_offsets2 = blame->line_offsets1;
blame->line_offsets2 = NULL;
diff_data_free(blame->data2); /* does not free pointer itself */
memset(blame->data2, 0, sizeof(*blame->data2));
d = blame->data2;
blame->data2 = blame->data1;
blame->data1 = d;
return NULL;
}
static const struct got_error *
blame_open(struct got_blame **blamep, const char *path,
struct got_object_id *start_commit_id, struct got_repository *repo,
const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
void *arg, got_cancel_cb cancel_cb, void *cancel_arg)
{
const struct got_error *err = NULL;
struct got_object_id *obj_id = NULL;
struct got_blob_object *blob = NULL;
struct got_blame *blame = NULL;
struct got_object_id *id = NULL;
int lineno;
struct got_commit_graph *graph = NULL;
*blamep = NULL;
err = got_object_id_by_path(&obj_id, repo, start_commit_id, path);
if (err)
goto done;
err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
if (err)
goto done;
blame = calloc(1, sizeof(*blame));
if (blame == NULL) {
err = got_error_from_errno("calloc");
goto done;
}
blame->data1 = calloc(1, sizeof(*blame->data1));
if (blame->data1 == NULL) {
err = got_error_from_errno("calloc");
goto done;
}
blame->data2 = calloc(1, sizeof(*blame->data2));
if (blame->data2 == NULL) {
err = got_error_from_errno("calloc");
goto done;
}
blame->f2 = got_opentemp();
if (blame->f2 == NULL) {
err = got_error_from_errno("got_opentemp");
goto done;
}
err = got_diff_get_config(&blame->cfg, GOT_DIFF_ALGORITHM_PATIENCE,
blame_atomize_file, blame);
if (err)
goto done;
err = blame_prepare_file(blame->f2, &blame->map2, &blame->size2,
&blame->nlines2, &blame->line_offsets2, blame->data2,
blame->cfg, blob);
blame->nlines = blame->nlines2;
if (err || blame->nlines == 0)
goto done;
got_object_blob_close(blob);
blob = NULL;
/* Don't include \n at EOF in the blame line count. */
if (blame->line_offsets2[blame->nlines - 1] == blame->size2)
blame->nlines--;
blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
if (blame->lines == NULL) {
err = got_error_from_errno("calloc");
goto done;
}
blame->linemap2 = calloc(blame->nlines2, sizeof(*blame->linemap2));
if (blame->linemap2 == NULL) {
err = got_error_from_errno("calloc");
goto done;
}
for (lineno = 0; lineno < blame->nlines2; lineno++)
blame->linemap2[lineno] = lineno;
err = got_commit_graph_open(&graph, path, 1);
if (err)
goto done;
err = got_commit_graph_iter_start(graph, start_commit_id, repo,
cancel_cb, cancel_arg);
if (err)
goto done;
for (;;) {
struct got_object_id *next_id;
err = got_commit_graph_iter_next(&next_id, graph, repo,
cancel_cb, cancel_arg);
if (err) {
if (err->code == GOT_ERR_ITER_COMPLETED) {
err = NULL;
break;
}
goto done;
}
if (next_id) {
id = next_id;
err = blame_commit(blame, id, path, repo, cb, arg);
if (err) {
if (err->code == GOT_ERR_ITER_COMPLETED)
err = NULL;
goto done;
}
if (blame->nannotated == blame->nlines)
break;
err = close_file2_and_reuse_file1(blame);
if (err)
goto done;
}
}
if (id && blame->nannotated < blame->nlines) {
/* Annotate remaining non-annotated lines with last commit. */
for (lineno = 0; lineno < blame->nlines; lineno++) {
err = annotate_line(blame, lineno, id, cb, arg);
if (err)
goto done;
}
}
done:
if (graph)
got_commit_graph_close(graph);
free(obj_id);
if (blob)
got_object_blob_close(blob);
if (err) {
if (blame)
blame_close(blame);
} else
*blamep = blame;
return err;
}
const struct got_error *
got_blame(const char *path, struct got_object_id *commit_id,
struct got_repository *repo,
const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
void *arg, got_cancel_cb cancel_cb, void* cancel_arg)
{
const struct got_error *err = NULL, *close_err = NULL;
struct got_blame *blame;
char *abspath;
if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
return got_error_from_errno2("asprintf", path);
err = blame_open(&blame, abspath, commit_id, repo, cb, arg,
cancel_cb, cancel_arg);
free(abspath);
if (blame)
close_err = blame_close(blame);
return err ? err : close_err;
}