~ubuntu-branches/ubuntu/edgy/git-core/edgy-backports

« back to all changes in this revision

Viewing changes to builtin-blame.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones
  • Date: 2007-11-29 07:28:44 UTC
  • mfrom: (8.1.2 dapper-backports)
  • Revision ID: package-import@ubuntu.com-20071129072844-umsb7y3140yhxkth
Tags: 1:1.5.3.6-1.1~dapper1
* backport to dapper et al.
  - debian/rules changes to support source:Upstream-Version for old dpkg.
  - allow asciidoc (>7.0.2-3)

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
#include "diff.h"
14
14
#include "diffcore.h"
15
15
#include "revision.h"
 
16
#include "quote.h"
16
17
#include "xdiff-interface.h"
17
 
 
18
 
#include <time.h>
19
 
#include <sys/time.h>
20
 
#include <regex.h>
 
18
#include "cache-tree.h"
 
19
#include "path-list.h"
 
20
#include "mailmap.h"
21
21
 
22
22
static char blame_usage[] =
23
 
"git-blame [-c] [-l] [-t] [-f] [-n] [-p] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [commit] [--] file\n"
24
 
"  -c, --compatibility Use the same output mode as git-annotate (Default: off)\n"
25
 
"  -l, --long          Show long commit SHA1 (Default: off)\n"
26
 
"  -t, --time          Show raw timestamp (Default: off)\n"
 
23
"git-blame [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-p] [-w] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [--contents <filename>] [--incremental] [commit] [--] file\n"
 
24
"  -c                  Use the same output mode as git-annotate (Default: off)\n"
 
25
"  -b                  Show blank SHA-1 for boundary commits (Default: off)\n"
 
26
"  -l                  Show long commit SHA1 (Default: off)\n"
 
27
"  --root              Do not treat root commits as boundaries (Default: off)\n"
 
28
"  -t                  Show raw timestamp (Default: off)\n"
27
29
"  -f, --show-name     Show original filename (Default: auto)\n"
28
30
"  -n, --show-number   Show original linenumber (Default: off)\n"
 
31
"  -s                  Suppress author name and timestamp (Default: off)\n"
29
32
"  -p, --porcelain     Show in a format designed for machine consumption\n"
 
33
"  -w                  Ignore whitespace differences\n"
30
34
"  -L n,m              Process only line range n,m, counting from 1\n"
31
35
"  -M, -C              Find line movements within and across files\n"
 
36
"  --incremental       Show blame entries as we find them, incrementally\n"
 
37
"  --contents file     Use <file>'s contents as the final image\n"
32
38
"  -S revs-file        Use revisions from revs-file instead of calling git-rev-list\n";
33
39
 
34
40
static int longest_file;
36
42
static int max_orig_digits;
37
43
static int max_digits;
38
44
static int max_score_digits;
 
45
static int show_root;
 
46
static int blank_boundary;
 
47
static int incremental;
 
48
static int cmd_is_annotate;
 
49
static int xdl_opts = XDF_NEED_MINIMAL;
 
50
static struct path_list mailmap;
39
51
 
40
52
#ifndef DEBUG
41
53
#define DEBUG 0
49
61
#define PICKAXE_BLAME_MOVE              01
50
62
#define PICKAXE_BLAME_COPY              02
51
63
#define PICKAXE_BLAME_COPY_HARDER       04
 
64
#define PICKAXE_BLAME_COPY_HARDEST      010
52
65
 
53
66
/*
54
67
 * blame for a blame_entry with score lower than these thresholds
74
87
        char path[FLEX_ARRAY];
75
88
};
76
89
 
 
90
/*
 
91
 * Given an origin, prepare mmfile_t structure to be used by the
 
92
 * diff machinery
 
93
 */
77
94
static char *fill_origin_blob(struct origin *o, mmfile_t *file)
78
95
{
79
96
        if (!o->file.ptr) {
80
 
                char type[10];
 
97
                enum object_type type;
81
98
                num_read_blob++;
82
 
                file->ptr = read_sha1_file(o->blob_sha1, type,
 
99
                file->ptr = read_sha1_file(o->blob_sha1, &type,
83
100
                                           (unsigned long *)(&(file->size)));
 
101
                if (!file->ptr)
 
102
                        die("Cannot read blob %s for path %s",
 
103
                            sha1_to_hex(o->blob_sha1),
 
104
                            o->path);
84
105
                o->file = *file;
85
106
        }
86
107
        else
88
109
        return file->ptr;
89
110
}
90
111
 
 
112
/*
 
113
 * Origin is refcounted and usually we keep the blob contents to be
 
114
 * reused.
 
115
 */
91
116
static inline struct origin *origin_incref(struct origin *o)
92
117
{
93
118
        if (o)
105
130
        }
106
131
}
107
132
 
 
133
/*
 
134
 * Each group of lines is described by a blame_entry; it can be split
 
135
 * as we pass blame to the parents.  They form a linked list in the
 
136
 * scoreboard structure, sorted by the target line number.
 
137
 */
108
138
struct blame_entry {
109
139
        struct blame_entry *prev;
110
140
        struct blame_entry *next;
131
161
        int s_lno;
132
162
 
133
163
        /* how significant this entry is -- cached to avoid
134
 
         * scanning the lines over and over
 
164
         * scanning the lines over and over.
135
165
         */
136
166
        unsigned score;
137
167
};
138
168
 
 
169
/*
 
170
 * The current state of the blame assignment.
 
171
 */
139
172
struct scoreboard {
140
173
        /* the final commit (i.e. where we started digging from) */
141
174
        struct commit *final;
142
175
 
143
176
        const char *path;
144
177
 
145
 
        /* the contents in the final; pointed into by buf pointers of
146
 
         * blame_entries
 
178
        /*
 
179
         * The contents in the final image.
 
180
         * Used by many functions to obtain contents of the nth line,
 
181
         * indexed with scoreboard.lineno[blame_entry.lno].
147
182
         */
148
183
        const char *final_buf;
149
184
        unsigned long final_buf_size;
156
191
        int *lineno;
157
192
};
158
193
 
159
 
static int cmp_suspect(struct origin *a, struct origin *b)
 
194
static inline int same_suspect(struct origin *a, struct origin *b)
160
195
{
161
 
        int cmp = hashcmp(a->commit->object.sha1, b->commit->object.sha1);
162
 
        if (cmp)
163
 
                return cmp;
164
 
        return strcmp(a->path, b->path);
 
196
        if (a == b)
 
197
                return 1;
 
198
        if (a->commit != b->commit)
 
199
                return 0;
 
200
        return !strcmp(a->path, b->path);
165
201
}
166
202
 
167
 
#define cmp_suspect(a, b) ( ((a)==(b)) ? 0 : cmp_suspect(a,b) )
168
 
 
169
203
static void sanity_check_refcnt(struct scoreboard *);
170
204
 
 
205
/*
 
206
 * If two blame entries that are next to each other came from
 
207
 * contiguous lines in the same origin (i.e. <commit, path> pair),
 
208
 * merge them together.
 
209
 */
171
210
static void coalesce(struct scoreboard *sb)
172
211
{
173
212
        struct blame_entry *ent, *next;
174
213
 
175
214
        for (ent = sb->ent; ent && (next = ent->next); ent = next) {
176
 
                if (!cmp_suspect(ent->suspect, next->suspect) &&
 
215
                if (same_suspect(ent->suspect, next->suspect) &&
177
216
                    ent->guilty == next->guilty &&
178
217
                    ent->s_lno + ent->num_lines == next->s_lno) {
179
218
                        ent->num_lines += next->num_lines;
191
230
                sanity_check_refcnt(sb);
192
231
}
193
232
 
 
233
/*
 
234
 * Given a commit and a path in it, create a new origin structure.
 
235
 * The callers that add blame to the scoreboard should use
 
236
 * get_origin() to obtain shared, refcounted copy instead of calling
 
237
 * this function directly.
 
238
 */
194
239
static struct origin *make_origin(struct commit *commit, const char *path)
195
240
{
196
241
        struct origin *o;
201
246
        return o;
202
247
}
203
248
 
 
249
/*
 
250
 * Locate an existing origin or create a new one.
 
251
 */
204
252
static struct origin *get_origin(struct scoreboard *sb,
205
253
                                 struct commit *commit,
206
254
                                 const char *path)
215
263
        return make_origin(commit, path);
216
264
}
217
265
 
 
266
/*
 
267
 * Fill the blob_sha1 field of an origin if it hasn't, so that later
 
268
 * call to fill_origin_blob() can use it to locate the data.  blob_sha1
 
269
 * for an origin is also used to pass the blame for the entire file to
 
270
 * the parent to detect the case where a child's blob is identical to
 
271
 * that of its parent's.
 
272
 */
218
273
static int fill_blob_sha1(struct origin *origin)
219
274
{
220
275
        unsigned mode;
221
 
        char type[10];
222
276
 
223
277
        if (!is_null_sha1(origin->blob_sha1))
224
278
                return 0;
226
280
                           origin->path,
227
281
                           origin->blob_sha1, &mode))
228
282
                goto error_out;
229
 
        if (sha1_object_info(origin->blob_sha1, type, NULL) ||
230
 
            strcmp(type, blob_type))
 
283
        if (sha1_object_info(origin->blob_sha1, NULL) != OBJ_BLOB)
231
284
                goto error_out;
232
285
        return 0;
233
286
 error_out:
235
288
        return -1;
236
289
}
237
290
 
 
291
/*
 
292
 * We have an origin -- check if the same path exists in the
 
293
 * parent and return an origin structure to represent it.
 
294
 */
