status: cleanup API to wt_status_print

Refactor the API between builtin/commit.c and wt-status.[ch]. Hide the details of the various wt_*status_print() routines inside wt-status.c behind a single (new) wt_status_print() routine. Eliminate the switch statements from builtin/commit.c. Allow details of new status formats to be isolated within wt-status.c Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff Hostetler committed Aug 5, 2016 at 18:00 UTC be7e795efe35d3e50199af5452b218e5121b1713
3 files changed +43 -49
builtin/commit.c
+9 -42
@@ -142,14 +142,7 @@ static int show_ignored_in_status, have_option_m;
142 static const char *only_include_assumed;
143 static struct strbuf message = STRBUF_INIT;
144
145 -static enum status_format {
146 - STATUS_FORMAT_NONE = 0,
147 - STATUS_FORMAT_LONG,
148 - STATUS_FORMAT_SHORT,
149 - STATUS_FORMAT_PORCELAIN,
150 -
151 - STATUS_FORMAT_UNSPECIFIED
152 -} status_format = STATUS_FORMAT_UNSPECIFIED;
145 +static enum wt_status_format status_format = STATUS_FORMAT_UNSPECIFIED;
146
147 static int opt_parse_m(const struct option *opt, const char *arg, int unset)
148 {
@@ -500,24 +493,11 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
493 s->fp = fp;
494 s->nowarn = nowarn;
495 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
496 + s->status_format = status_format;
497 + s->ignore_submodule_arg = ignore_submodule_arg;
498
499 wt_status_collect(s);
505 -
506 - switch (status_format) {
507 - case STATUS_FORMAT_SHORT:
508 - wt_shortstatus_print(s);
509 - break;
510 - case STATUS_FORMAT_PORCELAIN:
511 - wt_porcelain_print(s);
512 - break;
513 - case STATUS_FORMAT_UNSPECIFIED:
514 - die("BUG: finalize_deferred_config() should have been called");
515 - break;
516 - case STATUS_FORMAT_NONE:
517 - case STATUS_FORMAT_LONG:
518 - wt_longstatus_print(s);
519 - break;
520 - }
500 + wt_status_print(s);
501
502 return s->commitable;
503 }
@@ -1099,7 +1079,7 @@ static const char *read_commit_message(const char *name)
1079 * is not in effect here.
1080 */
1081 static struct status_deferred_config {
1102 - enum status_format status_format;
1082 + enum wt_status_format status_format;
1083 int show_branch;
1084 } status_deferred_config = {
1085 STATUS_FORMAT_UNSPECIFIED,
@@ -1381,6 +1361,9 @@ int cmd_status(int argc, const char **argv, const char *prefix)
1361
1362 s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;
1363 s.ignore_submodule_arg = ignore_submodule_arg;
1364 + s.status_format = status_format;
1365 + s.verbose = verbose;
1366 +
1367 wt_status_collect(&s);
1368
1369 if (0 <= fd)
@@ -1389,23 +1372,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
1372 if (s.relative_paths)
1373 s.prefix = prefix;
1374
1392 - switch (status_format) {
1393 - case STATUS_FORMAT_SHORT:
1394 - wt_shortstatus_print(&s);
1395 - break;
1396 - case STATUS_FORMAT_PORCELAIN:
1397 - wt_porcelain_print(&s);
1398 - break;
1399 - case STATUS_FORMAT_UNSPECIFIED:
1400 - die("BUG: finalize_deferred_config() should have been called");
1401 - break;
1402 - case STATUS_FORMAT_NONE:
1403 - case STATUS_FORMAT_LONG:
1404 - s.verbose = verbose;
1405 - s.ignore_submodule_arg = ignore_submodule_arg;
1406 - wt_longstatus_print(&s);
1407 - break;
1408 - }
1375 + wt_status_print(&s);
1376 return 0;
1377 }
1378
wt-status.c
+22 -3
@@ -1447,7 +1447,7 @@ static void wt_longstatus_print_state(struct wt_status *s,
1447 show_bisect_in_progress(s, state, state_color);
1448 }
1449
1450 -void wt_longstatus_print(struct wt_status *s)
1450 +static void wt_longstatus_print(struct wt_status *s)
1451 {
1452 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1453 const char *branch_status_color = color(WT_STATUS_HEADER, s);
@@ -1714,7 +1714,7 @@ static void wt_shortstatus_print_tracking(struct wt_status *s)
1714 fputc(s->null_termination ? '\0' : '\n', s->fp);
1715 }
1716
1717 -void wt_shortstatus_print(struct wt_status *s)
1717 +static void wt_shortstatus_print(struct wt_status *s)
1718 {
1719 int i;
1720
@@ -1746,7 +1746,7 @@ void wt_shortstatus_print(struct wt_status *s)
1746 }
1747 }
1748
1749 -void wt_porcelain_print(struct wt_status *s)
1749 +static void wt_porcelain_print(struct wt_status *s)
1750 {
1751 s->use_color = 0;
1752 s->relative_paths = 0;
@@ -1754,3 +1754,22 @@ void wt_porcelain_print(struct wt_status *s)
1754 s->no_gettext = 1;
1755 wt_shortstatus_print(s);
1756 }
1757 +
1758 +void wt_status_print(struct wt_status *s)
1759 +{
1760 + switch (s->status_format) {
1761 + case STATUS_FORMAT_SHORT:
1762 + wt_shortstatus_print(s);
1763 + break;
1764 + case STATUS_FORMAT_PORCELAIN:
1765 + wt_porcelain_print(s);
1766 + break;
1767 + case STATUS_FORMAT_UNSPECIFIED:
1768 + die("BUG: finalize_deferred_config() should have been called");
1769 + break;
1770 + case STATUS_FORMAT_NONE:
1771 + case STATUS_FORMAT_LONG:
1772 + wt_longstatus_print(s);
1773 + break;
1774 + }
1775 +}
wt-status.h
+12 -4
@@ -43,6 +43,15 @@ struct wt_status_change_data {
43 unsigned new_submodule_commits : 1;
44 };
45
46 +enum wt_status_format {
47 + STATUS_FORMAT_NONE = 0,
48 + STATUS_FORMAT_LONG,
49 + STATUS_FORMAT_SHORT,
50 + STATUS_FORMAT_PORCELAIN,
51 +
52 + STATUS_FORMAT_UNSPECIFIED
53 +};
54 +
55 struct wt_status {
56 int is_initial;
57 char *branch;
@@ -66,6 +75,8 @@ struct wt_status {
75 int show_branch;
76 int hints;
77
78 + enum wt_status_format status_format;
79 +
80 /* These are computed during processing of the individual sections */
81 int commitable;
82 int workdir_dirty;
@@ -99,6 +110,7 @@ struct wt_status_state {
110 void wt_status_truncate_message_at_cut_line(struct strbuf *);
111 void wt_status_add_cut_line(FILE *fp);
112 void wt_status_prepare(struct wt_status *s);
113 +void wt_status_print(struct wt_status *s);
114 void wt_status_collect(struct wt_status *s);
115 void wt_status_get_state(struct wt_status_state *state, int get_detached_from);
116 int wt_status_check_rebase(const struct worktree *wt,
@@ -106,10 +118,6 @@ int wt_status_check_rebase(const struct worktree *wt,
118 int wt_status_check_bisect(const struct worktree *wt,
119 struct wt_status_state *state);
120
109 -void wt_longstatus_print(struct wt_status *s);
110 -void wt_shortstatus_print(struct wt_status *s);
111 -void wt_porcelain_print(struct wt_status *s);
112 -
121 __attribute__((format (printf, 3, 4)))
122 void status_printf_ln(struct wt_status *s, const char *color, const char *fmt, ...);
123 __attribute__((format (printf, 3, 4)))