"git-add -n -u" should not add but just report
authorJunio C Hamano <gitster@pobox.com>
Wed, 21 May 2008 19:04:34 +0000 (12:04 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 21 May 2008 19:04:41 +0000 (12:04 -0700)
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin-add.c
builtin-checkout.c
builtin-commit.c
builtin-mv.c
cache.h
read-cache.c
t/t2200-add-update.sh
index 4a91e3eb118850882fbc22e8d8f37e8bbfaa7617..05af57f6ec6d3d9b936a1808a005cb08e47a8d94 100644 (file)
@@ -82,9 +82,9 @@ static void fill_directory(struct dir_struct *dir, const char **pathspec,
 static void update_callback(struct diff_queue_struct *q,
                            struct diff_options *opt, void *cbdata)
 {
-       int i, verbose;
+       int i, flags;
 
-       verbose = *((int *)cbdata);
+       flags = *((int *)cbdata);
        for (i = 0; i < q->nr; i++) {
                struct diff_filepair *p = q->queue[i];
                const char *path = p->one->path;
@@ -94,18 +94,19 @@ static void update_callback(struct diff_queue_struct *q,
                case DIFF_STATUS_UNMERGED:
                case DIFF_STATUS_MODIFIED:
                case DIFF_STATUS_TYPE_CHANGED:
-                       add_file_to_cache(path, verbose);
+                       add_file_to_cache(path, flags);
                        break;
                case DIFF_STATUS_DELETED:
-                       remove_file_from_cache(path);
-                       if (verbose)
+                       if (!(flags & ADD_CACHE_PRETEND))
+                               remove_file_from_cache(path);
+                       if (flags)
                                printf("remove '%s'\n", path);
                        break;
                }
        }
 }
 
-void add_files_to_cache(int verbose, const char *prefix, const char **pathspec)
+void add_files_to_cache(const char *prefix, const char **pathspec, int flags)
 {
        struct rev_info rev;
        init_revisions(&rev, prefix);
@@ -113,7 +114,7 @@ void add_files_to_cache(int verbose, const char *prefix, const char **pathspec)
        rev.prune_data = pathspec;
        rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
        rev.diffopt.format_callback = update_callback;
-       rev.diffopt.format_callback_data = &verbose;
+       rev.diffopt.format_callback_data = &flags;
        run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
 }
 
@@ -209,10 +210,13 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 
        if (take_worktree_changes) {
                const char **pathspec;
+               int flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
+                            (show_only ? ADD_CACHE_PRETEND : 0));
+
                if (read_cache() < 0)
                        die("index file corrupt");
                pathspec = get_pathspec(prefix, argv);
-               add_files_to_cache(verbose, prefix, pathspec);
+               add_files_to_cache(prefix, pathspec, flags);
                goto finish;
        }
 
@@ -254,7 +258,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
        }
 
        for (i = 0; i < dir.nr; i++)
