2173d4f66851e3286ef2fca48e8ae88da1d1db3a
   1#include "wt-status.h"
   2#include "color.h"
   3#include "cache.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_use_color = 0;
  12static char wt_status_colors[][COLOR_MAXLEN] = {
  13        "",         /* WT_STATUS_HEADER: normal */
  14        "\033[32m", /* WT_STATUS_UPDATED: green */
  15        "\033[31m", /* WT_STATUS_CHANGED: red */
  16        "\033[31m", /* WT_STATUS_UNTRACKED: red */
  17};
  18static const char* use_add_msg = "use \"git add file1 file2\" to include for commit";
  19
  20static int parse_status_slot(const char *var, int offset)
  21{
  22        if (!strcasecmp(var+offset, "header"))
  23                return WT_STATUS_HEADER;
  24        if (!strcasecmp(var+offset, "updated"))
  25                return WT_STATUS_UPDATED;
  26        if (!strcasecmp(var+offset, "changed"))
  27                return WT_STATUS_CHANGED;
  28        if (!strcasecmp(var+offset, "untracked"))
  29                return WT_STATUS_UNTRACKED;
  30        die("bad config variable '%s'", var);
  31}
  32
  33static const char* color(int slot)
  34{
  35        return wt_status_use_color ? wt_status_colors[slot] : "";
  36}
  37
  38void wt_status_prepare(struct wt_status *s)
  39{
  40        unsigned char sha1[20];
  41        const char *head;
  42
  43        s->is_initial = get_sha1("HEAD", sha1) ? 1 : 0;
  44
  45        head = resolve_ref("HEAD", sha1, 0, NULL);
  46        s->branch = head ? xstrdup(head) : NULL;
  47
  48        s->reference = "HEAD";
  49        s->amend = 0;
  50        s->verbose = 0;
  51        s->commitable = 0;
  52        s->untracked = 0;
  53}
  54
  55static void wt_status_print_header(const char *main, const char *sub)
  56{
  57        const char *c = color(WT_STATUS_HEADER);
  58        color_printf_ln(c, "# %s:", main);
  59        color_printf_ln(c, "#   (%s)", sub);
  60        color_printf_ln(c, "#");
  61}
  62
  63static void wt_status_print_trailer(void)
  64{
  65        color_printf_ln(color(WT_STATUS_HEADER), "#");
  66}
  67
  68static const char *quote_crlf(const char *in, char *buf, size_t sz)
  69{
  70        const char *scan;
  71        char *out;
  72        const char *ret = in;
  73
  74        for (scan = in, out = buf; *scan; scan++) {
  75                int ch = *scan;
  76                int quoted;
  77
  78                switch (ch) {
  79                case '\n':
  80                        quoted = 'n';
  81                        break;
  82                case '\r':
  83                        quoted = 'r';
  84                        break;
  85                default:
  86                        *out++ = ch;
  87                        continue;
  88                }
  89                *out++ = '\\';
  90                *out++ = quoted;
  91                ret = buf;
  92        }
  93        *out = '\0';
  94        return ret;
  95}
  96
  97static void wt_status_print_filepair(int t, struct diff_filepair *p)
  98{
  99        const char *c = color(t);
 100        const char *one, *two;
 101        char onebuf[PATH_MAX], twobuf[PATH_MAX];
 102
 103        one = quote_crlf(p->one->path, onebuf, sizeof(onebuf));
 104        two = quote_crlf(p->two->path, twobuf, sizeof(twobuf));
 105
 106        color_printf(color(WT_STATUS_HEADER), "#\t");
 107        switch (p->status) {
 108        case DIFF_STATUS_ADDED:
 109                color_printf(c, "new file:   %s", one);
 110                break;
 111        case DIFF_STATUS_COPIED:
 112                color_printf(c, "copied:     %s -> %s", one, two);
 113                break;
 114        case DIFF_STATUS_DELETED:
 115                color_printf(c, "deleted:    %s", one);
 116                break;
 117        case DIFF_STATUS_MODIFIED:
 118                color_printf(c, "modified:   %s", one);
 119                break;
 120        case DIFF_STATUS_RENAMED:
 121                color_printf(c, "renamed:    %s -> %s", one, two);
 122                break;
 123        case DIFF_STATUS_TYPE_CHANGED:
 124                color_printf(c, "typechange: %s", one);
 125                break;
 126        case DIFF_STATUS_UNKNOWN:
 127                color_printf(c, "unknown:    %s", one);
 128                break;
 129        case DIFF_STATUS_UNMERGED:
 130                color_printf(c, "unmerged:   %s", one);
 131                break;
 132        default:
 133                die("bug: unhandled diff status %c", p->status);
 134        }
 135        printf("\n");
 136}
 137
 138static void wt_status_print_updated_cb(struct diff_queue_struct *q,
 139                struct diff_options *options,
 140                void *data)
 141{
 142        struct wt_status *s = data;
 143        int shown_header = 0;
 144        int i;
 145        for (i = 0; i < q->nr; i++) {
 146                if (q->queue[i]->status == 'U')
 147                        continue;
 148                if (!shown_header) {
 149                        wt_status_print_header("Updated but not checked in",
 150                                        "will commit");
 151                        s->commitable = 1;
 152                        shown_header = 1;
 153                }
 154                wt_status_print_filepair(WT_STATUS_UPDATED, q->queue[i]);
 155        }
 156        if (shown_header)
 157                wt_status_print_trailer();
 158}
 159
 160static void wt_status_print_changed_cb(struct diff_queue_struct *q,
 161                        struct diff_options *options,
 162                        void *data)
 163{
 164        int i;
 165        if (q->nr)
 166                wt_status_print_header("Changed but not updated", use_add_msg);
 167        for (i = 0; i < q->nr; i++)
 168                wt_status_print_filepair(WT_STATUS_CHANGED, q->queue[i]);
 169        if (q->nr)
 170                wt_status_print_trailer();
 171}
 172
 173void wt_status_print_initial(struct wt_status *s)
 174{
 175        int i;
 176        char buf[PATH_MAX];
 177
 178        read_cache();
 179        if (active_nr) {
 180                s->commitable = 1;
 181                wt_status_print_header("Updated but not checked in",
 182                                "will commit");
 183        }
 184        for (i = 0; i < active_nr; i++) {
 185                color_printf(color(WT_STATUS_HEADER), "#\t");
 186                color_printf_ln(color(WT_STATUS_UPDATED), "new file: %s",
 187                                quote_crlf(active_cache[i]->name,
 188                                           buf, sizeof(buf)));
 189        }
 190        if (active_nr)
 191                wt_status_print_trailer();
 192}
 193
 194static void wt_status_print_updated(struct wt_status *s)
 195{
 196        struct rev_info rev;
 197        init_revisions(&rev, NULL);
 198        setup_revisions(0, NULL, &rev, s->reference);
 199        rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
 200        rev.diffopt.format_callback = wt_status_print_updated_cb;
 201        rev.diffopt.format_callback_data = s;
 202        rev.diffopt.detect_rename = 1;
 203        run_diff_index(&rev, 1);
 204}
 205
 206static void wt_status_print_changed(struct wt_status *s)
 207{
 208        struct rev_info rev;
 209        init_revisions(&rev, "");
 210        setup_revisions(0, NULL, &rev, NULL);
 211        rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
 212        rev.diffopt.format_callback = wt_status_print_changed_cb;
 213        rev.diffopt.format_callback_data = s;
 214        run_diff_files(&rev, 0);
 215}
 216
 217static void wt_status_print_untracked(const struct wt_status *s)
 218{
 219        struct dir_struct dir;
 220        const char *x;
 221        int i;
 222        int shown_header = 0;
 223
 224        memset(&dir, 0, sizeof(dir));
 225
 226        dir.exclude_per_dir = ".gitignore";
 227        if (!s->untracked) {
 228                dir.show_other_directories = 1;
 229                dir.hide_empty_directories = 1;
 230        }
 231        x = git_path("info/exclude");
 232        if (file_exists(x))
 233                add_excludes_from_file(&dir, x);
 234
 235        read_directory(&dir, ".", "", 0);
 236        for(i = 0; i < dir.nr; i++) {
 237                /* check for matching entry, which is unmerged; lifted from
 238                 * builtin-ls-files:show_other_files */
 239                struct dir_entry *ent = dir.entries[i];
 240                int pos = cache_name_pos(ent->name, ent->len);
 241                struct cache_entry *ce;
 242                if (0 <= pos)
 243                        die("bug in wt_status_print_untracked");
 244                pos = -pos - 1;
 245                if (pos < active_nr) {
 246                        ce = active_cache[pos];
 247                        if (ce_namelen(ce) == ent->len &&
 248                            !memcmp(ce->name, ent->name, ent->len))
 249                                continue;
 250                }
 251                if (!shown_header) {
 252                        wt_status_print_header("Untracked files", use_add_msg);
 253                        shown_header = 1;
 254                }
 255                color_printf(color(WT_STATUS_HEADER), "#\t");
 256                color_printf_ln(color(WT_STATUS_UNTRACKED), "%.*s",
 257                                ent->len, ent->name);
 258        }
 259}
 260
 261static void wt_status_print_verbose(struct wt_status *s)
 262{
 263        struct rev_info rev;
 264        init_revisions(&rev, NULL);
 265        setup_revisions(0, NULL, &rev, s->reference);
 266        rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
 267        rev.diffopt.detect_rename = 1;
 268        run_diff_index(&rev, 1);
 269}
 270
 271void wt_status_print(struct wt_status *s)
 272{
 273        if (s->branch && strcmp(s->branch, "refs/heads/master"))
 274                color_printf_ln(color(WT_STATUS_HEADER),
 275                        "# On branch %s", s->branch);
 276
 277        if (s->is_initial) {
 278                color_printf_ln(color(WT_STATUS_HEADER), "#");
 279                color_printf_ln(color(WT_STATUS_HEADER), "# Initial commit");
 280                color_printf_ln(color(WT_STATUS_HEADER), "#");
 281                wt_status_print_initial(s);
 282        }
 283        else {
 284                wt_status_print_updated(s);
 285                discard_cache();
 286        }
 287
 288        wt_status_print_changed(s);
 289        wt_status_print_untracked(s);
 290
 291        if (s->verbose && !s->is_initial)
 292                wt_status_print_verbose(s);
 293        if (!s->commitable)
 294                printf("%s (%s)\n",
 295                        s->amend ? "# No changes" : "nothing to commit",
 296                        use_add_msg);
 297}
 298
 299int git_status_config(const char *k, const char *v)
 300{
 301        if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
 302                wt_status_use_color = git_config_colorbool(k, v);
 303                return 0;
 304        }
 305        if (!strncmp(k, "status.color.", 13) || !strncmp(k, "color.status", 13)) {
 306                int slot = parse_status_slot(k, 13);
 307                color_parse(v, k, wt_status_colors[slot]);
 308        }
 309        return git_default_config(k, v);
 310}