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