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