blame: add config options for the output of ignored or unblamable lines
authorBarret Rhoden <brho@google.com>
Wed, 15 May 2019 21:45:00 +0000 (17:45 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 16 May 2019 02:36:23 +0000 (11:36 +0900)
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>
Documentation/blame-options.txt
Documentation/config/blame.txt
blame.c
blame.h
builtin/blame.c
t/t8013-blame-ignore-revs.sh
index 2c2d1ceb565349ee92122295bac7cb0a73a1ac4a..5d122db6e9e6863fcf1e69ebc14feb1393501e0b 100644 (file)
@@ -115,7 +115,12 @@ take effect.
        change never happened.  Lines that were changed or added by an ignored
        commit will be blamed on the previous commit that changed that line or
        nearby lines.  This option may be specified multiple times to ignore
-       more than one revision.
+       more than one revision.  If the `blame.markIgnoredLines` config option
+       is set, then lines that were changed by an ignored commit and attributed to
+       another commit will be marked with a `?` in the blame output.  If the
+       `blame.markUnblamableLines` config option is set, then those lines touched
+       by an ignored commit that we could not attribute to another revision are
+       marked with a '*'.
 
 --ignore-revs-file <file>::
        Ignore revisions listed in `file`, which must be in the same format as an
index 4da2788f306d50090fd82e4a00cf499634f0c073..9468e8599c0c16d3bd54ec67ba65351be9de1a69 100644 (file)
@@ -26,3 +26,12 @@ blame.ignoreRevsFile::
        `#` are ignored.  This option may be repeated multiple times.  Empty
        file names will reset the list of ignored revisions.  This option will
        be handled before the command line option `--ignore-revs-file`.
+
+blame.markUnblamables::
+       Mark lines that were changed by an ignored revision that we could not
+       attribute to another commit with a '*' in the output of
+       linkgit:git-blame[1].
+
+blame.markIgnoredLines::
+       Mark lines that were changed by an ignored revision that we attributed to
+       another commit with a '?' in the output of linkgit:git-blame[1].
diff --git a/blame.c b/blame.c
index c57d7a3632d73cccd15bd741ae5e47ae803561fd..472ee67ae27a220980d87b053a9eff15e73dee1e 100644 (file)
--- a/blame.c
+++ b/blame.c
@@ -479,7 +479,9 @@ void blame_coalesce(struct blame_scoreboard *sb)
 
        for (ent = sb->ent; ent && (next = ent->next); ent = next) {
                if (ent->suspect == next->suspect &&
-                   ent->s_lno + ent->num_lines == next->s_lno) {
+                   ent->s_lno + ent->num_lines == next->s_lno &&
+                   ent->ignored == next->ignored &&
+                   ent->unblamable == next->unblamable) {
                        ent->num_lines += next->num_lines;
                        ent->next = next->next;
                        blame_origin_decref(next->suspect);
@@ -729,8 +731,14 @@ static void split_overlap(struct blame_entry *split,
                          struct blame_origin *parent)
 {
        int chunk_end_lno;
+       int i;
        memset(split, 0, sizeof(struct blame_entry [3]));
 
+       for (i = 0; i < 3; i++) {
+               split[i].ignored = e->ignored;
+               split[i].unblamable = e->unblamable;
+       }
+
        if (e->s_lno < tlno) {
                /* there is a pre-chunk part not blamed on parent */
                split[0].suspect = blame_origin_incref(e->suspect);
@@ -851,6 +859,8 @@ static struct blame_entry *split_blame_at(struct blame_entry *e, int len,
        struct blame_entry *n = xcalloc(1, sizeof(struct blame_entry));
 
        n->suspect = new_suspect;
+       n->ignored = e->ignored;
+       n->unblamable = e->unblamable;
        n->lno = e->lno + len;
        n->s_lno = e->s_lno + len;
        n->num_lines = e->num_lines - len;
@@ -939,12 +949,14 @@ static void ignore_blame_entry(struct blame_entry *e,
                                              blame_origin_incref(e->suspect));
                }
                if (line_blames[i].is_parent) {
+                       e->ignored = 1;
                        blame_origin_decref(e->suspect);
                        e->suspect = blame_origin_incref(parent);
                        e->s_lno = line_blames[i - entry_len + 1].s_lno;
                        e->next = *ignoredp;
                        *ignoredp = e;
                } else {
+                       e->unblamable = 1;
                        /* e->s_lno is already in the target's address space. */
                        e->next = *diffp;
                        *diffp = e;
diff --git a/blame.h b/blame.h
index 086b92915e4b86578688f49e7ebe05e577bc5edd..53df8b4c5b3fd0ef2f1f2708055ecaeb60574e43 100644 (file)
--- a/blame.h
+++ b/blame.h
@@ -92,6 +92,8 @@ struct blame_entry {
         * scanning the lines over and over.
         */
        unsigned score;
+       int ignored;
+       int unblamable;
 };
 
 /*
index cd9e14e568b2af7773cc7f128b15b6a0e002a344..bb321eb2000179ad924588080202a527a751d7f6 100644 (file)
@@ -53,6 +53,8 @@ static int show_progress;
 static char repeated_meta_color[COLOR_MAXLEN];
 static int coloring_mode;
 static struct string_list ignore_revs_file_list = STRING_LIST_INIT_NODUP;
+static int mark_unblamable_lines;
+static int mark_ignored_lines;
 
 static struct date_mode blame_date_mode = { DATE_ISO8601 };
 static size_t blame_date_width;
@@ -480,6 +482,14 @@ static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int
                        }
                }
 
+               if (mark_unblamable_lines && ent->unblamable) {
+                       length--;
+                       putchar('*');
+               }
+               if (mark_ignored_lines && ent->ignored) {
+                       length--;
+                       putchar('?');
+               }
                printf("%.*s", length, hex);
                if (opt & OUTPUT_ANNOTATE_COMPAT) {
                        const char *name;
@@ -706,6 +716,14 @@ static int git_blame_config(const char *var, const char *value, void *cb)
                string_list_insert(&ignore_revs_file_list, str);
                return 0;
        }
+       if (!strcmp(var, "blame.markunblamablelines")) {
+               mark_unblamable_lines = git_config_bool(var, value);
+               return 0;
+       }
+       if (!strcmp(var, "blame.markignoredlines")) {
+               mark_ignored_lines = git_config_bool(var, value);
+               return 0;
+       }
        if (!strcmp(var, "color.blame.repeatedlines")) {
                if (color_parse_mem(value, strlen(value), repeated_meta_color))
                        warning(_("invalid color '%s' in color.blame.repeatedLines"),
index fdb2fa87978197762806a4bb0d1fbcf7af169fb8..36dc31eb391361e002ac83da679a5be6c5b23b35 100755 (executable)
@@ -121,6 +121,77 @@ test_expect_success bad_files_and_revs '
        test_must_fail git blame file --ignore-revs-file ignore_norev 2>err &&
        test_i18ngrep "invalid object name: NOREV" err
        '
+
+# For ignored revs that have added 'unblamable' lines, mark those lines with a
+# '*'
+#      A--B--X--Y
+# Lines 3 and 4 are from Y and unblamable.  This was set up in
+# ignore_rev_adding_unblamable_lines.
+test_expect_success mark_unblamable_lines '
+       git config --add blame.markUnblamableLines true &&
+
+       git blame --ignore-rev Y file >blame_raw &&
+       echo "*" >expect &&
+
+       sed -n "3p" blame_raw | cut -c1 >actual &&
+       test_cmp expect actual &&
+
+       sed -n "4p" blame_raw | cut -c1 >actual &&
+       test_cmp expect actual
+       '
+
+# Commit Z will touch the first two lines.  Y touched all four.
+#      A--B--X--Y--Z
+# The blame output when ignoring Z should be:
+# ?Y ... 1)
+# ?Y ... 2)
+# Y  ... 3)
+# Y  ... 4)
+# We're checking only the first character
+test_expect_success mark_ignored_lines '
+       git config --add blame.markIgnoredLines true &&
+
+       test_write_lines line-one-Z line-two-Z y3 y4 >file &&
+       git add file &&
+       test_tick &&
+       git commit -m Z &&
+       git tag Z &&
+
+       git blame --ignore-rev Z file >blame_raw &&
+       echo "?" >expect &&
+
+       sed -n "1p" blame_raw | cut -c1 >actual &&
+       test_cmp expect actual &&
+
+       sed -n "2p" blame_raw | cut -c1 >actual &&
+       test_cmp expect actual &&
+
+       sed -n "3p" blame_raw | cut -c1 >actual &&
+       ! test_cmp expect actual &&
+
+       sed -n "4p" blame_raw | cut -c1 >actual &&
+       ! test_cmp expect actual
+       '
+
+# For ignored revs that added 'unblamable' lines and more recent commits changed
+# the blamable lines, mark the unblamable lines with a
+# '*'
+#      A--B--X--Y--Z
+# Lines 3 and 4 are from Y and unblamable, as set up in
+# ignore_rev_adding_unblamable_lines.  Z changed lines 1 and 2.
+test_expect_success mark_unblamable_lines_intermediate '
+       git config --add blame.markUnblamableLines true &&
+
+       git blame --ignore-rev Y file >blame_raw 2>stderr &&
+       echo "*" >expect &&
+
+       sed -n "3p" blame_raw | cut -c1 >actual &&
+       test_cmp expect actual &&
+
+       sed -n "4p" blame_raw | cut -c1 >actual &&
+       test_cmp expect actual
+       '
+
 # The heuristic called by guess_line_blames() tries to find the size of a
 # blame_entry 'e' in the parent's address space.  Those calculations need to
 # check for negative or zero values for when a blame entry is completely outside