958a53c9cb7d240734df5f8b2c41f99c03a66912
   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 "argv-array.h"
  12#include "remote.h"
  13#include "refs.h"
  14#include "submodule.h"
  15#include "column.h"
  16#include "strbuf.h"
  17
  18static char default_wt_status_colors[][COLOR_MAXLEN] = {
  19        GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
  20        GIT_COLOR_GREEN,  /* WT_STATUS_UPDATED */
  21        GIT_COLOR_RED,    /* WT_STATUS_CHANGED */
  22        GIT_COLOR_RED,    /* WT_STATUS_UNTRACKED */
  23        GIT_COLOR_RED,    /* WT_STATUS_NOBRANCH */
  24        GIT_COLOR_RED,    /* WT_STATUS_UNMERGED */
  25        GIT_COLOR_GREEN,  /* WT_STATUS_LOCAL_BRANCH */
  26        GIT_COLOR_RED,    /* WT_STATUS_REMOTE_BRANCH */
  27        GIT_COLOR_NIL,    /* WT_STATUS_ONBRANCH */
  28};
  29
  30static const char *color(int slot, struct wt_status *s)
  31{
  32        const char *c = "";
  33        if (want_color(s->use_color))
  34                c = s->color_palette[slot];
  35        if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
  36                c = s->color_palette[WT_STATUS_HEADER];
  37        return c;
  38}
  39
  40static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
  41                const char *fmt, va_list ap, const char *trail)
  42{
  43        struct strbuf sb = STRBUF_INIT;
  44        struct strbuf linebuf = STRBUF_INIT;
  45        const char *line, *eol;
  46
  47        strbuf_vaddf(&sb, fmt, ap);
  48        if (!sb.len) {
  49                strbuf_addch(&sb, comment_line_char);
  50                if (!trail)
  51                        strbuf_addch(&sb, ' ');
  52                color_print_strbuf(s->fp, color, &sb);
  53                if (trail)
  54                        fprintf(s->fp, "%s", trail);
  55                strbuf_release(&sb);
  56                return;
  57        }
  58        for (line = sb.buf; *line; line = eol + 1) {
  59                eol = strchr(line, '\n');
  60
  61                strbuf_reset(&linebuf);
  62                if (at_bol) {
  63                        strbuf_addch(&linebuf, comment_line_char);
  64                        if (*line != '\n' && *line != '\t')
  65                                strbuf_addch(&linebuf, ' ');
  66                }
  67                if (eol)
  68                        strbuf_add(&linebuf, line, eol - line);
  69                else
  70                        strbuf_addstr(&linebuf, line);
  71                color_print_strbuf(s->fp, color, &linebuf);
  72                if (eol)
  73                        fprintf(s->fp, "\n");
  74                else
  75                        break;
  76                at_bol = 1;
  77        }
  78        if (trail)
  79                fprintf(s->fp, "%s", trail);
  80        strbuf_release(&linebuf);
  81        strbuf_release(&sb);
  82}
  83
  84void status_printf_ln(struct wt_status *s, const char *color,
  85                        const char *fmt, ...)
  86{
  87        va_list ap;
  88
  89        va_start(ap, fmt);
  90        status_vprintf(s, 1, color, fmt, ap, "\n");
  91        va_end(ap);
  92}
  93
  94void status_printf(struct wt_status *s, const char *color,
  95                        const char *fmt, ...)
  96{
  97        va_list ap;
  98
  99        va_start(ap, fmt);
 100        status_vprintf(s, 1, color, fmt, ap, NULL);
 101        va_end(ap);
 102}
 103
 104static void status_printf_more(struct wt_status *s, const char *color,
 105                               const char *fmt, ...)
 106{
 107        va_list ap;
 108
 109        va_start(ap, fmt);
 110        status_vprintf(s, 0, color, fmt, ap, NULL);
 111        va_end(ap);
 112}
 113
 114void wt_status_prepare(struct wt_status *s)
 115{
 116        unsigned char sha1[20];
 117
 118        memset(s, 0, sizeof(*s));
 119        memcpy(s->color_palette, default_wt_status_colors,
 120               sizeof(default_wt_status_colors));
 121        s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
 122        s->use_color = -1;
 123        s->relative_paths = 1;
 124        s->branch = resolve_refdup("HEAD", sha1, 0, NULL);
 125        s->reference = "HEAD";
 126        s->fp = stdout;
 127        s->index_file = get_index_file();
 128        s->change.strdup_strings = 1;
 129        s->untracked.strdup_strings = 1;
 130        s->ignored.strdup_strings = 1;
 131        s->show_branch = -1;  /* unspecified */
 132}
 133
 134static void wt_status_print_unmerged_header(struct wt_status *s)
 135{
 136        int i;
 137        int del_mod_conflict = 0;
 138        int both_deleted = 0;
 139        int not_deleted = 0;
 140        const char *c = color(WT_STATUS_HEADER, s);
 141
 142        status_printf_ln(s, c, _("Unmerged paths:"));
 143
 144        for (i = 0; i < s->change.nr; i++) {
 145                struct string_list_item *it = &(s->change.items[i]);
 146                struct wt_status_change_data *d = it->util;
 147
 148                switch (d->stagemask) {
 149                case 0:
 150                        break;
 151                case 1:
 152                        both_deleted = 1;
 153                        break;
 154                case 3:
 155                case 5:
 156                        del_mod_conflict = 1;
 157                        break;
 158                default:
 159                        not_deleted = 1;
 160                        break;
 161                }
 162        }
 163
 164        if (!advice_status_hints)
 165                return;
 166        if (s->whence != FROM_COMMIT)
 167                ;
 168        else if (!s->is_initial)
 169                status_printf_ln(s, c, _("  (use \"git reset %s <file>...\" to unstage)"), s->reference);
 170        else
 171                status_printf_ln(s, c, _("  (use \"git rm --cached <file>...\" to unstage)"));
 172
 173        if (!both_deleted) {
 174                if (!del_mod_conflict)
 175                        status_printf_ln(s, c, _("  (use \"git add <file>...\" to mark resolution)"));
 176                else
 177                        status_printf_ln(s, c, _("  (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
 178        } else if (!del_mod_conflict && !not_deleted) {
 179                status_printf_ln(s, c, _("  (use \"git rm <file>...\" to mark resolution)"));
 180        } else {
 181                status_printf_ln(s, c, _("  (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
 182        }
 183        status_printf_ln(s, c, "");
 184}
 185
 186static void wt_status_print_cached_header(struct wt_status *s)
 187{
 188        const char *c = color(WT_STATUS_HEADER, s);
 189
 190        status_printf_ln(s, c, _("Changes to be committed:"));
 191        if (!advice_status_hints)
 192                return;
 193        if (s->whence != FROM_COMMIT)
 194                ; /* NEEDSWORK: use "git reset --unresolve"??? */
 195        else if (!s->is_initial)
 196                status_printf_ln(s, c, _("  (use \"git reset %s <file>...\" to unstage)"), s->reference);
 197        else
 198                status_printf_ln(s, c, _("  (use \"git rm --cached <file>...\" to unstage)"));
 199        status_printf_ln(s, c, "");
 200}
 201
 202static void wt_status_print_dirty_header(struct wt_status *s,
 203                                         int has_deleted,
 204                                         int has_dirty_submodules)
 205{
 206        const char *c = color(WT_STATUS_HEADER, s);
 207
 208        status_printf_ln(s, c, _("Changes not staged for commit:"));
 209        if (!advice_status_hints)
 210                return;
 211        if (!has_deleted)
 212                status_printf_ln(s, c, _("  (use \"git add <file>...\" to update what will be committed)"));
 213        else
 214                status_printf_ln(s, c, _("  (use \"git add/rm <file>...\" to update what will be committed)"));
 215        status_printf_ln(s, c, _("  (use \"git checkout -- <file>...\" to discard changes in working directory)"));
 216        if (has_dirty_submodules)
 217                status_printf_ln(s, c, _("  (commit or discard the untracked or modified content in submodules)"));
 218        status_printf_ln(s, c, "");
 219}
 220
 221static void wt_status_print_other_header(struct wt_status *s,
 222                                         const char *what,
 223                                         const char *how)
 224{
 225        const char *c = color(WT_STATUS_HEADER, s);
 226        status_printf_ln(s, c, "%s:", what);
 227        if (!advice_status_hints)
 228                return;
 229        status_printf_ln(s, c, _("  (use \"git %s <file>...\" to include in what will be committed)"), how);
 230        status_printf_ln(s, c, "");
 231}
 232
 233static void wt_status_print_trailer(struct wt_status *s)
 234{
 235        status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
 236}
 237
 238#define quote_path quote_path_relative
 239
 240static void wt_status_print_unmerged_data(struct wt_status *s,
 241                                          struct string_list_item *it)
 242{
 243        const char *c = color(WT_STATUS_UNMERGED, s);
 244        struct wt_status_change_data *d = it->util;
 245        struct strbuf onebuf = STRBUF_INIT;
 246        const char *one, *how = _("bug");
 247
 248        one = quote_path(it->string, s->prefix, &onebuf);
 249        status_printf(s, color(WT_STATUS_HEADER, s), "\t");
 250        switch (d->stagemask) {
 251        case 1: how = _("both deleted:"); break;
 252        case 2: how = _("added by us:"); break;
 253        case 3: how = _("deleted by them:"); break;
 254        case 4: how = _("added by them:"); break;
 255        case 5: how = _("deleted by us:"); break;
 256        case 6: how = _("both added:"); break;
 257        case 7: how = _("both modified:"); break;
 258        }
 259        status_printf_more(s, c, "%-20s%s\n", how, one);
 260        strbuf_release(&onebuf);
 261}
 262
 263static void wt_status_print_change_data(struct wt_status *s,
 264                                        int change_type,
 265                                        struct string_list_item *it)
 266{
 267        struct wt_status_change_data *d = it->util;
 268        const char *c = color(change_type, s);
 269        int status;
 270        char *one_name;
 271        char *two_name;
 272        const char *one, *two;
 273        struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
 274        struct strbuf extra = STRBUF_INIT;
 275
 276        one_name = two_name = it->string;
 277        switch (change_type) {
 278        case WT_STATUS_UPDATED:
 279                status = d->index_status;
 280                if (d->head_path)
 281                        one_name = d->head_path;
 282                break;
 283        case WT_STATUS_CHANGED:
 284                if (d->new_submodule_commits || d->dirty_submodule) {
 285                        strbuf_addstr(&extra, " (");
 286                        if (d->new_submodule_commits)
 287                                strbuf_addf(&extra, _("new commits, "));
 288                        if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
 289                                strbuf_addf(&extra, _("modified content, "));
 290                        if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
 291                                strbuf_addf(&extra, _("untracked content, "));
 292                        strbuf_setlen(&extra, extra.len - 2);
 293                        strbuf_addch(&extra, ')');
 294                }
 295                status = d->worktree_status;
 296                break;
 297        default:
 298                die("BUG: unhandled change_type %d in wt_status_print_change_data",
 299                    change_type);
 300        }
 301
 302        one = quote_path(one_name, s->prefix, &onebuf);
 303        two = quote_path(two_name, s->prefix, &twobuf);
 304
 305        status_printf(s, color(WT_STATUS_HEADER, s), "\t");
 306        switch (status) {
 307        case DIFF_STATUS_ADDED:
 308                status_printf_more(s, c, _("new file:   %s"), one);
 309                break;
 310        case DIFF_STATUS_COPIED:
 311                status_printf_more(s, c, _("copied:     %s -> %s"), one, two);
 312                break;
 313        case DIFF_STATUS_DELETED:
 314                status_printf_more(s, c, _("deleted:    %s"), one);
 315                break;
 316        case DIFF_STATUS_MODIFIED:
 317                status_printf_more(s, c, _("modified:   %s"), one);
 318                break;
 319        case DIFF_STATUS_RENAMED:
 320                status_printf_more(s, c, _("renamed:    %s -> %s"), one, two);
 321                break;
 322        case DIFF_STATUS_TYPE_CHANGED:
 323                status_printf_more(s, c, _("typechange: %s"), one);
 324                break;
 325        case DIFF_STATUS_UNKNOWN:
 326                status_printf_more(s, c, _("unknown:    %s"), one);
 327                break;
 328        case DIFF_STATUS_UNMERGED:
 329                status_printf_more(s, c, _("unmerged:   %s"), one);
 330                break;
 331        default:
 332                die(_("bug: unhandled diff status %c"), status);
 333        }
 334        if (extra.len) {
 335                status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
 336                strbuf_release(&extra);
 337        }
 338        status_printf_more(s, GIT_COLOR_NORMAL, "\n");
 339        strbuf_release(&onebuf);
 340        strbuf_release(&twobuf);
 341}
 342
 343static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
 344                                         struct diff_options *options,
 345                                         void *data)
 346{
 347        struct wt_status *s = data;
 348        int i;
 349
 350        if (!q->nr)
 351                return;
 352        s->workdir_dirty = 1;
 353        for (i = 0; i < q->nr; i++) {
 354                struct diff_filepair *p;
 355                struct string_list_item *it;
 356                struct wt_status_change_data *d;
 357
 358                p = q->queue[i];
 359                it = string_list_insert(&s->change, p->one->path);
 360                d = it->util;
 361                if (!d) {
 362                        d = xcalloc(1, sizeof(*d));
 363                        it->util = d;
 364                }
 365                if (!d->worktree_status)
 366                        d->worktree_status = p->status;
 367                d->dirty_submodule = p->two->dirty_submodule;
 368                if (S_ISGITLINK(p->two->mode))
 369                        d->new_submodule_commits = !!hashcmp(p->one->sha1, p->two->sha1);
 370        }
 371}
 372
 373static int unmerged_mask(const char *path)
 374{
 375        int pos, mask;
 376        const struct cache_entry *ce;
 377
 378        pos = cache_name_pos(path, strlen(path));
 379        if (0 <= pos)
 380                return 0;
 381
 382        mask = 0;
 383        pos = -pos-1;
 384        while (pos < active_nr) {
 385                ce = active_cache[pos++];
 386                if (strcmp(ce->name, path) || !ce_stage(ce))
 387                        break;
 388                mask |= (1 << (ce_stage(ce) - 1));
 389        }
 390        return mask;
 391}
 392
 393static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
 394                                         struct diff_options *options,
 395                                         void *data)
 396{
 397        struct wt_status *s = data;
 398        int i;
 399
 400        for (i = 0; i < q->nr; i++) {
 401                struct diff_filepair *p;
 402                struct string_list_item *it;
 403                struct wt_status_change_data *d;
 404
 405                p = q->queue[i];
 406                it = string_list_insert(&s->change, p->two->path);
 407                d = it->util;
 408                if (!d) {
 409                        d = xcalloc(1, sizeof(*d));
 410                        it->util = d;
 411                }
 412                if (!d->index_status)
 413                        d->index_status = p->status;
 414                switch (p->status) {
 415                case DIFF_STATUS_COPIED:
 416                case DIFF_STATUS_RENAMED:
 417                        d->head_path = xstrdup(p->one->path);
 418                        break;
 419                case DIFF_STATUS_UNMERGED:
 420                        d->stagemask = unmerged_mask(p->two->path);
 421                        break;
 422                }
 423        }
 424}
 425
 426static void wt_status_collect_changes_worktree(struct wt_status *s)
 427{
 428        struct rev_info rev;
 429
 430        init_revisions(&rev, NULL);
 431        setup_revisions(0, NULL, &rev, NULL);
 432        rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
 433        DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES);
 434        if (!s->show_untracked_files)
 435                DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
 436        if (s->ignore_submodule_arg) {
 437                DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
 438                handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
 439        }
 440        rev.diffopt.format_callback = wt_status_collect_changed_cb;
 441        rev.diffopt.format_callback_data = s;
 442        init_pathspec(&rev.prune_data, s->pathspec);
 443        run_diff_files(&rev, 0);
 444}
 445
 446static void wt_status_collect_changes_index(struct wt_status *s)
 447{
 448        struct rev_info rev;
 449        struct setup_revision_opt opt;
 450
 451        init_revisions(&rev, NULL);
 452        memset(&opt, 0, sizeof(opt));
 453        opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
 454        setup_revisions(0, NULL, &rev, &opt);
 455
 456        if (s->ignore_submodule_arg) {
 457                DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
 458                handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
 459        }
 460
 461        rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
 462        rev.diffopt.format_callback = wt_status_collect_updated_cb;
 463        rev.diffopt.format_callback_data = s;
 464        rev.diffopt.detect_rename = 1;
 465        rev.diffopt.rename_limit = 200;
 466        rev.diffopt.break_opt = 0;
 467        init_pathspec(&rev.prune_data, s->pathspec);
 468        run_diff_index(&rev, 1);
 469}
 470
 471static void wt_status_collect_changes_initial(struct wt_status *s)
 472{
 473        struct pathspec pathspec;
 474        int i;
 475
 476        init_pathspec(&pathspec, s->pathspec);
 477        for (i = 0; i < active_nr; i++) {
 478                struct string_list_item *it;
 479                struct wt_status_change_data *d;
 480                const struct cache_entry *ce = active_cache[i];
 481
 482                if (!ce_path_match(ce, &pathspec))
 483                        continue;
 484                it = string_list_insert(&s->change, ce->name);
 485                d = it->util;
 486                if (!d) {
 487                        d = xcalloc(1, sizeof(*d));
 488                        it->util = d;
 489                }
 490                if (ce_stage(ce)) {
 491                        d->index_status = DIFF_STATUS_UNMERGED;
 492                        d->stagemask |= (1 << (ce_stage(ce) - 1));
 493                }
 494                else
 495                        d->index_status = DIFF_STATUS_ADDED;
 496        }
 497        free_pathspec(&pathspec);
 498}
 499
 500static void wt_status_collect_untracked(struct wt_status *s)
 501{
 502        int i;
 503        struct dir_struct dir;
 504        struct timeval t_begin;
 505
 506        if (!s->show_untracked_files)
 507                return;
 508
 509        if (advice_status_u_option)
 510                gettimeofday(&t_begin, NULL);
 511
 512        memset(&dir, 0, sizeof(dir));
 513        if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
 514                dir.flags |=
 515                        DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
 516        if (s->show_ignored_files)
 517                dir.flags |= DIR_SHOW_IGNORED_TOO;
 518        setup_standard_excludes(&dir);
 519
 520        fill_directory(&dir, s->pathspec);
 521
 522        for (i = 0; i < dir.nr; i++) {
 523                struct dir_entry *ent = dir.entries[i];
 524                if (cache_name_is_other(ent->name, ent->len) &&
 525                    match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
 526                        string_list_insert(&s->untracked, ent->name);
 527                free(ent);
 528        }
 529
 530        for (i = 0; i < dir.ignored_nr; i++) {
 531                struct dir_entry *ent = dir.ignored[i];
 532                if (cache_name_is_other(ent->name, ent->len) &&
 533                    match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
 534                        string_list_insert(&s->ignored, ent->name);
 535                free(ent);
 536        }
 537
 538        free(dir.entries);
 539        free(dir.ignored);
 540        clear_directory(&dir);
 541
 542        if (advice_status_u_option) {
 543                struct timeval t_end;
 544                gettimeofday(&t_end, NULL);
 545                s->untracked_in_ms =
 546                        (uint64_t)t_end.tv_sec * 1000 + t_end.tv_usec / 1000 -
 547                        ((uint64_t)t_begin.tv_sec * 1000 + t_begin.tv_usec / 1000);
 548        }
 549}
 550
 551void wt_status_collect(struct wt_status *s)
 552{
 553        wt_status_collect_changes_worktree(s);
 554
 555        if (s->is_initial)
 556                wt_status_collect_changes_initial(s);
 557        else
 558                wt_status_collect_changes_index(s);
 559        wt_status_collect_untracked(s);
 560}
 561
 562static void wt_status_print_unmerged(struct wt_status *s)
 563{
 564        int shown_header = 0;
 565        int i;
 566
 567        for (i = 0; i < s->change.nr; i++) {
 568                struct wt_status_change_data *d;
 569                struct string_list_item *it;
 570                it = &(s->change.items[i]);
 571                d = it->util;
 572                if (!d->stagemask)
 573                        continue;
 574                if (!shown_header) {
 575                        wt_status_print_unmerged_header(s);
 576                        shown_header = 1;
 577                }
 578                wt_status_print_unmerged_data(s, it);
 579        }
 580        if (shown_header)
 581                wt_status_print_trailer(s);
 582
 583}
 584
 585static void wt_status_print_updated(struct wt_status *s)
 586{
 587        int shown_header = 0;
 588        int i;
 589
 590        for (i = 0; i < s->change.nr; i++) {
 591                struct wt_status_change_data *d;
 592                struct string_list_item *it;
 593                it = &(s->change.items[i]);
 594                d = it->util;
 595                if (!d->index_status ||
 596                    d->index_status == DIFF_STATUS_UNMERGED)
 597                        continue;
 598                if (!shown_header) {
 599                        wt_status_print_cached_header(s);
 600                        s->commitable = 1;
 601                        shown_header = 1;
 602                }
 603                wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
 604        }
 605        if (shown_header)
 606                wt_status_print_trailer(s);
 607}
 608
 609/*
 610 * -1 : has delete
 611 *  0 : no change
 612 *  1 : some change but no delete
 613 */
 614static int wt_status_check_worktree_changes(struct wt_status *s,
 615                                             int *dirty_submodules)
 616{
 617        int i;
 618        int changes = 0;
 619
 620        *dirty_submodules = 0;
 621
 622        for (i = 0; i < s->change.nr; i++) {
 623                struct wt_status_change_data *d;
 624                d = s->change.items[i].util;
 625                if (!d->worktree_status ||
 626                    d->worktree_status == DIFF_STATUS_UNMERGED)
 627                        continue;
 628                if (!changes)
 629                        changes = 1;
 630                if (d->dirty_submodule)
 631                        *dirty_submodules = 1;
 632                if (d->worktree_status == DIFF_STATUS_DELETED)
 633                        changes = -1;
 634        }
 635        return changes;
 636}
 637
 638static void wt_status_print_changed(struct wt_status *s)
 639{
 640        int i, dirty_submodules;
 641        int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
 642
 643        if (!worktree_changes)
 644                return;
 645
 646        wt_status_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
 647
 648        for (i = 0; i < s->change.nr; i++) {
 649                struct wt_status_change_data *d;
 650                struct string_list_item *it;
 651                it = &(s->change.items[i]);
 652                d = it->util;
 653                if (!d->worktree_status ||
 654                    d->worktree_status == DIFF_STATUS_UNMERGED)
 655                        continue;
 656                wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
 657        }
 658        wt_status_print_trailer(s);
 659}
 660
 661static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted)
 662{
 663        struct child_process sm_summary;
 664        char summary_limit[64];
 665        char index[PATH_MAX];
 666        const char *env[] = { NULL, NULL };
 667        struct argv_array argv = ARGV_ARRAY_INIT;
 668
 669        sprintf(summary_limit, "%d", s->submodule_summary);
 670        snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
 671
 672        env[0] = index;
 673        argv_array_push(&argv, "submodule");
 674        argv_array_push(&argv, "summary");
 675        argv_array_push(&argv, uncommitted ? "--files" : "--cached");
 676        argv_array_push(&argv, "--for-status");
 677        argv_array_push(&argv, "--summary-limit");
 678        argv_array_push(&argv, summary_limit);
 679        if (!uncommitted)
 680                argv_array_push(&argv, s->amend ? "HEAD^" : "HEAD");
 681
 682        memset(&sm_summary, 0, sizeof(sm_summary));
 683        sm_summary.argv = argv.argv;
 684        sm_summary.env = env;
 685        sm_summary.git_cmd = 1;
 686        sm_summary.no_stdin = 1;
 687        fflush(s->fp);
 688        sm_summary.out = dup(fileno(s->fp));    /* run_command closes it */
 689        run_command(&sm_summary);
 690        argv_array_clear(&argv);
 691}
 692
 693static void wt_status_print_other(struct wt_status *s,
 694                                  struct string_list *l,
 695                                  const char *what,
 696                                  const char *how)
 697{
 698        int i;
 699        struct strbuf buf = STRBUF_INIT;
 700        static struct string_list output = STRING_LIST_INIT_DUP;
 701        struct column_options copts;
 702
 703        if (!l->nr)
 704                return;
 705
 706        wt_status_print_other_header(s, what, how);
 707
 708        for (i = 0; i < l->nr; i++) {
 709                struct string_list_item *it;
 710                const char *path;
 711                it = &(l->items[i]);
 712                path = quote_path(it->string, s->prefix, &buf);
 713                if (column_active(s->colopts)) {
 714                        string_list_append(&output, path);
 715                        continue;
 716                }
 717                status_printf(s, color(WT_STATUS_HEADER, s), "\t");
 718                status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
 719                                   "%s\n", path);
 720        }
 721
 722        strbuf_release(&buf);
 723        if (!column_active(s->colopts))
 724                return;
 725
 726        strbuf_addf(&buf, "%s#\t%s",
 727                    color(WT_STATUS_HEADER, s),
 728                    color(WT_STATUS_UNTRACKED, s));
 729        memset(&copts, 0, sizeof(copts));
 730        copts.padding = 1;
 731        copts.indent = buf.buf;
 732        if (want_color(s->use_color))
 733                copts.nl = GIT_COLOR_RESET "\n";
 734        print_columns(&output, s->colopts, &copts);
 735        string_list_clear(&output, 0);
 736        strbuf_release(&buf);
 737}
 738
 739static void wt_status_print_verbose(struct wt_status *s)
 740{
 741        struct rev_info rev;
 742        struct setup_revision_opt opt;
 743
 744        init_revisions(&rev, NULL);
 745        DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
 746
 747        memset(&opt, 0, sizeof(opt));
 748        opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
 749        setup_revisions(0, NULL, &rev, &opt);
 750
 751        rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
 752        rev.diffopt.detect_rename = 1;
 753        rev.diffopt.file = s->fp;
 754        rev.diffopt.close_file = 0;
 755        /*
 756         * If we're not going to stdout, then we definitely don't
 757         * want color, since we are going to the commit message
 758         * file (and even the "auto" setting won't work, since it
 759         * will have checked isatty on stdout).
 760         */
 761        if (s->fp != stdout)
 762                rev.diffopt.use_color = 0;
 763        run_diff_index(&rev, 1);
 764}
 765
 766static void wt_status_print_tracking(struct wt_status *s)
 767{
 768        struct strbuf sb = STRBUF_INIT;
 769        const char *cp, *ep;
 770        struct branch *branch;
 771
 772        assert(s->branch && !s->is_initial);
 773        if (prefixcmp(s->branch, "refs/heads/"))
 774                return;
 775        branch = branch_get(s->branch + 11);
 776        if (!format_tracking_info(branch, &sb))
 777                return;
 778
 779        for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
 780                color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
 781                                 "%c %.*s", comment_line_char,
 782                                 (int)(ep - cp), cp);
 783        color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
 784                         comment_line_char);
 785}
 786
 787static int has_unmerged(struct wt_status *s)
 788{
 789        int i;
 790
 791        for (i = 0; i < s->change.nr; i++) {
 792                struct wt_status_change_data *d;
 793                d = s->change.items[i].util;
 794                if (d->stagemask)
 795                        return 1;
 796        }
 797        return 0;
 798}
 799
 800static void show_merge_in_progress(struct wt_status *s,
 801                                struct wt_status_state *state,
 802                                const char *color)
 803{
 804        if (has_unmerged(s)) {
 805                status_printf_ln(s, color, _("You have unmerged paths."));
 806                if (advice_status_hints)
 807                        status_printf_ln(s, color,
 808                                _("  (fix conflicts and run \"git commit\")"));
 809        } else {
 810                status_printf_ln(s, color,
 811                        _("All conflicts fixed but you are still merging."));
 812                if (advice_status_hints)
 813                        status_printf_ln(s, color,
 814                                _("  (use \"git commit\" to conclude merge)"));
 815        }
 816        wt_status_print_trailer(s);
 817}
 818
 819static void show_am_in_progress(struct wt_status *s,
 820                                struct wt_status_state *state,
 821                                const char *color)
 822{
 823        status_printf_ln(s, color,
 824                _("You are in the middle of an am session."));
 825        if (state->am_empty_patch)
 826                status_printf_ln(s, color,
 827                        _("The current patch is empty."));
 828        if (advice_status_hints) {
 829                if (!state->am_empty_patch)
 830                        status_printf_ln(s, color,
 831                                _("  (fix conflicts and then run \"git am --continue\")"));
 832                status_printf_ln(s, color,
 833                        _("  (use \"git am --skip\" to skip this patch)"));
 834                status_printf_ln(s, color,
 835                        _("  (use \"git am --abort\" to restore the original branch)"));
 836        }
 837        wt_status_print_trailer(s);
 838}
 839
 840static char *read_line_from_git_path(const char *filename)
 841{
 842        struct strbuf buf = STRBUF_INIT;
 843        FILE *fp = fopen(git_path("%s", filename), "r");
 844        if (!fp) {
 845                strbuf_release(&buf);
 846                return NULL;
 847        }
 848        strbuf_getline(&buf, fp, '\n');
 849        if (!fclose(fp)) {
 850                return strbuf_detach(&buf, NULL);
 851        } else {
 852                strbuf_release(&buf);
 853                return NULL;
 854        }
 855}
 856
 857static int split_commit_in_progress(struct wt_status *s)
 858{
 859        int split_in_progress = 0;
 860        char *head = read_line_from_git_path("HEAD");
 861        char *orig_head = read_line_from_git_path("ORIG_HEAD");
 862        char *rebase_amend = read_line_from_git_path("rebase-merge/amend");
 863        char *rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
 864
 865        if (!head || !orig_head || !rebase_amend || !rebase_orig_head ||
 866            !s->branch || strcmp(s->branch, "HEAD"))
 867                return split_in_progress;
 868
 869        if (!strcmp(rebase_amend, rebase_orig_head)) {
 870                if (strcmp(head, rebase_amend))
 871                        split_in_progress = 1;
 872        } else if (strcmp(orig_head, rebase_orig_head)) {
 873                split_in_progress = 1;
 874        }
 875
 876        if (!s->amend && !s->nowarn && !s->workdir_dirty)
 877                split_in_progress = 0;
 878
 879        free(head);
 880        free(orig_head);
 881        free(rebase_amend);
 882        free(rebase_orig_head);
 883        return split_in_progress;
 884}
 885
 886static void show_rebase_in_progress(struct wt_status *s,
 887                                struct wt_status_state *state,
 888                                const char *color)
 889{
 890        struct stat st;
 891
 892        if (has_unmerged(s)) {
 893                if (state->branch)
 894                        status_printf_ln(s, color,
 895                                         _("You are currently rebasing branch '%s' on '%s'."),
 896                                         state->branch,
 897                                         state->onto);
 898                else
 899                        status_printf_ln(s, color,
 900                                         _("You are currently rebasing."));
 901                if (advice_status_hints) {
 902                        status_printf_ln(s, color,
 903                                _("  (fix conflicts and then run \"git rebase --continue\")"));
 904                        status_printf_ln(s, color,
 905                                _("  (use \"git rebase --skip\" to skip this patch)"));
 906                        status_printf_ln(s, color,
 907                                _("  (use \"git rebase --abort\" to check out the original branch)"));
 908                }
 909        } else if (state->rebase_in_progress || !stat(git_path("MERGE_MSG"), &st)) {
 910                if (state->branch)
 911                        status_printf_ln(s, color,
 912                                         _("You are currently rebasing branch '%s' on '%s'."),
 913                                         state->branch,
 914                                         state->onto);
 915                else
 916                        status_printf_ln(s, color,
 917                                         _("You are currently rebasing."));
 918                if (advice_status_hints)
 919                        status_printf_ln(s, color,
 920                                _("  (all conflicts fixed: run \"git rebase --continue\")"));
 921        } else if (split_commit_in_progress(s)) {
 922                if (state->branch)
 923                        status_printf_ln(s, color,
 924                                         _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
 925                                         state->branch,
 926                                         state->onto);
 927                else
 928                        status_printf_ln(s, color,
 929                                         _("You are currently splitting a commit during a rebase."));
 930                if (advice_status_hints)
 931                        status_printf_ln(s, color,
 932                                _("  (Once your working directory is clean, run \"git rebase --continue\")"));
 933        } else {
 934                if (state->branch)
 935                        status_printf_ln(s, color,
 936                                         _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
 937                                         state->branch,
 938                                         state->onto);
 939                else
 940                        status_printf_ln(s, color,
 941                                         _("You are currently editing a commit during a rebase."));
 942                if (advice_status_hints && !s->amend) {
 943                        status_printf_ln(s, color,
 944                                _("  (use \"git commit --amend\" to amend the current commit)"));
 945                        status_printf_ln(s, color,
 946                                _("  (use \"git rebase --continue\" once you are satisfied with your changes)"));
 947                }
 948        }
 949        wt_status_print_trailer(s);
 950}
 951
 952static void show_cherry_pick_in_progress(struct wt_status *s,
 953                                        struct wt_status_state *state,
 954                                        const char *color)
 955{
 956        status_printf_ln(s, color, _("You are currently cherry-picking."));
 957        if (advice_status_hints) {
 958                if (has_unmerged(s))
 959                        status_printf_ln(s, color,
 960                                _("  (fix conflicts and run \"git cherry-pick --continue\")"));
 961                else
 962                        status_printf_ln(s, color,
 963                                _("  (all conflicts fixed: run \"git cherry-pick --continue\")"));
 964                status_printf_ln(s, color,
 965                        _("  (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
 966        }
 967        wt_status_print_trailer(s);
 968}
 969
 970static void show_revert_in_progress(struct wt_status *s,
 971                                        struct wt_status_state *state,
 972                                        const char *color)
 973{
 974        status_printf_ln(s, color, _("You are currently reverting commit %s."),
 975                         find_unique_abbrev(state->revert_head_sha1, DEFAULT_ABBREV));
 976        if (advice_status_hints) {
 977                if (has_unmerged(s))
 978                        status_printf_ln(s, color,
 979                                _("  (fix conflicts and run \"git revert --continue\")"));
 980                else
 981                        status_printf_ln(s, color,
 982                                _("  (all conflicts fixed: run \"git revert --continue\")"));
 983                status_printf_ln(s, color,
 984                        _("  (use \"git revert --abort\" to cancel the revert operation)"));
 985        }
 986        wt_status_print_trailer(s);
 987}
 988
 989static void show_bisect_in_progress(struct wt_status *s,
 990                                struct wt_status_state *state,
 991                                const char *color)
 992{
 993        if (state->branch)
 994                status_printf_ln(s, color,
 995                                 _("You are currently bisecting, started from branch '%s'."),
 996                                 state->branch);
 997        else
 998                status_printf_ln(s, color,
 999                                 _("You are currently bisecting."));
1000        if (advice_status_hints)
1001                status_printf_ln(s, color,
1002                        _("  (use \"git bisect reset\" to get back to the original branch)"));
1003        wt_status_print_trailer(s);
1004}
1005
1006/*
1007 * Extract branch information from rebase/bisect
1008 */
1009static char *read_and_strip_branch(const char *path)
1010{
1011        struct strbuf sb = STRBUF_INIT;
1012        unsigned char sha1[20];
1013
1014        if (strbuf_read_file(&sb, git_path("%s", path), 0) <= 0)
1015                goto got_nothing;
1016
1017        while (&sb.len && sb.buf[sb.len - 1] == '\n')
1018                strbuf_setlen(&sb, sb.len - 1);
1019        if (!sb.len)
1020                goto got_nothing;
1021        if (!prefixcmp(sb.buf, "refs/heads/"))
1022                strbuf_remove(&sb,0, strlen("refs/heads/"));
1023        else if (!prefixcmp(sb.buf, "refs/"))
1024                ;
1025        else if (!get_sha1_hex(sb.buf, sha1)) {
1026                const char *abbrev;
1027                abbrev = find_unique_abbrev(sha1, DEFAULT_ABBREV);
1028                strbuf_reset(&sb);
1029                strbuf_addstr(&sb, abbrev);
1030        } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1031                goto got_nothing;
1032        else                    /* bisect */
1033                ;
1034        return strbuf_detach(&sb, NULL);
1035
1036got_nothing:
1037        strbuf_release(&sb);
1038        return NULL;
1039}
1040
1041struct grab_1st_switch_cbdata {
1042        struct strbuf buf;
1043        unsigned char nsha1[20];
1044};
1045
1046static int grab_1st_switch(unsigned char *osha1, unsigned char *nsha1,
1047                           const char *email, unsigned long timestamp, int tz,
1048                           const char *message, void *cb_data)
1049{
1050        struct grab_1st_switch_cbdata *cb = cb_data;
1051        const char *target = NULL, *end;
1052
1053        if (prefixcmp(message, "checkout: moving from "))
1054                return 0;
1055        message += strlen("checkout: moving from ");
1056        target = strstr(message, " to ");
1057        if (!target)
1058                return 0;
1059        target += strlen(" to ");
1060        strbuf_reset(&cb->buf);
1061        hashcpy(cb->nsha1, nsha1);
1062        for (end = target; *end && *end != '\n'; end++)
1063                ;
1064        strbuf_add(&cb->buf, target, end - target);
1065        return 1;
1066}
1067
1068static void wt_status_get_detached_from(struct wt_status_state *state)
1069{
1070        struct grab_1st_switch_cbdata cb;
1071        struct commit *commit;
1072        unsigned char sha1[20];
1073        char *ref = NULL;
1074
1075        strbuf_init(&cb.buf, 0);
1076        if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch, &cb) <= 0) {
1077                strbuf_release(&cb.buf);
1078                return;
1079        }
1080
1081        if (dwim_ref(cb.buf.buf, cb.buf.len, sha1, &ref) == 1 &&
1082            /* sha1 is a commit? match without further lookup */
1083            (!hashcmp(cb.nsha1, sha1) ||
1084             /* perhaps sha1 is a tag, try to dereference to a commit */
1085             ((commit = lookup_commit_reference_gently(sha1, 1)) != NULL &&
1086              !hashcmp(cb.nsha1, commit->object.sha1)))) {
1087                int ofs;
1088                if (!prefixcmp(ref, "refs/tags/"))
1089                        ofs = strlen("refs/tags/");
1090                else if (!prefixcmp(ref, "refs/remotes/"))
1091                        ofs = strlen("refs/remotes/");
1092                else
1093                        ofs = 0;
1094                state->detached_from = xstrdup(ref + ofs);
1095        } else
1096                state->detached_from =
1097                        xstrdup(find_unique_abbrev(cb.nsha1, DEFAULT_ABBREV));
1098        hashcpy(state->detached_sha1, cb.nsha1);
1099
1100        free(ref);
1101        strbuf_release(&cb.buf);
1102}
1103
1104void wt_status_get_state(struct wt_status_state *state,
1105                         int get_detached_from)
1106{
1107        struct stat st;
1108        unsigned char sha1[20];
1109
1110        if (!stat(git_path("MERGE_HEAD"), &st)) {
1111                state->merge_in_progress = 1;
1112        } else if (!stat(git_path("rebase-apply"), &st)) {
1113                if (!stat(git_path("rebase-apply/applying"), &st)) {
1114                        state->am_in_progress = 1;
1115                        if (!stat(git_path("rebase-apply/patch"), &st) && !st.st_size)
1116                                state->am_empty_patch = 1;
1117                } else {
1118                        state->rebase_in_progress = 1;
1119                        state->branch = read_and_strip_branch("rebase-apply/head-name");
1120                        state->onto = read_and_strip_branch("rebase-apply/onto");
1121                }
1122        } else if (!stat(git_path("rebase-merge"), &st)) {
1123                if (!stat(git_path("rebase-merge/interactive"), &st))
1124                        state->rebase_interactive_in_progress = 1;
1125                else
1126                        state->rebase_in_progress = 1;
1127                state->branch = read_and_strip_branch("rebase-merge/head-name");
1128                state->onto = read_and_strip_branch("rebase-merge/onto");
1129        } else if (!stat(git_path("CHERRY_PICK_HEAD"), &st)) {
1130                state->cherry_pick_in_progress = 1;
1131        }
1132        if (!stat(git_path("BISECT_LOG"), &st)) {
1133                state->bisect_in_progress = 1;
1134                state->branch = read_and_strip_branch("BISECT_START");
1135        }
1136        if (!stat(git_path("REVERT_HEAD"), &st) &&
1137            !get_sha1("REVERT_HEAD", sha1)) {
1138                state->revert_in_progress = 1;
1139                hashcpy(state->revert_head_sha1, sha1);
1140        }
1141
1142        if (get_detached_from)
1143                wt_status_get_detached_from(state);
1144}
1145
1146static void wt_status_print_state(struct wt_status *s,
1147                                  struct wt_status_state *state)
1148{
1149        const char *state_color = color(WT_STATUS_HEADER, s);
1150        if (state->merge_in_progress)
1151                show_merge_in_progress(s, state, state_color);
1152        else if (state->am_in_progress)
1153                show_am_in_progress(s, state, state_color);
1154        else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
1155                show_rebase_in_progress(s, state, state_color);
1156        else if (state->cherry_pick_in_progress)
1157                show_cherry_pick_in_progress(s, state, state_color);
1158        else if (state->revert_in_progress)
1159                show_revert_in_progress(s, state, state_color);
1160        if (state->bisect_in_progress)
1161                show_bisect_in_progress(s, state, state_color);
1162}
1163
1164void wt_status_print(struct wt_status *s)
1165{
1166        const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1167        const char *branch_status_color = color(WT_STATUS_HEADER, s);
1168        struct wt_status_state state;
1169
1170        memset(&state, 0, sizeof(state));
1171        wt_status_get_state(&state,
1172                            s->branch && !strcmp(s->branch, "HEAD"));
1173
1174        if (s->branch) {
1175                const char *on_what = _("On branch ");
1176                const char *branch_name = s->branch;
1177                if (!prefixcmp(branch_name, "refs/heads/"))
1178                        branch_name += 11;
1179                else if (!strcmp(branch_name, "HEAD")) {
1180                        branch_status_color = color(WT_STATUS_NOBRANCH, s);
1181                        if (state.rebase_in_progress || state.rebase_interactive_in_progress) {
1182                                on_what = _("rebase in progress; onto ");
1183                                branch_name = state.onto;
1184                        } else if (state.detached_from) {
1185                                unsigned char sha1[20];
1186                                branch_name = state.detached_from;
1187                                if (!get_sha1("HEAD", sha1) &&
1188                                    !hashcmp(sha1, state.detached_sha1))
1189                                        on_what = _("HEAD detached at ");
1190                                else
1191                                        on_what = _("HEAD detached from ");
1192                        } else {
1193                                branch_name = "";
1194                                on_what = _("Not currently on any branch.");
1195                        }
1196                }
1197                status_printf(s, color(WT_STATUS_HEADER, s), "");
1198                status_printf_more(s, branch_status_color, "%s", on_what);
1199                status_printf_more(s, branch_color, "%s\n", branch_name);
1200                if (!s->is_initial)
1201                        wt_status_print_tracking(s);
1202        }
1203
1204        wt_status_print_state(s, &state);
1205        free(state.branch);
1206        free(state.onto);
1207        free(state.detached_from);
1208
1209        if (s->is_initial) {
1210                status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
1211                status_printf_ln(s, color(WT_STATUS_HEADER, s), _("Initial commit"));
1212                status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
1213        }
1214
1215        wt_status_print_updated(s);
1216        wt_status_print_unmerged(s);
1217        wt_status_print_changed(s);
1218        if (s->submodule_summary &&
1219            (!s->ignore_submodule_arg ||
1220             strcmp(s->ignore_submodule_arg, "all"))) {
1221                wt_status_print_submodule_summary(s, 0);  /* staged */
1222                wt_status_print_submodule_summary(s, 1);  /* unstaged */
1223        }
1224        if (s->show_untracked_files) {
1225                wt_status_print_other(s, &s->untracked, _("Untracked files"), "add");
1226                if (s->show_ignored_files)
1227                        wt_status_print_other(s, &s->ignored, _("Ignored files"), "add -f");
1228                if (advice_status_u_option && 2000 < s->untracked_in_ms) {
1229                        status_printf_ln(s, GIT_COLOR_NORMAL, "");
1230                        status_printf_ln(s, GIT_COLOR_NORMAL,
1231                                         _("It took %.2f seconds to enumerate untracked files. 'status -uno'\n"
1232                                           "may speed it up, but you have to be careful not to forget to add\n"
1233                                           "new files yourself (see 'git help status')."),
1234                                         s->untracked_in_ms / 1000.0);
1235                }
1236        } else if (s->commitable)
1237                status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
1238                        advice_status_hints
1239                        ? _(" (use -u option to show untracked files)") : "");
1240
1241        if (s->verbose)
1242                wt_status_print_verbose(s);
1243        if (!s->commitable) {
1244                if (s->amend)
1245                        status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
1246                else if (s->nowarn)
1247                        ; /* nothing */
1248                else if (s->workdir_dirty) {
1249                        if (advice_status_hints)
1250                                printf(_("no changes added to commit "
1251                                         "(use \"git add\" and/or \"git commit -a\")\n"));
1252                        else
1253                                printf(_("no changes added to commit\n"));
1254                } else if (s->untracked.nr) {
1255                        if (advice_status_hints)
1256                                printf(_("nothing added to commit but untracked files "
1257                                         "present (use \"git add\" to track)\n"));
1258                        else
1259                                printf(_("nothing added to commit but untracked files present\n"));
1260                } else if (s->is_initial) {
1261                        if (advice_status_hints)
1262                                printf(_("nothing to commit (create/copy files "
1263                                         "and use \"git add\" to track)\n"));
1264                        else
1265                                printf(_("nothing to commit\n"));
1266                } else if (!s->show_untracked_files) {
1267                        if (advice_status_hints)
1268                                printf(_("nothing to commit (use -u to show untracked files)\n"));
1269                        else
1270                                printf(_("nothing to commit\n"));
1271                } else
1272                        printf(_("nothing to commit, working directory clean\n"));
1273        }
1274}
1275
1276static void wt_shortstatus_unmerged(struct string_list_item *it,
1277                           struct wt_status *s)
1278{
1279        struct wt_status_change_data *d = it->util;
1280        const char *how = "??";
1281
1282        switch (d->stagemask) {
1283        case 1: how = "DD"; break; /* both deleted */
1284        case 2: how = "AU"; break; /* added by us */
1285        case 3: how = "UD"; break; /* deleted by them */
1286        case 4: how = "UA"; break; /* added by them */
1287        case 5: how = "DU"; break; /* deleted by us */
1288        case 6: how = "AA"; break; /* both added */
1289        case 7: how = "UU"; break; /* both modified */
1290        }
1291        color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
1292        if (s->null_termination) {
1293                fprintf(stdout, " %s%c", it->string, 0);
1294        } else {
1295                struct strbuf onebuf = STRBUF_INIT;
1296                const char *one;
1297                one = quote_path(it->string, s->prefix, &onebuf);
1298                printf(" %s\n", one);
1299                strbuf_release(&onebuf);
1300        }
1301}
1302
1303static void wt_shortstatus_status(struct string_list_item *it,
1304                         struct wt_status *s)
1305{
1306        struct wt_status_change_data *d = it->util;
1307
1308        if (d->index_status)
1309                color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
1310        else
1311                putchar(' ');
1312        if (d->worktree_status)
1313                color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
1314        else
1315                putchar(' ');
1316        putchar(' ');
1317        if (s->null_termination) {
1318                fprintf(stdout, "%s%c", it->string, 0);
1319                if (d->head_path)
1320                        fprintf(stdout, "%s%c", d->head_path, 0);
1321        } else {
1322                struct strbuf onebuf = STRBUF_INIT;
1323                const char *one;
1324                if (d->head_path) {
1325                        one = quote_path(d->head_path, s->prefix, &onebuf);
1326                        if (*one != '"' && strchr(one, ' ') != NULL) {
1327                                putchar('"');
1328                                strbuf_addch(&onebuf, '"');
1329                                one = onebuf.buf;
1330                        }
1331                        printf("%s -> ", one);
1332                        strbuf_release(&onebuf);
1333                }
1334                one = quote_path(it->string, s->prefix, &onebuf);
1335                if (*one != '"' && strchr(one, ' ') != NULL) {
1336                        putchar('"');
1337                        strbuf_addch(&onebuf, '"');
1338                        one = onebuf.buf;
1339                }
1340                printf("%s\n", one);
1341                strbuf_release(&onebuf);
1342        }
1343}
1344
1345static void wt_shortstatus_other(struct string_list_item *it,
1346                                 struct wt_status *s, const char *sign)
1347{
1348        if (s->null_termination) {
1349                fprintf(stdout, "%s %s%c", sign, it->string, 0);
1350        } else {
1351                struct strbuf onebuf = STRBUF_INIT;
1352                const char *one;
1353                one = quote_path(it->string, s->prefix, &onebuf);
1354                color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
1355                printf(" %s\n", one);
1356                strbuf_release(&onebuf);
1357        }
1358}
1359
1360static void wt_shortstatus_print_tracking(struct wt_status *s)
1361{
1362        struct branch *branch;
1363        const char *header_color = color(WT_STATUS_HEADER, s);
1364        const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
1365        const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
1366
1367        const char *base;
1368        const char *branch_name;
1369        int num_ours, num_theirs;
1370
1371        color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
1372
1373        if (!s->branch)
1374                return;
1375        branch_name = s->branch;
1376
1377        if (!prefixcmp(branch_name, "refs/heads/"))
1378                branch_name += 11;
1379        else if (!strcmp(branch_name, "HEAD")) {
1380                branch_name = _("HEAD (no branch)");
1381                branch_color_local = color(WT_STATUS_NOBRANCH, s);
1382        }
1383
1384        branch = branch_get(s->branch + 11);
1385        if (s->is_initial)
1386                color_fprintf(s->fp, header_color, _("Initial commit on "));
1387        if (!stat_tracking_info(branch, &num_ours, &num_theirs)) {
1388                color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1389                fputc(s->null_termination ? '\0' : '\n', s->fp);
1390                return;
1391        }
1392
1393        base = branch->merge[0]->dst;
1394        base = shorten_unambiguous_ref(base, 0);
1395        color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1396        color_fprintf(s->fp, header_color, "...");
1397        color_fprintf(s->fp, branch_color_remote, "%s", base);
1398
1399        color_fprintf(s->fp, header_color, " [");
1400        if (!num_ours) {
1401                color_fprintf(s->fp, header_color, _("behind "));
1402                color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1403        } else if (!num_theirs) {
1404                color_fprintf(s->fp, header_color, _("ahead "));
1405                color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1406        } else {
1407                color_fprintf(s->fp, header_color, _("ahead "));
1408                color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1409                color_fprintf(s->fp, header_color, _(", behind "));
1410                color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1411        }
1412
1413        color_fprintf(s->fp, header_color, "]");
1414        fputc(s->null_termination ? '\0' : '\n', s->fp);
1415}
1416
1417void wt_shortstatus_print(struct wt_status *s)
1418{
1419        int i;
1420
1421        if (s->show_branch)
1422                wt_shortstatus_print_tracking(s);
1423
1424        for (i = 0; i < s->change.nr; i++) {
1425                struct wt_status_change_data *d;
1426                struct string_list_item *it;
1427
1428                it = &(s->change.items[i]);
1429                d = it->util;
1430                if (d->stagemask)
1431                        wt_shortstatus_unmerged(it, s);
1432                else
1433                        wt_shortstatus_status(it, s);
1434        }
1435        for (i = 0; i < s->untracked.nr; i++) {
1436                struct string_list_item *it;
1437
1438                it = &(s->untracked.items[i]);
1439                wt_shortstatus_other(it, s, "??");
1440        }
1441        for (i = 0; i < s->ignored.nr; i++) {
1442                struct string_list_item *it;
1443
1444                it = &(s->ignored.items[i]);
1445                wt_shortstatus_other(it, s, "!!");
1446        }
1447}
1448
1449void wt_porcelain_print(struct wt_status *s)
1450{
1451        s->use_color = 0;
1452        s->relative_paths = 0;
1453        s->prefix = NULL;
1454        wt_shortstatus_print(s);
1455}