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