Skip to content

Commit 12da1d1

Browse files
trastgitster
authored andcommitted
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 contexts: * A set of lines we are tracking (which will change as we dig through history). * 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 <[email protected]> Jens Lehmann <[email protected]> Jonathan Nieder <[email protected]> Junio C Hamano <[email protected]> Ramsay Jones <[email protected]> Will Palmer <[email protected]> Apologies to everyone I forgot. Signed-off-by: Bo Yang <[email protected]> Signed-off-by: Thomas Rast <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c7edcae commit 12da1d1

20 files changed

+2156
-2
lines changed

Documentation/git-log.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ produced by --stat etc.
6969
Note that only message is considered, if also a diff is shown
7070
its size is not included.
7171

72+
-L <start>,<end>:<file>::
73+
Trace the evolution of the line range given by "<start>,<end>"
74+
within the <file>. You may not give any pathspec limiters.
75+
This is currently limited to a walk starting from a single
76+
revision, i.e., you may only give zero or one positive
77+
revision arguments.
78+
79+
<start> and <end> can take one of these forms:
80+
81+
include::line-range-format.txt[]
82+
You can specify this option more than once.
83+
84+
85+
--full-line-diff::
86+
Always print the interesting range even if the current commit
87+
does not change any line of the range.
88+
7289
[\--] <path>...::
7390
Show only commits that are enough to explain how the files
7491
that match the specified paths came to be. See "History
@@ -138,6 +155,11 @@ Examples
138155
This makes sense only when following a strict policy of merging all
139156
topic branches when staying on a single integration branch.
140157

158+
git log -L '/int main/',/^}/:main.c::
159+
160+
Shows how the function `main()` in the file 'main.c' evolved
161+
over time.
162+
141163
`git log -3`::
142164
Limits the number of commits to show to 3.
143165

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ LIB_H += help.h
683683
LIB_H += http.h
684684
LIB_H += kwset.h
685685
LIB_H += levenshtein.h
686+
LIB_H += line-log.h
686687
LIB_H += line-range.h
687688
LIB_H += list-objects.h
688689
LIB_H += ll-merge.h
@@ -799,6 +800,7 @@ LIB_OBJS += hex.o
799800
LIB_OBJS += ident.o
800801
LIB_OBJS += kwset.o
801802
LIB_OBJS += levenshtein.o
803+
LIB_OBJS += line-log.o
802804
LIB_OBJS += line-range.o
803805
LIB_OBJS += list-objects.o
804806
LIB_OBJS += ll-merge.o

builtin/log.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "remote.h"
2020
#include "string-list.h"
2121
#include "parse-options.h"
22+
#include "line-log.h"
2223
#include "branch.h"
2324
#include "streaming.h"
2425
#include "version.h"
@@ -41,6 +42,12 @@ static const char * const builtin_log_usage[] = {
4142
NULL
4243
};
4344

45+
struct line_opt_callback_data {
46+
struct rev_info *rev;
47+
const char *prefix;
48+
struct string_list args;
49+
};
50+
4451
static int parse_decoration_style(const char *var, const char *value)
4552
{
4653
switch (git_config_maybe_bool(var, value)) {
@@ -75,6 +82,19 @@ static int decorate_callback(const struct option *opt, const char *arg, int unse
7582
return 0;
7683
}
7784

85+
static int log_line_range_callback(const struct option *option, const char *arg, int unset)
86+
{
87+
struct line_opt_callback_data *data = option->value;
88+
89+
if (!arg)
90+
return -1;
91+
92+
data->rev->line_level_traverse = 1;
93+
string_list_append(&data->args, arg);
94+
95+
return 0;
96+
}
97+
7898
static void cmd_log_init_defaults(struct rev_info *rev)
7999
{
80100
if (fmt_pretty)
@@ -97,16 +117,23 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
97117
{
98118
struct userformat_want w;
99119
int quiet = 0, source = 0, mailmap = 0;
120+
static struct line_opt_callback_data line_cb = {NULL, NULL, STRING_LIST_INIT_DUP};
100121

101122
const struct option builtin_log_options[] = {
102123
OPT_BOOLEAN(0, "quiet", &quiet, N_("suppress diff output")),
103124
OPT_BOOLEAN(0, "source", &source, N_("show source")),
104125
OPT_BOOLEAN(0, "use-mailmap", &mailmap, N_("Use mail map file")),
105126
{ OPTION_CALLBACK, 0, "decorate", NULL, NULL, N_("decorate options"),
106127
PARSE_OPT_OPTARG, decorate_callback},
128+
OPT_CALLBACK('L', NULL, &line_cb, "n,m:file",
129+
"Process line range n,m in file, counting from 1",
130+
log_line_range_callback),
107131
OPT_END()
108132
};
109133

134+
line_cb.rev = rev;
135+
line_cb.prefix = prefix;
136+
110137
mailmap = use_mailmap_config;
111138
argc = parse_options(argc, argv, prefix,
112139
builtin_log_options, builtin_log_usage,
@@ -160,6 +187,10 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
160187
rev->show_decorations = 1;
161188
load_ref_decorations(decoration_style);
162189
}
190+
191+
if (rev->line_level_traverse)
192+
line_log_init(rev, line_cb.prefix, &line_cb.args);
193+
163194
setup_pager();
164195
}
165196

0 commit comments

Comments
 (0)