blame.hon commit Merge branch 'jh/trace2-pretty-output' (93fc876)
   1#ifndef BLAME_H
   2#define BLAME_H
   3
   4#include "cache.h"
   5#include "commit.h"
   6#include "xdiff-interface.h"
   7#include "revision.h"
   8#include "prio-queue.h"
   9#include "diff.h"
  10
  11#define PICKAXE_BLAME_MOVE              01
  12#define PICKAXE_BLAME_COPY              02
  13#define PICKAXE_BLAME_COPY_HARDER       04
  14#define PICKAXE_BLAME_COPY_HARDEST      010
  15
  16#define BLAME_DEFAULT_MOVE_SCORE        20
  17#define BLAME_DEFAULT_COPY_SCORE        40
  18
  19/*
  20 * One blob in a commit that is being suspected
  21 */
  22struct blame_origin {
  23        int refcnt;
  24        /* Record preceding blame record for this blob */
  25        struct blame_origin *previous;
  26        /* origins are put in a list linked via `next' hanging off the
  27         * corresponding commit's util field in order to make finding
  28         * them fast.  The presence in this chain does not count
  29         * towards the origin's reference count.  It is tempting to
  30         * let it count as long as the commit is pending examination,
  31         * but even under circumstances where the commit will be
  32         * present multiple times in the priority queue of unexamined
  33         * commits, processing the first instance will not leave any
  34         * work requiring the origin data for the second instance.  An
  35         * interspersed commit changing that would have to be
  36         * preexisting with a different ancestry and with the same
  37         * commit date in order to wedge itself between two instances
  38         * of the same commit in the priority queue _and_ produce
  39         * blame entries relevant for it.  While we don't want to let
  40         * us get tripped up by this case, it certainly does not seem
  41         * worth optimizing for.
  42         */
  43        struct blame_origin *next;
  44        struct commit *commit;
  45        /* `suspects' contains blame entries that may be attributed to
  46         * this origin's commit or to parent commits.  When a commit
  47         * is being processed, all suspects will be moved, either by
  48         * assigning them to an origin in a different commit, or by
  49         * shipping them to the scoreboard's ent list because they
  50         * cannot be attributed to a different commit.
  51         */
  52        struct blame_entry *suspects;
  53        mmfile_t file;
  54        int num_lines;
  55        void *fingerprints;
  56        struct object_id blob_oid;
  57        unsigned short mode;
  58        /* guilty gets set when shipping any suspects to the final
  59         * blame list instead of other commits
  60         */
  61        char guilty;
  62        char path[FLEX_ARRAY];
  63};
  64
  65/*
  66 * Each group of lines is described by a blame_entry; it can be split
  67 * as we pass blame to the parents.  They are arranged in linked lists
  68 * kept as `suspects' of some unprocessed origin, or entered (when the
  69 * blame origin has been finalized) into the scoreboard structure.
  70 * While the scoreboard structure is only sorted at the end of
  71 * processing (according to final image line number), the lists
  72 * attached to an origin are sorted by the target line number.
  73 */
  74struct blame_entry {
  75        struct blame_entry *next;
  76
  77        /* the first line of this group in the final image;
  78         * internally all line numbers are 0 based.
  79         */
  80        int lno;
  81
  82        /* how many lines this group has */
  83        int num_lines;
  84
  85        /* the commit that introduced this group into the final image */
  86        struct blame_origin *suspect;
  87
  88        /* the line number of the first line of this group in the
  89         * suspect's file; internally all line numbers are 0 based.
  90         */
  91        int s_lno;
  92
  93        /* how significant this entry is -- cached to avoid
  94         * scanning the lines over and over.
  95         */
  96        unsigned score;
  97        int ignored;
  98        int unblamable;
  99};
 100
 101/*
 102 * The current state of the blame assignment.
 103 */
 104struct blame_scoreboard {
 105        /* the final commit (i.e. where we started digging from) */
 106        struct commit *final;
 107        /* Priority queue for commits with unassigned blame records */
 108        struct prio_queue commits;
 109        struct repository *repo;
 110        struct rev_info *revs;
 111        const char *path;
 112
 113        /*
 114         * The contents in the final image.
 115         * Used by many functions to obtain contents of the nth line,
 116         * indexed with scoreboard.lineno[blame_entry.lno].
 117         */
 118        const char *final_buf;
 119        unsigned long final_buf_size;
 120
 121        /* linked list of blames */
 122        struct blame_entry *ent;
 123
 124        struct oidset ignore_list;
 125
 126        /* look-up a line in the final buffer */
 127        int num_lines;
 128        int *lineno;
 129
 130        /* stats */
 131        int num_read_blob;
 132        int num_get_patch;
 133        int num_commits;
 134
 135        /*
 136         * blame for a blame_entry with score lower than these thresholds
 137         * is not passed to the parent using move/copy logic.
 138         */
 139        unsigned move_score;
 140        unsigned copy_score;
 141
 142        /* use this file's contents as the final image */
 143        const char *contents_from;
 144
 145        /* flags */
 146        int reverse;
 147        int show_root;
 148        int xdl_opts;
 149        int no_whole_file_rename;
 150        int debug;
 151
 152        /* callbacks */
 153        void(*on_sanity_fail)(struct blame_scoreboard *, int);
 154        void(*found_guilty_entry)(struct blame_entry *, void *);
 155
 156        void *found_guilty_entry_data;
 157};
 158
 159/*
 160 * Origin is refcounted and usually we keep the blob contents to be
 161 * reused.
 162 */
 163static inline struct blame_origin *blame_origin_incref(struct blame_origin *o)
 164{
 165        if (o)
 166                o->refcnt++;
 167        return o;
 168}
 169void blame_origin_decref(struct blame_origin *o);
 170
 171void blame_coalesce(struct blame_scoreboard *sb);
 172void blame_sort_final(struct blame_scoreboard *sb);
 173unsigned blame_entry_score(struct blame_scoreboard *sb, struct blame_entry *e);
 174void assign_blame(struct blame_scoreboard *sb, int opt);
 175const char *blame_nth_line(struct blame_scoreboard *sb, long lno);
 176
 177void init_scoreboard(struct blame_scoreboard *sb);
 178void setup_scoreboard(struct blame_scoreboard *sb,
 179                      const char *path,
 180                      struct blame_origin **orig);
 181
 182struct blame_entry *blame_entry_prepend(struct blame_entry *head,
 183                                        long start, long end,
 184                                        struct blame_origin *o);
 185
 186struct blame_origin *get_blame_suspects(struct commit *commit);
 187
 188#endif /* BLAME_H */