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>
Linus Torvalds committed
Apr 16, 2007 at 16:05 UTC
ca135e7acc06f7d24ead732d2a1a531428da7135
3 files changed
+61
-2
builtin-log.c
+32
-2
@@ -13,16 +13,43 @@
13
#include "tag.h"
14
#include "reflog-walk.h"
15
#include "patch-ids.h"
16
+#include "refs.h"
17
18
static int default_show_root = 1;
19
20
/* this is in builtin-diff.c */
21
void add_head(struct rev_info *revs);
22
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
+
48
static void cmd_log_init(int argc, const char **argv, const char *prefix,
49
struct rev_info *rev)
50
{
51
int i;
52
+ int decorate = 0;
53
54
rev->abbrev = DEFAULT_ABBREV;
55
rev->commit_format = CMIT_FMT_DEFAULT;
@@ -39,8 +66,11 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
66
git_log_output_encoding = xstrdup(arg);
67
else
68
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
74
die("unrecognized argument: %s", arg);
75
}
76
}
commit.h
+8
@@ -3,6 +3,7 @@
3
4
#include "object.h"
5
#include "tree.h"
6
+#include "decorate.h"
7
8
struct commit_list {
9
struct commit *item;
@@ -21,6 +22,13 @@ struct commit {
22
extern int save_commit_buffer;
23
extern const char *commit_type;
24
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
+
32
struct commit *lookup_commit(const unsigned char *sha1);
33
struct commit *lookup_commit_reference(const unsigned char *sha1);
34
struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
log-tree.c
+21
@@ -4,6 +4,8 @@
4
#include "log-tree.h"
5
#include "reflog-walk.h"
6
7
+struct decoration name_decoration = { "object names" };
8
+
9
static void show_parents(struct commit *commit, int abbrev)
10
{
11
struct commit_list *p;
@@ -13,6 +15,23 @@ static void show_parents(struct commit *commit, int abbrev)
15
}
16
}
17
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
+
35
/*
36
* Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
37
* Signed-off-by: and Acked-by: lines.
@@ -136,6 +155,7 @@ void show_log(struct rev_info *opt, const char *sep)
155
fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
156
if (opt->parents)
157
show_parents(commit, abbrev_commit);
158
+ show_decorations(commit);
159
putchar(opt->diffopt.line_termination);
160
return;
161
}
@@ -240,6 +260,7 @@ void show_log(struct rev_info *opt, const char *sep)
260
printf(" (from %s)",
261
diff_unique_abbrev(parent->object.sha1,
262
abbrev_commit));
263
+ show_decorations(commit);
264
printf("%s",
265
diff_get_color(opt->diffopt.color_diff, DIFF_RESET));
266
putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');