-               add_file_to_cache(dir.entries[i]->name, verbose);
+               add_file_to_cache(dir.entries[i]->name, verbose ? ADD_CACHE_VERBOSE : 0);
 
  finish:
        if (active_cache_changed) {
index 10ec137ccec7d7f281562817d81a1035c4aa6124..05c06421b6c3a6d75b598e3c58a40c8461febd75 100644 (file)
@@ -282,7 +282,7 @@ static int merge_working_tree(struct checkout_opts *opts,
                         * entries in the index.
                         */
 
-                       add_files_to_cache(0, NULL, NULL);
+                       add_files_to_cache(NULL, NULL, 0);
                        work = write_tree_from_memory();
 
                        ret = reset_to_new(new->commit->tree, opts->quiet);
index 0baec6db6ac76de81b93f4c990f8347814618be7..924fca1953c155d0cec5ae2a0f53b7c43e8b8c58 100644 (file)
@@ -246,7 +246,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix)
         */
        if (all || (also && pathspec && *pathspec)) {
                int fd = hold_locked_index(&index_lock, 1);
-               add_files_to_cache(0, also ? prefix : NULL, pathspec);
+               add_files_to_cache(also ? prefix : NULL, pathspec, 0);
                refresh_cache(REFRESH_QUIET);
                if (write_cache(fd, active_cache, active_nr) ||
                    close_lock_file(&index_lock))
index 94f6dd2aad2da512bab0be2e84859eced6553cc9..df9ea97744f0473f1ee4a7c7c8e78a87ccb22865 100644 (file)
@@ -256,7 +256,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 
                for (i = 0; i < added.nr; i++) {
                        const char *path = added.items[i].path;
-                       add_file_to_cache(path, verbose);
+                       add_file_to_cache(path, verbose ? ADD_CACHE_VERBOSE : 0);
                }
 
                for (i = 0; i < deleted.nr; i++)
diff --git a/cache.h b/cache.h
index 093f04cec0459e67c54f3fc5b536f59fb0f343dc..b1a8427b9193ed49ba3afd8a31e278b268616ad5 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -261,8 +261,8 @@ static inline void remove_name_hash(struct cache_entry *ce)
 #define add_cache_entry(ce, option) add_index_entry(&the_index, (ce), (option))
 #define remove_cache_entry_at(pos) remove_index_entry_at(&the_index, (pos))
 #define remove_file_from_cache(path) remove_file_from_index(&the_index, (path))
-#define add_to_cache(path, st, verbose) add_to_index(&the_index, (path), (st), (verbose))
-#define add_file_to_cache(path, verbose) add_file_to_index(&the_index, (path), (verbose))
+#define add_to_cache(path, st, flags) add_to_index(&the_index, (path), (st), (flags))
+#define add_file_to_cache(path, flags) add_file_to_index(&the_index, (path), (flags))
 #define refresh_cache(flags) refresh_index(&the_index, (flags), NULL, NULL)
 #define ce_match_stat(ce, st, options) ie_match_stat(&the_index, (ce), (st), (options))
 #define ce_modified(ce, st, options) ie_modified(&the_index, (ce), (st), (options))
@@ -366,8 +366,10 @@ extern int add_index_entry(struct index_state *, struct cache_entry *ce, int opt
 extern struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really);
 extern int remove_index_entry_at(struct index_state *, int pos);
 extern int remove_file_from_index(struct index_state *, const char *path);
-extern int add_to_index(struct index_state *, const char *path, struct stat *, int verbose);
-extern int add_file_to_index(struct index_state *, const char *path, int verbose);
+#define ADD_CACHE_VERBOSE 1
+#define ADD_CACHE_PRETEND 2
+extern int add_to_index(struct index_state *, const char *path, struct stat *, int flags);
+extern int add_file_to_index(struct index_state *, const char *path, int flags);
 extern struct cache_entry *make_cache_entry(unsigned int mode, const unsigned char *sha1, const char *path, int stage, int refresh);
 extern int ce_same_name(struct cache_entry *a, struct cache_entry *b);
 
@@ -782,7 +784,7 @@ extern int convert_to_git(const char *path, const char *src, size_t len,
 extern int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst);
 
 /* add */
-void add_files_to_cache(int verbose, const char *prefix, const char **pathspec);
+void add_files_to_cache(const char *prefix, const char **pathspec, int flags);
 
 /* diff.c */
 extern int diff_auto_refresh_index;
index 0382804e7694e9a0e87cc2dc8340187ddeea2d64..5d967e8ec9520a54fe615c4aef6fa6c8e5294dc6 100644 (file)
@@ -462,12 +462,14 @@ static struct cache_entry *create_alias_ce(struct cache_entry *ce, struct cache_
        return new;
 }
 
-int add_to_index(struct index_state *istate, const char *path, struct stat *st, int verbose)
+int add_to_index(struct index_state *istate, const char *path, struct stat *st, int flags)
 {
-       int size, namelen;
+       int size, namelen, was_same;
        mode_t st_mode = st->st_mode;
        struct cache_entry *ce, *alias;
        unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_RACY_IS_DIRTY;
+       int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND);
+       int pretend = flags & ADD_CACHE_PRETEND;
 
        if (!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode))
                die("%s: can only add regular files, symbolic links or git-directories", path);
@@ -509,19 +511,28 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
        if (ignore_case && alias && different_name(ce, alias))
                ce = create_alias_ce(ce, alias);
        ce->ce_flags |= CE_ADDED;
-       if (add_index_entry(istate, ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE))
+
+       /* It was suspected to be recily clean, but it turns out to be Ok */
+       was_same = (alias &&
+                   !ce_stage(alias) &&
+                   !hashcmp(alias->sha1, ce->sha1) &&
+                   ce->ce_mode == alias->ce_mode);
+
+       if (pretend)
+               ;
+       else if (add_index_entry(istate, ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE))
                die("unable to add %s to index",path);
-       if (verbose)
+       if (verbose && !was_same)
                printf("add '%s'\n", path);
        return 0;
 }
 
-int add_file_to_index(struct index_state *istate, const char *path, int verbose)
+int add_file_to_index(struct index_state *istate, const char *path, int flags)
 {
        struct stat st;
        if (lstat(path, &st))
                die("%s: unable to stat (%s)", path, strerror(errno));
-       return add_to_index(istate, path, &st, verbose);
+       return add_to_index(istate, path, &st, flags);
 }
 
 struct cache_entry *make_cache_entry(unsigned int mode,
index b664341926071a3286ba949dc09b5e62b280e79a..f57a6e077c3b85dcdedc3f4813150feebc8e647d 100755 (executable)
@@ -111,4 +111,21 @@ test_expect_success 'touch and then add explicitly' '
 
 '
 
+test_expect_success 'add -n -u should not add but just report' '
+
+       (
+               echo "add '\''check'\''" &&
+               echo "remove '\''top'\''"
+       ) >expect &&
+       before=$(git ls-files -s check top) &&
+       echo changed >>check &&
+       rm -f top &&
+       git add -n -u >actual &&
+       after=$(git ls-files -s check top) &&
+
+       test "$before" = "$after" &&
+       test_cmp expect actual
+
+'
+
 test_done