Skip to content

Commit e502f9e

Browse files
pietergitster
authored andcommitted
builtin-commit.c: show on which branch a commit was added
This outputs the current branch on which a commit was created, just for reference. For example: Created commit 6d42875 on master: Fix submodule invalid command error This also reminds the committer when he is on a detached HEAD: Created commit 02a7172 on detached HEAD: Also do this for 'git commit --amend' Signed-off-by: Pieter de Bie <pdebie@ai.rug.nl> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 80d12c2 commit e502f9e

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

builtin-commit.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,10 +878,37 @@ 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+
strbuf_addstr(&buf, "format:%h");
888+
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/")) {
893+
const char *cp;
894+
strbuf_addstr(&buf, " on ");
895+
for (cp = head + 11; *cp; cp++) {
896+
if (*cp == '%')
897+
strbuf_addstr(&buf, "%x25");
898+
else
899+
strbuf_addch(&buf, *cp);
900+
}
901+
}
902+
strbuf_addstr(&buf, ": %s");
903+
904+
return strbuf_detach(&buf, NULL);
905+
}
906+
881907
static void print_summary(const char *prefix, const unsigned char *sha1)
882908
{
883909
struct rev_info rev;
884910
struct commit *commit;
911+
char *format = get_commit_format_string();
885912

886913
commit = lookup_commit(sha1);
887914
if (!commit)
@@ -899,7 +926,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
899926

900927
rev.verbose_header = 1;
901928
rev.show_root_diff = 1;
902-
get_commit_format("format:%h: %s", &rev);
929+
get_commit_format(format, &rev);
903930
rev.always_show_header = 0;
904931
rev.diffopt.detect_rename = 1;
905932
rev.diffopt.rename_limit = 100;
@@ -910,10 +937,11 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
910937

911938
if (!log_tree_commit(&rev, commit)) {
912939
struct strbuf buf = STRBUF_INIT;
913-
format_commit_message(commit, "%h: %s", &buf, DATE_NORMAL);
940+
format_commit_message(commit, format + 7, &buf, DATE_NORMAL);
914941
printf("%s\n", buf.buf);
915942
strbuf_release(&buf);
916943
}
944+
free(format);
917945
}
918946

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

0 commit comments

Comments
 (0)