Skip to content

Commit

Permalink
reformat informational commit message
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Jeff King authored and Shawn O. Pearce committed Oct 3, 2008
1 parent 72c69eb commit c85db25
Showing 1 changed file with 10 additions and 27 deletions.
37 changes: 10 additions & 27 deletions builtin-commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -931,15 +909,20 @@ 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;
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)
Expand Down

0 comments on commit c85db25

Please sign in to comment.