Skip to content

Commit f26a001

Browse files
Kristian Høgsberggitster
authored andcommitted
Enable wt-status output to a given FILE pointer.
Still defaults to stdout, but you can now override wt_status.fp after calling wt_status_prepare(). Signed-off-by: Kristian Høgsberg <krh@redhat.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 6b30852 commit f26a001

File tree

4 files changed

+58
-52
lines changed

4 files changed

+58
-52
lines changed

color.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,39 +135,39 @@ int git_config_colorbool(const char *var, const char *value)
135135
return git_config_bool(var, value);
136136
}
137137

138-
static int color_vprintf(const char *color, const char *fmt,
138+
static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
139139
va_list args, const char *trail)
140140
{
141141
int r = 0;
142142

143143
if (*color)
144-
r += printf("%s", color);
145-
r += vprintf(fmt, args);
144+
r += fprintf(fp, "%s", color);
145+
r += vfprintf(fp, fmt, args);
146146
if (*color)
147-
r += printf("%s", COLOR_RESET);
147+
r += fprintf(fp, "%s", COLOR_RESET);
148148
if (trail)
149-
r += printf("%s", trail);
149+
r += fprintf(fp, "%s", trail);
150150
return r;
151151
}
152152

153153

154154

155-
int color_printf(const char *color, const char *fmt, ...)
155+
int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
156156
{
157157
va_list args;
158158
int r;
159159
va_start(args, fmt);
160-
r = color_vprintf(color, fmt, args, NULL);
160+
r = color_vfprintf(fp, color, fmt, args, NULL);
161161
va_end(args);
162162
return r;
163163
}
164164

165-
int color_printf_ln(const char *color, const char *fmt, ...)
165+
int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
166166
{
167167
va_list args;
168168
int r;
169169
va_start(args, fmt);
170-
r = color_vprintf(color, fmt, args, "\n");
170+
r = color_vfprintf(fp, color, fmt, args, "\n");
171171
va_end(args);
172172
return r;
173173
}

color.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
int git_config_colorbool(const char *var, const char *value);
88
void color_parse(const char *var, const char *value, char *dst);
9-
int color_printf(const char *color, const char *fmt, ...);
10-
int color_printf_ln(const char *color, const char *fmt, ...);
9+
int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
10+
int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...);
1111

1212
#endif /* COLOR_H */

wt-status.c

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -52,31 +52,33 @@ void wt_status_prepare(struct wt_status *s)
5252
head = resolve_ref("HEAD", sha1, 0, NULL);
5353
s->branch = head ? xstrdup(head) : NULL;
5454
s->reference = "HEAD";
55+
s->fp = stdout;
5556
}
5657

57-
static void wt_status_print_cached_header(const char *reference)
58+
static void wt_status_print_cached_header(struct wt_status *s)
5859
{
5960
const char *c = color(WT_STATUS_HEADER);
60-
color_printf_ln(c, "# Changes to be committed:");
61-
if (reference) {
62-
color_printf_ln(c, "# (use \"git reset %s <file>...\" to unstage)", reference);
61+
color_fprintf_ln(s->fp, c, "# Changes to be committed:");
62+
if (s->reference) {
63+
color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
6364
} else {
64-
color_printf_ln(c, "# (use \"git rm --cached <file>...\" to unstage)");
65+
color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
6566
}
66-
color_printf_ln(c, "#");
67+
color_fprintf_ln(s->fp, c, "#");
6768
}
6869

69-
static void wt_status_print_header(const char *main, const char *sub)
70+
static void wt_status_print_header(struct wt_status *s,
71+
const char *main, const char *sub)
7072
{
7173
const char *c = color(WT_STATUS_HEADER);
72-
color_printf_ln(c, "# %s:", main);
73-
color_printf_ln(c, "# (%s)", sub);
74-
color_printf_ln(c, "#");
74+
color_fprintf_ln(s->fp, c, "# %s:", main);
75+
color_fprintf_ln(s->fp, c, "# (%s)", sub);
76+
color_fprintf_ln(s->fp, c, "#");
7577
}
7678

77-
static void wt_status_print_trailer(void)
79+
static void wt_status_print_trailer(struct wt_status *s)
7880
{
79-
color_printf_ln(color(WT_STATUS_HEADER), "#");
81+
color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
8082
}
8183

8284
static const char *quote_crlf(const char *in, char *buf, size_t sz)
@@ -108,7 +110,8 @@ static const char *quote_crlf(const char *in, char *buf, size_t sz)
108110
return ret;
109111
}
110112

