Skip to content

Commit f4a75a4

Browse files
committed
Merge branch 'pb/commit-where'
* pb/commit-where: tutorial: update output of git commit reformat informational commit message git commit: Reformat output somewhat builtin-commit.c: show on which branch a commit was added
2 parents 46dc1b0 + b724fd0 commit f4a75a4

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

Documentation/gittutorial-2.txt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,27 @@ 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+
[master (root-commit)] created 54196cc: "initial commit"
36+
1 files changed, 1 insertions(+), 0 deletions(-)
3637
create mode 100644 file.txt
3738
$ echo 'hello world!' >file.txt
3839
$ git commit -a -m "add emphasis"
39-
Created commit c4d59f390b9cfd4318117afde11d601c1085f241
40+
[master] created c4d59f3: "add emphasis"
41+
1 files changed, 1 insertions(+), 1 deletions(-)
4042
------------------------------------------------
4143

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

4446
We saw in part one of the tutorial that commits have names like this.
4547
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
48+
a 40-digit hex name. That name is the SHA1 hash of the object's
4749
contents; among other things, this ensures that git will never store
4850
the same data twice (since identical data is given an identical SHA1
4951
name), and that the contents of a git object will never change (since
50-
that would change the object's name as well).
52+
that would change the object's name as well). The 7 char hex strings
53+
here are simply the abbreviation of such 40 character long strings.
54+
Abbreviations can be used everywhere where the 40 character strings
55+
can be used, so long as they are unambiguous.
5156

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

builtin-commit.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,9 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
879879
{
880880
struct rev_info rev;
881881
struct commit *commit;
882+
static const char *format = "format:%h: \"%s\"";
883+
unsigned char junk_sha1[20];
884+
const char *head = resolve_ref("HEAD", junk_sha1, 0, NULL);
882885

883886
commit = lookup_commit(sha1);
884887
if (!commit)
@@ -896,18 +899,24 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
896899

897900
rev.verbose_header = 1;
898901
rev.show_root_diff = 1;
899-
get_commit_format("format:%h: %s", &rev);
902+
get_commit_format(format, &rev);
900903
rev.always_show_header = 0;
901904
rev.diffopt.detect_rename = 1;
902905
rev.diffopt.rename_limit = 100;
903906
rev.diffopt.break_opt = 0;
904907
diff_setup_done(&rev.diffopt);
905908

906-
printf("Created %scommit ", initial_commit ? "initial " : "");
909+
printf("[%s%s]: created ",
910+
!prefixcmp(head, "refs/heads/") ?
911+
head + 11 :
912+
!strcmp(head, "HEAD") ?
913+
"detached HEAD" :
914+
head,
915+
initial_commit ? " (root-commit)" : "");
907916

908917
if (!log_tree_commit(&rev, commit)) {
909918
struct strbuf buf = STRBUF_INIT;
910-
format_commit_message(commit, "%h: %s", &buf, DATE_NORMAL);
919+
format_commit_message(commit, format + 7, &buf, DATE_NORMAL);
911920
printf("%s\n", buf.buf);
912921
strbuf_release(&buf);
913922
}

0 commit comments

Comments
 (0)