diff-cache.con commit [PATCH] Bugfix: read-cache.c:write_cache() misrecords number of entries. (025a070)
   1#include "cache.h"
   2#include "diff.h"
   3
   4static int cached_only = 0;
   5static int diff_output_format = DIFF_FORMAT_HUMAN;
   6static int match_nonexisting = 0;
   7static int detect_rename = 0;
   8static int diff_setup_opt = 0;
   9static int diff_score_opt = 0;
  10static const char *pickaxe = NULL;
  11static int pickaxe_opts = 0;
  12static int diff_break_opt = -1;
  13static const char *orderfile = NULL;
  14
  15/* A file entry went away or appeared */
  16static void show_file(const char *prefix, struct cache_entry *ce, unsigned char *sha1, unsigned int mode)
  17{
  18        diff_addremove(prefix[0], ntohl(mode), sha1, ce->name, NULL);
  19}
  20
  21static int get_stat_data(struct cache_entry *ce, unsigned char **sha1p, unsigned int *modep)
  22{
  23        unsigned char *sha1 = ce->sha1;
  24        unsigned int mode = ce->ce_mode;
  25
  26        if (!cached_only) {
  27                static unsigned char no_sha1[20];
  28                int changed;
  29                struct stat st;
  30                if (lstat(ce->name, &st) < 0) {
  31                        if (errno == ENOENT && match_nonexisting) {
  32                                *sha1p = sha1;
  33                                *modep = mode;
  34                                return 0;
  35                        }
  36                        return -1;
  37                }
  38                changed = ce_match_stat(ce, &st);
  39                if (changed) {
  40                        mode = create_ce_mode(st.st_mode);
  41                        sha1 = no_sha1;
  42                }
  43        }
  44
  45        *sha1p = sha1;
  46        *modep = mode;
  47        return 0;
  48}
  49
  50static void show_new_file(struct cache_entry *new)
  51{
  52        unsigned char *sha1;
  53        unsigned int mode;
  54
  55        /* New file in the index: it might actually be different in the working copy */
  56        if (get_stat_data(new, &sha1, &mode) < 0)
  57                return;
  58
  59        show_file("+", new, sha1, mode);
  60}
  61
  62static int show_modified(struct cache_entry *old,
  63                         struct cache_entry *new,
  64                         int report_missing)
  65{
  66        unsigned int mode, oldmode;
  67        unsigned char *sha1;
  68
  69        if (get_stat_data(new, &sha1, &mode) < 0) {
  70                if (report_missing)
  71                        show_file("-", old, old->sha1, old->ce_mode);
  72                return -1;
  73        }
  74
  75        oldmode = old->ce_mode;
  76        if (mode == oldmode && !memcmp(sha1, old->sha1, 20) &&
  77            detect_rename < DIFF_DETECT_COPY)
  78                return 0;
  79
  80        mode = ntohl(mode);
  81        oldmode = ntohl(oldmode);
  82
  83        diff_change(oldmode, mode,
  84                    old->sha1, sha1, old->name, NULL);
  85        return 0;
  86}
  87
  88static int diff_cache(struct cache_entry **ac, int entries)
  89{
  90        while (entries) {
  91                struct cache_entry *ce = *ac;
  92                int same = (entries > 1) && ce_same_name(ce, ac[1]);
  93
  94                switch (ce_stage(ce)) {
  95                case 0:
  96                        /* No stage 1 entry? That means it's a new file */
  97                        if (!same) {
  98                                show_new_file(ce);
  99                                break;
 100                        }
 101                        /* Show difference between old and new */
 102                        show_modified(ac[1], ce, 1);
 103                        break;
 104                case 1:
 105                        /* No stage 3 (merge) entry? That means it's been deleted */
 106                        if (!same) {
 107                                show_file("-", ce, ce->sha1, ce->ce_mode);
 108                                break;
 109                        }
 110                        /* We come here with ce pointing at stage 1
 111                         * (original tree) and ac[1] pointing at stage
 112                         * 3 (unmerged).  show-modified with
 113                         * report-mising set to false does not say the
 114                         * file is deleted but reports true if work
 115                         * tree does not have it, in which case we
 116                         * fall through to report the unmerged state.
 117                         * Otherwise, we show the differences between
 118                         * the original tree and the work tree.
 119                         */
 120                        if (!cached_only && !show_modified(ce, ac[1], 0))
 121                                break;
 122                        /* fallthru */
 123                case 3:
 124                        diff_unmerge(ce->name);
 125                        break;
 126
 127                default:
 128                        die("impossible cache entry stage");
 129                }
 130
 131                /*
 132                 * Ignore all the different stages for this file,
 133                 * we've handled the relevant cases now.
 134                 */
 135                do {
 136                        ac++;
 137                        entries--;
 138                } while (entries && ce_same_name(ce, ac[0]));
 139        }
 140        return 0;
 141}
 142
 143/*
 144 * This turns all merge entries into "stage 3". That guarantees that
 145 * when we read in the new tree (into "stage 1"), we won't lose sight
 146 * of the fact that we had unmerged entries.
 147 */
 148static void mark_merge_entries(void)
 149{
 150        int i;
 151        for (i = 0; i < active_nr; i++) {
 152                struct cache_entry *ce = active_cache[i];
 153                if (!ce_stage(ce))
 154                        continue;
 155                ce->ce_flags |= htons(CE_STAGEMASK);
 156        }
 157}
 158
 159static char *diff_cache_usage =
 160"git-diff-cache [-p] [-r] [-z] [-m] [-M] [-C] [-R] [-S<string>] [-O<orderfile>] [--cached] <tree-ish> [<path>...]";
 161
 162int main(int argc, const char **argv)
 163{
 164        const char *tree_name = NULL;
 165        unsigned char sha1[20];
 166        const char **pathspec = NULL;
 167        void *tree;
 168        unsigned long size;
 169        int ret;
 170        int allow_options = 1;
 171        int i;
 172
 173        read_cache();
 174        for (i = 1; i < argc; i++) {
 175                const char *arg = argv[i];
 176
 177                if (!allow_options || *arg != '-') {
 178                        if (tree_name) {
 179                                pathspec = argv + i;
 180                                break;
 181                        }
 182                        tree_name = arg;
 183                        continue;
 184                }
 185                        
 186                if (!strcmp(arg, "--")) {
 187                        allow_options = 0;
 188                        continue;
 189                }
 190                if (!strcmp(arg, "-r")) {
 191                        /* We accept the -r flag just to look like git-diff-tree */
 192                        continue;
 193                }
 194                if (!strcmp(arg, "-p")) {
 195                        diff_output_format = DIFF_FORMAT_PATCH;
 196                        continue;
 197                }
 198                if (!strncmp(arg, "-B", 2)) {
 199                        if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1)
 200                                usage(diff_cache_usage);
 201                        continue;
 202                }
 203                if (!strncmp(arg, "-M", 2)) {
 204                        detect_rename = DIFF_DETECT_RENAME;
 205                        if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
 206                                usage(diff_cache_usage);
 207                        continue;
 208                }
 209                if (!strncmp(arg, "-C", 2)) {
 210                        detect_rename = DIFF_DETECT_COPY;
 211                        if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
 212                                usage(diff_cache_usage);
 213                        continue;
 214                }
 215                if (!strcmp(arg, "-z")) {
 216                        diff_output_format = DIFF_FORMAT_MACHINE;
 217                        continue;
 218                }
 219                if (!strcmp(arg, "-R")) {
 220                        diff_setup_opt |= DIFF_SETUP_REVERSE;
 221                        continue;
 222                }
 223                if (!strncmp(arg, "-S", 2)) {
 224                        pickaxe = arg + 2;
 225                        continue;
 226                }
 227                if (!strncmp(arg, "-O", 2)) {
 228                        orderfile = arg + 2;
 229                        continue;
 230                }
 231                if (!strcmp(arg, "--pickaxe-all")) {
 232                        pickaxe_opts = DIFF_PICKAXE_ALL;
 233                        continue;
 234                }
 235                if (!strcmp(arg, "-m")) {
 236                        match_nonexisting = 1;
 237                        continue;
 238                }
 239                if (!strcmp(arg, "--cached")) {
 240                        cached_only = 1;
 241                        continue;
 242                }
 243                usage(diff_cache_usage);
 244        }
 245
 246        if (!tree_name || get_sha1(tree_name, sha1))
 247                usage(diff_cache_usage);
 248
 249        /* The rest is for paths restriction. */
 250        diff_setup(diff_setup_opt);
 251
 252        mark_merge_entries();
 253
 254        tree = read_object_with_reference(sha1, "tree", &size, NULL);
 255        if (!tree)
 256                die("bad tree object %s", tree_name);
 257        if (read_tree(tree, size, 1))
 258                die("unable to read tree object %s", tree_name);
 259
 260        ret = diff_cache(active_cache, active_nr);
 261
 262        diffcore_std(pathspec ? : NULL,
 263                     detect_rename, diff_score_opt,
 264                     pickaxe, pickaxe_opts,
 265                     diff_break_opt,
 266                     orderfile);
 267        diff_flush(diff_output_format, 1);
 268        return ret;
 269}