111-
static void wt_status_print_filepair(int t, struct diff_filepair *p)
113+
static void wt_status_print_filepair(struct wt_status *s,
114+
int t, struct diff_filepair *p)
112115
{
113116
const char *c = color(t);
114117
const char *one, *two;
@@ -117,36 +120,36 @@ static void wt_status_print_filepair(int t, struct diff_filepair *p)
117120
one = quote_crlf(p->one->path, onebuf, sizeof(onebuf));
118121
two = quote_crlf(p->two->path, twobuf, sizeof(twobuf));
119122

120-
color_printf(color(WT_STATUS_HEADER), "#\t");
123+
color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
121124
switch (p->status) {
122125
case DIFF_STATUS_ADDED:
123-
color_printf(c, "new file: %s", one);
126+
color_fprintf(s->fp, c, "new file: %s", one);
124127
break;
125128
case DIFF_STATUS_COPIED:
126-
color_printf(c, "copied: %s -> %s", one, two);
129+
color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
127130
break;
128131
case DIFF_STATUS_DELETED:
129-
color_printf(c, "deleted: %s", one);
132+
color_fprintf(s->fp, c, "deleted: %s", one);
130133
break;
131134
case DIFF_STATUS_MODIFIED:
132-
color_printf(c, "modified: %s", one);
135+
color_fprintf(s->fp, c, "modified: %s", one);
133136
break;
134137
case DIFF_STATUS_RENAMED:
135-
color_printf(c, "renamed: %s -> %s", one, two);
138+
color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
136139
break;
137140
case DIFF_STATUS_TYPE_CHANGED:
138-
color_printf(c, "typechange: %s", one);
141+
color_fprintf(s->fp, c, "typechange: %s", one);
139142
break;
140143
case DIFF_STATUS_UNKNOWN:
141-
color_printf(c, "unknown: %s", one);
144+
color_fprintf(s->fp, c, "unknown: %s", one);
142145
break;
143146
case DIFF_STATUS_UNMERGED:
144-
color_printf(c, "unmerged: %s", one);
147+
color_fprintf(s->fp, c, "unmerged: %s", one);
145148
break;
146149
default:
147150
die("bug: unhandled diff status %c", p->status);
148151
}
149-
printf("\n");
152+
fprintf(s->fp, "\n");
150153
}
151154

152155
static void wt_status_print_updated_cb(struct diff_queue_struct *q,
@@ -160,14 +163,14 @@ static void wt_status_print_updated_cb(struct diff_queue_struct *q,
160163
if (q->queue[i]->status == 'U')
161164
continue;
162165
if (!shown_header) {
163-
wt_status_print_cached_header(s->reference);
166+
wt_status_print_cached_header(s);
164167
s->commitable = 1;
165168
shown_header = 1;
166169
}
167-
wt_status_print_filepair(WT_STATUS_UPDATED, q->queue[i]);
170+
wt_status_print_filepair(s, WT_STATUS_UPDATED, q->queue[i]);
168171
}
169172
if (shown_header)
170-
wt_status_print_trailer();
173+
wt_status_print_trailer(s);
171174
}
172175

