Browse files

Implement line-history search (git log -L)

This is a rewrite of much of Bo's work, mainly in an effort to split
it into smaller, easier to understand routines.

The algorithm is built around the struct range_set, which encodes a
series of line ranges as intervals [a,b).  This is used in two

* A set of lines we are tracking (which will change as we dig through
* To encode diffs, as pairs of ranges.

The main routine is range_set_map_across_diff().  It processes the
diff between a commit C and some parent P.  It determines which diff
hunks are relevant to the ranges tracked in C, and computes the new
ranges for P.

The algorithm is then simply to process history in topological order
from newest to oldest, computing ranges and (partial) diffs.  At
branch points, we need to merge the ranges we are watching.  We will
find that many commits do not affect the chosen ranges, and mark them
TREESAME (in addition to those already filtered by pathspec limiting).
Another pass of history simplification then gets rid of such commits.

This is wired as an extra filtering pass in the log machinery.  This
currently only reduces code duplication, but should allow for other
simplifications and options to be used.

Finally, we hook a diff printer into the output chain.  Ideally we
would wire directly into the diff logic, to optionally use features
like word diff.  However, that will require some major reworking of
the diff chain, so we completely replace the output with our own diff
for now.

As this was a GSoC project, and has quite some history by now, many
people have helped.  In no particular order, thanks go to

  Jakub Narebski <>
  Jens Lehmann <>
  Jonathan Nieder <>
  Junio C Hamano <>
  Ramsay Jones <>
  Will Palmer <>

Apologies to everyone I forgot.

Signed-off-by: Bo Yang <>
Signed-off-by: Thomas Rast <>
Signed-off-by: Junio C Hamano <>
  • Loading branch information...
trast authored and gitster committed Mar 28, 2013
1 parent c7edcae commit 12da1d1f6ffcd546a892a33302bb34fd37169022
@@ -69,6 +69,23 @@ produced by --stat etc.
Note that only message is considered, if also a diff is shown
its size is not included.
+-L <start>,<end>:<file>::
+ Trace the evolution of the line range given by "<start>,<end>"
+ within the <file>. You may not give any pathspec limiters.
+ This is currently limited to a walk starting from a single
+ revision, i.e., you may only give zero or one positive
+ revision arguments.
+<start> and <end> can take one of these forms:
+You can specify this option more than once.
+ Always print the interesting range even if the current commit
+ does not change any line of the range.
[\--] <path>...::
Show only commits that are enough to explain how the files
that match the specified paths came to be. See "History
@@ -138,6 +155,11 @@ Examples
This makes sense only when following a strict policy of merging all
topic branches when staying on a single integration branch.
+git log -L '/int main/',/^}/:main.c::
+ Shows how the function `main()` in the file 'main.c' evolved
+ over time.
`git log -3`::
Limits the number of commits to show to 3.
@@ -683,6 +683,7 @@ LIB_H += help.h
LIB_H += http.h
LIB_H += kwset.h
LIB_H += levenshtein.h
+LIB_H += line-log.h
LIB_H += line-range.h
LIB_H += list-objects.h
LIB_H += ll-merge.h
@@ -799,6 +800,7 @@ LIB_OBJS += hex.o
LIB_OBJS += ident.o
LIB_OBJS += kwset.o
LIB_OBJS += levenshtein.o
+LIB_OBJS += line-log.o
LIB_OBJS += line-range.o
LIB_OBJS += list-objects.o
LIB_OBJS += ll-merge.o
@@ -19,6 +19,7 @@
#include "remote.h"
#include "string-list.h"
#include "parse-options.h"
+#include "line-log.h"
#include "branch.h"
#include "streaming.h"
#include "version.h"
@@ -41,6 +42,12 @@ static const char * const builtin_log_usage[] = {
+struct line_opt_callback_data {
+ struct rev_info *rev;
+ const char *prefix;
+ struct string_list args;
static int parse_decoration_style(const char *var, const char *value)
switch (git_config_maybe_bool(var, value)) {
@@ -75,6 +82,19 @@ static int decorate_callback(const struct option *opt, const char *arg, int unse
return 0;
+static int log_line_range_callback(const struct option *option, const char *arg, int unset)
+ struct line_opt_callback_data *data = option->value;
+ if (!arg)
+ return -1;
+ data->rev->line_level_traverse = 1;
+ string_list_append(&data->args, arg);
+ return 0;
static void cmd_log_init_defaults(struct rev_info *rev)
if (fmt_pretty)
@@ -97,16 +117,23 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
struct userformat_want w;
int quiet = 0, source = 0, mailmap = 0;
+ static struct line_opt_callback_data line_cb = {NULL, NULL, STRING_LIST_INIT_DUP};
const struct option builtin_log_options[] = {
OPT_BOOLEAN(0, "quiet", &quiet, N_("suppress diff output")),
OPT_BOOLEAN(0, "source", &source, N_("show source")),
OPT_BOOLEAN(0, "use-mailmap", &mailmap, N_("Use mail map file")),
{ OPTION_CALLBACK, 0, "decorate", NULL, NULL, N_("decorate options"),
PARSE_OPT_OPTARG, decorate_callback},
+ OPT_CALLBACK('L', NULL, &line_cb, "n,m:file",
+ "Process line range n,m in file, counting from 1",
+ log_line_range_callback),
+ line_cb.rev = rev;
+ line_cb.prefix = prefix;
mailmap = use_mailmap_config;
argc = parse_options(argc, argv, prefix,
builtin_log_options, builtin_log_usage,
@@ -160,6 +187,10 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
rev->show_decorations = 1;
+ if (rev->line_level_traverse)
+ line_log_init(rev, line_cb.prefix, &line_cb.args);
Oops, something went wrong.

0 comments on commit 12da1d1

Please sign in to comment.