Merge branch 'jk/http-auth-redirects'
[gitweb.git] / builtin / blame.c
index 079dcd3407881ce0ea988c3f487faf8ca6d3e5d2..6da7233968b6563d85d0689e3b1cae3ebe4027fb 100644 (file)
@@ -22,6 +22,7 @@
 #include "utf8.h"
 #include "userdiff.h"
 #include "line-range.h"
+#include "line-log.h"
 
 static char blame_usage[] = N_("git blame [options] [rev-opts] [rev] [--] file");
 
@@ -408,7 +409,7 @@ static struct origin *find_origin(struct scoreboard *sb,
        paths[0] = origin->path;
        paths[1] = NULL;
 
-       diff_tree_setup_paths(paths, &diff_opts);
+       parse_pathspec(&diff_opts.pathspec, PATHSPEC_ALL_MAGIC, 0, "", paths);
        diff_setup_done(&diff_opts);
 
        if (is_null_sha1(origin->commit->object.sha1))
@@ -458,7 +459,7 @@ static struct origin *find_origin(struct scoreboard *sb,
                }
        }
        diff_flush(&diff_opts);
-       diff_tree_release_paths(&diff_opts);
+       free_pathspec(&diff_opts.pathspec);
        if (porigin) {
                /*
                 * Create a freestanding copy that is not part of
@@ -486,15 +487,12 @@ static struct origin *find_rename(struct scoreboard *sb,
        struct origin *porigin = NULL;
        struct diff_options diff_opts;
        int i;
-       const char *paths[2];
 
        diff_setup(&diff_opts);
        DIFF_OPT_SET(&diff_opts, RECURSIVE);
        diff_opts.detect_rename = DIFF_DETECT_RENAME;
        diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
        diff_opts.single_follow = origin->path;
-       paths[0] = NULL;
-       diff_tree_setup_paths(paths, &diff_opts);
        diff_setup_done(&diff_opts);
 
        if (is_null_sha1(origin->commit->object.sha1))
@@ -516,7 +514,7 @@ static struct origin *find_rename(struct scoreboard *sb,
                }
        }
        diff_flush(&diff_opts);
-       diff_tree_release_paths(&diff_opts);
+       free_pathspec(&diff_opts.pathspec);
        return porigin;
 }
 
@@ -1064,7 +1062,6 @@ static int find_copy_in_parent(struct scoreboard *sb,
                               int opt)
 {
        struct diff_options diff_opts;
-       const char *paths[1];
        int i, j;
        int retval;
        struct blame_list *blame_list;
@@ -1078,8 +1075,6 @@ static int find_copy_in_parent(struct scoreboard *sb,
        DIFF_OPT_SET(&diff_opts, RECURSIVE);
        diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
 
-       paths[0] = NULL;
-       diff_tree_setup_paths(paths, &diff_opts);
        diff_setup_done(&diff_opts);
 
        /* Try "find copies harder" on new path if requested;
@@ -1162,7 +1157,7 @@ static int find_copy_in_parent(struct scoreboard *sb,
        }
        reset_scanned_flag(sb);
        diff_flush(&diff_opts);
-       diff_tree_release_paths(&diff_opts);
+       free_pathspec(&diff_opts.pathspec);
        return retval;
 }
 
@@ -1937,18 +1932,6 @@ static const char *add_prefix(const char *prefix, const char *path)
        return prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
 }
 
-/*
- * Parsing of -L option
- */
-static void prepare_blame_range(struct scoreboard *sb,
-                               const char *bottomtop,
-                               long lno,
-                               long *bottom, long *top)
-{
-       if (parse_range_arg(bottomtop, nth_line_cb, sb, lno, bottom, top, sb->path))
-               usage(blame_usage);
-}
-
 static int git_blame_config(const char *var, const char *value, void *cb)
 {
        if (!strcmp(var, "blame.showroot")) {
@@ -2245,38 +2228,27 @@ static int blame_move_callback(const struct option *option, const char *arg, int
        return 0;
 }
 
-static int blame_bottomtop_callback(const struct option *option, const char *arg, int unset)
-{
-       const char **bottomtop = option->value;
-       if (!arg)
-               return -1;
-       if (*bottomtop)
-               die("More than one '-L n,m' option given");
-       *bottomtop = arg;
-       return 0;
-}
-
 int cmd_blame(int argc, const char **argv, const char *prefix)
 {
        struct rev_info revs;
        const char *path;
        struct scoreboard sb;
        struct origin *o;
-       struct blame_entry *ent;
-       long dashdash_pos, bottom, top, lno;
+       struct blame_entry *ent = NULL;
+       long dashdash_pos, lno;
        const char *final_commit_name = NULL;
        enum object_type type;
 
-       static const char *bottomtop = NULL;
+       static struct string_list range_list;
        static int output_option = 0, opt = 0;
        static int show_stats = 0;
        static const char *revs_file = NULL;
        static const char *contents_from = NULL;
        static const struct option options[] = {
-               OPT_BOOLEAN(0, "incremental", &incremental, N_("Show blame entries as we find them, incrementally")),
-               OPT_BOOLEAN('b', NULL, &blank_boundary, N_("Show blank SHA-1 for boundary commits (Default: off)")),
-               OPT_BOOLEAN(0, "root", &show_root, N_("Do not treat root commits as boundaries (Default: off)")),
-               OPT_BOOLEAN(0, "show-stats", &show_stats, N_("Show work cost statistics")),
+               OPT_BOOL(0, "incremental", &incremental, N_("Show blame entries as we find them, incrementally")),
+               OPT_BOOL('b', NULL, &blank_boundary, N_("Show blank SHA-1 for boundary commits (Default: off)")),
+               OPT_BOOL(0, "root", &show_root, N_("Do not treat root commits as boundaries (Default: off)")),
+               OPT_BOOL(0, "show-stats", &show_stats, N_("Show work cost statistics")),
                OPT_BIT(0, "score-debug", &output_option, N_("Show output score for blame entries"), OUTPUT_SHOW_SCORE),
                OPT_BIT('f', "show-name", &output_option, N_("Show original filename (Default: auto)"), OUTPUT_SHOW_NAME),
                OPT_BIT('n', "show-number", &output_option, N_("Show original linenumber (Default: off)"), OUTPUT_SHOW_NUMBER),
@@ -2293,13 +2265,16 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
                OPT_STRING(0, "contents", &contents_from, N_("file"), N_("Use <file>'s contents as the final image")),
                { OPTION_CALLBACK, 'C', NULL, &opt, N_("score"), N_("Find line copies within and across files"), PARSE_OPT_OPTARG, blame_copy_callback },
                { OPTION_CALLBACK, 'M', NULL, &opt, N_("score"), N_("Find line movements within and across files"), PARSE_OPT_OPTARG, blame_move_callback },
-               OPT_CALLBACK('L', NULL, &bottomtop, N_("n,m"), N_("Process only line range n,m, counting from 1"), blame_bottomtop_callback),
+               OPT_STRING_LIST('L', NULL, &range_list, N_("n,m"), N_("Process only line range n,m, counting from 1")),
                OPT__ABBREV(&abbrev),
                OPT_END()
        };
 
        struct parse_opt_ctx_t ctx;
        int cmd_is_annotate = !strcmp(argv[0], "annotate");
+       struct range_set ranges;
+       unsigned int range_i;
+       long anchor;
 
        git_config(git_blame_config, NULL);
        init_revisions(&revs, NULL);
@@ -2492,22 +2467,48 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
        num_read_blob++;
        lno = prepare_lines(&sb);
 
-       bottom = top = 0;
-       if (bottomtop)
-               prepare_blame_range(&sb, bottomtop, lno, &bottom, &top);
-       if (bottom < 1)
-               bottom = 1;
-       if (top < 1)
-               top = lno;
-       bottom--;
-       if (lno < top || lno < bottom)
-               die("file %s has only %lu lines", path, lno);
-
-       ent = xcalloc(1, sizeof(*ent));
-       ent->lno = bottom;
-       ent->num_lines = top - bottom;
-       ent->suspect = o;
-       ent->s_lno = bottom;
+       if (lno && !range_list.nr)
+               string_list_append(&range_list, xstrdup("1"));
+
+       anchor = 1;
+       range_set_init(&ranges, range_list.nr);
+       for (range_i = 0; range_i < range_list.nr; ++range_i) {
+               long bottom, top;
+               if (parse_range_arg(range_list.items[range_i].string,
+                                   nth_line_cb, &sb, lno, anchor,
+                                   &bottom, &top, sb.path))
+                       usage(blame_usage);
+               if (lno < top || ((lno || bottom) && lno < bottom))
+                       die("file %s has only %lu lines", path, lno);
+               if (bottom < 1)
+                       bottom = 1;
+               if (top < 1)
+                       top = lno;
+               bottom--;
+               range_set_append_unsafe(&ranges, bottom, top);
+               anchor = top + 1;
+       }
+       sort_and_merge_range_set(&ranges);
+
+       for (range_i = ranges.nr; range_i > 0; --range_i) {
+               const struct range *r = &ranges.ranges[range_i - 1];
+               long bottom = r->start;
+               long top = r->end;
+               struct blame_entry *next = ent;
+               ent = xcalloc(1, sizeof(*ent));
+               ent->lno = bottom;
+               ent->num_lines = top - bottom;
+               ent->suspect = o;
+               ent->s_lno = bottom;
+               ent->next = next;
+               if (next)
+                       next->prev = ent;
+               origin_incref(o);
+       }
+       origin_decref(o);
+
+       range_set_release(&ranges);
+       string_list_clear(&range_list, 0);
 
        sb.ent = ent;
        sb.path = path;