THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

blame: add config options for the output of ignored or unblamable lines When ignoring commits, the commit that is blamed might not be responsible for the change, due to the inaccuracy of our heuristic. Users might want to know when a particular line has a potentially inaccurate blame. Furthermore, guess_line_blames() may fail to find any parent commit for a given line touched by an ignored commit. Those 'unblamable' lines remain blamed on an ignored commit. Users might want to know if a line is unblamable so that they do not spend time investigating a commit they know is uninteresting. This patch adds two config options to mark these two types of lines in the output of blame. The first option can identify ignored lines by specifying blame.markIgnoredLines. When this option is set, each blame line that was blamed on a commit other than the ignored commit is marked with a '?'. For example: 278b6158d6fdb (Barret Rhoden 2016-04-11 13:57:54 -0400 26) appears as: ?278b6158d6fd (Barret Rhoden 2016-04-11 13:57:54 -0400 26) where the '?' is placed before the commit, and the hash has one fewer characters. Sometimes we are unable to even guess at what ancestor commit touched a line. These lines are 'unblamable.' The second option, blame.markUnblamableLines, will mark the line with '*'. For example, say we ignore e5e8d36d04cbe, yet we are unable to blame this line on another commit: e5e8d36d04cbe (Barret Rhoden 2016-04-11 13:57:54 -0400 26) appears as: *e5e8d36d04cb (Barret Rhoden 2016-04-11 13:57:54 -0400 26) When these config options are used together, every line touched by an ignored commit will be marked with either a '?' or a '*'. Signed-off-by: Barret Rhoden <brho@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
6 months ago
blame: add the ability to ignore commits and their changes Commits that make formatting changes or function renames are often not interesting when blaming a file. A user may deem such a commit as 'not interesting' and want to ignore and its changes it when assigning blame. For example, say a file has the following git history / rev-list: ---O---A---X---B---C---D---Y---E---F Commits X and Y both touch a particular line, and the other commits do not: X: "Take a third parameter" -MyFunc(1, 2); +MyFunc(1, 2, 3); Y: "Remove camelcase" -MyFunc(1, 2, 3); +my_func(1, 2, 3); git-blame will blame Y for the change. I'd like to be able to ignore Y: both the existence of the commit as well as any changes it made. This differs from -S rev-list, which specifies the list of commits to process for the blame. We would still process Y, but just don't let the blame 'stick.' This patch adds the ability for users to ignore a revision with --ignore-rev=rev, which may be repeated. They can specify a set of files of full object names of revs, e.g. SHA-1 hashes, one per line. A single file may be specified with the blame.ignoreRevFile config option or with --ignore-rev-file=file. Both the config option and the command line option may be repeated multiple times. An empty file name "" will clear the list of revs from previously processed files. Config options are processed before command line options. For a typical use case, projects will maintain the file containing revisions for commits that perform mass reformatting, and their users have the option to ignore all of the commits in that file. Additionally, a user can use the --ignore-rev option for one-off investigation. To go back to the example above, X was a substantive change to the function, but not the change the user is interested in. The user inspected X, but wanted to find the previous change to that line - perhaps a commit that introduced that function call. To make this work, we can't simply remove all ignored commits from the rev-list. We need to diff the changes introduced by Y so that we can ignore them. We let the blames get passed to Y, just like when processing normally. When Y is the target, we make sure that Y does not *keep* any blames. Any changes that Y is responsible for get passed to its parent. Note we make one pass through all of the scapegoats (parents) to attempt to pass blame normally; we don't know if we *need* to ignore the commit until we've checked all of the parents. The blame_entry will get passed up the tree until we find a commit that has a diff chunk that affects those lines. One issue is that the ignored commit *did* make some change, and there is no general solution to finding the line in the parent commit that corresponds to a given line in the ignored commit. That makes it hard to attribute a particular line within an ignored commit's diff correctly. For example, the parent of an ignored commit has this, say at line 11: commit-a 11) #include "a.h" commit-b 12) #include "b.h" Commit X, which we will ignore, swaps these lines: commit-X 11) #include "b.h" commit-X 12) #include "a.h" We can pass that blame entry to the parent, but line 11 will be attributed to commit A, even though "include b.h" came from commit B. The blame mechanism will be looking at the parent's view of the file at line number 11. ignore_blame_entry() is set up to allow alternative algorithms for guessing per-line blames. Any line that is not attributed to the parent will continue to be blamed on the ignored commit as if that commit was not ignored. Upcoming patches have the ability to detect these lines and mark them in the blame output. The existing algorithm is simple: blame each line on the corresponding line in the parent's diff chunk. Any lines beyond that stay with the target. For example, the parent of an ignored commit has this, say at line 11: commit-a 11) void new_func_1(void *x, void *y); commit-b 12) void new_func_2(void *x, void *y); commit-c 13) some_line_c commit-d 14) some_line_d After a commit 'X', we have: commit-X 11) void new_func_1(void *x, commit-X 12) void *y); commit-X 13) void new_func_2(void *x, commit-X 14) void *y); commit-c 15) some_line_c commit-d 16) some_line_d Commit X nets two additionally lines: 13 and 14. The current guess_line_blames() algorithm will not attribute these to the parent, whose diff chunk is only two lines - not four. When we ignore with the current algorithm, we get: commit-a 11) void new_func_1(void *x, commit-b 12) void *y); commit-X 13) void new_func_2(void *x, commit-X 14) void *y); commit-c 15) some_line_c commit-d 16) some_line_d Note that line 12 was blamed on B, though B was the commit for new_func_2(), not new_func_1(). Even when guess_line_blames() finds a line in the parent, it may still be incorrect. Signed-off-by: Barret Rhoden <brho@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
6 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #ifndef BLAME_H
  2. #define BLAME_H
  3. #include "cache.h"
  4. #include "commit.h"
  5. #include "xdiff-interface.h"
  6. #include "revision.h"
  7. #include "prio-queue.h"
  8. #include "diff.h"
  9. #define PICKAXE_BLAME_MOVE 01
  10. #define PICKAXE_BLAME_COPY 02
  11. #define PICKAXE_BLAME_COPY_HARDER 04
  12. #define PICKAXE_BLAME_COPY_HARDEST 010
  13. #define BLAME_DEFAULT_MOVE_SCORE 20
  14. #define BLAME_DEFAULT_COPY_SCORE 40
  15. /*
  16. * One blob in a commit that is being suspected
  17. */
  18. struct blame_origin {
  19. int refcnt;
  20. /* Record preceding blame record for this blob */
  21. struct blame_origin *previous;
  22. /* origins are put in a list linked via `next' hanging off the
  23. * corresponding commit's util field in order to make finding
  24. * them fast. The presence in this chain does not count
  25. * towards the origin's reference count. It is tempting to
  26. * let it count as long as the commit is pending examination,
  27. * but even under circumstances where the commit will be
  28. * present multiple times in the priority queue of unexamined
  29. * commits, processing the first instance will not leave any
  30. * work requiring the origin data for the second instance. An
  31. * interspersed commit changing that would have to be
  32. * preexisting with a different ancestry and with the same
  33. * commit date in order to wedge itself between two instances
  34. * of the same commit in the priority queue _and_ produce
  35. * blame entries relevant for it. While we don't want to let
  36. * us get tripped up by this case, it certainly does not seem
  37. * worth optimizing for.
  38. */
  39. struct blame_origin *next;
  40. struct commit *commit;
  41. /* `suspects' contains blame entries that may be attributed to
  42. * this origin's commit or to parent commits. When a commit
  43. * is being processed, all suspects will be moved, either by
  44. * assigning them to an origin in a different commit, or by
  45. * shipping them to the scoreboard's ent list because they
  46. * cannot be attributed to a different commit.
  47. */
  48. struct blame_entry *suspects;
  49. mmfile_t file;
  50. int num_lines;
  51. void *fingerprints;
  52. struct object_id blob_oid;
  53. unsigned short mode;
  54. /* guilty gets set when shipping any suspects to the final
  55. * blame list instead of other commits
  56. */
  57. char guilty;
  58. char path[FLEX_ARRAY];
  59. };
  60. /*
  61. * Each group of lines is described by a blame_entry; it can be split
  62. * as we pass blame to the parents. They are arranged in linked lists
  63. * kept as `suspects' of some unprocessed origin, or entered (when the
  64. * blame origin has been finalized) into the scoreboard structure.
  65. * While the scoreboard structure is only sorted at the end of
  66. * processing (according to final image line number), the lists
  67. * attached to an origin are sorted by the target line number.
  68. */
  69. struct blame_entry {
  70. struct blame_entry *next;
  71. /* the first line of this group in the final image;
  72. * internally all line numbers are 0 based.
  73. */
  74. int lno;
  75. /* how many lines this group has */
  76. int num_lines;
  77. /* the commit that introduced this group into the final image */
  78. struct blame_origin *suspect;
  79. /* the line number of the first line of this group in the
  80. * suspect's file; internally all line numbers are 0 based.
  81. */
  82. int s_lno;
  83. /* how significant this entry is -- cached to avoid
  84. * scanning the lines over and over.
  85. */
  86. unsigned score;
  87. int ignored;
  88. int unblamable;
  89. };
  90. /*
  91. * The current state of the blame assignment.
  92. */
  93. struct blame_scoreboard {
  94. /* the final commit (i.e. where we started digging from) */
  95. struct commit *final;
  96. /* Priority queue for commits with unassigned blame records */
  97. struct prio_queue commits;
  98. struct repository *repo;
  99. struct rev_info *revs;
  100. const char *path;
  101. /*
  102. * The contents in the final image.
  103. * Used by many functions to obtain contents of the nth line,
  104. * indexed with scoreboard.lineno[blame_entry.lno].
  105. */
  106. const char *final_buf;
  107. unsigned long final_buf_size;
  108. /* linked list of blames */
  109. struct blame_entry *ent;
  110. struct oidset ignore_list;
  111. /* look-up a line in the final buffer */
  112. int num_lines;
  113. int *lineno;
  114. /* stats */
  115. int num_read_blob;
  116. int num_get_patch;
  117. int num_commits;
  118. /*
  119. * blame for a blame_entry with score lower than these thresholds
  120. * is not passed to the parent using move/copy logic.
  121. */
  122. unsigned move_score;
  123. unsigned copy_score;
  124. /* use this file's contents as the final image */
  125. const char *contents_from;
  126. /* flags */
  127. int reverse;
  128. int show_root;
  129. int xdl_opts;
  130. int no_whole_file_rename;
  131. int debug;
  132. /* callbacks */
  133. void(*on_sanity_fail)(struct blame_scoreboard *, int);
  134. void(*found_guilty_entry)(struct blame_entry *, void *);
  135. void *found_guilty_entry_data;
  136. };
  137. /*
  138. * Origin is refcounted and usually we keep the blob contents to be
  139. * reused.
  140. */
  141. static inline struct blame_origin *blame_origin_incref(struct blame_origin *o)
  142. {
  143. if (o)
  144. o->refcnt++;
  145. return o;
  146. }
  147. void blame_origin_decref(struct blame_origin *o);
  148. void blame_coalesce(struct blame_scoreboard *sb);
  149. void blame_sort_final(struct blame_scoreboard *sb);
  150. unsigned blame_entry_score(struct blame_scoreboard *sb, struct blame_entry *e);
  151. void assign_blame(struct blame_scoreboard *sb, int opt);
  152. const char *blame_nth_line(struct blame_scoreboard *sb, long lno);
  153. void init_scoreboard(struct blame_scoreboard *sb);
  154. void setup_scoreboard(struct blame_scoreboard *sb,
  155. const char *path,
  156. struct blame_origin **orig);
  157. struct blame_entry *blame_entry_prepend(struct blame_entry *head,
  158. long start, long end,
  159. struct blame_origin *o);
  160. struct blame_origin *get_blame_suspects(struct commit *commit);
  161. #endif /* BLAME_H */