238
295
static struct origin *find_origin(struct scoreboard *sb,
239
296
                                  struct commit *parent,
240
297
                                  struct origin *origin)
244
301
        const char *paths[2];
245
302
 
246
303
        if (parent->util) {
247
 
                /* This is a freestanding copy of origin and not
248
 
                 * refcounted.
 
304
                /*
 
305
                 * Each commit object can cache one origin in that
 
306
                 * commit.  This is a freestanding copy of origin and
 
307
                 * not refcounted.
249
308
                 */
250
309
                struct origin *cached = parent->util;
251
310
                if (!strcmp(cached->path, origin->path)) {
 
311
                        /*
 
312
                         * The same path between origin and its parent
 
313
                         * without renaming -- the most common case.
 
314
                         */
252
315
                        porigin = get_origin(sb, parent, cached->path);
 
316
 
 
317
                        /*
 
318
                         * If the origin was newly created (i.e. get_origin
 
319
                         * would call make_origin if none is found in the
 
320
                         * scoreboard), it does not know the blob_sha1,
 
321
                         * so copy it.  Otherwise porigin was in the
 
322
                         * scoreboard and already knows blob_sha1.
 
323
                         */
253
324
                        if (porigin->refcnt == 1)
254
325
                                hashcpy(porigin->blob_sha1, cached->blob_sha1);
255
326
                        return porigin;
273
344
        diff_tree_setup_paths(paths, &diff_opts);
274
345
        if (diff_setup_done(&diff_opts) < 0)
275
346
                die("diff-setup");
276
 
        diff_tree_sha1(parent->tree->object.sha1,
277
 
                       origin->commit->tree->object.sha1,
278
 
                       "", &diff_opts);
 
347
 
 
348
        if (is_null_sha1(origin->commit->object.sha1))
 
349
                do_diff_cache(parent->tree->object.sha1, &diff_opts);
 
350
        else
 
351
                diff_tree_sha1(parent->tree->object.sha1,
 
352
                               origin->commit->tree->object.sha1,
 
353
                               "", &diff_opts);
279
354
        diffcore_std(&diff_opts);
280
355
 
281
356
        /* It is either one entry that says "modified", or "created",
306
381
        }
307
382
        diff_flush(&diff_opts);
308
383
        if (porigin) {
 
384
                /*
 
385
                 * Create a freestanding copy that is not part of
 
386
                 * the refcounted origin found in the scoreboard, and
 
387
                 * cache it in the commit.
 
388
                 */
309
389
                struct origin *cached;
 
390
 
310
391
                cached = make_origin(porigin->commit, porigin->path);
311
392
                hashcpy(cached->blob_sha1, porigin->blob_sha1);
312
393
                parent->util = cached;
314
395
        return porigin;
315
396
}
316
397
 
 
398
/*
 
399
 * We have an origin -- find the path that corresponds to it in its
 
400
 * parent and return an origin structure to represent it.
 
401
 */
317
402
static struct origin *find_rename(struct scoreboard *sb,
318
403
                                  struct commit *parent,
319
404
                                  struct origin *origin)
332
417
        diff_tree_setup_paths(paths, &diff_opts);
333
418
        if (diff_setup_done(&diff_opts) < 0)
334
419
                die("diff-setup");
335
 
        diff_tree_sha1(parent->tree->object.sha1,
336
 
                       origin->commit->tree->object.sha1,
337
 
                       "", &diff_opts);
 
420
 
 
421
        if (is_null_sha1(origin->commit->object.sha1))
 
422
                do_diff_cache(parent->tree->object.sha1, &diff_opts);
 
423
        else
 
424
                diff_tree_sha1(parent->tree->object.sha1,
 
425
                               origin->commit->tree->object.sha1,
 
426
                               "", &diff_opts);
338
427
        diffcore_std(&diff_opts);
339
428
 
340
429
        for (i = 0; i < diff_queued_diff.nr; i++) {
350
439
        return porigin;
351
440
}
352
441
 
 
442
/*
 
443
 * Parsing of patch chunks...
 
444
 */
353
445
struct chunk {
354
446
        /* line number in postimage; up to but not including this
355
447
         * line is the same as preimage
429
521
        xdemitconf_t xecfg;
430
522
        xdemitcb_t ecb;
431
523
 
432
 
        xpp.flags = XDF_NEED_MINIMAL;
 
524
        xpp.flags = xdl_opts;
 
525
        memset(&xecfg, 0, sizeof(xecfg));
433
526
        xecfg.ctxlen = context;
434
 
        xecfg.flags = 0;
435
527
        ecb.outf = xdiff_outf;
436
528
        ecb.priv = &state;
437
529
        memset(&state, 0, sizeof(state));
451
543
        return state.ret;
452
544
}
453
545
 
 
546
/*
 
547
 * Run diff between two origins and grab the patch output, so that
 
548
 * we can pass blame for lines origin is currently suspected for
 
549
 * to its parent.
 
550
 */
454
551
static struct patch *get_patch(struct origin *parent, struct origin *origin)
455
552
{
456
553
        mmfile_t file_p, file_o;
471
568
        free(p);
472
569
}
473
570
 
 
571
/*
 
572
 * Link in a new blame entry to the scoreboard.  Entries that cover the
 
573
 * same line range have been removed from the scoreboard previously.
 
574
 */
474
575
static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e)
475
576
{
476
577
        struct blame_entry *ent, *prev = NULL;
494
595
                e->next->prev = e;
495
596
}
496
597
 
 
598
/*
 
599
 * src typically is on-stack; we want to copy the information in it to
 
600
 * an malloced blame_entry that is already on the linked list of the
 
601
 * scoreboard.  The origin of dst loses a refcnt while the origin of src
 
602
 * gains one.
 
603
 */
497
604
static void dup_entry(struct blame_entry *dst, struct blame_entry *src)
498
605
{
499
606
        struct blame_entry *p, *n;
513
620
        return sb->final_buf + sb->lineno[lno];
514
621
}
515
622
 
 
623
/*
 
624
 * It is known that lines between tlno to same came from parent, and e
 
625
 * has an overlap with that range.  it also is known that parent's
 
626
 * line plno corresponds to e's line tlno.
 
627
 *
 
628
 *                <---- e ----->
 
629
 *                   <------>
 
630
 *                   <------------>
 
631
 *             <------------>
 
632
 *             <------------------>
 
633
 *
 
634
 * Split e into potentially three parts; before this chunk, the chunk
 
635
 * to be blamed for the parent, and after that portion.
 
636
 */
516
637
static void split_overlap(struct blame_entry *split,
517
638
                          struct blame_entry *e,
518
639
                          int tlno, int plno, int same,
519
640
                          struct origin *parent)
520
641
{
521
 
        /* it is known that lines between tlno to same came from
522
 
         * parent, and e has an overlap with that range.  it also is
523
 
         * known that parent's line plno corresponds to e's line tlno.
524
 
         *
525
 
         *                <---- e ----->
526
 
         *                   <------>
527
 
         *                   <------------>
528
 
         *             <------------>
529
 
         *             <------------------>
530
 
         *
531
 
         * Potentially we need to split e into three parts; before
532
 
         * this chunk, the chunk to be blamed for parent, and after
533
 
         * that portion.
534
 
         */
535
642
        int chunk_end_lno;
536
643
        memset(split, 0, sizeof(struct blame_entry [3]));
537
644
 
561
668
                chunk_end_lno = e->lno + e->num_lines;
562
669
        split[1].num_lines = chunk_end_lno - split[1].lno;
563
670
 
 
671
        /*
 
672
         * if it turns out there is nothing to blame the parent for,
 
673
         * forget about the splitting.  !split[1].suspect signals this.
 
674
         */
564
675
        if (split[1].num_lines < 1)
565
676
                return;
566
677
        split[1].suspect = origin_incref(parent);
567
678
}
568
679
 
 
680
/*
 
681
 * split_overlap() divided an existing blame e into up to three parts
 
682
 * in split.  Adjust the linked list of blames in the scoreboard to
 
683
 * reflect the split.
 
684
 */
569
685
static void split_blame(struct scoreboard *sb,
570
686
                        struct blame_entry *split,
571
687
                        struct blame_entry *e)
573
689
        struct blame_entry *new_entry;
574
690
 
575
691
        if (split[0].suspect && split[2].suspect) {
576
 
                /* we need to split e into two and add another for parent */
 
692
                /* The first part (reuse storage for the existing entry e) */
577
693
                dup_entry(e, &split[0]);
578
694
 
 
695
                /* The last part -- me */
579
696
                new_entry = xmalloc(sizeof(*new_entry));
580
697
                memcpy(new_entry, &(split[2]), sizeof(struct blame_entry));
581
698
                add_blame_entry(sb, new_entry);
582
699
 
 
700
                /* ... and the middle part -- parent */
583
701
                new_entry = xmalloc(sizeof(*new_entry));
584
702
                memcpy(new_entry, &(split[1]), sizeof(struct blame_entry));
585
703
                add_blame_entry(sb, new_entry);
586
704
        }
587
705
        else if (!split[0].suspect && !split[2].suspect)
588
 
                /* parent covers the entire area */
 
706
                /*
 
707
                 * The parent covers the entire area; reuse storage for
 
708
                 * e and replace it with the parent.
 
709
                 */
589
710
                dup_entry(e, &split[1]);
590
711
        else if (split[0].suspect) {
 
712
                /* me and then parent */
591
713
                dup_entry(e, &split[0]);
592
714
 
593
715
                new_entry = xmalloc(sizeof(*new_entry));
595
717
                add_blame_entry(sb, new_entry);
596
718
        }
597
719
        else {
 
720
                /* parent and then me */
598
721
                dup_entry(e, &split[1]);
599
722
 
600
723
                new_entry = xmalloc(sizeof(*new_entry));
625
748
        }
626
749
}
627
750
 
 
751
/*
 
752
 * After splitting the blame, the origins used by the
 
753
 * on-stack blame_entry should lose one refcnt each.
 
754
 */
