Skip to content

Commit c85db25

Browse files
peffspearce
authored andcommitted
reformat informational commit message
When committing, we print a message like: Created [DETACHED commit] <hash> (<subject>) on <branch> 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 <peff@peff.net> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
1 parent 72c69eb commit c85db25

File tree

1 file changed

+10
-27
lines changed

1 file changed

+10
-27
lines changed

builtin-commit.c

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -878,35 +878,13 @@ int cmd_status(int argc, const char **argv, const char *prefix)
878878
return commitable ? 0 : 1;
879879
}
880880

881-
static char *get_commit_format_string(void)
882-
{
883-
unsigned char sha[20];
884-
const char *head = resolve_ref("HEAD", sha, 0, NULL);
885-
struct strbuf buf = STRBUF_INIT;
886-
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)");
890-
891-
if (!prefixcmp(head, "refs/heads/")) {
892-
const char *cp;
893-
strbuf_addstr(&buf, " on ");
894-
for (cp = head + 11; *cp; cp++) {
895-
if (*cp == '%')
896-
strbuf_addstr(&buf, "%x25");
897-
else
898-
strbuf_addch(&buf, *cp);
899-
}
900-
}
901-
902-
return strbuf_detach(&buf, NULL);
903-
}
904-
905881
static void print_summary(const char *prefix, const unsigned char *sha1)
906882
{
907883
struct rev_info rev;
908884
struct commit *commit;
909-
char *format = get_commit_format_string();
885+
static const char *format = "format:%h: \"%s\"";
886+
unsigned char junk_sha1[20];
887+
const char *head = resolve_ref("HEAD", junk_sha1, 0, NULL);
910888

911889
commit = lookup_commit(sha1);
912890
if (!commit)
@@ -931,15 +909,20 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
931909
rev.diffopt.break_opt = 0;
932910
diff_setup_done(&rev.diffopt);
933911

934-
printf("Created %s", initial_commit ? "root-commit " : "");
912+
printf("[%s%s]: created ",
913+
!prefixcmp(head, "refs/heads/") ?
914+
head + 11 :
915+
!strcmp(head, "HEAD") ?
916+
"detached HEAD" :
917+
head,
918+
initial_commit ? " (root-commit)" : "");
935919

936920
if (!log_tree_commit(&rev, commit)) {
937921
struct strbuf buf = STRBUF_INIT;
938922
format_commit_message(commit, format + 7, &buf, DATE_NORMAL);
939923
printf("%s\n", buf.buf);
940924
strbuf_release(&buf);
941925
}
942-
free(format);
943926
}
944927

945928
static int git_commit_config(const char *k, const char *v, void *cb)

0 commit comments

Comments
 (0)