19
#include <sys/time.h>
20
21
static char pickaxe_usage[] =
22
-"git-pickaxe [-c] [-l] [-t] [-f] [-n] [-p] [-L n,m] [-S <revs-file>] [commit] [--] file\n"
22
+"git-pickaxe [-c] [-l] [-t] [-f] [-n] [-p] [-L n,m] [-S <revs-file>] [-M] [commit] [--] file\n"
23
" -c, --compatibility Use the same output mode as git-annotate (Default: off)\n"
24
" -l, --long Show long commit SHA1 (Default: off)\n"
25
" -t, --time Show raw timestamp (Default: off)\n"
27
" -n, --show-number Show original linenumber (Default: off)\n"
28
" -p, --porcelain Show in a format designed for machine consumption\n"
29
" -L n,m Process only line range n,m, counting from 1\n"
30
+" -M Find line movements within the file\n"
31
" -S revs-file Use revisions from revs-file instead of calling git-rev-list\n";
32
33
static int longest_file;
37
38
#define DEBUG 0
39
40
+#define PICKAXE_BLAME_MOVE 01
41
+
42
/* bits #0..7 in revision.h, #8..11 used for merge_bases() in commit.c */
43
#define METAINFO_SHOWN (1u<<12)
44
#define MORE_THAN_ONE_PATH (1u<<13)
545
return 0;
546
}
547
548
+static void copy_split_if_better(struct blame_entry best_so_far[3],
549
+ struct blame_entry this[3])
550
+{
551
+ if (!this[1].suspect)
552
+ return;
553
+ if (best_so_far[1].suspect &&
554
+ (this[1].num_lines < best_so_far[1].num_lines))
555
+ return;
556
+ memcpy(best_so_far, this, sizeof(struct blame_entry [3]));
557
+}
558
+
559
+static void find_copy_in_blob(struct scoreboard *sb,
560
+ struct blame_entry *ent,
561
+ struct origin *parent,
562
+ struct blame_entry split[3],
563
+ mmfile_t *file_p)
564
+{
565
+ const char *cp;
566
+ int cnt;
567
+ mmfile_t file_o;
568
+ struct patch *patch;
569
+ int i, plno, tlno;
570
+
571
+ cp = nth_line(sb, ent->lno);
572
+ file_o.ptr = (char*) cp;
573
+ cnt = ent->num_lines;
574
+
575
+ while (cnt && cp < sb->final_buf + sb->final_buf_size) {
576
+ if (*cp++ == '\n')
577
+ cnt--;
578
+ }
579
+ file_o.size = cp - file_o.ptr;
580
+
581
+ patch = compare_buffer(file_p, &file_o, 1);
582
+
583
+ memset(split, 0, sizeof(struct blame_entry [3]));
584
+ plno = tlno = 0;
585
+ for (i = 0; i < patch->num; i++) {
586
+ struct chunk *chunk = &patch->chunks[i];
587
+
588
+ /* tlno to chunk->same are the same as ent */
589
+ if (ent->num_lines <= tlno)
590
+ break;
591
+ if (tlno < chunk->same) {
592
+ struct blame_entry this[3];
593
+ split_overlap(this, ent,
594
+ tlno + ent->s_lno, plno,
595
+ chunk->same + ent->s_lno,
596
+ parent);
597
+ copy_split_if_better(split, this);
598
+ }
599
+ plno = chunk->p_next;
600
+ tlno = chunk->t_next;
601
+ }
602
+ free_patch(patch);
603
+}
604
+
605
+static int find_move_in_parent(struct scoreboard *sb,
606
+ struct origin *target,
607
+ struct origin *parent)
608
+{
609
+ int last_in_target;
610
+ struct blame_entry *ent, split[3];
611
+ mmfile_t file_p;
612
+ char type[10];
613
+ char *blob_p;
614
+
615
+ last_in_target = find_last_in_target(sb, target);
616
+ if (last_in_target < 0)
617
+ return 1; /* nothing remains for this target */
618
+
619
+ blob_p = read_sha1_file(parent->blob_sha1, type,
620
+ (unsigned long *) &file_p.size);
621
+ file_p.ptr = blob_p;
622
+ if (!file_p.ptr) {
623
+ free(blob_p);
624
+ return 0;
625
+ }
626
+
627
+ for (ent = sb->ent; ent; ent = ent->next) {
628
+ if (ent->suspect != target || ent->guilty)
629
+ continue;
630
+ find_copy_in_blob(sb, ent, parent, split, &file_p);
631
+ if (split[1].suspect)
632
+ split_blame(sb, split, ent);
633
+ }
634
+ free(blob_p);
635
+ return 0;
636
+}
637
+
638
#define MAXPARENT 16
639
547
-static void pass_blame(struct scoreboard *sb, struct origin *origin)
640
+static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
641
{
642
int i;
643
struct commit *commit = origin->commit;
682
if (pass_blame_to_parent(sb, origin, porigin))
683
return;
684
}
685
+
686
+ /*
687
+ * Optionally run "miff" to find moves in parents' files here.
688
+ */
689
+ if (opt & PICKAXE_BLAME_MOVE)
690
+ for (i = 0, parent = commit->parents;
691
+ i < MAXPARENT && parent;
692
+ parent = parent->next, i++) {
693
+ struct origin *porigin = parent_origin[i];
694
+ if (!porigin)
695
+ continue;
696
+ if (find_move_in_parent(sb, origin, porigin))
697
+ return;
698
+ }
699
+
700
}
701
594
-static void assign_blame(struct scoreboard *sb, struct rev_info *revs)
702
+static void assign_blame(struct scoreboard *sb, struct rev_info *revs, int opt)
703
{
704
while (1) {
705
struct blame_entry *ent;
717
parse_commit(commit);
718
if (!(commit->object.flags & UNINTERESTING) &&
719
!(revs->max_age != -1 && commit->date < revs->max_age))
612
- pass_blame(sb, suspect);
720
+ pass_blame(sb, suspect, opt);
721
722
/* Take responsibility for the remaining entries */
723
for (ent = sb->ent; ent; ent = ent->next)
1075
struct scoreboard sb;
1076
struct origin *o;
1077
struct blame_entry *ent;
970
- int i, seen_dashdash, unk;
1078
+ int i, seen_dashdash, unk, opt;
1079
long bottom, top, lno;
1080
int output_option = 0;
1081
const char *revs_file = NULL;
1082
const char *final_commit_name = NULL;
1083
char type[10];
1084
1085
+ opt = 0;
1086
bottom = top = 0;
1087
seen_dashdash = 0;
1088
for (unk = i = 1; i < argc; i++) {
1097
output_option |= OUTPUT_LONG_OBJECT_NAME;
1098
else if (!strcmp("-S", arg) && ++i < argc)
1099
revs_file = argv[i];
1100
+ else if (!strcmp("-M", arg))
1101
+ opt |= PICKAXE_BLAME_MOVE;
1102
else if (!strcmp("-L", arg) && ++i < argc) {
1103
char *term;
1104
arg = argv[i];
1287
die("reading graft file %s failed: %s",
1288
revs_file, strerror(errno));
1289
1179
- assign_blame(&sb, &revs);
1290
+ assign_blame(&sb, &revs, opt);
1291
1292
coalesce(&sb);
1293