builtin-commit: refactor short-status code into wt-status.c
[gitweb.git] / builtin-commit.c
index aa4a3587990c73a2359f33aaab10529789a9e58a..8411236fda1f74a6b5aa861ce81775c49290e4d2 100644 (file)
@@ -72,6 +72,13 @@ static int use_editor = 1, initial_commit, in_merge;
 static const char *only_include_assumed;
 static struct strbuf message;
 
+static int null_termination;
+static enum {
+       STATUS_FORMAT_LONG,
+       STATUS_FORMAT_SHORT,
+       STATUS_FORMAT_PORCELAIN,
+} status_format = STATUS_FORMAT_LONG;
+
 static int opt_parse_m(const struct option *opt, const char *arg, int unset)
 {
        struct strbuf *buf = opt->value;
@@ -105,6 +112,12 @@ static struct option builtin_commit_options[] = {
        OPT_BOOLEAN('o', "only", &only, "commit only specified files"),
        OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"),
        OPT_BOOLEAN(0, "dry-run", &dry_run, "show what would be committed"),
+       OPT_SET_INT(0, "short", &status_format, "show status concisely",
+                   STATUS_FORMAT_SHORT),
+       OPT_SET_INT(0, "porcelain", &status_format,
+                   "show porcelain output format", STATUS_FORMAT_PORCELAIN),
+       OPT_BOOLEAN('z', "null", &null_termination,
+                   "terminate entries with NUL"),
        OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
        { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, "mode", "show untracked files, optional modes: all, normal, no. (Default: all)", PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
        OPT_BOOLEAN(0, "allow-empty", &allow_empty, "ok to record an empty change"),
@@ -363,7 +376,18 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
        s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
 
        wt_status_collect(s);
-       wt_status_print(s);
+
+       switch (status_format) {
+       case STATUS_FORMAT_SHORT:
+               wt_shortstatus_print(s, null_termination);
+               break;
+       case STATUS_FORMAT_PORCELAIN:
+               wt_shortstatus_print(s, null_termination);
+               break;
+       case STATUS_FORMAT_LONG:
+               wt_status_print(s);
+               break;
+       }
 
        return s->commitable;
 }
@@ -821,6 +845,11 @@ static int parse_and_validate_options(int argc, const char *argv[],
        else if (interactive && argc > 0)
                die("Paths with --interactive does not make sense.");
 
+       if (null_termination && status_format == STATUS_FORMAT_LONG)
+               status_format = STATUS_FORMAT_PORCELAIN;
+       if (status_format != STATUS_FORMAT_LONG)
+               dry_run = 1;
+
        return argc;
 }
 
@@ -897,110 +926,17 @@ static int git_status_config(const char *k, const char *v, void *cb)
        return git_diff_ui_config(k, v, NULL);
 }
 
-#define quote_path quote_path_relative
-
-static void short_unmerged(int null_termination, struct string_list_item *it,
-                          struct wt_status *s)
-{
-       struct wt_status_change_data *d = it->util;
-       const char *how = "??";
-
-       switch (d->stagemask) {
-       case 1: how = "DD"; break; /* both deleted */
-       case 2: how = "AU"; break; /* added by us */
-       case 3: how = "UD"; break; /* deleted by them */
-       case 4: how = "UA"; break; /* added by them */
-       case 5: how = "DU"; break; /* deleted by us */
-       case 6: how = "AA"; break; /* both added */
-       case 7: how = "UU"; break; /* both modified */
-       }
-       printf("%s ", how);
-       if (null_termination) {
-               fprintf(stdout, "%s%c", it->string, 0);
-       } else {
-               struct strbuf onebuf = STRBUF_INIT;
-               const char *one;
-               one = quote_path(it->string, -1, &onebuf, s->prefix);
-               printf("%s\n", one);
-               strbuf_release(&onebuf);
-       }
-}
-
-static void short_status(int null_termination, struct string_list_item *it,
-                        struct wt_status *s)
-{
-       struct wt_status_change_data *d = it->util;
-
-       printf("%c%c ",
-              !d->index_status ? ' ' : d->index_status,
-              !d->worktree_status ? ' ' : d->worktree_status);
-       if (null_termination) {
-               fprintf(stdout, "%s%c", it->string, 0);
-               if (d->head_path)
-                       fprintf(stdout, "%s%c", d->head_path, 0);
-       } else {
-               struct strbuf onebuf = STRBUF_INIT;
-               const char *one;
-               if (d->head_path) {
-                       one = quote_path(d->head_path, -1, &onebuf, s->prefix);
-                       printf("%s -> ", one);
-                       strbuf_release(&onebuf);
-               }
-               one = quote_path(it->string, -1, &onebuf, s->prefix);
-               printf("%s\n", one);
-               strbuf_release(&onebuf);
-       }
-}
-
-static void short_untracked(int null_termination, struct string_list_item *it,
-                           struct wt_status *s)
-{
-       if (null_termination) {
-               fprintf(stdout, "?? %s%c", it->string, 0);
-       } else {
-               struct strbuf onebuf = STRBUF_INIT;
-               const char *one;
-               one = quote_path(it->string, -1, &onebuf, s->prefix);
-               printf("?? %s\n", one);
-               strbuf_release(&onebuf);
-       }
-}
-
-static void short_print(struct wt_status *s, int null_termination)
-{
-       int i;
-       for (i = 0; i < s->change.nr; i++) {
-               struct wt_status_change_data *d;
-               struct string_list_item *it;
-
-               it = &(s->change.items[i]);
-               d = it->util;
-               if (d->stagemask)
-                       short_unmerged(null_termination, it, s);
-               else
-                       short_status(null_termination, it, s);
-       }
-       for (i = 0; i < s->untracked.nr; i++) {
-               struct string_list_item *it;
-
-               it = &(s->untracked.items[i]);
-               short_untracked(null_termination, it, s);
-       }
-}
-
 int cmd_status(int argc, const char **argv, const char *prefix)
 {
        struct wt_status s;
-       static int null_termination;
-       static enum {
-               STATUS_FORMAT_LONG,
-               STATUS_FORMAT_SHORT,
-       } status_format = STATUS_FORMAT_LONG;
        unsigned char sha1[20];
        static struct option builtin_status_options[] = {
                OPT__VERBOSE(&verbose),
                OPT_SET_INT('s', "short", &status_format,
                            "show status concisely", STATUS_FORMAT_SHORT),
+               OPT_SET_INT(0, "porcelain", &status_format,
+                           "show porcelain output format",
+                           STATUS_FORMAT_PORCELAIN),
                OPT_BOOLEAN('z', "null", &null_termination,
                            "terminate entries with NUL"),
                { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg,
@@ -1011,7 +947,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
        };
 
        if (null_termination && status_format == STATUS_FORMAT_LONG)
-               status_format = STATUS_FORMAT_SHORT;
+               status_format = STATUS_FORMAT_PORCELAIN;
 
        wt_status_prepare(&s);
        git_config(git_status_config, &s);
@@ -1030,7 +966,12 @@ int cmd_status(int argc, const char **argv, const char *prefix)
 
        switch (status_format) {
        case STATUS_FORMAT_SHORT:
-               short_print(&s, null_termination);
+               if (s.relative_paths)
+                       s.prefix = prefix;
+               wt_shortstatus_print(&s, null_termination);
+               break;
+       case STATUS_FORMAT_PORCELAIN:
+               wt_shortstatus_print(&s, null_termination);
                break;
        case STATUS_FORMAT_LONG:
                s.verbose = verbose;