From e502f9e7e6ceb8dfbdb94e2675355847740fc28f Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Mon, 8 Sep 2008 01:05:41 +0200 Subject: [PATCH 1/4] 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(-) diff --git a/builtin-commit.c b/builtin-commit.c index 8165bb3d31..f7b90604aa 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) From 72c69ebc035efe08aef03866184aa9b344814d93 Mon Sep 17 00:00:00 2001 From: Andreas Ericsson Date: Tue, 30 Sep 2008 11:52:50 +0200 Subject: [PATCH 2/4] 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 --- Documentation/gittutorial-2.txt | 13 ++++++++----- builtin-commit.c | 12 +++++------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/Documentation/gittutorial-2.txt b/Documentation/gittutorial-2.txt index 660904686c..8484e7a026 100644 --- a/Documentation/gittutorial-2.txt +++ b/Documentation/gittutorial-2.txt @@ -32,22 +32,25 @@ Initialized empty Git repository in .git/ $ echo 'hello world' > file.txt $ git add . $ git commit -a -m "initial commit" -Created initial commit 54196cc2703dc165cbd373a65a4dcf22d50ae7f7 +Created root-commit 54196cc (initial commit) on master create mode 100644 file.txt $ echo 'hello world!' >file.txt $ git commit -a -m "add emphasis" -Created commit c4d59f390b9cfd4318117afde11d601c1085f241 +Created c4d59f3 (add emphasis) on master ------------------------------------------------ -What are the 40 digits of hex that git responded to the commit with? +What are the 7 digits of hex that git responded to the commit with? We saw in part one of the tutorial that commits have names like this. It turns out that every object in the git history is stored under -such a 40-digit hex name. That name is the SHA1 hash of the object's +a 40-digit hex name. That name is the SHA1 hash of the object's contents; among other things, this ensures that git will never store the same data twice (since identical data is given an identical SHA1 name), and that the contents of a git object will never change (since -that would change the object's name as well). +that would change the object's name as well). The 7 char hex strings +here are simply the abbreviation of such 40 character long strings. +Abbreviations can be used everywhere where the 40 character strings +can be used, so long as they are unambiguous. It is expected that the content of the commit object you created while following the example above generates a different SHA1 hash than diff --git a/builtin-commit.c b/builtin-commit.c index f7b90604aa..4f0745fa0e 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; From c85db254d9d3309bb330444385dbb3b7be5100d3 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Wed, 1 Oct 2008 18:31:25 -0400 Subject: [PATCH 3/4] 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(-) diff --git a/builtin-commit.c b/builtin-commit.c index 4f0745fa0e..f2e32a830e 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) From b724fd094496625325dae009170445ef12862f96 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Fri, 3 Oct 2008 22:13:49 -0400 Subject: [PATCH 4/4] tutorial: update output of git commit Commit c85db254 changed the format of the message produced by "git commit" when creating a commit. This patch updates the example session in the tutorial to the new format. It also adds in the missing diffstat summary lines, which should have been added long ago. Signed-off-by: Jeff King Signed-off-by: Shawn O. Pearce --- Documentation/gittutorial-2.txt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Documentation/gittutorial-2.txt b/Documentation/gittutorial-2.txt index 8484e7a026..bab0f34b45 100644 --- a/Documentation/gittutorial-2.txt +++ b/Documentation/gittutorial-2.txt @@ -32,11 +32,13 @@ Initialized empty Git repository in .git/ $ echo 'hello world' > file.txt $ git add . $ git commit -a -m "initial commit" -Created root-commit 54196cc (initial commit) on master +[master (root-commit)] created 54196cc: "initial commit" + 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 file.txt $ echo 'hello world!' >file.txt $ git commit -a -m "add emphasis" -Created c4d59f3 (add emphasis) on master +[master] created c4d59f3: "add emphasis" + 1 files changed, 1 insertions(+), 1 deletions(-) ------------------------------------------------ What are the 7 digits of hex that git responded to the commit with?