@matheusbsbs / matheus-bagliano / commits / 27e7304567
Fork 0

blame: Rename detection (take 2)

Signed-off-by: Fredrik Kuivinen <freku045@student.liu.se> Signed-off-by: Junio C Hamano <junkio@cox.net>

Fredrik Kuivinen committed Mar 10, 2006 at 10:21 UTC 27e7304567c92c9771efa7a8b787c487391a5fcd
1 file changed +199 -40
blame.c
+199 -40
@@ -14,6 +14,7 @@
14 #include "tree.h"
15 #include "blob.h"
16 #include "diff.h"
17 +#include "diffcore.h"
18 #include "revision.h"
19
20 #define DEBUG 0
@@ -34,7 +35,9 @@ struct util_info {
35 char *buf;
36 unsigned long size;
37 int num_lines;
37 -// const char* path;
38 + const char* pathname;
39 +
40 + void* topo_data;
41 };
42
43 struct chunk {
@@ -342,25 +345,34 @@ static int map_line(struct commit *commit, int line)
345 return info->line_map[line];
346 }
347
345 -static int fill_util_info(struct commit *commit, const char *path)
348 +static struct util_info* get_util(struct commit *commit)
349 {
347 - struct util_info *util;
348 - if (commit->object.util)
349 - return 0;
350 + struct util_info *util = commit->object.util;
351 +
352 + if (util)
353 + return util;
354
355 util = xmalloc(sizeof(struct util_info));
356 + util->buf = NULL;
357 + util->size = 0;
358 + util->line_map = NULL;
359 + util->num_lines = -1;
360 + util->pathname = NULL;
361 + commit->object.util = util;
362 + return util;
363 +}
364 +
365 +static int fill_util_info(struct commit *commit)
366 +{
367 + struct util_info *util = commit->object.util;
368 +
369 + assert(util);
370 + assert(util->pathname);
371
353 - if (get_blob_sha1(commit->tree, path, util->sha1)) {
354 - free(util);
372 + if (get_blob_sha1(commit->tree, util->pathname, util->sha1))
373 return 1;
356 - } else {
357 - util->buf = NULL;
358 - util->size = 0;
359 - util->line_map = NULL;
360 - util->num_lines = -1;
361 - commit->object.util = util;
374 + else
375 return 0;
363 - }
376 }
377
378 static void alloc_line_map(struct commit *commit)
@@ -389,10 +401,11 @@ static void alloc_line_map(struct commit *commit)
401
402 static void init_first_commit(struct commit* commit, const char* filename)
403 {
392 - struct util_info* util;
404 + struct util_info* util = commit->object.util;
405 int i;
406
395 - if (fill_util_info(commit, filename))
407 + util->pathname = filename;
408 + if (fill_util_info(commit))
409 die("fill_util_info failed");
410
411 alloc_line_map(commit);
@@ -453,7 +466,7 @@ static void process_commits(struct rev_info *rev, const char *path,
466 if(num_parents == 0)
467 *initial = commit;
468
456 - if(fill_util_info(commit, path))
469 + if (fill_util_info(commit))
470 continue;
471
472 alloc_line_map(commit);
@@ -471,7 +484,7 @@ static void process_commits(struct rev_info *rev, const char *path,
484 printf("parent: %s\n",
485 sha1_to_hex(parent->object.sha1));
486
474 - if(fill_util_info(parent, path)) {
487 + if (fill_util_info(parent)) {
488 num_parents--;
489 continue;
490 }
@@ -511,6 +524,135 @@ static void process_commits(struct rev_info *rev, const char *path,
524 } while ((commit = get_revision(rev)) != NULL);
525 }
526
527 +
528 +static int compare_tree_path(struct rev_info* revs,
529 + struct commit* c1, struct commit* c2)
530 +{
531 + const char* paths[2];
532 + struct util_info* util = c2->object.util;
533 + paths[0] = util->pathname;
534 + paths[1] = NULL;
535 +
536 + diff_tree_setup_paths(get_pathspec(revs->prefix, paths));
537 + return rev_compare_tree(c1->tree, c2->tree);
538 +}
539 +
540 +
541 +static int same_tree_as_empty_path(struct rev_info *revs, struct tree* t1,
542 + const char* path)
543 +{
544 + const char* paths[2];
545 + paths[0] = path;
546 + paths[1] = NULL;
547 +
548 + diff_tree_setup_paths(get_pathspec(revs->prefix, paths));
549 + return rev_same_tree_as_empty(t1);
550 +}
551 +
552 +static const char* find_rename(struct commit* commit, struct commit* parent)
553 +{
554 + struct util_info* cutil = commit->object.util;
555 + struct diff_options diff_opts;
556 + const char *paths[1];
557 + int i;
558 +
559 + if (DEBUG) {
560 + printf("find_rename commit: %s ",
561 + sha1_to_hex(commit->object.sha1));
562 + puts(sha1_to_hex(parent->object.sha1));
563 + }
564 +
565 + diff_setup(&diff_opts);
566 + diff_opts.recursive = 1;
567 + diff_opts.detect_rename = DIFF_DETECT_RENAME;
568 + paths[0] = NULL;
569 + diff_tree_setup_paths(paths);
570 + if (diff_setup_done(&diff_opts) < 0)
571 + die("diff_setup_done failed");
572 +
573 + diff_tree_sha1(commit->tree->object.sha1, parent->tree->object.sha1,
574 + "", &diff_opts);
575 + diffcore_std(&diff_opts);
576 +
577 + for (i = 0; i < diff_queued_diff.nr; i++) {
578 + struct diff_filepair *p = diff_queued_diff.queue[i];
579 +
580 + if (p->status == 'R' && !strcmp(p->one->path, cutil->pathname)) {
581 + if (DEBUG)
582 + printf("rename %s -> %s\n", p->one->path, p->two->path);
583 + return p->two->path;
584 + }
585 + }
586 +
587 + return 0;
588 +}
589 +
590 +static void simplify_commit(struct rev_info *revs, struct commit *commit)
591 +{
592 + struct commit_list **pp, *parent;
593 +
594 + if (!commit->tree)
595 + return;
596 +
597 + if (!commit->parents) {
598 + struct util_info* util = commit->object.util;
599 + if (!same_tree_as_empty_path(revs, commit->tree,
600 + util->pathname))
601 + commit->object.flags |= TREECHANGE;
602 + return;
603 + }
604 +
605 + pp = &commit->parents;
606 + while ((parent = *pp) != NULL) {
607 + struct commit *p = parent->item;
608 +
609 + if (p->object.flags & UNINTERESTING) {
610 + pp = &parent->next;
611 + continue;
612 + }
613 +
614 + parse_commit(p);
615 + switch (compare_tree_path(revs, p, commit)) {
616 + case REV_TREE_SAME:
617 + parent->next = NULL;
618 + commit->parents = parent;
619 + get_util(p)->pathname = get_util(commit)->pathname;
620 + return;
621 +
622 + case REV_TREE_NEW:
623 + {
624 +
625 + struct util_info* util = commit->object.util;
626 + if (revs->remove_empty_trees &&
627 + same_tree_as_empty_path(revs, p->tree,
628 + util->pathname)) {
629 + const char* new_name = find_rename(commit, p);
630 + if (new_name) {
631 + struct util_info* putil = get_util(p);
632 + if (!putil->pathname)
633 + putil->pathname = strdup(new_name);
634 + } else {
635 + *pp = parent->next;
636 + continue;
637 + }
638 + }
639 + }
640 +
641 + /* fallthrough */
642 + case REV_TREE_DIFFERENT:
643 + pp = &parent->next;
644 + if (!get_util(p)->pathname)
645 + get_util(p)->pathname =
646 + get_util(commit)->pathname;
647 + continue;
648 + }
649 + die("bad tree compare for commit %s",
650 + sha1_to_hex(commit->object.sha1));
651 + }
652 + commit->object.flags |= TREECHANGE;
653 +}
654 +
655 +
656 struct commit_info
657 {
658 char* author;
@@ -569,6 +711,18 @@ static const char* format_time(unsigned long time, const char* tz_str)
711 return time_buf;
712 }
713
714 +static void topo_setter(struct commit* c, void* data)
715 +{
716 + struct util_info* util = c->object.util;
717 + util->topo_data = data;
718 +}
719 +
720 +static void* topo_getter(struct commit* c)
721 +{
722 + struct util_info* util = c->object.util;
723 + return util->topo_data;
724 +}
725 +
726 int main(int argc, const char **argv)
727 {
728 int i;
@@ -580,8 +734,8 @@ int main(int argc, const char **argv)
734 int sha1_len = 8;
735 int compability = 0;
736 int options = 1;
737 + struct commit* start_commit;
738
584 - int num_args;
739 const char* args[10];
740 struct rev_info rev;
741
@@ -634,28 +788,29 @@ int main(int argc, const char **argv)
788 strcpy(filename_buf, filename);
789 filename = filename_buf;
790
637 - {
638 - struct commit* c;
639 - if (get_sha1(commit, sha1))
640 - die("get_sha1 failed, commit '%s' not found", commit);
641 - c = lookup_commit_reference(sha1);
642 -
643 - if (fill_util_info(c, filename)) {
644 - printf("%s not found in %s\n", filename, commit);
645 - return 1;
646 - }
791 + if (get_sha1(commit, sha1))
792 + die("get_sha1 failed, commit '%s' not found", commit);
793 + start_commit = lookup_commit_reference(sha1);
794 + get_util(start_commit)->pathname = filename;
795 + if (fill_util_info(start_commit)) {
796 + printf("%s not found in %s\n", filename, commit);
797 + return 1;
798 }
799
649 - num_args = 0;
650 - args[num_args++] = NULL;
651 - args[num_args++] = "--topo-order";
652 - args[num_args++] = "--remove-empty";
653 - args[num_args++] = commit;
654 - args[num_args++] = "--";
655 - args[num_args++] = filename;
656 - args[num_args] = NULL;
800
658 - setup_revisions(num_args, args, &rev, "HEAD");
801 + init_revisions(&rev);
802 + rev.remove_empty_trees = 1;
803 + rev.topo_order = 1;
804 + rev.prune_fn = simplify_commit;
805 + rev.topo_setter = topo_setter;
806 + rev.topo_getter = topo_getter;
807 + rev.limited = 1;
808 +
809 + commit_list_insert(start_commit, &rev.commits);
810 +
811 + args[0] = filename;
812 + args[1] = NULL;
813 + diff_tree_setup_paths(args);
814 prepare_revision_walk(&rev);
815 process_commits(&rev, filename, &initial);
816
@@ -665,17 +820,21 @@ int main(int argc, const char **argv)
820
821 for (i = 0; i < num_blame_lines; i++) {
822 struct commit *c = blame_lines[i];
823 + struct util_info* u;
824 +
825 if (!c)
826 c = initial;
827
828 + u = c->object.util;
829 get_commit_info(c, &ci);
830 fwrite(sha1_to_hex(c->object.sha1), sha1_len, 1, stdout);
831 if(compability)
832 printf("\t(%10s\t%10s\t%d)", ci.author,
833 format_time(ci.author_time, ci.author_tz), i+1);
834 else
677 - printf(" (%-15.15s %10s %*d) ", ci.author,
678 - format_time(ci.author_time, ci.author_tz),
835 + printf(" %s (%-15.15s %10s %*d) ", u->pathname,
836 + ci.author, format_time(ci.author_time,
837 + ci.author_tz),
838 max_digits, i+1);
839
840 if(i == num_blame_lines - 1) {