+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);
+}
+