628
755
static void decref_split(struct blame_entry *split)
629
756
{
630
757
        int i;
633
760
                origin_decref(split[i].suspect);
634
761
}
635
762
 
 
763
/*
 
764
 * Helper for blame_chunk().  blame_entry e is known to overlap with
 
765
 * the patch hunk; split it and pass blame to the parent.
 
766
 */
636
767
static void blame_overlap(struct scoreboard *sb, struct blame_entry *e,
637
768
                          int tlno, int plno, int same,
638
769
                          struct origin *parent)
645
776
        decref_split(split);
646
777
}
647
778
 
 
779
/*
 
780
 * Find the line number of the last line the target is suspected for.
 
781
 */
648
782
static int find_last_in_target(struct scoreboard *sb, struct origin *target)
649
783
{
650
784
        struct blame_entry *e;
651
785
        int last_in_target = -1;
652
786
 
653
787
        for (e = sb->ent; e; e = e->next) {
654
 
                if (e->guilty || cmp_suspect(e->suspect, target))
 
788
                if (e->guilty || !same_suspect(e->suspect, target))
655
789
                        continue;
656
790
                if (last_in_target < e->s_lno + e->num_lines)
657
791
                        last_in_target = e->s_lno + e->num_lines;
659
793
        return last_in_target;
660
794
}
661
795
 
 
796
/*
 
797
 * Process one hunk from the patch between the current suspect for
 
798
 * blame_entry e and its parent.  Find and split the overlap, and
 
799
 * pass blame to the overlapping part to the parent.
 
800
 */
662
801
static void blame_chunk(struct scoreboard *sb,
663
802
                        int tlno, int plno, int same,
664
803
                        struct origin *target, struct origin *parent)
666
805
        struct blame_entry *e;
667
806
 
668
807
        for (e = sb->ent; e; e = e->next) {
669
 
                if (e->guilty || cmp_suspect(e->suspect, target))
 
808
                if (e->guilty || !same_suspect(e->suspect, target))
670
809
                        continue;
671
810
                if (same <= e->s_lno)
672
811
                        continue;
675
814
        }
676
815
}
677
816
 
 
817
/*
 
818
 * We are looking at the origin 'target' and aiming to pass blame
 
819
 * for the lines it is suspected to its parent.  Run diff to find
 
820
 * which lines came from parent and pass blame for them.
 
821
 */
678
822
static int pass_blame_to_parent(struct scoreboard *sb,
679
823
                                struct origin *target,
680
824
                                struct origin *parent)
695
839
                plno = chunk->p_next;
696
840
                tlno = chunk->t_next;
697
841
        }
698
 
        /* rest (i.e. anything above tlno) are the same as parent */
 
842
        /* The rest (i.e. anything after tlno) are the same as the parent */
699
843
        blame_chunk(sb, tlno, plno, last_in_target, target, parent);
700
844
 
701
845
        free_patch(patch);
702
846
        return 0;
703
847
}
704
848
 
 
849
/*
 
850
 * The lines in blame_entry after splitting blames many times can become
 
851
 * very small and trivial, and at some point it becomes pointless to
 
852
 * blame the parents.  E.g. "\t\t}\n\t}\n\n" appears everywhere in any
 
853
 * ordinary C program, and it is not worth to say it was copied from
 
854
 * totally unrelated file in the parent.
 
855
 *
 
856
 * Compute how trivial the lines in the blame_entry are.
 
857
 */
705
858
static unsigned ent_score(struct scoreboard *sb, struct blame_entry *e)
706
859
{
707
860
        unsigned score;
723
876
        return score;
724
877
}
725
878
 
 
879
/*
 
880
 * best_so_far[] and this[] are both a split of an existing blame_entry
 
881
 * that passes blame to the parent.  Maintain best_so_far the best split
 
882
 * so far, by comparing this and best_so_far and copying this into
 
883
 * bst_so_far as needed.
 
884
 */
726
885
static void copy_split_if_better(struct scoreboard *sb,
727
886
                                 struct blame_entry *best_so_far,
728
887
                                 struct blame_entry *this)
742
901
        memcpy(best_so_far, this, sizeof(struct blame_entry [3]));
743
902
}
744
903
 
 
904
/*
 
905
 * We are looking at a part of the final image represented by
 
906
 * ent (tlno and same are offset by ent->s_lno).
 
907
 * tlno is where we are looking at in the final image.
 
908
 * up to (but not including) same match preimage.
 
909
 * plno is where we are looking at in the preimage.
 
910
 *
 
911
 * <-------------- final image ---------------------->
 
912
 *       <------ent------>
 
913
 *         ^tlno ^same
 
914
 *    <---------preimage----->
 
915
 *         ^plno
 
916
 *
 
917
 * All line numbers are 0-based.
 
918
 */
 
919
static void handle_split(struct scoreboard *sb,
 
920
                         struct blame_entry *ent,
 
921
                         int tlno, int plno, int same,
 
922
                         struct origin *parent,
 
923
                         struct blame_entry *split)
 
924
{
 
925
        if (ent->num_lines <= tlno)
 
926
                return;
 
927
        if (tlno < same) {
 
928
                struct blame_entry this[3];
 
929
                tlno += ent->s_lno;
 
930
                same += ent->s_lno;
 
931
                split_overlap(this, ent, tlno, plno, same, parent);
 
932
                copy_split_if_better(sb, split, this);
 
933
                decref_split(this);
 
934
        }
 
935
}
 
936
 
 
937
/*
 
938
 * Find the lines from parent that are the same as ent so that
 
939
 * we can pass blames to it.  file_p has the blob contents for
 
940
 * the parent.
 
941
 */
