stash: convert branch to builtin
[gitweb.git] / builtin / blame.c
index 67adaef4d80ee4e2fcd28755255935f0fcae6c03..6d798f99392e54b4392713846652111d457787bb 100644 (file)
@@ -7,7 +7,9 @@
 
 #include "cache.h"
 #include "config.h"
+#include "color.h"
 #include "builtin.h"
+#include "repository.h"
 #include "commit.h"
 #include "diff.h"
 #include "revision.h"
@@ -22,7 +24,9 @@
 #include "line-log.h"
 #include "dir.h"
 #include "progress.h"
+#include "object-store.h"
 #include "blame.h"
+#include "string-list.h"
 
 static char blame_usage[] = N_("git blame [<options>] [<rev-opts>] [<rev>] [--] <file>");
 
@@ -46,6 +50,8 @@ static int xdl_opts;
 static int abbrev = -1;
 static int no_whole_file_rename;
 static int show_progress;
+static char repeated_meta_color[COLOR_MAXLEN];
+static int coloring_mode;
 
 static struct date_mode blame_date_mode = { DATE_ISO8601 };
 static size_t blame_date_width;
@@ -316,10 +322,12 @@ static const char *format_time(timestamp_t time, const char *tz_str,
 #define OUTPUT_PORCELAIN       010
 #define OUTPUT_SHOW_NAME       020
 #define OUTPUT_SHOW_NUMBER     040
-#define OUTPUT_SHOW_SCORE      0100
-#define OUTPUT_NO_AUTHOR       0200
+#define OUTPUT_SHOW_SCORE      0100
+#define OUTPUT_NO_AUTHOR       0200
 #define OUTPUT_SHOW_EMAIL      0400
-#define OUTPUT_LINE_PORCELAIN 01000
+#define OUTPUT_LINE_PORCELAIN  01000
+#define OUTPUT_COLOR_LINE      02000
+#define OUTPUT_SHOW_AGE_WITH_COLOR     04000
 
 static void emit_porcelain_details(struct blame_origin *suspect, int repeat)
 {
@@ -367,6 +375,64 @@ static void emit_porcelain(struct blame_scoreboard *sb, struct blame_entry *ent,
                putchar('\n');
 }
 
+static struct color_field {
+       timestamp_t hop;
+       char col[COLOR_MAXLEN];
+} *colorfield;
+static int colorfield_nr, colorfield_alloc;
+
+static void parse_color_fields(const char *s)
+{
+       struct string_list l = STRING_LIST_INIT_DUP;
+       struct string_list_item *item;
+       enum { EXPECT_DATE, EXPECT_COLOR } next = EXPECT_COLOR;
+
+       colorfield_nr = 0;
+
+       /* Ideally this would be stripped and split at the same time? */
+       string_list_split(&l, s, ',', -1);
+       ALLOC_GROW(colorfield, colorfield_nr + 1, colorfield_alloc);
+
+       for_each_string_list_item(item, &l) {
+               switch (next) {
+               case EXPECT_DATE:
+                       colorfield[colorfield_nr].hop = approxidate(item->string);
+                       next = EXPECT_COLOR;
+                       colorfield_nr++;
+                       ALLOC_GROW(colorfield, colorfield_nr + 1, colorfield_alloc);
+                       break;
+               case EXPECT_COLOR:
+                       if (color_parse(item->string, colorfield[colorfield_nr].col))
+                               die(_("expecting a color: %s"), item->string);
+                       next = EXPECT_DATE;
+                       break;
+               }
+       }
+
+       if (next == EXPECT_COLOR)
+               die(_("must end with a color"));
+
+       colorfield[colorfield_nr].hop = TIME_MAX;
+       string_list_clear(&l, 0);
+}
+
+static void setup_default_color_by_age(void)
+{
+       parse_color_fields("blue,12 month ago,white,1 month ago,red");
+}
+
+static void determine_line_heat(struct blame_entry *ent, const char **dest_color)
+{
+       int i = 0;
+       struct commit_info ci;
+       get_commit_info(ent->suspect->commit, &ci, 1);
+
+       while (i < colorfield_nr && ci.author_time > colorfield[i].hop)
+               i++;
+
+       *dest_color = colorfield[i].col;
+}
+
 static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int opt)
 {
        int cnt;
@@ -375,15 +441,35 @@ static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int
        struct commit_info ci;
        char hex[GIT_MAX_HEXSZ + 1];
        int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);
+       const char *default_color = NULL, *color = NULL, *reset = NULL;
 
        get_commit_info(suspect->commit, &ci, 1);
        oid_to_hex_r(hex, &suspect->commit->object.oid);
 
        cp = blame_nth_line(sb, ent->lno);
+
+       if (opt & OUTPUT_SHOW_AGE_WITH_COLOR) {
+               determine_line_heat(ent, &default_color);
+               color = default_color;
+               reset = GIT_COLOR_RESET;
+       }
+
        for (cnt = 0; cnt < ent->num_lines; cnt++) {
                char ch;
                int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? GIT_SHA1_HEXSZ : abbrev;
 
+               if (opt & OUTPUT_COLOR_LINE) {
+                       if (cnt > 0) {
+                               color = repeated_meta_color;
+                               reset = GIT_COLOR_RESET;
+                       } else  {
+                               color = default_color ? default_color : NULL;
+                               reset = default_color ? GIT_COLOR_RESET : NULL;
+                       }
+               }
+               if (color)
+                       fputs(color, stdout);
+
                if (suspect->commit->object.flags & UNINTERESTING) {
                        if (blank_boundary)
                                memset(hex, ' ', length);
@@ -433,6 +519,8 @@ static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int
                        printf(" %*d) ",
                               max_digits, ent->lno + 1 + cnt);
                }
+               if (reset)
+                       fputs(reset, stdout);
                do {
                        ch = *cp++;
                        putchar(ch);
@@ -457,7 +545,7 @@ static void output(struct blame_scoreboard *sb, int option)
                        struct commit *commit = ent->suspect->commit;
                        if (commit->object.flags & MORE_THAN_ONE_PATH)
                                continue;
-                       for (suspect = commit->util; suspect; suspect = suspect->next) {
+                       for (suspect = get_blame_suspects(commit); suspect; suspect = suspect->next) {
                                if (suspect->guilty && count++) {
                                        commit->object.flags |= MORE_THAN_ONE_PATH;
                                        break;
@@ -490,7 +578,7 @@ static int read_ancestry(const char *graft_file)
                /* The format is just "Commit Parent1 Parent2 ...\n" */
                struct commit_graft *graft = read_graft_line(&buf);
                if (graft)
-                       register_commit_graft(graft, 0);
+                       register_commit_graft(the_repository, graft, 0);
        }
        fclose(fp);
        strbuf_release(&buf);
@@ -499,7 +587,7 @@ static int read_ancestry(const char *graft_file)
 
 static int update_auto_abbrev(int auto_abbrev, struct blame_origin *suspect)
 {
-       const char *uniq = find_unique_abbrev(suspect->commit->object.oid.hash,
+       const char *uniq = find_unique_abbrev(&suspect->commit->object.oid,
                                              auto_abbrev);
        int len = strlen(uniq);
        if (auto_abbrev < len)
@@ -607,6 +695,30 @@ static int git_blame_config(const char *var, const char *value, void *cb)
                parse_date_format(value, &blame_date_mode);
                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"),
+                               value);
+               return 0;
+       }
+       if (!strcmp(var, "color.blame.highlightrecent")) {
+               parse_color_fields(value);
+               return 0;
+       }
+
+       if (!strcmp(var, "blame.coloring")) {
+               if (!strcmp(value, "repeatedLines")) {
+                       coloring_mode |= OUTPUT_COLOR_LINE;
+               } else if (!strcmp(value, "highlightRecent")) {
+                       coloring_mode |= OUTPUT_SHOW_AGE_WITH_COLOR;
+               } else if (!strcmp(value, "none")) {
+                       coloring_mode &= ~(OUTPUT_COLOR_LINE |
+                                           OUTPUT_SHOW_AGE_WITH_COLOR);
+               } else {
+                       warning(_("invalid value for blame.coloring"));
+                       return 0;
+               }
+       }
 
        if (git_diff_heuristic_config(var, value, cb) < 0)
                return -1;
@@ -620,6 +732,8 @@ static int blame_copy_callback(const struct option *option, const char *arg, int
 {
        int *opt = option->value;
 
+       BUG_ON_OPT_NEG(unset);
+
        /*
         * -C enables copy from removed files;
         * -C -C enables copy from existing files, but only
@@ -642,6 +756,8 @@ static int blame_move_callback(const struct option *option, const char *arg, int
 {
        int *opt = option->value;
 
+       BUG_ON_OPT_NEG(unset);
+
        *opt |= PICKAXE_BLAME_MOVE;
 
        if (arg)
@@ -649,6 +765,15 @@ static int blame_move_callback(const struct option *option, const char *arg, int
        return 0;
 }
 
+static int is_a_rev(const char *name)
+{
+       struct object_id oid;
+
+       if (get_oid(name, &oid))
+               return 0;
+       return OBJ_NONE < oid_object_info(the_repository, &oid, NULL);
+}
+
 int cmd_blame(int argc, const char **argv, const char *prefix)
 {
        struct rev_info revs;
@@ -681,6 +806,8 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
                OPT_BIT('s', NULL, &output_option, N_("Suppress author name and timestamp (Default: off)"), OUTPUT_NO_AUTHOR),
                OPT_BIT('e', "show-email", &output_option, N_("Show author email instead of name (Default: off)"), OUTPUT_SHOW_EMAIL),
                OPT_BIT('w', NULL, &xdl_opts, N_("Ignore whitespace differences"), XDF_IGNORE_WHITESPACE),
+               OPT_BIT(0, "color-lines", &output_option, N_("color redundant metadata from previous line differently"), OUTPUT_COLOR_LINE),
+               OPT_BIT(0, "color-by-age", &output_option, N_("color lines by age"), OUTPUT_SHOW_AGE_WITH_COLOR),
 
                /*
                 * The following two options are parsed by parse_revision_opt()
@@ -705,11 +832,12 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
        unsigned int range_i;
        long anchor;
 
+       setup_default_color_by_age();
        git_config(git_blame_config, &output_option);
-       init_revisions(&revs, NULL);
+       repo_init_revisions(the_repository, &revs, NULL);
        revs.date_mode = blame_date_mode;
-       DIFF_OPT_SET(&revs.diffopt, ALLOW_TEXTCONV);
-       DIFF_OPT_SET(&revs.diffopt, FOLLOW_RENAMES);
+       revs.diffopt.flags.allow_textconv = 1;
+       revs.diffopt.flags.follow_renames = 1;
 
        save_commit_buffer = 0;
        dashdash_pos = 0;
@@ -720,7 +848,10 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
        for (;;) {
                switch (parse_options_step(&ctx, options, blame_opt_usage)) {
                case PARSE_OPT_HELP:
+               case PARSE_OPT_ERROR:
                        exit(129);
+               case PARSE_OPT_COMPLETE:
+                       exit(0);
                case PARSE_OPT_DONE:
                        if (ctx.argv[0])
                                dashdash_pos = ctx.cpidx;
@@ -734,9 +865,9 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
                parse_revision_opt(&revs, &ctx, options, blame_opt_usage);
        }
 parse_done:
-       no_whole_file_rename = !DIFF_OPT_TST(&revs.diffopt, FOLLOW_RENAMES);
+       no_whole_file_rename = !revs.diffopt.flags.follow_renames;
        xdl_opts |= revs.diffopt.xdl_opts & XDF_INDENT_HEURISTIC;
-       DIFF_OPT_CLR(&revs.diffopt, FOLLOW_RENAMES);
+       revs.diffopt.flags.follow_renames = 0;
        argc = parse_options_end(&ctx);
 
        if (incremental || (output_option & OUTPUT_PORCELAIN)) {
@@ -803,7 +934,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
        }
        blame_date_width -= 1; /* strip the null */
 
-       if (DIFF_OPT_TST(&revs.diffopt, FIND_COPIES_HARDER))
+       if (revs.diffopt.flags.find_copies_harder)
                opt |= (PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE |
                        PICKAXE_BLAME_COPY_HARDER);
 
@@ -845,16 +976,15 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
        } else {
                if (argc < 2)
                        usage_with_options(blame_opt_usage, options);
-               path = add_prefix(prefix, argv[argc - 1]);
-               if (argc == 3 && !file_exists(path)) { /* (2b) */
+               if (argc == 3 && is_a_rev(argv[argc - 1])) { /* (2b) */
                        path = add_prefix(prefix, argv[1]);
                        argv[1] = argv[2];
+               } else {        /* (2a) */
+                       if (argc == 2 && is_a_rev(argv[1]) && !get_git_work_tree())
+                               die("missing <path> to blame");
+                       path = add_prefix(prefix, argv[argc - 1]);
                }
                argv[argc - 1] = "--";
-
-               setup_work_tree();
-               if (!file_exists(path))
-                       die_errno("cannot stat path '%s'", path);
        }
 
        revs.disable_stdin = 1;
@@ -864,6 +994,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
        sb.revs = &revs;
        sb.contents_from = contents_from;
        sb.reverse = reverse;
+       sb.repo = the_repository;
        setup_scoreboard(&sb, path, &o);
        lno = sb.num_lines;
 
@@ -876,15 +1007,15 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
                long bottom, top;
                if (parse_range_arg(range_list.items[range_i].string,
                                    nth_line_cb, &sb, lno, anchor,
-                                   &bottom, &top, sb.path))
+                                   &bottom, &top, sb.path, &the_index))
                        usage(blame_usage);
-               if (lno < top || ((lno || bottom) && lno < bottom))
+               if ((!lno && (top || bottom)) || lno < bottom)
                        die(Q_("file %s has only %lu line",
                               "file %s has only %lu lines",
                               lno), path, lno);
                if (bottom < 1)
                        bottom = 1;
-               if (top < 1)
+               if (top < 1 || lno < top)
                        top = lno;
                bottom--;
                range_set_append_unsafe(&ranges, bottom, top);
@@ -940,8 +1071,19 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
 
        blame_coalesce(&sb);
 
-       if (!(output_option & OUTPUT_PORCELAIN))
+       if (!(output_option & (OUTPUT_COLOR_LINE | OUTPUT_SHOW_AGE_WITH_COLOR)))
+               output_option |= coloring_mode;
+
+       if (!(output_option & OUTPUT_PORCELAIN)) {
                find_alignment(&sb, &output_option);
+               if (!*repeated_meta_color &&
+                   (output_option & OUTPUT_COLOR_LINE))
+                       xsnprintf(repeated_meta_color,
+                                 sizeof(repeated_meta_color),
+                                 "%s", GIT_COLOR_CYAN);
+       }
+       if (output_option & OUTPUT_ANNOTATE_COMPAT)
+               output_option &= ~(OUTPUT_COLOR_LINE | OUTPUT_SHOW_AGE_WITH_COLOR);
 
        output(&sb, output_option);
        free((void *)sb.final_buf);