From e502f9e7e6ceb8dfbdb94e2675355847740fc28f Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Mon, 8 Sep 2008 01:05:41 +0200 Subject: builtin-commit.c: show on which branch a commit was added 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 Signed-off-by: Junio C Hamano --- builtin-commit.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'builtin-commit.c') diff --git a/builtin-commit.c b/builtin-commit.c index 8165bb3d3..f7b90604a 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, "%x25"); + 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) -- cgit v1.2.1 From 72c69ebc035efe08aef03866184aa9b344814d93 Mon Sep 17 00:00:00 2001 From: Andreas Ericsson Date: Tue, 30 Sep 2008 11:52:50 +0200 Subject: git commit: Reformat output somewhat Previously, we used to print something along the lines of Created commit abc9056 on master: Snib the sprock but that output was sometimes confusing, as many projects use the "subsystem: message" style of commit subjects (just like this commit message does). When such improvements are done on topic-branches, it's not uncommon to name the topic-branch the same as the subsystem, leading to output like this: Created commit abc9056 on i386: i386: Snib the sprock which doesn't look very nice and can be highly confusing. This patch alters the format so that the noise-word "commit" is dropped except when it makes the output read better and the commit subject is put inside parentheses. We also emphasize the detached case so that users do not overlook it in case the commit subject is long enough to extend to the next line. The end result looks thusly: normal case Created abc9056 (i386: Snib the sprock) on i386 detached head Created DETACHED commit abc9056 (i386: Snib the sprock) While we're at it, we rename "initial commit" to "root-commit" to align it with the argument to 'git log', producing this: initial commit Created root-commit abc9056 (i386: Snib the sprock) on i386 Documentation/gittutorial-2.txt is updated accordingly so that new users recognize what they're looking at. Signed-off-by: Andreas Ericsson Signed-off-by: Shawn O. Pearce --- builtin-commit.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'builtin-commit.c') diff --git a/builtin-commit.c b/builtin-commit.c index f7b90604a..4f0745fa0 100644 --- a/builtin-commit.c +++ b/builtin-commit.c @@ -884,12 +884,11 @@ static char *get_commit_format_string(void) const char *head = resolve_ref("HEAD", sha, 0, NULL); struct strbuf buf = STRBUF_INIT; - strbuf_addstr(&buf, "format:%h"); + /* use shouty-caps if we're on detached HEAD */ + strbuf_addf(&buf, "format:%s", strcmp("HEAD", head) ? "" : "DETACHED commit"); + strbuf_addstr(&buf, "%h (%s)"); - /* Are we on a detached HEAD? */ - if (!strcmp("HEAD", head)) - strbuf_addstr(&buf, " on detached HEAD"); - else if (!prefixcmp(head, "refs/heads/")) { + if (!prefixcmp(head, "refs/heads/")) { const char *cp; strbuf_addstr(&buf, " on "); for (cp = head + 11; *cp; cp++) { @@ -899,7 +898,6 @@ static char *get_commit_format_string(void) strbuf_addch(&buf, *cp); } } - strbuf_addstr(&buf, ": %s"); return strbuf_detach(&buf, NULL); } @@ -933,7 +931,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1) rev.diffopt.break_opt = 0; diff_setup_done(&rev.diffopt); - printf("Created %scommit ", initial_commit ? "initial " : ""); + printf("Created %s", initial_commit ? "root-commit " : ""); if (!log_tree_commit(&rev, commit)) { struct strbuf buf = STRBUF_INIT; -- cgit v1.2.1 From c85db254d9d3309bb330444385dbb3b7be5100d3 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Wed, 1 Oct 2008 18:31:25 -0400 Subject: reformat informational commit message When committing, we print a message like: Created [DETACHED commit] () on The most useful bit of information there (besides the detached status, if it is present) is which branch you made the commit on. However, it is sometimes hard to see because the subject dominates the line. Instead, let's put the most useful information (detached status and commit branch) on the far left, with the subject (which is least likely to be interesting) on the far right. We'll use brackets to offset the branch name so the line is not mistaken for an error line of the form "program: some sort of error". E.g.,: [jk/bikeshed] created bd8098f: "reformat informational commit message" Signed-off-by: Jeff King Signed-off-by: Shawn O. Pearce --- builtin-commit.c | 37 ++++++++++--------------------------- 1 file changed, 10 insertions(+), 27 deletions(-) (limited to 'builtin-commit.c') diff --git a/builtin-commit.c b/builtin-commit.c index 4f0745fa0..f2e32a830 100644 --- a/builtin-commit.c +++ b/builtin-commit.c @@ -878,35 +878,13 @@ 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; - - /* use shouty-caps if we're on detached HEAD */ - strbuf_addf(&buf, "format:%s", strcmp("HEAD", head) ? "" : "DETACHED commit"); - strbuf_addstr(&buf, "%h (%s)"); - - if (!prefixcmp(head, "refs/heads/")) { - const char *cp; - strbuf_addstr(&buf, " on "); - for (cp = head + 11; *cp; cp++) { - if (*cp == '%') - strbuf_addstr(&buf, "%x25"); - else - strbuf_addch(&buf, *cp); - } - } - - 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(); + static const char *format = "format:%h: \"%s\""; + unsigned char junk_sha1[20]; + const char *head = resolve_ref("HEAD", junk_sha1, 0, NULL); commit = lookup_commit(sha1); if (!commit) @@ -931,7 +909,13 @@ static void print_summary(const char *prefix, const unsigned char *sha1) rev.diffopt.break_opt = 0; diff_setup_done(&rev.diffopt); - printf("Created %s", initial_commit ? "root-commit " : ""); + printf("[%s%s]: created ", + !prefixcmp(head, "refs/heads/") ? + head + 11 : + !strcmp(head, "HEAD") ? + "detached HEAD" : + head, + initial_commit ? " (root-commit)" : ""); if (!log_tree_commit(&rev, commit)) { struct strbuf buf = STRBUF_INIT; @@ -939,7 +923,6 @@ static void print_summary(const char *prefix, const unsigned char *sha1) printf("%s\n", buf.buf); strbuf_release(&buf); } - free(format); } static int git_commit_config(const char *k, const char *v, void *cb) -- cgit v1.2.1