745
942
static void find_copy_in_blob(struct scoreboard *sb,
746
943
                              struct blame_entry *ent,
747
944
                              struct origin *parent,
754
951
        struct patch *patch;
755
952
        int i, plno, tlno;
756
953
 
 
954
        /*
 
955
         * Prepare mmfile that contains only the lines in ent.
 
956
         */
757
957
        cp = nth_line(sb, ent->lno);
758
958
        file_o.ptr = (char*) cp;
759
959
        cnt = ent->num_lines;
766
966
 
767
967
        patch = compare_buffer(file_p, &file_o, 1);
768
968
 
 
969
        /*
 
970
         * file_o is a part of final image we are annotating.
 
971
         * file_p partially may match that image.
 
972
         */
769
973
        memset(split, 0, sizeof(struct blame_entry [3]));
770
974
        plno = tlno = 0;
771
975
        for (i = 0; i < patch->num; i++) {
772
976
                struct chunk *chunk = &patch->chunks[i];
773
977
 
774
 
                /* tlno to chunk->same are the same as ent */
775
 
                if (ent->num_lines <= tlno)
776
 
                        break;
777
 
                if (tlno < chunk->same) {
778
 
                        struct blame_entry this[3];
779
 
                        split_overlap(this, ent,
780
 
                                      tlno + ent->s_lno, plno,
781
 
                                      chunk->same + ent->s_lno,
782
 
                                      parent);
783
 
                        copy_split_if_better(sb, split, this);
784
 
                        decref_split(this);
785
 
                }
 
978
                handle_split(sb, ent, tlno, plno, chunk->same, parent, split);
786
979
                plno = chunk->p_next;
787
980
                tlno = chunk->t_next;
788
981
        }
 
982
        /* remainder, if any, all match the preimage */
 
983
        handle_split(sb, ent, tlno, plno, ent->num_lines, parent, split);
789
984
        free_patch(patch);
790
985
}
791
986
 
 
987
/*
 
988
 * See if lines currently target is suspected for can be attributed to
 
989
 * parent.
 
990
 */
792
991
static int find_move_in_parent(struct scoreboard *sb,
793
992
                               struct origin *target,
794
993
                               struct origin *parent)
809
1008
        while (made_progress) {
810
1009
                made_progress = 0;
811
1010
                for (e = sb->ent; e; e = e->next) {
812
 
                        if (e->guilty || cmp_suspect(e->suspect, target))
 
1011
                        if (e->guilty || !same_suspect(e->suspect, target))
813
1012
                                continue;
814
1013
                        find_copy_in_blob(sb, e, parent, split, &file_p);
815
1014
                        if (split[1].suspect &&
823
1022
        return 0;
824
1023
}
825
1024
 
826
 
 
827
1025
struct blame_list {
828
1026
        struct blame_entry *ent;
829
1027
        struct blame_entry split[3];
830
1028
};
831
1029
 
 
1030
/*
 
1031
 * Count the number of entries the target is suspected for,
 
1032
 * and prepare a list of entry and the best split.
 
1033
 */
832
1034
static struct blame_list *setup_blame_list(struct scoreboard *sb,
833
1035
                                           struct origin *target,
834
1036
                                           int *num_ents_p)
837
1039
        int num_ents, i;
838
1040
        struct blame_list *blame_list = NULL;
839
1041
 
840
 
        /* Count the number of entries the target is suspected for,
841
 
         * and prepare a list of entry and the best split.
842
 
         */
843
1042
        for (e = sb->ent, num_ents = 0; e; e = e->next)
844
 
                if (!e->guilty && !cmp_suspect(e->suspect, target))
 
1043
                if (!e->guilty && same_suspect(e->suspect, target))
845
1044
                        num_ents++;
846
1045
        if (num_ents) {
847
1046
                blame_list = xcalloc(num_ents, sizeof(struct blame_list));
848
1047
                for (e = sb->ent, i = 0; e; e = e->next)
849
 
                        if (!e->guilty && !cmp_suspect(e->suspect, target))
 
1048
                        if (!e->guilty && same_suspect(e->suspect, target))
850
1049
                                blame_list[i++].ent = e;
851
1050
        }
852
1051
        *num_ents_p = num_ents;
853
1052
        return blame_list;
854
1053
}
855
1054
 
 
1055
/*
 
1056
 * For lines target is suspected for, see if we can find code movement
 
1057
 * across file boundary from the parent commit.  porigin is the path
 
1058
 * in the parent we already tried.
 
1059
 */
856
1060
static int find_copy_in_parent(struct scoreboard *sb,
857
1061
                               struct origin *target,
858
1062
                               struct commit *parent,
886
1090
         * and this code needs to be after diff_setup_done(), which
887
1091
         * usually makes find-copies-harder imply copy detection.
888
1092
         */
889
 
        if ((opt & PICKAXE_BLAME_COPY_HARDER) &&
890
 
            (!porigin || strcmp(target->path, porigin->path)))
 
1093
        if ((opt & PICKAXE_BLAME_COPY_HARDEST)
 
1094
            || ((opt & PICKAXE_BLAME_COPY_HARDER)
 
1095
                && (!porigin || strcmp(target->path, porigin->path))))
891
1096
                diff_opts.find_copies_harder = 1;
892
1097
 
893
 
        diff_tree_sha1(parent->tree->object.sha1,
894
 
                       target->commit->tree->object.sha1,
895
 
                       "", &diff_opts);
 
1098
        if (is_null_sha1(target->commit->object.sha1))
 
1099
                do_diff_cache(parent->tree->object.sha1, &diff_opts);
 
1100
        else
 
1101
                diff_tree_sha1(parent->tree->object.sha1,
 
1102
                               target->commit->tree->object.sha1,
 
1103
                               "", &diff_opts);
896
1104
 
897
1105
        if (!diff_opts.find_copies_harder)
898
1106
                diffcore_std(&diff_opts);
953
1161
        return retval;
954
1162
}
955
1163
 
956
 
/* The blobs of origin and porigin exactly match, so everything
 
1164
/*
 
1165
 * The blobs of origin and porigin exactly match, so everything
957
1166
 * origin is suspected for can be blamed on the parent.
958
1167
 */
959
1168
static void pass_whole_blame(struct scoreboard *sb,
967
1176
                origin->file.ptr = NULL;
968
1177
        }
969
1178
        for (e = sb->ent; e; e = e->next) {
970
 
                if (cmp_suspect(e->suspect, origin))
 
1179
                if (!same_suspect(e->suspect, origin))
971
1180
                        continue;
972
1181
                origin_incref(porigin);
973
1182
                origin_decref(e->suspect);
1038
1247
        }
1039
1248
 
1040
1249
        /*
1041
 
         * Optionally run "miff" to find moves in parents' files here.
 
1250
         * Optionally find moves in parents' files.
1042
1251
         */
1043
1252
        if (opt & PICKAXE_BLAME_MOVE)
1044
1253
                for (i = 0, parent = commit->parents;
1052
1261
                }
1053
1262
 
1054
1263
        /*
1055
 
         * Optionally run "ciff" to find copies from parents' files here.
 
1264
         * Optionally find copies from parents' files.
1056
1265
         */
1057
1266
        if (opt & PICKAXE_BLAME_COPY)
1058
1267
                for (i = 0, parent = commit->parents;
1069
1278
                origin_decref(parent_origin[i]);
1070
1279
}
1071
1280
 
1072
 
static void assign_blame(struct scoreboard *sb, struct rev_info *revs, int opt)
1073
 
{
1074
 
        while (1) {
1075
 
                struct blame_entry *ent;
1076
 
                struct commit *commit;
1077
 
                struct origin *suspect = NULL;
1078
 
 
1079
 
                /* find one suspect to break down */
1080
 
                for (ent = sb->ent; !suspect && ent; ent = ent->next)
1081
 
                        if (!ent->guilty)
1082
 
                                suspect = ent->suspect;
1083
 
                if (!suspect)
1084
 
                        return; /* all done */
1085
 
 
1086
 
                origin_incref(suspect);
1087
 
                commit = suspect->commit;
1088
 
                if (!commit->object.parsed)
1089
 
                        parse_commit(commit);
1090
 
                if (!(commit->object.flags & UNINTERESTING) &&
1091
 
                    !(revs->max_age != -1 && commit->date  < revs->max_age))
1092
 
                        pass_blame(sb, suspect, opt);
1093
 
 
1094
 
                /* Take responsibility for the remaining entries */
1095
 
                for (ent = sb->ent; ent; ent = ent->next)
1096
 
                        if (!cmp_suspect(ent->suspect, suspect))
1097
 
                                ent->guilty = 1;
1098
 
                origin_decref(suspect);
1099
 
 
1100
 
                if (DEBUG) /* sanity */
1101
 
                        sanity_check_refcnt(sb);
1102
 
        }
1103
 
}
1104
 
 
1105
 
static const char *format_time(unsigned long time, const char *tz_str,
1106
 
                               int show_raw_time)
1107
 
{
1108
 
        static char time_buf[128];
1109
 
        time_t t = time;
1110
 
        int minutes, tz;
1111
 
        struct tm *tm;
1112
 
 
1113
 
        if (show_raw_time) {
1114
 
                sprintf(time_buf, "%lu %s", time, tz_str);
1115
 
                return time_buf;
1116
 
        }
1117
 
 
1118
 
        tz = atoi(tz_str);
1119
 
        minutes = tz < 0 ? -tz : tz;
1120
 
        minutes = (minutes / 100)*60 + (minutes % 100);
1121
 
        minutes = tz < 0 ? -minutes : minutes;
1122
 
        t = time + minutes * 60;
1123
 
        tm = gmtime(&t);
1124
 
 
1125
 
        strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", tm);
1126
 
        strcat(time_buf, tz_str);
1127
 
        return time_buf;
1128
 
}
1129
 
 
 
1281
/*
 
1282
 * Information on commits, used for output.
 
1283
 */
1130
1284
struct commit_info
1131
1285
{
1132
 
        char *author;
1133
 
        char *author_mail;
 
1286
        const char *author;
 
1287
        const char *author_mail;
1134
1288
        unsigned long author_time;
1135
 
        char *author_tz;
 
1289
        const char *author_tz;
1136
1290
 
1137
1291
        /* filled only when asked for details */
1138
 
        char *committer;
1139
 
        char *committer_mail;
 
1292
        const char *committer;
 
1293
        const char *committer_mail;
1140
1294
        unsigned long committer_time;
1141
 
        char *committer_tz;
 
1295
        const char *committer_tz;
1142
1296
 
1143
 
        char *summary;
 
1297
        const char *summary;
1144
1298
};
1145
1299
 
 
1300
/*
 
1301
 * Parse author/committer line in the commit object buffer
 
1302
 */
1146
1303
static void get_ac_line(const char *inbuf, const char *what,
1147
 
                        int bufsz, char *person, char **mail,
1148
 
                        unsigned long *time, char **tz)
 
1304
                        int bufsz, char *person, const char **mail,
 
1305
                        unsigned long *time, const char **tz)
