Re: [PATCH 3/3] add a "lua" pretty format

2012-10-06 Thread Jeff King
On Mon, Sep 24, 2012 at 08:25:39PM -0400, Jeff King wrote:

> @@ -1168,7 +1180,11 @@ void format_commit_message(const struct commit *commit,
>   free(enc);
>   }
>  
> - strbuf_expand(sb, format, format_commit_item, &context);
> + if (pretty_ctx->fmt == CMIT_FMT_USERFORMAT)
> + strbuf_expand(sb, format, format_commit_item, &context);
> + else if (pretty_ctx->fmt == CMIT_FMT_LUA)
> + lua_commit_format(sb, &context);
> +

This hunk breaks lots of tests. I know we are not seriously considering
the lua series as-is, but in case anybody wants to play with it, here is
the fix (and we would need the same fix regardless of language, anyway).

You might want to queue this on jk/lua-hackery (probably it would be
squashed in for a real series).

-- >8 --
Subject: [PATCH] pretty: fix up one-off format_commit_message calls

If the usual pretty-print code invokes format_commit_message,
the "fmt" field of the pretty_print_context will always have
either CMIT_FMT_USERFORMAT or CMIT_FMT_LUA in it, and we can
just choose which to use.

However, many call sites invoke format_commit_message
directly without bothering to set the "fmt" field of the
context; they expect format_commit_message to just default
to CMIT_FMT_USERFORMAT in that case, since previously that
was the only format it handled.

The recent addition of the lua formatter broke that
assumption. Rather than require each caller to be more
strict, let's just default to USERFORMAT when the format is
set to something nonsensical.

Signed-off-by: Jeff King 
---
 pretty.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/pretty.c b/pretty.c
index fdd4258..7289590 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1180,10 +1180,10 @@ void format_commit_message(const struct commit *commit,
free(enc);
}
 
-   if (pretty_ctx->fmt == CMIT_FMT_USERFORMAT)
-   strbuf_expand(sb, format, format_commit_item, &context);
-   else if (pretty_ctx->fmt == CMIT_FMT_LUA)
+   if (pretty_ctx->fmt == CMIT_FMT_LUA)
lua_commit_format(sb, &context);
+   else
+   strbuf_expand(sb, format, format_commit_item, &context);
 
rewrap_message_tail(sb, &context, 0, 0, 0);
 
-- 
1.8.0.rc0.22.g285fd2d

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 3/3] add a "lua" pretty format

2012-09-24 Thread Jeff King
With this patch, you can do:

  git log --pretty=lua:'
return abbrev(hash()) .. " (" .. author().email .. ") " .. subject()
  '

Signed-off-by: Jeff King 
---
 commit.h   |  1 +
 log-tree.c |  3 ++-
 pretty.c   | 21 +++--
 3 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/commit.h b/commit.h
index 71cd4af..8865a36 100644
--- a/commit.h
+++ b/commit.h
@@ -73,6 +73,7 @@ enum cmit_fmt {
CMIT_FMT_ONELINE,
CMIT_FMT_EMAIL,
CMIT_FMT_USERFORMAT,
+   CMIT_FMT_LUA,
 
CMIT_FMT_UNSPECIFIED
 };
diff --git a/log-tree.c b/log-tree.c
index c894930..c8274d1 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -599,7 +599,8 @@ void show_log(struct rev_info *opt)
if (opt->commit_format == CMIT_FMT_EMAIL) {
log_write_email_headers(opt, commit, &ctx.subject, 
&extra_headers,
&ctx.need_8bit_cte);
-   } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
+   } else if (opt->commit_format != CMIT_FMT_USERFORMAT &&
+  opt->commit_format != CMIT_FMT_LUA) {
fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
if (opt->commit_format != CMIT_FMT_ONELINE)
fputs("commit ", stdout);
diff --git a/pretty.c b/pretty.c
index 0d4eb3d..fdd4258 100644
--- a/pretty.c
+++ b/pretty.c
@@ -10,6 +10,7 @@
 #include "color.h"
 #include "reflog-walk.h"
 #include "gpg-interface.h"
+#include "lua-commit.h"
 
 static char *user_format;
 static struct cmt_fmt_map {
@@ -33,6 +34,13 @@ static void save_user_format(struct rev_info *rev, const 
char *cp, int is_tforma
rev->commit_format = CMIT_FMT_USERFORMAT;
 }
 
+static void save_lua_format(struct rev_info *rev, const char *cp, int 
is_tformat)
+{
+   lua_commit_init(cp);
+   save_user_format(rev, cp, is_tformat);
+   rev->commit_format = CMIT_FMT_LUA;
+}
+
 static int git_pretty_formats_config(const char *var, const char *value, void 
*cb)
 {
struct cmt_fmt_map *commit_format = NULL;
@@ -155,6 +163,10 @@ void get_commit_format(const char *arg, struct rev_info 
*rev)
save_user_format(rev, strchr(arg, ':') + 1, arg[0] == 't');
return;
}
+   if (!prefixcmp(arg, "lua:")) {
+   save_lua_format(rev, arg + 4, 1);
+   return;
+   }
 
if (strchr(arg, '%')) {
save_user_format(rev, arg, 1);
@@ -1168,7 +1180,11 @@ void format_commit_message(const struct commit *commit,
free(enc);
}
 
-   strbuf_expand(sb, format, format_commit_item, &context);
+   if (pretty_ctx->fmt == CMIT_FMT_USERFORMAT)
+   strbuf_expand(sb, format, format_commit_item, &context);
+   else if (pretty_ctx->fmt == CMIT_FMT_LUA)
+   lua_commit_format(sb, &context);
+
rewrap_message_tail(sb, &context, 0, 0, 0);
 
if (context.message != commit->buffer)
@@ -1328,7 +1344,8 @@ void pretty_print_commit(const struct 
pretty_print_context *pp,
const char *encoding;
int need_8bit_cte = pp->need_8bit_cte;
 
-   if (pp->fmt == CMIT_FMT_USERFORMAT) {
+   if (pp->fmt == CMIT_FMT_USERFORMAT ||
+   pp->fmt == CMIT_FMT_LUA) {
format_commit_message(commit, user_format, sb, pp);
return;
}
-- 
1.7.12.1.10.g6537447
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html