From: Junio C Hamano Date: Sun, 25 May 2008 21:03:50 +0000 (-0700) Subject: Merge branch 'jc/add-n-u' X-Git-Tag: v1.5.6-rc0~3 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/0166592495e21b075fa48225ff21568269bf51d4?hp=-c Merge branch 'jc/add-n-u' * jc/add-n-u: Make git add -n and git -u -n output consistent "git-add -n -u" should not add but just report Conflicts: builtin-add.c builtin-mv.c cache.h read-cache.c --- 0166592495e21b075fa48225ff21568269bf51d4 diff --combined builtin-add.c index 73235ed08a,8f81f3fbfb..6e4e645cb7 --- a/builtin-add.c +++ b/builtin-add.c @@@ -79,18 -79,12 +79,18 @@@ static void fill_directory(struct dir_s prune_directory(dir, pathspec, baselen); } +struct update_callback_data +{ + int flags; + int add_errors; +}; + static void update_callback(struct diff_queue_struct *q, struct diff_options *opt, void *cbdata) { - int i, flags; + int i; + struct update_callback_data *data = cbdata; - flags = *((int *)cbdata); for (i = 0; i < q->nr; i++) { struct diff_filepair *p = q->queue[i]; const char *path = p->one->path; @@@ -100,35 -94,28 +100,36 @@@ case DIFF_STATUS_UNMERGED: case DIFF_STATUS_MODIFIED: case DIFF_STATUS_TYPE_CHANGED: - if (add_file_to_cache(path, data->flags & ADD_FILES_VERBOSE)) { - if (!(data->flags & ADD_FILES_IGNORE_ERRORS)) - add_file_to_cache(path, flags); ++ if (add_file_to_cache(path, data->flags)) { ++ if (!(data->flags & ADD_CACHE_IGNORE_ERRORS)) + die("updating files failed"); + data->add_errors++; + } break; case DIFF_STATUS_DELETED: - remove_file_from_cache(path); - if (data->flags & ADD_FILES_VERBOSE) - if (!(flags & ADD_CACHE_PRETEND)) ++ if (!(data->flags & ADD_CACHE_PRETEND)) + remove_file_from_cache(path); - if (flags) ++ if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE)) printf("remove '%s'\n", path); break; } } } -void add_files_to_cache(const char *prefix, const char **pathspec, int flags) +int add_files_to_cache(const char *prefix, const char **pathspec, int flags) { + struct update_callback_data data; struct rev_info rev; init_revisions(&rev, prefix); setup_revisions(0, NULL, &rev, NULL); rev.prune_data = pathspec; rev.diffopt.output_format = DIFF_FORMAT_CALLBACK; rev.diffopt.format_callback = update_callback; - rev.diffopt.format_callback_data = &flags; + data.flags = flags; + data.add_errors = 0; + rev.diffopt.format_callback_data = &data; run_diff_files(&rev, DIFF_RACY_IS_MODIFIED); + return !!data.add_errors; } static void refresh(int verbose, const char **pathspec) @@@ -191,7 -178,6 +192,7 @@@ static const char ignore_error[] "The following paths are ignored by one of your .gitignore files:\n"; static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0; +static int ignore_add_errors; static struct option builtin_add_options[] = { OPT__DRY_RUN(&show_only), @@@ -202,25 -188,15 +203,26 @@@ OPT_BOOLEAN('f', NULL, &ignored_too, "allow adding otherwise ignored files"), OPT_BOOLEAN('u', NULL, &take_worktree_changes, "update tracked files"), OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"), + OPT_BOOLEAN( 0 , "ignore-errors", &ignore_add_errors, "just skip files which cannot be added because of errors"), OPT_END(), }; +static int add_config(const char *var, const char *value) +{ + if (!strcasecmp(var, "add.ignore-errors")) { + ignore_add_errors = git_config_bool(var, value); + return 0; + } + return git_default_config(var, value); +} + int cmd_add(int argc, const char **argv, const char *prefix) { + int exit_status = 0; int i, newfd; const char **pathspec; struct dir_struct dir; + int flags; argc = parse_options(argc, argv, builtin_add_options, builtin_add_usage, 0); @@@ -229,23 -205,19 +231,20 @@@ if (add_interactive) exit(interactive_add(argc, argv, prefix)); - git_config(git_default_config); + git_config(add_config); newfd = hold_locked_index(&lock_file, 1); + flags = ((verbose ? ADD_CACHE_VERBOSE : 0) | - (show_only ? ADD_CACHE_PRETEND : 0)); ++ (show_only ? ADD_CACHE_PRETEND : 0) | ++ (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0)); + if (take_worktree_changes) { - int flags = 0; const char **pathspec; if (read_cache() < 0) die("index file corrupt"); pathspec = get_pathspec(prefix, argv); - - if (verbose) - flags |= ADD_FILES_VERBOSE; - if (ignore_add_errors) - flags |= ADD_FILES_IGNORE_ERRORS; - - add_files_to_cache(prefix, pathspec, flags); + exit_status = add_files_to_cache(prefix, pathspec, flags); goto finish; } @@@ -263,17 -235,6 +262,6 @@@ fill_directory(&dir, pathspec, ignored_too); - if (show_only) { - const char *sep = "", *eof = ""; - for (i = 0; i < dir.nr; i++) { - printf("%s%s", sep, dir.entries[i]->name); - sep = " "; - eof = "\n"; - } - fputs(eof, stdout); - return 0; - } - if (read_cache() < 0) die("index file corrupt"); @@@ -287,11 -248,7 +275,11 @@@ } for (i = 0; i < dir.nr; i++) - if (add_file_to_cache(dir.entries[i]->name, verbose)) { - add_file_to_cache(dir.entries[i]->name, flags); ++ if (add_file_to_cache(dir.entries[i]->name, flags)) { + if (!ignore_add_errors) + die("adding files failed"); + exit_status = 1; + } finish: if (active_cache_changed) { @@@ -300,5 -257,5 +288,5 @@@ die("Unable to write new index file"); } - return 0; + return exit_status; } diff --combined builtin-mv.c index fb8ffb41aa,df9ea97744..fb906b3fc7 --- a/builtin-mv.c +++ b/builtin-mv.c @@@ -256,8 -256,7 +256,8 @@@ int cmd_mv(int argc, const char **argv for (i = 0; i < added.nr; i++) { const char *path = added.items[i].path; - if (add_file_to_cache(path, verbose)) - add_file_to_cache(path, verbose ? ADD_CACHE_VERBOSE : 0); ++ if (add_file_to_cache(path, verbose ? ADD_CACHE_VERBOSE : 0)) + die("updating index entries failed"); } for (i = 0; i < deleted.nr; i++) diff --combined cache.h index 3d4e8e77d8,b1a8427b91..943bee9e7d --- a/cache.h +++ b/cache.h @@@ -261,8 -261,8 +261,8 @@@ static inline void remove_name_hash(str #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)) @@@ -317,7 -317,6 +317,7 @@@ extern char *get_graft_file(void) extern int set_git_dir(const char *path); extern const char *get_git_work_tree(void); extern const char *read_gitfile_gently(const char *path); +extern void set_git_work_tree(const char *tree); #define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES" @@@ -330,10 -329,6 +330,10 @@@ extern const char *prefix_filename(cons extern void verify_filename(const char *prefix, const char *name); extern void verify_non_filename(const char *prefix, const char *name); +#define INIT_DB_QUIET 0x0001 + +extern int init_db(const char *template_dir, unsigned int flags); + #define alloc_nr(x) (((x)+16)*3/2) /* @@@ -371,8 -366,10 +371,11 @@@ extern int add_index_entry(struct index 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 ++#define ADD_CACHE_IGNORE_ERRORS 4 + 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); @@@ -393,7 -390,6 +396,7 @@@ extern void fill_stat_cache_info(struc #define REFRESH_UNMERGED 0x0002 /* allow unmerged */ #define REFRESH_QUIET 0x0004 /* be quiet about it */ #define REFRESH_IGNORE_MISSING 0x0008 /* ignore non-existent */ +#define REFRESH_IGNORE_SUBMODULES 0x0008 /* ignore submodules */ extern int refresh_index(struct index_state *, unsigned int flags, const char **pathspec, char *seen); struct lock_file { @@@ -404,7 -400,6 +407,7 @@@ char filename[PATH_MAX]; }; extern int hold_lock_file_for_update(struct lock_file *, const char *path, int); +extern int hold_lock_file_for_append(struct lock_file *, const char *path, int); extern int commit_lock_file(struct lock_file *); extern int hold_locked_index(struct lock_file *, int); @@@ -528,7 -523,6 +531,7 @@@ extern void * read_sha1_file(const unsi extern int hash_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *sha1); extern int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *return_sha1); extern int pretend_sha1_file(void *, unsigned long, enum object_type, unsigned char *); +extern int force_object_loose(const unsigned char *sha1, time_t mtime); extern int check_sha1_signature(const unsigned char *sha1, void *buf, unsigned long size, const char *type); @@@ -622,7 -616,6 +625,7 @@@ extern struct alternate_object_databas char base[FLEX_ARRAY]; /* more */ } *alt_odb_list; extern void prepare_alt_odb(void); +extern void add_to_alternates_file(const char *reference); struct pack_window { struct pack_window *next; @@@ -791,13 -784,7 +794,11 @@@ extern int convert_to_git(const char *p extern int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst); /* add */ - #define ADD_FILES_VERBOSE 01 - #define ADD_FILES_IGNORE_ERRORS 02 -void add_files_to_cache(const char *prefix, const char **pathspec, int flags); +/* + * return 0 if success, 1 - if addition of a file failed and + * ADD_FILES_IGNORE_ERRORS was specified in flags + */ +int add_files_to_cache(const char *prefix, const char **pathspec, int flags); /* diff.c */ extern int diff_auto_refresh_index; diff --combined read-cache.c index bc039819ee,5d967e8ec9..ac9a8e7e32 --- a/read-cache.c +++ b/read-cache.c @@@ -462,15 -462,17 +462,17 @@@ static struct cache_entry *create_alias 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); + return error("%s: can only add regular files, symbolic links or git-directories", path); namelen = strlen(path); if (S_ISDIR(st_mode)) { @@@ -505,23 -507,32 +507,32 @@@ return 0; } if (index_path(ce->sha1, path, st, 1)) - die("unable to index file %s", path); + return error("unable to index file %s", path); 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); + return error("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, @@@ -942,7 -953,6 +953,7 @@@ int refresh_index(struct index_state *i int allow_unmerged = (flags & REFRESH_UNMERGED) != 0; int quiet = (flags & REFRESH_QUIET) != 0; int not_new = (flags & REFRESH_IGNORE_MISSING) != 0; + int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) != 0; unsigned int options = really ? CE_MATCH_IGNORE_VALID : 0; for (i = 0; i < istate->cache_nr; i++) { @@@ -950,9 -960,6 +961,9 @@@ int cache_errno = 0; ce = istate->cache[i]; + if (ignore_submodules && S_ISGITLINK(ce->ce_mode)) + continue; + if (ce_stage(ce)) { while ((i < istate->cache_nr) && ! strcmp(istate->cache[i]->name, ce->name))