From 960b8ad1b1824b1b82c2b09a000c2119f97633a0 Mon Sep 17 00:00:00 2001 From: Alex Riesen Date: Mon, 12 May 2008 19:57:45 +0200 Subject: Make the exit code of add_file_to_index actually useful Update the programs which used the function (as add_file_to_cache). Signed-off-by: Alex Riesen Signed-off-by: Junio C Hamano --- builtin-add.c | 6 ++++-- builtin-commit.c | 7 ++++--- builtin-mv.c | 3 ++- read-cache.c | 6 +++--- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/builtin-add.c b/builtin-add.c index 4a91e3eb1..4d72ab678 100644 --- a/builtin-add.c +++ b/builtin-add.c @@ -94,7 +94,8 @@ 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); + if (add_file_to_cache(path, verbose)) + die("updating files failed"); break; case DIFF_STATUS_DELETED: remove_file_from_cache(path); @@ -254,7 +255,8 @@ 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); + if (add_file_to_cache(dir.entries[i]->name, verbose)) + die("adding files failed"); finish: if (active_cache_changed) { diff --git a/builtin-commit.c b/builtin-commit.c index a65c2b8c3..ae29d35d7 100644 --- a/builtin-commit.c +++ b/builtin-commit.c @@ -178,9 +178,10 @@ static void add_remove_files(struct path_list *list) struct stat st; struct path_list_item *p = &(list->items[i]); - if (!lstat(p->path, &st)) - add_to_cache(p->path, &st, 0); - else + if (!lstat(p->path, &st)) { + if (add_to_cache(p->path, &st, 0)) + die("updating files failed"); + } else remove_file_from_cache(p->path); } } diff --git a/builtin-mv.c b/builtin-mv.c index 94f6dd2aa..fb8ffb41a 100644 --- a/builtin-mv.c +++ b/builtin-mv.c @@ -256,7 +256,8 @@ 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); + if (add_file_to_cache(path, verbose)) + die("updating index entries failed"); } for (i = 0; i < deleted.nr; i++) diff --git a/read-cache.c b/read-cache.c index 0382804e7..8b467f8f4 100644 --- a/read-cache.c +++ b/read-cache.c @@ -470,7 +470,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st, unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_RACY_IS_DIRTY; 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,12 +505,12 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st, 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)) - die("unable to add %s to index",path); + return error("unable to add %s to index",path); if (verbose) printf("add '%s'\n", path); return 0; -- cgit v1.2.1 From 7ae02a30e817eda16ea362c6304b6ae28c3a7644 Mon Sep 17 00:00:00 2001 From: Alex Riesen Date: Mon, 12 May 2008 19:58:10 +0200 Subject: Extend interface of add_files_to_cache to allow ignore indexing errors Signed-off-by: Alex Riesen Signed-off-by: Junio C Hamano --- builtin-add.c | 37 ++++++++++++++++++++++++++++--------- builtin-checkout.c | 2 +- builtin-commit.c | 2 +- cache.h | 8 +++++++- 4 files changed, 37 insertions(+), 12 deletions(-) diff --git a/builtin-add.c b/builtin-add.c index 4d72ab678..786280818 100644 --- a/builtin-add.c +++ b/builtin-add.c @@ -79,12 +79,18 @@ static void fill_directory(struct dir_struct *dir, const char **pathspec, 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, verbose; + int i; + struct update_callback_data *data = cbdata; - verbose = *((int *)cbdata); for (i = 0; i < q->nr; i++) { struct diff_filepair *p = q->queue[i]; const char *path = p->one->path; @@ -94,28 +100,35 @@ static void update_callback(struct diff_queue_struct *q, case DIFF_STATUS_UNMERGED: case DIFF_STATUS_MODIFIED: case DIFF_STATUS_TYPE_CHANGED: - if (add_file_to_cache(path, verbose)) - die("updating files failed"); + if (add_file_to_cache(path, data->flags & ADD_FILES_VERBOSE)) { + if (!(data->flags & ADD_FILES_IGNORE_ERRORS)) + die("updating files failed"); + data->add_errors++; + } break; case DIFF_STATUS_DELETED: remove_file_from_cache(path); - if (verbose) + if (data->flags & ADD_FILES_VERBOSE) printf("remove '%s'\n", path); break; } } } -void add_files_to_cache(int verbose, const char *prefix, const char **pathspec) +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 = &verbose; + 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) @@ -193,6 +206,7 @@ static struct option builtin_add_options[] = { 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; @@ -209,11 +223,16 @@ int cmd_add(int argc, const char **argv, const char *prefix) newfd = hold_locked_index(&lock_file, 1); if (take_worktree_changes) { + int flags = 0; const char **pathspec; if (read_cache() < 0) die("index file corrupt"); pathspec = get_pathspec(prefix, argv); - add_files_to_cache(verbose, prefix, pathspec); + + if (verbose) + flags |= ADD_FILES_VERBOSE; + + exit_status = add_files_to_cache(prefix, pathspec, flags); goto finish; } @@ -265,5 +284,5 @@ int cmd_add(int argc, const char **argv, const char *prefix) die("Unable to write new index file"); } - return 0; + return exit_status; } diff --git a/builtin-checkout.c b/builtin-checkout.c index 10ec137cc..05c06421b 100644 --- a/builtin-checkout.c +++ b/builtin-checkout.c @@ -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); diff --git a/builtin-commit.c b/builtin-commit.c index ae29d35d7..6a2f5c3d9 100644 --- a/builtin-commit.c +++ b/builtin-commit.c @@ -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)) diff --git a/cache.h b/cache.h index 9cee9a5f3..4fb629000 100644 --- a/cache.h +++ b/cache.h @@ -781,7 +781,13 @@ 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); +#define ADD_FILES_VERBOSE 01 +#define ADD_FILES_IGNORE_ERRORS 02 +/* + * 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; -- cgit v1.2.1 From 984b83ef23fdcf6a933f635f182e7bc10130094a Mon Sep 17 00:00:00 2001 From: Alex Riesen Date: Mon, 12 May 2008 19:58:29 +0200 Subject: Add --ignore-errors to git-add to allow it to skip files with read errors Signed-off-by: Alex Riesen Signed-off-by: Junio C Hamano --- Documentation/git-add.txt | 7 ++++++- builtin-add.c | 11 +++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Documentation/git-add.txt b/Documentation/git-add.txt index e0e730b6c..bb4abe26b 100644 --- a/Documentation/git-add.txt +++ b/Documentation/git-add.txt @@ -9,7 +9,7 @@ SYNOPSIS -------- [verse] 'git-add' [-n] [-v] [-f] [--interactive | -i] [--patch | -p] [-u] [--refresh] - [--] ... + [--ignore-errors] [--] ... DESCRIPTION ----------- @@ -83,6 +83,11 @@ OPTIONS Don't add the file(s), but only refresh their stat() information in the index. +\--ignore-errors:: + If some files could not be added because of errors indexing + them, do not abort the operation, but continue adding the + others. The command shall still exit with non-zero status. + \--:: This option can be used to separate command-line options from the list of files, (useful when filenames might be mistaken diff --git a/builtin-add.c b/builtin-add.c index 786280818..522519ec8 100644 --- a/builtin-add.c +++ b/builtin-add.c @@ -191,6 +191,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), @@ -201,6 +202,7 @@ static struct option builtin_add_options[] = { 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(), }; @@ -231,6 +233,8 @@ int cmd_add(int argc, const char **argv, const char *prefix) if (verbose) flags |= ADD_FILES_VERBOSE; + if (ignore_add_errors) + flags |= ADD_FILES_IGNORE_ERRORS; exit_status = add_files_to_cache(prefix, pathspec, flags); goto finish; @@ -274,8 +278,11 @@ int cmd_add(int argc, const char **argv, const char *prefix) } for (i = 0; i < dir.nr; i++) - if (add_file_to_cache(dir.entries[i]->name, verbose)) - die("adding files failed"); + if (add_file_to_cache(dir.entries[i]->name, verbose)) { + if (!ignore_add_errors) + die("adding files failed"); + exit_status = 1; + } finish: if (active_cache_changed) { -- cgit v1.2.1 From 89597436450d4665ea8f9f782e71d42539d9f24b Mon Sep 17 00:00:00 2001 From: Alex Riesen Date: Mon, 12 May 2008 19:58:48 +0200 Subject: Add a test for git-add --ignore-errors Signed-off-by: Alex Riesen Signed-off-by: Junio C Hamano --- t/t3700-add.sh | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/t/t3700-add.sh b/t/t3700-add.sh index 287e058e3..01e4d6251 100755 --- a/t/t3700-add.sh +++ b/t/t3700-add.sh @@ -179,4 +179,26 @@ test_expect_success 'git add --refresh' ' test -z "`git diff-index HEAD -- foo`" ' +test_expect_success 'git add should fail atomically upon an unreadable file' ' + git reset --hard && + date >foo1 && + date >foo2 && + chmod 0 foo2 && + test_must_fail git add --verbose . && + ! ( git ls-files foo1 | grep foo1 ) +' + +rm -f foo2 + +test_expect_success 'git add --ignore-errors' ' + git reset --hard && + date >foo1 && + date >foo2 && + chmod 0 foo2 && + test_must_fail git add --verbose --ignore-errors . && + git ls-files foo1 | grep foo1 +' + +rm -f foo2 + test_done -- cgit v1.2.1 From dad25e4a7c34a3ece1355f84b8c4661438754531 Mon Sep 17 00:00:00 2001 From: Alex Riesen Date: Mon, 12 May 2008 19:59:23 +0200 Subject: Add a config option to ignore errors for git-add Signed-off-by: Alex Riesen Signed-off-by: Junio C Hamano --- builtin-add.c | 11 ++++++++++- t/t3700-add.sh | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/builtin-add.c b/builtin-add.c index 522519ec8..73235ed08 100644 --- a/builtin-add.c +++ b/builtin-add.c @@ -206,6 +206,15 @@ static struct option builtin_add_options[] = { 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; @@ -220,7 +229,7 @@ int cmd_add(int argc, const char **argv, const char *prefix) 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); diff --git a/t/t3700-add.sh b/t/t3700-add.sh index 01e4d6251..5b46ba356 100755 --- a/t/t3700-add.sh +++ b/t/t3700-add.sh @@ -201,4 +201,25 @@ test_expect_success 'git add --ignore-errors' ' rm -f foo2 +test_expect_success 'git add (add.ignore-errors)' ' + git config add.ignore-errors 1 && + git reset --hard && + date >foo1 && + date >foo2 && + chmod 0 foo2 && + test_must_fail git add --verbose . && + git ls-files foo1 | grep foo1 +' +rm -f foo2 + +test_expect_success 'git add (add.ignore-errors = false)' ' + git config add.ignore-errors 0 && + git reset --hard && + date >foo1 && + date >foo2 && + chmod 0 foo2 && + test_must_fail git add --verbose . && + ! ( git ls-files foo1 | grep foo1 ) +' + test_done -- cgit v1.2.1