@matheusbsbs / matheus-bagliano / commits / 931233bc66
Fork 0

git-pickaxe: -L /regexp/,/regexp/

With this change, you can specify the beginning and the ending line of the range you wish to inspect with pattern matching. For example, these are equivalent with the git.git sources: git pickaxe -L 7,21 v1.4.0 -- commit.c git pickaxe -L '/^struct sort_node/,/^}/' v1.4.0 -- commit.c git pickaxe -L '7,/^}/' v1.4.0 -- commit.c git pickaxe -L '/^struct sort_node/,21' v1.4.0 -- commit.c Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Nov 6, 2006 at 17:08 UTC 931233bc666b40de039d50f82f12279c94e5e5f0
1 file changed +83 -13
builtin-pickaxe.c
+83 -13
@@ -17,6 +17,7 @@
17
18 #include <time.h>
19 #include <sys/time.h>
20 +#include <regex.h>
21
22 static char pickaxe_usage[] =
23 "git-pickaxe [-c] [-l] [-t] [-f] [-n] [-p] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [commit] [--] file\n"
@@ -1533,6 +1534,78 @@ static const char *add_prefix(const char *prefix, const char *path)
1534 return prefix_path(prefix, strlen(prefix), path);
1535 }
1536
1537 +static const char *parse_loc(const char *spec,
1538 + struct scoreboard *sb, long lno,
1539 + long begin, long *ret)
1540 +{
1541 + char *term;
1542 + const char *line;
1543 + long num;
1544 + int reg_error;
1545 + regex_t regexp;
1546 + regmatch_t match[1];
1547 +
1548 + num = strtol(spec, &term, 10);
1549 + if (term != spec) {
1550 + *ret = num;
1551 + return term;
1552 + }
1553 + if (spec[0] != '/')
1554 + return spec;
1555 +
1556 + /* it could be a regexp of form /.../ */
1557 + for (term = (char*) spec + 1; *term && *term != '/'; term++) {
1558 + if (*term == '\\')
1559 + term++;
1560 + }
1561 + if (*term != '/')
1562 + return spec;
1563 +
1564 + /* try [spec+1 .. term-1] as regexp */
1565 + *term = 0;
1566 + begin--; /* input is in human terms */
1567 + line = nth_line(sb, begin);
1568 +
1569 + if (!(reg_error = regcomp(&regexp, spec + 1, REG_NEWLINE)) &&
1570 + !(reg_error = regexec(&regexp, line, 1, match, 0))) {
1571 + const char *cp = line + match[0].rm_so;
1572 + const char *nline;
1573 +
1574 + while (begin++ < lno) {
1575 + nline = nth_line(sb, begin);
1576 + if (line <= cp && cp < nline)
1577 + break;
1578 + line = nline;
1579 + }
1580 + *ret = begin;
1581 + regfree(&regexp);
1582 + *term++ = '/';
1583 + return term;
1584 + }
1585 + else {
1586 + char errbuf[1024];
1587 + regerror(reg_error, &regexp, errbuf, 1024);
1588 + die("-L parameter '%s': %s", spec + 1, errbuf);
1589 + }
1590 +}
1591 +
1592 +static void prepare_blame_range(struct scoreboard *sb,
1593 + const char *bottomtop,
1594 + long lno,
1595 + long *bottom, long *top)
1596 +{
1597 + const char *term;
1598 +
1599 + term = parse_loc(bottomtop, sb, lno, 1, bottom);
1600 + if (*term == ',') {
1601 + term = parse_loc(term + 1, sb, lno, *bottom + 1, top);
1602 + if (*term)
1603 + usage(pickaxe_usage);
1604 + }
1605 + if (*term)
1606 + usage(pickaxe_usage);
1607 +}
1608 +
1609 int cmd_pickaxe(int argc, const char **argv, const char *prefix)
1610 {
1611 struct rev_info revs;
@@ -1546,11 +1619,11 @@ int cmd_pickaxe(int argc, const char **argv, const char *prefix)
1619 const char *revs_file = NULL;
1620 const char *final_commit_name = NULL;
1621 char type[10];
1622 + const char *bottomtop = NULL;
1623
1624 save_commit_buffer = 0;
1625
1626 opt = 0;
1553 - bottom = top = 0;
1627 seen_dashdash = 0;
1628 for (unk = i = 1; i < argc; i++) {
1629 const char *arg = argv[i];
@@ -1575,7 +1648,6 @@ int cmd_pickaxe(int argc, const char **argv, const char *prefix)
1648 blame_copy_score = parse_score(arg+2);
1649 }
1650 else if (!strncmp("-L", arg, 2)) {
1578 - char *term;
1651 if (!arg[2]) {
1652 if (++i >= argc)
1653 usage(pickaxe_usage);
@@ -1583,18 +1655,9 @@ int cmd_pickaxe(int argc, const char **argv, const char *prefix)
1655 }
1656 else
1657 arg += 2;
1586 - if (bottom || top)
1658 + if (bottomtop)
1659 die("More than one '-L n,m' option given");
1588 - bottom = strtol(arg, &term, 10);
1589 - if (*term == ',') {
1590 - top = strtol(term + 1, &term, 10);
1591 - if (*term)
1592 - usage(pickaxe_usage);
1593 - }
1594 - if (bottom && top && top < bottom) {
1595 - unsigned long tmp;
1596 - tmp = top; top = bottom; bottom = tmp;
1597 - }
1660 + bottomtop = arg;
1661 }
1662 else if (!strcmp("--score-debug", arg))
1663 output_option |= OUTPUT_SHOW_SCORE;
@@ -1755,6 +1818,13 @@ int cmd_pickaxe(int argc, const char **argv, const char *prefix)
1818 num_read_blob++;
1819 lno = prepare_lines(&sb);
1820
1821 + bottom = top = 0;
1822 + if (bottomtop)
1823 + prepare_blame_range(&sb, bottomtop, lno, &bottom, &top);
1824 + if (bottom && top && top < bottom) {
1825 + long tmp;
1826 + tmp = top; top = bottom; bottom = tmp;
1827 + }
1828 if (bottom < 1)
1829 bottom = 1;
1830 if (top < 1)