pretty: share code between format_decoration and show_decorations

This also adds color support to format_decorations() Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Apr 19, 2013 at 09:08 UTC 9d3f002f21f78576f3c31ae94ce78de54e305c95
4 files changed +39 -38
log-tree.c
+32 -16
@@ -175,36 +175,52 @@ static void show_children(struct rev_info *opt, struct commit *commit, int abbre
175 }
176 }
177
178 -void show_decorations(struct rev_info *opt, struct commit *commit)
178 +/*
179 + * The caller makes sure there is no funny color before
180 + * calling. format_decorations makes sure the same after return.
181 + */
182 +void format_decorations(struct strbuf *sb,
183 + const struct commit *commit,
184 + int use_color)
185 {
186 const char *prefix;
187 struct name_decoration *decoration;
188 const char *color_commit =
183 - diff_get_color_opt(&opt->diffopt, DIFF_COMMIT);
189 + diff_get_color(use_color, DIFF_COMMIT);
190 const char *color_reset =
185 - decorate_get_color_opt(&opt->diffopt, DECORATION_NONE);
191 + decorate_get_color(use_color, DECORATION_NONE);
192
187 - if (opt->show_source && commit->util)
188 - printf("\t%s", (char *) commit->util);
189 - if (!opt->show_decorations)
190 - return;
193 decoration = lookup_decoration(&name_decoration, &commit->object);
194 if (!decoration)
195 return;
196 prefix = " (";
197 while (decoration) {
196 - printf("%s", prefix);
197 - fputs(decorate_get_color_opt(&opt->diffopt, decoration->type),
198 - stdout);
198 + strbuf_addstr(sb, color_commit);
199 + strbuf_addstr(sb, prefix);
200 + strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
201 if (decoration->type == DECORATION_REF_TAG)
200 - fputs("tag: ", stdout);
201 - printf("%s", decoration->name);
202 - fputs(color_reset, stdout);
203 - fputs(color_commit, stdout);
202 + strbuf_addstr(sb, "tag: ");
203 + strbuf_addstr(sb, decoration->name);
204 + strbuf_addstr(sb, color_reset);
205 prefix = ", ";
206 decoration = decoration->next;
207 }
207 - putchar(')');
208 + strbuf_addstr(sb, color_commit);
209 + strbuf_addch(sb, ')');
210 + strbuf_addstr(sb, color_reset);
211 +}
212 +
213 +void show_decorations(struct rev_info *opt, struct commit *commit)
214 +{
215 + struct strbuf sb = STRBUF_INIT;
216 +
217 + if (opt->show_source && commit->util)
218 + printf("\t%s", (char *) commit->util);
219 + if (!opt->show_decorations)
220 + return;
221 + format_decorations(&sb, commit, opt->diffopt.use_color);
222 + fputs(sb.buf, stdout);
223 + strbuf_release(&sb);
224 }
225
226 static unsigned int digits_in_number(unsigned int number)
@@ -540,8 +556,8 @@ void show_log(struct rev_info *opt)
556 printf(" (from %s)",
557 find_unique_abbrev(parent->object.sha1,
558 abbrev_commit));
559 + fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), stdout);
560 show_decorations(opt, commit);
544 - printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
561 if (opt->commit_format == CMIT_FMT_ONELINE) {
562 putchar(' ');
563 } else {
log-tree.h
+1
@@ -13,6 +13,7 @@ int log_tree_diff_flush(struct rev_info *);
13 int log_tree_commit(struct rev_info *, struct commit *);
14 int log_tree_opt_parse(struct rev_info *, const char **, int);
15 void show_log(struct rev_info *opt);
16 +void format_decorations(struct strbuf *sb, const struct commit *commit, int use_color);
17 void show_decorations(struct rev_info *opt, struct commit *commit);
18 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
19 const char **subject_p,
pretty.c
+2 -18
@@ -908,23 +908,6 @@ static void parse_commit_message(struct format_commit_context *c)
908 c->commit_message_parsed = 1;
909 }
910
911 -static void format_decoration(struct strbuf *sb, const struct commit *commit)
912 -{
913 - struct name_decoration *d;
914 - const char *prefix = " (";
915 -
916 - load_ref_decorations(DECORATE_SHORT_REFS);
917 - d = lookup_decoration(&name_decoration, &commit->object);
918 - while (d) {
919 - strbuf_addstr(sb, prefix);
920 - prefix = ", ";
921 - strbuf_addstr(sb, d->name);
922 - d = d->next;
923 - }
924 - if (prefix[0] == ',')
925 - strbuf_addch(sb, ')');
926 -}
927 -
911 static void strbuf_wrap(struct strbuf *sb, size_t pos,
912 size_t width, size_t indent1, size_t indent2)
913 {
@@ -1103,7 +1086,8 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
1086 strbuf_addstr(sb, get_revision_mark(NULL, commit));
1087 return 1;
1088 case 'd':
1106 - format_decoration(sb, commit);
1089 + load_ref_decorations(DECORATE_SHORT_REFS);
1090 + format_decorations(sb, commit, 0);
1091 return 1;
1092 case 'g': /* reflog info */
1093 switch(placeholder[1]) {
t/t4207-log-decoration-colors.sh
+4 -4
@@ -44,15 +44,15 @@ test_expect_success setup '
44 '
45
46 cat >expected <<EOF
47 -${c_commit}COMMIT_ID (${c_HEAD}HEAD${c_reset}${c_commit},\
47 +${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_HEAD}HEAD${c_reset}${c_commit},\
48 ${c_tag}tag: v1.0${c_reset}${c_commit},\
49 ${c_tag}tag: B${c_reset}${c_commit},\
50 ${c_branch}master${c_reset}${c_commit})${c_reset} B
51 -${c_commit}COMMIT_ID (${c_tag}tag: A1${c_reset}${c_commit},\
51 +${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_tag}tag: A1${c_reset}${c_commit},\
52 ${c_remoteBranch}other/master${c_reset}${c_commit})${c_reset} A1
53 -${c_commit}COMMIT_ID (${c_stash}refs/stash${c_reset}${c_commit})${c_reset}\
53 +${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_stash}refs/stash${c_reset}${c_commit})${c_reset}\
54 On master: Changes to A.t
55 -${c_commit}COMMIT_ID (${c_tag}tag: A${c_reset}${c_commit})${c_reset} A
55 +${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_tag}tag: A${c_reset}${c_commit})${c_reset} A
56 EOF
57
58 # We want log to show all, but the second parent to refs/stash is irrelevant