git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v4 05/18] Parse the -L options

From
Bo Yang <struggleyb.nku@gmail.com>
Date
Aug 5, 2010, 16:11 UTC
Message-ID
<1281024717-7855-6-git-send-email-struggleyb.nku@gmail.com>
In-Reply-To
<1281024717-7855-1-git-send-email-struggleyb.nku@gmail.com>

With the two new APIs of parse options added in the previous commit, we parse the multiple '-L n,m <pathspec>' syntax.

Notice that users can give more than one '-L n,m' for each pathspec. And a pathspec with all its '-L' options maps to a single diff_line_range structure.

This has the exactly the same semantics as 'git blame -L n,m <pathspec>' because we refactored and reused the blame code.

Signed-off-by: Bo Yang <struggleyb.nku@gmail.com>
---
 builtin/log.c |  103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 102 insertions(+), 1 deletions(-)
diff --git a/builtin/log.c b/builtin/log.c
index 08b8722..84d781e 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -19,6 +19,7 @@
 #include "remote.h"
 #include "string-list.h"
 #include "parse-options.h"
+#include "line.h"
 
 /* Set a default date-time format for git log ("log.date" config variable) */
 static const char *default_date_mode = NULL;
@@ -27,11 +28,24 @@ static int default_show_root = 1;
 static int decoration_style;
 static const char *fmt_patch_subject_prefix = "PATCH";
 static const char *fmt_pretty;
+static const char *dashdash = "--";
 
-static const char * const builtin_log_usage =
+static char builtin_log_usage[] =
 	"git log [<options>] [<since>..<until>] [[--] <path>...]\n"
+	"git log [<options>] -L n,m <path>\n"
 	"   or: git show [options] <object>...";
 
