Skip to content

Commit 76c61fb

Browse files
committed
log: decorate HEAD with branch name under --decorate=full, too
The previous step to teach "log --decorate" to show "HEAD -> master" instead of "HEAD, master" when showing the commit at the tip of the 'master' branch, when the 'master' branch is checked out, did not work for "log --decorate=full". The commands in the "log" family prepare commit decorations for all refs upfront, and the actual string used in a decoration depends on how load_ref_decorations() is called very early in the process. By default, "git log --decorate" stores names with common prefixes such as "refs/heads" stripped; "git log --decorate=full" stores the full refnames. When the current_pointed_by_HEAD() function has to decide if "HEAD" points at the branch a decoration describes, however, what was passed to load_ref_decorations() to decide to strip (or keep) such a common prefix is long lost. This makes it impossible to reliably tell if a decoration that stores "refs/heads/master", for example, is the 'master' branch (under "--decorate" with prefix omitted) or 'refs/heads/master' branch (under "--decorate=full"). Keep what was passed to load_ref_decorations() in a global next to the global variable name_decoration, and use that to decide how to match what was read from "HEAD" and what is in a decoration. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 51ff0f2 commit 76c61fb

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

log-tree.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include "line-log.h"
1414

1515
static struct decoration name_decoration = { "object names" };
16+
static int decoration_loaded;
17+
static int decoration_flags;
1618

1719
static char decoration_colors[][COLOR_MAXLEN] = {
1820
GIT_COLOR_RESET,
@@ -146,9 +148,9 @@ static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
146148

147149
void load_ref_decorations(int flags)
148150
{
149-
static int loaded;
150-
if (!loaded) {
151-
loaded = 1;
151+
if (!decoration_loaded) {
152+
decoration_loaded = 1;
153+
decoration_flags = flags;
152154
for_each_ref(add_ref_decoration, &flags);
153155
head_ref(add_ref_decoration, &flags);
154156
for_each_commit_graft(add_graft_decoration, NULL);
@@ -196,8 +198,19 @@ static const struct name_decoration *current_pointed_by_HEAD(const struct name_d
196198
branch_name = resolve_ref_unsafe("HEAD", 0, unused, &rru_flags);
197199
if (!(rru_flags & REF_ISSYMREF))
198200
return NULL;
199-
if (!skip_prefix(branch_name, "refs/heads/", &branch_name))
200-
return NULL;
201+
202+
if ((decoration_flags == DECORATE_SHORT_REFS)) {
203+
if (!skip_prefix(branch_name, "refs/heads/", &branch_name))
204+
return NULL;
205+
} else {
206+
/*
207+
* Each decoration has a refname in full; keep
208+
* branch_name also in full, but still make sure
209+
* HEAD is a reasonable ref.
210+
*/
211+
if (!starts_with(branch_name, "refs/"))
212+
return NULL;
213+
}
201214

202215
/* OK, do we have that ref in the list? */
203216
for (list = decoration; list; list = list->next)

t/t4013/diff.log_--decorate=full_--all

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Date: Mon Jun 26 00:06:00 2006 +0000
55

66
Rearranged lines in dir/sub
77

8-
commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (HEAD, refs/heads/master)
8+
commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (HEAD -> refs/heads/master)
99
Merge: 9a6d494 c7a2ab9
1010
Author: A U Thor <[email protected]>
1111
Date: Mon Jun 26 00:04:00 2006 +0000

0 commit comments

Comments
 (0)