1149
1306
{
1150
 
        int len;
1151
 
        char *tmp, *endp;
 
1307
        int len, tzlen, maillen;
 
1308
        char *tmp, *endp, *timepos;
1152
1309
 
1153
1310
        tmp = strstr(inbuf, what);
1154
1311
        if (!tmp)
1162
1319
        if (bufsz <= len) {
1163
1320
        error_out:
1164
1321
                /* Ugh */
1165
 
                person = *mail = *tz = "(unknown)";
 
1322
                *mail = *tz = "(unknown)";
1166
1323
                *time = 0;
1167
1324
                return;
1168
1325
        }
1174
1331
        while (*tmp != ' ')
1175
1332
                tmp--;
1176
1333
        *tz = tmp+1;
 
1334
        tzlen = (person+len)-(tmp+1);
1177
1335
 
1178
1336
        *tmp = 0;
1179
1337
        while (*tmp != ' ')
1180
1338
                tmp--;
1181
1339
        *time = strtoul(tmp, NULL, 10);
 
1340
        timepos = tmp;
1182
1341
 
1183
1342
        *tmp = 0;
1184
1343
        while (*tmp != ' ')
1185
1344
                tmp--;
1186
1345
        *mail = tmp + 1;
1187
1346
        *tmp = 0;
 
1347
        maillen = timepos - tmp;
 
1348
 
 
1349
        if (!mailmap.nr)
 
1350
                return;
 
1351
 
 
1352
        /*
 
1353
         * mailmap expansion may make the name longer.
 
1354
         * make room by pushing stuff down.
 
1355
         */
 
1356
        tmp = person + bufsz - (tzlen + 1);
 
1357
        memmove(tmp, *tz, tzlen);
 
1358
        tmp[tzlen] = 0;
 
1359
        *tz = tmp;
 
1360
 
 
1361
        tmp = tmp - (maillen + 1);
 
1362
        memmove(tmp, *mail, maillen);
 
1363
        tmp[maillen] = 0;
 
1364
        *mail = tmp;
 
1365
 
 
1366
        /*
 
1367
         * Now, convert e-mail using mailmap
 
1368
         */
 
1369
        map_email(&mailmap, tmp + 1, person, tmp-person-1);
1188
1370
}
1189
1371
 
1190
1372
static void get_commit_info(struct commit *commit,
1197
1379
        static char committer_buf[1024];
1198
1380
        static char summary_buf[1024];
1199
1381
 
1200
 
        /* We've operated without save_commit_buffer, so
 
1382
        /*
 
1383
         * We've operated without save_commit_buffer, so
1201
1384
         * we now need to populate them for output.
1202
1385
         */
1203
1386
        if (!commit->buffer) {
1204
 
                char type[20];
 
1387
                enum object_type type;
1205
1388
                unsigned long size;
1206
1389
                commit->buffer =
1207
 
                        read_sha1_file(commit->object.sha1, type, &size);
 
1390
                        read_sha1_file(commit->object.sha1, &type, &size);
 
1391
                if (!commit->buffer)
 
1392
                        die("Cannot read commit %s",
 
1393
                            sha1_to_hex(commit->object.sha1));
1208
1394
        }
1209
1395
        ret->author = author_buf;
1210
1396
        get_ac_line(commit->buffer, "\nauthor ",
1229
1415
        tmp += 2;
1230
1416
        endp = strchr(tmp, '\n');
1231
1417
        if (!endp)
1232
 
                goto error_out;
 
1418
                endp = tmp + strlen(tmp);
1233
1419
        len = endp - tmp;
1234
 
        if (len >= sizeof(summary_buf))
 
1420
        if (len >= sizeof(summary_buf) || len == 0)
1235
1421
                goto error_out;
1236
1422
        memcpy(summary_buf, tmp, len);
1237
1423
        summary_buf[len] = 0;
1238
1424
}
1239
1425
 
 
1426
/*
 
1427
 * To allow LF and other nonportable characters in pathnames,
 
1428
 * they are c-style quoted as needed.
 
1429
 */
 
1430
static void write_filename_info(const char *path)
 
1431
{
 
1432
        printf("filename ");
 
1433
        write_name_quoted(NULL, 0, path, 1, stdout);
 
1434
        putchar('\n');
 
1435
}
 
1436
 
 
1437
/*
 
1438
 * The blame_entry is found to be guilty for the range.  Mark it
 
1439
 * as such, and show it in incremental output.
 
1440
 */
 
1441
static void found_guilty_entry(struct blame_entry *ent)
 
1442
{
 
1443
        if (ent->guilty)
 
1444
                return;
 
1445
        ent->guilty = 1;
 
1446
        if (incremental) {
 
1447
                struct origin *suspect = ent->suspect;
 
1448
 
 
1449
                printf("%s %d %d %d\n",
 
1450
                       sha1_to_hex(suspect->commit->object.sha1),
 
1451
                       ent->s_lno + 1, ent->lno + 1, ent->num_lines);
 
1452
                if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
 
1453
                        struct commit_info ci;
 
1454
                        suspect->commit->object.flags |= METAINFO_SHOWN;
 
1455
                        get_commit_info(suspect->commit, &ci, 1);
 
1456
                        printf("author %s\n", ci.author);
 
1457
                        printf("author-mail %s\n", ci.author_mail);
 
1458
                        printf("author-time %lu\n", ci.author_time);
 
1459
                        printf("author-tz %s\n", ci.author_tz);
 
1460
                        printf("committer %s\n", ci.committer);
 
1461
                        printf("committer-mail %s\n", ci.committer_mail);
 
1462
                        printf("committer-time %lu\n", ci.committer_time);
 
1463
                        printf("committer-tz %s\n", ci.committer_tz);
 
1464
                        printf("summary %s\n", ci.summary);
 
1465
                        if (suspect->commit->object.flags & UNINTERESTING)
 
1466
                                printf("boundary\n");
 
1467
                }
 
1468
                write_filename_info(suspect->path);
 
1469
                maybe_flush_or_die(stdout, "stdout");
 
1470
        }
 
1471
}
 
1472
 
 
1473
/*
 
1474
 * The main loop -- while the scoreboard has lines whose true origin
 
1475
 * is still unknown, pick one blame_entry, and allow its current
 
1476
 * suspect to pass blames to its parents.
 
1477
 */
 
1478
static void assign_blame(struct scoreboard *sb, struct rev_info *revs, int opt)
 
