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

[PATCH] Add --format that is a synonym to --pretty

From
Nanako Shiraishi <nanako3@lavabit.com>
Date
Feb 24, 2009, 04:06 UTC
Message-ID
<20090224130626.6117@nanako3.lavabit.com>
In-Reply-To
<7v3ae4r53f.fsf@gitster.siamese.dyndns.org>

Some people prefer to call the pretty-print styles "format", and get annoyed to see "git log --format=short" fail. Introduce it as a synonym to --pretty so that both can be used interchangeably without breaking examples in existing web pages or ppeople's expectations.

Having to say --format="format:%h %s" is redundant because none of the predefined pretty-print styles have per-cent sign in it, so this patch also makes it possible to say --pretty="%h %s" (and --format="%h %s").

Signed-off-by: Nanako Shiraishi <nanako3@lavabit.com>
---

I think doing it this way won't risk any compatibility issue. Please be gentle; this is my first real patch in the C language.

 Documentation/pretty-formats.txt |    9 +++++++++
 Documentation/pretty-options.txt |    1 +
 pretty.c                         |   20 ++++++++++++++------
 revision.c                       |    2 +-
 4 files changed, 25 insertions(+), 7 deletions(-)
diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
index 159390c..5c6e678 100644
--- a/Documentation/pretty-formats.txt
+++ b/Documentation/pretty-formats.txt
@@ -152,3 +152,12 @@ $ git log -2 --pretty=tformat:%h 4da45bef \
 4da45be
 7134973
 ---------------------
++
+In addition, any unrecognized string that has a `%` in it is interpreted
+as if it has `tformat:` in front of it.  For example, these two are
+equivalent:
++
+---------------------
+$ git log -2 --pretty=tformat:%h 4da45bef
+$ git log -2 --pretty=%h 4da45bef
+---------------------
diff --git a/Documentation/pretty-options.txt b/Documentation/pretty-options.txt
index 5f21efe..6596019 100644
--- a/Documentation/pretty-options.txt
+++ b/Documentation/pretty-options.txt
@@ -1,4 +1,5 @@
 --pretty[='<format>']::
+--format[='<format>']::
 
 	Pretty-print the contents of the commit logs in a given format,
 	where '<format>' can be one of 'oneline', 'short', 'medium',
diff --git a/pretty.c b/pretty.c
index 6cd9149..d739f6d 100644
--- a/pretty.c
+++ b/pretty.c
@@ -10,6 +10,15 @@
 
 static char *user_format;
 
+static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
+{
+	free(user_format);
+	user_format = xstrdup(cp);
+	if (is_tformat)
+		rev->use_terminator = 1;
+	rev->commit_format = CMIT_FMT_USERFORMAT;
+}
+
 void get_commit_format(const char *arg, struct rev_info *rev)
 {
 	int i;
@@ -33,12 +42,7 @@ void get_commit_format(const char *arg, struct rev_info *rev)
 		return;
 	}
 	if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) {
-		const char *cp = strchr(arg, ':') + 1;
-		free(user_format);
-		user_format = xstrdup(cp);
-		if (arg[0] == 't')
-			rev->use_terminator = 1;
-		rev->commit_format = CMIT_FMT_USERFORMAT;
+		save_user_format(rev, strchr(arg, ':') + 1, arg[0] == 't');
 		return;
 	}
 	for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
@@ -50,6 +54,10 @@ void get_commit_format(const char *arg, struct rev_info *rev)
 			return;
 		}
 	}
+	if (strchr(arg, '%')) {
+		save_user_format(rev, arg, 1);
+		return;
+	}
 
 	die("invalid --pretty format: %s", arg);
 }
diff --git a/revision.c b/revision.c
index 286e416..556c319 100644
--- a/revision.c
+++ b/revision.c
@@ -1144,7 +1144,7 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
 	} else if (!strcmp(arg, "--pretty")) {
 		revs->verbose_header = 1;
 		get_commit_format(arg+8, revs);
-	} else if (!prefixcmp(arg, "--pretty=")) {
+	} else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) {
 		revs->verbose_header = 1;
 		get_commit_format(arg+9, revs);
 	} else if (!strcmp(arg, "--graph")) {
-- 
1.6.2.rc1

-- 
Nanako Shiraishi
http://ivory.ap.teacup.com/nanako3/
Previous: Felipe ContrerasNext: Jeff King
Message 16 of 23 in “revision.c: add --format option for 'git log'”
  1. revision.c: add --format option for 'git log'Felipe Contreras, Feb 21, 2009
  2. Junio C HamanoFeb 22, 2009
  3. Felipe ContrerasFeb 22, 2009
  4. Junio C HamanoFeb 22, 2009
  5. Junio C HamanoFeb 22, 2009
  6. Felipe ContrerasFeb 22, 2009
  7. Junio C HamanoFeb 22, 2009
  8. Felipe ContrerasFeb 22, 2009
  9. Junio C HamanoFeb 23, 2009
  10. Felipe ContrerasFeb 24, 2009
  11. Felipe ContrerasFeb 24, 2009
  12. Junio C HamanoFeb 24, 2009
  13. Nanako ShiraishiFeb 24, 2009
  14. Junio C HamanoFeb 24, 2009
  15. Felipe ContrerasFeb 24, 2009
  16. Add --format that is a synonym to --prettyNanako Shiraishi, Feb 24, 2009
  17. Jeff KingFeb 24, 2009
  18. Junio C HamanoFeb 24, 2009
  19. Jeff KingFeb 24, 2009
  20. Felipe ContrerasFeb 24, 2009
  21. Linus TorvaldsFeb 22, 2009
  22. Jakub NarebskiFeb 22, 2009
  23. Wincent ColaiutaFeb 23, 2009

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.