+static const char *log_opt_usage[] = {
+	builtin_log_usage,
+	NULL
+};
+
+struct line_opt_callback_data {
+	struct diff_line_range **range;
+	struct parse_opt_ctx_t *ctx;
+	struct rev_info *rev;
+};
+
 static int parse_decoration_style(const char *var, const char *value)
 {
 	switch (git_config_maybe_bool(var, value)) {
@@ -49,12 +63,42 @@ static int parse_decoration_style(const char *var, const char *value)
 	return -1;
 }
 
+static int log_line_range_callback(const struct option *option, const char *arg, int unset)
+{
+	struct line_opt_callback_data *data = option->value;
+	struct diff_line_range *r = *data->range;
+	struct parse_opt_ctx_t *ctx = data->ctx;
+
+	if (!arg)
+		return -1;
+
+	if (r->nr == 0 && r->next == NULL) {
+		ctx->out[ctx->cpidx++] = dashdash;
+	}
+
+	diff_line_range_append(r, arg);
+	data->rev->line_level_traverse = 1;
+	return 0;
+}
+
 static void cmd_log_init(int argc, const char **argv, const char *prefix,
 			 struct rev_info *rev, struct setup_revision_opt *opt)
 {
 	int i;
 	int decoration_given = 0;
 	struct userformat_want w;
+	const char *path = NULL, *pathspec = NULL;
+	static struct diff_line_range *range = NULL, *r = NULL;
+	static struct parse_opt_ctx_t ctx;
+	static struct line_opt_callback_data line_cb = {&range, &ctx, NULL};
+	static const struct option options[] = {
+		OPT_CALLBACK('L', NULL, &line_cb, "n,m", "Process only line range n,m, counting from 1", log_line_range_callback),
+		OPT_END()
+	};
+
+	line_cb.rev = rev;
+	range = xmalloc(sizeof(*range));
+	DIFF_LINE_RANGE_INIT(range);
 
 	rev->abbrev = DEFAULT_ABBREV;
 	rev->commit_format = CMIT_FMT_DEFAULT;
@@ -75,6 +119,58 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
 	 */
 	if (argc == 2 && !strcmp(argv[1], "-h"))
 		usage(builtin_log_usage);
+
+	parse_options_start(&ctx, argc, argv, prefix, PARSE_OPT_KEEP_DASHDASH |
+			PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_STOP_AT_NON_OPTION);
+	for (;;) {
+		switch (parse_options_step(&ctx, options, log_opt_usage)) {
+		case PARSE_OPT_HELP:
+			exit(129);
+		case PARSE_OPT_DONE:
+			goto parse_done;
+		case PARSE_OPT_NON_OPTION:
+			path = parse_options_current(&ctx);
+			pathspec = prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
+			range->spec = alloc_filespec(pathspec);
+			free((void *)pathspec);
+			if (range->nr == 0) {
+				if(range->next) {
+					die("Path %s need a -L <range> option\n"
+					"If you want follow the history of the whole file "
+					"whether to using 'git log' without -L or using "
+					"'git log -L 1,$ <path>'", range->spec->path);
+				} else {
+					parse_options_next(&ctx, 1);
+					continue;
+				}
+			}
+			r = xmalloc(sizeof(*r));
+			DIFF_LINE_RANGE_INIT(r);
+			r->next = range;
+			range = r;
+			parse_options_next(&ctx, 1);
+			continue;
+		case PARSE_OPT_UNKNOWN:
+			parse_options_next(&ctx, 1);
+			continue;
+		}
+
+		parse_revision_opt(rev, &ctx, options, log_opt_usage);
+	}
+parse_done:
+	argc = parse_options_end(&ctx);
+
+	/* die if '-L <range>' with no pathspec follow */
+	if (range->nr > 0 && range->spec == NULL) {
+		die("Each -L should follow a pathspec");
+	}
+	/* clear up the last range */
+	if (range->nr == 0) {
+		struct diff_line_range *r = range->next;
+		DIFF_LINE_RANGE_CLEAR(range);
+		range = r;
+	}
+
 	argc = setup_revisions(argc, argv, rev, opt);
 
 	memset(&w, 0, sizeof(w));
@@ -125,6 +221,11 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
 		rev->show_decorations = 1;
 		load_ref_decorations(decoration_style);
 	}
+
+	/* Test whether line level history is asked for */
+	if (range && range->nr > 0) {
+		setup_line(rev, range);
+	}
 }
 
 /*
-- 
1.7.2.20.g388bbb
Previous: Bo YangNext: Junio C Hamano
Message 9 of 28 in “Reroll the line log series”
  1. 00/18 Reroll the line log seriesBo Yang, Aug 5, 2010
  2. 01/18 parse-options: enhance STOP_AT_NON_OPTIONBo Yang, Aug 5, 2010
  3. 02/18 parse-options: add two helper functionsBo Yang, Aug 5, 2010
  4. Thomas RastAug 5, 2010
  5. 03/18 Add the basic data structure for line level historyBo Yang, Aug 5, 2010
  6. Thomas RastAug 5, 2010
  7. Junio C HamanoAug 6, 2010
  8. 04/18 Refactor parse_locBo Yang, Aug 5, 2010
  9. 05/18 Parse the -L optionsBo Yang, Aug 5, 2010
  10. Junio C HamanoAug 6, 2010
  11. Bo YangAug 10, 2010
  12. 06/18 Export three functions from diff.cBo Yang, Aug 5, 2010
  13. 07/18 Add range clone functionsBo Yang, Aug 5, 2010
  14. 08/18 map/take range to the parent of commitsBo Yang, Aug 5, 2010
  15. Thomas RastAug 5, 2010
  16. 09/18 Print the line logBo Yang, Aug 5, 2010
  17. 10/18 Hook line history into cmd_log, ensuring a topo-ordered walkBo Yang, Aug 5, 2010
  18. 11/18 Add tests for line history browserBo Yang, Aug 5, 2010
  19. Thomas RastAug 5, 2010
  20. Bo YangAug 6, 2010
  21. Thomas RastAug 6, 2010
  22. 12/18 Make rewrite_parents public to other part of gitBo Yang, Aug 5, 2010
  23. 13/18 Make graph_next_line external to other part of gitBo Yang, Aug 5, 2010
  24. 14/18 Add parent rewriting to line history browserBo Yang, Aug 5, 2010
  25. 15/18 Add --graph prefix before line history outputBo Yang, Aug 5, 2010
  26. 16/18 Add --full-line-diff optionBo Yang, Aug 5, 2010
  27. 17/18 Add test cases for '--graph' of line level logBo Yang, Aug 5, 2010
  28. 18/18 Document line history browserBo Yang, Aug 5, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.