Skip to content

Commit 72c69eb

Browse files
agericspearce
authored andcommitted
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 <ae@op5.se> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
1 parent e502f9e commit 72c69eb

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

Documentation/gittutorial-2.txt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,25 @@ Initialized empty Git repository in .git/
3232
$ echo 'hello world' > file.txt
3333
$ git add .
3434
$ git commit -a -m "initial commit"
35-
Created initial commit 54196cc2703dc165cbd373a65a4dcf22d50ae7f7
35+
Created root-commit 54196cc (initial commit) on master
3636
create mode 100644 file.txt
3737
$ echo 'hello world!' >file.txt
3838
$ git commit -a -m "add emphasis"
39-
Created commit c4d59f390b9cfd4318117afde11d601c1085f241
39+
Created c4d59f3 (add emphasis) on master
4040
------------------------------------------------
4141

42-
What are the 40 digits of hex that git responded to the commit with?
42+
What are the 7 digits of hex that git responded to the commit with?
4343

4444
We saw in part one of the tutorial that commits have names like this.
4545
It turns out that every object in the git history is stored under
46-
such a 40-digit hex name. That name is the SHA1 hash of the object's
46+
a 40-digit hex name. That name is the SHA1 hash of the object's
4747
contents; among other things, this ensures that git will never store
4848
the same data twice (since identical data is given an identical SHA1
4949
name), and that the contents of a git object will never change (since
50-
that would change the object's name as well).
50+
that would change the object's name as well). The 7 char hex strings
51+
here are simply the abbreviation of such 40 character long strings.
52+
Abbreviations can be used everywhere where the 40 character strings
53+
can be used, so long as they are unambiguous.
5154

5255
It is expected that the content of the commit object you created while
5356
following the example above generates a different SHA1 hash than

builtin-commit.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -884,12 +884,11 @@ static char *get_commit_format_string(void)
884884
const char *head = resolve_ref("HEAD", sha, 0, NULL);
885885
struct strbuf buf = STRBUF_INIT;
886886

887-
strbuf_addstr(&buf, "format:%h");
887+
/* use shouty-caps if we're on detached HEAD */
888+
strbuf_addf(&buf, "format:%s", strcmp("HEAD", head) ? "" : "DETACHED commit");
889+
strbuf_addstr(&buf, "%h (%s)");
888890

889-
/* Are we on a detached HEAD? */
890-
if (!strcmp("HEAD", head))
891-
strbuf_addstr(&buf, " on detached HEAD");
892-
else if (!prefixcmp(head, "refs/heads/")) {
891+
if (!prefixcmp(head, "refs/heads/")) {
893892
const char *cp;
894893
strbuf_addstr(&buf, " on ");
895894
for (cp = head + 11; *cp; cp++) {
@@ -899,7 +898,6 @@ static char *get_commit_format_string(void)
899898
strbuf_addch(&buf, *cp);
900899
}
901900
}
902-
strbuf_addstr(&buf, ": %s");
903901

904902
return strbuf_detach(&buf, NULL);
905903
}
@@ -933,7 +931,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
933931
rev.diffopt.break_opt = 0;
934932
diff_setup_done(&rev.diffopt);
935933

936-
printf("Created %scommit ", initial_commit ? "initial " : "");
934+
printf("Created %s", initial_commit ? "root-commit " : "");
937935

938936
if (!log_tree_commit(&rev, commit)) {
939937
struct strbuf buf = STRBUF_INIT;

0 commit comments

Comments
 (0)