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