{"thread":{"id":"33038","subject":"[PATCH v8 2/5] blame: introduce $ as \"end of file\" in -L syntax","startedAt":"2013-02-28T16:38:18Z","lastAt":"2013-03-13T16:05:23Z","messageCount":21,"participants":["Thomas Rast","Junio C Hamano"],"isPatch":true,"patchVersion":8,"patchTotal":5},"messages":[{"id":"210422","messageId":"cover.1362069310.git.trast@student.ethz.ch","threadId":"33038","inReplyTo":null,"subject":"[PATCH v8 0/5] git log -L","fromName":"Thomas Rast","fromEmail":"trast@student.ethz.ch","sentAt":"2013-02-28T16:38:18Z","receivedAt":"2013-02-28T16:38:18Z","isPatch":true,"sender":{"key":"tr@thomasrast.ch","avatar":"https://avatars.githubusercontent.com/u/153510?v=4"},"body":"Here's another shot :-)\n\nFor an instructive example, apply it and run\n\n  git log -L:archiver:archive.h\n\nin your git.git.\n\nLast time around, apart from some minor issues, there was one main\npoint discussed[1]: it should be possible to do the line-log in two\nseparate steps, one for filtering and one for the diffing.\n\nThat would have some really nice advantages, in particular it would\nlet us generate the diff within the usual diff chain.  That way it\nwould work together with e.g. --word-diff automatically.\n\nHowever, with the whole diff chain IMHO already being in some\ndisarray, that's such a far-off goal (and a good example of gsoc scope\ncreep!) that I can't see myself working on it in the near future\nunless someone decides to pay me for working on Git.  Meanwhile, this\nseems to be the only blocker for the inclusion of a feature for which\nall feedback has been \"WANT!\" since it worked for the first time.\n\nSo I'd rather settle for including this slightly hacky version, and\nthen improving on it incrementally.\n\nThere are more things that need to be done: my rewrite for readability\n(see [1] again) lost the support for -C and -C -C, so it no longer is\nany use with big code moves.  The only thing that currently works is\n-M, which will track the ranges across a file rename that can be\ndetected by the diff engine (i.e., it works by letting the diff engine\nform a rename filepair).\n\nChanges since v7 include:\n\n- An all-new patch that allows for funcname matching: -L:pattern:file.\n  I find it makes calling line-log (or blame, for that matter) much\n  more natural.  I'm not exactly married to the syntax; in particular\n  it feels weird in blame where you have to say 'git blame -L:foo file'.\n  If you have better ideas, please shout.\n\n- Tests!  Yay.  Also fixes for bugs found by the tests ;-)\n\n- Dropped the unnecessary patch \"Export three functions from diff.c\".\n\n\nBo Yang (2):\n  Refactor parse_loc\n  Export rewrite_parents() for 'log -L'\n\nThomas Rast (3):\n  blame: introduce $ as \"end of file\" in -L syntax\n  Implement line-history search (git log -L)\n  log -L: :pattern:file syntax to find by funcname\n\n Documentation/blame-options.txt     |   19 +-\n Documentation/git-log.txt           |   22 +\n Documentation/line-range-format.txt |   31 +\n Makefile                            |    2 +\n builtin/blame.c                     |   99 +--\n builtin/log.c                       |   31 +\n line-log.c                          | 1433 +++++++++++++++++++++++++++++++++++\n line-log.h                          |   80 ++\n log-tree.c                          |    4 +\n revision.c                          |   22 +-\n revision.h                          |   16 +-\n t/t4211-line-log.sh                 |   42 +\n t/t4211/expect.beginning-of-file    |   43 ++\n t/t4211/expect.end-of-file          |   62 ++\n t/t4211/expect.move-support-f       |   40 +\n t/t4211/expect.simple-f             |   59 ++\n t/t4211/expect.simple-f-to-main     |  100 +++\n t/t4211/expect.simple-main          |   68 ++\n t/t4211/expect.simple-main-to-end   |   70 ++\n t/t4211/expect.two-ranges           |  102 +++\n t/t4211/expect.vanishes-early       |   39 +\n t/t4211/history.export              |  330 ++++++++\n t/t8003-blame-corner-cases.sh       |    6 +\n 23 files changed, 2601 insertions(+), 119 deletions(-)\n create mode 100644 Documentation/line-range-format.txt\n create mode 100644 line-log.c\n create mode 100644 line-log.h\n create mode 100755 t/t4211-line-log.sh\n create mode 100644 t/t4211/expect.beginning-of-file\n create mode 100644 t/t4211/expect.end-of-file\n create mode 100644 t/t4211/expect.move-support-f\n create mode 100644 t/t4211/expect.simple-f\n create mode 100644 t/t4211/expect.simple-f-to-main\n create mode 100644 t/t4211/expect.simple-main\n create mode 100644 t/t4211/expect.simple-main-to-end\n create mode 100644 t/t4211/expect.two-ranges\n create mode 100644 t/t4211/expect.vanishes-early\n create mode 100644 t/t4211/history.export\n\n-- \n1.8.2.rc1.388.g1bd82c8\n"},{"id":"210426","messageId":"d22070b416a1787ece6f98a722614a54826b93ce.1362069310.git.trast@student.ethz.ch","threadId":"33038","inReplyTo":"cover.1362069310.git.trast@student.ethz.ch","subject":"[PATCH v8 1/5] Refactor parse_loc","fromName":"Thomas Rast","fromEmail":"trast@student.ethz.ch","sentAt":"2013-02-28T16:38:19Z","receivedAt":"2013-02-28T16:38:19Z","isPatch":true,"sender":{"key":"tr@thomasrast.ch","avatar":"https://avatars.githubusercontent.com/u/153510?v=4"},"body":"From: Bo Yang <struggleyb.nku@gmail.com>\n\nWe want to use the same style of -L n,m argument for 'git log -L' as\nfor git-blame.  Refactor the argument parsing of the range arguments\nfrom builtin/blame.c to the (new) file that will hold the 'git log -L'\nlogic.\n\nTo accommodate different data structures in blame and log -L, the file\ncontents are abstracted away; parse_range_arg takes a callback that it\nuses to get the contents of a line of the (notional) file.\n\nThe new test is for a case that made me pause during debugging: the\n'blame -L with invalid end' test was the only one that noticed an\noutright failure to parse the end *at all*.  So make a more explicit\ntest for that.\n\nSigned-off-by: Bo Yang <struggleyb.nku@gmail.com>\nSigned-off-by: Thomas Rast <trast@student.ethz.ch>\n---\n Documentation/blame-options.txt     |  19 +------\n Documentation/line-range-format.txt |  18 +++++++\n Makefile                            |   2 +\n builtin/blame.c                     |  99 +++-------------------------------\n line-log.c                          | 105 ++++++++++++++++++++++++++++++++++++\n line-log.h                          |  23 ++++++++\n t/t8003-blame-corner-cases.sh       |   6 +++\n 7 files changed, 163 insertions(+), 109 deletions(-)\n create mode 100644 Documentation/line-range-format.txt\n create mode 100644 line-log.c\n create mode 100644 line-log.h\n\ndiff --git a/Documentation/blame-options.txt b/Documentation/blame-options.txt\nindex b0d31df..6998d9f 100644\n--- a/Documentation/blame-options.txt\n+++ b/Documentation/blame-options.txt\n@@ -13,24 +13,7 @@\n \tAnnotate only the given line range.  <start> and <end> can take\n \tone of these forms:\n \n-\t- number\n-+\n-If <start> or <end> is a number, it specifies an\n-absolute line number (lines count from 1).\n-+\n-\n-- /regex/\n-+\n-This form will use the first line matching the given\n-POSIX regex.  If <end> is a regex, it will search\n-starting at the line given by <start>.\n-+\n-\n-- +offset or -offset\n-+\n-This is only valid for <end> and will specify a number\n-of lines before or after the line given by <start>.\n-+\n+include::line-range-format.txt[]\n \n -l::\n \tShow long rev (Default: off).\ndiff --git a/Documentation/line-range-format.txt b/Documentation/line-range-format.txt\nnew file mode 100644\nindex 0000000..265bc23\n--- /dev/null\n+++ b/Documentation/line-range-format.txt\n@@ -0,0 +1,18 @@\n+- number\n++\n+If <start> or <end> is a number, it specifies an\n+absolute line number (lines count from 1).\n++\n+\n+- /regex/\n++\n+This form will use the first line matching the given\n+POSIX regex.  If <end> is a regex, it will search\n+starting at the line given by <start>.\n++\n+\n+- +offset or -offset\n++\n+This is only valid for <end> and will specify a number\n+of lines before or after the line given by <start>.\n++\ndiff --git a/Makefile b/Makefile\nindex 26d3332..76831fe 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -683,6 +683,7 @@ LIB_H += help.h\n LIB_H += http.h\n LIB_H += kwset.h\n LIB_H += levenshtein.h\n+LIB_H += line-log.h\n LIB_H += list-objects.h\n LIB_H += ll-merge.h\n LIB_H += log-tree.h\n@@ -798,6 +799,7 @@ LIB_OBJS += hex.o\n LIB_OBJS += ident.o\n LIB_OBJS += kwset.o\n LIB_OBJS += levenshtein.o\n+LIB_OBJS += line-log.o\n LIB_OBJS += list-objects.o\n LIB_OBJS += ll-merge.o\n LIB_OBJS += lockfile.o\ndiff --git a/builtin/blame.c b/builtin/blame.c\nindex 86100e9..c933b15 100644\n--- a/builtin/blame.c\n+++ b/builtin/blame.c\n@@ -21,6 +21,7 @@\n #include \"parse-options.h\"\n #include \"utf8.h\"\n #include \"userdiff.h\"\n+#include \"line-log.h\"\n \n static char blame_usage[] = N_(\"git blame [options] [rev-opts] [rev] [--] file\");\n \n@@ -566,11 +567,16 @@ static void dup_entry(struct blame_entry *dst, struct blame_entry *src)\n \tdst->score = 0;\n }\n \n-static const char *nth_line(struct scoreboard *sb, int lno)\n+static const char *nth_line(struct scoreboard *sb, long lno)\n {\n \treturn sb->final_buf + sb->lineno[lno];\n }\n \n+static const char *nth_line_cb(void *data, long lno)\n+{\n+\treturn nth_line((struct scoreboard *)data, lno);\n+}\n+\n /*\n  * It is known that lines between tlno to same came from parent, and e\n  * has an overlap with that range.  it also is known that parent's\n@@ -1927,83 +1933,6 @@ static const char *add_prefix(const char *prefix, const char *path)\n }\n \n /*\n- * Parsing of (comma separated) one item in the -L option\n- */\n-static const char *parse_loc(const char *spec,\n-\t\t\t     struct scoreboard *sb, long lno,\n-\t\t\t     long begin, long *ret)\n-{\n-\tchar *term;\n-\tconst char *line;\n-\tlong num;\n-\tint reg_error;\n-\tregex_t regexp;\n-\tregmatch_t match[1];\n-\n-\t/* Allow \"-L <something>,+20\" to mean starting at <something>\n-\t * for 20 lines, or \"-L <something>,-5\" for 5 lines ending at\n-\t * <something>.\n-\t */\n-\tif (1 < begin && (spec[0] == '+' || spec[0] == '-')) {\n-\t\tnum = strtol(spec + 1, &term, 10);\n-\t\tif (term != spec + 1) {\n-\t\t\tif (spec[0] == '-')\n-\t\t\t\tnum = 0 - num;\n-\t\t\tif (0 < num)\n-\t\t\t\t*ret = begin + num - 2;\n-\t\t\telse if (!num)\n-\t\t\t\t*ret = begin;\n-\t\t\telse\n-\t\t\t\t*ret = begin + num;\n-\t\t\treturn term;\n-\t\t}\n-\t\treturn spec;\n-\t}\n-\tnum = strtol(spec, &term, 10);\n-\tif (term != spec) {\n-\t\t*ret = num;\n-\t\treturn term;\n-\t}\n-\tif (spec[0] != '/')\n-\t\treturn spec;\n-\n-\t/* it could be a regexp of form /.../ */\n-\tfor (term = (char *) spec + 1; *term && *term != '/'; term++) {\n-\t\tif (*term == '\\\\')\n-\t\t\tterm++;\n-\t}\n-\tif (*term != '/')\n-\t\treturn spec;\n-\n-\t/* try [spec+1 .. term-1] as regexp */\n-\t*term = 0;\n-\tbegin--; /* input is in human terms */\n-\tline = nth_line(sb, begin);\n-\n-\tif (!(reg_error = regcomp(&regexp, spec + 1, REG_NEWLINE)) &&\n-\t    !(reg_error = regexec(&regexp, line, 1, match, 0))) {\n-\t\tconst char *cp = line + match[0].rm_so;\n-\t\tconst char *nline;\n-\n-\t\twhile (begin++ < lno) {\n-\t\t\tnline = nth_line(sb, begin);\n-\t\t\tif (line <= cp && cp < nline)\n-\t\t\t\tbreak;\n-\t\t\tline = nline;\n-\t\t}\n-\t\t*ret = begin;\n-\t\tregfree(&regexp);\n-\t\t*term++ = '/';\n-\t\treturn term;\n-\t}\n-\telse {\n-\t\tchar errbuf[1024];\n-\t\tregerror(reg_error, &regexp, errbuf, 1024);\n-\t\tdie(\"-L parameter '%s': %s\", spec + 1, errbuf);\n-\t}\n-}\n-\n-/*\n  * Parsing of -L option\n  */\n static void prepare_blame_range(struct scoreboard *sb,\n@@ -2011,15 +1940,7 @@ static void prepare_blame_range(struct scoreboard *sb,\n \t\t\t\tlong lno,\n \t\t\t\tlong *bottom, long *top)\n {\n-\tconst char *term;\n-\n-\tterm = parse_loc(bottomtop, sb, lno, 1, bottom);\n-\tif (*term == ',') {\n-\t\tterm = parse_loc(term + 1, sb, lno, *bottom + 1, top);\n-\t\tif (*term)\n-\t\t\tusage(blame_usage);\n-\t}\n-\tif (*term)\n+\tif (parse_range_arg(bottomtop, nth_line_cb, sb, lno, bottom, top))\n \t\tusage(blame_usage);\n }\n \n@@ -2569,10 +2490,6 @@ int cmd_blame(int argc, const char **argv, const char *prefix)\n \tbottom = top = 0;\n \tif (bottomtop)\n \t\tprepare_blame_range(&sb, bottomtop, lno, &bottom, &top);\n-\tif (bottom && top && top < bottom) {\n-\t\tlong tmp;\n-\t\ttmp = top; top = bottom; bottom = tmp;\n-\t}\n \tif (bottom < 1)\n \t\tbottom = 1;\n \tif (top < 1)\ndiff --git a/line-log.c b/line-log.c\nnew file mode 100644\nindex 0000000..a24a86b\n--- /dev/null\n+++ b/line-log.c\n@@ -0,0 +1,105 @@\n+#include \"git-compat-util.h\"\n+#include \"line-log.h\"\n+\n+/*\n+ * Parse one item in the -L option\n+ */\n+const char *parse_loc(const char *spec, nth_line_fn_t nth_line,\n+\t\tvoid *data, long lines, long begin, long *ret)\n+{\n+\tchar *term;\n+\tconst char *line;\n+\tlong num;\n+\tint reg_error;\n+\tregex_t regexp;\n+\tregmatch_t match[1];\n+\n+\t/*\n+\t * Allow \"-L <something>,+20\" to mean starting at <something>\n+\t * for 20 lines, or \"-L <something>,-5\" for 5 lines ending at\n+\t * <something>.\n+\t */\n+\tif (begin != -1 && (spec[0] == '+' || spec[0] == '-')) {\n+\t\tnum = strtol(spec + 1, &term, 10);\n+\t\tif (term != spec + 1) {\n+\t\t\tif (spec[0] == '-')\n+\t\t\t\tnum = 0 - num;\n+\t\t\tif (0 < num)\n+\t\t\t\t*ret = begin + num - 2;\n+\t\t\telse if (!num)\n+\t\t\t\t*ret = begin;\n+\t\t\telse\n+\t\t\t\t*ret = begin + num;\n+\t\t\treturn term;\n+\t\t}\n+\t\treturn spec;\n+\t}\n+\tnum = strtol(spec, &term, 10);\n+\tif (term != spec) {\n+\t\t*ret = num;\n+\t\treturn term;\n+\t}\n+\tif (spec[0] != '/')\n+\t\treturn spec;\n+\n+\t/* it could be a regexp of form /.../ */\n+\tfor (term = (char *) spec + 1; *term && *term != '/'; term++) {\n+\t\tif (*term == '\\\\')\n+\t\t\tterm++;\n+\t}\n+\tif (*term != '/')\n+\t\treturn spec;\n+\n+\t/* try [spec+1 .. term-1] as regexp */\n+\t*term = 0;\n+\tif (begin == -1)\n+\t\tbegin = 1;\n+\tbegin--; /* input is in human terms */\n+\tline = nth_line(data, begin);\n+\n+\tif (!(reg_error = regcomp(&regexp, spec + 1, REG_NEWLINE)) &&\n+\t    !(reg_error = regexec(&regexp, line, 1, match, 0))) {\n+\t\tconst char *cp = line + match[0].rm_so;\n+\t\tconst char *nline;\n+\n+\t\twhile (begin++ < lines) {\n+\t\t\tnline = nth_line(data, begin);\n+\t\t\tif (line <= cp && cp < nline)\n+\t\t\t\tbreak;\n+\t\t\tline = nline;\n+\t\t}\n+\t\t*ret = begin;\n+\t\tregfree(&regexp);\n+\t\t*term++ = '/';\n+\t\treturn term;\n+\t} else {\n+\t\tchar errbuf[1024];\n+\t\tregerror(reg_error, &regexp, errbuf, 1024);\n+\t\tdie(\"-L parameter '%s': %s\", spec + 1, errbuf);\n+\t}\n+}\n+\n+int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,\n+\t\tvoid *cb_data, long lines, long *begin, long *end)\n+{\n+\targ = parse_loc(arg, nth_line_cb, cb_data, lines, -1, begin);\n+\n+\tif (*arg == ',') {\n+\t\targ = parse_loc(arg+1, nth_line_cb, cb_data, lines, *begin+1, end);\n+\t\tif (*begin > *end) {\n+\t\t\tlong tmp = *begin;\n+\t\t\t*begin = *end;\n+\t\t\t*end = tmp;\n+\t\t}\n+\t}\n+\n+\tif (*begin <= 0)\n+\t\t*begin = 1;\n+\tif (*end > lines)\n+\t\t*end = lines;\n+\n+\tif (*arg)\n+\t\treturn -1;\n+\n+\treturn 0;\n+}\ndiff --git a/line-log.h b/line-log.h\nnew file mode 100644\nindex 0000000..22b1c80\n--- /dev/null\n+++ b/line-log.h\n@@ -0,0 +1,23 @@\n+#ifndef LINE_LOG_H\n+#define LINE_LOG_H\n+\n+/*\n+ * Parse one item in an -L begin,end option w.r.t. the notional file\n+ * object 'cb_data' consisting of 'lines' lines.\n+ *\n+ * The 'nth_line_cb' callback is used to determine the start of the\n+ * line 'lno' inside the 'cb_data'.  The caller is expected to already\n+ * have a suitable map at hand to make this a constant-time lookup.\n+ *\n+ * Returns 0 in case of success and -1 if there was an error.  The\n+ * caller should print a usage message in the latter case.\n+ */\n+\n+typedef const char *(*nth_line_fn_t)(void *data, long lno);\n+\n+extern int parse_range_arg(const char *arg,\n+\t\t\t   nth_line_fn_t nth_line_cb,\n+\t\t\t   void *cb_data, long lines,\n+\t\t\t   long *begin, long *end);\n+\n+#endif /* LINE_LOG_H */\ndiff --git a/t/t8003-blame-corner-cases.sh b/t/t8003-blame-corner-cases.sh\nindex 230143c..e7cac1d 100755\n--- a/t/t8003-blame-corner-cases.sh\n+++ b/t/t8003-blame-corner-cases.sh\n@@ -175,6 +175,12 @@ test_expect_success 'blame -L with invalid end' '\n \tgrep \"has only 2 lines\" errors\n '\n \n+test_expect_success 'blame parses <end> part of -L' '\n+\tgit blame -L1,1 tres >out &&\n+\tcat out &&\n+\ttest $(wc -l < out) -eq 1\n+'\n+\n test_expect_success 'indent of line numbers, nine lines' '\n \tgit blame nine_lines >actual &&\n \ttest $(grep -c \"  \" actual) = 0\n-- \n1.8.2.rc1.388.g1bd82c8\n"},{"id":"210421","messageId":"7973d90cfcd86a3c15776b8718ad6bd84ee7b4ac.1362069310.git.trast@student.ethz.ch","threadId":"33038","inReplyTo":"cover.1362069310.git.trast@student.ethz.ch","subject":"[PATCH v8 2/5] blame: introduce $ as \"end of file\" in -L syntax","fromName":"Thomas Rast","fromEmail":"trast@student.ethz.ch","sentAt":"2013-02-28T16:38:20Z","receivedAt":"2013-02-28T16:38:20Z","isPatch":true,"sender":{"key":"tr@thomasrast.ch","avatar":"https://avatars.githubusercontent.com/u/153510?v=4"},"body":"To save the user a lookup of the last line number, introduce $ as a\nshorthand for the last line.  This is mostly useful to spell \"until\nthe end of the file\" as '-L<begin>,$'.\n\nSigned-off-by: Thomas Rast <trast@student.ethz.ch>\n---\n Documentation/line-range-format.txt | 6 ++++++\n line-log.c                          | 8 ++++++++\n 2 files changed, 14 insertions(+)\n\ndiff --git a/Documentation/line-range-format.txt b/Documentation/line-range-format.txt\nindex 265bc23..9ce0688 100644\n--- a/Documentation/line-range-format.txt\n+++ b/Documentation/line-range-format.txt\n@@ -16,3 +16,9 @@ starting at the line given by <start>.\n This is only valid for <end> and will specify a number\n of lines before or after the line given by <start>.\n +\n+\n+- `$`\n++\n+A literal dollar sign can be used as a shorthand for the last line in\n+the file.\n++\ndiff --git a/line-log.c b/line-log.c\nindex a24a86b..b167b00 100644\n--- a/line-log.c\n+++ b/line-log.c\n@@ -15,6 +15,14 @@ const char *parse_loc(const char *spec, nth_line_fn_t nth_line,\n \tregmatch_t match[1];\n \n \t/*\n+\t * $ is a synonym for \"the end of the file\".\n+\t */\n+\tif (spec[0] == '$') {\n+\t\t*ret = lines;\n+\t\treturn spec + 1;\n+\t}\n+\n+\t/*\n \t * Allow \"-L <something>,+20\" to mean starting at <something>\n \t * for 20 lines, or \"-L <something>,-5\" for 5 lines ending at\n \t * <something>.\n-- \n1.8.2.rc1.388.g1bd82c8\n"},{"id":"210423","messageId":"8269ddf785d10c05e39905041df317b685e85168.1362069310.git.trast@student.ethz.ch","threadId":"33038","inReplyTo":"cover.1362069310.git.trast@student.ethz.ch","subject":"[PATCH v8 3/5] Export rewrite_parents() for 'log -L'","fromName":"Thomas Rast","fromEmail":"trast@student.ethz.ch","sentAt":"2013-02-28T16:38:21Z","receivedAt":"2013-02-28T16:38:21Z","isPatch":true,"sender":{"key":"tr@thomasrast.ch","avatar":"https://avatars.githubusercontent.com/u/153510?v=4"},"body":"From: Bo Yang <struggleyb.nku@gmail.com>\n\nThe function rewrite_one is used to rewrite a single\nparent of the current commit, and is used by rewrite_parents\nto rewrite all the parents.\n\nDecouple the dependence between them by making rewrite_one\na callback function that is passed to rewrite_parents. Then\nexport rewrite_parents for reuse by the line history browser.\n\nWe will use this function in line.c.\n\nSigned-off-by: Bo Yang <struggleyb.nku@gmail.com>\nSigned-off-by: Thomas Rast <trast@student.ethz.ch>\n---\n revision.c | 13 ++++---------\n revision.h | 10 ++++++++++\n 2 files changed, 14 insertions(+), 9 deletions(-)\n\ndiff --git a/revision.c b/revision.c\nindex ef60205..46319d5 100644\n--- a/revision.c\n+++ b/revision.c\n@@ -2173,12 +2173,6 @@ int prepare_revision_walk(struct rev_info *revs)\n \treturn 0;\n }\n \n-enum rewrite_result {\n-\trewrite_one_ok,\n-\trewrite_one_noparents,\n-\trewrite_one_error\n-};\n-\n static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)\n {\n \tstruct commit_list *cache = NULL;\n@@ -2200,12 +2194,13 @@ static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp\n \t}\n }\n \n-static int rewrite_parents(struct rev_info *revs, struct commit *commit)\n+int rewrite_parents(struct rev_info *revs, struct commit *commit,\n+\trewrite_parent_fn_t rewrite_parent)\n {\n \tstruct commit_list **pp = &commit->parents;\n \twhile (*pp) {\n \t\tstruct commit_list *parent = *pp;\n-\t\tswitch (rewrite_one(revs, &parent->item)) {\n+\t\tswitch (rewrite_parent(revs, &parent->item)) {\n \t\tcase rewrite_one_ok:\n \t\t\tbreak;\n \t\tcase rewrite_one_noparents:\n@@ -2371,7 +2366,7 @@ enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)\n \tif (action == commit_show &&\n \t    !revs->show_all &&\n \t    revs->prune && revs->dense && want_ancestry(revs)) {\n-\t\tif (rewrite_parents(revs, commit) < 0)\n+\t\tif (rewrite_parents(revs, commit, rewrite_one) < 0)\n \t\t\treturn commit_error;\n \t}\n \treturn action;\ndiff --git a/revision.h b/revision.h\nindex 5da09ee..640110d 100644\n--- a/revision.h\n+++ b/revision.h\n@@ -241,4 +241,14 @@ enum commit_action {\n extern enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit);\n extern enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit);\n \n+enum rewrite_result {\n+\trewrite_one_ok,\n+\trewrite_one_noparents,\n+\trewrite_one_error\n+};\n+\n+typedef enum rewrite_result (*rewrite_parent_fn_t)(struct rev_info *revs, struct commit **pp);\n+\n+extern int rewrite_parents(struct rev_info *revs, struct commit *commit,\n+\trewrite_parent_fn_t rewrite_parent);\n #endif\n-- \n1.8.2.rc1.388.g1bd82c8\n"},{"id":"210425","messageId":"9af548b2a7e4a4da9eb30e99b0223f20788b4fc1.1362069310.git.trast@student.ethz.ch","threadId":"33038","inReplyTo":"cover.1362069310.git.trast@student.ethz.ch","subject":"[PATCH v8 4/5] Implement line-history search (git log -L)","fromName":"Thomas Rast","fromEmail":"trast@student.ethz.ch","sentAt":"2013-02-28T16:38:22Z","receivedAt":"2013-02-28T16:38:22Z","isPatch":true,"sender":{"key":"tr@thomasrast.ch","avatar":"https://avatars.githubusercontent.com/u/153510?v=4"},"body":"This is a rewrite of much of Bo's work, mainly in an effort to split\nit into smaller, easier to understand routines.\n\nThe algorithm is built around the struct range_set, which encodes a\nseries of line ranges as intervals [a,b).  This is used in two\ncontexts:\n\n* A set of lines we are tracking (which will change as we dig through\n  history).\n* To encode diffs, as pairs of ranges.\n\nThe main routine is range_set_map_across_diff().  It processes the\ndiff between a commit C and some parent P.  It determines which diff\nhunks are relevant to the ranges tracked in C, and computes the new\nranges for P.\n\nThe algorithm is then simply to process history in topological order\nfrom newest to oldest, computing ranges and (partial) diffs.  At\nbranch points, we need to merge the ranges we are watching.  We will\nfind that many commits do not affect the chosen ranges, and mark them\nTREESAME (in addition to those already filtered by pathspec limiting).\nAnother pass of history simplification then gets rid of such commits.\n\nThis is wired as an extra filtering pass in the log machinery.  This\ncurrently only reduces code duplication, but should allow for other\nsimplifications and options to be used.\n\nFinally, we hook a diff printer into the output chain.  Ideally we\nwould wire directly into the diff logic, to optionally use features\nlike word diff.  However, that will require some major reworking of\nthe diff chain, so we completely replace the output with our own diff\nfor now.\n\nSigned-off-by: Bo Yang <struggleyb.nku@gmail.com>\nSigned-off-by: Thomas Rast <trast@student.ethz.ch>\n---\n Documentation/git-log.txt        |   22 +\n builtin/log.c                    |   31 +\n line-log.c                       | 1208 +++++++++++++++++++++++++++++++++++++-\n line-log.h                       |   56 ++\n log-tree.c                       |    4 +\n revision.c                       |    9 +\n revision.h                       |    6 +-\n t/t4211-line-log.sh              |   41 ++\n t/t4211/expect.beginning-of-file |   43 ++\n t/t4211/expect.end-of-file       |   62 ++\n t/t4211/expect.move-support-f    |   40 ++\n t/t4211/expect.simple-f          |   59 ++\n t/t4211/expect.simple-main       |   68 +++\n t/t4211/expect.two-ranges        |  102 ++++\n t/t4211/expect.vanishes-early    |   39 ++\n t/t4211/history.export           |  330 +++++++++++\n 16 files changed, 2112 insertions(+), 8 deletions(-)\n create mode 100755 t/t4211-line-log.sh\n create mode 100644 t/t4211/expect.beginning-of-file\n create mode 100644 t/t4211/expect.end-of-file\n create mode 100644 t/t4211/expect.move-support-f\n create mode 100644 t/t4211/expect.simple-f\n create mode 100644 t/t4211/expect.simple-main\n create mode 100644 t/t4211/expect.two-ranges\n create mode 100644 t/t4211/expect.vanishes-early\n create mode 100644 t/t4211/history.export\n\ndiff --git a/Documentation/git-log.txt b/Documentation/git-log.txt\nindex 69db578..8727c60 100644\n--- a/Documentation/git-log.txt\n+++ b/Documentation/git-log.txt\n@@ -69,6 +69,23 @@ produced by --stat etc.\n \tNote that only message is considered, if also a diff is shown\n \tits size is not included.\n \n+-L <start>,<end>:<file>::\n+\tTrace the evolution of the line range given by \"<start>,<end>\"\n+\twithin the <file>.  You may not give any pathspec limiters.\n+\tThis is currently limited to a walk starting from a single\n+\trevision, i.e., you may only give zero or one positive\n+\trevision arguments.\n+\n+<start> and <end> can take one of these forms:\n+\n+include::line-range-format.txt[]\n+You can specify this option more than once.\n+\n+\n+--full-line-diff::\n+\tAlways print the interesting range even if the current commit\n+\tdoes not change any line of the range.\n+\n [\\--] <path>...::\n \tShow only commits that are enough to explain how the files\n \tthat match the specified paths came to be.  See \"History\n@@ -138,6 +155,11 @@ Examples\n \tThis makes sense only when following a strict policy of merging all\n \ttopic branches when staying on a single integration branch.\n \n+git log -L '/int main/',/^}/:main.c::\n+\n+\tShows how the function `main()` in the file 'main.c' evolved\n+\tover time.\n+\n `git log -3`::\n \tLimits the number of commits to show to 3.\n \ndiff --git a/builtin/log.c b/builtin/log.c\nindex 8f0b2e8..17b94ef 100644\n--- a/builtin/log.c\n+++ b/builtin/log.c\n@@ -19,6 +19,7 @@\n #include \"remote.h\"\n #include \"string-list.h\"\n #include \"parse-options.h\"\n+#include \"line-log.h\"\n #include \"branch.h\"\n #include \"streaming.h\"\n #include \"version.h\"\n@@ -41,6 +42,12 @@\n \tNULL\n };\n \n+struct line_opt_callback_data {\n+\tstruct rev_info *rev;\n+\tconst char *prefix;\n+\tstruct string_list args;\n+};\n+\n static int parse_decoration_style(const char *var, const char *value)\n {\n \tswitch (git_config_maybe_bool(var, value)) {\n@@ -75,6 +82,19 @@ static int decorate_callback(const struct option *opt, const char *arg, int unse\n \treturn 0;\n }\n \n+static int log_line_range_callback(const struct option *option, const char *arg, int unset)\n+{\n+\tstruct line_opt_callback_data *data = option->value;\n+\n+\tif (!arg)\n+\t\treturn -1;\n+\n+\tdata->rev->line_level_traverse = 1;\n+\tstring_list_append(&data->args, arg);\n+\n+\treturn 0;\n+}\n+\n static void cmd_log_init_defaults(struct rev_info *rev)\n {\n \tif (fmt_pretty)\n@@ -97,6 +117,7 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,\n {\n \tstruct userformat_want w;\n \tint quiet = 0, source = 0, mailmap = 0;\n+\tstatic struct line_opt_callback_data line_cb = {0, 0, STRING_LIST_INIT_DUP};\n \n \tconst struct option builtin_log_options[] = {\n \t\tOPT_BOOLEAN(0, \"quiet\", &quiet, N_(\"suppress diff output\")),\n@@ -104,9 +125,15 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,\n \t\tOPT_BOOLEAN(0, \"use-mailmap\", &mailmap, N_(\"Use mail map file\")),\n \t\t{ OPTION_CALLBACK, 0, \"decorate\", NULL, NULL, N_(\"decorate options\"),\n \t\t  PARSE_OPT_OPTARG, decorate_callback},\n+\t\tOPT_CALLBACK('L', NULL, &line_cb, \"n,m:file\",\n+\t\t\t     \"Process line range n,m in file, counting from 1\",\n+\t\t\t     log_line_range_callback),\n \t\tOPT_END()\n \t};\n \n+\tline_cb.rev = rev;\n+\tline_cb.prefix = prefix;\n+\n \tmailmap = use_mailmap_config;\n \targc = parse_options(argc, argv, prefix,\n \t\t\t     builtin_log_options, builtin_log_usage,\n@@ -160,6 +187,10 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,\n \t\trev->show_decorations = 1;\n \t\tload_ref_decorations(decoration_style);\n \t}\n+\n+\tif (rev->line_level_traverse)\n+\t\tline_log_init(rev, line_cb.prefix, &line_cb.args);\n+\n \tsetup_pager();\n }\n \ndiff --git a/line-log.c b/line-log.c\nindex b167b00..789b7c4 100644\n--- a/line-log.c\n+++ b/line-log.c\n@@ -1,6 +1,442 @@\n #include \"git-compat-util.h\"\n+#include \"cache.h\"\n+#include \"tag.h\"\n+#include \"blob.h\"\n+#include \"tree.h\"\n+#include \"diff.h\"\n+#include \"commit.h\"\n+#include \"decorate.h\"\n+#include \"revision.h\"\n+#include \"xdiff-interface.h\"\n+#include \"strbuf.h\"\n+#include \"log-tree.h\"\n+#include \"graph.h\"\n #include \"line-log.h\"\n \n+void range_set_grow (struct range_set *rs, size_t extra)\n+{\n+\tALLOC_GROW(rs->ranges, rs->nr + extra, rs->alloc);\n+}\n+\n+/* Either initialization would be fine */\n+#define RANGE_SET_INIT {0}\n+\n+void range_set_init (struct range_set *rs, size_t prealloc)\n+{\n+\trs->alloc = rs->nr = 0;\n+\trs->ranges = NULL;\n+\tif (prealloc)\n+\t\trange_set_grow(rs, prealloc);\n+}\n+\n+void range_set_release (struct range_set *rs)\n+{\n+\tfree(rs->ranges);\n+\trs->alloc = rs->nr = 0;\n+\trs->ranges = NULL;\n+}\n+\n+/* dst must be uninitialized! */\n+void range_set_copy (struct range_set *dst, struct range_set *src)\n+{\n+\trange_set_init(dst, src->nr);\n+\tmemcpy(dst->ranges, src->ranges, src->nr*sizeof(struct range_set));\n+\tdst->nr = src->nr;\n+}\n+void range_set_move (struct range_set *dst, struct range_set *src)\n+{\n+\trange_set_release(dst);\n+\tdst->ranges = src->ranges;\n+\tdst->nr = src->nr;\n+\tdst->alloc = src->alloc;\n+\tsrc->ranges = NULL;\n+\tsrc->alloc = src->nr = 0;\n+}\n+\n+/* tack on a _new_ range _at the end_ */\n+void range_set_append (struct range_set *rs, long a, long b)\n+{\n+\tassert(a <= b);\n+\tassert(rs->nr == 0 || rs->ranges[rs->nr-1].end <= a);\n+\trange_set_grow(rs, 1);\n+\trs->ranges[rs->nr].start = a;\n+\trs->ranges[rs->nr].end = b;\n+\trs->nr++;\n+}\n+\n+static int range_cmp (const void *_r, const void *_s)\n+{\n+\tconst struct range *r = _r;\n+\tconst struct range *s = _s;\n+\n+\t/* this could be simply 'return r.start-s.start', but for the types */\n+\tif (r->start == s->start)\n+\t\treturn 0;\n+\tif (r->start < s->start)\n+\t\treturn -1;\n+\treturn 1;\n+}\n+\n+/*\n+ * Helper: In-place pass of sorting and merging the ranges in the\n+ * range set, to re-establish the invariants after another operation\n+ *\n+ * NEEDSWORK currently not needed\n+ */\n+static void sort_and_merge_range_set (struct range_set *rs)\n+{\n+\tint i;\n+\tint o = 1; /* output cursor */\n+\n+\tqsort(rs->ranges, rs->nr, sizeof(struct range), range_cmp);\n+\n+\tfor (i = 1; i < rs->nr; i++) {\n+\t\tif (rs->ranges[i].start <= rs->ranges[o-1].end) {\n+\t\t\trs->ranges[o-1].end = rs->ranges[i].end;\n+\t\t} else {\n+\t\t\trs->ranges[o].start = rs->ranges[i].start;\n+\t\t\trs->ranges[o].end = rs->ranges[i].end;\n+\t\t\to++;\n+\t\t}\n+\t}\n+\tassert(o <= rs->nr);\n+\trs->nr = o;\n+}\n+\n+/*\n+ * Union of range sets (i.e., sets of line numbers).  Used to merge\n+ * them when searches meet at a common ancestor.\n+ *\n+ * This is also where the ranges are consolidated into canonical form:\n+ * overlapping and adjacent ranges are merged, and empty ranges are\n+ * removed.\n+ */\n+static void range_set_union (struct range_set *out,\n+\t\t\t     struct range_set *a, struct range_set *b)\n+{\n+\tint i = 0, j = 0, o = 0;\n+\tstruct range *ra = a->ranges;\n+\tstruct range *rb = b->ranges;\n+\t/* cannot make an alias of out->ranges: it may change during grow */\n+\n+\tassert(out->nr == 0);\n+\twhile (i < a->nr || j < b->nr) {\n+\t\tstruct range *new;\n+\t\tif (i < a->nr && j < b->nr) {\n+\t\t\tif (ra[i].start < rb[j].start)\n+\t\t\t\tnew = &ra[i++];\n+\t\t\telse if (ra[i].start > rb[j].start)\n+\t\t\t\tnew = &rb[j++];\n+\t\t\telse if (ra[i].end < rb[j].end)\n+\t\t\t\tnew = &ra[i++];\n+\t\t\telse\n+\t\t\t\tnew = &rb[j++];\n+\t\t} else if (i < a->nr)      /* b exhausted */\n+\t\t\tnew = &ra[i++];\n+\t\telse                       /* a exhausted */\n+\t\t\tnew = &rb[j++];\n+\t\tif (new->start == new->end)\n+\t\t\t; /* empty range */\n+\t\telse if (!o || out->ranges[o-1].end < new->start) {\n+\t\t\trange_set_grow(out, 1);\n+\t\t\tout->ranges[o].start = new->start;\n+\t\t\tout->ranges[o].end = new->end;\n+\t\t\to++;\n+\t\t} else if (out->ranges[o-1].end < new->end) {\n+\t\t\tout->ranges[o-1].end = new->end;\n+\t\t}\n+\t}\n+\tout->nr = o;\n+}\n+\n+/*\n+ * Difference of range sets (out = a \\ b).  Pass the \"interesting\"\n+ * ranges as 'a' and the target side of the diff as 'b': it removes\n+ * the ranges for which the commit is responsible.\n+ */\n+static void range_set_difference (struct range_set *out,\n+\t\t\t\t  struct range_set *a, struct range_set *b)\n+{\n+\tint i, j =  0;\n+\tfor (i = 0; i < a->nr; i++) {\n+\t\tlong start = a->ranges[i].start;\n+\t\tlong end = a->ranges[i].end;\n+\t\twhile (start < end) {\n+\t\t\twhile (j < b->nr && start >= b->ranges[j].end)\n+\t\t\t\t/*\n+\t\t\t\t * a:         |-------\n+\t\t\t\t * b: ------|\n+\t\t\t\t */\n+\t\t\t\tj++;\n+\t\t\tif (j >= b->nr || end < b->ranges[j].start) {\n+\t\t\t\t/*\n+\t\t\t\t * b exhausted, or\n+\t\t\t\t * a:  ----|\n+\t\t\t\t * b:         |----\n+\t\t\t\t */\n+\t\t\t\trange_set_append(out, start, end);\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t\tif (start >= b->ranges[j].start) {\n+\t\t\t\t/*\n+\t\t\t\t * a:     |--????\n+\t\t\t\t * b: |------|\n+\t\t\t\t */\n+\t\t\t\tstart = b->ranges[j].end;\n+\t\t\t} else if (end > b->ranges[j].start) {\n+\t\t\t\t/*\n+\t\t\t\t * a: |-----|\n+\t\t\t\t * b:    |--?????\n+\t\t\t\t */\n+\t\t\t\tif (start < b->ranges[j].start)\n+\t\t\t\t\trange_set_append(out, start, b->ranges[j].start);\n+\t\t\t\tstart = b->ranges[j].end;\n+\t\t\t}\n+\t\t}\n+\t}\n+}\n+\n+static void diff_ranges_init (struct diff_ranges *diff)\n+{\n+\trange_set_init(&diff->parent, 0);\n+\trange_set_init(&diff->target, 0);\n+}\n+\n+static void diff_ranges_release (struct diff_ranges *diff)\n+{\n+\trange_set_release(&diff->parent);\n+\trange_set_release(&diff->target);\n+}\n+\n+void line_log_data_init(struct line_log_data *r)\n+{\n+\tmemset(r, 0, sizeof(struct line_log_data));\n+\trange_set_init(&r->ranges, 0);\n+}\n+\n+static void line_log_data_clear(struct line_log_data *r)\n+{\n+\trange_set_release(&r->ranges);\n+\tif (r->pair)\n+\t\tdiff_free_filepair(r->pair);\n+}\n+\n+static void free_line_log_data(struct line_log_data *r)\n+{\n+\twhile (r) {\n+\t\tstruct line_log_data *next = r->next;\n+\t\tline_log_data_clear(r);\n+\t\tfree(r);\n+\t\tr = next;\n+\t}\n+}\n+\n+static struct line_log_data *\n+search_line_log_data(struct line_log_data *list, const char *path,\n+\t\t     struct line_log_data **insertion_point)\n+{\n+\tstruct line_log_data *p = list;\n+\tif (insertion_point)\n+\t\t*insertion_point = NULL;\n+\twhile (p) {\n+\t\tint cmp = strcmp(p->spec->path, path);\n+\t\tif (!cmp)\n+\t\t\treturn p;\n+\t\tif (insertion_point && cmp < 0)\n+\t\t\t*insertion_point = p;\n+\t\tp = p->next;\n+\t}\n+\treturn NULL;\n+}\n+\n+static void line_log_data_insert (struct line_log_data **list,\n+\t\t\t\t  struct diff_filespec *spec,\n+\t\t\t\t  long begin, long end)\n+{\n+\tstruct line_log_data *ip;\n+\tstruct line_log_data *p = search_line_log_data(*list, spec->path, &ip);\n+\n+\tif (p) {\n+\t\trange_set_append(&p->ranges, begin, end);\n+\t\tsort_and_merge_range_set(&p->ranges);\n+\t\tfree_filespec(spec);\n+\t\treturn;\n+\t}\n+\n+\tp = xcalloc(1, sizeof(struct line_log_data));\n+\tp->spec = spec;\n+\trange_set_append(&p->ranges, begin, end);\n+\tif (ip) {\n+\t\tp->next = ip->next;\n+\t\tip->next = p;\n+\t} else {\n+\t\tp->next = *list;\n+\t\t*list = p;\n+\t}\n+}\n+\n+struct collect_diff_cbdata {\n+\tstruct diff_ranges *diff;\n+};\n+\n+static int collect_diff_cb (long start_a, long count_a,\n+\t\t\t    long start_b, long count_b,\n+\t\t\t    void *data)\n+{\n+\tstruct collect_diff_cbdata *d = data;\n+\n+\tif (count_a >= 0)\n+\t\trange_set_append(&d->diff->parent, start_a, start_a + count_a);\n+\tif (count_b >= 0)\n+\t\trange_set_append(&d->diff->target, start_b, start_b + count_b);\n+\n+\treturn 0;\n+}\n+\n+static void collect_diff (mmfile_t *parent, mmfile_t *target, struct diff_ranges *out)\n+{\n+\tstruct collect_diff_cbdata cbdata = {0};\n+\txpparam_t xpp;\n+\txdemitconf_t xecfg;\n+\txdemitcb_t ecb;\n+\n+\tmemset(&xpp, 0, sizeof(xpp));\n+\tmemset(&xecfg, 0, sizeof(xecfg));\n+\txecfg.ctxlen = xecfg.interhunkctxlen = 0;\n+\n+\tcbdata.diff = out;\n+\txecfg.hunk_func = collect_diff_cb;\n+\tmemset(&ecb, 0, sizeof(ecb));\n+\tecb.priv = &cbdata;\n+\txdi_diff(parent, target, &xpp, &xecfg, &ecb);\n+}\n+\n+/*\n+ * These are handy for debugging.  Removing them with #if 0 silences\n+ * the \"unused function\" warning.\n+ */\n+#if 0\n+static void dump_range_set (struct range_set *rs, const char *desc)\n+{\n+\tint i;\n+\tprintf(\"range set %s (%d items):\\n\", desc, rs->nr);\n+\tfor (i = 0; i < rs->nr; i++)\n+\t\tprintf(\"\\t[%ld,%ld]\\n\", rs->ranges[i].start, rs->ranges[i].end);\n+}\n+\n+static void dump_line_log_data (struct line_log_data *r)\n+{\n+\tchar buf[4096];\n+\twhile (r) {\n+\t\tsnprintf(buf, 4096, \"file %s\\n\", r->spec->path);\n+\t\tdump_range_set(&r->ranges, buf);\n+\t\tr = r->next;\n+\t}\n+}\n+\n+static void dump_diff_ranges (struct diff_ranges *diff, const char *desc)\n+{\n+\tint i;\n+\tassert(diff->parent.nr == diff->target.nr);\n+\tprintf(\"diff ranges %s (%d items):\\n\", desc, diff->parent.nr);\n+\tprintf(\"\\tparent\\ttarget\\n\");\n+\tfor (i = 0; i < diff->parent.nr; i++) {\n+\t\tprintf(\"\\t[%ld,%ld]\\t[%ld,%ld]\\n\",\n+\t\t       diff->parent.ranges[i].start,\n+\t\t       diff->parent.ranges[i].end,\n+\t\t       diff->target.ranges[i].start,\n+\t\t       diff->target.ranges[i].end);\n+\t}\n+}\n+#endif\n+\n+\n+static int ranges_overlap (struct range *a, struct range *b)\n+{\n+\treturn !(a->end <= b->start || b->end <= a->start);\n+}\n+\n+/*\n+ * Given a diff and the set of interesting ranges, determine all hunks\n+ * of the diff which touch (overlap) at least one of the interesting\n+ * ranges in the target.\n+ */\n+static void diff_ranges_filter_touched (struct diff_ranges *out,\n+\t\t\t\t\tstruct diff_ranges *diff,\n+\t\t\t\t\tstruct range_set *rs)\n+{\n+\tint i, j = 0;\n+\n+\tassert(out->target.nr == 0);\n+\n+\tfor (i = 0; i < diff->target.nr; i++) {\n+\t\twhile (diff->target.ranges[i].start > rs->ranges[j].end) {\n+\t\t\tj++;\n+\t\t\tif (j == rs->nr)\n+\t\t\t\treturn;\n+\t\t}\n+\t\tif (ranges_overlap(&diff->target.ranges[i], &rs->ranges[j])) {\n+\t\t\trange_set_append(&out->parent,\n+\t\t\t\t\t diff->parent.ranges[i].start,\n+\t\t\t\t\t diff->parent.ranges[i].end);\n+\t\t\trange_set_append(&out->target,\n+\t\t\t\t\t diff->target.ranges[i].start,\n+\t\t\t\t\t diff->target.ranges[i].end);\n+\t\t}\n+\t}\n+}\n+\n+/*\n+ * Adjust the line counts in 'rs' to account for the lines\n+ * added/removed in the diff.\n+ */\n+static void range_set_shift_diff (struct range_set *out,\n+\t\t\t\t  struct range_set *rs,\n+\t\t\t\t  struct diff_ranges *diff)\n+{\n+\tint i, j = 0;\n+\tlong offset = 0;\n+\tstruct range *src = rs->ranges;\n+\tstruct range *target = diff->target.ranges;\n+\tstruct range *parent = diff->parent.ranges;\n+\n+\tfor (i = 0; i < rs->nr; i++) {\n+\t\twhile (j < diff->target.nr && src[i].start >= target[j].start) {\n+\t\t\toffset += (parent[j].end-parent[j].start)\n+\t\t\t\t- (target[j].end-target[j].start);\n+\t\t\tj++;\n+\t\t}\n+\t\trange_set_append(out, src[i].start+offset, src[i].end+offset);\n+\t}\n+}\n+\n+/*\n+ * Given a diff and the set of interesting ranges, map the ranges\n+ * across the diff.  That is: observe that the target commit takes\n+ * blame for all the + (target-side) ranges.  So for every pair of\n+ * ranges in the diff that was touched, we remove the latter and add\n+ * its parent side.\n+ */\n+static void range_set_map_across_diff (struct range_set *out,\n+\t\t\t\t       struct range_set *rs,\n+\t\t\t\t       struct diff_ranges *diff,\n+\t\t\t\t       struct diff_ranges **touched_out)\n+{\n+\tstruct diff_ranges *touched = xmalloc(sizeof(*touched));\n+\tstruct range_set tmp1 = RANGE_SET_INIT;\n+\tstruct range_set tmp2 = RANGE_SET_INIT;\n+\n+\tdiff_ranges_init(touched);\n+\tdiff_ranges_filter_touched(touched, diff, rs);\n+\trange_set_difference(&tmp1, rs, &touched->target);\n+\trange_set_shift_diff(&tmp2, &tmp1, diff);\n+\trange_set_union(out, &tmp2, &touched->parent);\n+\trange_set_release(&tmp1);\n+\trange_set_release(&tmp2);\n+\n+\t*touched_out = touched;\n+}\n+\n /*\n  * Parse one item in the -L option\n  */\n@@ -18,7 +454,8 @@ const char *parse_loc(const char *spec, nth_line_fn_t nth_line,\n \t * $ is a synonym for \"the end of the file\".\n \t */\n \tif (spec[0] == '$') {\n-\t\t*ret = lines;\n+\t\tif (ret)\n+\t\t\t*ret = lines;\n \t\treturn spec + 1;\n \t}\n \n@@ -30,6 +467,8 @@ const char *parse_loc(const char *spec, nth_line_fn_t nth_line,\n \tif (begin != -1 && (spec[0] == '+' || spec[0] == '-')) {\n \t\tnum = strtol(spec + 1, &term, 10);\n \t\tif (term != spec + 1) {\n+\t\t\tif (!ret)\n+\t\t\t\treturn term;\n \t\t\tif (spec[0] == '-')\n \t\t\t\tnum = 0 - num;\n \t\t\tif (0 < num)\n@@ -44,7 +483,8 @@ const char *parse_loc(const char *spec, nth_line_fn_t nth_line,\n \t}\n \tnum = strtol(spec, &term, 10);\n \tif (term != spec) {\n-\t\t*ret = num;\n+\t\tif (ret)\n+\t\t\t*ret = num;\n \t\treturn term;\n \t}\n \tif (spec[0] != '/')\n@@ -58,6 +498,10 @@ const char *parse_loc(const char *spec, nth_line_fn_t nth_line,\n \tif (*term != '/')\n \t\treturn spec;\n \n+\t/* in the scan-only case we are not interested in the regex */\n+\tif (!ret)\n+\t\treturn term+1;\n+\n \t/* try [spec+1 .. term-1] as regexp */\n \t*term = 0;\n \tif (begin == -1)\n@@ -101,13 +545,763 @@ int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,\n \t\t}\n \t}\n \n-\tif (*begin <= 0)\n-\t\t*begin = 1;\n-\tif (*end > lines)\n-\t\t*end = lines;\n-\n \tif (*arg)\n \t\treturn -1;\n \n \treturn 0;\n }\n+\n+const char *skip_range_arg(const char *arg)\n+{\n+\targ = parse_loc(arg, NULL, NULL, 0, -1, 0);\n+\n+\tif (*arg == ',')\n+\t\targ = parse_loc(arg+1, NULL, NULL, 0, 0, 0);\n+\n+\treturn arg;\n+}\n+\n+static struct commit *check_single_commit(struct rev_info *revs)\n+{\n+\tstruct object *commit = NULL;\n+\tint found = -1;\n+\tint i;\n+\n+\tfor (i = 0; i < revs->pending.nr; i++) {\n+\t\tstruct object *obj = revs->pending.objects[i].item;\n+\t\tif (obj->flags & UNINTERESTING)\n+\t\t\tcontinue;\n+\t\twhile (obj->type == OBJ_TAG)\n+\t\t\tobj = deref_tag(obj, NULL, 0);\n+\t\tif (obj->type != OBJ_COMMIT)\n+\t\t\tdie(\"Non commit %s?\", revs->pending.objects[i].name);\n+\t\tif (commit)\n+\t\t\tdie(\"More than one commit to dig from: %s and %s?\",\n+\t\t\t    revs->pending.objects[i].name,\n+\t\t\t    revs->pending.objects[found].name);\n+\t\tcommit = obj;\n+\t\tfound = i;\n+\t}\n+\n+\tif (!commit)\n+\t\tdie(\"No commit specified?\");\n+\n+\treturn (struct commit *) commit;\n+}\n+\n+static void fill_blob_sha1(struct commit *commit, struct diff_filespec *spec)\n+{\n+\tunsigned mode;\n+\tunsigned char sha1[20];\n+\n+\tif (get_tree_entry(commit->object.sha1, spec->path,\n+\t\t\t   sha1, &mode))\n+\t\tdie(\"There is no path %s in the commit\", spec->path);\n+\tfill_filespec(spec, sha1, 1, mode);\n+\n+\treturn;\n+}\n+\n+static void fill_line_ends(struct diff_filespec *spec, long *lines,\n+\tunsigned long **line_ends)\n+{\n+\tint num = 0, size = 50;\n+\tlong cur = 0;\n+\tunsigned long *ends = NULL;\n+\tchar *data = NULL;\n+\n+\tif (diff_populate_filespec(spec, 0))\n+\t\tdie(\"Cannot read blob %s\", sha1_to_hex(spec->sha1));\n+\n+\tends = xmalloc(size * sizeof(*ends));\n+\tends[cur++] = 0;\n+\tdata = spec->data;\n+\twhile (num < spec->size) {\n+\t\tif (data[num] == '\\n' || num == spec->size - 1) {\n+\t\t\tALLOC_GROW(ends, (cur + 1), size);\n+\t\t\tends[cur++] = num;\n+\t\t}\n+\t\tnum++;\n+\t}\n+\n+\t/* shrink the array to fit the elements */\n+\tends = xrealloc(ends, cur * sizeof(*ends));\n+\t*lines = cur-1;\n+\t*line_ends = ends;\n+}\n+\n+struct nth_line_cb {\n+\tstruct diff_filespec *spec;\n+\tlong lines;\n+\tunsigned long *line_ends;\n+};\n+\n+static const char *nth_line(void *data, long line)\n+{\n+\tstruct nth_line_cb *d = data;\n+\tassert(d && line <= d->lines);\n+\tassert(d->spec && d->spec->data);\n+\n+\tif (line == 0)\n+\t\treturn (char *)d->spec->data;\n+\telse\n+\t\treturn (char *)d->spec->data + d->line_ends[line] + 1;\n+}\n+\n+static struct line_log_data *\n+parse_lines(struct commit *commit, const char *prefix, struct string_list *args)\n+{\n+\tlong lines = 0;\n+\tunsigned long *ends = NULL;\n+\tstruct nth_line_cb cb_data;\n+\tstruct string_list_item *item;\n+\tstruct line_log_data *ranges = NULL;\n+\n+\tfor_each_string_list_item(item, args) {\n+\t\tconst char *name_part, *range_part;\n+\t\tconst char *full_name;\n+\t\tstruct diff_filespec *spec;\n+\t\tlong begin = 0, end = 0;\n+\n+\t\tname_part = skip_range_arg(item->string);\n+\t\tif (!name_part || *name_part != ':')\n+\t\t\tdie(\"-L argument '%s' not of the form start,end:file\",\n+\t\t\t    item->string);\n+\t\trange_part = xstrndup(item->string, name_part - item->string);\n+\t\tname_part++;\n+\n+\t\tfull_name = prefix_path(prefix, prefix ? strlen(prefix) : 0,\n+\t\t\t\t\tname_part);\n+\n+\t\tspec = alloc_filespec(full_name);\n+\t\tfill_blob_sha1(commit, spec);\n+\t\tfill_line_ends(spec, &lines, &ends);\n+\t\tcb_data.spec = spec;\n+\t\tcb_data.lines = lines;\n+\t\tcb_data.line_ends = ends;\n+\n+\t\tif (parse_range_arg(range_part, nth_line, &cb_data,\n+\t\t\t\t    lines, &begin, &end))\n+\t\t\tdie(\"malformed -L argument '%s'\", range_part);\n+\t\tif (begin < 1)\n+\t\t\tbegin = 1;\n+\t\tif (end < 1)\n+\t\t\tend = lines;\n+\t\tbegin--;\n+\t\tif (lines < end || lines < begin)\n+\t\t\tdie(\"file %s has only %ld lines\", name_part, lines);\n+\t\tline_log_data_insert(&ranges, spec, begin, end);\n+\n+\t\tfree(ends);\n+\t\tends = NULL;\n+\t}\n+\n+\treturn ranges;\n+}\n+\n+static struct line_log_data *line_log_data_copy_one(struct line_log_data *r)\n+{\n+\tstruct line_log_data *ret = xmalloc(sizeof(*ret));\n+\n+\tassert(r);\n+\tline_log_data_init(ret);\n+\trange_set_copy(&ret->ranges, &r->ranges);\n+\n+\tret->spec = r->spec;\n+\tassert(ret->spec);\n+\tret->spec->count++;\n+\n+\treturn ret;\n+}\n+\n+static struct line_log_data *\n+line_log_data_copy(struct line_log_data *r)\n+{\n+\tstruct line_log_data *ret = NULL;\n+\tstruct line_log_data *tmp = NULL, *prev = NULL;\n+\n+\tassert(r);\n+\tret = tmp = prev = line_log_data_copy_one(r);\n+\tr = r->next;\n+\twhile (r) {\n+\t\ttmp = line_log_data_copy_one(r);\n+\t\tprev->next = tmp;\n+\t\tprev = tmp;\n+\t\tr = r->next;\n+\t}\n+\n+\treturn ret;\n+}\n+\n+/* merge two range sets across files */\n+static struct line_log_data *line_log_data_merge(struct line_log_data *a,\n+\t\tstruct line_log_data *b)\n+{\n+\tstruct line_log_data *head = NULL, **pp = &head;\n+\n+\twhile (a || b) {\n+\t\tstruct line_log_data *src;\n+\t\tstruct line_log_data *src2 = NULL;\n+\t\tstruct line_log_data *d;\n+\t\tint cmp;\n+\t\tif (!a)\n+\t\t\tcmp = 1;\n+\t\telse if (!b)\n+\t\t\tcmp = -1;\n+\t\telse\n+\t\t\tcmp = strcmp(a->spec->path, b->spec->path);\n+\t\tif (cmp < 0) {\n+\t\t\tsrc = a;\n+\t\t\ta = a->next;\n+\t\t} else if (cmp == 0) {\n+\t\t\tsrc = a;\n+\t\t\ta = a->next;\n+\t\t\tsrc2 = b;\n+\t\t\tb = b->next;\n+\t\t} else {\n+\t\t\tsrc = b;\n+\t\t\tb = b->next;\n+\t\t}\n+\t\td = xmalloc(sizeof(struct line_log_data));\n+\t\tline_log_data_init(d);\n+\t\td->spec = src->spec;\n+\t\td->spec->count++;\n+\t\t*pp = d;\n+\t\tpp = &d->next;\n+\t\tif (src2)\n+\t\t\trange_set_union(&d->ranges, &src->ranges, &src2->ranges);\n+\t\telse\n+\t\t\trange_set_copy(&d->ranges, &src->ranges);\n+\t}\n+\n+\treturn head;\n+}\n+\n+static void add_line_range(struct rev_info *revs, struct commit *commit,\n+\t\tstruct line_log_data *range)\n+{\n+\tstruct line_log_data *old = NULL;\n+\tstruct line_log_data *new = NULL;\n+\n+\told = lookup_decoration(&revs->line_log_data, &commit->object);\n+\tif (old && range) {\n+\t\tnew = line_log_data_merge(old, range);\n+\t\tfree_line_log_data(old);\n+\t} else if (range)\n+\t\tnew = line_log_data_copy(range);\n+\n+\tif (new)\n+\t\tadd_decoration(&revs->line_log_data, &commit->object, new);\n+}\n+\n+static void clear_commit_line_range(struct rev_info *revs, struct commit *commit)\n+{\n+\tstruct line_log_data *r;\n+\tr = lookup_decoration(&revs->line_log_data, &commit->object);\n+\tif (!r)\n+\t\treturn;\n+\tfree_line_log_data(r);\n+\tadd_decoration(&revs->line_log_data, &commit->object, NULL);\n+}\n+\n+static struct line_log_data *lookup_line_range(struct rev_info *revs,\n+\t\tstruct commit *commit)\n+{\n+\tstruct line_log_data *ret = NULL;\n+\n+\tret = lookup_decoration(&revs->line_log_data, &commit->object);\n+\treturn ret;\n+}\n+\n+void line_log_init(struct rev_info *rev, const char *prefix, struct string_list *args)\n+{\n+\tstruct commit *commit = NULL;\n+\tstruct line_log_data *range;\n+\n+\tcommit = check_single_commit(rev);\n+\trange = parse_lines(commit, prefix, args);\n+\tadd_line_range(rev, commit, range);\n+\n+\tif (!rev->diffopt.detect_rename) {\n+\t\tint i, count = 0;\n+\t\tstruct line_log_data *r = range;\n+\t\tconst char **paths;\n+\t\twhile (r) {\n+\t\t\tcount++;\n+\t\t\tr = r->next;\n+\t\t}\n+\t\tpaths = xmalloc((count+1)*sizeof(char *));\n+\t\tr = range;\n+\t\tfor (i = 0; i < count; i++) {\n+\t\t\tpaths[i] = xstrdup(r->spec->path);\n+\t\t\tr = r->next;\n+\t\t}\n+\t\tpaths[count] = NULL;\n+\t\tinit_pathspec(&rev->diffopt.pathspec, paths);\n+\t\tfree(paths);\n+\t}\n+}\n+\n+static void load_tree_desc(struct tree_desc *desc, void **tree,\n+\t\tconst unsigned char *sha1)\n+{\n+\tunsigned long size;\n+\t*tree = read_object_with_reference(sha1, tree_type, &size, NULL);\n+\tif (!tree)\n+\t\tdie(\"Unable to read tree (%s)\", sha1_to_hex(sha1));\n+\tinit_tree_desc(desc, *tree, size);\n+}\n+\n+static int count_parents(struct commit *commit)\n+{\n+\tstruct commit_list *parents = commit->parents;\n+\tint count = 0;\n+\twhile (parents) {\n+\t\tcount++;\n+\t\tparents = parents->next;\n+\t}\n+\treturn count;\n+}\n+\n+static void move_diff_queue(struct diff_queue_struct *dst,\n+\t\t\t    struct diff_queue_struct *src)\n+{\n+\tassert(src != dst);\n+\tmemcpy(dst, src, sizeof(struct diff_queue_struct));\n+\tDIFF_QUEUE_CLEAR(src);\n+}\n+\n+static void queue_diffs(struct diff_options *opt,\n+\t\t\tstruct diff_queue_struct *queue,\n+\t\t\tstruct commit *commit, struct commit *parent)\n+{\n+\tvoid *tree1 = NULL, *tree2 = NULL;\n+\tstruct tree_desc desc1, desc2;\n+\n+\tassert(commit);\n+\tload_tree_desc(&desc2, &tree2, commit->tree->object.sha1);\n+\tif (parent)\n+\t\tload_tree_desc(&desc1, &tree1, parent->tree->object.sha1);\n+\telse\n+\t\tinit_tree_desc(&desc1, \"\", 0);\n+\n+\tDIFF_QUEUE_CLEAR(&diff_queued_diff);\n+\tdiff_tree(&desc1, &desc2, \"\", opt);\n+\tdiffcore_std(opt);\n+\tmove_diff_queue(queue, &diff_queued_diff);\n+\n+\tif (tree1)\n+\t\tfree(tree1);\n+\tif (tree2)\n+\t\tfree(tree2);\n+}\n+\n+static char *get_nth_line(long line, unsigned long *ends, void *data)\n+{\n+\tif (line == 0)\n+\t\treturn (char *)data;\n+\telse\n+\t\treturn (char *)data + ends[line] + 1;\n+}\n+\n+static void print_line(const char *prefix, char first,\n+\t\t       long line, unsigned long *ends, void *data,\n+\t\t       const char *color, const char *reset)\n+{\n+\tchar *begin = get_nth_line(line, ends, data);\n+\tchar *end = get_nth_line(line+1, ends, data);\n+\tint had_nl = 0;\n+\n+\tif (end > begin && end[-1] == '\\n') {\n+\t\tend--;\n+\t\thad_nl = 1;\n+\t}\n+\n+\tfputs(prefix, stdout);\n+\tfputs(color, stdout);\n+\tputchar(first);\n+\tfwrite(begin, 1, end-begin, stdout);\n+\tfputs(reset, stdout);\n+\tputchar('\\n');\n+\tif (!had_nl)\n+\t\tfputs(\"\\\\ No newline at end of file\\n\", stdout);\n+}\n+\n+static char *output_prefix(struct diff_options *opt)\n+{\n+\tchar *prefix = \"\";\n+\n+\tif (opt->output_prefix) {\n+\t\tstruct strbuf *sb = opt->output_prefix(opt, opt->output_prefix_data);\n+\t\tprefix = sb->buf;\n+\t}\n+\n+\treturn prefix;\n+}\n+\n+static void dump_diff_hacky_one(struct rev_info *rev, struct line_log_data *range)\n+{\n+\tint i, j = 0;\n+\tlong p_lines, t_lines;\n+\tunsigned long *p_ends = NULL, *t_ends = NULL;\n+\tstruct diff_filepair *pair = range->pair;\n+\tstruct diff_ranges *diff = &range->diff;\n+\n+\tstruct diff_options *opt = &rev->diffopt;\n+\tchar *prefix = output_prefix(opt);\n+\tconst char *c_reset = diff_get_color(opt->use_color, DIFF_RESET);\n+\tconst char *c_frag = diff_get_color(opt->use_color, DIFF_FRAGINFO);\n+\tconst char *c_meta = diff_get_color(opt->use_color, DIFF_METAINFO);\n+\tconst char *c_old = diff_get_color(opt->use_color, DIFF_FILE_OLD);\n+\tconst char *c_new = diff_get_color(opt->use_color, DIFF_FILE_NEW);\n+\tconst char *c_plain = diff_get_color(opt->use_color, DIFF_PLAIN);\n+\n+\tif (!pair || !diff)\n+\t\treturn;\n+\n+\tif (pair->one->sha1_valid)\n+\t\tfill_line_ends(pair->one, &p_lines, &p_ends);\n+\tfill_line_ends(pair->two, &t_lines, &t_ends);\n+\n+\tprintf(\"%s%sdiff --git a/%s b/%s%s\\n\", prefix, c_meta, pair->one->path, pair->two->path, c_reset);\n+\tprintf(\"%s%s--- %s%s%s\\n\", prefix, c_meta,\n+\t       pair->one->sha1_valid ? \"a/\" : \"\",\n+\t       pair->one->sha1_valid ? pair->one->path : \"/dev/null\",\n+\t       c_reset);\n+\tprintf(\"%s%s+++ b/%s%s\\n\", prefix, c_meta, pair->two->path, c_reset);\n+\tfor (i = 0; i < range->ranges.nr; i++) {\n+\t\tlong p_start, p_end;\n+\t\tlong t_start = range->ranges.ranges[i].start;\n+\t\tlong t_end = range->ranges.ranges[i].end;\n+\t\tlong t_cur = t_start;\n+\t\tint j_last;\n+\n+\t\twhile (j < diff->target.nr && diff->target.ranges[j].end < t_start)\n+\t\t\tj++;\n+\t\tif (j == diff->target.nr || diff->target.ranges[j].start > t_end)\n+\t\t\tcontinue;\n+\n+\t\t/* Scan ahead to determine the last diff that falls in this range */\n+\t\tj_last = j;\n+\t\twhile (j_last < diff->target.nr && diff->target.ranges[j_last].start < t_end)\n+\t\t\tj_last++;\n+\t\tif (j_last > j)\n+\t\t\tj_last--;\n+\n+\t\t/*\n+\t\t * Compute parent hunk headers: we know that the diff\n+\t\t * has the correct line numbers (but not all hunks).\n+\t\t * So it suffices to shift the start/end according to\n+\t\t * the line numbers of the first/last hunk(s) that\n+\t\t * fall in this range.\n+\t\t */\n+\t\tif (t_start < diff->target.ranges[j].start)\n+\t\t\tp_start = diff->parent.ranges[j].start - (diff->target.ranges[j].start-t_start);\n+\t\telse\n+\t\t\tp_start = diff->parent.ranges[j].start;\n+\t\tif (t_end > diff->target.ranges[j_last].end)\n+\t\t\tp_end = diff->parent.ranges[j_last].end + (t_end-diff->target.ranges[j_last].end);\n+\t\telse\n+\t\t\tp_end = diff->parent.ranges[j_last].end;\n+\n+\t\tif (!p_start && !p_end) {\n+\t\t\tp_start = -1;\n+\t\t\tp_end = -1;\n+\t\t}\n+\n+\t\t/* Now output a diff hunk for this range */\n+\t\tprintf(\"%s%s@@ -%ld,%ld +%ld,%ld @@%s\\n\",\n+\t\t       prefix, c_frag,\n+\t\t       p_start+1, p_end-p_start, t_start+1, t_end-t_start,\n+\t\t       c_reset);\n+\t\twhile (j < diff->target.nr && diff->target.ranges[j].start < t_end) {\n+\t\t\tint k;\n+\t\t\tfor (; t_cur < diff->target.ranges[j].start; t_cur++)\n+\t\t\t\tprint_line(prefix, ' ', t_cur, t_ends, pair->two->data,\n+\t\t\t\t\t   c_plain, c_reset);\n+\t\t\tfor (k = diff->parent.ranges[j].start; k < diff->parent.ranges[j].end; k++)\n+\t\t\t\tprint_line(prefix, '-', k, p_ends, pair->one->data,\n+\t\t\t\t\t   c_old, c_reset);\n+\t\t\tfor (; t_cur < diff->target.ranges[j].end && t_cur < t_end; t_cur++)\n+\t\t\t\tprint_line(prefix, '+', t_cur, t_ends, pair->two->data,\n+\t\t\t\t\t   c_new, c_reset);\n+\t\t\tj++;\n+\t\t}\n+\t\tfor (; t_cur < t_end; t_cur++)\n+\t\t\tprint_line(prefix, ' ', t_cur, t_ends, pair->two->data,\n+\t\t\t\t   c_plain, c_reset);\n+\t}\n+\n+\tfree(p_ends);\n+\tfree(t_ends);\n+}\n+\n+/*\n+ * NEEDSWORK: manually building a diff here is not the Right\n+ * Thing(tm).  log -L should be built into the diff pipeline.\n+ */\n+static void dump_diff_hacky(struct rev_info *rev, struct line_log_data *range)\n+{\n+\tputs(output_prefix(&rev->diffopt));\n+\twhile (range) {\n+\t\tdump_diff_hacky_one(rev, range);\n+\t\trange = range->next;\n+\t}\n+}\n+\n+/*\n+ * Unlike most other functions, this destructively operates on\n+ * 'range'.\n+ */\n+static int process_diff_filepair(struct rev_info *rev,\n+\t\t\t\t struct diff_filepair *pair,\n+\t\t\t\t struct line_log_data *range,\n+\t\t\t\t struct diff_ranges **diff_out)\n+{\n+\tstruct line_log_data *rg = range;\n+\tstruct range_set tmp;\n+\tstruct diff_ranges diff;\n+\tmmfile_t file_parent, file_target;\n+\n+\tassert(pair->two->path);\n+\twhile (rg) {\n+\t\tassert(rg->spec->path);\n+\t\tif (!strcmp(rg->spec->path, pair->two->path))\n+\t\t\tbreak;\n+\t\trg = rg->next;\n+\t}\n+\n+\tif (!rg)\n+\t\treturn 0;\n+\tif (rg->ranges.nr == 0)\n+\t\treturn 0;\n+\n+\tassert(pair->two->sha1_valid);\n+\tdiff_populate_filespec(pair->two, 0);\n+\tfile_target.ptr = pair->two->data;\n+\tfile_target.size = pair->two->size;\n+\n+\tif (pair->one->sha1_valid) {\n+\t\tdiff_populate_filespec(pair->one, 0);\n+\t\tfile_parent.ptr = pair->one->data;\n+\t\tfile_parent.size = pair->one->size;\n+\t} else {\n+\t\tfile_parent.ptr = \"\";\n+\t\tfile_parent.size = 0;\n+\t}\n+\n+\tdiff_ranges_init(&diff);\n+\tcollect_diff(&file_parent, &file_target, &diff);\n+\n+\t/* NEEDSWORK should apply some heuristics to prevent mismatches */\n+\trg->spec->path = xstrdup(pair->one->path);\n+\n+\trange_set_init(&tmp, 0);\n+\trange_set_map_across_diff(&tmp, &rg->ranges, &diff, diff_out);\n+\trange_set_release(&rg->ranges);\n+\trange_set_move(&rg->ranges, &tmp);\n+\n+\tdiff_ranges_release(&diff);\n+\n+\treturn ((*diff_out)->parent.nr > 0);\n+}\n+\n+static struct diff_filepair *diff_filepair_dup(struct diff_filepair *pair)\n+{\n+\tstruct diff_filepair *new = xmalloc(sizeof(struct diff_filepair));\n+\tnew->one = pair->one;\n+\tnew->two = pair->two;\n+\tnew->one->count++;\n+\tnew->two->count++;\n+\treturn new;\n+}\n+\n+static void free_diffqueues(int n, struct diff_queue_struct *dq)\n+{\n+\tint i, j;\n+\tfor (i = 0; i < n; i++)\n+\t\tfor (j = 0; j < dq[i].nr; j++)\n+\t\t\tdiff_free_filepair(dq[i].queue[j]);\n+\tfree(dq);\n+}\n+\n+static int process_all_files(struct line_log_data **range_out,\n+\t\t\t     struct rev_info *rev,\n+\t\t\t     struct diff_queue_struct *queue,\n+\t\t\t     struct line_log_data *range)\n+{\n+\tint i, changed = 0;\n+\n+\t*range_out = line_log_data_copy(range);\n+\n+\tfor (i = 0; i < queue->nr; i++) {\n+\t\tstruct diff_ranges *pairdiff = NULL;\n+\t\tif (process_diff_filepair(rev, queue->queue[i], *range_out, &pairdiff)) {\n+\t\t\tstruct line_log_data *rg = range;\n+\t\t\tchanged++;\n+\t\t\t/* NEEDSWORK tramples over data structures not owned here */\n+\t\t\twhile (rg && strcmp(rg->spec->path, queue->queue[i]->two->path))\n+\t\t\t\trg = rg->next;\n+\t\t\tassert(rg);\n+\t\t\trg->pair = diff_filepair_dup(queue->queue[i]);\n+\t\t\tmemcpy(&rg->diff, pairdiff, sizeof(struct diff_ranges));\n+\t\t}\n+\t}\n+\n+\treturn changed;\n+}\n+\n+int line_log_print(struct rev_info *rev, struct commit *commit)\n+{\n+       struct line_log_data *range = lookup_line_range(rev, commit);\n+\n+       show_log(rev);\n+       dump_diff_hacky(rev, range);\n+       return 1;\n+}\n+\n+static int process_ranges_ordinary_commit(struct rev_info *rev, struct commit *commit)\n+{\n+\tstruct commit *parent = NULL;\n+\tstruct diff_queue_struct queue;\n+\tstruct line_log_data *range = lookup_line_range(rev, commit);\n+\tstruct line_log_data *parent_range;\n+\tint changed;\n+\n+\tif (commit->parents)\n+\t\tparent = commit->parents->item;\n+\n+\tqueue_diffs(&rev->diffopt, &queue, commit, parent);\n+\tchanged = process_all_files(&parent_range, rev, &queue, range);\n+\tif (parent)\n+\t\tadd_line_range(rev, parent, parent_range);\n+\treturn changed;\n+}\n+\n+static int process_ranges_merge_commit(struct rev_info *rev, struct commit *commit)\n+{\n+\tstruct line_log_data *range = lookup_line_range(rev, commit);\n+\tstruct diff_queue_struct *diffqueues;\n+\tstruct line_log_data **cand;\n+\tstruct commit **parents;\n+\tstruct commit_list *p;\n+\tint i;\n+\tint nparents = count_parents(commit);\n+\n+\tdiffqueues = xmalloc(nparents * sizeof(*diffqueues));\n+\tcand = xmalloc(nparents * sizeof(*cand));\n+\tparents = xmalloc(nparents * sizeof(*parents));\n+\n+\tp = commit->parents;\n+\tfor (i = 0; i < nparents; i++) {\n+\t\tparents[i] = p->item;\n+\t\tp = p->next;\n+\t\tqueue_diffs(&rev->diffopt, &diffqueues[i], commit, parents[i]);\n+\t}\n+\n+\tfor (i = 0; i < nparents; i++) {\n+\t\tint changed;\n+\t\tcand[i] = NULL;\n+\t\tchanged = process_all_files(&cand[i], rev, &diffqueues[i], range);\n+\t\tif (!changed) {\n+\t\t\t/*\n+\t\t\t * This parent can take all the blame, so we\n+\t\t\t * don't follow any other path in history\n+\t\t\t */\n+\t\t\tadd_line_range(rev, parents[i], cand[i]);\n+\t\t\tclear_commit_line_range(rev, commit);\n+\t\t\tcommit->parents = xmalloc(sizeof(struct commit_list));\n+\t\t\tcommit->parents->item = parents[i];\n+\t\t\tcommit->parents->next = NULL;\n+\t\t\tfree(parents);\n+\t\t\tfree(cand);\n+\t\t\tfree_diffqueues(nparents, diffqueues);\n+\t\t\t/* NEEDSWORK leaking like a sieve */\n+\t\t\treturn 0;\n+\t\t}\n+\t}\n+\n+\t/*\n+\t * No single parent took the blame.  We add the candidates\n+\t * from the above loop to the parents.\n+\t */\n+\tfor (i = 0; i < nparents; i++) {\n+\t\tadd_line_range(rev, parents[i], cand[i]);\n+\t}\n+\n+\tclear_commit_line_range(rev, commit);\n+\tfree(parents);\n+\tfree(cand);\n+\tfree_diffqueues(nparents, diffqueues);\n+\treturn 1;\n+\n+\t/* NEEDSWORK evil merge detection stuff */\n+\t/* NEEDSWORK leaking like a sieve */\n+}\n+\n+static int process_ranges_arbitrary_commit(struct rev_info *rev, struct commit *commit)\n+{\n+\tint changed = 0;\n+\n+\tif (lookup_line_range(rev, commit)) {\n+\t\tif (!commit->parents || !commit->parents->next)\n+\t\t\tchanged = process_ranges_ordinary_commit(rev, commit);\n+\t\telse\n+\t\t\tchanged = process_ranges_merge_commit(rev, commit);\n+\t}\n+\n+\tif (!changed)\n+\t\tcommit->object.flags |= TREESAME;\n+\n+\treturn changed;\n+}\n+\n+static enum rewrite_result line_log_rewrite_one(struct rev_info *rev, struct commit **pp)\n+{\n+\tfor (;;) {\n+\t\tstruct commit *p = *pp;\n+\t\tif (p->parents && p->parents->next)\n+\t\t\treturn rewrite_one_ok;\n+\t\tif (p->object.flags & UNINTERESTING)\n+\t\t\treturn rewrite_one_ok;\n+\t\tif (!(p->object.flags & TREESAME))\n+\t\t\treturn rewrite_one_ok;\n+\t\tif (!p->parents)\n+\t\t\treturn rewrite_one_noparents;\n+\t\t*pp = p->parents->item;\n+\t}\n+}\n+\n+int line_log_filter(struct rev_info *rev)\n+{\n+\tstruct commit *commit;\n+\tstruct commit_list *list = rev->commits;\n+\tstruct commit_list *out = NULL, *cur = NULL;\n+\n+\tlist = rev->commits;\n+\twhile (list) {\n+\t\tstruct commit_list *to_free = NULL;\n+\t\tcommit = list->item;\n+\t\tif (process_ranges_arbitrary_commit(rev, commit)) {\n+\t\t\tif (cur)\n+\t\t\t\tcur->next = list;\n+\t\t\telse\n+\t\t\t\tout = list;\n+\t\t\tcur = list;\n+\t\t} else\n+\t\t\tto_free = list;\n+\t\tlist = list->next;\n+\t\tfree(to_free);\n+\t}\n+\tcur->next = NULL;\n+\n+\tlist = out;\n+\twhile (list) {\n+\t\trewrite_parents(rev, list->item, line_log_rewrite_one);\n+\t\tlist = list->next;\n+\t}\n+\n+\trev->commits = out;\n+\n+\treturn 0;\n+}\ndiff --git a/line-log.h b/line-log.h\nindex 22b1c80..e3ec094 100644\n--- a/line-log.h\n+++ b/line-log.h\n@@ -1,6 +1,8 @@\n #ifndef LINE_LOG_H\n #define LINE_LOG_H\n \n+#include \"diffcore.h\"\n+\n /*\n  * Parse one item in an -L begin,end option w.r.t. the notional file\n  * object 'cb_data' consisting of 'lines' lines.\n@@ -20,4 +22,58 @@ extern int parse_range_arg(const char *arg,\n \t\t\t   void *cb_data, long lines,\n \t\t\t   long *begin, long *end);\n \n+/*\n+ * Scan past a range argument that could be parsed by\n+ * 'parse_range_arg', to help the caller determine the start of the\n+ * filename in '-L n,m:file' syntax.\n+ *\n+ * Returns a pointer to the first character after the 'n,m' part, or\n+ * NULL in case the argument is obviously malformed.\n+ */\n+\n+extern const char *skip_range_arg(const char *arg);\n+\n+struct rev_info;\n+struct commit;\n+\n+/* A range [start,end].  Lines are numbered starting at 0, and the\n+ * ranges include start but exclude end. */\n+struct range {\n+\tlong start, end;\n+};\n+\n+/* A set of ranges.  The ranges must always be disjoint and sorted. */\n+struct range_set {\n+\tint alloc, nr;\n+\tstruct range *ranges;\n+};\n+\n+/* A diff, encoded as the set of pre- and post-image ranges where the\n+ * files differ. A pair of ranges corresponds to a hunk. */\n+struct diff_ranges {\n+\tstruct range_set parent;\n+\tstruct range_set target;\n+};\n+\n+/* Linked list of interesting files and their associated ranges.  The\n+ * list must be kept sorted by spec->path */\n+struct line_log_data {\n+\tstruct line_log_data *next;\n+\tstruct diff_filespec *spec;\n+\tchar status;\n+\tstruct range_set ranges;\n+\tint arg_alloc, arg_nr;\n+\tconst char **args;\n+\tstruct diff_filepair *pair;\n+\tstruct diff_ranges diff;\n+};\n+\n+extern void line_log_data_init(struct line_log_data *r);\n+\n+extern void line_log_init(struct rev_info *rev, const char *prefix, struct string_list *args);\n+\n+extern int line_log_filter(struct rev_info *rev);\n+\n+extern int line_log_print(struct rev_info *rev, struct commit *commit);\n+\n #endif /* LINE_LOG_H */\ndiff --git a/log-tree.c b/log-tree.c\nindex 5dc45c4..793d2c8 100644\n--- a/log-tree.c\n+++ b/log-tree.c\n@@ -9,6 +9,7 @@\n #include \"string-list.h\"\n #include \"color.h\"\n #include \"gpg-interface.h\"\n+#include \"line-log.h\"\n \n struct decoration name_decoration = { \"object names\" };\n \n@@ -856,6 +857,9 @@ int log_tree_commit(struct rev_info *opt, struct commit *commit)\n \tlog.parent = NULL;\n \topt->loginfo = &log;\n \n+\tif (opt->line_level_traverse)\n+\t\treturn line_log_print(opt, commit);\n+\n \tshown = log_tree_diff(opt, commit, &log);\n \tif (!shown && opt->loginfo && opt->always_show_header) {\n \t\tlog.parent = NULL;\ndiff --git a/revision.c b/revision.c\nindex 46319d5..3ad9d08 100644\n--- a/revision.c\n+++ b/revision.c\n@@ -13,6 +13,7 @@\n #include \"decorate.h\"\n #include \"log-tree.h\"\n #include \"string-list.h\"\n+#include \"line-log.h\"\n #include \"mailmap.h\"\n \n volatile show_early_output_fn_t show_early_output;\n@@ -1896,6 +1897,12 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s\n \tif (revs->combine_merges)\n \t\trevs->ignore_merges = 0;\n \trevs->diffopt.abbrev = revs->abbrev;\n+\n+\tif (revs->line_level_traverse) {\n+\t\trevs->limited = 1;\n+\t\trevs->topo_order = 1;\n+\t}\n+\n \tdiff_setup_done(&revs->diffopt);\n \n \tgrep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,\n@@ -2166,6 +2173,8 @@ int prepare_revision_walk(struct rev_info *revs)\n \t\t\treturn -1;\n \tif (revs->topo_order)\n \t\tsort_in_topological_order(&revs->commits, revs->lifo);\n+\tif (revs->line_level_traverse)\n+\t\tline_log_filter(revs);\n \tif (revs->simplify_merges)\n \t\tsimplify_merges(revs);\n \tif (revs->children.name)\ndiff --git a/revision.h b/revision.h\nindex 640110d..6b2870c 100644\n--- a/revision.h\n+++ b/revision.h\n@@ -96,7 +96,8 @@ struct rev_info {\n \t\t\tcherry_mark:1,\n \t\t\tbisect:1,\n \t\t\tancestry_path:1,\n-\t\t\tfirst_parent_only:1;\n+\t\t\tfirst_parent_only:1,\n+\t\t\tline_level_traverse:1;\n \n \t/* Diff flags */\n \tunsigned int\tdiff:1,\n@@ -175,6 +176,9 @@ struct rev_info {\n \tint count_left;\n \tint count_right;\n \tint count_same;\n+\n+\t/* line level range that we are chasing */\n+\tstruct decoration line_log_data;\n };\n \n #define REV_TREE_SAME\t\t0\ndiff --git a/t/t4211-line-log.sh b/t/t4211-line-log.sh\nnew file mode 100755\nindex 0000000..639a16f\n--- /dev/null\n+++ b/t/t4211-line-log.sh\n@@ -0,0 +1,41 @@\n+#!/bin/sh\n+\n+test_description='test log -L'\n+. ./test-lib.sh\n+\n+test_expect_success 'setup (import history)' '\n+\tgit fast-import < \"$TEST_DIRECTORY\"/t4211/history.export &&\n+\tgit reset --hard\n+'\n+\n+canned_test () {\n+\ttest_expect_success \"$1\" \"\n+\t\tgit log $1 >actual &&\n+\t\ttest_cmp \\\"\\$TEST_DIRECTORY\\\"/t4211/expect.$2 actual\n+\t\"\n+\tcp actual \"$TEST_DIRECTORY\"/t4211/expect.$2\n+}\n+\n+canned_test \"-L 4,12:a.c simple\" simple-f\n+canned_test \"-L 4,+9:a.c simple\" simple-f\n+canned_test \"-L '/long f/,/^}/:a.c' simple\" simple-f\n+\n+canned_test \"-L '/main/,/^}/:a.c' simple\" simple-main\n+\n+canned_test \"-L 1,+4:a.c simple\" beginning-of-file\n+canned_test \"-L 0,+5:a.c simple\" beginning-of-file\n+\n+canned_test \"-L 20:a.c simple\" end-of-file\n+canned_test \"-L 20,'$':a.c simple\" end-of-file\n+\n+test_expect_success \"-L 20,10000:a.c (bogus end)\" '\n+\ttest_must_fail git log -L 20,10000:a.c simple 2>errors &&\n+\tgrep \"has only.*lines\" errors\n+'\n+\n+canned_test \"-L '/long f/',/^}/:a.c -L /main/,/^}/:a.c simple\" two-ranges\n+canned_test \"-L 24,+1:a.c simple\" vanishes-early\n+\n+canned_test \"-L '/long f/,/^}/:b.c' move-support\" move-support-f\n+\n+test_done\ndiff --git a/t/t4211/expect.beginning-of-file b/t/t4211/expect.beginning-of-file\nnew file mode 100644\nindex 0000000..91b4054\n--- /dev/null\n+++ b/t/t4211/expect.beginning-of-file\n@@ -0,0 +1,43 @@\n+commit 4a23ae5c98d59a58c6da036156959f2dc9f472ad\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:47:40 2013 +0100\n+\n+    change at very beginning\n+\n+diff --git a/a.c b/a.c\n+--- a/a.c\n++++ b/a.c\n+@@ -1,3 +1,4 @@\n++#include <unistd.h>\n+ #include <stdio.h>\n+ \n+ long f(long x)\n+\n+commit a6eb82647d5d67f893da442f8f9375fd89a3b1e2\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:45:16 2013 +0100\n+\n+    touch both functions\n+\n+diff --git a/a.c b/a.c\n+--- a/a.c\n++++ b/a.c\n+@@ -1,3 +1,3 @@\n+ #include <stdio.h>\n+ \n+-int f(int x)\n++long f(long x)\n+\n+commit de4c48ae814792c02a49c4c3c0c757ae69c55f6a\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:44:48 2013 +0100\n+\n+    initial\n+\n+diff --git a/a.c b/a.c\n+--- /dev/null\n++++ b/a.c\n+@@ -0,0 +1,3 @@\n++#include <stdio.h>\n++\n++int f(int x)\ndiff --git a/t/t4211/expect.end-of-file b/t/t4211/expect.end-of-file\nnew file mode 100644\nindex 0000000..bd25bb2\n--- /dev/null\n+++ b/t/t4211/expect.end-of-file\n@@ -0,0 +1,62 @@\n+commit 4659538844daa2849b1a9e7d6fadb96fcd26fc83\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:48:43 2013 +0100\n+\n+    change back to complete line\n+\n+diff --git a/a.c b/a.c\n+--- a/a.c\n++++ b/a.c\n+@@ -20,3 +20,5 @@\n+ \tprintf(\"%ld\\n\", f(15));\n+ \treturn 0;\n+-}\n+\\ No newline at end of file\n++}\n++\n++/* incomplete lines are bad! */\n+\n+commit 100b61a6f2f720f812620a9d10afb3a960ccb73c\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:48:10 2013 +0100\n+\n+    change to an incomplete line at end\n+\n+diff --git a/a.c b/a.c\n+--- a/a.c\n++++ b/a.c\n+@@ -20,3 +20,3 @@\n+ \tprintf(\"%ld\\n\", f(15));\n+ \treturn 0;\n+-}\n++}\n+\\ No newline at end of file\n+\n+commit a6eb82647d5d67f893da442f8f9375fd89a3b1e2\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:45:16 2013 +0100\n+\n+    touch both functions\n+\n+diff --git a/a.c b/a.c\n+--- a/a.c\n++++ b/a.c\n+@@ -19,3 +19,3 @@\n+-\tprintf(\"%d\\n\", f(15));\n++\tprintf(\"%ld\\n\", f(15));\n+ \treturn 0;\n+ }\n+\n+commit de4c48ae814792c02a49c4c3c0c757ae69c55f6a\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:44:48 2013 +0100\n+\n+    initial\n+\n+diff --git a/a.c b/a.c\n+--- /dev/null\n++++ b/a.c\n+@@ -0,0 +18,3 @@\n++\tprintf(\"%d\\n\", f(15));\n++\treturn 0;\n++}\ndiff --git a/t/t4211/expect.move-support-f b/t/t4211/expect.move-support-f\nnew file mode 100644\nindex 0000000..78a8cf1\n--- /dev/null\n+++ b/t/t4211/expect.move-support-f\n@@ -0,0 +1,40 @@\n+commit 6ce3c4ff690136099bb17e1a8766b75764726ea7\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:49:50 2013 +0100\n+\n+    another simple change\n+\n+diff --git a/b.c b/b.c\n+--- a/b.c\n++++ b/b.c\n+@@ -4,9 +4,9 @@\n+ long f(long x)\n+ {\n+ \tint s = 0;\n+ \twhile (x) {\n+-\t\tx >>= 1;\n++\t\tx /= 2;\n+ \t\ts++;\n+ \t}\n+ \treturn s;\n+ }\n+\n+commit e6da343666244ea9e67cbe3f3bd26da860f9fe0e\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:49:28 2013 +0100\n+\n+    move file\n+\n+diff --git a/b.c b/b.c\n+--- /dev/null\n++++ b/b.c\n+@@ -0,0 +4,9 @@\n++long f(long x)\n++{\n++\tint s = 0;\n++\twhile (x) {\n++\t\tx >>= 1;\n++\t\ts++;\n++\t}\n++\treturn s;\n++}\ndiff --git a/t/t4211/expect.simple-f b/t/t4211/expect.simple-f\nnew file mode 100644\nindex 0000000..a1f5bc4\n--- /dev/null\n+++ b/t/t4211/expect.simple-f\n@@ -0,0 +1,59 @@\n+commit a6eb82647d5d67f893da442f8f9375fd89a3b1e2\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:45:16 2013 +0100\n+\n+    touch both functions\n+\n+diff --git a/a.c b/a.c\n+--- a/a.c\n++++ b/a.c\n+@@ -3,9 +3,9 @@\n+-int f(int x)\n++long f(long x)\n+ {\n+ \tint s = 0;\n+ \twhile (x) {\n+ \t\tx >>= 1;\n+ \t\ts++;\n+ \t}\n+ \treturn s;\n+ }\n+\n+commit f04fb20f2c77850996cba739709acc6faecc58f7\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:44:55 2013 +0100\n+\n+    change f()\n+\n+diff --git a/a.c b/a.c\n+--- a/a.c\n++++ b/a.c\n+@@ -3,8 +3,9 @@\n+ int f(int x)\n+ {\n+ \tint s = 0;\n+ \twhile (x) {\n+ \t\tx >>= 1;\n+ \t\ts++;\n+ \t}\n++\treturn s;\n+ }\n+\n+commit de4c48ae814792c02a49c4c3c0c757ae69c55f6a\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:44:48 2013 +0100\n+\n+    initial\n+\n+diff --git a/a.c b/a.c\n+--- /dev/null\n++++ b/a.c\n+@@ -0,0 +3,8 @@\n++int f(int x)\n++{\n++\tint s = 0;\n++\twhile (x) {\n++\t\tx >>= 1;\n++\t\ts++;\n++\t}\n++}\ndiff --git a/t/t4211/expect.simple-main b/t/t4211/expect.simple-main\nnew file mode 100644\nindex 0000000..39ce39b\n--- /dev/null\n+++ b/t/t4211/expect.simple-main\n@@ -0,0 +1,68 @@\n+commit 4659538844daa2849b1a9e7d6fadb96fcd26fc83\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:48:43 2013 +0100\n+\n+    change back to complete line\n+\n+diff --git a/a.c b/a.c\n+--- a/a.c\n++++ b/a.c\n+@@ -18,5 +18,5 @@\n+ int main ()\n+ {\n+ \tprintf(\"%ld\\n\", f(15));\n+ \treturn 0;\n+-}\n+\\ No newline at end of file\n++}\n+\n+commit 100b61a6f2f720f812620a9d10afb3a960ccb73c\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:48:10 2013 +0100\n+\n+    change to an incomplete line at end\n+\n+diff --git a/a.c b/a.c\n+--- a/a.c\n++++ b/a.c\n+@@ -18,5 +18,5 @@\n+ int main ()\n+ {\n+ \tprintf(\"%ld\\n\", f(15));\n+ \treturn 0;\n+-}\n++}\n+\\ No newline at end of file\n+\n+commit a6eb82647d5d67f893da442f8f9375fd89a3b1e2\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:45:16 2013 +0100\n+\n+    touch both functions\n+\n+diff --git a/a.c b/a.c\n+--- a/a.c\n++++ b/a.c\n+@@ -17,5 +17,5 @@\n+ int main ()\n+ {\n+-\tprintf(\"%d\\n\", f(15));\n++\tprintf(\"%ld\\n\", f(15));\n+ \treturn 0;\n+ }\n+\n+commit de4c48ae814792c02a49c4c3c0c757ae69c55f6a\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:44:48 2013 +0100\n+\n+    initial\n+\n+diff --git a/a.c b/a.c\n+--- /dev/null\n++++ b/a.c\n+@@ -0,0 +16,5 @@\n++int main ()\n++{\n++\tprintf(\"%d\\n\", f(15));\n++\treturn 0;\n++}\ndiff --git a/t/t4211/expect.two-ranges b/t/t4211/expect.two-ranges\nnew file mode 100644\nindex 0000000..6109aa0\n--- /dev/null\n+++ b/t/t4211/expect.two-ranges\n@@ -0,0 +1,102 @@\n+commit 4659538844daa2849b1a9e7d6fadb96fcd26fc83\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:48:43 2013 +0100\n+\n+    change back to complete line\n+\n+diff --git a/a.c b/a.c\n+--- a/a.c\n++++ b/a.c\n+@@ -18,5 +18,5 @@\n+ int main ()\n+ {\n+ \tprintf(\"%ld\\n\", f(15));\n+ \treturn 0;\n+-}\n+\\ No newline at end of file\n++}\n+\n+commit 100b61a6f2f720f812620a9d10afb3a960ccb73c\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:48:10 2013 +0100\n+\n+    change to an incomplete line at end\n+\n+diff --git a/a.c b/a.c\n+--- a/a.c\n++++ b/a.c\n+@@ -18,5 +18,5 @@\n+ int main ()\n+ {\n+ \tprintf(\"%ld\\n\", f(15));\n+ \treturn 0;\n+-}\n++}\n+\\ No newline at end of file\n+\n+commit a6eb82647d5d67f893da442f8f9375fd89a3b1e2\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:45:16 2013 +0100\n+\n+    touch both functions\n+\n+diff --git a/a.c b/a.c\n+--- a/a.c\n++++ b/a.c\n+@@ -3,9 +3,9 @@\n+-int f(int x)\n++long f(long x)\n+ {\n+ \tint s = 0;\n+ \twhile (x) {\n+ \t\tx >>= 1;\n+ \t\ts++;\n+ \t}\n+ \treturn s;\n+ }\n+@@ -17,5 +17,5 @@\n+ int main ()\n+ {\n+-\tprintf(\"%d\\n\", f(15));\n++\tprintf(\"%ld\\n\", f(15));\n+ \treturn 0;\n+ }\n+\n+commit f04fb20f2c77850996cba739709acc6faecc58f7\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:44:55 2013 +0100\n+\n+    change f()\n+\n+diff --git a/a.c b/a.c\n+--- a/a.c\n++++ b/a.c\n+@@ -3,8 +3,9 @@\n+ int f(int x)\n+ {\n+ \tint s = 0;\n+ \twhile (x) {\n+ \t\tx >>= 1;\n+ \t\ts++;\n+ \t}\n++\treturn s;\n+ }\n+\n+commit de4c48ae814792c02a49c4c3c0c757ae69c55f6a\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:44:48 2013 +0100\n+\n+    initial\n+\n+diff --git a/a.c b/a.c\n+--- /dev/null\n++++ b/a.c\n+@@ -0,0 +3,8 @@\n++int f(int x)\n++{\n++\tint s = 0;\n++\twhile (x) {\n++\t\tx >>= 1;\n++\t\ts++;\n++\t}\n++}\ndiff --git a/t/t4211/expect.vanishes-early b/t/t4211/expect.vanishes-early\nnew file mode 100644\nindex 0000000..1f7cd06\n--- /dev/null\n+++ b/t/t4211/expect.vanishes-early\n@@ -0,0 +1,39 @@\n+commit 4659538844daa2849b1a9e7d6fadb96fcd26fc83\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:48:43 2013 +0100\n+\n+    change back to complete line\n+\n+diff --git a/a.c b/a.c\n+--- a/a.c\n++++ b/a.c\n+@@ -22,1 +24,1 @@\n+-}\n+\\ No newline at end of file\n++/* incomplete lines are bad! */\n+\n+commit 100b61a6f2f720f812620a9d10afb3a960ccb73c\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:48:10 2013 +0100\n+\n+    change to an incomplete line at end\n+\n+diff --git a/a.c b/a.c\n+--- a/a.c\n++++ b/a.c\n+@@ -22,1 +22,1 @@\n+-}\n++}\n+\\ No newline at end of file\n+\n+commit de4c48ae814792c02a49c4c3c0c757ae69c55f6a\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:44:48 2013 +0100\n+\n+    initial\n+\n+diff --git a/a.c b/a.c\n+--- /dev/null\n++++ b/a.c\n+@@ -0,0 +20,1 @@\n++}\ndiff --git a/t/t4211/history.export b/t/t4211/history.export\nnew file mode 100644\nindex 0000000..c159794\n--- /dev/null\n+++ b/t/t4211/history.export\n@@ -0,0 +1,330 @@\n+blob\n+mark :1\n+data 157\n+#include <stdio.h>\n+\n+int f(int x)\n+{\n+\tint s = 0;\n+\twhile (x) {\n+\t\tx >>= 1;\n+\t\ts++;\n+\t}\n+}\n+\n+/*\n+ * A comment.\n+ */\n+\n+int main ()\n+{\n+\tprintf(\"%d\\n\", f(15));\n+\treturn 0;\n+}\n+\n+reset refs/tags/simple\n+commit refs/tags/simple\n+mark :2\n+author Thomas Rast <trast@student.ethz.ch> 1362044688 +0100\n+committer Thomas Rast <trast@student.ethz.ch> 1362044688 +0100\n+data 8\n+initial\n+M 100644 :1 a.c\n+\n+blob\n+mark :3\n+data 168\n+#include <stdio.h>\n+\n+int f(int x)\n+{\n+\tint s = 0;\n+\twhile (x) {\n+\t\tx >>= 1;\n+\t\ts++;\n+\t}\n+\treturn s;\n+}\n+\n+/*\n+ * A comment.\n+ */\n+\n+int main ()\n+{\n+\tprintf(\"%d\\n\", f(15));\n+\treturn 0;\n+}\n+\n+commit refs/tags/simple\n+mark :4\n+author Thomas Rast <trast@student.ethz.ch> 1362044695 +0100\n+committer Thomas Rast <trast@student.ethz.ch> 1362044695 +0100\n+data 11\n+change f()\n+from :2\n+M 100644 :3 a.c\n+\n+blob\n+mark :5\n+data 171\n+#include <stdio.h>\n+\n+long f(long x)\n+{\n+\tint s = 0;\n+\twhile (x) {\n+\t\tx >>= 1;\n+\t\ts++;\n+\t}\n+\treturn s;\n+}\n+\n+/*\n+ * A comment.\n+ */\n+\n+int main ()\n+{\n+\tprintf(\"%ld\\n\", f(15));\n+\treturn 0;\n+}\n+\n+commit refs/tags/simple\n+mark :6\n+author Thomas Rast <trast@student.ethz.ch> 1362044716 +0100\n+committer Thomas Rast <trast@student.ethz.ch> 1362044716 +0100\n+data 21\n+touch both functions\n+from :4\n+M 100644 :5 a.c\n+\n+blob\n+mark :7\n+data 185\n+#include <stdio.h>\n+\n+long f(long x)\n+{\n+\tint s = 0;\n+\twhile (x) {\n+\t\tx >>= 1;\n+\t\ts++;\n+\t}\n+\treturn s;\n+}\n+\n+/*\n+ * This is only an example!\n+ */\n+\n+int main ()\n+{\n+\tprintf(\"%ld\\n\", f(15));\n+\treturn 0;\n+}\n+\n+commit refs/tags/simple\n+mark :8\n+author Thomas Rast <trast@student.ethz.ch> 1362044741 +0100\n+committer Thomas Rast <trast@student.ethz.ch> 1362044741 +0100\n+data 14\n+touch comment\n+from :6\n+M 100644 :7 a.c\n+\n+blob\n+mark :9\n+data 205\n+#include <unistd.h>\n+#include <stdio.h>\n+\n+long f(long x)\n+{\n+\tint s = 0;\n+\twhile (x) {\n+\t\tx >>= 1;\n+\t\ts++;\n+\t}\n+\treturn s;\n+}\n+\n+/*\n+ * This is only an example!\n+ */\n+\n+int main ()\n+{\n+\tprintf(\"%ld\\n\", f(15));\n+\treturn 0;\n+}\n+\n+commit refs/tags/simple\n+mark :10\n+author Thomas Rast <trast@student.ethz.ch> 1362044860 +0100\n+committer Thomas Rast <trast@student.ethz.ch> 1362044860 +0100\n+data 25\n+change at very beginning\n+from :8\n+M 100644 :9 a.c\n+\n+blob\n+mark :11\n+data 204\n+#include <unistd.h>\n+#include <stdio.h>\n+\n+long f(long x)\n+{\n+\tint s = 0;\n+\twhile (x) {\n+\t\tx >>= 1;\n+\t\ts++;\n+\t}\n+\treturn s;\n+}\n+\n+/*\n+ * This is only an example!\n+ */\n+\n+int main ()\n+{\n+\tprintf(\"%ld\\n\", f(15));\n+\treturn 0;\n+}\n+commit refs/tags/simple\n+mark :12\n+author Thomas Rast <trast@student.ethz.ch> 1362044890 +0100\n+committer Thomas Rast <trast@student.ethz.ch> 1362044890 +0100\n+data 36\n+change to an incomplete line at end\n+from :10\n+M 100644 :11 a.c\n+\n+blob\n+mark :13\n+data 238\n+#include <unistd.h>\n+#include <stdio.h>\n+\n+long f(long x)\n+{\n+\tint s = 0;\n+\twhile (x) {\n+\t\tx >>= 1;\n+\t\ts++;\n+\t}\n+\treturn s;\n+}\n+\n+/*\n+ * This is only an example!\n+ */\n+\n+int main ()\n+{\n+\tprintf(\"%ld\\n\", f(15));\n+\treturn 0;\n+}\n+\n+/* incomplete lines are bad! */\n+\n+commit refs/tags/simple\n+mark :14\n+author Thomas Rast <trast@student.ethz.ch> 1362044923 +0100\n+committer Thomas Rast <trast@student.ethz.ch> 1362044923 +0100\n+data 29\n+change back to complete line\n+from :12\n+M 100644 :13 a.c\n+\n+commit refs/tags/move-support\n+mark :15\n+author Thomas Rast <trast@student.ethz.ch> 1362044968 +0100\n+committer Thomas Rast <trast@student.ethz.ch> 1362044968 +0100\n+data 10\n+move file\n+from :14\n+D a.c\n+M 100644 :13 b.c\n+\n+blob\n+mark :16\n+data 237\n+#include <unistd.h>\n+#include <stdio.h>\n+\n+long f(long x)\n+{\n+\tint s = 0;\n+\twhile (x) {\n+\t\tx /= 2;\n+\t\ts++;\n+\t}\n+\treturn s;\n+}\n+\n+/*\n+ * This is only an example!\n+ */\n+\n+int main ()\n+{\n+\tprintf(\"%ld\\n\", f(15));\n+\treturn 0;\n+}\n+\n+/* incomplete lines are bad! */\n+\n+commit refs/tags/move-support\n+mark :17\n+author Thomas Rast <trast@student.ethz.ch> 1362044990 +0100\n+committer Thomas Rast <trast@student.ethz.ch> 1362044990 +0100\n+data 22\n+another simple change\n+from :15\n+M 100644 :16 b.c\n+\n+blob\n+mark :18\n+data 254\n+#include <unistd.h>\n+#include <stdio.h>\n+\n+long f(long x);\n+\n+/*\n+ * This is only an example!\n+ */\n+\n+int main ()\n+{\n+\tprintf(\"%ld\\n\", f(15));\n+\treturn 0;\n+}\n+\n+/* incomplete lines are bad! */\n+\n+long f(long x)\n+{\n+\tint s = 0;\n+\twhile (x) {\n+\t\tx /= 2;\n+\t\ts++;\n+\t}\n+\treturn s;\n+}\n+\n+commit refs/heads/master\n+mark :19\n+author Thomas Rast <trast@student.ethz.ch> 1362045024 +0100\n+committer Thomas Rast <trast@student.ethz.ch> 1362045024 +0100\n+data 21\n+move within the file\n+from :17\n+M 100644 :18 b.c\n+\n+reset refs/heads/master\n+from :19\n+\n-- \n1.8.2.rc1.388.g1bd82c8\n"},{"id":"210424","messageId":"eba9760baed7fef88c469544730e22fd8adb1748.1362069310.git.trast@student.ethz.ch","threadId":"33038","inReplyTo":"cover.1362069310.git.trast@student.ethz.ch","subject":"[PATCH v8 5/5] log -L: :pattern:file syntax to find by funcname","fromName":"Thomas Rast","fromEmail":"trast@student.ethz.ch","sentAt":"2013-02-28T16:38:23Z","receivedAt":"2013-02-28T16:38:23Z","isPatch":true,"sender":{"key":"tr@thomasrast.ch","avatar":"https://avatars.githubusercontent.com/u/153510?v=4"},"body":"This new syntax finds a funcname matching /pattern/, and then takes from there\nup to (but not including) the next funcname.  So you can say\n\n  git log -L:main:main.c\n\nand it will dig up the main() function and show its line-log, provided\nthere are no other funcnames matching 'main'.\n\nSigned-off-by: Thomas Rast <trast@student.ethz.ch>\n---\n Documentation/line-range-format.txt |   7 ++\n builtin/blame.c                     |   2 +-\n line-log.c                          | 130 +++++++++++++++++++++++++++++++++++-\n line-log.h                          |   3 +-\n t/t4211-line-log.sh                 |   3 +-\n t/t4211/expect.simple-f-to-main     | 100 +++++++++++++++++++++++++++\n t/t4211/expect.simple-main-to-end   |  70 +++++++++++++++++++\n 7 files changed, 310 insertions(+), 5 deletions(-)\n create mode 100644 t/t4211/expect.simple-f-to-main\n create mode 100644 t/t4211/expect.simple-main-to-end\n\ndiff --git a/Documentation/line-range-format.txt b/Documentation/line-range-format.txt\nindex 9ce0688..5279a76 100644\n--- a/Documentation/line-range-format.txt\n+++ b/Documentation/line-range-format.txt\n@@ -22,3 +22,10 @@ of lines before or after the line given by <start>.\n A literal dollar sign can be used as a shorthand for the last line in\n the file.\n +\n+\n+- :regex\n++\n+If the entire range is of the form :regex, it denotes the range from\n+the first funcname line that matches <regex>, up to the next funcname\n+line.\n++\ndiff --git a/builtin/blame.c b/builtin/blame.c\nindex c933b15..b7d210f 100644\n--- a/builtin/blame.c\n+++ b/builtin/blame.c\n@@ -1940,7 +1940,7 @@ static void prepare_blame_range(struct scoreboard *sb,\n \t\t\t\tlong lno,\n \t\t\t\tlong *bottom, long *top)\n {\n-\tif (parse_range_arg(bottomtop, nth_line_cb, sb, lno, bottom, top))\n+\tif (parse_range_arg(bottomtop, nth_line_cb, sb, lno, bottom, top, sb->path))\n \t\tusage(blame_usage);\n }\n \ndiff --git a/line-log.c b/line-log.c\nindex 789b7c4..a74bbaf 100644\n--- a/line-log.c\n+++ b/line-log.c\n@@ -11,6 +11,7 @@\n #include \"strbuf.h\"\n #include \"log-tree.h\"\n #include \"graph.h\"\n+#include \"userdiff.h\"\n #include \"line-log.h\"\n \n void range_set_grow (struct range_set *rs, size_t extra)\n@@ -531,9 +532,130 @@ const char *parse_loc(const char *spec, nth_line_fn_t nth_line,\n \t}\n }\n \n+static int match_funcname(xdemitconf_t *xecfg, const char *bol, const char *eol)\n+{\n+\tif (xecfg) {\n+\t\tchar buf[1];\n+\t\treturn xecfg->find_func(bol, eol - bol, buf, 1,\n+\t\t\t\t\txecfg->find_func_priv) >= 0;\n+\t}\n+\n+\tif (bol == eol)\n+\t\treturn 0;\n+\tif (isalpha(*bol) || *bol == '_' || *bol == '$')\n+\t\treturn 1;\n+\treturn 0;\n+}\n+\n+static const char *find_funcname_matching_regexp(xdemitconf_t *xecfg, const char *start,\n+\t\t\t\t\t\t regex_t *regexp)\n+{\n+\tint reg_error;\n+\tregmatch_t match[1];\n+\twhile (1) {\n+\t\tconst char *bol, *eol;\n+\t\treg_error = regexec(regexp, start, 1, match, 0);\n+\t\tif (reg_error == REG_NOMATCH)\n+\t\t\treturn NULL;\n+\t\telse if (reg_error) {\n+\t\t\tchar errbuf[1024];\n+\t\t\tregerror(reg_error, regexp, errbuf, 1024);\n+\t\t\tdie(\"-L parameter: regexec() failed: %s\", errbuf);\n+\t\t}\n+\t\t/* determine extent of line matched */\n+\t\tbol = start+match[0].rm_so;\n+\t\teol = start+match[0].rm_eo;\n+\t\twhile (bol > start && *bol != '\\n')\n+\t\t\tbol--;\n+\t\tif (*bol == '\\n')\n+\t\t\tbol++;\n+\t\twhile (*eol && *eol != '\\n')\n+\t\t\teol++;\n+\t\tif (*eol == '\\n')\n+\t\t\teol++;\n+\t\t/* is it a funcname line? */\n+\t\tif (match_funcname(xecfg, (char*) bol, (char*) eol))\n+\t\t\treturn bol;\n+\t\tstart = eol;\n+\t}\n+}\n+\n+static const char *parse_range_funcname(const char *arg, nth_line_fn_t nth_line_cb,\n+\t\t\t\t\tvoid *cb_data, long lines, long *begin, long *end,\n+\t\t\t\t\tconst char *path)\n+{\n+\tconst char *pattern;\n+\tconst char *term;\n+\tstruct userdiff_driver *drv;\n+\txdemitconf_t *xecfg = NULL;\n+\tconst char *start;\n+\tconst char *p;\n+\tint reg_error;\n+\tregex_t regexp;\n+\n+\tpattern = arg+1;\n+\tterm = (char*) strchr(pattern, ':');\n+\tif (term) {\n+\t\tassert(!begin);\n+\t\treturn term;\n+\t}\n+\t/* all of the rest is the regex */\n+\tterm = pattern + strlen(pattern);\n+\n+\tstart = nth_line_cb(cb_data, 0);\n+\n+\tdrv = userdiff_find_by_path(path);\n+\tif (drv && drv->funcname.pattern) {\n+\t\tconst struct userdiff_funcname *pe = &drv->funcname;\n+\t\txecfg = xcalloc(1, sizeof(*xecfg));\n+\t\txdiff_set_find_func(xecfg, pe->pattern, pe->cflags);\n+\t}\n+\n+\treg_error = regcomp(&regexp, pattern, REG_NEWLINE);\n+\tif (reg_error) {\n+\t\tchar errbuf[1024];\n+\t\tregerror(reg_error, &regexp, errbuf, 1024);\n+\t\tdie(\"-L parameter '%s': %s\", pattern, errbuf);\n+\t}\n+\n+\tp = find_funcname_matching_regexp(xecfg, (char*) start, &regexp);\n+\tif (!p)\n+\t\tdie(\"-L parameter '%s': no match\", pattern);\n+\t*begin = 0;\n+\twhile (p > nth_line_cb(cb_data, *begin))\n+\t\t(*begin)++;\n+\n+\tif (*begin >= lines)\n+\t\tdie(\"-L parameter '%s' matches at EOF\", pattern);\n+\n+\t*end = *begin+1;\n+\twhile (*end < lines) {\n+\t\tconst char *bol = nth_line_cb(cb_data, *end);\n+\t\tconst char *eol = nth_line_cb(cb_data, *end+1);\n+\t\tif (match_funcname(xecfg, bol, eol))\n+\t\t\tbreak;\n+\t\t(*end)++;\n+\t}\n+\n+\tregfree(&regexp);\n+\tfree(xecfg);\n+\n+\t/* compensate for 1-based numbering */\n+\t(*begin)++;\n+\n+\treturn term;\n+}\n+\n int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,\n-\t\tvoid *cb_data, long lines, long *begin, long *end)\n+\t\t    void *cb_data, long lines, long *begin, long *end,\n+\t\t    const char *path)\n {\n+\tif (*arg == ':') {\n+\t\targ = parse_range_funcname(arg, nth_line_cb, cb_data, lines, begin, end, path);\n+\t\tif (*arg)\n+\t\t\treturn -1;\n+\t}\n+\n \targ = parse_loc(arg, nth_line_cb, cb_data, lines, -1, begin);\n \n \tif (*arg == ',') {\n@@ -553,6 +675,9 @@ int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,\n \n const char *skip_range_arg(const char *arg)\n {\n+\tif (*arg == ':')\n+\t\treturn parse_range_funcname(arg, NULL, NULL, 0, NULL, NULL, NULL);\n+\n \targ = parse_loc(arg, NULL, NULL, 0, -1, 0);\n \n \tif (*arg == ',')\n@@ -681,7 +806,8 @@ static const char *nth_line(void *data, long line)\n \t\tcb_data.line_ends = ends;\n \n \t\tif (parse_range_arg(range_part, nth_line, &cb_data,\n-\t\t\t\t    lines, &begin, &end))\n+\t\t\t\t    lines, &begin, &end,\n+\t\t\t\t    spec->path))\n \t\t\tdie(\"malformed -L argument '%s'\", range_part);\n \t\tif (begin < 1)\n \t\t\tbegin = 1;\ndiff --git a/line-log.h b/line-log.h\nindex e3ec094..071e739 100644\n--- a/line-log.h\n+++ b/line-log.h\n@@ -20,7 +20,8 @@\n extern int parse_range_arg(const char *arg,\n \t\t\t   nth_line_fn_t nth_line_cb,\n \t\t\t   void *cb_data, long lines,\n-\t\t\t   long *begin, long *end);\n+\t\t\t   long *begin, long *end,\n+\t\t\t   const char *path);\n \n /*\n  * Scan past a range argument that could be parsed by\ndiff --git a/t/t4211-line-log.sh b/t/t4211-line-log.sh\nindex 639a16f..d488690 100755\n--- a/t/t4211-line-log.sh\n+++ b/t/t4211-line-log.sh\n@@ -13,14 +13,15 @@ canned_test () {\n \t\tgit log $1 >actual &&\n \t\ttest_cmp \\\"\\$TEST_DIRECTORY\\\"/t4211/expect.$2 actual\n \t\"\n-\tcp actual \"$TEST_DIRECTORY\"/t4211/expect.$2\n }\n \n canned_test \"-L 4,12:a.c simple\" simple-f\n canned_test \"-L 4,+9:a.c simple\" simple-f\n canned_test \"-L '/long f/,/^}/:a.c' simple\" simple-f\n+canned_test \"-L :f:a.c simple\" simple-f-to-main\n \n canned_test \"-L '/main/,/^}/:a.c' simple\" simple-main\n+canned_test \"-L :main:a.c simple\" simple-main-to-end\n \n canned_test \"-L 1,+4:a.c simple\" beginning-of-file\n canned_test \"-L 0,+5:a.c simple\" beginning-of-file\ndiff --git a/t/t4211/expect.simple-f-to-main b/t/t4211/expect.simple-f-to-main\nnew file mode 100644\nindex 0000000..a475768\n--- /dev/null\n+++ b/t/t4211/expect.simple-f-to-main\n@@ -0,0 +1,100 @@\n+commit 39b6eb2d5b706d3322184a169f666f25ed3fbd00\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:45:41 2013 +0100\n+\n+    touch comment\n+\n+diff --git a/a.c b/a.c\n+--- a/a.c\n++++ b/a.c\n+@@ -3,14 +3,14 @@\n+ long f(long x)\n+ {\n+ \tint s = 0;\n+ \twhile (x) {\n+ \t\tx >>= 1;\n+ \t\ts++;\n+ \t}\n+ \treturn s;\n+ }\n+ \n+ /*\n+- * A comment.\n++ * This is only an example!\n+  */\n+ \n+\n+commit a6eb82647d5d67f893da442f8f9375fd89a3b1e2\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:45:16 2013 +0100\n+\n+    touch both functions\n+\n+diff --git a/a.c b/a.c\n+--- a/a.c\n++++ b/a.c\n+@@ -3,14 +3,14 @@\n+-int f(int x)\n++long f(long x)\n+ {\n+ \tint s = 0;\n+ \twhile (x) {\n+ \t\tx >>= 1;\n+ \t\ts++;\n+ \t}\n+ \treturn s;\n+ }\n+ \n+ /*\n+  * A comment.\n+  */\n+ \n+\n+commit f04fb20f2c77850996cba739709acc6faecc58f7\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:44:55 2013 +0100\n+\n+    change f()\n+\n+diff --git a/a.c b/a.c\n+--- a/a.c\n++++ b/a.c\n+@@ -3,13 +3,14 @@\n+ int f(int x)\n+ {\n+ \tint s = 0;\n+ \twhile (x) {\n+ \t\tx >>= 1;\n+ \t\ts++;\n+ \t}\n++\treturn s;\n+ }\n+ \n+ /*\n+  * A comment.\n+  */\n+ \n+\n+commit de4c48ae814792c02a49c4c3c0c757ae69c55f6a\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:44:48 2013 +0100\n+\n+    initial\n+\n+diff --git a/a.c b/a.c\n+--- /dev/null\n++++ b/a.c\n+@@ -0,0 +3,13 @@\n++int f(int x)\n++{\n++\tint s = 0;\n++\twhile (x) {\n++\t\tx >>= 1;\n++\t\ts++;\n++\t}\n++}\n++\n++/*\n++ * A comment.\n++ */\n++\ndiff --git a/t/t4211/expect.simple-main-to-end b/t/t4211/expect.simple-main-to-end\nnew file mode 100644\nindex 0000000..8480bd9\n--- /dev/null\n+++ b/t/t4211/expect.simple-main-to-end\n@@ -0,0 +1,70 @@\n+commit 4659538844daa2849b1a9e7d6fadb96fcd26fc83\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:48:43 2013 +0100\n+\n+    change back to complete line\n+\n+diff --git a/a.c b/a.c\n+--- a/a.c\n++++ b/a.c\n+@@ -18,5 +18,7 @@\n+ int main ()\n+ {\n+ \tprintf(\"%ld\\n\", f(15));\n+ \treturn 0;\n+-}\n+\\ No newline at end of file\n++}\n++\n++/* incomplete lines are bad! */\n+\n+commit 100b61a6f2f720f812620a9d10afb3a960ccb73c\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:48:10 2013 +0100\n+\n+    change to an incomplete line at end\n+\n+diff --git a/a.c b/a.c\n+--- a/a.c\n++++ b/a.c\n+@@ -18,5 +18,5 @@\n+ int main ()\n+ {\n+ \tprintf(\"%ld\\n\", f(15));\n+ \treturn 0;\n+-}\n++}\n+\\ No newline at end of file\n+\n+commit a6eb82647d5d67f893da442f8f9375fd89a3b1e2\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:45:16 2013 +0100\n+\n+    touch both functions\n+\n+diff --git a/a.c b/a.c\n+--- a/a.c\n++++ b/a.c\n+@@ -17,5 +17,5 @@\n+ int main ()\n+ {\n+-\tprintf(\"%d\\n\", f(15));\n++\tprintf(\"%ld\\n\", f(15));\n+ \treturn 0;\n+ }\n+\n+commit de4c48ae814792c02a49c4c3c0c757ae69c55f6a\n+Author: Thomas Rast <trast@student.ethz.ch>\n+Date:   Thu Feb 28 10:44:48 2013 +0100\n+\n+    initial\n+\n+diff --git a/a.c b/a.c\n+--- /dev/null\n++++ b/a.c\n+@@ -0,0 +16,5 @@\n++int main ()\n++{\n++\tprintf(\"%d\\n\", f(15));\n++\treturn 0;\n++}\n-- \n1.8.2.rc1.388.g1bd82c8\n"},{"id":"210427","messageId":"7vobf4icjh.fsf@alter.siamese.dyndns.org","threadId":"33038","inReplyTo":"d22070b416a1787ece6f98a722614a54826b93ce.1362069310.git.trast@student.ethz.ch","subject":"Re: [PATCH v8 1/5] Refactor parse_loc","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-02-28T17:16:50Z","receivedAt":"2013-02-28T17:16:50Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Thomas Rast <trast@student.ethz.ch> writes:\n\n> From: Bo Yang <struggleyb.nku@gmail.com>\n>\n> We want to use the same style of -L n,m argument for 'git log -L' as\n> for git-blame.  Refactor the argument parsing of the range arguments\n> from builtin/blame.c to the (new) file that will hold the 'git log -L'\n> logic.\n>\n> To accommodate different data structures in blame and log -L, the file\n> contents are abstracted away; parse_range_arg takes a callback that it\n> uses to get the contents of a line of the (notional) file.\n>\n> The new test is for a case that made me pause during debugging: the\n> 'blame -L with invalid end' test was the only one that noticed an\n> outright failure to parse the end *at all*.  So make a more explicit\n> test for that.\n>\n> Signed-off-by: Bo Yang <struggleyb.nku@gmail.com>\n> Signed-off-by: Thomas Rast <trast@student.ethz.ch>\n> ---\n>  Documentation/blame-options.txt     |  19 +------\n>  Documentation/line-range-format.txt |  18 +++++++\n>  Makefile                            |   2 +\n>  builtin/blame.c                     |  99 +++-------------------------------\n>  line-log.c                          | 105 ++++++++++++++++++++++++++++++++++++\n>  line-log.h                          |  23 ++++++++\n\nWas this churn necessary?  \n\nIt is strange to move existing functions that will be tweaked to be\nshared by two different codepaths (blame and line-log) to the new\nuser.\n\nThe only effect this has, as opposed to tweaking the functions in\nplace and making them extern, is to make it harder to see the tweaks\nmade while moving the lines, and also make it more cumbersome to\ndetermine the lineage of the code later.\n \nIt would have been understandable if they were moved to a new\nlibrary-ish file (perhaps \"line-range.[ch]\"); even though that\napproach shares the same downsides, at least it would have a better\nexcuse \"We will share this, so let's move it to a neutral third\nplace to allow us to hide the implementation details from both\nusers\".  The arrangement this patch series makes does not even have\nthat excuse.  The final implementation still stay with one of the\nusers; the only difference is that it is away from the original user\nand close to the new user.\n\n> @@ -1927,83 +1933,6 @@ static const char *add_prefix(const char *prefix, const char *path)\n>  }\n>  \n>  /*\n> - * Parsing of (comma separated) one item in the -L option\n> - */\n> -static const char *parse_loc(const char *spec,\n> -\t\t\t     struct scoreboard *sb, long lno,\n> -\t\t\t     long begin, long *ret)\n> -{\n> -\tchar *term;\n> -\tconst char *line;\n> -\tlong num;\n> -\tint reg_error;\n> -\tregex_t regexp;\n> -\tregmatch_t match[1];\n> -\n> -\t/* Allow \"-L <something>,+20\" to mean starting at <something>\n> -\t * for 20 lines, or \"-L <something>,-5\" for 5 lines ending at\n> -\t * <something>.\n> -\t */\n> -\tif (1 < begin && (spec[0] == '+' || spec[0] == '-')) {\n\nDid you fix a bug here?\n\nThis original only process -L begin,+20 and -L begin,-4 for the\nvalue of begin larger than 1 while reading the second part that\ncomes after the comma, but incoming begin must be 2 or more, because\nthe caller adds one to the result of parsing what comes before the\ncomma, and \"1 < begin\" here, not \"begin != -1\", is done for that\nreason.\n\nI noticed these slight differences only after eyeballing the lines\ndeleted from here and lines added to the other place, but the\ndifferences would have been unnoticed if reviewers were not careful.\n\nAgain, was it really necessary to move these functions to the new\nfile?\n\n> diff --git a/line-log.c b/line-log.c\n> new file mode 100644\n> index 0000000..a24a86b\n> --- /dev/null\n> +++ b/line-log.c\n> @@ -0,0 +1,105 @@\n> +#include \"git-compat-util.h\"\n> +#include \"line-log.h\"\n> +\n> +/*\n> + * Parse one item in the -L option\n> + */\n> +const char *parse_loc(const char *spec, nth_line_fn_t nth_line,\n> +\t\tvoid *data, long lines, long begin, long *ret)\n> +{\n> +\tchar *term;\n> +\tconst char *line;\n> +\tlong num;\n> +\tint reg_error;\n> +\tregex_t regexp;\n> +\tregmatch_t match[1];\n> +\n> +\t/*\n> +\t * Allow \"-L <something>,+20\" to mean starting at <something>\n> +\t * for 20 lines, or \"-L <something>,-5\" for 5 lines ending at\n> +\t * <something>.\n> +\t */\n> +\tif (begin != -1 && (spec[0] == '+' || spec[0] == '-')) {\n> +\t\tnum = strtol(spec + 1, &term, 10);\n> +\t\tif (term != spec + 1) {\n> +\t\t\tif (spec[0] == '-')\n> +\t\t\t\tnum = 0 - num;\n> +\t\t\tif (0 < num)\n> +\t\t\t\t*ret = begin + num - 2;\n> +\t\t\telse if (!num)\n> +\t\t\t\t*ret = begin;\n> +\t\t\telse\n> +\t\t\t\t*ret = begin + num;\n> +\t\t\treturn term;\n> +\t\t}\n> +\t\treturn spec;\n> +\t}\n> +\tnum = strtol(spec, &term, 10);\n> +\tif (term != spec) {\n> +\t\t*ret = num;\n> +\t\treturn term;\n> +\t}\n> +\tif (spec[0] != '/')\n> +\t\treturn spec;\n> +\n> +\t/* it could be a regexp of form /.../ */\n> +\tfor (term = (char *) spec + 1; *term && *term != '/'; term++) {\n> +\t\tif (*term == '\\\\')\n> +\t\t\tterm++;\n> +\t}\n> +\tif (*term != '/')\n> +\t\treturn spec;\n> +\n> +\t/* try [spec+1 .. term-1] as regexp */\n> +\t*term = 0;\n> +\tif (begin == -1)\n> +\t\tbegin = 1;\n> +\tbegin--; /* input is in human terms */\n> +\tline = nth_line(data, begin);\n> +\n> +\tif (!(reg_error = regcomp(&regexp, spec + 1, REG_NEWLINE)) &&\n> +\t    !(reg_error = regexec(&regexp, line, 1, match, 0))) {\n> +\t\tconst char *cp = line + match[0].rm_so;\n> +\t\tconst char *nline;\n> +\n> +\t\twhile (begin++ < lines) {\n> +\t\t\tnline = nth_line(data, begin);\n> +\t\t\tif (line <= cp && cp < nline)\n> +\t\t\t\tbreak;\n> +\t\t\tline = nline;\n> +\t\t}\n> +\t\t*ret = begin;\n> +\t\tregfree(&regexp);\n> +\t\t*term++ = '/';\n> +\t\treturn term;\n> +\t} else {\n> +\t\tchar errbuf[1024];\n> +\t\tregerror(reg_error, &regexp, errbuf, 1024);\n> +\t\tdie(\"-L parameter '%s': %s\", spec + 1, errbuf);\n> +\t}\n> +}\n> +\n> +int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,\n> +\t\tvoid *cb_data, long lines, long *begin, long *end)\n> +{\n\nThis, especially the nth-line function, is a good abstraction.\n\n> +\targ = parse_loc(arg, nth_line_cb, cb_data, lines, -1, begin);\n> +\n> +\tif (*arg == ',') {\n\nCan the input string begin with a comma, e.g. \",-4\" which we may\nwant to mean the last four lines or something?\n\n> +\t\targ = parse_loc(arg+1, nth_line_cb, cb_data, lines, *begin+1, end);\n> +\t\tif (*begin > *end) {\n> +\t\t\tlong tmp = *begin;\n> +\t\t\t*begin = *end;\n> +\t\t\t*end = tmp;\n> +\t\t}\n> +\t}\n> +\n> +\tif (*begin <= 0)\n> +\t\t*begin = 1;\n> +\tif (*end > lines)\n> +\t\t*end = lines;\n> +\n> +\tif (*arg)\n> +\t\treturn -1;\n> +\n> +\treturn 0;\n> +}\n> diff --git a/line-log.h b/line-log.h\n> new file mode 100644\n> index 0000000..22b1c80\n> --- /dev/null\n> +++ b/line-log.h\n> @@ -0,0 +1,23 @@\n> +#ifndef LINE_LOG_H\n> +#define LINE_LOG_H\n> +\n> +/*\n> + * Parse one item in an -L begin,end option w.r.t. the notional file\n> + * object 'cb_data' consisting of 'lines' lines.\n> + *\n> + * The 'nth_line_cb' callback is used to determine the start of the\n> + * line 'lno' inside the 'cb_data'.  The caller is expected to already\n> + * have a suitable map at hand to make this a constant-time lookup.\n> + *\n> + * Returns 0 in case of success and -1 if there was an error.  The\n> + * caller should print a usage message in the latter case.\n> + */\n\nline number counts from 0 or 1?\n\n> +\n> +typedef const char *(*nth_line_fn_t)(void *data, long lno);\n> +\n> +extern int parse_range_arg(const char *arg,\n> +\t\t\t   nth_line_fn_t nth_line_cb,\n> +\t\t\t   void *cb_data, long lines,\n> +\t\t\t   long *begin, long *end);\n> +\n> +#endif /* LINE_LOG_H */\n> diff --git a/t/t8003-blame-corner-cases.sh b/t/t8003-blame-corner-cases.sh\n> index 230143c..e7cac1d 100755\n> --- a/t/t8003-blame-corner-cases.sh\n> +++ b/t/t8003-blame-corner-cases.sh\n> @@ -175,6 +175,12 @@ test_expect_success 'blame -L with invalid end' '\n>  \tgrep \"has only 2 lines\" errors\n>  '\n>  \n> +test_expect_success 'blame parses <end> part of -L' '\n> +\tgit blame -L1,1 tres >out &&\n> +\tcat out &&\n> +\ttest $(wc -l < out) -eq 1\n> +'\n> +\n>  test_expect_success 'indent of line numbers, nine lines' '\n>  \tgit blame nine_lines >actual &&\n>  \ttest $(grep -c \"  \" actual) = 0\n"},{"id":"210429","messageId":"7vk3psicgf.fsf@alter.siamese.dyndns.org","threadId":"33038","inReplyTo":"7973d90cfcd86a3c15776b8718ad6bd84ee7b4ac.1362069310.git.trast@student.ethz.ch","subject":"Re: [PATCH v8 2/5] blame: introduce $ as \"end of file\" in -L syntax","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-02-28T17:18:40Z","receivedAt":"2013-02-28T17:18:40Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Thomas Rast <trast@student.ethz.ch> writes:\n\n> To save the user a lookup of the last line number, introduce $ as a\n> shorthand for the last line.  This is mostly useful to spell \"until\n> the end of the file\" as '-L<begin>,$'.\n\nDoesn't \"-L <begin>\" or \"-L <begin>,\" do that already?  If it were\nto introduce \"-L $-4,\" or \"-L$-4,+2\", I would understand why the\naddition may be useful, but otherwise I do not think it adds much\nvalue.\n\n>\n> Signed-off-by: Thomas Rast <trast@student.ethz.ch>\n> ---\n>  Documentation/line-range-format.txt | 6 ++++++\n>  line-log.c                          | 8 ++++++++\n>  2 files changed, 14 insertions(+)\n>\n> diff --git a/Documentation/line-range-format.txt b/Documentation/line-range-format.txt\n> index 265bc23..9ce0688 100644\n> --- a/Documentation/line-range-format.txt\n> +++ b/Documentation/line-range-format.txt\n> @@ -16,3 +16,9 @@ starting at the line given by <start>.\n>  This is only valid for <end> and will specify a number\n>  of lines before or after the line given by <start>.\n>  +\n> +\n> +- `$`\n> ++\n> +A literal dollar sign can be used as a shorthand for the last line in\n> +the file.\n> ++\n> diff --git a/line-log.c b/line-log.c\n> index a24a86b..b167b00 100644\n> --- a/line-log.c\n> +++ b/line-log.c\n> @@ -15,6 +15,14 @@ const char *parse_loc(const char *spec, nth_line_fn_t nth_line,\n>  \tregmatch_t match[1];\n>  \n>  \t/*\n> +\t * $ is a synonym for \"the end of the file\".\n> +\t */\n> +\tif (spec[0] == '$') {\n> +\t\t*ret = lines;\n> +\t\treturn spec + 1;\n> +\t}\n> +\n> +\t/*\n>  \t * Allow \"-L <something>,+20\" to mean starting at <something>\n>  \t * for 20 lines, or \"-L <something>,-5\" for 5 lines ending at\n>  \t * <something>.\n"},{"id":"210430","messageId":"7vfw0gicec.fsf@alter.siamese.dyndns.org","threadId":"33038","inReplyTo":"8269ddf785d10c05e39905041df317b685e85168.1362069310.git.trast@student.ethz.ch","subject":"Re: [PATCH v8 3/5] Export rewrite_parents() for 'log -L'","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-02-28T17:19:55Z","receivedAt":"2013-02-28T17:19:55Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Thomas Rast <trast@student.ethz.ch> writes:\n\n> From: Bo Yang <struggleyb.nku@gmail.com>\n>\n> The function rewrite_one is used to rewrite a single\n> parent of the current commit, and is used by rewrite_parents\n> to rewrite all the parents.\n>\n> Decouple the dependence between them by making rewrite_one\n> a callback function that is passed to rewrite_parents. Then\n> export rewrite_parents for reuse by the line history browser.\n>\n> We will use this function in line.c.\n\nDid you mean log-line.c or line-log.c or something?\n\n> Signed-off-by: Bo Yang <struggleyb.nku@gmail.com>\n> Signed-off-by: Thomas Rast <trast@student.ethz.ch>\n> ---\n>  revision.c | 13 ++++---------\n>  revision.h | 10 ++++++++++\n>  2 files changed, 14 insertions(+), 9 deletions(-)\n>\n> diff --git a/revision.c b/revision.c\n> index ef60205..46319d5 100644\n> --- a/revision.c\n> +++ b/revision.c\n> @@ -2173,12 +2173,6 @@ int prepare_revision_walk(struct rev_info *revs)\n>  \treturn 0;\n>  }\n>  \n> -enum rewrite_result {\n> -\trewrite_one_ok,\n> -\trewrite_one_noparents,\n> -\trewrite_one_error\n> -};\n> -\n>  static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)\n>  {\n>  \tstruct commit_list *cache = NULL;\n> @@ -2200,12 +2194,13 @@ static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp\n>  \t}\n>  }\n>  \n> -static int rewrite_parents(struct rev_info *revs, struct commit *commit)\n> +int rewrite_parents(struct rev_info *revs, struct commit *commit,\n> +\trewrite_parent_fn_t rewrite_parent)\n>  {\n>  \tstruct commit_list **pp = &commit->parents;\n>  \twhile (*pp) {\n>  \t\tstruct commit_list *parent = *pp;\n> -\t\tswitch (rewrite_one(revs, &parent->item)) {\n> +\t\tswitch (rewrite_parent(revs, &parent->item)) {\n>  \t\tcase rewrite_one_ok:\n>  \t\t\tbreak;\n>  \t\tcase rewrite_one_noparents:\n> @@ -2371,7 +2366,7 @@ enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)\n>  \tif (action == commit_show &&\n>  \t    !revs->show_all &&\n>  \t    revs->prune && revs->dense && want_ancestry(revs)) {\n> -\t\tif (rewrite_parents(revs, commit) < 0)\n> +\t\tif (rewrite_parents(revs, commit, rewrite_one) < 0)\n>  \t\t\treturn commit_error;\n>  \t}\n>  \treturn action;\n> diff --git a/revision.h b/revision.h\n> index 5da09ee..640110d 100644\n> --- a/revision.h\n> +++ b/revision.h\n> @@ -241,4 +241,14 @@ enum commit_action {\n>  extern enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit);\n>  extern enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit);\n>  \n> +enum rewrite_result {\n> +\trewrite_one_ok,\n> +\trewrite_one_noparents,\n> +\trewrite_one_error\n> +};\n> +\n> +typedef enum rewrite_result (*rewrite_parent_fn_t)(struct rev_info *revs, struct commit **pp);\n> +\n> +extern int rewrite_parents(struct rev_info *revs, struct commit *commit,\n> +\trewrite_parent_fn_t rewrite_parent);\n>  #endif\n"},{"id":"210431","messageId":"7vbob4iaxh.fsf@alter.siamese.dyndns.org","threadId":"33038","inReplyTo":"9af548b2a7e4a4da9eb30e99b0223f20788b4fc1.1362069310.git.trast@student.ethz.ch","subject":"Re: [PATCH v8 4/5] Implement line-history search (git log -L)","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-02-28T17:51:38Z","receivedAt":"2013-02-28T17:51:38Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Thomas Rast <trast@student.ethz.ch> writes:\n\n> @@ -138,6 +155,11 @@ Examples\n>  \tThis makes sense only when following a strict policy of merging all\n>  \ttopic branches when staying on a single integration branch.\n>  \n> +git log -L '/int main/',/^}/:main.c::\n> +\n> +\tShows how the function `main()` in the file 'main.c' evolved\n> +\tover time.\n\nPerhaps have \"^\" before that \"int\"?\n\n> diff --git a/line-log.c b/line-log.c\n> index b167b00..789b7c4 100644\n> --- a/line-log.c\n> +++ b/line-log.c\n> @@ -1,6 +1,442 @@\n>  #include \"git-compat-util.h\"\n> +#include \"cache.h\"\n> +#include \"tag.h\"\n> +#include \"blob.h\"\n> +#include \"tree.h\"\n> +#include \"diff.h\"\n> +#include \"commit.h\"\n> +#include \"decorate.h\"\n> +#include \"revision.h\"\n> +#include \"xdiff-interface.h\"\n> +#include \"strbuf.h\"\n> +#include \"log-tree.h\"\n> +#include \"graph.h\"\n>  #include \"line-log.h\"\n>  \n> +void range_set_grow (struct range_set *rs, size_t extra)\n\nLose the extra SP (everywhere in this file).\n\n> +{\n> +\tALLOC_GROW(rs->ranges, rs->nr + extra, rs->alloc);\n> +}\n> +\n> +/* Either initialization would be fine */\n> +#define RANGE_SET_INIT {0}\n> +\n> +void range_set_init (struct range_set *rs, size_t prealloc)\n> +{\n> +\trs->alloc = rs->nr = 0;\n> +\trs->ranges = NULL;\n> +\tif (prealloc)\n> +\t\trange_set_grow(rs, prealloc);\n> +}\n> +\n> +void range_set_release (struct range_set *rs)\n> +{\n> +\tfree(rs->ranges);\n> +\trs->alloc = rs->nr = 0;\n> +\trs->ranges = NULL;\n> +}\n> +\n> +/* dst must be uninitialized! */\n> +void range_set_copy (struct range_set *dst, struct range_set *src)\n> +{\n> +\trange_set_init(dst, src->nr);\n> +\tmemcpy(dst->ranges, src->ranges, src->nr*sizeof(struct range_set));\n> +\tdst->nr = src->nr;\n> +}\n> +void range_set_move (struct range_set *dst, struct range_set *src)\n> +{\n> +\trange_set_release(dst);\n> +\tdst->ranges = src->ranges;\n> +\tdst->nr = src->nr;\n> +\tdst->alloc = src->alloc;\n> +\tsrc->ranges = NULL;\n> +\tsrc->alloc = src->nr = 0;\n> +}\n> +\n> +/* tack on a _new_ range _at the end_ */\n> +void range_set_append (struct range_set *rs, long a, long b)\n> +{\n> +\tassert(a <= b);\n> +\tassert(rs->nr == 0 || rs->ranges[rs->nr-1].end <= a);\n> +\trange_set_grow(rs, 1);\n> +\trs->ranges[rs->nr].start = a;\n> +\trs->ranges[rs->nr].end = b;\n> +\trs->nr++;\n> +}\n> +\n> +static int range_cmp (const void *_r, const void *_s)\n> +{\n> +\tconst struct range *r = _r;\n> +\tconst struct range *s = _s;\n> +\n> +\t/* this could be simply 'return r.start-s.start', but for the types */\n> +\tif (r->start == s->start)\n> +\t\treturn 0;\n> +\tif (r->start < s->start)\n> +\t\treturn -1;\n> +\treturn 1;\n> +}\n> +\n> +/*\n> + * Helper: In-place pass of sorting and merging the ranges in the\n> + * range set, to re-establish the invariants after another operation\n> + *\n> + * NEEDSWORK currently not needed\n> + */\n> +static void sort_and_merge_range_set (struct range_set *rs)\n> +{\n> +\tint i;\n> +\tint o = 1; /* output cursor */\n> +\n> +\tqsort(rs->ranges, rs->nr, sizeof(struct range), range_cmp);\n> +\n> +\tfor (i = 1; i < rs->nr; i++) {\n> +\t\tif (rs->ranges[i].start <= rs->ranges[o-1].end) {\n> +\t\t\trs->ranges[o-1].end = rs->ranges[i].end;\n> +\t\t} else {\n> +\t\t\trs->ranges[o].start = rs->ranges[i].start;\n> +\t\t\trs->ranges[o].end = rs->ranges[i].end;\n> +\t\t\to++;\n> +\t\t}\n> +\t}\n> +\tassert(o <= rs->nr);\n> +\trs->nr = o;\n> +}\n> +\n> +/*\n> + * Union of range sets (i.e., sets of line numbers).  Used to merge\n> + * them when searches meet at a common ancestor.\n> + *\n> + * This is also where the ranges are consolidated into canonical form:\n> + * overlapping and adjacent ranges are merged, and empty ranges are\n> + * removed.\n> + */\n> +static void range_set_union (struct range_set *out,\n> +\t\t\t     struct range_set *a, struct range_set *b)\n> +{\n> +\tint i = 0, j = 0, o = 0;\n> +\tstruct range *ra = a->ranges;\n> +\tstruct range *rb = b->ranges;\n> +\t/* cannot make an alias of out->ranges: it may change during grow */\n> +\n> +\tassert(out->nr == 0);\n> +\twhile (i < a->nr || j < b->nr) {\n> +\t\tstruct range *new;\n> +\t\tif (i < a->nr && j < b->nr) {\n> +\t\t\tif (ra[i].start < rb[j].start)\n> +\t\t\t\tnew = &ra[i++];\n> +\t\t\telse if (ra[i].start > rb[j].start)\n> +\t\t\t\tnew = &rb[j++];\n> +\t\t\telse if (ra[i].end < rb[j].end)\n> +\t\t\t\tnew = &ra[i++];\n> +\t\t\telse\n> +\t\t\t\tnew = &rb[j++];\n> +\t\t} else if (i < a->nr)      /* b exhausted */\n> +\t\t\tnew = &ra[i++];\n> +\t\telse                       /* a exhausted */\n> +\t\t\tnew = &rb[j++];\n> +\t\tif (new->start == new->end)\n> +\t\t\t; /* empty range */\n> +\t\telse if (!o || out->ranges[o-1].end < new->start) {\n> +\t\t\trange_set_grow(out, 1);\n> +\t\t\tout->ranges[o].start = new->start;\n> +\t\t\tout->ranges[o].end = new->end;\n> +\t\t\to++;\n> +\t\t} else if (out->ranges[o-1].end < new->end) {\n> +\t\t\tout->ranges[o-1].end = new->end;\n> +\t\t}\n> +\t}\n> +\tout->nr = o;\n> +}\n> +\n> +/*\n> + * Difference of range sets (out = a \\ b).  Pass the \"interesting\"\n> + * ranges as 'a' and the target side of the diff as 'b': it removes\n> + * the ranges for which the commit is responsible.\n> + */\n> +static void range_set_difference (struct range_set *out,\n> +\t\t\t\t  struct range_set *a, struct range_set *b)\n> +{\n> +\tint i, j =  0;\n> +\tfor (i = 0; i < a->nr; i++) {\n> +\t\tlong start = a->ranges[i].start;\n> +\t\tlong end = a->ranges[i].end;\n> +\t\twhile (start < end) {\n> +\t\t\twhile (j < b->nr && start >= b->ranges[j].end)\n> +\t\t\t\t/*\n> +\t\t\t\t * a:         |-------\n> +\t\t\t\t * b: ------|\n> +\t\t\t\t */\n> +\t\t\t\tj++;\n> +\t\t\tif (j >= b->nr || end < b->ranges[j].start) {\n> +\t\t\t\t/*\n> +\t\t\t\t * b exhausted, or\n> +\t\t\t\t * a:  ----|\n> +\t\t\t\t * b:         |----\n> +\t\t\t\t */\n> +\t\t\t\trange_set_append(out, start, end);\n> +\t\t\t\tbreak;\n> +\t\t\t}\n> +\t\t\tif (start >= b->ranges[j].start) {\n> +\t\t\t\t/*\n> +\t\t\t\t * a:     |--????\n> +\t\t\t\t * b: |------|\n> +\t\t\t\t */\n> +\t\t\t\tstart = b->ranges[j].end;\n> +\t\t\t} else if (end > b->ranges[j].start) {\n> +\t\t\t\t/*\n> +\t\t\t\t * a: |-----|\n> +\t\t\t\t * b:    |--?????\n> +\t\t\t\t */\n> +\t\t\t\tif (start < b->ranges[j].start)\n> +\t\t\t\t\trange_set_append(out, start, b->ranges[j].start);\n> +\t\t\t\tstart = b->ranges[j].end;\n> +\t\t\t}\n> +\t\t}\n> +\t}\n> +}\n> +\n> +static void diff_ranges_init (struct diff_ranges *diff)\n> +{\n> +\trange_set_init(&diff->parent, 0);\n> +\trange_set_init(&diff->target, 0);\n> +}\n> +\n> +static void diff_ranges_release (struct diff_ranges *diff)\n> +{\n> +\trange_set_release(&diff->parent);\n> +\trange_set_release(&diff->target);\n> +}\n> +\n> +void line_log_data_init(struct line_log_data *r)\n> +{\n> +\tmemset(r, 0, sizeof(struct line_log_data));\n> +\trange_set_init(&r->ranges, 0);\n> +}\n> +\n> +static void line_log_data_clear(struct line_log_data *r)\n> +{\n> +\trange_set_release(&r->ranges);\n> +\tif (r->pair)\n> +\t\tdiff_free_filepair(r->pair);\n> +}\n> +\n> +static void free_line_log_data(struct line_log_data *r)\n> +{\n> +\twhile (r) {\n> +\t\tstruct line_log_data *next = r->next;\n> +\t\tline_log_data_clear(r);\n> +\t\tfree(r);\n> +\t\tr = next;\n> +\t}\n> +}\n> +\n> +static struct line_log_data *\n> +search_line_log_data(struct line_log_data *list, const char *path,\n> +\t\t     struct line_log_data **insertion_point)\n> +{\n> +\tstruct line_log_data *p = list;\n> +\tif (insertion_point)\n> +\t\t*insertion_point = NULL;\n> +\twhile (p) {\n> +\t\tint cmp = strcmp(p->spec->path, path);\n> +\t\tif (!cmp)\n> +\t\t\treturn p;\n> +\t\tif (insertion_point && cmp < 0)\n> +\t\t\t*insertion_point = p;\n> +\t\tp = p->next;\n> +\t}\n> +\treturn NULL;\n> +}\n> +\n> +static void line_log_data_insert (struct line_log_data **list,\n> +\t\t\t\t  struct diff_filespec *spec,\n> +\t\t\t\t  long begin, long end)\n> +{\n> +\tstruct line_log_data *ip;\n> +\tstruct line_log_data *p = search_line_log_data(*list, spec->path, &ip);\n> +\n> +\tif (p) {\n> +\t\trange_set_append(&p->ranges, begin, end);\n> +\t\tsort_and_merge_range_set(&p->ranges);\n> +\t\tfree_filespec(spec);\n> +\t\treturn;\n> +\t}\n> +\n> +\tp = xcalloc(1, sizeof(struct line_log_data));\n> +\tp->spec = spec;\n> +\trange_set_append(&p->ranges, begin, end);\n> +\tif (ip) {\n> +\t\tp->next = ip->next;\n> +\t\tip->next = p;\n> +\t} else {\n> +\t\tp->next = *list;\n> +\t\t*list = p;\n> +\t}\n> +}\n> +\n> +struct collect_diff_cbdata {\n> +\tstruct diff_ranges *diff;\n> +};\n> +\n> +static int collect_diff_cb (long start_a, long count_a,\n> +\t\t\t    long start_b, long count_b,\n> +\t\t\t    void *data)\n> +{\n> +\tstruct collect_diff_cbdata *d = data;\n> +\n> +\tif (count_a >= 0)\n> +\t\trange_set_append(&d->diff->parent, start_a, start_a + count_a);\n> +\tif (count_b >= 0)\n> +\t\trange_set_append(&d->diff->target, start_b, start_b + count_b);\n> +\n> +\treturn 0;\n> +}\n> +\n> +static void collect_diff (mmfile_t *parent, mmfile_t *target, struct diff_ranges *out)\n> +{\n> +\tstruct collect_diff_cbdata cbdata = {0};\n> +\txpparam_t xpp;\n> +\txdemitconf_t xecfg;\n> +\txdemitcb_t ecb;\n> +\n> +\tmemset(&xpp, 0, sizeof(xpp));\n> +\tmemset(&xecfg, 0, sizeof(xecfg));\n> +\txecfg.ctxlen = xecfg.interhunkctxlen = 0;\n> +\n> +\tcbdata.diff = out;\n> +\txecfg.hunk_func = collect_diff_cb;\n> +\tmemset(&ecb, 0, sizeof(ecb));\n> +\tecb.priv = &cbdata;\n> +\txdi_diff(parent, target, &xpp, &xecfg, &ecb);\n> +}\n> +\n> +/*\n> + * These are handy for debugging.  Removing them with #if 0 silences\n> + * the \"unused function\" warning.\n> + */\n> +#if 0\n> +static void dump_range_set (struct range_set *rs, const char *desc)\n> +{\n> +\tint i;\n> +\tprintf(\"range set %s (%d items):\\n\", desc, rs->nr);\n> +\tfor (i = 0; i < rs->nr; i++)\n> +\t\tprintf(\"\\t[%ld,%ld]\\n\", rs->ranges[i].start, rs->ranges[i].end);\n> +}\n> +\n> +static void dump_line_log_data (struct line_log_data *r)\n> +{\n> +\tchar buf[4096];\n> +\twhile (r) {\n> +\t\tsnprintf(buf, 4096, \"file %s\\n\", r->spec->path);\n> +\t\tdump_range_set(&r->ranges, buf);\n> +\t\tr = r->next;\n> +\t}\n> +}\n> +\n> +static void dump_diff_ranges (struct diff_ranges *diff, const char *desc)\n> +{\n> +\tint i;\n> +\tassert(diff->parent.nr == diff->target.nr);\n> +\tprintf(\"diff ranges %s (%d items):\\n\", desc, diff->parent.nr);\n> +\tprintf(\"\\tparent\\ttarget\\n\");\n> +\tfor (i = 0; i < diff->parent.nr; i++) {\n> +\t\tprintf(\"\\t[%ld,%ld]\\t[%ld,%ld]\\n\",\n> +\t\t       diff->parent.ranges[i].start,\n> +\t\t       diff->parent.ranges[i].end,\n> +\t\t       diff->target.ranges[i].start,\n> +\t\t       diff->target.ranges[i].end);\n> +\t}\n> +}\n> +#endif\n> +\n> +\n> +static int ranges_overlap (struct range *a, struct range *b)\n> +{\n> +\treturn !(a->end <= b->start || b->end <= a->start);\n> +}\n> +\n> +/*\n> + * Given a diff and the set of interesting ranges, determine all hunks\n> + * of the diff which touch (overlap) at least one of the interesting\n> + * ranges in the target.\n> + */\n> +static void diff_ranges_filter_touched (struct diff_ranges *out,\n> +\t\t\t\t\tstruct diff_ranges *diff,\n> +\t\t\t\t\tstruct range_set *rs)\n> +{\n> +\tint i, j = 0;\n> +\n> +\tassert(out->target.nr == 0);\n> +\n> +\tfor (i = 0; i < diff->target.nr; i++) {\n> +\t\twhile (diff->target.ranges[i].start > rs->ranges[j].end) {\n> +\t\t\tj++;\n> +\t\t\tif (j == rs->nr)\n> +\t\t\t\treturn;\n> +\t\t}\n> +\t\tif (ranges_overlap(&diff->target.ranges[i], &rs->ranges[j])) {\n> +\t\t\trange_set_append(&out->parent,\n> +\t\t\t\t\t diff->parent.ranges[i].start,\n> +\t\t\t\t\t diff->parent.ranges[i].end);\n> +\t\t\trange_set_append(&out->target,\n> +\t\t\t\t\t diff->target.ranges[i].start,\n> +\t\t\t\t\t diff->target.ranges[i].end);\n> +\t\t}\n> +\t}\n> +}\n> +\n> +/*\n> + * Adjust the line counts in 'rs' to account for the lines\n> + * added/removed in the diff.\n> + */\n> +static void range_set_shift_diff (struct range_set *out,\n> +\t\t\t\t  struct range_set *rs,\n> +\t\t\t\t  struct diff_ranges *diff)\n> +{\n> +\tint i, j = 0;\n> +\tlong offset = 0;\n> +\tstruct range *src = rs->ranges;\n> +\tstruct range *target = diff->target.ranges;\n> +\tstruct range *parent = diff->parent.ranges;\n> +\n> +\tfor (i = 0; i < rs->nr; i++) {\n> +\t\twhile (j < diff->target.nr && src[i].start >= target[j].start) {\n> +\t\t\toffset += (parent[j].end-parent[j].start)\n> +\t\t\t\t- (target[j].end-target[j].start);\n> +\t\t\tj++;\n> +\t\t}\n> +\t\trange_set_append(out, src[i].start+offset, src[i].end+offset);\n> +\t}\n> +}\n> +\n> +/*\n> + * Given a diff and the set of interesting ranges, map the ranges\n> + * across the diff.  That is: observe that the target commit takes\n> + * blame for all the + (target-side) ranges.  So for every pair of\n> + * ranges in the diff that was touched, we remove the latter and add\n> + * its parent side.\n> + */\n> +static void range_set_map_across_diff (struct range_set *out,\n> +\t\t\t\t       struct range_set *rs,\n> +\t\t\t\t       struct diff_ranges *diff,\n> +\t\t\t\t       struct diff_ranges **touched_out)\n> +{\n> +\tstruct diff_ranges *touched = xmalloc(sizeof(*touched));\n> +\tstruct range_set tmp1 = RANGE_SET_INIT;\n> +\tstruct range_set tmp2 = RANGE_SET_INIT;\n> +\n> +\tdiff_ranges_init(touched);\n> +\tdiff_ranges_filter_touched(touched, diff, rs);\n> +\trange_set_difference(&tmp1, rs, &touched->target);\n> +\trange_set_shift_diff(&tmp2, &tmp1, diff);\n> +\trange_set_union(out, &tmp2, &touched->parent);\n> +\trange_set_release(&tmp1);\n> +\trange_set_release(&tmp2);\n> +\n> +\t*touched_out = touched;\n> +}\n> +\n>  /*\n>   * Parse one item in the -L option\n>   */\n> @@ -18,7 +454,8 @@ const char *parse_loc(const char *spec, nth_line_fn_t nth_line,\n>  \t * $ is a synonym for \"the end of the file\".\n>  \t */\n>  \tif (spec[0] == '$') {\n> -\t\t*ret = lines;\n> +\t\tif (ret)\n> +\t\t\t*ret = lines;\n>  \t\treturn spec + 1;\n>  \t}\n\nThis should have been part of the earlier patches, no?\n\n> @@ -30,6 +467,8 @@ const char *parse_loc(const char *spec, nth_line_fn_t nth_line,\n>  \tif (begin != -1 && (spec[0] == '+' || spec[0] == '-')) {\n>  \t\tnum = strtol(spec + 1, &term, 10);\n>  \t\tif (term != spec + 1) {\n> +\t\t\tif (!ret)\n> +\t\t\t\treturn term;\n>  \t\t\tif (spec[0] == '-')\n>  \t\t\t\tnum = 0 - num;\n>  \t\t\tif (0 < num)\n> @@ -44,7 +483,8 @@ const char *parse_loc(const char *spec, nth_line_fn_t nth_line,\n>  \t}\n>  \tnum = strtol(spec, &term, 10);\n>  \tif (term != spec) {\n> -\t\t*ret = num;\n> +\t\tif (ret)\n> +\t\t\t*ret = num;\n>  \t\treturn term;\n>  \t}\n>  \tif (spec[0] != '/')\n> @@ -58,6 +498,10 @@ const char *parse_loc(const char *spec, nth_line_fn_t nth_line,\n>  \tif (*term != '/')\n>  \t\treturn spec;\n>  \n> +\t/* in the scan-only case we are not interested in the regex */\n> +\tif (!ret)\n> +\t\treturn term+1;\n> +\n>  \t/* try [spec+1 .. term-1] as regexp */\n>  \t*term = 0;\n>  \tif (begin == -1)\n> @@ -101,13 +545,763 @@ int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,\n>  \t\t}\n>  \t}\n>  \n> -\tif (*begin <= 0)\n> -\t\t*begin = 1;\n> -\tif (*end > lines)\n> -\t\t*end = lines;\n> -\n>  \tif (*arg)\n>  \t\treturn -1;\n>  \n>  \treturn 0;\n>  }\n> +\n> +const char *skip_range_arg(const char *arg)\n> +{\n> +\targ = parse_loc(arg, NULL, NULL, 0, -1, 0);\n> +\n> +\tif (*arg == ',')\n> +\t\targ = parse_loc(arg+1, NULL, NULL, 0, 0, 0);\n> +\n> +\treturn arg;\n> +}\n> +\n> +static struct commit *check_single_commit(struct rev_info *revs)\n> +{\n> +\tstruct object *commit = NULL;\n> +\tint found = -1;\n> +\tint i;\n> +\n> +\tfor (i = 0; i < revs->pending.nr; i++) {\n> +\t\tstruct object *obj = revs->pending.objects[i].item;\n> +\t\tif (obj->flags & UNINTERESTING)\n> +\t\t\tcontinue;\n> +\t\twhile (obj->type == OBJ_TAG)\n> +\t\t\tobj = deref_tag(obj, NULL, 0);\n> +\t\tif (obj->type != OBJ_COMMIT)\n> +\t\t\tdie(\"Non commit %s?\", revs->pending.objects[i].name);\n> +\t\tif (commit)\n> +\t\t\tdie(\"More than one commit to dig from: %s and %s?\",\n> +\t\t\t    revs->pending.objects[i].name,\n> +\t\t\t    revs->pending.objects[found].name);\n> +\t\tcommit = obj;\n> +\t\tfound = i;\n> +\t}\n> +\n> +\tif (!commit)\n> +\t\tdie(\"No commit specified?\");\n> +\n> +\treturn (struct commit *) commit;\n> +}\n> +\n> +static void fill_blob_sha1(struct commit *commit, struct diff_filespec *spec)\n> +{\n> +\tunsigned mode;\n> +\tunsigned char sha1[20];\n> +\n> +\tif (get_tree_entry(commit->object.sha1, spec->path,\n> +\t\t\t   sha1, &mode))\n> +\t\tdie(\"There is no path %s in the commit\", spec->path);\n> +\tfill_filespec(spec, sha1, 1, mode);\n> +\n> +\treturn;\n> +}\n> +\n> +static void fill_line_ends(struct diff_filespec *spec, long *lines,\n> +\tunsigned long **line_ends)\n> +{\n> +\tint num = 0, size = 50;\n> +\tlong cur = 0;\n> +\tunsigned long *ends = NULL;\n> +\tchar *data = NULL;\n> +\n> +\tif (diff_populate_filespec(spec, 0))\n> +\t\tdie(\"Cannot read blob %s\", sha1_to_hex(spec->sha1));\n> +\n> +\tends = xmalloc(size * sizeof(*ends));\n> +\tends[cur++] = 0;\n> +\tdata = spec->data;\n> +\twhile (num < spec->size) {\n> +\t\tif (data[num] == '\\n' || num == spec->size - 1) {\n> +\t\t\tALLOC_GROW(ends, (cur + 1), size);\n> +\t\t\tends[cur++] = num;\n> +\t\t}\n> +\t\tnum++;\n> +\t}\n> +\n> +\t/* shrink the array to fit the elements */\n> +\tends = xrealloc(ends, cur * sizeof(*ends));\n> +\t*lines = cur-1;\n> +\t*line_ends = ends;\n> +}\n> +\n> +struct nth_line_cb {\n> +\tstruct diff_filespec *spec;\n> +\tlong lines;\n> +\tunsigned long *line_ends;\n> +};\n> +\n> +static const char *nth_line(void *data, long line)\n> +{\n> +\tstruct nth_line_cb *d = data;\n> +\tassert(d && line <= d->lines);\n> +\tassert(d->spec && d->spec->data);\n> +\n> +\tif (line == 0)\n> +\t\treturn (char *)d->spec->data;\n> +\telse\n> +\t\treturn (char *)d->spec->data + d->line_ends[line] + 1;\n> +}\n> +\n> +static struct line_log_data *\n> +parse_lines(struct commit *commit, const char *prefix, struct string_list *args)\n> +{\n> +\tlong lines = 0;\n> +\tunsigned long *ends = NULL;\n> +\tstruct nth_line_cb cb_data;\n> +\tstruct string_list_item *item;\n> +\tstruct line_log_data *ranges = NULL;\n> +\n> +\tfor_each_string_list_item(item, args) {\n> +\t\tconst char *name_part, *range_part;\n> +\t\tconst char *full_name;\n> +\t\tstruct diff_filespec *spec;\n> +\t\tlong begin = 0, end = 0;\n> +\n> +\t\tname_part = skip_range_arg(item->string);\n> +\t\tif (!name_part || *name_part != ':')\n> +\t\t\tdie(\"-L argument '%s' not of the form start,end:file\",\n> +\t\t\t    item->string);\n> +\t\trange_part = xstrndup(item->string, name_part - item->string);\n> +\t\tname_part++;\n> +\n> +\t\tfull_name = prefix_path(prefix, prefix ? strlen(prefix) : 0,\n> +\t\t\t\t\tname_part);\n> +\n> +\t\tspec = alloc_filespec(full_name);\n> +\t\tfill_blob_sha1(commit, spec);\n> +\t\tfill_line_ends(spec, &lines, &ends);\n> +\t\tcb_data.spec = spec;\n> +\t\tcb_data.lines = lines;\n> +\t\tcb_data.line_ends = ends;\n> +\n> +\t\tif (parse_range_arg(range_part, nth_line, &cb_data,\n> +\t\t\t\t    lines, &begin, &end))\n> +\t\t\tdie(\"malformed -L argument '%s'\", range_part);\n> +\t\tif (begin < 1)\n> +\t\t\tbegin = 1;\n> +\t\tif (end < 1)\n> +\t\t\tend = lines;\n> +\t\tbegin--;\n> +\t\tif (lines < end || lines < begin)\n> +\t\t\tdie(\"file %s has only %ld lines\", name_part, lines);\n> +\t\tline_log_data_insert(&ranges, spec, begin, end);\n> +\n> +\t\tfree(ends);\n> +\t\tends = NULL;\n> +\t}\n> +\n> +\treturn ranges;\n> +}\n> +\n> +static struct line_log_data *line_log_data_copy_one(struct line_log_data *r)\n> +{\n> +\tstruct line_log_data *ret = xmalloc(sizeof(*ret));\n> +\n> +\tassert(r);\n> +\tline_log_data_init(ret);\n> +\trange_set_copy(&ret->ranges, &r->ranges);\n> +\n> +\tret->spec = r->spec;\n> +\tassert(ret->spec);\n> +\tret->spec->count++;\n> +\n> +\treturn ret;\n> +}\n> +\n> +static struct line_log_data *\n> +line_log_data_copy(struct line_log_data *r)\n> +{\n> +\tstruct line_log_data *ret = NULL;\n> +\tstruct line_log_data *tmp = NULL, *prev = NULL;\n> +\n> +\tassert(r);\n> +\tret = tmp = prev = line_log_data_copy_one(r);\n> +\tr = r->next;\n> +\twhile (r) {\n> +\t\ttmp = line_log_data_copy_one(r);\n> +\t\tprev->next = tmp;\n> +\t\tprev = tmp;\n> +\t\tr = r->next;\n> +\t}\n> +\n> +\treturn ret;\n> +}\n> +\n> +/* merge two range sets across files */\n> +static struct line_log_data *line_log_data_merge(struct line_log_data *a,\n> +\t\tstruct line_log_data *b)\n> +{\n> +\tstruct line_log_data *head = NULL, **pp = &head;\n> +\n> +\twhile (a || b) {\n> +\t\tstruct line_log_data *src;\n> +\t\tstruct line_log_data *src2 = NULL;\n> +\t\tstruct line_log_data *d;\n> +\t\tint cmp;\n> +\t\tif (!a)\n> +\t\t\tcmp = 1;\n> +\t\telse if (!b)\n> +\t\t\tcmp = -1;\n> +\t\telse\n> +\t\t\tcmp = strcmp(a->spec->path, b->spec->path);\n> +\t\tif (cmp < 0) {\n> +\t\t\tsrc = a;\n> +\t\t\ta = a->next;\n> +\t\t} else if (cmp == 0) {\n> +\t\t\tsrc = a;\n> +\t\t\ta = a->next;\n> +\t\t\tsrc2 = b;\n> +\t\t\tb = b->next;\n> +\t\t} else {\n> +\t\t\tsrc = b;\n> +\t\t\tb = b->next;\n> +\t\t}\n> +\t\td = xmalloc(sizeof(struct line_log_data));\n> +\t\tline_log_data_init(d);\n> +\t\td->spec = src->spec;\n> +\t\td->spec->count++;\n> +\t\t*pp = d;\n> +\t\tpp = &d->next;\n> +\t\tif (src2)\n> +\t\t\trange_set_union(&d->ranges, &src->ranges, &src2->ranges);\n> +\t\telse\n> +\t\t\trange_set_copy(&d->ranges, &src->ranges);\n> +\t}\n> +\n> +\treturn head;\n> +}\n> +\n> +static void add_line_range(struct rev_info *revs, struct commit *commit,\n> +\t\tstruct line_log_data *range)\n> +{\n> +\tstruct line_log_data *old = NULL;\n> +\tstruct line_log_data *new = NULL;\n> +\n> +\told = lookup_decoration(&revs->line_log_data, &commit->object);\n> +\tif (old && range) {\n> +\t\tnew = line_log_data_merge(old, range);\n> +\t\tfree_line_log_data(old);\n> +\t} else if (range)\n> +\t\tnew = line_log_data_copy(range);\n> +\n> +\tif (new)\n> +\t\tadd_decoration(&revs->line_log_data, &commit->object, new);\n> +}\n> +\n> +static void clear_commit_line_range(struct rev_info *revs, struct commit *commit)\n> +{\n> +\tstruct line_log_data *r;\n> +\tr = lookup_decoration(&revs->line_log_data, &commit->object);\n> +\tif (!r)\n> +\t\treturn;\n> +\tfree_line_log_data(r);\n> +\tadd_decoration(&revs->line_log_data, &commit->object, NULL);\n> +}\n> +\n> +static struct line_log_data *lookup_line_range(struct rev_info *revs,\n> +\t\tstruct commit *commit)\n> +{\n> +\tstruct line_log_data *ret = NULL;\n> +\n> +\tret = lookup_decoration(&revs->line_log_data, &commit->object);\n> +\treturn ret;\n> +}\n> +\n> +void line_log_init(struct rev_info *rev, const char *prefix, struct string_list *args)\n> +{\n> +\tstruct commit *commit = NULL;\n> +\tstruct line_log_data *range;\n> +\n> +\tcommit = check_single_commit(rev);\n> +\trange = parse_lines(commit, prefix, args);\n> +\tadd_line_range(rev, commit, range);\n> +\n> +\tif (!rev->diffopt.detect_rename) {\n> +\t\tint i, count = 0;\n> +\t\tstruct line_log_data *r = range;\n> +\t\tconst char **paths;\n> +\t\twhile (r) {\n> +\t\t\tcount++;\n> +\t\t\tr = r->next;\n> +\t\t}\n> +\t\tpaths = xmalloc((count+1)*sizeof(char *));\n> +\t\tr = range;\n> +\t\tfor (i = 0; i < count; i++) {\n> +\t\t\tpaths[i] = xstrdup(r->spec->path);\n> +\t\t\tr = r->next;\n> +\t\t}\n> +\t\tpaths[count] = NULL;\n> +\t\tinit_pathspec(&rev->diffopt.pathspec, paths);\n> +\t\tfree(paths);\n> +\t}\n> +}\n> +\n> +static void load_tree_desc(struct tree_desc *desc, void **tree,\n> +\t\tconst unsigned char *sha1)\n> +{\n> +\tunsigned long size;\n> +\t*tree = read_object_with_reference(sha1, tree_type, &size, NULL);\n> +\tif (!tree)\n> +\t\tdie(\"Unable to read tree (%s)\", sha1_to_hex(sha1));\n> +\tinit_tree_desc(desc, *tree, size);\n> +}\n> +\n> +static int count_parents(struct commit *commit)\n> +{\n> +\tstruct commit_list *parents = commit->parents;\n> +\tint count = 0;\n> +\twhile (parents) {\n> +\t\tcount++;\n> +\t\tparents = parents->next;\n> +\t}\n> +\treturn count;\n> +}\n> +\n> +static void move_diff_queue(struct diff_queue_struct *dst,\n> +\t\t\t    struct diff_queue_struct *src)\n> +{\n> +\tassert(src != dst);\n> +\tmemcpy(dst, src, sizeof(struct diff_queue_struct));\n> +\tDIFF_QUEUE_CLEAR(src);\n> +}\n> +\n> +static void queue_diffs(struct diff_options *opt,\n> +\t\t\tstruct diff_queue_struct *queue,\n> +\t\t\tstruct commit *commit, struct commit *parent)\n> +{\n> +\tvoid *tree1 = NULL, *tree2 = NULL;\n> +\tstruct tree_desc desc1, desc2;\n> +\n> +\tassert(commit);\n> +\tload_tree_desc(&desc2, &tree2, commit->tree->object.sha1);\n> +\tif (parent)\n> +\t\tload_tree_desc(&desc1, &tree1, parent->tree->object.sha1);\n> +\telse\n> +\t\tinit_tree_desc(&desc1, \"\", 0);\n> +\n> +\tDIFF_QUEUE_CLEAR(&diff_queued_diff);\n> +\tdiff_tree(&desc1, &desc2, \"\", opt);\n> +\tdiffcore_std(opt);\n> +\tmove_diff_queue(queue, &diff_queued_diff);\n> +\n> +\tif (tree1)\n> +\t\tfree(tree1);\n> +\tif (tree2)\n> +\t\tfree(tree2);\n> +}\n> +\n> +static char *get_nth_line(long line, unsigned long *ends, void *data)\n> +{\n> +\tif (line == 0)\n> +\t\treturn (char *)data;\n> +\telse\n> +\t\treturn (char *)data + ends[line] + 1;\n> +}\n> +\n> +static void print_line(const char *prefix, char first,\n> +\t\t       long line, unsigned long *ends, void *data,\n> +\t\t       const char *color, const char *reset)\n> +{\n> +\tchar *begin = get_nth_line(line, ends, data);\n> +\tchar *end = get_nth_line(line+1, ends, data);\n> +\tint had_nl = 0;\n> +\n> +\tif (end > begin && end[-1] == '\\n') {\n> +\t\tend--;\n> +\t\thad_nl = 1;\n> +\t}\n> +\n> +\tfputs(prefix, stdout);\n> +\tfputs(color, stdout);\n> +\tputchar(first);\n> +\tfwrite(begin, 1, end-begin, stdout);\n> +\tfputs(reset, stdout);\n> +\tputchar('\\n');\n> +\tif (!had_nl)\n> +\t\tfputs(\"\\\\ No newline at end of file\\n\", stdout);\n> +}\n> +\n> +static char *output_prefix(struct diff_options *opt)\n> +{\n> +\tchar *prefix = \"\";\n> +\n> +\tif (opt->output_prefix) {\n> +\t\tstruct strbuf *sb = opt->output_prefix(opt, opt->output_prefix_data);\n> +\t\tprefix = sb->buf;\n> +\t}\n> +\n> +\treturn prefix;\n> +}\n> +\n> +static void dump_diff_hacky_one(struct rev_info *rev, struct line_log_data *range)\n> +{\n> +...\n\nYuck.\n\n> +/*\n> + * NEEDSWORK: manually building a diff here is not the Right\n> + * Thing(tm).  log -L should be built into the diff pipeline.\n\nI am not sure about this design, and do not necessarily agree that\nwedging this to the diff pipeline is the right future direction.\n\nI have a feeling that \"log -L\" should actually be built around\n\"blame\".  You let blame to hit the first parent to take the blame,\nand then turn around to show a single \"diff-tree\" between the child\nand the parent with whatever other diff pipeline gizmo the user can\ngive you from the command line.  The blame also tells you what the\n\"interesting\" line range were at the first parent commit you found,\nso you can re-run the same thing with an updated range.\n\n> +/*\n> + * Unlike most other functions, this destructively operates on\n> + * 'range'.\n> + */\n> +static int process_diff_filepair(struct rev_info *rev,\n> +\t\t\t\t struct diff_filepair *pair,\n> +\t\t\t\t struct line_log_data *range,\n> +\t\t\t\t struct diff_ranges **diff_out)\n> +{\n> +\tstruct line_log_data *rg = range;\n> +\tstruct range_set tmp;\n> +\tstruct diff_ranges diff;\n> +\tmmfile_t file_parent, file_target;\n> +\n> +\tassert(pair->two->path);\n> +\twhile (rg) {\n> +\t\tassert(rg->spec->path);\n> +\t\tif (!strcmp(rg->spec->path, pair->two->path))\n> +\t\t\tbreak;\n> +\t\trg = rg->next;\n> +\t}\n> +\n> +\tif (!rg)\n> +\t\treturn 0;\n> +\tif (rg->ranges.nr == 0)\n> +\t\treturn 0;\n> +\n> +\tassert(pair->two->sha1_valid);\n> +\tdiff_populate_filespec(pair->two, 0);\n> +\tfile_target.ptr = pair->two->data;\n> +\tfile_target.size = pair->two->size;\n> +\n> +\tif (pair->one->sha1_valid) {\n> +\t\tdiff_populate_filespec(pair->one, 0);\n> +\t\tfile_parent.ptr = pair->one->data;\n> +\t\tfile_parent.size = pair->one->size;\n> +\t} else {\n> +\t\tfile_parent.ptr = \"\";\n> +\t\tfile_parent.size = 0;\n> +\t}\n> +\n> +\tdiff_ranges_init(&diff);\n> +\tcollect_diff(&file_parent, &file_target, &diff);\n> +\n> +\t/* NEEDSWORK should apply some heuristics to prevent mismatches */\n> +\trg->spec->path = xstrdup(pair->one->path);\n> +\n> +\trange_set_init(&tmp, 0);\n> +\trange_set_map_across_diff(&tmp, &rg->ranges, &diff, diff_out);\n> +\trange_set_release(&rg->ranges);\n> +\trange_set_move(&rg->ranges, &tmp);\n> +\n> +\tdiff_ranges_release(&diff);\n> +\n> +\treturn ((*diff_out)->parent.nr > 0);\n> +}\n> +\n> +static struct diff_filepair *diff_filepair_dup(struct diff_filepair *pair)\n> +{\n> +\tstruct diff_filepair *new = xmalloc(sizeof(struct diff_filepair));\n> +\tnew->one = pair->one;\n> +\tnew->two = pair->two;\n> +\tnew->one->count++;\n> +\tnew->two->count++;\n> +\treturn new;\n> +}\n> +\n> +static void free_diffqueues(int n, struct diff_queue_struct *dq)\n> +{\n> +\tint i, j;\n> +\tfor (i = 0; i < n; i++)\n> +\t\tfor (j = 0; j < dq[i].nr; j++)\n> +\t\t\tdiff_free_filepair(dq[i].queue[j]);\n> +\tfree(dq);\n> +}\n> +\n> +static int process_all_files(struct line_log_data **range_out,\n> +\t\t\t     struct rev_info *rev,\n> +\t\t\t     struct diff_queue_struct *queue,\n> +\t\t\t     struct line_log_data *range)\n> +{\n> +\tint i, changed = 0;\n> +\n> +\t*range_out = line_log_data_copy(range);\n> +\n> +\tfor (i = 0; i < queue->nr; i++) {\n> +\t\tstruct diff_ranges *pairdiff = NULL;\n> +\t\tif (process_diff_filepair(rev, queue->queue[i], *range_out, &pairdiff)) {\n> +\t\t\tstruct line_log_data *rg = range;\n> +\t\t\tchanged++;\n> +\t\t\t/* NEEDSWORK tramples over data structures not owned here */\n> +\t\t\twhile (rg && strcmp(rg->spec->path, queue->queue[i]->two->path))\n> +\t\t\t\trg = rg->next;\n> +\t\t\tassert(rg);\n> +\t\t\trg->pair = diff_filepair_dup(queue->queue[i]);\n> +\t\t\tmemcpy(&rg->diff, pairdiff, sizeof(struct diff_ranges));\n> +\t\t}\n> +\t}\n> +\n> +\treturn changed;\n> +}\n> +\n> +int line_log_print(struct rev_info *rev, struct commit *commit)\n> +{\n> +       struct line_log_data *range = lookup_line_range(rev, commit);\n> +\n> +       show_log(rev);\n> +       dump_diff_hacky(rev, range);\n> +       return 1;\n> +}\n> +\n> +static int process_ranges_ordinary_commit(struct rev_info *rev, struct commit *commit)\n> +{\n> +\tstruct commit *parent = NULL;\n> +\tstruct diff_queue_struct queue;\n> +\tstruct line_log_data *range = lookup_line_range(rev, commit);\n> +\tstruct line_log_data *parent_range;\n> +\tint changed;\n> +\n> +\tif (commit->parents)\n> +\t\tparent = commit->parents->item;\n> +\n> +\tqueue_diffs(&rev->diffopt, &queue, commit, parent);\n> +\tchanged = process_all_files(&parent_range, rev, &queue, range);\n> +\tif (parent)\n> +\t\tadd_line_range(rev, parent, parent_range);\n> +\treturn changed;\n> +}\n> +\n> +static int process_ranges_merge_commit(struct rev_info *rev, struct commit *commit)\n> +{\n> +\tstruct line_log_data *range = lookup_line_range(rev, commit);\n> +\tstruct diff_queue_struct *diffqueues;\n> +\tstruct line_log_data **cand;\n> +\tstruct commit **parents;\n> +\tstruct commit_list *p;\n> +\tint i;\n> +\tint nparents = count_parents(commit);\n> +\n> +\tdiffqueues = xmalloc(nparents * sizeof(*diffqueues));\n> +\tcand = xmalloc(nparents * sizeof(*cand));\n> +\tparents = xmalloc(nparents * sizeof(*parents));\n> +\n> +\tp = commit->parents;\n> +\tfor (i = 0; i < nparents; i++) {\n> +\t\tparents[i] = p->item;\n> +\t\tp = p->next;\n> +\t\tqueue_diffs(&rev->diffopt, &diffqueues[i], commit, parents[i]);\n> +\t}\n> +\n> +\tfor (i = 0; i < nparents; i++) {\n> +\t\tint changed;\n> +\t\tcand[i] = NULL;\n> +\t\tchanged = process_all_files(&cand[i], rev, &diffqueues[i], range);\n> +\t\tif (!changed) {\n> +\t\t\t/*\n> +\t\t\t * This parent can take all the blame, so we\n> +\t\t\t * don't follow any other path in history\n> +\t\t\t */\n> +\t\t\tadd_line_range(rev, parents[i], cand[i]);\n> +\t\t\tclear_commit_line_range(rev, commit);\n> +\t\t\tcommit->parents = xmalloc(sizeof(struct commit_list));\n> +\t\t\tcommit->parents->item = parents[i];\n> +\t\t\tcommit->parents->next = NULL;\n> +\t\t\tfree(parents);\n> +\t\t\tfree(cand);\n> +\t\t\tfree_diffqueues(nparents, diffqueues);\n> +\t\t\t/* NEEDSWORK leaking like a sieve */\n\nIndeed ;-)\n\n> +\t\t\treturn 0;\n> +\t\t}\n> +\t}\n> +\n> +\t/*\n> +\t * No single parent took the blame.  We add the candidates\n> +\t * from the above loop to the parents.\n> +\t */\n> +\tfor (i = 0; i < nparents; i++) {\n> +\t\tadd_line_range(rev, parents[i], cand[i]);\n> +\t}\n> +\n> +\tclear_commit_line_range(rev, commit);\n> +\tfree(parents);\n> +\tfree(cand);\n> +\tfree_diffqueues(nparents, diffqueues);\n> +\treturn 1;\n> +\n> +\t/* NEEDSWORK evil merge detection stuff */\n> +\t/* NEEDSWORK leaking like a sieve */\n> +}\n> +\n> +static int process_ranges_arbitrary_commit(struct rev_info *rev, struct commit *commit)\n> +{\n> +\tint changed = 0;\n> +\n> +\tif (lookup_line_range(rev, commit)) {\n> +\t\tif (!commit->parents || !commit->parents->next)\n> +\t\t\tchanged = process_ranges_ordinary_commit(rev, commit);\n> +\t\telse\n> +\t\t\tchanged = process_ranges_merge_commit(rev, commit);\n> +\t}\n\nThe code makes the decoration look-up for the same commit multiple\ntimes in a single codeflow; perhaps this entry point should keep the\nlook-up result and pass it down to the callchain?\n\nOverall, I like this better than the \"log --follow\" hack; as the\nrevision traversal is done without any pathspec when being \"careful\nand slow\" (aka -M), you do not suffer from the \"just use a singleton\npathspec globally regardless of what other history paths are being\ntraversed\" limitation of \"log --follow\".\n\nThe patch series certainly is interesting.\n"},{"id":"210435","messageId":"87lia86y2r.fsf@pctrast.inf.ethz.ch","threadId":"33038","inReplyTo":"7vobf4icjh.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH v8 1/5] Refactor parse_loc","fromName":"Thomas Rast","fromEmail":"trast@student.ethz.ch","sentAt":"2013-02-28T19:24:44Z","receivedAt":"2013-02-28T19:24:44Z","isPatch":true,"sender":{"key":"tr@thomasrast.ch","avatar":"https://avatars.githubusercontent.com/u/153510?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> Thomas Rast <trast@student.ethz.ch> writes:\n>\n>> From: Bo Yang <struggleyb.nku@gmail.com>\n>>\n>> We want to use the same style of -L n,m argument for 'git log -L' as\n>> for git-blame.  Refactor the argument parsing of the range arguments\n>> from builtin/blame.c to the (new) file that will hold the 'git log -L'\n>> logic.\n>>\n>> To accommodate different data structures in blame and log -L, the file\n>> contents are abstracted away; parse_range_arg takes a callback that it\n>> uses to get the contents of a line of the (notional) file.\n>>\n>> The new test is for a case that made me pause during debugging: the\n>> 'blame -L with invalid end' test was the only one that noticed an\n>> outright failure to parse the end *at all*.  So make a more explicit\n>> test for that.\n>>\n>> Signed-off-by: Bo Yang <struggleyb.nku@gmail.com>\n>> Signed-off-by: Thomas Rast <trast@student.ethz.ch>\n>> ---\n>>  Documentation/blame-options.txt     |  19 +------\n>>  Documentation/line-range-format.txt |  18 +++++++\n>>  Makefile                            |   2 +\n>>  builtin/blame.c                     |  99 +++-------------------------------\n>>  line-log.c                          | 105 ++++++++++++++++++++++++++++++++++++\n>>  line-log.h                          |  23 ++++++++\n>\n> Was this churn necessary?  \n>\n> It is strange to move existing functions that will be tweaked to be\n> shared by two different codepaths (blame and line-log) to the new\n> user.\n>\n> The only effect this has, as opposed to tweaking the functions in\n> place and making them extern, is to make it harder to see the tweaks\n> made while moving the lines, and also make it more cumbersome to\n> determine the lineage of the code later.\n>  \n> It would have been understandable if they were moved to a new\n> library-ish file (perhaps \"line-range.[ch]\"); even though that\n> approach shares the same downsides, at least it would have a better\n> excuse \"We will share this, so let's move it to a neutral third\n> place to allow us to hide the implementation details from both\n> users\".  The arrangement this patch series makes does not even have\n> that excuse.  The final implementation still stay with one of the\n> users; the only difference is that it is away from the original user\n> and close to the new user.\n\nEven though I am moving from builtin/blame.c to line-log.c?  I would\notherwise have to call from a rather lib-ish file into a \"frontend\"\nfile.  I always figured I wasn't supposed to do that.\n\n-- \nThomas Rast\ntrast@{inf,student}.ethz.ch\n"},{"id":"210436","messageId":"87fw0g6xp4.fsf@pctrast.inf.ethz.ch","threadId":"33038","inReplyTo":"7vbob4iaxh.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH v8 4/5] Implement line-history search (git log -L)","fromName":"Thomas Rast","fromEmail":"trast@student.ethz.ch","sentAt":"2013-02-28T19:32:55Z","receivedAt":"2013-02-28T19:32:55Z","isPatch":true,"sender":{"key":"tr@thomasrast.ch","avatar":"https://avatars.githubusercontent.com/u/153510?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n>> +/*\n>> + * NEEDSWORK: manually building a diff here is not the Right\n>> + * Thing(tm).  log -L should be built into the diff pipeline.\n>\n> I am not sure about this design, and do not necessarily agree that\n> wedging this to the diff pipeline is the right future direction.\n>\n> I have a feeling that \"log -L\" should actually be built around\n> \"blame\".  You let blame to hit the first parent to take the blame,\n> and then turn around to show a single \"diff-tree\" between the child\n> and the parent with whatever other diff pipeline gizmo the user can\n> give you from the command line.  The blame also tells you what the\n> \"interesting\" line range were at the first parent commit you found,\n> so you can re-run the same thing with an updated range.\n\nHrm, now that you mention it, this is actually a brilliant idea.\n\n-- \nThomas Rast\ntrast@{inf,student}.ethz.ch\n"},{"id":"210437","messageId":"7v7glsi55i.fsf@alter.siamese.dyndns.org","threadId":"33038","inReplyTo":"cover.1362069310.git.trast@student.ethz.ch","subject":"Re: [PATCH v8 0/5] git log -L","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-02-28T19:56:25Z","receivedAt":"2013-02-28T19:56:25Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Thomas Rast <trast@student.ethz.ch> writes:\n\n> For an instructive example, apply it and run\n>\n>   git log -L:archiver:archive.h\n>\n> in your git.git.\n\nFun ;-)\n\nThis is queued at the tip of 'pu' so that poeple can have fun.\n\nThanks.\n"},{"id":"210438","messageId":"7v38wgi38z.fsf@alter.siamese.dyndns.org","threadId":"33038","inReplyTo":"87fw0g6xp4.fsf@pctrast.inf.ethz.ch","subject":"Re: [PATCH v8 4/5] Implement line-history search (git log -L)","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-02-28T20:37:32Z","receivedAt":"2013-02-28T20:37:32Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Thomas Rast <trast@student.ethz.ch> writes:\n\n> Junio C Hamano <gitster@pobox.com> writes:\n>\n>>> +/*\n>>> + * NEEDSWORK: manually building a diff here is not the Right\n>>> + * Thing(tm).  log -L should be built into the diff pipeline.\n>>\n>> I am not sure about this design, and do not necessarily agree that\n>> wedging this to the diff pipeline is the right future direction.\n>>\n>> I have a feeling that \"log -L\" should actually be built around\n>> \"blame\".  You let blame to hit the first parent to take the blame,\n>> and then turn around to show a single \"diff-tree\" between the child\n>> and the parent with whatever other diff pipeline gizmo the user can\n>> give you from the command line.  The blame also tells you what the\n>> \"interesting\" line range were at the first parent commit you found,\n>> so you can re-run the same thing with an updated range.\n>\n> Hrm, now that you mention it, this is actually a brilliant idea.\n\nI don't know.  That is just me handwaving without giving a serious\nend-to-end thought.\n"},{"id":"210440","messageId":"87hakw3yma.fsf@pctrast.inf.ethz.ch","threadId":"33038","inReplyTo":"7v38wgi38z.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH v8 4/5] Implement line-history search (git log -L)","fromName":"Thomas Rast","fromEmail":"trast@student.ethz.ch","sentAt":"2013-02-28T21:41:17Z","receivedAt":"2013-02-28T21:41:17Z","isPatch":true,"sender":{"key":"tr@thomasrast.ch","avatar":"https://avatars.githubusercontent.com/u/153510?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> Thomas Rast <trast@student.ethz.ch> writes:\n>\n>> Junio C Hamano <gitster@pobox.com> writes:\n>>\n>>>> +/*\n>>>> + * NEEDSWORK: manually building a diff here is not the Right\n>>>> + * Thing(tm).  log -L should be built into the diff pipeline.\n>>>\n>>> I am not sure about this design, and do not necessarily agree that\n>>> wedging this to the diff pipeline is the right future direction.\n>>>\n>>> I have a feeling that \"log -L\" should actually be built around\n>>> \"blame\".  You let blame to hit the first parent to take the blame,\n>>> and then turn around to show a single \"diff-tree\" between the child\n>>> and the parent with whatever other diff pipeline gizmo the user can\n>>> give you from the command line.  The blame also tells you what the\n>>> \"interesting\" line range were at the first parent commit you found,\n>>> so you can re-run the same thing with an updated range.\n>>\n>> Hrm, now that you mention it, this is actually a brilliant idea.\n>\n> I don't know.  That is just me handwaving without giving a serious\n> end-to-end thought.\n\nHaving thought about it for some time, I think we need to figure out\nif/what can be shared.  I can't shake off the feeling that *something*\nshould be common between blame and log -L, but I can't exactly say what\nso far.\n\nYour suggestion of looking at the first blame hit is almost there.  (And\nin fact if it did work, it should be rather easy to prototype from blame\n--incremental.)  However, it works only for additions, not removals.\nLines that were removed do not show up in blame at all, and since a\npatch can also _only_ remove lines, blame would not find it even if we\nadjust the blamed range at every found commit.\n\nIt may be possible to fix that by doing a reverse blame, but I suspect\nthat runs into yet more trouble when trying to reverse-blame from two\ndifferent sides of history.\n\n\nThen there's a different issue if the order of code flips.  Suppose you\nhave\n\n  A1\n  A2\n  B1\n  B2\n\nand later change to\n\n  B1\n  B2\n  A1\n  A2\n\nDiffs can fundamentally not express this; they'll only see one side as\nunchanged, and the other as completely new.  Ideally we'd be able to\ntrack this case correctly -- as blame does? -- no matter which part is\nwithin our tracked range.\n\n\nAnyway, I believe this should be booked under \"future improvements\".\nI've had it in my own tree for ages and it already does the right thing\nmost of the time :-)\n\n-- \nThomas Rast\ntrast@{inf,student}.ethz.ch\n"},{"id":"210446","messageId":"7vmwuogjsm.fsf@alter.siamese.dyndns.org","threadId":"33038","inReplyTo":"7vbob4iaxh.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH v8 4/5] Implement line-history search (git log -L)","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-02-28T22:23:05Z","receivedAt":"2013-02-28T22:23:05Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> Overall, I like this better than the \"log --follow\" hack; as the\n> revision traversal is done without any pathspec when being \"careful\n> and slow\" (aka -M), you do not suffer from the \"just use a singleton\n> pathspec globally regardless of what other history paths are being\n> traversed\" limitation of \"log --follow\".\n>\n> The patch series certainly is interesting.\n\nHaving said that, I notice that \"careful and slow\" is just \"too slow\nto be usable\" even on a small tree like ours.  Try running\n\n    $ git log -M -L:get_name:builtin/describe.c\n\nand see how long you have to wait until you hit the first line of\noutput.\n\nIf some of the many NEEDSWORK in the code were fixed, this may\nbecome \"very slow but tolerable\", but in the current shape, I doubt\nit would be prudent to advance this to 'next' and further.\n"},{"id":"210450","messageId":"87vc9b1p48.fsf@pctrast.inf.ethz.ch","threadId":"33038","inReplyTo":"7vmwuogjsm.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH v8 4/5] Implement line-history search (git log -L)","fromName":"Thomas Rast","fromEmail":"trast@student.ethz.ch","sentAt":"2013-03-01T08:49:27Z","receivedAt":"2013-03-01T08:49:27Z","isPatch":true,"sender":{"key":"tr@thomasrast.ch","avatar":"https://avatars.githubusercontent.com/u/153510?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> Junio C Hamano <gitster@pobox.com> writes:\n>\n>> Overall, I like this better than the \"log --follow\" hack; as the\n>> revision traversal is done without any pathspec when being \"careful\n>> and slow\" (aka -M), you do not suffer from the \"just use a singleton\n>> pathspec globally regardless of what other history paths are being\n>> traversed\" limitation of \"log --follow\".\n>>\n>> The patch series certainly is interesting.\n>\n> Having said that, I notice that \"careful and slow\" is just \"too slow\n> to be usable\" even on a small tree like ours.  Try running\n>\n>     $ git log -M -L:get_name:builtin/describe.c\n>\n> and see how long you have to wait until you hit the first line of\n> output.\n\nI'll dig some more.  It *should* be essentially the following times\ntaken together:\n\n  $ time git log --raw -M --topo-order >/dev/null\n\n  real    0m5.448s\n  user    0m4.599s\n  sys     0m0.794s\n  $ time git log -L:get_name:builtin/describe.c >/dev/null\n\n  real    0m0.832s\n  user    0m0.796s\n  sys     0m0.032s\n\n  $ time git log -L:get_name:builtin-describe.c 81b50f3ce40^ >/dev/null\n\n  real    0m0.489s\n  user    0m0.465s\n  sys     0m0.022s\n\nSo I'm losing a factor of about 4 somewhere, which I can't explain right\nnow.\n\nIt could be improved further if --follow was fixed, because then the\nfirst step should be much faster than diffing *all* the trees.\n\n-- \nThomas Rast\ntrast@{inf,student}.ethz.ch\n"},{"id":"210461","messageId":"87ppzjxj1y.fsf@pctrast.inf.ethz.ch","threadId":"33038","inReplyTo":"87vc9b1p48.fsf@pctrast.inf.ethz.ch","subject":"Re: [PATCH v8 4/5] Implement line-history search (git log -L)","fromName":"Thomas Rast","fromEmail":"trast@student.ethz.ch","sentAt":"2013-03-01T14:59:21Z","receivedAt":"2013-03-01T14:59:21Z","isPatch":true,"sender":{"key":"tr@thomasrast.ch","avatar":"https://avatars.githubusercontent.com/u/153510?v=4"},"body":"Thomas Rast <trast@student.ethz.ch> writes:\n\n> Junio C Hamano <gitster@pobox.com> writes:\n>\n>> I notice that \"careful and slow\" is just \"too slow\n>> to be usable\" even on a small tree like ours.  Try running\n>>\n>>     $ git log -M -L:get_name:builtin/describe.c\n>>\n>> and see how long you have to wait until you hit the first line of\n>> output.\n>\n> I'll dig some more.  It *should* be essentially the following times\n> taken together:\n>\n>   $ time git log --raw -M --topo-order >/dev/null\n>   real    0m5.448s\n[...]\n>   $ time git log -L:get_name:builtin/describe.c >/dev/null\n>   real    0m0.832s\n[...]\n>   $ time git log -L:get_name:builtin-describe.c 81b50f3ce40^ >/dev/null\n>   real    0m0.489s\n[...]\n> So I'm losing a factor of about 4 somewhere, which I can't explain right\n> now.\n\nIt seems I still don't understand half of this code.\n\nlog -M --raw in the above somehow appears to use and optimize for\n-M100%, whereas the log -L code is currently written for general args.\n\nHowever, I couldn't pin down where this happens; I only know from call\ngraph profiling[1] that log -M --raw never goes through diffcore_std().\nAnd indeed according to the same sort of profiling, log -M -L spends\nmost of its time within diffcore_std() unpacking blobs to find renames.\n\nEven more confusingly, try_to_follow_renames() _does_ call into\ndiffcore_std, so there seems to be some merit to calling it after all.\n\nWith the hacky patch below,  I get something more reasonable:\n\n  $ time ./git-log -M -L:get_name:builtin/describe.c  >/dev/null\n\n  real    0m3.794s\n  user    0m3.734s\n  sys     0m0.045s\n\nThat's compared to about 35s on my machine without the patch.  It still\ncalls diffcore_std(), but before that it discards all diff pairs except\nthose affecting the path(s) we're interested in and any deletions (so\nthat they can be used as rename sources).  After diffcore_std() it\ndiscards all pairs that we're not interested in.\n\n[1]  valgrind --tool=callgrind --trace-children=yes\n\n-- >8 --\nSubject: [PATCH] WIP: speed up log -L... -M\n\n---\n line-log.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----\n 1 file changed, 54 insertions(+), 4 deletions(-)\n\ndiff --git a/line-log.c b/line-log.c\nindex a74bbaf..b03cc0b 100644\n--- a/line-log.c\n+++ b/line-log.c\n@@ -997,7 +997,52 @@ static void move_diff_queue(struct diff_queue_struct *dst,\n \tDIFF_QUEUE_CLEAR(src);\n }\n \n-static void queue_diffs(struct diff_options *opt,\n+static void filter_diffs_for_paths(struct line_log_data *range, int keep_deletions)\n+{\n+\tint i;\n+\tstruct diff_queue_struct outq;\n+\tDIFF_QUEUE_CLEAR(&outq);\n+\n+\t/* fprintf(stderr, \"-- filtering:\\n\"); */\n+\n+\tfor (i = 0; i < diff_queued_diff.nr; i++) {\n+\t\tstruct diff_filepair *p = diff_queued_diff.queue[i];\n+\t\tstruct line_log_data *rg = NULL;\n+\t\t/* fprintf(stderr, \"%-38s\\t%-38s\\n\", (p->one ? p->one->path : \"<none>\"), (p->two ? p->two->path : \"<none>\")); */\n+\t\tif (!DIFF_FILE_VALID(p->two)) {\n+\t\t\tif (keep_deletions)\n+\t\t\t\tdiff_q(&outq, p);\n+\t\t\telse\n+\t\t\t\tdiff_free_filepair(p);\n+\t\t\tcontinue;\n+\t\t}\n+\t\tfor (rg = range; rg; rg = rg->next) {\n+\t\t\tif (!strcmp(rg->spec->path, p->two->path))\n+\t\t\t\tbreak;\n+\t\t}\n+\t\tif (rg)\n+\t\t\tdiff_q(&outq, p);\n+\t\telse\n+\t\t\tdiff_free_filepair(p);\n+\t}\n+\tfree(diff_queued_diff.queue);\n+\tdiff_queued_diff = outq;\n+}\n+\n+static inline int diff_might_be_rename(void)\n+{\n+\tint i;\n+\tfor (i = 0; i < diff_queued_diff.nr; i++)\n+\t\tif (!DIFF_FILE_VALID(diff_queued_diff.queue[i]->one)) {\n+\t\t\t/* fprintf(stderr, \"diff_might_be_rename found creation of: %s\\n\", */\n+\t\t\t/* \tdiff_queued_diff.queue[i]->two->path); */\n+\t\t\treturn 1;\n+\t\t}\n+\treturn 0;\n+}\n+\n+static void queue_diffs(struct line_log_data *range,\n+\t\t\tstruct diff_options *opt,\n \t\t\tstruct diff_queue_struct *queue,\n \t\t\tstruct commit *commit, struct commit *parent)\n {\n@@ -1013,7 +1058,12 @@ static void queue_diffs(struct diff_options *opt,\n \n \tDIFF_QUEUE_CLEAR(&diff_queued_diff);\n \tdiff_tree(&desc1, &desc2, \"\", opt);\n-\tdiffcore_std(opt);\n+\tif (opt->detect_rename) {\n+\t\tfilter_diffs_for_paths(range, 1);\n+\t\tif (diff_might_be_rename())\n+\t\t\tdiffcore_std(opt);\n+\t\tfilter_diffs_for_paths(range, 0);\n+\t}\n \tmove_diff_queue(queue, &diff_queued_diff);\n \n \tif (tree1)\n@@ -1297,7 +1347,7 @@ static int process_ranges_ordinary_commit(struct rev_info *rev, struct commit *c\n \tif (commit->parents)\n \t\tparent = commit->parents->item;\n \n-\tqueue_diffs(&rev->diffopt, &queue, commit, parent);\n+\tqueue_diffs(range, &rev->diffopt, &queue, commit, parent);\n \tchanged = process_all_files(&parent_range, rev, &queue, range);\n \tif (parent)\n \t\tadd_line_range(rev, parent, parent_range);\n@@ -1322,7 +1372,7 @@ static int process_ranges_merge_commit(struct rev_info *rev, struct commit *comm\n \tfor (i = 0; i < nparents; i++) {\n \t\tparents[i] = p->item;\n \t\tp = p->next;\n-\t\tqueue_diffs(&rev->diffopt, &diffqueues[i], commit, parents[i]);\n+\t\tqueue_diffs(range, &rev->diffopt, &diffqueues[i], commit, parents[i]);\n \t}\n \n \tfor (i = 0; i < nparents; i++) {\n-- \n1.8.2.rc1.391.g6a988e5.dirty\n"},{"id":"211172","messageId":"7va9q85jt9.fsf@alter.siamese.dyndns.org","threadId":"33038","inReplyTo":"7vk3psicgf.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH v8 2/5] blame: introduce $ as \"end of file\" in -L syntax","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-03-12T22:34:26Z","receivedAt":"2013-03-12T22:34:26Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> Thomas Rast <trast@student.ethz.ch> writes:\n>\n>> To save the user a lookup of the last line number, introduce $ as a\n>> shorthand for the last line.  This is mostly useful to spell \"until\n>> the end of the file\" as '-L<begin>,$'.\n>\n> Doesn't \"-L <begin>\" or \"-L <begin>,\" do that already?  If it were\n> to introduce \"-L $-4,\" or \"-L$-4,+2\", I would understand why the\n> addition may be useful, but otherwise I do not think it adds much\n> value.\n\nIt is a quiet-period so there is no need to rush, but did anything\nhappened further on this series?\n"},{"id":"211199","messageId":"87wqtbivms.fsf@pctrast.inf.ethz.ch","threadId":"33038","inReplyTo":"7va9q85jt9.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH v8 2/5] blame: introduce $ as \"end of file\" in -L syntax","fromName":"Thomas Rast","fromEmail":"trast@inf.ethz.ch","sentAt":"2013-03-13T07:52:59Z","receivedAt":"2013-03-13T07:52:59Z","isPatch":true,"sender":{"key":"tr@thomasrast.ch","avatar":"https://avatars.githubusercontent.com/u/153510?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> Junio C Hamano <gitster@pobox.com> writes:\n>\n>> Thomas Rast <trast@student.ethz.ch> writes:\n>>\n>>> To save the user a lookup of the last line number, introduce $ as a\n>>> shorthand for the last line.  This is mostly useful to spell \"until\n>>> the end of the file\" as '-L<begin>,$'.\n>>\n>> Doesn't \"-L <begin>\" or \"-L <begin>,\" do that already?  If it were\n>> to introduce \"-L $-4,\" or \"-L$-4,+2\", I would understand why the\n>> addition may be useful, but otherwise I do not think it adds much\n>> value.\n>\n> It is a quiet-period so there is no need to rush, but did anything\n> happened further on this series?\n\nNo, I've been busy :-(\n\nThe only open point is in the other email:\n\n> >>  Documentation/blame-options.txt     |  19 +------\n> >>  Documentation/line-range-format.txt |  18 +++++++\n> >>  Makefile                            |   2 +\n> >>  builtin/blame.c                     |  99 +++-------------------------------\n> >>  line-log.c                          | 105 ++++++++++++++++++++++++++++++++++++\n> >>  line-log.h                          |  23 ++++++++\n> >\n> > Was this churn necessary?  \n> >\n> > It is strange to move existing functions that will be tweaked to be\n> > shared by two different codepaths (blame and line-log) to the new\n> > user.\n> [...]\n> \n> Even though I am moving from builtin/blame.c to line-log.c?  I would\n> otherwise have to call from a rather lib-ish file into a \"front\n\nYou haven't sent any reply to this.  Does that mean you agree?  Would\nyou prefer the shared file to be named something like line-range.c?\n\n-- \nThomas Rast\ntrast@{inf,student}.ethz.ch\n"},{"id":"211222","messageId":"7vip4v475o.fsf@alter.siamese.dyndns.org","threadId":"33038","inReplyTo":"87wqtbivms.fsf@pctrast.inf.ethz.ch","subject":"Re: [PATCH v8 2/5] blame: introduce $ as \"end of file\" in -L syntax","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-03-13T16:05:23Z","receivedAt":"2013-03-13T16:05:23Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Thomas Rast <trast@inf.ethz.ch> writes:\n\n>> Even though I am moving from builtin/blame.c to line-log.c?  I would\n>> otherwise have to call from a rather lib-ish file into a \"front\n>\n> You haven't sent any reply to this.  Does that mean you agree?  Would\n> you prefer the shared file to be named something like line-range.c?\n\nThe latter, or if you do not want a churn, not moving it out of\nblame.c at all.\n\nBecause \"log -L\" does not play well with other patch options, from\nthe end-user's perspective, even though may does not have cmd_logL()\nentry point, I do not think line-log.c is any more library-ish than\ncmd_log() or cmd_blame().\n"}]}