1479
{
 
1480
        while (1) {
 
1481
                struct blame_entry *ent;
 
1482
                struct commit *commit;
 
1483
                struct origin *suspect = NULL;
 
1484
 
 
1485
                /* find one suspect to break down */
 
1486
                for (ent = sb->ent; !suspect && ent; ent = ent->next)
 
1487
                        if (!ent->guilty)
 
1488
                                suspect = ent->suspect;
 
1489
                if (!suspect)
 
1490
                        return; /* all done */
 
1491
 
 
1492
                /*
 
1493
                 * We will use this suspect later in the loop,
 
1494
                 * so hold onto it in the meantime.
 
1495
                 */
 
1496
                origin_incref(suspect);
 
1497
                commit = suspect->commit;
 
1498
                if (!commit->object.parsed)
 
1499
                        parse_commit(commit);
 
1500
                if (!(commit->object.flags & UNINTERESTING) &&
 
1501
                    !(revs->max_age != -1 && commit->date < revs->max_age))
 
1502
                        pass_blame(sb, suspect, opt);
 
1503
                else {
 
1504
                        commit->object.flags |= UNINTERESTING;
 
1505
                        if (commit->object.parsed)
 
1506
                                mark_parents_uninteresting(commit);
 
1507
                }
 
1508
                /* treat root commit as boundary */
 
1509
                if (!commit->parents && !show_root)
 
1510
                        commit->object.flags |= UNINTERESTING;
 
1511
 
 
1512
                /* Take responsibility for the remaining entries */
 
1513
                for (ent = sb->ent; ent; ent = ent->next)
 
1514
                        if (same_suspect(ent->suspect, suspect))
 
1515
                                found_guilty_entry(ent);
 
1516
                origin_decref(suspect);
 
1517
 
 
1518
                if (DEBUG) /* sanity */
 
1519
                        sanity_check_refcnt(sb);
 
1520
        }
 
1521
}
 
1522
 
 
1523
static const char *format_time(unsigned long time, const char *tz_str,
 
1524
                               int show_raw_time)
 
1525
{
 
1526
        static char time_buf[128];
 
1527
        time_t t = time;
 
1528
        int minutes, tz;
 
1529
        struct tm *tm;
 
1530
 
 
1531
        if (show_raw_time) {
 
1532
                sprintf(time_buf, "%lu %s", time, tz_str);
 
1533
                return time_buf;
 
1534
        }
 
1535
 
 
1536
        tz = atoi(tz_str);
 
1537
        minutes = tz < 0 ? -tz : tz;
 
1538
        minutes = (minutes / 100)*60 + (minutes % 100);
 
1539
        minutes = tz < 0 ? -minutes : minutes;
 
1540
        t = time + minutes * 60;
 
1541
        tm = gmtime(&t);
 
1542
 
 
1543
        strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", tm);
 
1544
        strcat(time_buf, tz_str);
 
1545
        return time_buf;
 
1546
}
 
1547
 
1240
1548
#define OUTPUT_ANNOTATE_COMPAT  001
1241
1549
#define OUTPUT_LONG_OBJECT_NAME 002
1242
1550
#define OUTPUT_RAW_TIMESTAMP    004
1244
1552
#define OUTPUT_SHOW_NAME        020
1245
1553
#define OUTPUT_SHOW_NUMBER      040
1246
1554
#define OUTPUT_SHOW_SCORE      0100
 
1555
#define OUTPUT_NO_AUTHOR       0200
1247
1556
 
1248
1557
static void emit_porcelain(struct scoreboard *sb, struct blame_entry *ent)
1249
1558
{
1271
1580
                printf("committer-mail %s\n", ci.committer_mail);
1272
1581
                printf("committer-time %lu\n", ci.committer_time);
1273
1582
                printf("committer-tz %s\n", ci.committer_tz);
1274
 
                printf("filename %s\n", suspect->path);
 
1583
                write_filename_info(suspect->path);
1275
1584
                printf("summary %s\n", ci.summary);
 
1585
                if (suspect->commit->object.flags & UNINTERESTING)
 
1586
                        printf("boundary\n");
1276
1587
        }
1277
1588
        else if (suspect->commit->object.flags & MORE_THAN_ONE_PATH)
1278
 
                printf("filename %s\n", suspect->path);
 
1589
                write_filename_info(suspect->path);
1279
1590
 
1280
1591
        cp = nth_line(sb, ent->lno);
1281
1592
        for (cnt = 0; cnt < ent->num_lines; cnt++) {
1308
1619
        cp = nth_line(sb, ent->lno);
1309
1620
        for (cnt = 0; cnt < ent->num_lines; cnt++) {
1310
1621
                char ch;
1311
 
 
1312
 
                printf("%.*s", (opt & OUTPUT_LONG_OBJECT_NAME) ? 40 : 8, hex);
 
1622
                int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? 40 : 8;
 
1623
 
 
1624
                if (suspect->commit->object.flags & UNINTERESTING) {
 
1625
                        if (blank_boundary)
 
1626
                                memset(hex, ' ', length);
 
1627
                        else if (!cmd_is_annotate) {
 
1628
                                length--;
 
1629
                                putchar('^');
 
1630
                        }
 
1631
                }
 
1632
 
 
1633
                printf("%.*s", length, hex);
1313
1634
                if (opt & OUTPUT_ANNOTATE_COMPAT)
1314
1635
                        printf("\t(%10s\t%10s\t%d)", ci.author,
1315
1636
                               format_time(ci.author_time, ci.author_tz,
1326
1647
                        if (opt & OUTPUT_SHOW_NUMBER)
1327
1648
                                printf(" %*d", max_orig_digits,
1328
1649
                                       ent->s_lno + 1 + cnt);
1329
 
                        printf(" (%-*.*s %10s %*d) ",
1330
 
                               longest_author, longest_author, ci.author,
1331
 
                               format_time(ci.author_time, ci.author_tz,
1332
 
                                           show_raw_time),
 
1650
 
 
1651
                        if (!(opt & OUTPUT_NO_AUTHOR))
 
1652
                                printf(" (%-*.*s %10s",
 
1653
                                       longest_author, longest_author,
 
1654
                                       ci.author,
 
1655
                                       format_time(ci.author_time,
 
1656
                                                   ci.author_tz,
 
1657
                                                   show_raw_time));
 
1658
                        printf(" %*d) ",
1333
1659
                               max_digits, ent->lno + 1 + cnt);
1334
1660
                }
1335
1661
                do {
1370
1696
        }
1371
1697
}
1372
1698
 
 
1699
/*
 
1700
 * To allow quick access to the contents of nth line in the
 
1701
 * final image, prepare an index in the scoreboard.
 
1702
 */
1373
1703
static int prepare_lines(struct scoreboard *sb)
1374
1704
{
1375
1705
        const char *buf = sb->final_buf;
1397
1727
        return sb->num_lines;
1398
1728
}
1399
1729
 
 
1730
/*
 
1731
 * Add phony grafts for use with -S; this is primarily to
 
1732
 * support git-cvsserver that wants to give a linear history
 
1733
 * to its clients.
 
1734
 */
1400
1735
static int read_ancestry(const char *graft_file)
1401
1736
{
1402
1737
        FILE *fp = fopen(graft_file, "r");
1414
1749
        return 0;
1415
1750
}
1416
1751
 
 
1752
/*
 
1753
 * How many columns do we need to show line numbers in decimal?
 
1754
 */
1417
1755
static int lineno_width(int lines)
1418
1756
{
1419
 
        int i, width;
 
1757
        int i, width;
1420
1758
 
1421
 
        for (width = 1, i = 10; i <= lines + 1; width++)
1422
 
                i *= 10;
1423
 
        return width;
 
1759
        for (width = 1, i = 10; i <= lines + 1; width++)
 
1760
                i *= 10;
 
1761
        return width;
1424
1762
}
1425
1763
 
 
1764
/*
 
1765
 * How many columns do we need to show line numbers, authors,
 
1766
 * and filenames?
 
1767
 */
1426
1768
static void find_alignment(struct scoreboard *sb, int *option)
1427
1769
{
1428
1770
        int longest_src_lines = 0;
1461
1803
        max_score_digits = lineno_width(largest_score);
1462
1804
}
1463
1805
 
 
1806
/*
 
1807
 * For debugging -- origin is refcounted, and this asserts that
 
1808
 * we do not underflow.
 
1809
 */
1464
1810
static void sanity_check_refcnt(struct scoreboard *sb)
1465
1811
{
1466
1812
        int baa = 0;
1482
1828
                        ent->suspect->refcnt = -ent->suspect->refcnt;
1483
1829
        }
1484
1830
        for (ent = sb->ent; ent; ent = ent->next) {
1485
 
                /* then pick each and see if they have the the correct
1486
 
                 * refcnt.
 
1831
                /*
 
1832
                 * ... then pick each and see if they have the the
 
1833
                 * correct refcnt.
1487
1834
                 */
1488
1835
                int found;
1489
1836
                struct blame_entry *e;
1513
1860
        }
1514
1861
}
1515
1862
 
 
1863
/*
 
1864
 * Used for the command line parsing; check if the path exists
 
1865
 * in the working tree.
 
1866
 */
1516
1867
static int has_path_in_work_tree(const char *path)
1517
1868
{
1518
1869
        struct stat st;
1535
1886
        return prefix_path(prefix, strlen(prefix), path);
1536
1887
}
1537
1888
 
 
1889
/*
 
1890
 * Parsing of (comma separated) one item in the -L option
 
1891
 */
1538
1892
static const char *parse_loc(const char *spec,
1539
1893
                             struct scoreboard *sb, long lno,
1540
1894
                             long begin, long *ret)
1609
1963
        }
1610
1964
}
1611
1965
 
 
1966
/*
 
1967
 * Parsing of -L option
 
1968
 */
