Skip to content

Commit ca135e7

Browse files
torvaldsJunio C Hamano
authored andcommitted
Add support for "commit name decorations" to log family of commands
This adds "--decorate" as a log option, which prints out the ref names of any commits that are shown. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent a59b276 commit ca135e7

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

builtin-log.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,43 @@
1313
#include "tag.h"
1414
#include "reflog-walk.h"
1515
#include "patch-ids.h"
16+
#include "refs.h"
1617

1718
static int default_show_root = 1;
1819

1920
/* this is in builtin-diff.c */
2021
void add_head(struct rev_info *revs);
2122

23+
static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
24+
{
25+
int plen = strlen(prefix);
26+
int nlen = strlen(name);
27+
struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
28+
memcpy(res->name, prefix, plen);
29+
memcpy(res->name + plen, name, nlen + 1);
30+
res->next = add_decoration(&name_decoration, obj, res);
31+
}
32+
33+
static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
34+
{
35+
struct object *obj = parse_object(sha1);
36+
if (!obj)
37+
return 0;
38+
add_name_decoration("", refname, obj);
39+
while (obj->type == OBJ_TAG) {
40+
obj = ((struct tag *)obj)->tagged;
41+
if (!obj)
42+
break;
43+
add_name_decoration("tag: ", refname, obj);
44+
}
45+
return 0;
46+
}
47+
2248
static void cmd_log_init(int argc, const char **argv, const char *prefix,
2349
struct rev_info *rev)
2450
{
2551
int i;
52+
int decorate = 0;
2653

2754
rev->abbrev = DEFAULT_ABBREV;
2855
rev->commit_format = CMIT_FMT_DEFAULT;
@@ -39,8 +66,11 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
3966
git_log_output_encoding = xstrdup(arg);
4067
else
4168
git_log_output_encoding = "";
42-
}
43-
else
69+
} else if (!strcmp(arg, "--decorate")) {
70+
if (!decorate)
71+
for_each_ref(add_ref_decoration, NULL);
72+
decorate = 1;
73+
} else
4474
die("unrecognized argument: %s", arg);
4575
}
4676
}

commit.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "object.h"
55
#include "tree.h"
6+
#include "decorate.h"
67

78
struct commit_list {
89
struct commit *item;
@@ -21,6 +22,13 @@ struct commit {
2122
extern int save_commit_buffer;
2223
extern const char *commit_type;
2324

25+
/* While we can decorate any object with a name, it's only used for commits.. */
26+
extern struct decoration name_decoration;
27+
struct name_decoration {
28+
struct name_decoration *next;
29+
char name[1];
30+
};
31+
2432
struct commit *lookup_commit(const unsigned char *sha1);
2533
struct commit *lookup_commit_reference(const unsigned char *sha1);
2634
struct commit *lookup_commit_reference_gently(const unsigned char *sha1,

log-tree.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "log-tree.h"
55
#include "reflog-walk.h"
66

7+
struct decoration name_decoration = { "object names" };
8+
79
static void show_parents(struct commit *commit, int abbrev)
810
{
911
struct commit_list *p;
@@ -13,6 +15,23 @@ static void show_parents(struct commit *commit, int abbrev)
1315
}
1416
}
1517

18+
static void show_decorations(struct commit *commit)
19+
{
20+
const char *prefix;
21+
struct name_decoration *decoration;
22+
23+
decoration = lookup_decoration(&name_decoration, &commit->object);
24+
if (!decoration)
25+
return;
26+
prefix = " (";
27+
while (decoration) {
28+
printf("%s%s", prefix, decoration->name);
29+
prefix = ", ";
30+
decoration = decoration->next;
31+
}
32+
putchar(')');
33+
}
34+
1635
/*
1736
* Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
1837
* Signed-off-by: and Acked-by: lines.
@@ -136,6 +155,7 @@ void show_log(struct rev_info *opt, const char *sep)
136155
fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
137156
if (opt->parents)
138157
show_parents(commit, abbrev_commit);
158+
show_decorations(commit);
139159
putchar(opt->diffopt.line_termination);
140160
return;
141161
}
@@ -240,6 +260,7 @@ void show_log(struct rev_info *opt, const char *sep)
240260
printf(" (from %s)",
241261
diff_unique_abbrev(parent->object.sha1,
242262
abbrev_commit));
263+
show_decorations(commit);
243264
printf("%s",
244265
diff_get_color(opt->diffopt.color_diff, DIFF_RESET));
245266
putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');

0 commit comments

Comments
 (0)