wt-status.con commit Merge branch 'tb/document-status-u-tradeoff' (5d04924)
   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 = 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        }
 296
 297        one = quote_path(one_name, -1, &onebuf, s->prefix);
 298        two = quote_path(two_name, -1, &twobuf, s->prefix);
 299
 300        status_printf(s, color(WT_STATUS_HEADER, s), "\t");
 301        switch (status) {
 302        case DIFF_STATUS_ADDED:
 303                status_printf_more(s, c, _("new file:   %s"), one);
 304                break;
 305        case DIFF_STATUS_COPIED:
 306                status_printf_more(s, c, _("copied:     %s -> %s"), one, two);
 307                break;
 308        case DIFF_STATUS_DELETED:
 309                status_printf_more(s, c, _("deleted:    %s"), one);
 310                break;
 311        case DIFF_STATUS_MODIFIED:
 312                status_printf_more(s, c, _("modified:   %s"), one);
 313                break;
 314        case DIFF_STATUS_RENAMED:
 315                status_printf_more(s, c, _("renamed:    %s -> %s"), one, two);
 316                break;
 317        case DIFF_STATUS_TYPE_CHANGED:
 318                status_printf_more(s, c, _("typechange: %s"), one);
 319                break;
 320        case DIFF_STATUS_UNKNOWN:
 321                status_printf_more(s, c, _("unknown:    %s"), one);
 322                break;
 323        case DIFF_STATUS_UNMERGED:
 324                status_printf_more(s, c, _("unmerged:   %s"), one);
 325                break;
 326        default:
 327                die(_("bug: unhandled diff status %c"), status);
 328        }
 329        if (extra.len) {
 330                status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
 331                strbuf_release(&extra);
 332        }
 333        status_printf_more(s, GIT_COLOR_NORMAL, "\n");
 334        strbuf_release(&onebuf);
 335        strbuf_release(&twobuf);
 336}
 337
 338static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
 339                                         struct diff_options *options,
 340                                         void *data)
 341{
 342        struct wt_status *s = data;
 343        int i;
 344
 345        if (!q->nr)
 346                return;
 347        s->workdir_dirty = 1;
 348        for (i = 0; i < q->nr; i++) {
 349                struct diff_filepair *p;
 350                struct string_list_item *it;
 351                struct wt_status_change_data *d;
 352
 353                p = q->queue[i];
 354                it = string_list_insert(&s->change, p->one->path);
 355                d = it->util;
 356                if (!d) {
 357                        d = xcalloc(1, sizeof(*d));
 358                        it->util = d;
 359                }
 360                if (!d->worktree_status)
 361                        d->worktree_status = p->status;
 362                d->dirty_submodule = p->two->dirty_submodule;
 363                if (S_ISGITLINK(p->two->mode))
 364                        d->new_submodule_commits = !!hashcmp(p->one->sha1, p->two->sha1);
 365        }
 366}
 367
 368static int unmerged_mask(const char *path)
 369{
 370        int pos, mask;
 371        struct cache_entry *ce;
 372
 373        pos = cache_name_pos(path, strlen(path));
 374        if (0 <= pos)
 375                return 0;
 376
 377        mask = 0;
 378        pos = -pos-1;
 379        while (pos < active_nr) {
 380                ce = active_cache[pos++];
 381                if (strcmp(ce->name, path) || !ce_stage(ce))
 382                        break;
 383                mask |= (1 << (ce_stage(ce) - 1));
 384        }
 385        return mask;
 386}
 387
 388static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
 389                                         struct diff_options *options,
 390                                         void *data)
 391{
 392        struct wt_status *s = data;
 393        int i;
 394
 395        for (i = 0; i < q->nr; i++) {
 396                struct diff_filepair *p;
 397                struct string_list_item *it;
 398                struct wt_status_change_data *d;
 399
 400                p = q->queue[i];
 401                it = string_list_insert(&s->change, p->two->path);
 402                d = it->util;
 403                if (!d) {
 404                        d = xcalloc(1, sizeof(*d));
 405                        it->util = d;
 406                }
 407                if (!d->index_status)
 408                        d->index_status = p->status;
 409                switch (p->status) {
 410                case DIFF_STATUS_COPIED:
 411                case DIFF_STATUS_RENAMED:
 412                        d->head_path = xstrdup(p->one->path);
 413                        break;
 414                case DIFF_STATUS_UNMERGED:
 415                        d->stagemask = unmerged_mask(p->two->path);
 416                        break;
 417                }
 418        }
 419}
 420
 421static void wt_status_collect_changes_worktree(struct wt_status *s)
 422{
 423        struct rev_info rev;
 424
 425        init_revisions(&rev, NULL);
 426        setup_revisions(0, NULL, &rev, NULL);
 427        rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
 428        DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES);
 429        if (!s->show_untracked_files)
 430                DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
 431        if (s->ignore_submodule_arg) {
 432                DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
 433                handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
 434        }
 435        rev.diffopt.format_callback = wt_status_collect_changed_cb;
 436        rev.diffopt.format_callback_data = s;
 437        init_pathspec(&rev.prune_data, s->pathspec);
 438        run_diff_files(&rev, 0);
 439}
 440
 441static void wt_status_collect_changes_index(struct wt_status *s)
 442{
 443        struct rev_info rev;
 444        struct setup_revision_opt opt;
 445
 446        init_revisions(&rev, NULL);
 447        memset(&opt, 0, sizeof(opt));
 448        opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
 449        setup_revisions(0, NULL, &rev, &opt);
 450
 451        if (s->ignore_submodule_arg) {
 452                DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
 453                handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
 454        }
 455
 456        rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
 457        rev.diffopt.format_callback = wt_status_collect_updated_cb;
 458        rev.diffopt.format_callback_data = s;
 459        rev.diffopt.detect_rename = 1;
 460        rev.diffopt.rename_limit = 200;
 461        rev.diffopt.break_opt = 0;
 462        init_pathspec(&rev.prune_data, s->pathspec);
 463        run_diff_index(&rev, 1);
 464}
 465
 466static void wt_status_collect_changes_initial(struct wt_status *s)
 467{
 468        struct pathspec pathspec;
 469        int i;
 470
 471        init_pathspec(&pathspec, s->pathspec);
 472        for (i = 0; i < active_nr; i++) {
 473                struct string_list_item *it;
 474                struct wt_status_change_data *d;
 475                struct cache_entry *ce = active_cache[i];
 476
 477                if (!ce_path_match(ce, &pathspec))
 478                        continue;
 479                it = string_list_insert(&s->change, ce->name);
 480                d = it->util;
 481                if (!d) {
 482                        d = xcalloc(1, sizeof(*d));
 483                        it->util = d;
 484                }
 485                if (ce_stage(ce)) {
 486                        d->index_status = DIFF_STATUS_UNMERGED;
 487                        d->stagemask |= (1 << (ce_stage(ce) - 1));
 488                }
 489                else
 490                        d->index_status = DIFF_STATUS_ADDED;
 491        }
 492        free_pathspec(&pathspec);
 493}
 494
 495static void wt_status_collect_untracked(struct wt_status *s)
 496{
 497        int i;
 498        struct dir_struct dir;
 499        struct timeval t_begin;
 500
 501        if (!s->show_untracked_files)
 502                return;
 503
 504        if (advice_status_u_option)
 505                gettimeofday(&t_begin, NULL);
 506
 507        memset(&dir, 0, sizeof(dir));
 508        if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
 509                dir.flags |=
 510                        DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
 511        setup_standard_excludes(&dir);
 512
 513        fill_directory(&dir, s->pathspec);
 514        for (i = 0; i < dir.nr; i++) {
 515                struct dir_entry *ent = dir.entries[i];
 516                if (cache_name_is_other(ent->name, ent->len) &&
 517                    match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
 518                        string_list_insert(&s->untracked, ent->name);
 519                free(ent);
 520        }
 521
 522        if (s->show_ignored_files) {
 523                dir.nr = 0;
 524                dir.flags = DIR_SHOW_IGNORED;
 525                if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
 526                        dir.flags |= DIR_SHOW_OTHER_DIRECTORIES;
 527                fill_directory(&dir, s->pathspec);
 528                for (i = 0; i < dir.nr; i++) {
 529                        struct dir_entry *ent = dir.entries[i];
 530                        if (cache_name_is_other(ent->name, ent->len) &&
 531                            match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
 532                                string_list_insert(&s->ignored, ent->name);
 533                        free(ent);
 534                }
 535        }
 536
 537        free(dir.entries);
 538
 539        if (advice_status_u_option) {
 540                struct timeval t_end;
 541                gettimeofday(&t_end, NULL);
 542                s->untracked_in_ms =
 543                        (uint64_t)t_end.tv_sec * 1000 + t_end.tv_usec / 1000 -
 544                        ((uint64_t)t_begin.tv_sec * 1000 + t_begin.tv_usec / 1000);
 545        }
 546}
 547
 548void wt_status_collect(struct wt_status *s)
 549{
 550        wt_status_collect_changes_worktree(s);
 551
 552        if (s->is_initial)
 553                wt_status_collect_changes_initial(s);
 554        else
 555                wt_status_collect_changes_index(s);
 556        wt_status_collect_untracked(s);
 557}
 558
 559static void wt_status_print_unmerged(struct wt_status *s)
 560{
 561        int shown_header = 0;
 562        int i;
 563
 564        for (i = 0; i < s->change.nr; i++) {
 565                struct wt_status_change_data *d;
 566                struct string_list_item *it;
 567                it = &(s->change.items[i]);
 568                d = it->util;
 569                if (!d->stagemask)
 570                        continue;
 571                if (!shown_header) {
 572                        wt_status_print_unmerged_header(s);
 573                        shown_header = 1;
 574                }
 575                wt_status_print_unmerged_data(s, it);
 576        }
 577        if (shown_header)
 578                wt_status_print_trailer(s);
 579
 580}
 581
 582static void wt_status_print_updated(struct wt_status *s)
 583{
 584        int shown_header = 0;
 585        int i;
 586
 587        for (i = 0; i < s->change.nr; i++) {
 588                struct wt_status_change_data *d;
 589                struct string_list_item *it;
 590                it = &(s->change.items[i]);
 591                d = it->util;
 592                if (!d->index_status ||
 593                    d->index_status == DIFF_STATUS_UNMERGED)
 594                        continue;
 595                if (!shown_header) {
 596                        wt_status_print_cached_header(s);
 597                        s->commitable = 1;
 598                        shown_header = 1;
 599                }
 600                wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
 601        }
 602        if (shown_header)
 603                wt_status_print_trailer(s);
 604}
 605
 606/*
 607 * -1 : has delete
 608 *  0 : no change
 609 *  1 : some change but no delete
 610 */
 611static int wt_status_check_worktree_changes(struct wt_status *s,
 612                                             int *dirty_submodules)
 613{
 614        int i;
 615        int changes = 0;
 616
 617        *dirty_submodules = 0;
 618
 619        for (i = 0; i < s->change.nr; i++) {
 620                struct wt_status_change_data *d;
 621                d = s->change.items[i].util;
 622                if (!d->worktree_status ||
 623                    d->worktree_status == DIFF_STATUS_UNMERGED)
 624                        continue;
 625                if (!changes)
 626                        changes = 1;
 627                if (d->dirty_submodule)
 628                        *dirty_submodules = 1;
 629                if (d->worktree_status == DIFF_STATUS_DELETED)
 630                        changes = -1;
 631        }
 632        return changes;
 633}
 634
 635static void wt_status_print_changed(struct wt_status *s)
 636{
 637        int i, dirty_submodules;
 638        int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
 639
 640        if (!worktree_changes)
 641                return;
 642
 643        wt_status_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
 644
 645        for (i = 0; i < s->change.nr; i++) {
 646                struct wt_status_change_data *d;
 647                struct string_list_item *it;
 648                it = &(s->change.items[i]);
 649                d = it->util;
 650                if (!d->worktree_status ||
 651                    d->worktree_status == DIFF_STATUS_UNMERGED)
 652                        continue;
 653                wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
 654        }
 655        wt_status_print_trailer(s);
 656}
 657
 658static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted)
 659{
 660        struct child_process sm_summary;
 661        char summary_limit[64];
 662        char index[PATH_MAX];
 663        const char *env[] = { NULL, NULL };
 664        const char *argv[8];
 665
 666        env[0] =        index;
 667        argv[0] =       "submodule";
 668        argv[1] =       "summary";
 669        argv[2] =       uncommitted ? "--files" : "--cached";
 670        argv[3] =       "--for-status";
 671        argv[4] =       "--summary-limit";
 672        argv[5] =       summary_limit;
 673        argv[6] =       uncommitted ? NULL : (s->amend ? "HEAD^" : "HEAD");
 674        argv[7] =       NULL;
 675
 676        sprintf(summary_limit, "%d", s->submodule_summary);
 677        snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
 678
 679        memset(&sm_summary, 0, sizeof(sm_summary));
 680        sm_summary.argv = argv;
 681        sm_summary.env = env;
 682        sm_summary.git_cmd = 1;
 683        sm_summary.no_stdin = 1;
 684        fflush(s->fp);
 685        sm_summary.out = dup(fileno(s->fp));    /* run_command closes it */
 686        run_command(&sm_summary);
 687}
 688
 689static void wt_status_print_other(struct wt_status *s,
 690                                  struct string_list *l,
 691                                  const char *what,
 692                                  const char *how)
 693{
 694        int i;
 695        struct strbuf buf = STRBUF_INIT;
 696        static struct string_list output = STRING_LIST_INIT_DUP;
 697        struct column_options copts;
 698
 699        if (!l->nr)
 700                return;
 701
 702        wt_status_print_other_header(s, what, how);
 703
 704        for (i = 0; i < l->nr; i++) {
 705                struct string_list_item *it;
 706                const char *path;
 707                it = &(l->items[i]);
 708                path = quote_path(it->string, strlen(it->string),
 709                                  &buf, s->prefix);
 710                if (column_active(s->colopts)) {
 711                        string_list_append(&output, path);
 712                        continue;
 713                }
 714                status_printf(s, color(WT_STATUS_HEADER, s), "\t");
 715                status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
 716                                   "%s\n", path);
 717        }
 718
 719        strbuf_release(&buf);
 720        if (!column_active(s->colopts))
 721                return;
 722
 723        strbuf_addf(&buf, "%s#\t%s",
 724                    color(WT_STATUS_HEADER, s),
 725                    color(WT_STATUS_UNTRACKED, s));
 726        memset(&copts, 0, sizeof(copts));
 727        copts.padding = 1;
 728        copts.indent = buf.buf;
 729        if (want_color(s->use_color))
 730                copts.nl = GIT_COLOR_RESET "\n";
 731        print_columns(&output, s->colopts, &copts);
 732        string_list_clear(&output, 0);
 733        strbuf_release(&buf);
 734}
 735
 736static void wt_status_print_verbose(struct wt_status *s)
 737{
 738        struct rev_info rev;
 739        struct setup_revision_opt opt;
 740
 741        init_revisions(&rev, NULL);
 742        DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
 743
 744        memset(&opt, 0, sizeof(opt));
 745        opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
 746        setup_revisions(0, NULL, &rev, &opt);
 747
 748        rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
 749        rev.diffopt.detect_rename = 1;
 750        rev.diffopt.file = s->fp;
 751        rev.diffopt.close_file = 0;
 752        /*
 753         * If we're not going to stdout, then we definitely don't
 754         * want color, since we are going to the commit message
 755         * file (and even the "auto" setting won't work, since it
 756         * will have checked isatty on stdout).
 757         */
 758        if (s->fp != stdout)
 759                rev.diffopt.use_color = 0;
 760        run_diff_index(&rev, 1);
 761}
 762
 763static void wt_status_print_tracking(struct wt_status *s)
 764{
 765        struct strbuf sb = STRBUF_INIT;
 766        const char *cp, *ep;
 767        struct branch *branch;
 768
 769        assert(s->branch && !s->is_initial);
 770        if (prefixcmp(s->branch, "refs/heads/"))
 771                return;
 772        branch = branch_get(s->branch + 11);
 773        if (!format_tracking_info(branch, &sb))
 774                return;
 775
 776        for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
 777                color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
 778                                 "%c %.*s", comment_line_char,
 779                                 (int)(ep - cp), cp);
 780        color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
 781                         comment_line_char);
 782}
 783
 784static int has_unmerged(struct wt_status *s)
 785{
 786        int i;
 787
 788        for (i = 0; i < s->change.nr; i++) {
 789                struct wt_status_change_data *d;
 790                d = s->change.items[i].util;
 791                if (d->stagemask)
 792                        return 1;
 793        }
 794        return 0;
 795}
 796
 797static void show_merge_in_progress(struct wt_status *s,
 798                                struct wt_status_state *state,
 799                                const char *color)
 800{
 801        if (has_unmerged(s)) {
 802                status_printf_ln(s, color, _("You have unmerged paths."));
 803                if (advice_status_hints)
 804                        status_printf_ln(s, color,
 805                                _("  (fix conflicts and run \"git commit\")"));
 806        } else {
 807                status_printf_ln(s, color,
 808                        _("All conflicts fixed but you are still merging."));
 809                if (advice_status_hints)
 810                        status_printf_ln(s, color,
 811                                _("  (use \"git commit\" to conclude merge)"));
 812        }
 813        wt_status_print_trailer(s);
 814}
 815
 816static void show_am_in_progress(struct wt_status *s,
 817                                struct wt_status_state *state,
 818                                const char *color)
 819{
 820        status_printf_ln(s, color,
 821                _("You are in the middle of an am session."));
 822        if (state->am_empty_patch)
 823                status_printf_ln(s, color,
 824                        _("The current patch is empty."));
 825        if (advice_status_hints) {
 826                if (!state->am_empty_patch)
 827                        status_printf_ln(s, color,
 828                                _("  (fix conflicts and then run \"git am --resolved\")"));
 829                status_printf_ln(s, color,
 830                        _("  (use \"git am --skip\" to skip this patch)"));
 831                status_printf_ln(s, color,
 832                        _("  (use \"git am --abort\" to restore the original branch)"));
 833        }
 834        wt_status_print_trailer(s);
 835}
 836
 837static char *read_line_from_git_path(const char *filename)
 838{
 839        struct strbuf buf = STRBUF_INIT;
 840        FILE *fp = fopen(git_path("%s", filename), "r");
 841        if (!fp) {
 842                strbuf_release(&buf);
 843                return NULL;
 844        }
 845        strbuf_getline(&buf, fp, '\n');
 846        if (!fclose(fp)) {
 847                return strbuf_detach(&buf, NULL);
 848        } else {
 849                strbuf_release(&buf);
 850                return NULL;
 851        }
 852}
 853
 854static int split_commit_in_progress(struct wt_status *s)
 855{
 856        int split_in_progress = 0;
 857        char *head = read_line_from_git_path("HEAD");
 858        char *orig_head = read_line_from_git_path("ORIG_HEAD");
 859        char *rebase_amend = read_line_from_git_path("rebase-merge/amend");
 860        char *rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
 861
 862        if (!head || !orig_head || !rebase_amend || !rebase_orig_head ||
 863            !s->branch || strcmp(s->branch, "HEAD"))
 864                return split_in_progress;
 865
 866        if (!strcmp(rebase_amend, rebase_orig_head)) {
 867                if (strcmp(head, rebase_amend))
 868                        split_in_progress = 1;
 869        } else if (strcmp(orig_head, rebase_orig_head)) {
 870                split_in_progress = 1;
 871        }
 872
 873        if (!s->amend && !s->nowarn && !s->workdir_dirty)
 874                split_in_progress = 0;
 875
 876        free(head);
 877        free(orig_head);
 878        free(rebase_amend);
 879        free(rebase_orig_head);
 880        return split_in_progress;
 881}
 882
 883static void show_rebase_in_progress(struct wt_status *s,
 884                                struct wt_status_state *state,
 885                                const char *color)
 886{
 887        struct stat st;
 888
 889        if (has_unmerged(s)) {
 890                if (state->branch)
 891                        status_printf_ln(s, color,
 892                                         _("You are currently rebasing branch '%s' on '%s'."),
 893                                         state->branch,
 894                                         state->onto);
 895                else
 896                        status_printf_ln(s, color,
 897                                         _("You are currently rebasing."));
 898                if (advice_status_hints) {
 899                        status_printf_ln(s, color,
 900                                _("  (fix conflicts and then run \"git rebase --continue\")"));
 901                        status_printf_ln(s, color,
 902                                _("  (use \"git rebase --skip\" to skip this patch)"));
 903                        status_printf_ln(s, color,
 904                                _("  (use \"git rebase --abort\" to check out the original branch)"));
 905                }
 906        } else if (state->rebase_in_progress || !stat(git_path("MERGE_MSG"), &st)) {
 907                if (state->branch)
 908                        status_printf_ln(s, color,
 909                                         _("You are currently rebasing branch '%s' on '%s'."),
 910                                         state->branch,
 911                                         state->onto);
 912                else
 913                        status_printf_ln(s, color,
 914                                         _("You are currently rebasing."));
 915                if (advice_status_hints)
 916                        status_printf_ln(s, color,
 917                                _("  (all conflicts fixed: run \"git rebase --continue\")"));
 918        } else if (split_commit_in_progress(s)) {
 919                if (state->branch)
 920                        status_printf_ln(s, color,
 921                                         _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
 922                                         state->branch,
 923                                         state->onto);
 924                else
 925                        status_printf_ln(s, color,
 926                                         _("You are currently splitting a commit during a rebase."));
 927                if (advice_status_hints)
 928                        status_printf_ln(s, color,
 929                                _("  (Once your working directory is clean, run \"git rebase --continue\")"));
 930        } else {
 931                if (state->branch)
 932                        status_printf_ln(s, color,
 933                                         _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
 934                                         state->branch,
 935                                         state->onto);
 936                else
 937                        status_printf_ln(s, color,
 938                                         _("You are currently editing a commit during a rebase."));
 939                if (advice_status_hints && !s->amend) {
 940                        status_printf_ln(s, color,
 941                                _("  (use \"git commit --amend\" to amend the current commit)"));
 942                        status_printf_ln(s, color,
 943                                _("  (use \"git rebase --continue\" once you are satisfied with your changes)"));
 944                }
 945        }
 946        wt_status_print_trailer(s);
 947}
 948
 949static void show_cherry_pick_in_progress(struct wt_status *s,
 950                                        struct wt_status_state *state,
 951                                        const char *color)
 952{
 953        status_printf_ln(s, color, _("You are currently cherry-picking."));
 954        if (advice_status_hints) {
 955                if (has_unmerged(s))
 956                        status_printf_ln(s, color,
 957                                _("  (fix conflicts and run \"git commit\")"));
 958                else
 959                        status_printf_ln(s, color,
 960                                _("  (all conflicts fixed: run \"git commit\")"));
 961        }
 962        wt_status_print_trailer(s);
 963}
 964
 965static void show_bisect_in_progress(struct wt_status *s,
 966                                struct wt_status_state *state,
 967                                const char *color)
 968{
 969        if (state->branch)
 970                status_printf_ln(s, color,
 971                                 _("You are currently bisecting branch '%s'."),
 972                                 state->branch);
 973        else
 974                status_printf_ln(s, color,
 975                                 _("You are currently bisecting."));
 976        if (advice_status_hints)
 977                status_printf_ln(s, color,
 978                        _("  (use \"git bisect reset\" to get back to the original branch)"));
 979        wt_status_print_trailer(s);
 980}
 981
 982/*
 983 * Extract branch information from rebase/bisect
 984 */
 985static void read_and_strip_branch(struct strbuf *sb,
 986                                  const char **branch,
 987                                  const char *path)
 988{
 989        unsigned char sha1[20];
 990
 991        strbuf_reset(sb);
 992        if (strbuf_read_file(sb, git_path("%s", path), 0) <= 0)
 993                return;
 994
 995        while (sb->len && sb->buf[sb->len - 1] == '\n')
 996                strbuf_setlen(sb, sb->len - 1);
 997        if (!sb->len)
 998                return;
 999        if (!prefixcmp(sb->buf, "refs/heads/"))
1000                *branch = sb->buf + strlen("refs/heads/");
1001        else if (!prefixcmp(sb->buf, "refs/"))
1002                *branch = sb->buf;
1003        else if (!get_sha1_hex(sb->buf, sha1)) {
1004                const char *abbrev;
1005                abbrev = find_unique_abbrev(sha1, DEFAULT_ABBREV);
1006                strbuf_reset(sb);
1007                strbuf_addstr(sb, abbrev);
1008                *branch = sb->buf;
1009        } else if (!strcmp(sb->buf, "detached HEAD")) /* rebase */
1010                ;
1011        else                    /* bisect */
1012                *branch = sb->buf;
1013}
1014
1015static void wt_status_print_state(struct wt_status *s)
1016{
1017        const char *state_color = color(WT_STATUS_HEADER, s);
1018        struct strbuf branch = STRBUF_INIT;
1019        struct strbuf onto = STRBUF_INIT;
1020        struct wt_status_state state;
1021        struct stat st;
1022
1023        memset(&state, 0, sizeof(state));
1024
1025        if (!stat(git_path("MERGE_HEAD"), &st)) {
1026                state.merge_in_progress = 1;
1027        } else if (!stat(git_path("rebase-apply"), &st)) {
1028                if (!stat(git_path("rebase-apply/applying"), &st)) {
1029                        state.am_in_progress = 1;
1030                        if (!stat(git_path("rebase-apply/patch"), &st) && !st.st_size)
1031                                state.am_empty_patch = 1;
1032                } else {
1033                        state.rebase_in_progress = 1;
1034                        read_and_strip_branch(&branch, &state.branch,
1035                                              "rebase-apply/head-name");
1036                        read_and_strip_branch(&onto, &state.onto,
1037                                              "rebase-apply/onto");
1038                }
1039        } else if (!stat(git_path("rebase-merge"), &st)) {
1040                if (!stat(git_path("rebase-merge/interactive"), &st))
1041                        state.rebase_interactive_in_progress = 1;
1042                else
1043                        state.rebase_in_progress = 1;
1044                read_and_strip_branch(&branch, &state.branch,
1045                                      "rebase-merge/head-name");
1046                read_and_strip_branch(&onto, &state.onto,
1047                                      "rebase-merge/onto");
1048        } else if (!stat(git_path("CHERRY_PICK_HEAD"), &st)) {
1049                state.cherry_pick_in_progress = 1;
1050        }
1051        if (!stat(git_path("BISECT_LOG"), &st)) {
1052                state.bisect_in_progress = 1;
1053                read_and_strip_branch(&branch, &state.branch,
1054                                      "BISECT_START");
1055        }
1056
1057        if (state.merge_in_progress)
1058                show_merge_in_progress(s, &state, state_color);
1059        else if (state.am_in_progress)
1060                show_am_in_progress(s, &state, state_color);
1061        else if (state.rebase_in_progress || state.rebase_interactive_in_progress)
1062                show_rebase_in_progress(s, &state, state_color);
1063        else if (state.cherry_pick_in_progress)
1064                show_cherry_pick_in_progress(s, &state, state_color);
1065        if (state.bisect_in_progress)
1066                show_bisect_in_progress(s, &state, state_color);
1067        strbuf_release(&branch);
1068        strbuf_release(&onto);
1069}
1070
1071void wt_status_print(struct wt_status *s)
1072{
1073        const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1074        const char *branch_status_color = color(WT_STATUS_HEADER, s);
1075
1076        if (s->branch) {
1077                const char *on_what = _("On branch ");
1078                const char *branch_name = s->branch;
1079                if (!prefixcmp(branch_name, "refs/heads/"))
1080                        branch_name += 11;
1081                else if (!strcmp(branch_name, "HEAD")) {
1082                        branch_name = "";
1083                        branch_status_color = color(WT_STATUS_NOBRANCH, s);
1084                        on_what = _("Not currently on any branch.");
1085                }
1086                status_printf(s, color(WT_STATUS_HEADER, s), "");
1087                status_printf_more(s, branch_status_color, "%s", on_what);
1088                status_printf_more(s, branch_color, "%s\n", branch_name);
1089                if (!s->is_initial)
1090                        wt_status_print_tracking(s);
1091        }
1092
1093        wt_status_print_state(s);
1094        if (s->is_initial) {
1095                status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
1096                status_printf_ln(s, color(WT_STATUS_HEADER, s), _("Initial commit"));
1097                status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
1098        }
1099
1100        wt_status_print_updated(s);
1101        wt_status_print_unmerged(s);
1102        wt_status_print_changed(s);
1103        if (s->submodule_summary &&
1104            (!s->ignore_submodule_arg ||
1105             strcmp(s->ignore_submodule_arg, "all"))) {
1106                wt_status_print_submodule_summary(s, 0);  /* staged */
1107                wt_status_print_submodule_summary(s, 1);  /* unstaged */
1108        }
1109        if (s->show_untracked_files) {
1110                wt_status_print_other(s, &s->untracked, _("Untracked files"), "add");
1111                if (s->show_ignored_files)
1112                        wt_status_print_other(s, &s->ignored, _("Ignored files"), "add -f");
1113                if (advice_status_u_option && 2000 < s->untracked_in_ms) {
1114                        status_printf_ln(s, GIT_COLOR_NORMAL, "");
1115                        status_printf_ln(s, GIT_COLOR_NORMAL,
1116                                 _("It took %.2f seconds to enumerate untracked files."
1117                                   "  'status -uno'"),
1118                                 s->untracked_in_ms / 1000.0);
1119                        status_printf_ln(s, GIT_COLOR_NORMAL,
1120                                 _("may speed it up, but you have to be careful not"
1121                                   " to forget to add"));
1122                        status_printf_ln(s, GIT_COLOR_NORMAL,
1123                                 _("new files yourself (see 'git help status')."));
1124                }
1125        } else if (s->commitable)
1126                status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
1127                        advice_status_hints
1128                        ? _(" (use -u option to show untracked files)") : "");
1129
1130        if (s->verbose)
1131                wt_status_print_verbose(s);
1132        if (!s->commitable) {
1133                if (s->amend)
1134                        status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
1135                else if (s->nowarn)
1136                        ; /* nothing */
1137                else if (s->workdir_dirty) {
1138                        if (advice_status_hints)
1139                                printf(_("no changes added to commit "
1140                                         "(use \"git add\" and/or \"git commit -a\")\n"));
1141                        else
1142                                printf(_("no changes added to commit\n"));
1143                } else if (s->untracked.nr) {
1144                        if (advice_status_hints)
1145                                printf(_("nothing added to commit but untracked files "
1146                                         "present (use \"git add\" to track)\n"));
1147                        else
1148                                printf(_("nothing added to commit but untracked files present\n"));
1149                } else if (s->is_initial) {
1150                        if (advice_status_hints)
1151                                printf(_("nothing to commit (create/copy files "
1152                                         "and use \"git add\" to track)\n"));
1153                        else
1154                                printf(_("nothing to commit\n"));
1155                } else if (!s->show_untracked_files) {
1156                        if (advice_status_hints)
1157                                printf(_("nothing to commit (use -u to show untracked files)\n"));
1158                        else
1159                                printf(_("nothing to commit\n"));
1160                } else
1161                        printf(_("nothing to commit, working directory clean\n"));
1162        }
1163}
1164
1165static void wt_shortstatus_unmerged(struct string_list_item *it,
1166                           struct wt_status *s)
1167{
1168        struct wt_status_change_data *d = it->util;
1169        const char *how = "??";
1170
1171        switch (d->stagemask) {
1172        case 1: how = "DD"; break; /* both deleted */
1173        case 2: how = "AU"; break; /* added by us */
1174        case 3: how = "UD"; break; /* deleted by them */
1175        case 4: how = "UA"; break; /* added by them */
1176        case 5: how = "DU"; break; /* deleted by us */
1177        case 6: how = "AA"; break; /* both added */
1178        case 7: how = "UU"; break; /* both modified */
1179        }
1180        color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
1181        if (s->null_termination) {
1182                fprintf(stdout, " %s%c", it->string, 0);
1183        } else {
1184                struct strbuf onebuf = STRBUF_INIT;
1185                const char *one;
1186                one = quote_path(it->string, -1, &onebuf, s->prefix);
1187                printf(" %s\n", one);
1188                strbuf_release(&onebuf);
1189        }
1190}
1191
1192static void wt_shortstatus_status(struct string_list_item *it,
1193                         struct wt_status *s)
1194{
1195        struct wt_status_change_data *d = it->util;
1196
1197        if (d->index_status)
1198                color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
1199        else
1200                putchar(' ');
1201        if (d->worktree_status)
1202                color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
1203        else
1204                putchar(' ');
1205        putchar(' ');
1206        if (s->null_termination) {
1207                fprintf(stdout, "%s%c", it->string, 0);
1208                if (d->head_path)
1209                        fprintf(stdout, "%s%c", d->head_path, 0);
1210        } else {
1211                struct strbuf onebuf = STRBUF_INIT;
1212                const char *one;
1213                if (d->head_path) {
1214                        one = quote_path(d->head_path, -1, &onebuf, s->prefix);
1215                        if (*one != '"' && strchr(one, ' ') != NULL) {
1216                                putchar('"');
1217                                strbuf_addch(&onebuf, '"');
1218                                one = onebuf.buf;
1219                        }
1220                        printf("%s -> ", one);
1221                        strbuf_release(&onebuf);
1222                }
1223                one = quote_path(it->string, -1, &onebuf, s->prefix);
1224                if (*one != '"' && strchr(one, ' ') != NULL) {
1225                        putchar('"');
1226                        strbuf_addch(&onebuf, '"');
1227                        one = onebuf.buf;
1228                }
1229                printf("%s\n", one);
1230                strbuf_release(&onebuf);
1231        }
1232}
1233
1234static void wt_shortstatus_other(struct string_list_item *it,
1235                                 struct wt_status *s, const char *sign)
1236{
1237        if (s->null_termination) {
1238                fprintf(stdout, "%s %s%c", sign, it->string, 0);
1239        } else {
1240                struct strbuf onebuf = STRBUF_INIT;
1241                const char *one;
1242                one = quote_path(it->string, -1, &onebuf, s->prefix);
1243                color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
1244                printf(" %s\n", one);
1245                strbuf_release(&onebuf);
1246        }
1247}
1248
1249static void wt_shortstatus_print_tracking(struct wt_status *s)
1250{
1251        struct branch *branch;
1252        const char *header_color = color(WT_STATUS_HEADER, s);
1253        const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
1254        const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
1255
1256        const char *base;
1257        const char *branch_name;
1258        int num_ours, num_theirs;
1259
1260        color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
1261
1262        if (!s->branch)
1263                return;
1264        branch_name = s->branch;
1265
1266        if (!prefixcmp(branch_name, "refs/heads/"))
1267                branch_name += 11;
1268        else if (!strcmp(branch_name, "HEAD")) {
1269                branch_name = _("HEAD (no branch)");
1270                branch_color_local = color(WT_STATUS_NOBRANCH, s);
1271        }
1272
1273        branch = branch_get(s->branch + 11);
1274        if (s->is_initial)
1275                color_fprintf(s->fp, header_color, _("Initial commit on "));
1276        if (!stat_tracking_info(branch, &num_ours, &num_theirs)) {
1277                color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1278                fputc(s->null_termination ? '\0' : '\n', s->fp);
1279                return;
1280        }
1281
1282        base = branch->merge[0]->dst;
1283        base = shorten_unambiguous_ref(base, 0);
1284        color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1285        color_fprintf(s->fp, header_color, "...");
1286        color_fprintf(s->fp, branch_color_remote, "%s", base);
1287
1288        color_fprintf(s->fp, header_color, " [");
1289        if (!num_ours) {
1290                color_fprintf(s->fp, header_color, _("behind "));
1291                color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1292        } else if (!num_theirs) {
1293                color_fprintf(s->fp, header_color, _("ahead "));
1294                color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1295        } else {
1296                color_fprintf(s->fp, header_color, _("ahead "));
1297                color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1298                color_fprintf(s->fp, header_color, _(", behind "));
1299                color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1300        }
1301
1302        color_fprintf(s->fp, header_color, "]");
1303        fputc(s->null_termination ? '\0' : '\n', s->fp);
1304}
1305
1306void wt_shortstatus_print(struct wt_status *s)
1307{
1308        int i;
1309
1310        if (s->show_branch)
1311                wt_shortstatus_print_tracking(s);
1312
1313        for (i = 0; i < s->change.nr; i++) {
1314                struct wt_status_change_data *d;
1315                struct string_list_item *it;
1316
1317                it = &(s->change.items[i]);
1318                d = it->util;
1319                if (d->stagemask)
1320                        wt_shortstatus_unmerged(it, s);
1321                else
1322                        wt_shortstatus_status(it, s);
1323        }
1324        for (i = 0; i < s->untracked.nr; i++) {
1325                struct string_list_item *it;
1326
1327                it = &(s->untracked.items[i]);
1328                wt_shortstatus_other(it, s, "??");
1329        }
1330        for (i = 0; i < s->ignored.nr; i++) {
1331                struct string_list_item *it;
1332
1333                it = &(s->ignored.items[i]);
1334                wt_shortstatus_other(it, s, "!!");
1335        }
1336}
1337
1338void wt_porcelain_print(struct wt_status *s)
1339{
1340        s->use_color = 0;
1341        s->relative_paths = 0;
1342        s->prefix = NULL;
1343        wt_shortstatus_print(s);
1344}