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>
Kristian Høgsberg committed
Sep 17, 2007 at 20:06 UTC
f26a0012262106e0c7e92d8d07f00611112d3a8d
4 files changed
+58
-52
color.c
+9
-9
@@ -135,39 +135,39 @@ int git_config_colorbool(const char *var, const char *value)
135
return git_config_bool(var, value);
136
}
137
138
-static int color_vprintf(const char *color, const char *fmt,
138
+static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
139
va_list args, const char *trail)
140
{
141
int r = 0;
142
143
if (*color)
144
- r += printf("%s", color);
145
- r += vprintf(fmt, args);
144
+ r += fprintf(fp, "%s", color);
145
+ r += vfprintf(fp, fmt, args);
146
if (*color)
147
- r += printf("%s", COLOR_RESET);
147
+ r += fprintf(fp, "%s", COLOR_RESET);
148
if (trail)
149
- r += printf("%s", trail);
149
+ r += fprintf(fp, "%s", trail);
150
return r;
151
}
152
153
154
155
-int color_printf(const char *color, const char *fmt, ...)
155
+int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
156
{
157
va_list args;
158
int r;
159
va_start(args, fmt);
160
- r = color_vprintf(color, fmt, args, NULL);
160
+ r = color_vfprintf(fp, color, fmt, args, NULL);
161
va_end(args);
162
return r;
163
}
164
165
-int color_printf_ln(const char *color, const char *fmt, ...)
165
+int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
166
{
167
va_list args;
168
int r;
169
va_start(args, fmt);
170
- r = color_vprintf(color, fmt, args, "\n");
170
+ r = color_vfprintf(fp, color, fmt, args, "\n");
171
va_end(args);
172
return r;
173
}
color.h
+2
-2
@@ -6,7 +6,7 @@
6
7
int git_config_colorbool(const char *var, const char *value);
8
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, ...);
11
12
#endif /* COLOR_H */
wt-status.c
+44
-41
@@ -52,31 +52,33 @@ void wt_status_prepare(struct wt_status *s)
52
head = resolve_ref("HEAD", sha1, 0, NULL);
53
s->branch = head ? xstrdup(head) : NULL;
54
s->reference = "HEAD";
55
+ s->fp = stdout;
56
}
57
57
-static void wt_status_print_cached_header(const char *reference)
58
+static void wt_status_print_cached_header(struct wt_status *s)
59
{
60
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);
64
} 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)");
66
}
66
- color_printf_ln(c, "#");
67
+ color_fprintf_ln(s->fp, c, "#");
68
}
69
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)
72
{
73
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, "#");
77
}
78
77
-static void wt_status_print_trailer(void)
79
+static void wt_status_print_trailer(struct wt_status *s)
80
{
79
- color_printf_ln(color(WT_STATUS_HEADER), "#");
81
+ color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
82
}
83
84
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)
110
return ret;
111
}
112
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)
115
{
116
const char *c = color(t);
117
const char *one, *two;
@@ -117,36 +120,36 @@ static void wt_status_print_filepair(int t, struct diff_filepair *p)
120
one = quote_crlf(p->one->path, onebuf, sizeof(onebuf));
121
two = quote_crlf(p->two->path, twobuf, sizeof(twobuf));
122
120
- color_printf(color(WT_STATUS_HEADER), "#\t");
123
+ color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
124
switch (p->status) {
125
case DIFF_STATUS_ADDED:
123
- color_printf(c, "new file: %s", one);
126
+ color_fprintf(s->fp, c, "new file: %s", one);
127
break;
128
case DIFF_STATUS_COPIED:
126
- color_printf(c, "copied: %s -> %s", one, two);
129
+ color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
130
break;
131
case DIFF_STATUS_DELETED:
129
- color_printf(c, "deleted: %s", one);
132
+ color_fprintf(s->fp, c, "deleted: %s", one);
133
break;
134
case DIFF_STATUS_MODIFIED:
132
- color_printf(c, "modified: %s", one);
135
+ color_fprintf(s->fp, c, "modified: %s", one);
136
break;
137
case DIFF_STATUS_RENAMED:
135
- color_printf(c, "renamed: %s -> %s", one, two);
138
+ color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
139
break;
140
case DIFF_STATUS_TYPE_CHANGED:
138
- color_printf(c, "typechange: %s", one);
141
+ color_fprintf(s->fp, c, "typechange: %s", one);
142
break;
143
case DIFF_STATUS_UNKNOWN:
141
- color_printf(c, "unknown: %s", one);
144
+ color_fprintf(s->fp, c, "unknown: %s", one);
145
break;
146
case DIFF_STATUS_UNMERGED:
144
- color_printf(c, "unmerged: %s", one);
147
+ color_fprintf(s->fp, c, "unmerged: %s", one);
148
break;
149
default:
150
die("bug: unhandled diff status %c", p->status);
151
}
149
- printf("\n");
152
+ fprintf(s->fp, "\n");
153
}
154
155
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,
163
if (q->queue[i]->status == 'U')
164
continue;
165
if (!shown_header) {
163
- wt_status_print_cached_header(s->reference);
166
+ wt_status_print_cached_header(s);
167
s->commitable = 1;
168
shown_header = 1;
169
}
167
- wt_status_print_filepair(WT_STATUS_UPDATED, q->queue[i]);
170
+ wt_status_print_filepair(s, WT_STATUS_UPDATED, q->queue[i]);
171
}
172
if (shown_header)
170
- wt_status_print_trailer();
173
+ wt_status_print_trailer(s);
174
}
175
176
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,
187
msg = use_add_rm_msg;
188
break;
189
}
187
- wt_status_print_header("Changed but not updated", msg);
190
+ wt_status_print_header(s, "Changed but not updated", msg);
191
}
192
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]);
194
if (q->nr)
192
- wt_status_print_trailer();
195
+ wt_status_print_trailer(s);
196
}
197
198
static void wt_read_cache(struct wt_status *s)
@@ -206,16 +209,16 @@ static void wt_status_print_initial(struct wt_status *s)
209
wt_read_cache(s);
210
if (active_nr) {
211
s->commitable = 1;
209
- wt_status_print_cached_header(NULL);
212
+ wt_status_print_cached_header(s);
213
}
214
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",
217
quote_crlf(active_cache[i]->name,
218
buf, sizeof(buf)));
219
}
220
if (active_nr)
218
- wt_status_print_trailer();
221
+ wt_status_print_trailer(s);
222
}
223
224
static void wt_status_print_updated(struct wt_status *s)
@@ -282,12 +285,12 @@ static void wt_status_print_untracked(struct wt_status *s)
285
}
286
if (!shown_header) {
287
s->workdir_untracked = 1;
285
- wt_status_print_header("Untracked files",
288
+ wt_status_print_header(s, "Untracked files",
289
use_add_to_include_msg);
290
shown_header = 1;
291
}
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",
294
ent->len, ent->name);
295
}
296
}
@@ -317,14 +320,14 @@ void wt_status_print(struct wt_status *s)
320
branch_name = "";
321
on_what = "Not currently on any branch.";
322
}
320
- color_printf_ln(color(WT_STATUS_HEADER),
323
+ color_fprintf_ln(s->fp, color(WT_STATUS_HEADER),
324
"# %s%s", on_what, branch_name);
325
}
326
327
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), "#");
331
wt_status_print_initial(s);
332
}
333
else {
@@ -338,7 +341,7 @@ void wt_status_print(struct wt_status *s)
341
wt_status_print_verbose(s);
342
if (!s->commitable) {
343
if (s->amend)
341
- printf("# No changes\n");
344
+ fprintf(s->fp, "# No changes\n");
345
else if (s->workdir_dirty)
346
printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
347
else if (s->workdir_untracked)
wt-status.h
+3
@@ -1,6 +1,8 @@
1
#ifndef STATUS_H
2
#define STATUS_H
3
4
+#include <stdio.h>
5
+
6
enum color_wt_status {
7
WT_STATUS_HEADER,
8
WT_STATUS_UPDATED,
@@ -19,6 +21,7 @@ struct wt_status {
21
int commitable;
22
int workdir_dirty;
23
int workdir_untracked;
24
+ FILE *fp;
25
};
26
27
int git_status_config(const char *var, const char *value);