1612
1969
static void prepare_blame_range(struct scoreboard *sb,
1613
1970
                                const char *bottomtop,
1614
1971
                                long lno,
1626
1983
                usage(blame_usage);
1627
1984
}
1628
1985
 
 
1986
static int git_blame_config(const char *var, const char *value)
 
1987
{
 
1988
        if (!strcmp(var, "blame.showroot")) {
 
1989
                show_root = git_config_bool(var, value);
 
1990
                return 0;
 
1991
        }
 
1992
        if (!strcmp(var, "blame.blankboundary")) {
 
1993
                blank_boundary = git_config_bool(var, value);
 
1994
                return 0;
 
1995
        }
 
1996
        return git_default_config(var, value);
 
1997
}
 
1998
 
 
1999
static struct commit *fake_working_tree_commit(const char *path, const char *contents_from)
 
2000
{
 
2001
        struct commit *commit;
 
2002
        struct origin *origin;
 
2003
        unsigned char head_sha1[20];
 
2004
        char *buf;
 
2005
        const char *ident;
 
2006
        int fd;
 
2007
        time_t now;
 
2008
        unsigned long fin_size;
 
2009
        int size, len;
 
2010
        struct cache_entry *ce;
 
2011
        unsigned mode;
 
2012
 
 
2013
        if (get_sha1("HEAD", head_sha1))
 
2014
                die("No such ref: HEAD");
 
2015
 
 
2016
        time(&now);
 
2017
        commit = xcalloc(1, sizeof(*commit));
 
2018
        commit->parents = xcalloc(1, sizeof(*commit->parents));
 
2019
        commit->parents->item = lookup_commit_reference(head_sha1);
 
2020
        commit->object.parsed = 1;
 
2021
        commit->date = now;
 
2022
        commit->object.type = OBJ_COMMIT;
 
2023
 
 
2024
        origin = make_origin(commit, path);
 
2025
 
 
2026
        if (!contents_from || strcmp("-", contents_from)) {
 
2027
                struct stat st;
 
2028
                const char *read_from;
 
2029
 
 
2030
                if (contents_from) {
 
2031
                        if (stat(contents_from, &st) < 0)
 
2032
                                die("Cannot stat %s", contents_from);
 
2033
                        read_from = contents_from;
 
2034
                }
 
2035
                else {
 
2036
                        if (lstat(path, &st) < 0)
 
2037
                                die("Cannot lstat %s", path);
 
2038
                        read_from = path;
 
2039
                }
 
2040
                fin_size = xsize_t(st.st_size);
 
2041
                buf = xmalloc(fin_size+1);
 
2042
                mode = canon_mode(st.st_mode);
 
2043
                switch (st.st_mode & S_IFMT) {
 
2044
                case S_IFREG:
 
2045
                        fd = open(read_from, O_RDONLY);
 
2046
                        if (fd < 0)
 
2047
                                die("cannot open %s", read_from);
 
2048
                        if (read_in_full(fd, buf, fin_size) != fin_size)
 
2049
                                die("cannot read %s", read_from);
 
2050
                        break;
 
2051
                case S_IFLNK:
 
2052
                        if (readlink(read_from, buf, fin_size+1) != fin_size)
 
2053
                                die("cannot readlink %s", read_from);
 
2054
                        break;
 
2055
                default:
 
2056
                        die("unsupported file type %s", read_from);
 
2057
                }
 
2058
        }
 
2059
        else {
 
2060
                /* Reading from stdin */
 
2061
                contents_from = "standard input";
 
2062
                buf = NULL;
 
2063
                fin_size = 0;
 
2064
                mode = 0;
 
2065
                while (1) {
 
2066
                        ssize_t cnt = 8192;
 
2067
                        buf = xrealloc(buf, fin_size + cnt);
 
2068
                        cnt = xread(0, buf + fin_size, cnt);
 
2069
                        if (cnt < 0)
 
2070
                                die("read error %s from stdin",
 
2071
                                    strerror(errno));
 
2072
                        if (!cnt)
 
2073
                                break;
 
2074
                        fin_size += cnt;
 
2075
                }
 
2076
                buf = xrealloc(buf, fin_size + 1);
 
2077
        }
 
2078
        buf[fin_size] = 0;
 
2079
        origin->file.ptr = buf;
 
2080
        origin->file.size = fin_size;
 
2081
        pretend_sha1_file(buf, fin_size, OBJ_BLOB, origin->blob_sha1);
 
2082
        commit->util = origin;
 
2083
 
 
2084
        /*
 
2085
         * Read the current index, replace the path entry with
 
2086
         * origin->blob_sha1 without mucking with its mode or type
 
2087
         * bits; we are not going to write this index out -- we just
 
2088
         * want to run "diff-index --cached".
 
2089
         */
 
2090
        discard_cache();
 
2091
        read_cache();
 
2092
 
 
2093
        len = strlen(path);
 
2094
        if (!mode) {
 
2095
                int pos = cache_name_pos(path, len);
 
2096
                if (0 <= pos)
 
2097
                        mode = ntohl(active_cache[pos]->ce_mode);
 
2098
                else
 
2099
                        /* Let's not bother reading from HEAD tree */
 
2100
                        mode = S_IFREG | 0644;
 
2101
        }
 
2102
        size = cache_entry_size(len);
 
2103
        ce = xcalloc(1, size);
 
2104
        hashcpy(ce->sha1, origin->blob_sha1);
 
2105
        memcpy(ce->name, path, len);
 
2106
        ce->ce_flags = create_ce_flags(len, 0);
 
2107
        ce->ce_mode = create_ce_mode(mode);
 
2108
        add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
 
2109
 
 
2110
        /*
 
2111
         * We are not going to write this out, so this does not matter
 
2112
         * right now, but someday we might optimize diff-index --cached
 
2113
         * with cache-tree information.
 
2114
         */
 
2115
        cache_tree_invalidate_path(active_cache_tree, path);
 
2116
 
 
2117
        commit->buffer = xmalloc(400);
 
2118
        ident = fmt_ident("Not Committed Yet", "not.committed.yet", NULL, 0);
 
2119
        snprintf(commit->buffer, 400,
 
2120
                "tree 0000000000000000000000000000000000000000\n"
 
2121
                "parent %s\n"
 
2122
                "author %s\n"
 
2123
                "committer %s\n\n"
 
2124
                "Version of %s from %s\n",
 
2125
                sha1_to_hex(head_sha1),
 
2126
                ident, ident, path, contents_from ? contents_from : path);
 
2127
        return commit;
 
2128
}
 
2129
 
