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

[PATCH] log: convert to parse-options

From
Carlos Martín Nieto <cmn@elego.de>
Date
Apr 14, 2011, 14:28 UTC
Message-ID
<1302791310-19640-1-git-send-email-cmn@elego.de>
In-Reply-To
<7vlizem9bx.fsf@alter.siamese.dyndns.org>

Use parse-options in cmd_log_init instead of manually iterating through them. This makes the code a bit cleaner but more importantly allows us to catch the "--quiet" option which causes some of the log-related commands to misbehave as it would otherwise get passed on to the diff.

Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
---

This "fixes" the previous --quiet effects by not letting it through. Later we can decide to make it mean the same as -s or just leave it like that.

 builtin/log.c |   77 ++++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 49 insertions(+), 28 deletions(-)
diff --git a/builtin/log.c b/builtin/log.c
index 9a15d69..5316be3 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -25,6 +25,7 @@ static const char *default_date_mode = NULL;
 
 static int default_show_root = 1;
 static int decoration_style;
+static int decoration_given = 0;
 static const char *fmt_patch_subject_prefix = "PATCH";
 static const char *fmt_pretty;
 
@@ -49,12 +50,51 @@ static int parse_decoration_style(const char *var, const char *value)
 	return -1;
 }
 
+static int decorate_callback(const struct option *opt, const char *arg, int unset)
+{
+	if (unset) {
+		decoration_style = 0;
+		return 0;
+	}
+
+	if (arg == NULL) {
+		decoration_style = DECORATE_SHORT_REFS;
+		decoration_given = 1;
+		return 0;
+	}
+
+	/* First arg is irrelevant, as it just tries to parse arg */
+	decoration_style = parse_decoration_style("decorate", arg);
+	if (decoration_style < 0)
+		die("invalid --decorate option: %s", arg);
+
+	decoration_given = 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;
+	int help, quiet, source;
+
+	const struct option builtin_log_options[] = {
+		OPT_BOOLEAN(0, "h", &help, "show help"),
+		OPT_BOOLEAN(0, "quiet", &quiet, "supress diff output"),
+		OPT_BOOLEAN(0, "source", &source, "show source"),
+		{ OPTION_CALLBACK, 0, "decorate", NULL, NULL, "decorate options",
+		  PARSE_OPT_OPTARG, decorate_callback},
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, builtin_log_options,
+						 builtin_log_usage,
+						 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |
+						 PARSE_OPT_KEEP_DASHDASH);
+
+	if (help)
+		usage(builtin_log_usage);
 
 	rev->abbrev = DEFAULT_ABBREV;
 	rev->commit_format = CMIT_FMT_DEFAULT;
@@ -69,14 +109,12 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
 	if (default_date_mode)
 		rev->date_mode = parse_date_format(default_date_mode);
 
-	/*
-	 * Check for -h before setup_revisions(), or "git log -h" will
-	 * fail when run without a git directory.
-	 */
-	if (argc == 2 && !strcmp(argv[1], "-h"))
-		usage(builtin_log_usage);
 	argc = setup_revisions(argc, argv, rev, opt);
 
+	/* Any arguments at this point are not recognized */
+	if (argc > 1)
+		die("unrecognized argument: %s", argv[1]);
+
 	memset(&w, 0, sizeof(w));
 	userformat_find_requirements(NULL, &w);
 
@@ -92,26 +130,9 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
 		if (rev->diffopt.nr_paths != 1)
 			usage("git logs can only follow renames on one pathname at a time");
 	}
-	for (i = 1; i < argc; i++) {
-		const char *arg = argv[i];
-		if (!strcmp(arg, "--decorate")) {
-			decoration_style = DECORATE_SHORT_REFS;
-			decoration_given = 1;
-		} else if (!prefixcmp(arg, "--decorate=")) {
-			const char *v = skip_prefix(arg, "--decorate=");
-			decoration_style = parse_decoration_style(arg, v);
-			if (decoration_style < 0)
-				die("invalid --decorate option: %s", arg);
-			decoration_given = 1;
-		} else if (!strcmp(arg, "--no-decorate")) {
-			decoration_style = 0;
-		} else if (!strcmp(arg, "--source")) {
-			rev->show_source = 1;
-		} else if (!strcmp(arg, "-h")) {
-			usage(builtin_log_usage);
-		} else
-			die("unrecognized argument: %s", arg);
-	}
+
+	if (source)
+		rev->show_source = 1;
 
 	/*
 	 * defeat log.decorate configuration interacting with --pretty=raw
-- 
1.7.4.2.437.g4fc7e.dirty
Previous: Carlos Martín NietoNext: Junio C Hamano
Message 13 of 17 in “Bug in "git diff --quiet" handling.”
  1. Paul GortmakerApr 11, 2011
  2. Junio C HamanoApr 11, 2011
  3. format-patch: don't pass on the --quiet flagCarlos Martín Nieto, Apr 12, 2011
  4. format-patch: document --quiet optionCarlos Martín Nieto, Apr 12, 2011
  5. Junio C HamanoApr 12, 2011
  6. Junio C HamanoApr 12, 2011
  7. Carlos Martín NietoApr 13, 2011
  8. Junio C HamanoApr 12, 2011
  9. Carlos Martín NietoApr 13, 2011
  10. whatchanged: always show the headerCarlos Martín Nieto, Apr 13, 2011
  11. Junio C HamanoApr 13, 2011
  12. Carlos Martín NietoApr 13, 2011
  13. log: convert to parse-optionsCarlos Martín Nieto, Apr 14, 2011
  14. Junio C HamanoApr 14, 2011
  15. log: convert to parse-options=?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?=, Apr 19, 2011
  16. Jeff KingApr 20, 2011
  17. Carlos Martín NietoApr 20, 2011

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.