wt-status.con commit Remove old generated files from .gitignore. (feb7f38)
   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
  11int wt_status_relative_paths = 1;
  12int wt_status_use_color = 0;
  13static char wt_status_colors[][COLOR_MAXLEN] = {
  14        "",         /* WT_STATUS_HEADER: normal */
  15        "\033[32m", /* WT_STATUS_UPDATED: green */
  16        "\033[31m", /* WT_STATUS_CHANGED: red */
  17        "\033[31m", /* WT_STATUS_UNTRACKED: red */
  18};
  19
  20static const char use_add_msg[] =
  21"use \"git add <file>...\" to update what will be committed";
  22static const char use_add_rm_msg[] =
  23"use \"git add/rm <file>...\" to update what will be committed";
  24static const char use_add_to_include_msg[] =
  25"use \"git add <file>...\" to include in what will be committed";
  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        die("bad config variable '%s'", var);
  39}
  40
  41static const char* color(int slot)
  42{
  43        return wt_status_use_color ? wt_status_colors[slot] : "";
  44}
  45
  46void wt_status_prepare(struct wt_status *s)
  47{
  48        unsigned char sha1[20];
  49        const char *head;
  50
  51        memset(s, 0, sizeof(*s));
  52        head = resolve_ref("HEAD", sha1, 0, NULL);
  53        s->branch = head ? xstrdup(head) : NULL;
  54        s->reference = "HEAD";
  55        s->fp = stdout;
  56        s->index_file = get_index_file();
  57}
  58
  59static void wt_status_print_cached_header(struct wt_status *s)
  60{
  61        const char *c = color(WT_STATUS_HEADER);
  62        color_fprintf_ln(s->fp, c, "# Changes to be committed:");
  63        if (s->reference) {
  64                color_fprintf_ln(s->fp, c, "#   (use \"git reset %s <file>...\" to unstage)", s->reference);
  65        } else {
  66                color_fprintf_ln(s->fp, c, "#   (use \"git rm --cached <file>...\" to unstage)");
  67        }
  68        color_fprintf_ln(s->fp, c, "#");
  69}
  70
  71static void wt_status_print_header(struct wt_status *s,
  72                                   const char *main, const char *sub)
  73{
  74        const char *c = color(WT_STATUS_HEADER);
  75        color_fprintf_ln(s->fp, c, "# %s:", main);
  76        color_fprintf_ln(s->fp, c, "#   (%s)", sub);
  77        color_fprintf_ln(s->fp, c, "#");
  78}
  79
  80static void wt_status_print_trailer(struct wt_status *s)
  81{
  82        color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
  83}
  84
  85static char *quote_path(const char *in, int len,
  86                        struct strbuf *out, const char *prefix)
  87{
  88        if (len < 0)
  89                len = strlen(in);
  90
  91        strbuf_grow(out, len);
  92        strbuf_setlen(out, 0);
  93        if (prefix) {
  94                int off = 0;
  95                while (prefix[off] && off < len && prefix[off] == in[off])
  96                        if (prefix[off] == '/') {
  97                                prefix += off + 1;
  98                                in += off + 1;
  99                                len -= off + 1;
 100                                off = 0;
 101                        } else
 102                                off++;
 103
 104                for (; *prefix; prefix++)
 105                        if (*prefix == '/')
 106                                strbuf_addstr(out, "../");
 107        }
 108
 109        for ( ; len > 0; in++, len--) {
 110                int ch = *in;
 111
 112                switch (ch) {
 113                case '\n':
 114                        strbuf_addstr(out, "\\n");
 115                        break;
 116                case '\r':
 117                        strbuf_addstr(out, "\\r");
 118                        break;
 119                default:
 120                        strbuf_addch(out, ch);
 121                        continue;
 122                }
 123        }
 124
 125        if (!out->len)
 126                strbuf_addstr(out, "./");
 127
 128        return out->buf;
 129}
 130
 131static void wt_status_print_filepair(struct wt_status *s,
 132                                     int t, struct diff_filepair *p)
 133{
 134        const char *c = color(t);
 135        const char *one, *two;
 136        struct strbuf onebuf, twobuf;
 137
 138        strbuf_init(&onebuf, 0);
 139        strbuf_init(&twobuf, 0);
 140        one = quote_path(p->one->path, -1, &onebuf, s->prefix);
 141        two = quote_path(p->two->path, -1, &twobuf, s->prefix);
 142
 143        color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
 144        switch (p->status) {
 145        case DIFF_STATUS_ADDED:
 146                color_fprintf(s->fp, c, "new file:   %s", one);
 147                break;
 148        case DIFF_STATUS_COPIED:
 149                color_fprintf(s->fp, c, "copied:     %s -> %s", one, two);
 150                break;
 151        case DIFF_STATUS_DELETED:
 152                color_fprintf(s->fp, c, "deleted:    %s", one);
 153                break;
 154        case DIFF_STATUS_MODIFIED:
 155                color_fprintf(s->fp, c, "modified:   %s", one);
 156                break;
 157        case DIFF_STATUS_RENAMED:
 158                color_fprintf(s->fp, c, "renamed:    %s -> %s", one, two);
 159                break;
 160        case DIFF_STATUS_TYPE_CHANGED:
 161                color_fprintf(s->fp, c, "typechange: %s", one);
 162                break;
 163        case DIFF_STATUS_UNKNOWN:
 164                color_fprintf(s->fp, c, "unknown:    %s", one);
 165                break;
 166        case DIFF_STATUS_UNMERGED:
 167                color_fprintf(s->fp, c, "unmerged:   %s", one);
 168                break;
 169        default:
 170                die("bug: unhandled diff status %c", p->status);
 171        }
 172        fprintf(s->fp, "\n");
 173        strbuf_release(&onebuf);
 174        strbuf_release(&twobuf);
 175}
 176
 177static void wt_status_print_updated_cb(struct diff_queue_struct *q,
 178                struct diff_options *options,
 179                void *data)
 180{
 181        struct wt_status *s = data;
 182        int shown_header = 0;
 183        int i;
 184        for (i = 0; i < q->nr; i++) {
 185                if (q->queue[i]->status == 'U')
 186                        continue;
 187                if (!shown_header) {
 188                        wt_status_print_cached_header(s);
 189                        s->commitable = 1;
 190                        shown_header = 1;
 191                }
 192                wt_status_print_filepair(s, WT_STATUS_UPDATED, q->queue[i]);
 193        }
 194        if (shown_header)
 195                wt_status_print_trailer(s);
 196}
 197
 198static void wt_status_print_changed_cb(struct diff_queue_struct *q,
 199                        struct diff_options *options,
 200                        void *data)
 201{
 202        struct wt_status *s = data;
 203        int i;
 204        if (q->nr) {
 205                const char *msg = use_add_msg;
 206                s->workdir_dirty = 1;
 207                for (i = 0; i < q->nr; i++)
 208                        if (q->queue[i]->status == DIFF_STATUS_DELETED) {
 209                                msg = use_add_rm_msg;
 210                                break;
 211                        }
 212                wt_status_print_header(s, "Changed but not updated", msg);
 213        }
 214        for (i = 0; i < q->nr; i++)
 215                wt_status_print_filepair(s, WT_STATUS_CHANGED, q->queue[i]);
 216        if (q->nr)
 217                wt_status_print_trailer(s);
 218}
 219
 220static void wt_read_cache(struct wt_status *s)
 221{
 222        discard_cache();
 223        read_cache_from(s->index_file);
 224}
 225
 226static void wt_status_print_initial(struct wt_status *s)
 227{
 228        int i;
 229        struct strbuf buf;
 230
 231        strbuf_init(&buf, 0);
 232        wt_read_cache(s);
 233        if (active_nr) {
 234                s->commitable = 1;
 235                wt_status_print_cached_header(s);
 236        }
 237        for (i = 0; i < active_nr; i++) {
 238                color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
 239                color_fprintf_ln(s->fp, color(WT_STATUS_UPDATED), "new file: %s",
 240                                quote_path(active_cache[i]->name, -1,
 241                                           &buf, s->prefix));
 242        }
 243        if (active_nr)
 244                wt_status_print_trailer(s);
 245        strbuf_release(&buf);
 246}
 247
 248static void wt_status_print_updated(struct wt_status *s)
 249{
 250        struct rev_info rev;
 251        init_revisions(&rev, NULL);
 252        setup_revisions(0, NULL, &rev, s->reference);
 253        rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
 254        rev.diffopt.format_callback = wt_status_print_updated_cb;
 255        rev.diffopt.format_callback_data = s;
 256        rev.diffopt.detect_rename = 1;
 257        rev.diffopt.rename_limit = 100;
 258        rev.diffopt.break_opt = 0;
 259        wt_read_cache(s);
 260        run_diff_index(&rev, 1);
 261}
 262
 263static void wt_status_print_changed(struct wt_status *s)
 264{
 265        struct rev_info rev;
 266        init_revisions(&rev, "");
 267        setup_revisions(0, NULL, &rev, NULL);
 268        rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
 269        rev.diffopt.format_callback = wt_status_print_changed_cb;
 270        rev.diffopt.format_callback_data = s;
 271        wt_read_cache(s);
 272        run_diff_files(&rev, 0);
 273}
 274
 275static void wt_status_print_untracked(struct wt_status *s)
 276{
 277        struct dir_struct dir;
 278        int i;
 279        int shown_header = 0;
 280        struct strbuf buf;
 281
 282        strbuf_init(&buf, 0);
 283        memset(&dir, 0, sizeof(dir));
 284
 285        if (!s->untracked) {
 286                dir.show_other_directories = 1;
 287                dir.hide_empty_directories = 1;
 288        }
 289        setup_standard_excludes(&dir);
 290
 291        read_directory(&dir, ".", "", 0, NULL);
 292        for(i = 0; i < dir.nr; i++) {
 293                /* check for matching entry, which is unmerged; lifted from
 294                 * builtin-ls-files:show_other_files */
 295                struct dir_entry *ent = dir.entries[i];
 296                int pos = cache_name_pos(ent->name, ent->len);
 297                struct cache_entry *ce;
 298                if (0 <= pos)
 299                        die("bug in wt_status_print_untracked");
 300                pos = -pos - 1;
 301                if (pos < active_nr) {
 302                        ce = active_cache[pos];
 303                        if (ce_namelen(ce) == ent->len &&
 304                            !memcmp(ce->name, ent->name, ent->len))
 305                                continue;
 306                }
 307                if (!shown_header) {
 308                        s->workdir_untracked = 1;
 309                        wt_status_print_header(s, "Untracked files",
 310                                               use_add_to_include_msg);
 311                        shown_header = 1;
 312                }
 313                color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
 314                color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%s",
 315                                quote_path(ent->name, ent->len,
 316                                        &buf, s->prefix));
 317        }
 318        strbuf_release(&buf);
 319}
 320
 321static void wt_status_print_verbose(struct wt_status *s)
 322{
 323        struct rev_info rev;
 324        int saved_stdout;
 325
 326        fflush(s->fp);
 327
 328        /* Sigh, the entire diff machinery is hardcoded to output to
 329         * stdout.  Do the dup-dance...*/
 330        saved_stdout = dup(STDOUT_FILENO);
 331        if (saved_stdout < 0 ||dup2(fileno(s->fp), STDOUT_FILENO) < 0)
 332                die("couldn't redirect stdout\n");
 333
 334        init_revisions(&rev, NULL);
 335        setup_revisions(0, NULL, &rev, s->reference);
 336        rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
 337        rev.diffopt.detect_rename = 1;
 338        wt_read_cache(s);
 339        run_diff_index(&rev, 1);
 340
 341        fflush(stdout);
 342
 343        if (dup2(saved_stdout, STDOUT_FILENO) < 0)
 344                die("couldn't restore stdout\n");
 345        close(saved_stdout);
 346}
 347
 348void wt_status_print(struct wt_status *s)
 349{
 350        unsigned char sha1[20];
 351        s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
 352
 353        if (s->branch) {
 354                const char *on_what = "On branch ";
 355                const char *branch_name = s->branch;
 356                if (!prefixcmp(branch_name, "refs/heads/"))
 357                        branch_name += 11;
 358                else if (!strcmp(branch_name, "HEAD")) {
 359                        branch_name = "";
 360                        on_what = "Not currently on any branch.";
 361                }
 362                color_fprintf_ln(s->fp, color(WT_STATUS_HEADER),
 363                        "# %s%s", on_what, branch_name);
 364        }
 365
 366        if (s->is_initial) {
 367                color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
 368                color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "# Initial commit");
 369                color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
 370                wt_status_print_initial(s);
 371        }
 372        else {
 373                wt_status_print_updated(s);
 374        }
 375
 376        wt_status_print_changed(s);
 377        wt_status_print_untracked(s);
 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
 393                        printf("nothing to commit (working directory clean)\n");
 394        }
 395}
 396
 397int git_status_config(const char *k, const char *v)
 398{
 399        if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
 400                wt_status_use_color = git_config_colorbool(k, v, -1);
 401                return 0;
 402        }
 403        if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
 404                int slot = parse_status_slot(k, 13);
 405                color_parse(v, k, wt_status_colors[slot]);
 406                return 0;
 407        }
 408        if (!strcmp(k, "status.relativepaths")) {
 409                wt_status_relative_paths = git_config_bool(k, v);
 410                return 0;
 411        }
 412        return git_default_config(k, v);
 413}