173176
static void wt_status_print_changed_cb(struct diff_queue_struct *q,
@@ -184,12 +187,12 @@ static void wt_status_print_changed_cb(struct diff_queue_struct *q,
184187
msg = use_add_rm_msg;
185188
break;
186189
}
187-
wt_status_print_header("Changed but not updated", msg);
190+
wt_status_print_header(s, "Changed but not updated", msg);
188191
}
189192
for (i = 0; i < q->nr; i++)
190-
wt_status_print_filepair(WT_STATUS_CHANGED, q->queue[i]);
193+
wt_status_print_filepair(s, WT_STATUS_CHANGED, q->queue[i]);
191194
if (q->nr)
192-
wt_status_print_trailer();
195+
wt_status_print_trailer(s);
193196
}
194197

195198
static void wt_read_cache(struct wt_status *s)
@@ -206,16 +209,16 @@ static void wt_status_print_initial(struct wt_status *s)
206209
wt_read_cache(s);
207210
if (active_nr) {
208211
s->commitable = 1;
209-
wt_status_print_cached_header(NULL);
212+
wt_status_print_cached_header(s);
210213
}
211214
for (i = 0; i < active_nr; i++) {
212-
color_printf(color(WT_STATUS_HEADER), "#\t");
213-
color_printf_ln(color(WT_STATUS_UPDATED), "new file: %s",
215+
color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
216+
color_fprintf_ln(s->fp, color(WT_STATUS_UPDATED), "new file: %s",
214217
quote_crlf(active_cache[i]->name,
215218
buf, sizeof(buf)));
216219
}
217220
if (active_nr)
218-
wt_status_print_trailer();
221+
wt_status_print_trailer(s);
219222
}
220223

221224
static void wt_status_print_updated(struct wt_status *s)
@@ -282,12 +285,12 @@ static void wt_status_print_untracked(struct wt_status *s)
282285
}
283286
if (!shown_header) {
284287
s->workdir_untracked = 1;
285-
wt_status_print_header("Untracked files",
288+
wt_status_print_header(s, "Untracked files",
286289
use_add_to_include_msg);
287290
shown_header = 1;
288291
}
289-
color_printf(color(WT_STATUS_HEADER), "#\t");
290-
color_printf_ln(color(WT_STATUS_UNTRACKED), "%.*s",
292+
color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
293+
color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%.*s",
291294
ent->len, ent->name);
292295
}
293296
}
@@ -317,14 +320,14 @@ void wt_status_print(struct wt_status *s)
317320
branch_name = "";
318321
on_what = "Not currently on any branch.";
319322
}
320-
color_printf_ln(color(WT_STATUS_HEADER),
323+
color_fprintf_ln(s->fp, color(WT_STATUS_HEADER),
321324
"# %s%s", on_what, branch_name);
322325
}
323326

324327
if (s->is_initial) {
325-
color_printf_ln(color(WT_STATUS_HEADER), "#");
326-
color_printf_ln(color(WT_STATUS_HEADER), "# Initial commit");
327-
color_printf_ln(color(WT_STATUS_HEADER), "#");
328+
color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
329+
color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "# Initial commit");
330+
color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
328331
wt_status_print_initial(s);
329332
}
330333
else {
@@ -338,7 +341,7 @@ void wt_status_print(struct wt_status *s)
338341
wt_status_print_verbose(s);
339342
if (!s->commitable) {
340343
if (s->amend)
341-
printf("# No changes\n");
344+
fprintf(s->fp, "# No changes\n");
342345
else if (s->workdir_dirty)
343346
printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
344347
else if (s->workdir_untracked)

wt-status.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef STATUS_H
22
#define STATUS_H
33

4+
#include <stdio.h>
5+
46
enum color_wt_status {
57
WT_STATUS_HEADER,
68
WT_STATUS_UPDATED,
@@ -19,6 +21,7 @@ struct wt_status {
1921
int commitable;
2022
int workdir_dirty;
2123
int workdir_untracked;
24+
FILE *fp;
2225
};
2326

2427
int git_status_config(const char *var, const char *value);

0 commit comments

Comments
 (0)