unpack-trees: allow Porcelain to give different error messages
[gitweb.git] / diff-lib.c
index 4581b594d0a65d5bbe782b1db5d69f33189a0e50..fe2ccec7e6b3230715cbdf04591d1c0efb6a6389 100644 (file)
@@ -10,6 +10,7 @@
 #include "cache-tree.h"
 #include "path-list.h"
 #include "unpack-trees.h"
+#include "refs.h"
 
 /*
  * diff-files
@@ -263,6 +264,9 @@ int setup_diff_no_index(struct rev_info *revs,
                        DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS);
                        break;
                }
+       if (nongit && argc != i + 2)
+               die("git diff [--no-index] takes two paths");
+
        if (argc != i + 2 || (!is_outside_repo(argv[i + 1], nongit, prefix) &&
                                !is_outside_repo(argv[i], nongit, prefix)))
                return -1;
@@ -334,6 +338,45 @@ int run_diff_files_cmd(struct rev_info *revs, int argc, const char **argv)
        return run_diff_files(revs, options);
 }
 
+/*
+ * Has the work tree entity been removed?
+ *
+ * Return 1 if it was removed from the work tree, 0 if an entity to be
+ * compared with the cache entry ce still exists (the latter includes
+ * the case where a directory that is not a submodule repository
+ * exists for ce that is a submodule -- it is a submodule that is not
+ * checked out).  Return negative for an error.
+ */
+static int check_removed(const struct cache_entry *ce, struct stat *st)
+{
+       if (lstat(ce->name, st) < 0) {
+               if (errno != ENOENT && errno != ENOTDIR)
+                       return -1;
+               return 1;
+       }
+       if (has_symlink_leading_path(ce_namelen(ce), ce->name))
+               return 1;
+       if (S_ISDIR(st->st_mode)) {
+               unsigned char sub[20];
+
+               /*
+                * If ce is already a gitlink, we can have a plain
+                * directory (i.e. the submodule is not checked out),
+                * or a checked out submodule.  Either case this is not
+                * a case where something was removed from the work tree,
+                * so we will return 0.
+                *
+                * Otherwise, if the directory is not a submodule
+                * repository, that means ce which was a blob turned into
+                * a directory --- the blob was removed!
+                */
+               if (!S_ISGITLINK(ce->ce_mode) &&
+                   resolve_gitlink_ref(ce->name, "HEAD", sub))
+                       return 1;
+       }
+       return 0;
+}
+
 int run_diff_files(struct rev_info *revs, unsigned int option)
 {
        int entries, i;
@@ -341,10 +384,12 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
        int silent_on_removed = option & DIFF_SILENT_ON_REMOVED;
        unsigned ce_option = ((option & DIFF_RACY_IS_MODIFIED)
                              ? CE_MATCH_RACY_IS_DIRTY : 0);
+       char symcache[PATH_MAX];
 
        if (diff_unmerged_stage < 0)
                diff_unmerged_stage = 2;
        entries = active_nr;
+       symcache[0] = '\0';
        for (i = 0; i < entries; i++) {
                struct stat st;
                unsigned int oldmode, newmode;
@@ -376,16 +421,17 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
                        memset(&(dpath->parent[0]), 0,
                               sizeof(struct combine_diff_parent)*5);
 
-                       if (lstat(ce->name, &st) < 0) {
-                               if (errno != ENOENT && errno != ENOTDIR) {
+                       changed = check_removed(ce, &st);
+                       if (!changed)
+                               dpath->mode = ce_mode_from_stat(ce, st.st_mode);
+                       else {
+                               if (changed < 0) {
                                        perror(ce->name);
                                        continue;
                                }
                                if (silent_on_removed)
                                        continue;
                        }
-                       else
-                               dpath->mode = ce_mode_from_stat(ce, st.st_mode);
 
                        while (i < entries) {
                                struct cache_entry *nce = active_cache[i];
@@ -438,8 +484,10 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
 
                if (ce_uptodate(ce))
                        continue;
-               if (lstat(ce->name, &st) < 0) {
-                       if (errno != ENOENT && errno != ENOTDIR) {
+
+               changed = check_removed(ce, &st);
+               if (changed) {
+                       if (changed < 0) {
                                perror(ce->name);
                                continue;
                        }
@@ -450,8 +498,11 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
                        continue;
                }
                changed = ce_match_stat(ce, &st, ce_option);
-               if (!changed && !DIFF_OPT_TST(&revs->diffopt, FIND_COPIES_HARDER))
-                       continue;
+               if (!changed) {
+                       ce_mark_uptodate(ce);
+                       if (!DIFF_OPT_TST(&revs->diffopt, FIND_COPIES_HARDER))
+                               continue;
+               }
                oldmode = ce->ce_mode;
                newmode = ce_mode_from_stat(ce, st.st_mode);
                diff_change(&revs->diffopt, oldmode, newmode,
@@ -468,6 +519,11 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
  * diff-index
  */
 
+struct oneway_unpack_data {
+       struct rev_info *revs;
+       char symcache[PATH_MAX];
+};
+
 /* A file entry went away or appeared */
 static void diff_index_show_file(struct rev_info *revs,
                                 const char *prefix,
@@ -481,7 +537,8 @@ static void diff_index_show_file(struct rev_info *revs,
 static int get_stat_data(struct cache_entry *ce,
                         const unsigned char **sha1p,
                         unsigned int *modep,
-                        int cached, int match_missing)
+                        int cached, int match_missing,
+                        struct oneway_unpack_data *cbdata)
 {
        const unsigned char *sha1 = ce->sha1;
        unsigned int mode = ce->ce_mode;
@@ -489,8 +546,11 @@ static int get_stat_data(struct cache_entry *ce,
        if (!cached) {
                int changed;
                struct stat st;
-               if (lstat(ce->name, &st) < 0) {
-                       if (errno == ENOENT && match_missing) {
+               changed = check_removed(ce, &st);
+               if (changed < 0)
+                       return -1;
+               else if (changed) {
+                       if (match_missing) {
                                *sha1p = sha1;
                                *modep = mode;
                                return 0;
@@ -509,23 +569,25 @@ static int get_stat_data(struct cache_entry *ce,
        return 0;
 }
 
-static void show_new_file(struct rev_info *revs,
+static void show_new_file(struct oneway_unpack_data *cbdata,
                          struct cache_entry *new,
                          int cached, int match_missing)
 {
        const unsigned char *sha1;
        unsigned int mode;
+       struct rev_info *revs = cbdata->revs;
 
-       /* New file in the index: it might actually be different in
+       /*
+        * New file in the index: it might actually be different in
         * the working copy.
         */
-       if (get_stat_data(new, &sha1, &mode, cached, match_missing) < 0)
+       if (get_stat_data(new, &sha1, &mode, cached, match_missing, cbdata) < 0)
                return;
 
        diff_index_show_file(revs, "+", new, sha1, mode);
 }
 
-static int show_modified(struct rev_info *revs,
+static int show_modified(struct oneway_unpack_data *cbdata,
                         struct cache_entry *old,
                         struct cache_entry *new,
                         int report_missing,
@@ -533,8 +595,9 @@ static int show_modified(struct rev_info *revs,
 {
        unsigned int mode, oldmode;
        const unsigned char *sha1;
+       struct rev_info *revs = cbdata->revs;
 
-       if (get_stat_data(new, &sha1, &mode, cached, match_missing) < 0) {
+       if (get_stat_data(new, &sha1, &mode, cached, match_missing, cbdata) < 0) {
                if (report_missing)
                        diff_index_show_file(revs, "-", old,
                                             old->sha1, old->ce_mode);
@@ -600,10 +663,10 @@ static void mark_merge_entries(void)
  */
 static void do_oneway_diff(struct unpack_trees_options *o,
        struct cache_entry *idx,
-       struct cache_entry *tree,
-       int idx_pos, int idx_nr)
+       struct cache_entry *tree)
 {
-       struct rev_info *revs = o->unpack_data;
+       struct oneway_unpack_data *cbdata = o->unpack_data;
+       struct rev_info *revs = cbdata->revs;
        int match_missing, cached;
 
        /*
@@ -626,7 +689,7 @@ static void do_oneway_diff(struct unpack_trees_options *o,
         * Something added to the tree?
         */
        if (!tree) {
-               show_new_file(revs, idx, cached, match_missing);
+               show_new_file(cbdata, idx, cached, match_missing);
                return;
        }
 
@@ -639,35 +702,22 @@ static void do_oneway_diff(struct unpack_trees_options *o,
        }
 
        /* Show difference between old and new */
-       show_modified(revs, tree, idx, 1, cached, match_missing);
+       show_modified(cbdata, tree, idx, 1, cached, match_missing);
 }
 
-/*
- * Count how many index entries go with the first one
- */
-static inline int count_skip(const struct cache_entry *src, int pos)
+static inline void skip_same_name(struct cache_entry *ce, struct unpack_trees_options *o)
 {
-       int skip = 1;
-
-       /* We can only have multiple entries if the first one is not stage-0 */
-       if (ce_stage(src)) {
-               struct cache_entry **p = active_cache + pos;
-               int namelen = ce_namelen(src);
-
-               for (;;) {
-                       const struct cache_entry *ce;
-                       pos++;
-                       if (pos >= active_nr)
-                               break;
-                       ce = *++p;
-                       if (ce_namelen(ce) != namelen)
-                               break;
-                       if (memcmp(ce->name, src->name, namelen))
-                               break;
-                       skip++;
-               }
+       int len = ce_namelen(ce);
+       const struct index_state *index = o->src_index;
+
+       while (o->pos < index->cache_nr) {
+               struct cache_entry *next = index->cache[o->pos];
+               if (len != ce_namelen(next))
+                       break;
+               if (memcmp(ce->name, next->name, len))
+                       break;
+               o->pos++;
        }
-       return skip;
 }
 
 /*
@@ -685,17 +735,15 @@ static inline int count_skip(const struct cache_entry *src, int pos)
  * the fairly complex unpack_trees() semantic requirements, including
  * the skipping, the path matching, the type conflict cases etc.
  */
-static int oneway_diff(struct cache_entry **src,
-       struct unpack_trees_options *o,
-       int index_pos)
+static int oneway_diff(struct cache_entry **src, struct unpack_trees_options *o)
 {
-       int skip = 0;
        struct cache_entry *idx = src[0];
        struct cache_entry *tree = src[1];
-       struct rev_info *revs = o->unpack_data;
+       struct oneway_unpack_data *cbdata = o->unpack_data;
+       struct rev_info *revs = cbdata->revs;
 
-       if (index_pos >= 0)
-               skip = count_skip(idx, index_pos);
+       if (idx && ce_stage(idx))
+               skip_same_name(idx, o);
 
        /*
         * Unpack-trees generates a DF/conflict entry if
@@ -707,9 +755,9 @@ static int oneway_diff(struct cache_entry **src,
                tree = NULL;
 
        if (ce_path_match(idx ? idx : tree, revs->prune_data))
-               do_oneway_diff(o, idx, tree, index_pos, skip);
+               do_oneway_diff(o, idx, tree);
 
-       return skip;
+       return 0;
 }
 
 int run_diff_index(struct rev_info *revs, int cached)
@@ -719,6 +767,7 @@ int run_diff_index(struct rev_info *revs, int cached)
        const char *tree_name;
        struct unpack_trees_options opts;
        struct tree_desc t;
+       struct oneway_unpack_data unpack_cb;
 
        mark_merge_entries();
 
@@ -728,12 +777,16 @@ int run_diff_index(struct rev_info *revs, int cached)
        if (!tree)
                return error("bad tree object %s", tree_name);
 
+       unpack_cb.revs = revs;
+       unpack_cb.symcache[0] = '\0';
        memset(&opts, 0, sizeof(opts));
        opts.head_idx = 1;
        opts.index_only = cached;
        opts.merge = 1;
        opts.fn = oneway_diff;
-       opts.unpack_data = revs;
+       opts.unpack_data = &unpack_cb;
+       opts.src_index = &the_index;
+       opts.dst_index = NULL;
 
        init_tree_desc(&t, tree->buffer, tree->size);
        if (unpack_trees(1, &t, &opts))
@@ -753,6 +806,7 @@ int do_diff_cache(const unsigned char *tree_sha1, struct diff_options *opt)
        struct cache_entry *last = NULL;
        struct unpack_trees_options opts;
        struct tree_desc t;
+       struct oneway_unpack_data unpack_cb;
 
        /*
         * This is used by git-blame to run diff-cache internally;
@@ -781,12 +835,16 @@ int do_diff_cache(const unsigned char *tree_sha1, struct diff_options *opt)
        if (!tree)
                die("bad tree object %s", sha1_to_hex(tree_sha1));
 
+       unpack_cb.revs = &revs;
+       unpack_cb.symcache[0] = '\0';
        memset(&opts, 0, sizeof(opts));
        opts.head_idx = 1;
        opts.index_only = 1;
        opts.merge = 1;
        opts.fn = oneway_diff;
-       opts.unpack_data = &revs;
+       opts.unpack_data = &unpack_cb;
+       opts.src_index = &the_index;
+       opts.dst_index = &the_index;
 
        init_tree_desc(&t, tree->buffer, tree->size);
        if (unpack_trees(1, &t, &opts))