1629
2130
int cmd_blame(int argc, const char **argv, const char *prefix)
1630
2131
{
1631
2132
        struct rev_info revs;
1636
2137
        int i, seen_dashdash, unk, opt;
1637
2138
        long bottom, top, lno;
1638
2139
        int output_option = 0;
 
2140
        int show_stats = 0;
1639
2141
        const char *revs_file = NULL;
1640
2142
        const char *final_commit_name = NULL;
1641
 
        char type[10];
 
2143
        enum object_type type;
1642
2144
        const char *bottomtop = NULL;
1643
 
 
 
2145
        const char *contents_from = NULL;
 
2146
 
 
2147
        cmd_is_annotate = !strcmp(argv[0], "annotate");
 
2148
 
 
2149
        git_config(git_blame_config);
1644
2150
        save_commit_buffer = 0;
1645
2151
 
1646
2152
        opt = 0;
1649
2155
                const char *arg = argv[i];
1650
2156
                if (*arg != '-')
1651
2157
                        break;
 
2158
                else if (!strcmp("-b", arg))
 
2159
                        blank_boundary = 1;
 
2160
                else if (!strcmp("--root", arg))
 
2161
                        show_root = 1;
 
2162
                else if (!strcmp(arg, "--show-stats"))
 
2163
                        show_stats = 1;
1652
2164
                else if (!strcmp("-c", arg))
1653
2165
                        output_option |= OUTPUT_ANNOTATE_COMPAT;
1654
2166
                else if (!strcmp("-t", arg))
1655
2167
                        output_option |= OUTPUT_RAW_TIMESTAMP;
1656
2168
                else if (!strcmp("-l", arg))
1657
2169
                        output_option |= OUTPUT_LONG_OBJECT_NAME;
 
2170
                else if (!strcmp("-s", arg))
 
2171
                        output_option |= OUTPUT_NO_AUTHOR;
 
2172
                else if (!strcmp("-w", arg))
 
2173
                        xdl_opts |= XDF_IGNORE_WHITESPACE;
1658
2174
                else if (!strcmp("-S", arg) && ++i < argc)
1659
2175
                        revs_file = argv[i];
1660
 
                else if (!strncmp("-M", arg, 2)) {
 
2176
                else if (!prefixcmp(arg, "-M")) {
1661
2177
                        opt |= PICKAXE_BLAME_MOVE;
1662
2178
                        blame_move_score = parse_score(arg+2);
1663
2179
                }
1664
 
                else if (!strncmp("-C", arg, 2)) {
 
2180
                else if (!prefixcmp(arg, "-C")) {
 
2181
                        /*
 
2182
                         * -C enables copy from removed files;
 
2183
                         * -C -C enables copy from existing files, but only
 
2184
                         *       when blaming a new file;
 
2185
                         * -C -C -C enables copy from existing files for
 
2186
                         *          everybody
 
2187
                         */
 
2188
                        if (opt & PICKAXE_BLAME_COPY_HARDER)
 
2189
                                opt |= PICKAXE_BLAME_COPY_HARDEST;
1665
2190
                        if (opt & PICKAXE_BLAME_COPY)
1666
2191
                                opt |= PICKAXE_BLAME_COPY_HARDER;
1667
2192
                        opt |= PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE;
1668
2193
                        blame_copy_score = parse_score(arg+2);
1669
2194
                }
1670
 
                else if (!strncmp("-L", arg, 2)) {
 
2195
                else if (!prefixcmp(arg, "-L")) {
1671
2196
                        if (!arg[2]) {
1672
2197
                                if (++i >= argc)
1673
2198
                                        usage(blame_usage);
1679
2204
                                die("More than one '-L n,m' option given");
1680
2205
                        bottomtop = arg;
1681
2206
                }
 
2207
                else if (!strcmp("--contents", arg)) {
 
2208
                        if (++i >= argc)
 
2209
                                usage(blame_usage);
 
2210
                        contents_from = argv[i];
 
2211
                }
 
2212
                else if (!strcmp("--incremental", arg))
 
2213
                        incremental = 1;
1682
2214
                else if (!strcmp("--score-debug", arg))
1683
2215
                        output_option |= OUTPUT_SHOW_SCORE;
1684
2216
                else if (!strcmp("-f", arg) ||
1704
2236
        if (!blame_copy_score)
1705
2237
                blame_copy_score = BLAME_DEFAULT_COPY_SCORE;
1706
2238
 
1707
 
        /* We have collected options unknown to us in argv[1..unk]
 
2239
        /*
 
2240
         * We have collected options unknown to us in argv[1..unk]
1708
2241
         * which are to be passed to revision machinery if we are
1709
 
         * going to do the "bottom" procesing.
 
2242
         * going to do the "bottom" processing.
1710
2243
         *
1711
2244
         * The remaining are:
1712
2245
         *
1752
2285
                        if (!strcmp(argv[j], "--"))
1753
2286
                                seen_dashdash = j;
1754
2287
                if (seen_dashdash) {
 
2288
                        /* (2) */
1755
2289
                        if (seen_dashdash + 1 != argc - 1)
1756
2290
                                usage(blame_usage);
1757
2291
                        path = add_prefix(prefix, argv[seen_dashdash + 1]);
1760
2294
                }
1761
2295
                else {
1762
2296
                        /* (3) */
 
2297
                        if (argc <= i)
 
2298
                                usage(blame_usage);
1763
2299
                        path = add_prefix(prefix, argv[i]);
1764
2300
                        if (i + 1 == argc - 1) {
1765
2301
                                final_commit_name = argv[i + 1];
1784
2320
        if (final_commit_name)
1785
2321
                argv[unk++] = final_commit_name;
1786
2322
 
1787
 
        /* Now we got rev and path.  We do not want the path pruning
 
2323
        /*
 
2324
         * Now we got rev and path.  We do not want the path pruning
1788
2325
         * but we may want "bottom" processing.
1789
2326
         */
1790
2327
        argv[unk++] = "--"; /* terminate the rev name */
1791
2328
        argv[unk] = NULL;
1792
2329
 
1793
2330
        init_revisions(&revs, NULL);
1794
 
        setup_revisions(unk, argv, &revs, "HEAD");
 
2331
        setup_revisions(unk, argv, &revs, NULL);
1795
2332
        memset(&sb, 0, sizeof(sb));
1796
2333
 
1797
 
        /* There must be one and only one positive commit in the
 
2334
        /*
 
2335
         * There must be one and only one positive commit in the
1798
2336
         * revs->pending array.
1799
2337
         */
1800
2338
        for (i = 0; i < revs.pending.nr; i++) {
1815
2353
        }
1816
2354
 
1817
2355
        if (!sb.final) {
1818
 
                /* "--not A B -- path" without anything positive */
1819
 
                unsigned char head_sha1[20];
1820
 
 
1821
 
                final_commit_name = "HEAD";
1822
 
                if (get_sha1(final_commit_name, head_sha1))
1823
 
                        die("No such ref: HEAD");
1824
 
                sb.final = lookup_commit_reference(head_sha1);
1825
 
                add_pending_object(&revs, &(sb.final->object), "HEAD");
 
2356
                /*
 
2357
                 * "--not A B -- path" without anything positive;
 
2358
                 * do not default to HEAD, but use the working tree
 
2359
                 * or "--contents".
 
2360
                 */
 
2361
                sb.final = fake_working_tree_commit(path, contents_from);
 
2362
                add_pending_object(&revs, &(sb.final->object), ":");
1826
2363
        }
 
2364
        else if (contents_from)
 
2365
                die("Cannot use --contents with final commit object name");
1827
2366
 
1828
 
        /* If we have bottom, this will mark the ancestors of the
 
2367
        /*
 
2368
         * If we have bottom, this will mark the ancestors of the
1829
2369
         * bottom commits we would reach while traversing as
1830
2370
         * uninteresting.
1831
2371
         */
1832
2372
        prepare_revision_walk(&revs);
1833
2373
 
1834
 
        o = get_origin(&sb, sb.final, path);
1835
 
        if (fill_blob_sha1(o))
1836
 
                die("no such path %s in %s", path, final_commit_name);
 
2374
        if (is_null_sha1(sb.final->object.sha1)) {
 
2375
                char *buf;
 
2376
                o = sb.final->util;
 
2377
                buf = xmalloc(o->file.size + 1);
 
2378
                memcpy(buf, o->file.ptr, o->file.size + 1);
 
2379
                sb.final_buf = buf;
 
2380
                sb.final_buf_size = o->file.size;
 
2381
        }
 
2382
        else {
 
2383
                o = get_origin(&sb, sb.final, path);
 
2384
                if (fill_blob_sha1(o))
 
2385
                        die("no such path %s in %s", path, final_commit_name);
1837
2386
 
1838
 
        sb.final_buf = read_sha1_file(o->blob_sha1, type, &sb.final_buf_size);
 
2387
                sb.final_buf = read_sha1_file(o->blob_sha1, &type,
 
2388
                                              &sb.final_buf_size);
 
2389
                if (!sb.final_buf)
 
2390
                        die("Cannot read blob %s for path %s",
 
2391
                            sha1_to_hex(o->blob_sha1),
 
2392
                            path);
 
2393
        }
1839
2394
        num_read_blob++;
1840
2395
        lno = prepare_lines(&sb);
1841
2396
 
1867
2422
                die("reading graft file %s failed: %s",
1868
2423
                    revs_file, strerror(errno));
1869
2424
 
 
2425
        read_mailmap(&mailmap, ".mailmap", NULL);
 
2426
 
 
2427
        if (!incremental)
 
2428
                setup_pager();
 
2429
 
1870
2430
        assign_blame(&sb, &revs, opt);
1871
2431
 
 
2432
        if (incremental)
 
2433
                return 0;
 
2434
 
1872
2435
        coalesce(&sb);
1873
2436
 
1874
2437
        if (!(output_option & OUTPUT_PORCELAIN))
1882
2445
                ent = e;
1883
2446
        }
1884
2447
 
1885
 
        if (DEBUG) {
 
2448
        if (show_stats) {
1886
2449
                printf("num read blob: %d\n", num_read_blob);
1887
2450
                printf("num get patch: %d\n", num_get_patch);
1888
2451
                printf("num commits: %d\n", num_commits);