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