Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 9d3f002

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d2ea4af commit 9d3f002

File tree

4 files changed

+39
-38
lines changed

4 files changed

+39
-38
lines changed

log-tree.c

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -175,36 +175,52 @@ static void show_children(struct rev_info *opt, struct commit *commit, int abbre
175175
}
176176
}
177177

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)
179185
{
180186
const char *prefix;
181187
struct name_decoration *decoration;
182188
const char *color_commit =
183-
diff_get_color_opt(&opt->diffopt, DIFF_COMMIT);
189+
diff_get_color(use_color, DIFF_COMMIT);
184190
const char *color_reset =
185-
decorate_get_color_opt(&opt->diffopt, DECORATION_NONE);
191+
decorate_get_color(use_color, DECORATION_NONE);
186192

187-
if (opt->show_source && commit->util)
188-
printf("\t%s", (char *) commit->util);
189-
if (!opt->show_decorations)
190-
return;
191193
decoration = lookup_decoration(&name_decoration, &commit->object);
192194
if (!decoration)
193195
return;
194196
prefix = " (";
195197
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));
199201
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);
204205
prefix = ", ";
205206
decoration = decoration->next;
206207
}
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);
208224
}
209225

210226
static unsigned int digits_in_number(unsigned int number)
@@ -540,8 +556,8 @@ void show_log(struct rev_info *opt)
540556
printf(" (from %s)",
541557
find_unique_abbrev(parent->object.sha1,
542558
abbrev_commit));
559+
fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), stdout);
543560
show_decorations(opt, commit);
544-
printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
545561
if (opt->commit_format == CMIT_FMT_ONELINE) {
546562
putchar(' ');
547563
} else {

log-tree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ int log_tree_diff_flush(struct rev_info *);
1313
int log_tree_commit(struct rev_info *, struct commit *);
1414
int log_tree_opt_parse(struct rev_info *, const char **, int);
1515
void show_log(struct rev_info *opt);
16+
void format_decorations(struct strbuf *sb, const struct commit *commit, int use_color);
1617
void show_decorations(struct rev_info *opt, struct commit *commit);
1718
void log_write_email_headers(struct rev_info *opt, struct commit *commit,
1819
const char **subject_p,

pretty.c

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -908,23 +908,6 @@ static void parse_commit_message(struct format_commit_context *c)
908908
c->commit_message_parsed = 1;
909909
}
910910

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-
928911
static void strbuf_wrap(struct strbuf *sb, size_t pos,
929912
size_t width, size_t indent1, size_t indent2)
930913
{
@@ -1103,7 +1086,8 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
11031086
strbuf_addstr(sb, get_revision_mark(NULL, commit));
11041087
return 1;
11051088
case 'd':
1106-
format_decoration(sb, commit);
1089+
load_ref_decorations(DECORATE_SHORT_REFS);
1090+
format_decorations(sb, commit, 0);
11071091
return 1;
11081092
case 'g': /* reflog info */
11091093
switch(placeholder[1]) {

t/t4207-log-decoration-colors.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ test_expect_success setup '
4444
'
4545

4646
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},\
4848
${c_tag}tag: v1.0${c_reset}${c_commit},\
4949
${c_tag}tag: B${c_reset}${c_commit},\
5050
${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},\
5252
${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}\
5454
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
5656
EOF
5757

5858
# We want log to show all, but the second parent to refs/stash is irrelevant

0 commit comments

Comments
 (0)