blame: accept multiple -L ranges

git-blame accepts only a single -L option or none. Clients requiring blame information for multiple disjoint ranges are therefore forced either to invoke git-blame multiple times, once for each range, or only once with no -L option to cover the entire file, both of which can be costly. Teach git-blame to accept multiple -L ranges. Overlapping and out-of-order ranges are accepted. In this patch, the X in -LX,Y is absolute (for instance, /RE/ patterns search from line 1), and Y is relative to X. Follow-up patches provide more flexibility over how X is anchored. Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Aug 6, 2013 at 09:59 UTC 58dbfa2e59a11b3dd060d69788af4c3decc7ee22
1 file changed +47 -32
builtin/blame.c
+47 -32
@@ -22,6 +22,7 @@
22 #include "utf8.h"
23 #include "userdiff.h"
24 #include "line-range.h"
25 +#include "line-log.h"
26
27 static char blame_usage[] = N_("git blame [options] [rev-opts] [rev] [--] file");
28
@@ -2233,29 +2234,18 @@ static int blame_move_callback(const struct option *option, const char *arg, int
2234 return 0;
2235 }
2236
2236 -static int blame_bottomtop_callback(const struct option *option, const char *arg, int unset)
2237 -{
2238 - const char **bottomtop = option->value;
2239 - if (!arg)
2240 - return -1;
2241 - if (*bottomtop)
2242 - die("More than one '-L n,m' option given");
2243 - *bottomtop = arg;
2244 - return 0;
2245 -}
2246 -
2237 int cmd_blame(int argc, const char **argv, const char *prefix)
2238 {
2239 struct rev_info revs;
2240 const char *path;
2241 struct scoreboard sb;
2242 struct origin *o;
2253 - struct blame_entry *ent;
2254 - long dashdash_pos, bottom, top, lno;
2243 + struct blame_entry *ent = NULL;
2244 + long dashdash_pos, lno;
2245 const char *final_commit_name = NULL;
2246 enum object_type type;
2247
2258 - static const char *bottomtop = NULL;
2248 + static struct string_list range_list;
2249 static int output_option = 0, opt = 0;
2250 static int show_stats = 0;
2251 static const char *revs_file = NULL;
@@ -2281,13 +2271,15 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
2271 OPT_STRING(0, "contents", &contents_from, N_("file"), N_("Use <file>'s contents as the final image")),
2272 { OPTION_CALLBACK, 'C', NULL, &opt, N_("score"), N_("Find line copies within and across files"), PARSE_OPT_OPTARG, blame_copy_callback },
2273 { OPTION_CALLBACK, 'M', NULL, &opt, N_("score"), N_("Find line movements within and across files"), PARSE_OPT_OPTARG, blame_move_callback },
2284 - OPT_CALLBACK('L', NULL, &bottomtop, N_("n,m"), N_("Process only line range n,m, counting from 1"), blame_bottomtop_callback),
2274 + OPT_STRING_LIST('L', NULL, &range_list, N_("n,m"), N_("Process only line range n,m, counting from 1")),
2275 OPT__ABBREV(&abbrev),
2276 OPT_END()
2277 };
2278
2279 struct parse_opt_ctx_t ctx;
2280 int cmd_is_annotate = !strcmp(argv[0], "annotate");
2281 + struct range_set ranges;
2282 + unsigned int range_i;
2283
2284 git_config(git_blame_config, NULL);
2285 init_revisions(&revs, NULL);
@@ -2480,23 +2472,46 @@ parse_done:
2472 num_read_blob++;
2473 lno = prepare_lines(&sb);
2474
2483 - bottom = top = 0;
2484 - if (bottomtop && parse_range_arg(bottomtop, nth_line_cb, &sb, lno,
2485 - &bottom, &top, sb.path))
2486 - usage(blame_usage);
2487 - if (lno < top || ((lno || bottom) && lno < bottom))
2488 - die("file %s has only %lu lines", path, lno);
2489 - if (bottom < 1)
2490 - bottom = 1;
2491 - if (top < 1)
2492 - top = lno;
2493 - bottom--;
2494 -
2495 - ent = xcalloc(1, sizeof(*ent));
2496 - ent->lno = bottom;
2497 - ent->num_lines = top - bottom;
2498 - ent->suspect = o;
2499 - ent->s_lno = bottom;
2475 + if (lno && !range_list.nr)
2476 + string_list_append(&range_list, xstrdup("1"));
2477 +
2478 + range_set_init(&ranges, range_list.nr);
2479 + for (range_i = 0; range_i < range_list.nr; ++range_i) {
2480 + long bottom, top;
2481 + if (parse_range_arg(range_list.items[range_i].string,
2482 + nth_line_cb, &sb, lno,
2483 + &bottom, &top, sb.path))
2484 + usage(blame_usage);
2485 + if (lno < top || ((lno || bottom) && lno < bottom))
2486 + die("file %s has only %lu lines", path, lno);
2487 + if (bottom < 1)
2488 + bottom = 1;
2489 + if (top < 1)
2490 + top = lno;
2491 + bottom--;
2492 + range_set_append_unsafe(&ranges, bottom, top);
2493 + }
2494 + sort_and_merge_range_set(&ranges);
2495 +
2496 + for (range_i = ranges.nr; range_i > 0; --range_i) {
2497 + const struct range *r = &ranges.ranges[range_i - 1];
2498 + long bottom = r->start;
2499 + long top = r->end;
2500 + struct blame_entry *next = ent;
2501 + ent = xcalloc(1, sizeof(*ent));
2502 + ent->lno = bottom;
2503 + ent->num_lines = top - bottom;
2504 + ent->suspect = o;
2505 + ent->s_lno = bottom;
2506 + ent->next = next;
2507 + if (next)
2508 + next->prev = ent;
2509 + origin_incref(o);
2510 + }
2511 + origin_decref(o);
2512 +
2513 + range_set_release(&ranges);
2514 + string_list_clear(&range_list, 0);
2515
2516 sb.ent = ent;
2517 sb.path = path;