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