@matheusbsbs / matheus-bagliano / commits / 38ed1d89f7
Fork 0

"git-add -n -u" should not add but just report

Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed May 21, 2008 at 12:04 UTC 38ed1d89f759699de56004b08668e1764613f47b
7 files changed +57 -23
builtin-add.c
+13 -9
@@ -82,9 +82,9 @@ static void fill_directory(struct dir_struct *dir, const char **pathspec,
82 static void update_callback(struct diff_queue_struct *q,
83 struct diff_options *opt, void *cbdata)
84 {
85 - int i, verbose;
85 + int i, flags;
86
87 - verbose = *((int *)cbdata);
87 + flags = *((int *)cbdata);
88 for (i = 0; i < q->nr; i++) {
89 struct diff_filepair *p = q->queue[i];
90 const char *path = p->one->path;
@@ -94,18 +94,19 @@ static void update_callback(struct diff_queue_struct *q,
94 case DIFF_STATUS_UNMERGED:
95 case DIFF_STATUS_MODIFIED:
96 case DIFF_STATUS_TYPE_CHANGED:
97 - add_file_to_cache(path, verbose);
97 + add_file_to_cache(path, flags);
98 break;
99 case DIFF_STATUS_DELETED:
100 - remove_file_from_cache(path);
101 - if (verbose)
100 + if (!(flags & ADD_CACHE_PRETEND))
101 + remove_file_from_cache(path);
102 + if (flags)
103 printf("remove '%s'\n", path);
104 break;
105 }
106 }
107 }
108
108 -void add_files_to_cache(int verbose, const char *prefix, const char **pathspec)
109 +void add_files_to_cache(const char *prefix, const char **pathspec, int flags)
110 {
111 struct rev_info rev;
112 init_revisions(&rev, prefix);
@@ -113,7 +114,7 @@ void add_files_to_cache(int verbose, const char *prefix, const char **pathspec)
114 rev.prune_data = pathspec;
115 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
116 rev.diffopt.format_callback = update_callback;
116 - rev.diffopt.format_callback_data = &verbose;
117 + rev.diffopt.format_callback_data = &flags;
118 run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
119 }
120
@@ -209,10 +210,13 @@ int cmd_add(int argc, const char **argv, const char *prefix)
210
211 if (take_worktree_changes) {
212 const char **pathspec;
213 + int flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
214 + (show_only ? ADD_CACHE_PRETEND : 0));
215 +
216 if (read_cache() < 0)
217 die("index file corrupt");
218 pathspec = get_pathspec(prefix, argv);
215 - add_files_to_cache(verbose, prefix, pathspec);
219 + add_files_to_cache(prefix, pathspec, flags);
220 goto finish;
221 }
222
@@ -254,7 +258,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
258 }
259
260 for (i = 0; i < dir.nr; i++)
257 - add_file_to_cache(dir.entries[i]->name, verbose);
261 + add_file_to_cache(dir.entries[i]->name, verbose ? ADD_CACHE_VERBOSE : 0);
262
263 finish:
264 if (active_cache_changed) {
builtin-checkout.c
+1 -1
@@ -282,7 +282,7 @@ static int merge_working_tree(struct checkout_opts *opts,
282 * entries in the index.
283 */
284
285 - add_files_to_cache(0, NULL, NULL);
285 + add_files_to_cache(NULL, NULL, 0);
286 work = write_tree_from_memory();
287
288 ret = reset_to_new(new->commit->tree, opts->quiet);
builtin-commit.c
+1 -1
@@ -246,7 +246,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix)
246 */
247 if (all || (also && pathspec && *pathspec)) {
248 int fd = hold_locked_index(&index_lock, 1);
249 - add_files_to_cache(0, also ? prefix : NULL, pathspec);
249 + add_files_to_cache(also ? prefix : NULL, pathspec, 0);
250 refresh_cache(REFRESH_QUIET);
251 if (write_cache(fd, active_cache, active_nr) ||
252 close_lock_file(&index_lock))
builtin-mv.c
+1 -1
@@ -256,7 +256,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
256
257 for (i = 0; i < added.nr; i++) {
258 const char *path = added.items[i].path;
259 - add_file_to_cache(path, verbose);
259 + add_file_to_cache(path, verbose ? ADD_CACHE_VERBOSE : 0);
260 }
261
262 for (i = 0; i < deleted.nr; i++)
cache.h
+7 -5
@@ -261,8 +261,8 @@ static inline void remove_name_hash(struct cache_entry *ce)
261 #define add_cache_entry(ce, option) add_index_entry(&the_index, (ce), (option))
262 #define remove_cache_entry_at(pos) remove_index_entry_at(&the_index, (pos))
263 #define remove_file_from_cache(path) remove_file_from_index(&the_index, (path))
264 -#define add_to_cache(path, st, verbose) add_to_index(&the_index, (path), (st), (verbose))
265 -#define add_file_to_cache(path, verbose) add_file_to_index(&the_index, (path), (verbose))
264 +#define add_to_cache(path, st, flags) add_to_index(&the_index, (path), (st), (flags))
265 +#define add_file_to_cache(path, flags) add_file_to_index(&the_index, (path), (flags))
266 #define refresh_cache(flags) refresh_index(&the_index, (flags), NULL, NULL)
267 #define ce_match_stat(ce, st, options) ie_match_stat(&the_index, (ce), (st), (options))
268 #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
366 extern struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really);
367 extern int remove_index_entry_at(struct index_state *, int pos);
368 extern int remove_file_from_index(struct index_state *, const char *path);
369 -extern int add_to_index(struct index_state *, const char *path, struct stat *, int verbose);
370 -extern int add_file_to_index(struct index_state *, const char *path, int verbose);
369 +#define ADD_CACHE_VERBOSE 1
370 +#define ADD_CACHE_PRETEND 2
371 +extern int add_to_index(struct index_state *, const char *path, struct stat *, int flags);
372 +extern int add_file_to_index(struct index_state *, const char *path, int flags);
373 extern struct cache_entry *make_cache_entry(unsigned int mode, const unsigned char *sha1, const char *path, int stage, int refresh);
374 extern int ce_same_name(struct cache_entry *a, struct cache_entry *b);
375
@@ -782,7 +784,7 @@ extern int convert_to_git(const char *path, const char *src, size_t len,
784 extern int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst);
785
786 /* add */
785 -void add_files_to_cache(int verbose, const char *prefix, const char **pathspec);
787 +void add_files_to_cache(const char *prefix, const char **pathspec, int flags);
788
789 /* diff.c */
790 extern int diff_auto_refresh_index;
read-cache.c
+17 -6
@@ -462,12 +462,14 @@ static struct cache_entry *create_alias_ce(struct cache_entry *ce, struct cache_
462 return new;
463 }
464
465 -int add_to_index(struct index_state *istate, const char *path, struct stat *st, int verbose)
465 +int add_to_index(struct index_state *istate, const char *path, struct stat *st, int flags)
466 {
467 - int size, namelen;
467 + int size, namelen, was_same;
468 mode_t st_mode = st->st_mode;
469 struct cache_entry *ce, *alias;
470 unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_RACY_IS_DIRTY;
471 + int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND);
472 + int pretend = flags & ADD_CACHE_PRETEND;
473
474 if (!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode))
475 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,
511 if (ignore_case && alias && different_name(ce, alias))
512 ce = create_alias_ce(ce, alias);
513 ce->ce_flags |= CE_ADDED;
512 - if (add_index_entry(istate, ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE))
514 +
515 + /* It was suspected to be recily clean, but it turns out to be Ok */
516 + was_same = (alias &&
517 + !ce_stage(alias) &&
518 + !hashcmp(alias->sha1, ce->sha1) &&
519 + ce->ce_mode == alias->ce_mode);
520 +
521 + if (pretend)
522 + ;
523 + else if (add_index_entry(istate, ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE))
524 die("unable to add %s to index",path);
514 - if (verbose)
525 + if (verbose && !was_same)
526 printf("add '%s'\n", path);
527 return 0;
528 }
529
519 -int add_file_to_index(struct index_state *istate, const char *path, int verbose)
530 +int add_file_to_index(struct index_state *istate, const char *path, int flags)
531 {
532 struct stat st;
533 if (lstat(path, &st))
534 die("%s: unable to stat (%s)", path, strerror(errno));
524 - return add_to_index(istate, path, &st, verbose);
535 + return add_to_index(istate, path, &st, flags);
536 }
537
538 struct cache_entry *make_cache_entry(unsigned int mode,
t/t2200-add-update.sh
+17
@@ -111,4 +111,21 @@ test_expect_success 'touch and then add explicitly' '
111
112 '
113
114 +test_expect_success 'add -n -u should not add but just report' '
115 +
116 + (
117 + echo "add '\''check'\''" &&
118 + echo "remove '\''top'\''"
119 + ) >expect &&
120 + before=$(git ls-files -s check top) &&
121 + echo changed >>check &&
122 + rm -f top &&
123 + git add -n -u >actual &&
124 + after=$(git ls-files -s check top) &&
125 +
126 + test "$before" = "$after" &&
127 + test_cmp expect actual
128 +
129 +'
130 +
131 test_done