Re: [PATCH] Builtin-commit: show on which branch a commit was added

3 messages, 2 authors, 2016-06-15 · open the first message on its own page

Re: [PATCH] Builtin-commit: show on which branch a commit was added

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:45:19

Junio C Hamano [off-list ref] writes:
I somehow suspect it might be much simpler, more contained and robust if you:
Ah, that would not work, sorry, because this if() statement is only about
an uninteresting case of --allow-empty.  You need to do this the hard way
and teach get_commit_format_string() quote % characters in branch names.

[PATCH 1/2] pretty.c: add %% format specifier

From: Pieter de Bie <hidden>
Date: 2016-06-15 22:45:19

This adds a %% format which just prints a literal %

Signed-off-by: Pieter de Bie <redacted>
---

On 7 sep 2008, at 07:59, Junio C Hamano wrote:
Junio C Hamano [off-list ref] writes:
quoted
I somehow suspect it might be much simpler, more contained and robust if you:
Ah, that would not work, sorry, because this if() statement is only about
an uninteresting case of --allow-empty.  You need to do this the hard way
and teach get_commit_format_string() quote % characters in branch names.
Yes, here is patch for this. Sorry for the style issues. I also fixed the
strbuf issue.

 Documentation/pretty-formats.txt |    1 +
 pretty.c                         |    3 +++
 2 files changed, 4 insertions(+), 0 deletions(-)
diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
index f18d33e..b91db86 100644
--- a/Documentation/pretty-formats.txt
+++ b/Documentation/pretty-formats.txt
@@ -127,6 +127,7 @@ The placeholders are:
 - '%m': left, right or boundary mark
 - '%n': newline
 - '%x00': print a byte from a hex code
+- '%%': print a literal '%'
 
 * 'tformat:'
 +
diff --git a/pretty.c b/pretty.c
index 8beafa0..f66d687 100644
--- a/pretty.c
+++ b/pretty.c
@@ -538,6 +538,9 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
 			return 3;
 		} else
 			return 0;
+	case '%':
+		strbuf_addch(sb, '%');
+		return 1;
 	}
 
 	/* these depend on the commit */
-- 
1.6.0.1.346.g880d9.dirty

[PATCH 2/2] builtin-commit: show on which branch a commit was added

From: Pieter de Bie <hidden>
Date: 2016-06-15 22:45:19

This outputs the current branch on which a commit was created, just for
reference. For example:

	Created commit 6d42875 on master: Fix submodule invalid command error

This also reminds the committer when he is on a detached HEAD:

	Created commit 02a7172 on detached HEAD: Also do this for 'git commit --amend'

Signed-off-by: Pieter de Bie <redacted>
---
 builtin-commit.c |   32 ++++++++++++++++++++++++++++++--
 1 files changed, 30 insertions(+), 2 deletions(-)
diff --git a/builtin-commit.c b/builtin-commit.c
index 8165bb3..47b76e6 100644
--- a/builtin-commit.c
+++ b/builtin-commit.c
@@ -878,10 +878,37 @@ int cmd_status(int argc, const char **argv, const char *prefix)
 	return commitable ? 0 : 1;
 }
 
+static char *get_commit_format_string(void)
+{
+	unsigned char sha[20];
+	const char *head = resolve_ref("HEAD", sha, 0, NULL);
+	struct strbuf buf = STRBUF_INIT;
+
+	strbuf_addstr(&buf, "format:%h");
+
+	/* Are we on a detached HEAD? */
+	if (!strcmp("HEAD", head))
+		strbuf_addstr(&buf, " on detached HEAD");
+	else if (!prefixcmp(head, "refs/heads/")) {
+		const char *cp;
+		strbuf_addstr(&buf, " on ");
+		for (cp = head + 11; *cp; cp++) {
+			if (*cp == '%')
+				strbuf_addstr(&buf, "%%");
+			else
+				strbuf_addch(&buf, *cp);
+		}
+	}
+	strbuf_addstr(&buf, ": %s");
+
+	return strbuf_detach(&buf, NULL);
+}
+
 static void print_summary(const char *prefix, const unsigned char *sha1)
 {
 	struct rev_info rev;
 	struct commit *commit;
+	char *format = get_commit_format_string();
 
 	commit = lookup_commit(sha1);
 	if (!commit)
@@ -899,7 +926,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
 
 	rev.verbose_header = 1;
 	rev.show_root_diff = 1;
-	get_commit_format("format:%h: %s", &rev);
+	get_commit_format(format, &rev);
 	rev.always_show_header = 0;
 	rev.diffopt.detect_rename = 1;
 	rev.diffopt.rename_limit = 100;
@@ -910,10 +937,11 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
 
 	if (!log_tree_commit(&rev, commit)) {
 		struct strbuf buf = STRBUF_INIT;
-		format_commit_message(commit, "%h: %s", &buf, DATE_NORMAL);
+		format_commit_message(commit, format + 7, &buf, DATE_NORMAL);
 		printf("%s\n", buf.buf);
 		strbuf_release(&buf);
 	}
+	free(format);
 }
 
 static int git_commit_config(const char *k, const char *v, void *cb)
-- 
1.6.0.1.346.g880d9.dirty
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help