Skip to content

Commit a1e3c2c

Browse files
committed
Merge branch 'rs/decorate'
* rs/decorate: add '%d' pretty format specifier to show decoration move load_ref_decorations() to log-tree.c and export it log: add load_ref_decorations()
2 parents b805ef0 + 3b3d443 commit a1e3c2c

File tree

5 files changed

+60
-28
lines changed

5 files changed

+60
-28
lines changed

Documentation/pretty-formats.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ The placeholders are:
116116
- '%cr': committer date, relative
117117
- '%ct': committer date, UNIX timestamp
118118
- '%ci': committer date, ISO 8601 format
119+
- '%d': ref names, like the --decorate option of linkgit:git-log[1]
119120
- '%e': encoding
120121
- '%s': subject
121122
- '%b': body

builtin-log.c

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "tag.h"
1515
#include "reflog-walk.h"
1616
#include "patch-ids.h"
17-
#include "refs.h"
1817
#include "run-command.h"
1918
#include "shortlog.h"
2019

@@ -25,31 +24,6 @@ static int default_show_root = 1;
2524
static const char *fmt_patch_subject_prefix = "PATCH";
2625
static const char *fmt_pretty;
2726

28-
static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
29-
{
30-
int plen = strlen(prefix);
31-
int nlen = strlen(name);
32-
struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
33-
memcpy(res->name, prefix, plen);
34-
memcpy(res->name + plen, name, nlen + 1);
35-
res->next = add_decoration(&name_decoration, obj, res);
36-
}
37-
38-
static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
39-
{
40-
struct object *obj = parse_object(sha1);
41-
if (!obj)
42-
return 0;
43-
add_name_decoration("", refname, obj);
44-
while (obj->type == OBJ_TAG) {
45-
obj = ((struct tag *)obj)->tagged;
46-
if (!obj)
47-
break;
48-
add_name_decoration("tag: ", refname, obj);
49-
}
50-
return 0;
51-
}
52-
5327
static void cmd_log_init(int argc, const char **argv, const char *prefix,
5428
struct rev_info *rev)
5529
{
@@ -80,8 +54,7 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
8054
for (i = 1; i < argc; i++) {
8155
const char *arg = argv[i];
8256
if (!strcmp(arg, "--decorate")) {
83-
if (!decorate)
84-
for_each_ref(add_ref_decoration, NULL);
57+
load_ref_decorations();
8558
decorate = 1;
8659
} else
8760
die("unrecognized argument: %s", arg);

log-tree.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,48 @@
11
#include "cache.h"
22
#include "diff.h"
33
#include "commit.h"
4+
#include "tag.h"
45
#include "graph.h"
56
#include "log-tree.h"
67
#include "reflog-walk.h"
8+
#include "refs.h"
79

810
struct decoration name_decoration = { "object names" };
911

12+
static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
13+
{
14+
int plen = strlen(prefix);
15+
int nlen = strlen(name);
16+
struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
17+
memcpy(res->name, prefix, plen);
18+
memcpy(res->name + plen, name, nlen + 1);
19+
res->next = add_decoration(&name_decoration, obj, res);
20+
}
21+
22+
static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
23+
{
24+
struct object *obj = parse_object(sha1);
25+
if (!obj)
26+
return 0;
27+
add_name_decoration("", refname, obj);
28+
while (obj->type == OBJ_TAG) {
29+
obj = ((struct tag *)obj)->tagged;
30+
if (!obj)
31+
break;
32+
add_name_decoration("tag: ", refname, obj);
33+
}
34+
return 0;
35+
}
36+
37+
void load_ref_decorations(void)
38+
{
39+
static int loaded;
40+
if (!loaded) {
41+
loaded = 1;
42+
for_each_ref(add_ref_decoration, NULL);
43+
}
44+
}
45+
1046
static void show_parents(struct commit *commit, int abbrev)
1147
{
1248
struct commit_list *p;

log-tree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ void log_write_email_headers(struct rev_info *opt, const char *name,
1717
const char **subject_p,
1818
const char **extra_headers_p,
1919
int *need_8bit_cte_p);
20+
void load_ref_decorations(void);
2021

2122
#endif

pretty.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "revision.h"
66
#include "string-list.h"
77
#include "mailmap.h"
8+
#include "log-tree.h"
89

910
static char *user_format;
1011

@@ -481,6 +482,23 @@ static void parse_commit_header(struct format_commit_context *context)
481482
context->commit_header_parsed = 1;
482483
}
483484

485+
static void format_decoration(struct strbuf *sb, const struct commit *commit)
486+
{
487+
struct name_decoration *d;
488+
const char *prefix = " (";
489+
490+
load_ref_decorations();
491+
d = lookup_decoration(&name_decoration, &commit->object);
492+
while (d) {
493+
strbuf_addstr(sb, prefix);
494+
prefix = ", ";
495+
strbuf_addstr(sb, d->name);
496+
d = d->next;
497+
}
498+
if (prefix[0] == ',')
499+
strbuf_addch(sb, ')');
500+
}
501+
484502
static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
485503
void *context)
486504
{
@@ -573,6 +591,9 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
573591
? '<'
574592
: '>');
575593
return 1;
594+
case 'd':
595+
format_decoration(sb, commit);
596+
return 1;
576597
}
577598

578599
/* For the rest we have to parse the commit header. */

0 commit comments

Comments
 (0)