wt-status.con commit Merge branch 'rr/test-3200-style' (0cb24fe)
   1#include "cache.h"
   2#include "wt-status.h"
   3#include "object.h"
   4#include "dir.h"
   5#include "commit.h"
   6#include "diff.h"
   7#include "revision.h"
   8#include "diffcore.h"
   9#include "quote.h"
  10#include "run-command.h"
  11#include "remote.h"
  12#include "refs.h"
  13#include "submodule.h"
  14#include "column.h"
  15#include "strbuf.h"
  16
  17static char default_wt_status_colors[][COLOR_MAXLEN] = {
  18        GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
  19        GIT_COLOR_GREEN,  /* WT_STATUS_UPDATED */
  20        GIT_COLOR_RED,    /* WT_STATUS_CHANGED */
  21        GIT_COLOR_RED,    /* WT_STATUS_UNTRACKED */
  22        GIT_COLOR_RED,    /* WT_STATUS_NOBRANCH */
  23        GIT_COLOR_RED,    /* WT_STATUS_UNMERGED */
  24        GIT_COLOR_GREEN,  /* WT_STATUS_LOCAL_BRANCH */
  25        GIT_COLOR_RED,    /* WT_STATUS_REMOTE_BRANCH */
  26        GIT_COLOR_NIL,    /* WT_STATUS_ONBRANCH */
  27};
  28
  29static const char *color(int slot, struct wt_status *s)
  30{
  31        const char *c = "";
  32        if (want_color(s->use_color))
  33                c = s->color_palette[slot];
  34        if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
  35                c = s->color_palette[WT_STATUS_HEADER];
  36        return c;
  37}
  38
  39static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
  40                const char *fmt, va_list ap, const char *trail)
  41{
  42        struct strbuf sb = STRBUF_INIT;
  43        struct strbuf linebuf = STRBUF_INIT;
  44        const char *line, *eol;
  45
  46        strbuf_vaddf(&sb, fmt, ap);
  47        if (!sb.len) {
  48                strbuf_addch(&sb, comment_line_char);
  49                if (!trail)
  50                        strbuf_addch(&sb, ' ');
  51                color_print_strbuf(s->fp, color, &sb);
  52                if (trail)
  53                        fprintf(s->fp, "%s", trail);
  54                strbuf_release(&sb);
  55                return;
  56        }
  57        for (line = sb.buf; *line; line = eol + 1) {
  58                eol = strchr(line, '\n');
  59
  60                strbuf_reset(&linebuf);
  61                if (at_bol) {
  62                        strbuf_addch(&linebuf, comment_line_char);
  63                        if (*line != '\n' && *line != '\t')
  64                                strbuf_addch(&linebuf, ' ');
  65                }
  66                if (eol)
  67                        strbuf_add(&linebuf, line, eol - line);
  68                else
  69                        strbuf_addstr(&linebuf, line);
  70                color_print_strbuf(s->fp, color, &linebuf);
  71                if (eol)
  72                        fprintf(s->fp, "\n");
  73                else
  74                        break;
  75                at_bol = 1;
  76        }
  77        if (trail)
  78                fprintf(s->fp, "%s", trail);
  79        strbuf_release(&linebuf);
  80        strbuf_release(&sb);
  81}
  82
  83void status_printf_ln(struct wt_status *s, const char *color,
  84                        const char *fmt, ...)
  85{
  86        va_list ap;
  87
  88        va_start(ap, fmt);
  89        status_vprintf(s, 1, color, fmt, ap, "\n");
  90        va_end(ap);
  91}
  92
  93void status_printf(struct wt_status *s, const char *color,
  94                        const char *fmt, ...)
  95{
  96        va_list ap;
  97
  98        va_start(ap, fmt);
  99        status_vprintf(s, 1, color, fmt, ap, NULL);
 100        va_end(ap);
 101}
 102
 103static void status_printf_more(struct wt_status *s, const char *color,
 104                               const char *fmt, ...)
 105{
 106        va_list ap;
 107
 108        va_start(ap, fmt);
 109        status_vprintf(s, 0, color, fmt, ap, NULL);
 110        va_end(ap);
 111}
 112
 113void wt_status_prepare(struct wt_status *s)
 114{
 115        unsigned char sha1[20];
 116
 117        memset(s, 0, sizeof(*s));
 118        memcpy(s->color_palette, default_wt_status_colors,
 119               sizeof(default_wt_status_colors));
 120        s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
 121        s->use_color = -1;
 122        s->relative_paths = 1;
 123        s->branch = resolve_refdup("HEAD", sha1, 0, NULL);
 124        s->reference = "HEAD";
 125        s->fp = stdout;
 126        s->index_file = get_index_file();
 127        s->change.strdup_strings = 1;
 128        s->untracked.strdup_strings = 1;
 129        s->ignored.strdup_strings = 1;
 130}
 131
 132static void wt_status_print_unmerged_header(struct wt_status *s)
 133{
 134        int i;
 135        int del_mod_conflict = 0;
 136        int both_deleted = 0;
 137        int not_deleted = 0;
 138        const char *c = color(WT_STATUS_HEADER, s);
 139
 140        status_printf_ln(s, c, _("Unmerged paths:"));
 141
 142        for (i = 0; i < s->change.nr; i++) {
 143                struct string_list_item *it = &(s->change.items[i]);
 144                struct wt_status_change_data *d = it->util;
 145
 146                switch (d->stagemask) {
 147                case 0:
 148                        break;
 149                case 1:
 150                        both_deleted = 1;
 151                        break;
 152                case 3:
 153                case 5:
 154                        del_mod_conflict = 1;
 155                        break;
 156                default:
 157                        not_deleted = 1;
 158                        break;
 159                }
 160        }
 161
 162        if (!advice_status_hints)
 163                return;
 164        if (s->whence != FROM_COMMIT)
 165                ;
 166        else if (!s->is_initial)
 167                status_printf_ln(s, c, _("  (use \"git reset %s <file>...\" to unstage)"), s->reference);
 168        else
 169                status_printf_ln(s, c, _("  (use \"git rm --cached <file>...\" to unstage)"));
 170
 171        if (!both_deleted) {
 172                if (!del_mod_conflict)
 173                        status_printf_ln(s, c, _("  (use \"git add <file>...\" to mark resolution)"));
 174                else
 175                        status_printf_ln(s, c, _("  (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
 176        } else if (!del_mod_conflict && !not_deleted) {
 177                status_printf_ln(s, c, _("  (use \"git rm <file>...\" to mark resolution)"));
 178        } else {
 179                status_printf_ln(s, c, _("  (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
 180        }
 181        status_printf_ln(s, c, "");
 182}
 183
 184static void wt_status_print_cached_header(struct wt_status *s)
 185{
 186        const char *c = color(WT_STATUS_HEADER, s);
 187
 188        status_printf_ln(s, c, _("Changes to be committed:"));
 189        if (!advice_status_hints)
 190                return;
 191        if (s->whence != FROM_COMMIT)
 192                ; /* NEEDSWORK: use "git reset --unresolve"??? */
 193        else if (!s->is_initial)
 194                status_printf_ln(s, c, _("  (use \"git reset %s <file>...\" to unstage)"), s->reference);
 195        else
 196                status_printf_ln(s, c, _("  (use \"git rm --cached <file>...\" to unstage)"));
 197        status_printf_ln(s, c, "");
 198}
 199
 200static void wt_status_print_dirty_header(struct wt_status *s,
 201                                         int has_deleted,
 202                                         int has_dirty_submodules)
 203{
 204        const char *c = color(WT_STATUS_HEADER, s);
 205
 206        status_printf_ln(s, c, _("Changes not staged for commit:"));
 207        if (!advice_status_hints)
 208                return;
 209        if (!has_deleted)
 210                status_printf_ln(s, c, _("  (use \"git add <file>...\" to update what will be committed)"));
 211        else
 212                status_printf_ln(s, c, _("  (use \"git add/rm <file>...\" to update what will be committed)"));
 213        status_printf_ln(s, c, _("  (use \"git checkout -- <file>...\" to discard changes in working directory)"));
 214        if (has_dirty_submodules)
 215                status_printf_ln(s, c, _("  (commit or discard the untracked or modified content in submodules)"));
 216        status_printf_ln(s, c, "");
 217}
 218
 219static void wt_status_print_other_header(struct wt_status *s,
 220                                         const char *what,
 221                                         const char *how)
 222{
 223        const char *c = color(WT_STATUS_HEADER, s);
 224        status_printf_ln(s, c, "%s:", what);
 225        if (!advice_status_hints)
 226                return;
 227        status_printf_ln(s, c, _("  (use \"git %s <file>...\" to include in what will be committed)"), how);
 228        status_printf_ln(s, c, "");
 229}
 230
 231static void wt_status_print_trailer(struct wt_status *s)
 232{
 233        status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
 234}
 235
 236#define quote_path quote_path_relative
 237
 238static void wt_status_print_unmerged_data(struct wt_status *s,
 239                                          struct string_list_item *it)
 240{
 241        const char *c = color(WT_STATUS_UNMERGED, s);
 242        struct wt_status_change_data *d = it->util;
 243        struct strbuf onebuf = STRBUF_INIT;
 244        const char *one, *how = _("bug");
 245
 246        one = quote_path(it->string, -1, &onebuf, s->prefix);
 247        status_printf(s, color(WT_STATUS_HEADER, s), "\t");
 248        switch (d->stagemask) {
 249        case 1: how = _("both deleted:"); break;
 250        case 2: how = _("added by us:"); break;
 251        case 3: how = _("deleted by them:"); break;
 252        case 4: how = _("added by them:"); break;
 253        case 5: how = _("deleted by us:"); break;
 254        case 6: how = _("both added:"); break;
 255        case 7: how = _("both modified:"); break;
 256        }
 257        status_printf_more(s, c, "%-20s%s\n", how, one);
 258        strbuf_release(&onebuf);
 259}
 260
 261static void wt_status_print_change_data(struct wt_status *s,
 262                                        int change_type,
 263                                        struct string_list_item *it)
 264{
 265        struct wt_status_change_data *d = it->util;
 266        const char *c = color(change_type, s);
 267        int status;
 268        char *one_name;
 269        char *two_name;
 270        const char *one, *two;
 271        struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
 272        struct strbuf extra = STRBUF_INIT;
 273
 274        one_name = two_name = it->string;
 275        switch (change_type) {
 276        case WT_STATUS_UPDATED:
 277                status = d->index_status;
 278                if (d->head_path)
 279                        one_name = d->head_path;
 280                break;
 281        case WT_STATUS_CHANGED:
 282                if (d->new_submodule_commits || d->dirty_submodule) {
 283                        strbuf_addstr(&extra, " (");
 284                        if (d->new_submodule_commits)
 285                                strbuf_addf(&extra, _("new commits, "));
 286                        if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
 287                                strbuf_addf(&extra, _("modified content, "));
 288                        if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
 289                                strbuf_addf(&extra, _("untracked content, "));
 290                        strbuf_setlen(&extra, extra.len - 2);
 291                        strbuf_addch(&extra, ')');
 292                }
 293                status = d->worktree_status;
 294                break;
 295        default:
 296                die("BUG: unhandled change_type %d in wt_status_print_change_data",
 297                    change_type);
 298        }
 299
 300        one = quote_path(one_name, -1, &onebuf, s->prefix);
 301        two = quote_path(two_name, -1, &twobuf, s->prefix);
 302
 303        status_printf(s, color(WT_STATUS_HEADER, s), "\t");
 304        switch (status) {
 305        case DIFF_STATUS_ADDED:
 306                status_printf_more(s, c, _("new file:   %s"), one);
 307                break;
 308        case DIFF_STATUS_COPIED:
 309                status_printf_more(s, c, _("copied:     %s -> %s"), one, two);
 310                break;
 311        case DIFF_STATUS_DELETED:
 312                status_printf_more(s, c, _("deleted:    %s"), one);
 313                break;
 314        case DIFF_STATUS_MODIFIED:
 315                status_printf_more(s, c, _("modified:   %s"), one);
 316                break;
 317        case DIFF_STATUS_RENAMED:
 318                status_printf_more(s, c, _("renamed:    %s -> %s"), one, two);
 319                break;
 320        case DIFF_STATUS_TYPE_CHANGED:
 321                status_printf_more(s, c, _("typechange: %s"), one);
 322                break;
 323        case DIFF_STATUS_UNKNOWN:
 324                status_printf_more(s, c, _("unknown:    %s"), one);
 325                break;
 326        case DIFF_STATUS_UNMERGED:
 327                status_printf_more(s, c, _("unmerged:   %s"), one);
 328                break;
 329        default:
 330                die(_("bug: unhandled diff status %c"), status);
 331        }
 332        if (extra.len) {
 333                status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
 334                strbuf_release(&extra);
 335        }
 336        status_printf_more(s, GIT_COLOR_NORMAL, "\n");
 337        strbuf_release(&onebuf);
 338        strbuf_release(&twobuf);
 339}
 340
 341static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
 342                                         struct diff_options *options,
 343                                         void *data)
 344{
 345        struct wt_status *s = data;
 346        int i;
 347
 348        if (!q->nr)
 349                return;
 350        s->workdir_dirty = 1;
 351        for (i = 0; i < q->nr; i++) {
 352                struct diff_filepair *p;
 353                struct string_list_item *it;
 354                struct wt_status_change_data *d;
 355
 356                p = q->queue[i];
 357                it = string_list_insert(&s->change, p->one->path);
 358                d = it->util;
 359                if (!d) {
 360                        d = xcalloc(1, sizeof(*d));
 361                        it->util = d;
 362                }
 363                if (!d->worktree_status)
 364                        d->worktree_status = p->status;
 365                d->dirty_submodule = p->two->dirty_submodule;
 366                if (S_ISGITLINK(p->two->mode))
 367                        d->new_submodule_commits = !!hashcmp(p->one->sha1, p->two->sha1);
 368        }
 369}
 370
 371static int unmerged_mask(const char *path)
 372{
 373        int pos, mask;
 374        struct cache_entry *ce;
 375
 376        pos = cache_name_pos(path, strlen(path));
 377        if (0 <= pos)
 378                return 0;
 379
 380        mask = 0;
 381        pos = -pos-1;
 382        while (pos < active_nr) {
 383                ce = active_cache[pos++];
 384                if (strcmp(ce->name, path) || !ce_stage(ce))
 385                        break;
 386                mask |= (1 << (ce_stage(ce) - 1));
 387        }
 388        return mask;
 389}
 390
 391static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
 392                                         struct diff_options *options,
 393                                         void *data)
 394{
 395        struct wt_status *s = data;
 396        int i;
 397
 398        for (i = 0; i < q->nr; i++) {
 399                struct diff_filepair *p;
 400                struct string_list_item *it;
 401                struct wt_status_change_data *d;
 402
 403                p = q->queue[i];
 404                it = string_list_insert(&s->change, p->two->path);
 405                d = it->util;
 406                if (!d) {
 407                        d = xcalloc(1, sizeof(*d));
 408                        it->util = d;
 409                }
 410                if (!d->index_status)
 411                        d->index_status = p->status;
 412                switch (p->status) {
 413                case DIFF_STATUS_COPIED:
 414                case DIFF_STATUS_RENAMED:
 415                        d->head_path = xstrdup(p->one->path);
 416                        break;
 417                case DIFF_STATUS_UNMERGED:
 418                        d->stagemask = unmerged_mask(p->two->path);
 419                        break;
 420                }
 421        }
 422}
 423
 424static void wt_status_collect_changes_worktree(struct wt_status *s)
 425{
 426        struct rev_info rev;
 427
 428        init_revisions(&rev, NULL);
 429        setup_revisions(0, NULL, &rev, NULL);
 430        rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
 431        DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES);
 432        if (!s->show_untracked_files)
 433                DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
 434        if (s->ignore_submodule_arg) {
 435                DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
 436                handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
 437        }
 438        rev.diffopt.format_callback = wt_status_collect_changed_cb;
 439        rev.diffopt.format_callback_data = s;
 440        init_pathspec(&rev.prune_data, s->pathspec);
 441        run_diff_files(&rev, 0);
 442}
 443
 444static void wt_status_collect_changes_index(struct wt_status *s)
 445{
 446        struct rev_info rev;
 447        struct setup_revision_opt opt;
 448
 449        init_revisions(&rev, NULL);
 450        memset(&opt, 0, sizeof(opt));
 451        opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
 452        setup_revisions(0, NULL, &rev, &opt);
 453
 454        if (s->ignore_submodule_arg) {
 455                DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
 456                handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
 457        }
 458
 459        rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
 460        rev.diffopt.format_callback = wt_status_collect_updated_cb;
 461        rev.diffopt.format_callback_data = s;
 462        rev.diffopt.detect_rename = 1;
 463        rev.diffopt.rename_limit = 200;
 464        rev.diffopt.break_opt = 0;
 465        init_pathspec(&rev.prune_data, s->pathspec);
 466        run_diff_index(&rev, 1);
 467}
 468
 469static void wt_status_collect_changes_initial(struct wt_status *s)
 470{
 471        struct pathspec pathspec;
 472        int i;
 473
 474        init_pathspec(&pathspec, s->pathspec);
 475        for (i = 0; i < active_nr; i++) {
 476                struct string_list_item *it;
 477                struct wt_status_change_data *d;
 478                struct cache_entry *ce = active_cache[i];
 479
 480                if (!ce_path_match(ce, &pathspec))
 481                        continue;
 482                it = string_list_insert(&s->change, ce->name);
 483                d = it->util;
 484                if (!d) {
 485                        d = xcalloc(1, sizeof(*d));
 486                        it->util = d;
 487                }
 488                if (ce_stage(ce)) {
 489                        d->index_status = DIFF_STATUS_UNMERGED;
 490                        d->stagemask |= (1 << (ce_stage(ce) - 1));
 491                }
 492                else
 493                        d->index_status = DIFF_STATUS_ADDED;
 494        }
 495        free_pathspec(&pathspec);
 496}
 497
 498static void wt_status_collect_untracked(struct wt_status *s)
 499{
 500        int i;
 501        struct dir_struct dir;
 502        struct timeval t_begin;
 503
 504        if (!s->show_untracked_files)
 505                return;
 506
 507        if (advice_status_u_option)
 508                gettimeofday(&t_begin, NULL);
 509
 510        memset(&dir, 0, sizeof(dir));
 511        if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
 512                dir.flags |=
 513                        DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
 514        setup_standard_excludes(&dir);
 515
 516        fill_directory(&dir, s->pathspec);
 517        for (i = 0; i < dir.nr; i++) {
 518                struct dir_entry *ent = dir.entries[i];
 519                if (cache_name_is_other(ent->name, ent->len) &&
 520                    match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
 521                        string_list_insert(&s->untracked, ent->name);
 522                free(ent);
 523        }
 524
 525        if (s->show_ignored_files) {
 526                dir.nr = 0;
 527                dir.flags = DIR_SHOW_IGNORED;
 528                if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
 529                        dir.flags |= DIR_SHOW_OTHER_DIRECTORIES;
 530                fill_directory(&dir, s->pathspec);
 531                for (i = 0; i < dir.nr; i++) {
 532                        struct dir_entry *ent = dir.entries[i];
 533                        if (cache_name_is_other(ent->name, ent->len) &&
 534                            match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
 535                                string_list_insert(&s->ignored, ent->name);
 536                        free(ent);
 537                }
 538        }
 539
 540        free(dir.entries);
 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        const char *argv[8];
 668
 669        env[0] =        index;
 670        argv[0] =       "submodule";
 671        argv[1] =       "summary";
 672        argv[2] =       uncommitted ? "--files" : "--cached";
 673        argv[3] =       "--for-status";
 674        argv[4] =       "--summary-limit";
 675        argv[5] =       summary_limit;
 676        argv[6] =       uncommitted ? NULL : (s->amend ? "HEAD^" : "HEAD");
 677        argv[7] =       NULL;
 678
 679        sprintf(summary_limit, "%d", s->submodule_summary);
 680        snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
 681
 682        memset(&sm_summary, 0, sizeof(sm_summary));
 683        sm_summary.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}
 691
 692static void wt_status_print_other(struct wt_status *s,
 693                                  struct string_list *l,
 694                                  const char *what,
 695                                  const char *how)
 696{
 697        int i;
 698        struct strbuf buf = STRBUF_INIT;
 699        static struct string_list output = STRING_LIST_INIT_DUP;
 700        struct column_options copts;
 701
 702        if (!l->nr)
 703                return;
 704
 705        wt_status_print_other_header(s, what, how);
 706
 707        for (i = 0; i < l->nr; i++) {
 708                struct string_list_item *it;
 709                const char *path;
 710                it = &(l->items[i]);
 711                path = quote_path(it->string, strlen(it->string),
 712                                  &buf, s->prefix);
 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 --resolved\")"));
 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 commit\")"));
 961                else
 962                        status_printf_ln(s, color,
 963                                _("  (all conflicts fixed: run \"git commit\")"));
 964        }
 965        wt_status_print_trailer(s);
 966}
 967
 968static void show_bisect_in_progress(struct wt_status *s,
 969                                struct wt_status_state *state,
 970                                const char *color)
 971{
 972        if (state->branch)
 973                status_printf_ln(s, color,
 974                                 _("You are currently bisecting branch '%s'."),
 975                                 state->branch);
 976        else
 977                status_printf_ln(s, color,
 978                                 _("You are currently bisecting."));
 979        if (advice_status_hints)
 980                status_printf_ln(s, color,
 981                        _("  (use \"git bisect reset\" to get back to the original branch)"));
 982        wt_status_print_trailer(s);
 983}
 984
 985/*
 986 * Extract branch information from rebase/bisect
 987 */
 988static void read_and_strip_branch(struct strbuf *sb,
 989                                  const char **branch,
 990                                  const char *path)
 991{
 992        unsigned char sha1[20];
 993
 994        strbuf_reset(sb);
 995        if (strbuf_read_file(sb, git_path("%s", path), 0) <= 0)
 996                return;
 997
 998        while (sb->len && sb->buf[sb->len - 1] == '\n')
 999                strbuf_setlen(sb, sb->len - 1);
1000        if (!sb->len)
1001                return;
1002        if (!prefixcmp(sb->buf, "refs/heads/"))
1003                *branch = sb->buf + strlen("refs/heads/");
1004        else if (!prefixcmp(sb->buf, "refs/"))
1005                *branch = sb->buf;
1006        else if (!get_sha1_hex(sb->buf, sha1)) {
1007                const char *abbrev;
1008                abbrev = find_unique_abbrev(sha1, DEFAULT_ABBREV);
1009                strbuf_reset(sb);
1010                strbuf_addstr(sb, abbrev);
1011                *branch = sb->buf;
1012        } else if (!strcmp(sb->buf, "detached HEAD")) /* rebase */
1013                ;
1014        else                    /* bisect */
1015                *branch = sb->buf;
1016}
1017
1018static void wt_status_print_state(struct wt_status *s)
1019{
1020        const char *state_color = color(WT_STATUS_HEADER, s);
1021        struct strbuf branch = STRBUF_INIT;
1022        struct strbuf onto = STRBUF_INIT;
1023        struct wt_status_state state;
1024        struct stat st;
1025
1026        memset(&state, 0, sizeof(state));
1027
1028        if (!stat(git_path("MERGE_HEAD"), &st)) {
1029                state.merge_in_progress = 1;
1030        } else if (!stat(git_path("rebase-apply"), &st)) {
1031                if (!stat(git_path("rebase-apply/applying"), &st)) {
1032                        state.am_in_progress = 1;
1033                        if (!stat(git_path("rebase-apply/patch"), &st) && !st.st_size)
1034                                state.am_empty_patch = 1;
1035                } else {
1036                        state.rebase_in_progress = 1;
1037                        read_and_strip_branch(&branch, &state.branch,
1038                                              "rebase-apply/head-name");
1039                        read_and_strip_branch(&onto, &state.onto,
1040                                              "rebase-apply/onto");
1041                }
1042        } else if (!stat(git_path("rebase-merge"), &st)) {
1043                if (!stat(git_path("rebase-merge/interactive"), &st))
1044                        state.rebase_interactive_in_progress = 1;
1045                else
1046                        state.rebase_in_progress = 1;
1047                read_and_strip_branch(&branch, &state.branch,
1048                                      "rebase-merge/head-name");
1049                read_and_strip_branch(&onto, &state.onto,
1050                                      "rebase-merge/onto");
1051        } else if (!stat(git_path("CHERRY_PICK_HEAD"), &st)) {
1052                state.cherry_pick_in_progress = 1;
1053        }
1054        if (!stat(git_path("BISECT_LOG"), &st)) {
1055                state.bisect_in_progress = 1;
1056                read_and_strip_branch(&branch, &state.branch,
1057                                      "BISECT_START");
1058        }
1059
1060        if (state.merge_in_progress)
1061                show_merge_in_progress(s, &state, state_color);
1062        else if (state.am_in_progress)
1063                show_am_in_progress(s, &state, state_color);
1064        else if (state.rebase_in_progress || state.rebase_interactive_in_progress)
1065                show_rebase_in_progress(s, &state, state_color);
1066        else if (state.cherry_pick_in_progress)
1067                show_cherry_pick_in_progress(s, &state, state_color);
1068        if (state.bisect_in_progress)
1069                show_bisect_in_progress(s, &state, state_color);
1070        strbuf_release(&branch);
1071        strbuf_release(&onto);
1072}
1073
1074void wt_status_print(struct wt_status *s)
1075{
1076        const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1077        const char *branch_status_color = color(WT_STATUS_HEADER, s);
1078
1079        if (s->branch) {
1080                const char *on_what = _("On branch ");
1081                const char *branch_name = s->branch;
1082                if (!prefixcmp(branch_name, "refs/heads/"))
1083                        branch_name += 11;
1084                else if (!strcmp(branch_name, "HEAD")) {
1085                        branch_name = "";
1086                        branch_status_color = color(WT_STATUS_NOBRANCH, s);
1087                        on_what = _("Not currently on any branch.");
1088                }
1089                status_printf(s, color(WT_STATUS_HEADER, s), "");
1090                status_printf_more(s, branch_status_color, "%s", on_what);
1091                status_printf_more(s, branch_color, "%s\n", branch_name);
1092                if (!s->is_initial)
1093                        wt_status_print_tracking(s);
1094        }
1095
1096        wt_status_print_state(s);
1097        if (s->is_initial) {
1098                status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
1099                status_printf_ln(s, color(WT_STATUS_HEADER, s), _("Initial commit"));
1100                status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
1101        }
1102
1103        wt_status_print_updated(s);
1104        wt_status_print_unmerged(s);
1105        wt_status_print_changed(s);
1106        if (s->submodule_summary &&
1107            (!s->ignore_submodule_arg ||
1108             strcmp(s->ignore_submodule_arg, "all"))) {
1109                wt_status_print_submodule_summary(s, 0);  /* staged */
1110                wt_status_print_submodule_summary(s, 1);  /* unstaged */
1111        }
1112        if (s->show_untracked_files) {
1113                wt_status_print_other(s, &s->untracked, _("Untracked files"), "add");
1114                if (s->show_ignored_files)
1115                        wt_status_print_other(s, &s->ignored, _("Ignored files"), "add -f");
1116                if (advice_status_u_option && 2000 < s->untracked_in_ms) {
1117                        status_printf_ln(s, GIT_COLOR_NORMAL, "");
1118                        status_printf_ln(s, GIT_COLOR_NORMAL,
1119                                 _("It took %.2f seconds to enumerate untracked files."
1120                                   "  'status -uno'"),
1121                                 s->untracked_in_ms / 1000.0);
1122                        status_printf_ln(s, GIT_COLOR_NORMAL,
1123                                 _("may speed it up, but you have to be careful not"
1124                                   " to forget to add"));
1125                        status_printf_ln(s, GIT_COLOR_NORMAL,
1126                                 _("new files yourself (see 'git help status')."));
1127                }
1128        } else if (s->commitable)
1129                status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
1130                        advice_status_hints
1131                        ? _(" (use -u option to show untracked files)") : "");
1132
1133        if (s->verbose)
1134                wt_status_print_verbose(s);
1135        if (!s->commitable) {
1136                if (s->amend)
1137                        status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
1138                else if (s->nowarn)
1139                        ; /* nothing */
1140                else if (s->workdir_dirty) {
1141                        if (advice_status_hints)
1142                                printf(_("no changes added to commit "
1143                                         "(use \"git add\" and/or \"git commit -a\")\n"));
1144                        else
1145                                printf(_("no changes added to commit\n"));
1146                } else if (s->untracked.nr) {
1147                        if (advice_status_hints)
1148                                printf(_("nothing added to commit but untracked files "
1149                                         "present (use \"git add\" to track)\n"));
1150                        else
1151                                printf(_("nothing added to commit but untracked files present\n"));
1152                } else if (s->is_initial) {
1153                        if (advice_status_hints)
1154                                printf(_("nothing to commit (create/copy files "
1155                                         "and use \"git add\" to track)\n"));
1156                        else
1157                                printf(_("nothing to commit\n"));
1158                } else if (!s->show_untracked_files) {
1159                        if (advice_status_hints)
1160                                printf(_("nothing to commit (use -u to show untracked files)\n"));
1161                        else
1162                                printf(_("nothing to commit\n"));
1163                } else
1164                        printf(_("nothing to commit, working directory clean\n"));
1165        }
1166}
1167
1168static void wt_shortstatus_unmerged(struct string_list_item *it,
1169                           struct wt_status *s)
1170{
1171        struct wt_status_change_data *d = it->util;
1172        const char *how = "??";
1173
1174        switch (d->stagemask) {
1175        case 1: how = "DD"; break; /* both deleted */
1176        case 2: how = "AU"; break; /* added by us */
1177        case 3: how = "UD"; break; /* deleted by them */
1178        case 4: how = "UA"; break; /* added by them */
1179        case 5: how = "DU"; break; /* deleted by us */
1180        case 6: how = "AA"; break; /* both added */
1181        case 7: how = "UU"; break; /* both modified */
1182        }
1183        color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
1184        if (s->null_termination) {
1185                fprintf(stdout, " %s%c", it->string, 0);
1186        } else {
1187                struct strbuf onebuf = STRBUF_INIT;
1188                const char *one;
1189                one = quote_path(it->string, -1, &onebuf, s->prefix);
1190                printf(" %s\n", one);
1191                strbuf_release(&onebuf);
1192        }
1193}
1194
1195static void wt_shortstatus_status(struct string_list_item *it,
1196                         struct wt_status *s)
1197{
1198        struct wt_status_change_data *d = it->util;
1199
1200        if (d->index_status)
1201                color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
1202        else
1203                putchar(' ');
1204        if (d->worktree_status)
1205                color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
1206        else
1207                putchar(' ');
1208        putchar(' ');
1209        if (s->null_termination) {
1210                fprintf(stdout, "%s%c", it->string, 0);
1211                if (d->head_path)
1212                        fprintf(stdout, "%s%c", d->head_path, 0);
1213        } else {
1214                struct strbuf onebuf = STRBUF_INIT;
1215                const char *one;
1216                if (d->head_path) {
1217                        one = quote_path(d->head_path, -1, &onebuf, s->prefix);
1218                        if (*one != '"' && strchr(one, ' ') != NULL) {
1219                                putchar('"');
1220                                strbuf_addch(&onebuf, '"');
1221                                one = onebuf.buf;
1222                        }
1223                        printf("%s -> ", one);
1224                        strbuf_release(&onebuf);
1225                }
1226                one = quote_path(it->string, -1, &onebuf, s->prefix);
1227                if (*one != '"' && strchr(one, ' ') != NULL) {
1228                        putchar('"');
1229                        strbuf_addch(&onebuf, '"');
1230                        one = onebuf.buf;
1231                }
1232                printf("%s\n", one);
1233                strbuf_release(&onebuf);
1234        }
1235}
1236
1237static void wt_shortstatus_other(struct string_list_item *it,
1238                                 struct wt_status *s, const char *sign)
1239{
1240        if (s->null_termination) {
1241                fprintf(stdout, "%s %s%c", sign, it->string, 0);
1242        } else {
1243                struct strbuf onebuf = STRBUF_INIT;
1244                const char *one;
1245                one = quote_path(it->string, -1, &onebuf, s->prefix);
1246                color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
1247                printf(" %s\n", one);
1248                strbuf_release(&onebuf);
1249        }
1250}
1251
1252static void wt_shortstatus_print_tracking(struct wt_status *s)
1253{
1254        struct branch *branch;
1255        const char *header_color = color(WT_STATUS_HEADER, s);
1256        const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
1257        const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
1258
1259        const char *base;
1260        const char *branch_name;
1261        int num_ours, num_theirs;
1262
1263        color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
1264
1265        if (!s->branch)
1266                return;
1267        branch_name = s->branch;
1268
1269        if (!prefixcmp(branch_name, "refs/heads/"))
1270                branch_name += 11;
1271        else if (!strcmp(branch_name, "HEAD")) {
1272                branch_name = _("HEAD (no branch)");
1273                branch_color_local = color(WT_STATUS_NOBRANCH, s);
1274        }
1275
1276        branch = branch_get(s->branch + 11);
1277        if (s->is_initial)
1278                color_fprintf(s->fp, header_color, _("Initial commit on "));
1279        if (!stat_tracking_info(branch, &num_ours, &num_theirs)) {
1280                color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1281                fputc(s->null_termination ? '\0' : '\n', s->fp);
1282                return;
1283        }
1284
1285        base = branch->merge[0]->dst;
1286        base = shorten_unambiguous_ref(base, 0);
1287        color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1288        color_fprintf(s->fp, header_color, "...");
1289        color_fprintf(s->fp, branch_color_remote, "%s", base);
1290
1291        color_fprintf(s->fp, header_color, " [");
1292        if (!num_ours) {
1293                color_fprintf(s->fp, header_color, _("behind "));
1294                color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1295        } else if (!num_theirs) {
1296                color_fprintf(s->fp, header_color, _("ahead "));
1297                color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1298        } else {
1299                color_fprintf(s->fp, header_color, _("ahead "));
1300                color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1301                color_fprintf(s->fp, header_color, _(", behind "));
1302                color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1303        }
1304
1305        color_fprintf(s->fp, header_color, "]");
1306        fputc(s->null_termination ? '\0' : '\n', s->fp);
1307}
1308
1309void wt_shortstatus_print(struct wt_status *s)
1310{
1311        int i;
1312
1313        if (s->show_branch)
1314                wt_shortstatus_print_tracking(s);
1315
1316        for (i = 0; i < s->change.nr; i++) {
1317                struct wt_status_change_data *d;
1318                struct string_list_item *it;
1319
1320                it = &(s->change.items[i]);
1321                d = it->util;
1322                if (d->stagemask)
1323                        wt_shortstatus_unmerged(it, s);
1324                else
1325                        wt_shortstatus_status(it, s);
1326        }
1327        for (i = 0; i < s->untracked.nr; i++) {
1328                struct string_list_item *it;
1329
1330                it = &(s->untracked.items[i]);
1331                wt_shortstatus_other(it, s, "??");
1332        }
1333        for (i = 0; i < s->ignored.nr; i++) {
1334                struct string_list_item *it;
1335
1336                it = &(s->ignored.items[i]);
1337                wt_shortstatus_other(it, s, "!!");
1338        }
1339}
1340
1341void wt_porcelain_print(struct wt_status *s)
1342{
1343        s->use_color = 0;
1344        s->relative_paths = 0;
1345        s->prefix = NULL;
1346        wt_shortstatus_print(s);
1347}