db20b86102691fdf516aa977e6d4950f2bc31ac1
   1#include "cache.h"
   2#include "wt-status.h"
   3#include "object.h"
   4#include "dir.h"
   5#include "commit.h"
   6#include "diff.h"
   7#include "revision.h"
   8#include "diffcore.h"
   9#include "quote.h"
  10#include "run-command.h"
  11#include "remote.h"
  12
  13static char default_wt_status_colors[][COLOR_MAXLEN] = {
  14        GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
  15        GIT_COLOR_GREEN,  /* WT_STATUS_UPDATED */
  16        GIT_COLOR_RED,    /* WT_STATUS_CHANGED */
  17        GIT_COLOR_RED,    /* WT_STATUS_UNTRACKED */
  18        GIT_COLOR_RED,    /* WT_STATUS_NOBRANCH */
  19        GIT_COLOR_RED,    /* WT_STATUS_UNMERGED */
  20};
  21
  22static const char *color(int slot, struct wt_status *s)
  23{
  24        return s->use_color > 0 ? s->color_palette[slot] : "";
  25}
  26
  27void wt_status_prepare(struct wt_status *s)
  28{
  29        unsigned char sha1[20];
  30        const char *head;
  31
  32        memset(s, 0, sizeof(*s));
  33        memcpy(s->color_palette, default_wt_status_colors,
  34               sizeof(default_wt_status_colors));
  35        s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
  36        s->use_color = -1;
  37        s->relative_paths = 1;
  38        head = resolve_ref("HEAD", sha1, 0, NULL);
  39        s->branch = head ? xstrdup(head) : NULL;
  40        s->reference = "HEAD";
  41        s->fp = stdout;
  42        s->index_file = get_index_file();
  43        s->change.strdup_strings = 1;
  44        s->untracked.strdup_strings = 1;
  45}
  46
  47static void wt_status_print_unmerged_header(struct wt_status *s)
  48{
  49        const char *c = color(WT_STATUS_HEADER, s);
  50
  51        color_fprintf_ln(s->fp, c, "# Unmerged paths:");
  52        if (!advice_status_hints)
  53                return;
  54        if (s->in_merge)
  55                ;
  56        else if (!s->is_initial)
  57                color_fprintf_ln(s->fp, c, "#   (use \"git reset %s <file>...\" to unstage)", s->reference);
  58        else
  59                color_fprintf_ln(s->fp, c, "#   (use \"git rm --cached <file>...\" to unstage)");
  60        color_fprintf_ln(s->fp, c, "#   (use \"git add/rm <file>...\" as appropriate to mark resolution)");
  61        color_fprintf_ln(s->fp, c, "#");
  62}
  63
  64static void wt_status_print_cached_header(struct wt_status *s)
  65{
  66        const char *c = color(WT_STATUS_HEADER, s);
  67
  68        color_fprintf_ln(s->fp, c, "# Changes to be committed:");
  69        if (!advice_status_hints)
  70                return;
  71        if (s->in_merge)
  72                ; /* NEEDSWORK: use "git reset --unresolve"??? */
  73        else if (!s->is_initial)
  74                color_fprintf_ln(s->fp, c, "#   (use \"git reset %s <file>...\" to unstage)", s->reference);
  75        else
  76                color_fprintf_ln(s->fp, c, "#   (use \"git rm --cached <file>...\" to unstage)");
  77        color_fprintf_ln(s->fp, c, "#");
  78}
  79
  80static void wt_status_print_dirty_header(struct wt_status *s,
  81                                         int has_deleted,
  82                                         int has_dirty_submodules)
  83{
  84        const char *c = color(WT_STATUS_HEADER, s);
  85
  86        color_fprintf_ln(s->fp, c, "# Changed but not updated:");
  87        if (!advice_status_hints)
  88                return;
  89        if (!has_deleted)
  90                color_fprintf_ln(s->fp, c, "#   (use \"git add <file>...\" to update what will be committed)");
  91        else
  92                color_fprintf_ln(s->fp, c, "#   (use \"git add/rm <file>...\" to update what will be committed)");
  93        color_fprintf_ln(s->fp, c, "#   (use \"git checkout -- <file>...\" to discard changes in working directory)");
  94        if (has_dirty_submodules)
  95                color_fprintf_ln(s->fp, c, "#   (commit or discard the untracked or modified content in submodules)");
  96        color_fprintf_ln(s->fp, c, "#");
  97}
  98
  99static void wt_status_print_untracked_header(struct wt_status *s)
 100{
 101        const char *c = color(WT_STATUS_HEADER, s);
 102        color_fprintf_ln(s->fp, c, "# Untracked files:");
 103        if (!advice_status_hints)
 104                return;
 105        color_fprintf_ln(s->fp, c, "#   (use \"git add <file>...\" to include in what will be committed)");
 106        color_fprintf_ln(s->fp, c, "#");
 107}
 108
 109static void wt_status_print_trailer(struct wt_status *s)
 110{
 111        color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
 112}
 113
 114#define quote_path quote_path_relative
 115
 116static void wt_status_print_unmerged_data(struct wt_status *s,
 117                                          struct string_list_item *it)
 118{
 119        const char *c = color(WT_STATUS_UNMERGED, s);
 120        struct wt_status_change_data *d = it->util;
 121        struct strbuf onebuf = STRBUF_INIT;
 122        const char *one, *how = "bug";
 123
 124        one = quote_path(it->string, -1, &onebuf, s->prefix);
 125        color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
 126        switch (d->stagemask) {
 127        case 1: how = "both deleted:"; break;
 128        case 2: how = "added by us:"; break;
 129        case 3: how = "deleted by them:"; break;
 130        case 4: how = "added by them:"; break;
 131        case 5: how = "deleted by us:"; break;
 132        case 6: how = "both added:"; break;
 133        case 7: how = "both modified:"; break;
 134        }
 135        color_fprintf(s->fp, c, "%-20s%s\n", how, one);
 136        strbuf_release(&onebuf);
 137}
 138
 139static void wt_status_print_change_data(struct wt_status *s,
 140                                        int change_type,
 141                                        struct string_list_item *it)
 142{
 143        struct wt_status_change_data *d = it->util;
 144        const char *c = color(change_type, s);
 145        int status = status;
 146        char *one_name;
 147        char *two_name;
 148        const char *one, *two;
 149        struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
 150        struct strbuf extra = STRBUF_INIT;
 151
 152        one_name = two_name = it->string;
 153        switch (change_type) {
 154        case WT_STATUS_UPDATED:
 155                status = d->index_status;
 156                if (d->head_path)
 157                        one_name = d->head_path;
 158                break;
 159        case WT_STATUS_CHANGED:
 160                if (d->new_submodule_commits || d->dirty_submodule) {
 161                        strbuf_addstr(&extra, " (");
 162                        if (d->new_submodule_commits)
 163                                strbuf_addf(&extra, "new commits, ");
 164                        if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
 165                                strbuf_addf(&extra, "modified content, ");
 166                        if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
 167                                strbuf_addf(&extra, "untracked content, ");
 168                        strbuf_setlen(&extra, extra.len - 2);
 169                        strbuf_addch(&extra, ')');
 170                }
 171                status = d->worktree_status;
 172                break;
 173        }
 174
 175        one = quote_path(one_name, -1, &onebuf, s->prefix);
 176        two = quote_path(two_name, -1, &twobuf, s->prefix);
 177
 178        color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
 179        switch (status) {
 180        case DIFF_STATUS_ADDED:
 181                color_fprintf(s->fp, c, "new file:   %s", one);
 182                break;
 183        case DIFF_STATUS_COPIED:
 184                color_fprintf(s->fp, c, "copied:     %s -> %s", one, two);
 185                break;
 186        case DIFF_STATUS_DELETED:
 187                color_fprintf(s->fp, c, "deleted:    %s", one);
 188                break;
 189        case DIFF_STATUS_MODIFIED:
 190                color_fprintf(s->fp, c, "modified:   %s", one);
 191                break;
 192        case DIFF_STATUS_RENAMED:
 193                color_fprintf(s->fp, c, "renamed:    %s -> %s", one, two);
 194                break;
 195        case DIFF_STATUS_TYPE_CHANGED:
 196                color_fprintf(s->fp, c, "typechange: %s", one);
 197                break;
 198        case DIFF_STATUS_UNKNOWN:
 199                color_fprintf(s->fp, c, "unknown:    %s", one);
 200                break;
 201        case DIFF_STATUS_UNMERGED:
 202                color_fprintf(s->fp, c, "unmerged:   %s", one);
 203                break;
 204        default:
 205                die("bug: unhandled diff status %c", status);
 206        }
 207        if (extra.len) {
 208                color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "%s", extra.buf);
 209                strbuf_release(&extra);
 210        }
 211        fprintf(s->fp, "\n");
 212        strbuf_release(&onebuf);
 213        strbuf_release(&twobuf);
 214}
 215
 216static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
 217                                         struct diff_options *options,
 218                                         void *data)
 219{
 220        struct wt_status *s = data;
 221        int i;
 222
 223        if (!q->nr)
 224                return;
 225        s->workdir_dirty = 1;
 226        for (i = 0; i < q->nr; i++) {
 227                struct diff_filepair *p;
 228                struct string_list_item *it;
 229                struct wt_status_change_data *d;
 230
 231                p = q->queue[i];
 232                it = string_list_insert(p->one->path, &s->change);
 233                d = it->util;
 234                if (!d) {
 235                        d = xcalloc(1, sizeof(*d));
 236                        it->util = d;
 237                }
 238                if (!d->worktree_status)
 239                        d->worktree_status = p->status;
 240                d->dirty_submodule = p->two->dirty_submodule;
 241                if (S_ISGITLINK(p->two->mode))
 242                        d->new_submodule_commits = !!hashcmp(p->one->sha1, p->two->sha1);
 243        }
 244}
 245
 246static int unmerged_mask(const char *path)
 247{
 248        int pos, mask;
 249        struct cache_entry *ce;
 250
 251        pos = cache_name_pos(path, strlen(path));
 252        if (0 <= pos)
 253                return 0;
 254
 255        mask = 0;
 256        pos = -pos-1;
 257        while (pos < active_nr) {
 258                ce = active_cache[pos++];
 259                if (strcmp(ce->name, path) || !ce_stage(ce))
 260                        break;
 261                mask |= (1 << (ce_stage(ce) - 1));
 262        }
 263        return mask;
 264}
 265
 266static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
 267                                         struct diff_options *options,
 268                                         void *data)
 269{
 270        struct wt_status *s = data;
 271        int i;
 272
 273        for (i = 0; i < q->nr; i++) {
 274                struct diff_filepair *p;
 275                struct string_list_item *it;
 276                struct wt_status_change_data *d;
 277
 278                p = q->queue[i];
 279                it = string_list_insert(p->two->path, &s->change);
 280                d = it->util;
 281                if (!d) {
 282                        d = xcalloc(1, sizeof(*d));
 283                        it->util = d;
 284                }
 285                if (!d->index_status)
 286                        d->index_status = p->status;
 287                switch (p->status) {
 288                case DIFF_STATUS_COPIED:
 289                case DIFF_STATUS_RENAMED:
 290                        d->head_path = xstrdup(p->one->path);
 291                        break;
 292                case DIFF_STATUS_UNMERGED:
 293                        d->stagemask = unmerged_mask(p->two->path);
 294                        break;
 295                }
 296        }
 297}
 298
 299static void wt_status_collect_changes_worktree(struct wt_status *s)
 300{
 301        struct rev_info rev;
 302
 303        init_revisions(&rev, NULL);
 304        setup_revisions(0, NULL, &rev, NULL);
 305        rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
 306        DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES);
 307        if (!s->show_untracked_files)
 308                DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
 309        rev.diffopt.format_callback = wt_status_collect_changed_cb;
 310        rev.diffopt.format_callback_data = s;
 311        rev.prune_data = s->pathspec;
 312        run_diff_files(&rev, 0);
 313}
 314
 315static void wt_status_collect_changes_index(struct wt_status *s)
 316{
 317        struct rev_info rev;
 318        struct setup_revision_opt opt;
 319
 320        init_revisions(&rev, NULL);
 321        memset(&opt, 0, sizeof(opt));
 322        opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
 323        setup_revisions(0, NULL, &rev, &opt);
 324
 325        rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
 326        rev.diffopt.format_callback = wt_status_collect_updated_cb;
 327        rev.diffopt.format_callback_data = s;
 328        rev.diffopt.detect_rename = 1;
 329        rev.diffopt.rename_limit = 200;
 330        rev.diffopt.break_opt = 0;
 331        rev.prune_data = s->pathspec;
 332        run_diff_index(&rev, 1);
 333}
 334
 335static void wt_status_collect_changes_initial(struct wt_status *s)
 336{
 337        int i;
 338
 339        for (i = 0; i < active_nr; i++) {
 340                struct string_list_item *it;
 341                struct wt_status_change_data *d;
 342                struct cache_entry *ce = active_cache[i];
 343
 344                if (!ce_path_match(ce, s->pathspec))
 345                        continue;
 346                it = string_list_insert(ce->name, &s->change);
 347                d = it->util;
 348                if (!d) {
 349                        d = xcalloc(1, sizeof(*d));
 350                        it->util = d;
 351                }
 352                if (ce_stage(ce)) {
 353                        d->index_status = DIFF_STATUS_UNMERGED;
 354                        d->stagemask |= (1 << (ce_stage(ce) - 1));
 355                }
 356                else
 357                        d->index_status = DIFF_STATUS_ADDED;
 358        }
 359}
 360
 361static void wt_status_collect_untracked(struct wt_status *s)
 362{
 363        int i;
 364        struct dir_struct dir;
 365
 366        if (!s->show_untracked_files)
 367                return;
 368        memset(&dir, 0, sizeof(dir));
 369        if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
 370                dir.flags |=
 371                        DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
 372        setup_standard_excludes(&dir);
 373
 374        fill_directory(&dir, s->pathspec);
 375        for (i = 0; i < dir.nr; i++) {
 376                struct dir_entry *ent = dir.entries[i];
 377                if (!cache_name_is_other(ent->name, ent->len))
 378                        continue;
 379                if (!match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
 380                        continue;
 381                string_list_insert(ent->name, &s->untracked);
 382        }
 383}
 384
 385void wt_status_collect(struct wt_status *s)
 386{
 387        wt_status_collect_changes_worktree(s);
 388
 389        if (s->is_initial)
 390                wt_status_collect_changes_initial(s);
 391        else
 392                wt_status_collect_changes_index(s);
 393        wt_status_collect_untracked(s);
 394}
 395
 396static void wt_status_print_unmerged(struct wt_status *s)
 397{
 398        int shown_header = 0;
 399        int i;
 400
 401        for (i = 0; i < s->change.nr; i++) {
 402                struct wt_status_change_data *d;
 403                struct string_list_item *it;
 404                it = &(s->change.items[i]);
 405                d = it->util;
 406                if (!d->stagemask)
 407                        continue;
 408                if (!shown_header) {
 409                        wt_status_print_unmerged_header(s);
 410                        shown_header = 1;
 411                }
 412                wt_status_print_unmerged_data(s, it);
 413        }
 414        if (shown_header)
 415                wt_status_print_trailer(s);
 416
 417}
 418
 419static void wt_status_print_updated(struct wt_status *s)
 420{
 421        int shown_header = 0;
 422        int i;
 423
 424        for (i = 0; i < s->change.nr; i++) {
 425                struct wt_status_change_data *d;
 426                struct string_list_item *it;
 427                it = &(s->change.items[i]);
 428                d = it->util;
 429                if (!d->index_status ||
 430                    d->index_status == DIFF_STATUS_UNMERGED)
 431                        continue;
 432                if (!shown_header) {
 433                        wt_status_print_cached_header(s);
 434                        s->commitable = 1;
 435                        shown_header = 1;
 436                }
 437                wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
 438        }
 439        if (shown_header)
 440                wt_status_print_trailer(s);
 441}
 442
 443/*
 444 * -1 : has delete
 445 *  0 : no change
 446 *  1 : some change but no delete
 447 */
 448static int wt_status_check_worktree_changes(struct wt_status *s,
 449                                             int *dirty_submodules)
 450{
 451        int i;
 452        int changes = 0;
 453
 454        *dirty_submodules = 0;
 455
 456        for (i = 0; i < s->change.nr; i++) {
 457                struct wt_status_change_data *d;
 458                d = s->change.items[i].util;
 459                if (!d->worktree_status ||
 460                    d->worktree_status == DIFF_STATUS_UNMERGED)
 461                        continue;
 462                if (!changes)
 463                        changes = 1;
 464                if (d->dirty_submodule)
 465                        *dirty_submodules = 1;
 466                if (d->worktree_status == DIFF_STATUS_DELETED)
 467                        changes = -1;
 468        }
 469        return changes;
 470}
 471
 472static void wt_status_print_changed(struct wt_status *s)
 473{
 474        int i, dirty_submodules;
 475        int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
 476
 477        if (!worktree_changes)
 478                return;
 479
 480        wt_status_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
 481
 482        for (i = 0; i < s->change.nr; i++) {
 483                struct wt_status_change_data *d;
 484                struct string_list_item *it;
 485                it = &(s->change.items[i]);
 486                d = it->util;
 487                if (!d->worktree_status ||
 488                    d->worktree_status == DIFF_STATUS_UNMERGED)
 489                        continue;
 490                wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
 491        }
 492        wt_status_print_trailer(s);
 493}
 494
 495static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted)
 496{
 497        struct child_process sm_summary;
 498        char summary_limit[64];
 499        char index[PATH_MAX];
 500        const char *env[] = { index, NULL };
 501        const char *argv[] = {
 502                "submodule",
 503                "summary",
 504                uncommitted ? "--files" : "--cached",
 505                "--for-status",
 506                "--summary-limit",
 507                summary_limit,
 508                uncommitted ? NULL : (s->amend ? "HEAD^" : "HEAD"),
 509                NULL
 510        };
 511
 512        sprintf(summary_limit, "%d", s->submodule_summary);
 513        snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
 514
 515        memset(&sm_summary, 0, sizeof(sm_summary));
 516        sm_summary.argv = argv;
 517        sm_summary.env = env;
 518        sm_summary.git_cmd = 1;
 519        sm_summary.no_stdin = 1;
 520        fflush(s->fp);
 521        sm_summary.out = dup(fileno(s->fp));    /* run_command closes it */
 522        run_command(&sm_summary);
 523}
 524
 525static void wt_status_print_untracked(struct wt_status *s)
 526{
 527        int i;
 528        struct strbuf buf = STRBUF_INIT;
 529
 530        if (!s->untracked.nr)
 531                return;
 532
 533        wt_status_print_untracked_header(s);
 534        for (i = 0; i < s->untracked.nr; i++) {
 535                struct string_list_item *it;
 536                it = &(s->untracked.items[i]);
 537                color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
 538                color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED, s), "%s",
 539                                 quote_path(it->string, strlen(it->string),
 540                                            &buf, s->prefix));
 541        }
 542        strbuf_release(&buf);
 543}
 544
 545static void wt_status_print_verbose(struct wt_status *s)
 546{
 547        struct rev_info rev;
 548        struct setup_revision_opt opt;
 549
 550        init_revisions(&rev, NULL);
 551        DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
 552
 553        memset(&opt, 0, sizeof(opt));
 554        opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
 555        setup_revisions(0, NULL, &rev, &opt);
 556
 557        rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
 558        rev.diffopt.detect_rename = 1;
 559        rev.diffopt.file = s->fp;
 560        rev.diffopt.close_file = 0;
 561        /*
 562         * If we're not going to stdout, then we definitely don't
 563         * want color, since we are going to the commit message
 564         * file (and even the "auto" setting won't work, since it
 565         * will have checked isatty on stdout).
 566         */
 567        if (s->fp != stdout)
 568                DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF);
 569        run_diff_index(&rev, 1);
 570}
 571
 572static void wt_status_print_tracking(struct wt_status *s)
 573{
 574        struct strbuf sb = STRBUF_INIT;
 575        const char *cp, *ep;
 576        struct branch *branch;
 577
 578        assert(s->branch && !s->is_initial);
 579        if (prefixcmp(s->branch, "refs/heads/"))
 580                return;
 581        branch = branch_get(s->branch + 11);
 582        if (!format_tracking_info(branch, &sb))
 583                return;
 584
 585        for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
 586                color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
 587                                 "# %.*s", (int)(ep - cp), cp);
 588        color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
 589}
 590
 591void wt_status_print(struct wt_status *s)
 592{
 593        const char *branch_color = color(WT_STATUS_HEADER, s);
 594
 595        if (s->branch) {
 596                const char *on_what = "On branch ";
 597                const char *branch_name = s->branch;
 598                if (!prefixcmp(branch_name, "refs/heads/"))
 599                        branch_name += 11;
 600                else if (!strcmp(branch_name, "HEAD")) {
 601                        branch_name = "";
 602                        branch_color = color(WT_STATUS_NOBRANCH, s);
 603                        on_what = "Not currently on any branch.";
 604                }
 605                color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# ");
 606                color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
 607                if (!s->is_initial)
 608                        wt_status_print_tracking(s);
 609        }
 610
 611        if (s->is_initial) {
 612                color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
 613                color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "# Initial commit");
 614                color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
 615        }
 616
 617        wt_status_print_updated(s);
 618        wt_status_print_unmerged(s);
 619        wt_status_print_changed(s);
 620        if (s->submodule_summary) {
 621                wt_status_print_submodule_summary(s, 0);  /* staged */
 622                wt_status_print_submodule_summary(s, 1);  /* unstaged */
 623        }
 624        if (s->show_untracked_files)
 625                wt_status_print_untracked(s);
 626        else if (s->commitable)
 627                 fprintf(s->fp, "# Untracked files not listed (use -u option to show untracked files)\n");
 628
 629        if (s->verbose)
 630                wt_status_print_verbose(s);
 631        if (!s->commitable) {
 632                if (s->amend)
 633                        fprintf(s->fp, "# No changes\n");
 634                else if (s->nowarn)
 635                        ; /* nothing */
 636                else if (s->workdir_dirty)
 637                        printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
 638                else if (s->untracked.nr)
 639                        printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
 640                else if (s->is_initial)
 641                        printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
 642                else if (!s->show_untracked_files)
 643                        printf("nothing to commit (use -u to show untracked files)\n");
 644                else
 645                        printf("nothing to commit (working directory clean)\n");
 646        }
 647}
 648
 649static void wt_shortstatus_unmerged(int null_termination, struct string_list_item *it,
 650                           struct wt_status *s)
 651{
 652        struct wt_status_change_data *d = it->util;
 653        const char *how = "??";
 654
 655        switch (d->stagemask) {
 656        case 1: how = "DD"; break; /* both deleted */
 657        case 2: how = "AU"; break; /* added by us */
 658        case 3: how = "UD"; break; /* deleted by them */
 659        case 4: how = "UA"; break; /* added by them */
 660        case 5: how = "DU"; break; /* deleted by us */
 661        case 6: how = "AA"; break; /* both added */
 662        case 7: how = "UU"; break; /* both modified */
 663        }
 664        color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
 665        if (null_termination) {
 666                fprintf(stdout, " %s%c", it->string, 0);
 667        } else {
 668                struct strbuf onebuf = STRBUF_INIT;
 669                const char *one;
 670                one = quote_path(it->string, -1, &onebuf, s->prefix);
 671                printf(" %s\n", one);
 672                strbuf_release(&onebuf);
 673        }
 674}
 675
 676static void wt_shortstatus_status(int null_termination, struct string_list_item *it,
 677                         struct wt_status *s)
 678{
 679        struct wt_status_change_data *d = it->util;
 680
 681        if (d->index_status)
 682                color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
 683        else
 684                putchar(' ');
 685        if (d->worktree_status)
 686                color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
 687        else
 688                putchar(' ');
 689        putchar(' ');
 690        if (null_termination) {
 691                fprintf(stdout, "%s%c", it->string, 0);
 692                if (d->head_path)
 693                        fprintf(stdout, "%s%c", d->head_path, 0);
 694        } else {
 695                struct strbuf onebuf = STRBUF_INIT;
 696                const char *one;
 697                if (d->head_path) {
 698                        one = quote_path(d->head_path, -1, &onebuf, s->prefix);
 699                        printf("%s -> ", one);
 700                        strbuf_release(&onebuf);
 701                }
 702                one = quote_path(it->string, -1, &onebuf, s->prefix);
 703                printf("%s\n", one);
 704                strbuf_release(&onebuf);
 705        }
 706}
 707
 708static void wt_shortstatus_untracked(int null_termination, struct string_list_item *it,
 709                            struct wt_status *s)
 710{
 711        if (null_termination) {
 712                fprintf(stdout, "?? %s%c", it->string, 0);
 713        } else {
 714                struct strbuf onebuf = STRBUF_INIT;
 715                const char *one;
 716                one = quote_path(it->string, -1, &onebuf, s->prefix);
 717                color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "??");
 718                printf(" %s\n", one);
 719                strbuf_release(&onebuf);
 720        }
 721}
 722
 723void wt_shortstatus_print(struct wt_status *s, int null_termination)
 724{
 725        int i;
 726        for (i = 0; i < s->change.nr; i++) {
 727                struct wt_status_change_data *d;
 728                struct string_list_item *it;
 729
 730                it = &(s->change.items[i]);
 731                d = it->util;
 732                if (d->stagemask)
 733                        wt_shortstatus_unmerged(null_termination, it, s);
 734                else
 735                        wt_shortstatus_status(null_termination, it, s);
 736        }
 737        for (i = 0; i < s->untracked.nr; i++) {
 738                struct string_list_item *it;
 739
 740                it = &(s->untracked.items[i]);
 741                wt_shortstatus_untracked(null_termination, it, s);
 742        }
 743}
 744
 745void wt_porcelain_print(struct wt_status *s, int null_termination)
 746{
 747        s->use_color = 0;
 748        s->relative_paths = 0;
 749        s->prefix = NULL;
 750        wt_shortstatus_print(s, null_termination);
 751}