wt-status.con commit blame: show "previous" information in --porcelain/--incremental format (96e1170)
   1#include "cache.h"
   2#include "wt-status.h"
   3#include "color.h"
   4#include "object.h"
   5#include "dir.h"
   6#include "commit.h"
   7#include "diff.h"
   8#include "revision.h"
   9#include "diffcore.h"
  10#include "quote.h"
  11#include "run-command.h"
  12#include "remote.h"
  13
  14int wt_status_relative_paths = 1;
  15int wt_status_use_color = -1;
  16int wt_status_submodule_summary;
  17static char wt_status_colors[][COLOR_MAXLEN] = {
  18        "",         /* WT_STATUS_HEADER: normal */
  19        "\033[32m", /* WT_STATUS_UPDATED: green */
  20        "\033[31m", /* WT_STATUS_CHANGED: red */
  21        "\033[31m", /* WT_STATUS_UNTRACKED: red */
  22        "\033[31m", /* WT_STATUS_NOBRANCH: red */
  23};
  24
  25enum untracked_status_type show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
  26
  27static int parse_status_slot(const char *var, int offset)
  28{
  29        if (!strcasecmp(var+offset, "header"))
  30                return WT_STATUS_HEADER;
  31        if (!strcasecmp(var+offset, "updated")
  32                || !strcasecmp(var+offset, "added"))
  33                return WT_STATUS_UPDATED;
  34        if (!strcasecmp(var+offset, "changed"))
  35                return WT_STATUS_CHANGED;
  36        if (!strcasecmp(var+offset, "untracked"))
  37                return WT_STATUS_UNTRACKED;
  38        if (!strcasecmp(var+offset, "nobranch"))
  39                return WT_STATUS_NOBRANCH;
  40        die("bad config variable '%s'", var);
  41}
  42
  43static const char* color(int slot)
  44{
  45        return wt_status_use_color > 0 ? wt_status_colors[slot] : "";
  46}
  47
  48void wt_status_prepare(struct wt_status *s)
  49{
  50        unsigned char sha1[20];
  51        const char *head;
  52
  53        memset(s, 0, sizeof(*s));
  54        head = resolve_ref("HEAD", sha1, 0, NULL);
  55        s->branch = head ? xstrdup(head) : NULL;
  56        s->reference = "HEAD";
  57        s->fp = stdout;
  58        s->index_file = get_index_file();
  59}
  60
  61static void wt_status_print_cached_header(struct wt_status *s)
  62{
  63        const char *c = color(WT_STATUS_HEADER);
  64        color_fprintf_ln(s->fp, c, "# Changes to be committed:");
  65        if (!s->is_initial) {
  66                color_fprintf_ln(s->fp, c, "#   (use \"git reset %s <file>...\" to unstage)", s->reference);
  67        } else {
  68                color_fprintf_ln(s->fp, c, "#   (use \"git rm --cached <file>...\" to unstage)");
  69        }
  70        color_fprintf_ln(s->fp, c, "#");
  71}
  72
  73static void wt_status_print_dirty_header(struct wt_status *s,
  74                                         int has_deleted)
  75{
  76        const char *c = color(WT_STATUS_HEADER);
  77        color_fprintf_ln(s->fp, c, "# Changed but not updated:");
  78        if (!has_deleted)
  79                color_fprintf_ln(s->fp, c, "#   (use \"git add <file>...\" to update what will be committed)");
  80        else
  81                color_fprintf_ln(s->fp, c, "#   (use \"git add/rm <file>...\" to update what will be committed)");
  82        color_fprintf_ln(s->fp, c, "#   (use \"git checkout -- <file>...\" to discard changes in working directory)");
  83        color_fprintf_ln(s->fp, c, "#");
  84}
  85
  86static void wt_status_print_untracked_header(struct wt_status *s)
  87{
  88        const char *c = color(WT_STATUS_HEADER);
  89        color_fprintf_ln(s->fp, c, "# Untracked files:");
  90        color_fprintf_ln(s->fp, c, "#   (use \"git add <file>...\" to include in what will be committed)");
  91        color_fprintf_ln(s->fp, c, "#");
  92}
  93
  94static void wt_status_print_trailer(struct wt_status *s)
  95{
  96        color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
  97}
  98
  99#define quote_path quote_path_relative
 100
 101static void wt_status_print_filepair(struct wt_status *s,
 102                                     int t, struct diff_filepair *p)
 103{
 104        const char *c = color(t);
 105        const char *one, *two;
 106        struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
 107
 108        one = quote_path(p->one->path, -1, &onebuf, s->prefix);
 109        two = quote_path(p->two->path, -1, &twobuf, s->prefix);
 110
 111        color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
 112        switch (p->status) {
 113        case DIFF_STATUS_ADDED:
 114                color_fprintf(s->fp, c, "new file:   %s", one);
 115                break;
 116        case DIFF_STATUS_COPIED:
 117                color_fprintf(s->fp, c, "copied:     %s -> %s", one, two);
 118                break;
 119        case DIFF_STATUS_DELETED:
 120                color_fprintf(s->fp, c, "deleted:    %s", one);
 121                break;
 122        case DIFF_STATUS_MODIFIED:
 123                color_fprintf(s->fp, c, "modified:   %s", one);
 124                break;
 125        case DIFF_STATUS_RENAMED:
 126                color_fprintf(s->fp, c, "renamed:    %s -> %s", one, two);
 127                break;
 128        case DIFF_STATUS_TYPE_CHANGED:
 129                color_fprintf(s->fp, c, "typechange: %s", one);
 130                break;
 131        case DIFF_STATUS_UNKNOWN:
 132                color_fprintf(s->fp, c, "unknown:    %s", one);
 133                break;
 134        case DIFF_STATUS_UNMERGED:
 135                color_fprintf(s->fp, c, "unmerged:   %s", one);
 136                break;
 137        default:
 138                die("bug: unhandled diff status %c", p->status);
 139        }
 140        fprintf(s->fp, "\n");
 141        strbuf_release(&onebuf);
 142        strbuf_release(&twobuf);
 143}
 144
 145static void wt_status_print_updated_cb(struct diff_queue_struct *q,
 146                struct diff_options *options,
 147                void *data)
 148{
 149        struct wt_status *s = data;
 150        int shown_header = 0;
 151        int i;
 152        for (i = 0; i < q->nr; i++) {
 153                if (q->queue[i]->status == 'U')
 154                        continue;
 155                if (!shown_header) {
 156                        wt_status_print_cached_header(s);
 157                        s->commitable = 1;
 158                        shown_header = 1;
 159                }
 160                wt_status_print_filepair(s, WT_STATUS_UPDATED, q->queue[i]);
 161        }
 162        if (shown_header)
 163                wt_status_print_trailer(s);
 164}
 165
 166static void wt_status_print_changed_cb(struct diff_queue_struct *q,
 167                        struct diff_options *options,
 168                        void *data)
 169{
 170        struct wt_status *s = data;
 171        int i;
 172        if (q->nr) {
 173                int has_deleted = 0;
 174                s->workdir_dirty = 1;
 175                for (i = 0; i < q->nr; i++)
 176                        if (q->queue[i]->status == DIFF_STATUS_DELETED) {
 177                                has_deleted = 1;
 178                                break;
 179                        }
 180                wt_status_print_dirty_header(s, has_deleted);
 181        }
 182        for (i = 0; i < q->nr; i++)
 183                wt_status_print_filepair(s, WT_STATUS_CHANGED, q->queue[i]);
 184        if (q->nr)
 185                wt_status_print_trailer(s);
 186}
 187
 188static void wt_status_print_initial(struct wt_status *s)
 189{
 190        int i;
 191        struct strbuf buf = STRBUF_INIT;
 192
 193        if (active_nr) {
 194                s->commitable = 1;
 195                wt_status_print_cached_header(s);
 196        }
 197        for (i = 0; i < active_nr; i++) {
 198                color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
 199                color_fprintf_ln(s->fp, color(WT_STATUS_UPDATED), "new file: %s",
 200                                quote_path(active_cache[i]->name, -1,
 201                                           &buf, s->prefix));
 202        }
 203        if (active_nr)
 204                wt_status_print_trailer(s);
 205        strbuf_release(&buf);
 206}
 207
 208static void wt_status_print_updated(struct wt_status *s)
 209{
 210        struct rev_info rev;
 211        init_revisions(&rev, NULL);
 212        setup_revisions(0, NULL, &rev, s->reference);
 213        rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
 214        rev.diffopt.format_callback = wt_status_print_updated_cb;
 215        rev.diffopt.format_callback_data = s;
 216        rev.diffopt.detect_rename = 1;
 217        rev.diffopt.rename_limit = 200;
 218        rev.diffopt.break_opt = 0;
 219        run_diff_index(&rev, 1);
 220}
 221
 222static void wt_status_print_changed(struct wt_status *s)
 223{
 224        struct rev_info rev;
 225        init_revisions(&rev, "");
 226        setup_revisions(0, NULL, &rev, NULL);
 227        rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
 228        rev.diffopt.format_callback = wt_status_print_changed_cb;
 229        rev.diffopt.format_callback_data = s;
 230        run_diff_files(&rev, 0);
 231}
 232
 233static void wt_status_print_submodule_summary(struct wt_status *s)
 234{
 235        struct child_process sm_summary;
 236        char summary_limit[64];
 237        char index[PATH_MAX];
 238        const char *env[] = { index, NULL };
 239        const char *argv[] = {
 240                "submodule",
 241                "summary",
 242                "--cached",
 243                "--for-status",
 244                "--summary-limit",
 245                summary_limit,
 246                s->amend ? "HEAD^" : "HEAD",
 247                NULL
 248        };
 249
 250        sprintf(summary_limit, "%d", wt_status_submodule_summary);
 251        snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
 252
 253        memset(&sm_summary, 0, sizeof(sm_summary));
 254        sm_summary.argv = argv;
 255        sm_summary.env = env;
 256        sm_summary.git_cmd = 1;
 257        sm_summary.no_stdin = 1;
 258        fflush(s->fp);
 259        sm_summary.out = dup(fileno(s->fp));    /* run_command closes it */
 260        run_command(&sm_summary);
 261}
 262
 263static void wt_status_print_untracked(struct wt_status *s)
 264{
 265        struct dir_struct dir;
 266        int i;
 267        int shown_header = 0;
 268        struct strbuf buf = STRBUF_INIT;
 269
 270        memset(&dir, 0, sizeof(dir));
 271
 272        if (!s->untracked) {
 273                dir.show_other_directories = 1;
 274                dir.hide_empty_directories = 1;
 275        }
 276        setup_standard_excludes(&dir);
 277
 278        read_directory(&dir, ".", "", 0, NULL);
 279        for(i = 0; i < dir.nr; i++) {
 280                /* check for matching entry, which is unmerged; lifted from
 281                 * builtin-ls-files:show_other_files */
 282                struct dir_entry *ent = dir.entries[i];
 283                int pos = cache_name_pos(ent->name, ent->len);
 284                struct cache_entry *ce;
 285                if (0 <= pos)
 286                        die("bug in wt_status_print_untracked");
 287                pos = -pos - 1;
 288                if (pos < active_nr) {
 289                        ce = active_cache[pos];
 290                        if (ce_namelen(ce) == ent->len &&
 291                            !memcmp(ce->name, ent->name, ent->len))
 292                                continue;
 293                }
 294                if (!shown_header) {
 295                        s->workdir_untracked = 1;
 296                        wt_status_print_untracked_header(s);
 297                        shown_header = 1;
 298                }
 299                color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
 300                color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%s",
 301                                quote_path(ent->name, ent->len,
 302                                        &buf, s->prefix));
 303        }
 304        strbuf_release(&buf);
 305}
 306
 307static void wt_status_print_verbose(struct wt_status *s)
 308{
 309        struct rev_info rev;
 310
 311        init_revisions(&rev, NULL);
 312        setup_revisions(0, NULL, &rev, s->reference);
 313        rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
 314        rev.diffopt.detect_rename = 1;
 315        rev.diffopt.file = s->fp;
 316        rev.diffopt.close_file = 0;
 317        run_diff_index(&rev, 1);
 318}
 319
 320static void wt_status_print_tracking(struct wt_status *s)
 321{
 322        struct strbuf sb = STRBUF_INIT;
 323        const char *cp, *ep;
 324        struct branch *branch;
 325
 326        assert(s->branch && !s->is_initial);
 327        if (prefixcmp(s->branch, "refs/heads/"))
 328                return;
 329        branch = branch_get(s->branch + 11);
 330        if (!format_tracking_info(branch, &sb))
 331                return;
 332
 333        for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
 334                color_fprintf_ln(s->fp, color(WT_STATUS_HEADER),
 335                                 "# %.*s", (int)(ep - cp), cp);
 336        color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
 337}
 338
 339void wt_status_print(struct wt_status *s)
 340{
 341        unsigned char sha1[20];
 342        const char *branch_color = color(WT_STATUS_HEADER);
 343
 344        s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
 345        if (s->branch) {
 346                const char *on_what = "On branch ";
 347                const char *branch_name = s->branch;
 348                if (!prefixcmp(branch_name, "refs/heads/"))
 349                        branch_name += 11;
 350                else if (!strcmp(branch_name, "HEAD")) {
 351                        branch_name = "";
 352                        branch_color = color(WT_STATUS_NOBRANCH);
 353                        on_what = "Not currently on any branch.";
 354                }
 355                color_fprintf(s->fp, color(WT_STATUS_HEADER), "# ");
 356                color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
 357                if (!s->is_initial)
 358                        wt_status_print_tracking(s);
 359        }
 360
 361        if (s->is_initial) {
 362                color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
 363                color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "# Initial commit");
 364                color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
 365                wt_status_print_initial(s);
 366        }
 367        else {
 368                wt_status_print_updated(s);
 369        }
 370
 371        wt_status_print_changed(s);
 372        if (wt_status_submodule_summary)
 373                wt_status_print_submodule_summary(s);
 374        if (show_untracked_files)
 375                wt_status_print_untracked(s);
 376        else if (s->commitable)
 377                 fprintf(s->fp, "# Untracked files not listed (use -u option to show untracked files)\n");
 378
 379        if (s->verbose && !s->is_initial)
 380                wt_status_print_verbose(s);
 381        if (!s->commitable) {
 382                if (s->amend)
 383                        fprintf(s->fp, "# No changes\n");
 384                else if (s->nowarn)
 385                        ; /* nothing */
 386                else if (s->workdir_dirty)
 387                        printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
 388                else if (s->workdir_untracked)
 389                        printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
 390                else if (s->is_initial)
 391                        printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
 392                else if (!show_untracked_files)
 393                        printf("nothing to commit (use -u to show untracked files)\n");
 394                else
 395                        printf("nothing to commit (working directory clean)\n");
 396        }
 397}
 398
 399int git_status_config(const char *k, const char *v, void *cb)
 400{
 401        if (!strcmp(k, "status.submodulesummary")) {
 402                int is_bool;
 403                wt_status_submodule_summary = git_config_bool_or_int(k, v, &is_bool);
 404                if (is_bool && wt_status_submodule_summary)
 405                        wt_status_submodule_summary = -1;
 406                return 0;
 407        }
 408        if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
 409                wt_status_use_color = git_config_colorbool(k, v, -1);
 410                return 0;
 411        }
 412        if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
 413                int slot = parse_status_slot(k, 13);
 414                if (!v)
 415                        return config_error_nonbool(k);
 416                color_parse(v, k, wt_status_colors[slot]);
 417                return 0;
 418        }
 419        if (!strcmp(k, "status.relativepaths")) {
 420                wt_status_relative_paths = git_config_bool(k, v);
 421                return 0;
 422        }
 423        if (!strcmp(k, "status.showuntrackedfiles")) {
 424                if (!v)
 425                        return config_error_nonbool(k);
 426                else if (!strcmp(v, "no"))
 427                        show_untracked_files = SHOW_NO_UNTRACKED_FILES;
 428                else if (!strcmp(v, "normal"))
 429                        show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
 430                else if (!strcmp(v, "all"))
 431                        show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
 432                else
 433                        return error("Invalid untracked files mode '%s'", v);
 434                return 0;
 435        }
 436        return git_color_default_config(k, v, cb);
 437}