Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * "git add" builtin command |
| 3 | * |
| 4 | * Copyright (C) 2006 Linus Torvalds |
| 5 | */ |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 6 | #include "cache.h" |
| 7 | #include "builtin.h" |
| 8 | #include "dir.h" |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 9 | #include "pathspec.h" |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 10 | #include "exec_cmd.h" |
Junio C Hamano | 93872e0 | 2006-05-20 01:28:49 -0700 | [diff] [blame] | 11 | #include "cache-tree.h" |
Kristian Høgsberg | 5868016 | 2007-09-17 20:06:44 -0400 | [diff] [blame] | 12 | #include "run-command.h" |
Kristian Høgsberg | 5c46f75 | 2007-10-03 17:45:02 -0400 | [diff] [blame] | 13 | #include "parse-options.h" |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 14 | #include "diff.h" |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 15 | #include "diffcore.h" |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 16 | #include "revision.h" |
Junio C Hamano | 568508e | 2011-10-28 14:48:40 -0700 | [diff] [blame] | 17 | #include "bulk-checkin.h" |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 18 | |
Kristian Høgsberg | 5c46f75 | 2007-10-03 17:45:02 -0400 | [diff] [blame] | 19 | static const char * const builtin_add_usage[] = { |
Matthieu Moy | d32805d | 2013-02-12 10:24:44 +0100 | [diff] [blame] | 20 | N_("git add [options] [--] <pathspec>..."), |
Kristian Høgsberg | 5c46f75 | 2007-10-03 17:45:02 -0400 | [diff] [blame] | 21 | NULL |
| 22 | }; |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 23 | static int patch_interactive, add_interactive, edit_interactive; |
Jeff King | 93c44d4 | 2007-05-12 02:42:00 -0400 | [diff] [blame] | 24 | static int take_worktree_changes; |
James Bowes | 896bdfa | 2007-02-27 22:31:10 -0500 | [diff] [blame] | 25 | |
Jonathan Nieder | 9cba13c | 2011-03-16 02:08:34 -0500 | [diff] [blame] | 26 | struct update_callback_data { |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 27 | int flags; |
| 28 | int add_errors; |
Jonathan Nieder | 71c7b05 | 2013-03-19 15:50:50 -0700 | [diff] [blame] | 29 | const char *implicit_dot; |
| 30 | size_t implicit_dot_len; |
Junio C Hamano | d226b14 | 2013-04-17 12:32:21 -0700 | [diff] [blame] | 31 | |
| 32 | /* only needed for 2.0 transition preparation */ |
| 33 | int warn_add_would_remove; |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 34 | }; |
| 35 | |
Jonathan Nieder | e24afab | 2013-03-19 15:45:26 -0700 | [diff] [blame] | 36 | static const char *option_with_implicit_dot; |
| 37 | static const char *short_option_with_implicit_dot; |
| 38 | |
| 39 | static void warn_pathless_add(void) |
| 40 | { |
Jonathan Nieder | 16d41d4 | 2013-03-19 15:45:52 -0700 | [diff] [blame] | 41 | static int shown; |
Jonathan Nieder | e24afab | 2013-03-19 15:45:26 -0700 | [diff] [blame] | 42 | assert(option_with_implicit_dot && short_option_with_implicit_dot); |
| 43 | |
Jonathan Nieder | 16d41d4 | 2013-03-19 15:45:52 -0700 | [diff] [blame] | 44 | if (shown) |
| 45 | return; |
| 46 | shown = 1; |
| 47 | |
Jonathan Nieder | e24afab | 2013-03-19 15:45:26 -0700 | [diff] [blame] | 48 | /* |
| 49 | * To be consistent with "git add -p" and most Git |
| 50 | * commands, we should default to being tree-wide, but |
| 51 | * this is not the original behavior and can't be |
| 52 | * changed until users trained themselves not to type |
| 53 | * "git add -u" or "git add -A". For now, we warn and |
| 54 | * keep the old behavior. Later, the behavior can be changed |
| 55 | * to tree-wide, keeping the warning for a while, and |
| 56 | * eventually we can drop the warning. |
| 57 | */ |
| 58 | warning(_("The behavior of 'git add %s (or %s)' with no path argument from a\n" |
| 59 | "subdirectory of the tree will change in Git 2.0 and should not be used anymore.\n" |
| 60 | "To add content for the whole tree, run:\n" |
| 61 | "\n" |
| 62 | " git add %s :/\n" |
| 63 | " (or git add %s :/)\n" |
| 64 | "\n" |
| 65 | "To restrict the command to the current directory, run:\n" |
| 66 | "\n" |
| 67 | " git add %s .\n" |
| 68 | " (or git add %s .)\n" |
| 69 | "\n" |
Matthieu Moy | c9f35b8 | 2013-03-11 09:01:33 +0100 | [diff] [blame] | 70 | "With the current Git version, the command is restricted to " |
| 71 | "the current directory.\n" |
| 72 | ""), |
Jonathan Nieder | e24afab | 2013-03-19 15:45:26 -0700 | [diff] [blame] | 73 | option_with_implicit_dot, short_option_with_implicit_dot, |
| 74 | option_with_implicit_dot, short_option_with_implicit_dot, |
| 75 | option_with_implicit_dot, short_option_with_implicit_dot); |
| 76 | } |
| 77 | |
Junio C Hamano | 75973b2 | 2011-04-20 18:11:19 -0700 | [diff] [blame] | 78 | static int fix_unmerged_status(struct diff_filepair *p, |
| 79 | struct update_callback_data *data) |
| 80 | { |
| 81 | if (p->status != DIFF_STATUS_UNMERGED) |
| 82 | return p->status; |
| 83 | if (!(data->flags & ADD_CACHE_IGNORE_REMOVAL) && !p->two->mode) |
| 84 | /* |
| 85 | * This is not an explicit add request, and the |
| 86 | * path is missing from the working tree (deleted) |
| 87 | */ |
| 88 | return DIFF_STATUS_DELETED; |
| 89 | else |
| 90 | /* |
| 91 | * Either an explicit add request, or path exists |
| 92 | * in the working tree. An attempt to explicitly |
| 93 | * add a path that does not exist in the working tree |
| 94 | * will be caught as an error by the caller immediately. |
| 95 | */ |
| 96 | return DIFF_STATUS_MODIFIED; |
| 97 | } |
| 98 | |
Junio C Hamano | ccc663b | 2013-04-21 21:04:35 -0700 | [diff] [blame] | 99 | static const char *add_would_remove_warning = N_( |
| 100 | "You ran 'git add' with neither '-A (--all)' or '--no-all', whose\n" |
| 101 | "behaviour will change in Git 2.0 with respect to paths you removed from\n" |
| 102 | "your working tree. Paths like '%s' that are\n" |
| 103 | "removed are ignored with this version of Git.\n" |
| 104 | "\n" |
| 105 | "* 'git add --no-all <pathspec>', which is the current default, ignores\n" |
| 106 | " paths you removed from your working tree.\n" |
| 107 | "\n" |
| 108 | "* 'git add --all <pathspec>' will let you also record the removals.\n" |
| 109 | "\n" |
| 110 | "Run 'git status' to check the paths you removed from your working tree.\n"); |
| 111 | |
Junio C Hamano | d226b14 | 2013-04-17 12:32:21 -0700 | [diff] [blame] | 112 | static void warn_add_would_remove(const char *path) |
| 113 | { |
Junio C Hamano | ccc663b | 2013-04-21 21:04:35 -0700 | [diff] [blame] | 114 | warning(_(add_would_remove_warning), path); |
Junio C Hamano | d226b14 | 2013-04-17 12:32:21 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 117 | static void update_callback(struct diff_queue_struct *q, |
| 118 | struct diff_options *opt, void *cbdata) |
| 119 | { |
| 120 | int i; |
| 121 | struct update_callback_data *data = cbdata; |
Jonathan Nieder | 71c7b05 | 2013-03-19 15:50:50 -0700 | [diff] [blame] | 122 | const char *implicit_dot = data->implicit_dot; |
| 123 | size_t implicit_dot_len = data->implicit_dot_len; |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 124 | |
| 125 | for (i = 0; i < q->nr; i++) { |
| 126 | struct diff_filepair *p = q->queue[i]; |
| 127 | const char *path = p->one->path; |
Jonathan Nieder | 71c7b05 | 2013-03-19 15:50:50 -0700 | [diff] [blame] | 128 | /* |
| 129 | * Check if "git add -A" or "git add -u" was run from a |
| 130 | * subdirectory with a modified file outside that directory, |
| 131 | * and warn if so. |
| 132 | * |
| 133 | * "git add -u" will behave like "git add -u :/" instead of |
| 134 | * "git add -u ." in the future. This warning prepares for |
| 135 | * that change. |
| 136 | */ |
| 137 | if (implicit_dot && |
| 138 | strncmp_icase(path, implicit_dot, implicit_dot_len)) { |
| 139 | warn_pathless_add(); |
| 140 | continue; |
| 141 | } |
Junio C Hamano | 75973b2 | 2011-04-20 18:11:19 -0700 | [diff] [blame] | 142 | switch (fix_unmerged_status(p, data)) { |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 143 | default: |
Ævar Arnfjörð Bjarmason | 990ac4b | 2011-02-22 23:41:29 +0000 | [diff] [blame] | 144 | die(_("unexpected diff status %c"), p->status); |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 145 | case DIFF_STATUS_MODIFIED: |
| 146 | case DIFF_STATUS_TYPE_CHANGED: |
| 147 | if (add_file_to_index(&the_index, path, data->flags)) { |
| 148 | if (!(data->flags & ADD_CACHE_IGNORE_ERRORS)) |
Ævar Arnfjörð Bjarmason | 990ac4b | 2011-02-22 23:41:29 +0000 | [diff] [blame] | 149 | die(_("updating files failed")); |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 150 | data->add_errors++; |
| 151 | } |
| 152 | break; |
| 153 | case DIFF_STATUS_DELETED: |
Junio C Hamano | d226b14 | 2013-04-17 12:32:21 -0700 | [diff] [blame] | 154 | if (data->warn_add_would_remove) { |
| 155 | warn_add_would_remove(path); |
| 156 | data->warn_add_would_remove = 0; |
| 157 | } |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 158 | if (data->flags & ADD_CACHE_IGNORE_REMOVAL) |
| 159 | break; |
| 160 | if (!(data->flags & ADD_CACHE_PRETEND)) |
| 161 | remove_file_from_index(&the_index, path); |
| 162 | if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE)) |
Ævar Arnfjörð Bjarmason | 475c73e | 2011-02-22 23:41:32 +0000 | [diff] [blame] | 163 | printf(_("remove '%s'\n"), path); |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 164 | break; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
Junio C Hamano | d226b14 | 2013-04-17 12:32:21 -0700 | [diff] [blame] | 169 | static void update_files_in_cache(const char *prefix, const char **pathspec, |
| 170 | struct update_callback_data *data) |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 171 | { |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 172 | struct rev_info rev; |
Jonathan Nieder | 71c7b05 | 2013-03-19 15:50:50 -0700 | [diff] [blame] | 173 | |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 174 | init_revisions(&rev, prefix); |
| 175 | setup_revisions(0, NULL, &rev, NULL); |
Nguyễn Thái Ngọc Duy | afe069d | 2010-12-17 19:43:06 +0700 | [diff] [blame] | 176 | init_pathspec(&rev.prune_data, pathspec); |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 177 | rev.diffopt.output_format = DIFF_FORMAT_CALLBACK; |
| 178 | rev.diffopt.format_callback = update_callback; |
Junio C Hamano | d226b14 | 2013-04-17 12:32:21 -0700 | [diff] [blame] | 179 | rev.diffopt.format_callback_data = data; |
Junio C Hamano | 75973b2 | 2011-04-20 18:11:19 -0700 | [diff] [blame] | 180 | rev.max_count = 0; /* do not compare unmerged paths with stage #2 */ |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 181 | run_diff_files(&rev, DIFF_RACY_IS_MODIFIED); |
Junio C Hamano | d226b14 | 2013-04-17 12:32:21 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | int add_files_to_cache(const char *prefix, const char **pathspec, int flags) |
| 185 | { |
| 186 | struct update_callback_data data; |
| 187 | |
| 188 | memset(&data, 0, sizeof(data)); |
| 189 | data.flags = flags; |
| 190 | update_files_in_cache(prefix, pathspec, &data); |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 191 | return !!data.add_errors; |
| 192 | } |
| 193 | |
Jonathan Nieder | 9df84e9 | 2013-03-19 15:51:34 -0700 | [diff] [blame] | 194 | #define WARN_IMPLICIT_DOT (1u << 0) |
| 195 | static char *prune_directory(struct dir_struct *dir, const char **pathspec, |
| 196 | int prefix, unsigned flag) |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 197 | { |
Linus Torvalds | f259339 | 2006-05-17 13:23:19 -0700 | [diff] [blame] | 198 | char *seen; |
| 199 | int i, specs; |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 200 | struct dir_entry **src, **dst; |
| 201 | |
Linus Torvalds | f259339 | 2006-05-17 13:23:19 -0700 | [diff] [blame] | 202 | for (specs = 0; pathspec[specs]; specs++) |
| 203 | /* nothing */; |
Peter Eriksen | 28f7581 | 2006-07-25 09:30:18 +0200 | [diff] [blame] | 204 | seen = xcalloc(specs, 1); |
Linus Torvalds | f259339 | 2006-05-17 13:23:19 -0700 | [diff] [blame] | 205 | |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 206 | src = dst = dir->entries; |
| 207 | i = dir->nr; |
| 208 | while (--i >= 0) { |
| 209 | struct dir_entry *entry = *src++; |
Junio C Hamano | 4d06f8a | 2006-12-29 11:01:31 -0800 | [diff] [blame] | 210 | if (match_pathspec(pathspec, entry->name, entry->len, |
| 211 | prefix, seen)) |
| 212 | *dst++ = entry; |
Jonathan Nieder | 9df84e9 | 2013-03-19 15:51:34 -0700 | [diff] [blame] | 213 | else if (flag & WARN_IMPLICIT_DOT) |
| 214 | /* |
| 215 | * "git add -A" was run from a subdirectory with a |
| 216 | * new file outside that directory. |
| 217 | * |
| 218 | * "git add -A" will behave like "git add -A :/" |
| 219 | * instead of "git add -A ." in the future. |
| 220 | * Warn about the coming behavior change. |
| 221 | */ |
| 222 | warn_pathless_add(); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 223 | } |
| 224 | dir->nr = dst - dir->entries; |
Adam Spiers | 4b78d7b | 2013-01-06 16:58:09 +0000 | [diff] [blame] | 225 | add_pathspec_matches_against_index(pathspec, seen, specs); |
Junio C Hamano | 81f45e7 | 2010-02-09 17:30:49 -0500 | [diff] [blame] | 226 | return seen; |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Adam Spiers | 9d67b61 | 2013-01-06 16:58:10 +0000 | [diff] [blame] | 229 | /* |
| 230 | * Checks the index to see whether any path in pathspec refers to |
| 231 | * something inside a submodule. If so, dies with an error message. |
| 232 | */ |
Johannes Schindelin | 2ce53f9 | 2009-01-02 19:08:40 +0100 | [diff] [blame] | 233 | static void treat_gitlinks(const char **pathspec) |
| 234 | { |
| 235 | int i; |
| 236 | |
| 237 | if (!pathspec || !*pathspec) |
| 238 | return; |
| 239 | |
Adam Spiers | 9d67b61 | 2013-01-06 16:58:10 +0000 | [diff] [blame] | 240 | for (i = 0; pathspec[i]; i++) |
| 241 | pathspec[i] = check_path_for_gitlink(pathspec[i]); |
Johannes Schindelin | 2ce53f9 | 2009-01-02 19:08:40 +0100 | [diff] [blame] | 242 | } |
| 243 | |
Alexandre Julliard | d616813 | 2007-08-11 23:59:01 +0200 | [diff] [blame] | 244 | static void refresh(int verbose, const char **pathspec) |
| 245 | { |
| 246 | char *seen; |
| 247 | int i, specs; |
| 248 | |
| 249 | for (specs = 0; pathspec[specs]; specs++) |
| 250 | /* nothing */; |
| 251 | seen = xcalloc(specs, 1); |
Matthieu Moy | 43673fd | 2009-08-21 10:57:58 +0200 | [diff] [blame] | 252 | refresh_index(&the_index, verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET, |
Ævar Arnfjörð Bjarmason | ed2a808 | 2011-02-22 23:41:33 +0000 | [diff] [blame] | 253 | pathspec, seen, _("Unstaged changes after refreshing the index:")); |
Alexandre Julliard | d616813 | 2007-08-11 23:59:01 +0200 | [diff] [blame] | 254 | for (i = 0; i < specs; i++) { |
| 255 | if (!seen[i]) |
Ævar Arnfjörð Bjarmason | 990ac4b | 2011-02-22 23:41:29 +0000 | [diff] [blame] | 256 | die(_("pathspec '%s' did not match any files"), pathspec[i]); |
Alexandre Julliard | d616813 | 2007-08-11 23:59:01 +0200 | [diff] [blame] | 257 | } |
Benoit Sigoure | 399f0a8 | 2007-10-29 08:00:33 +0100 | [diff] [blame] | 258 | free(seen); |
Alexandre Julliard | d616813 | 2007-08-11 23:59:01 +0200 | [diff] [blame] | 259 | } |
| 260 | |
Adam Spiers | 512aaf9 | 2013-01-06 16:58:11 +0000 | [diff] [blame] | 261 | /* |
| 262 | * Normalizes argv relative to prefix, via get_pathspec(), and then |
| 263 | * runs die_if_path_beyond_symlink() on each path in the normalized |
| 264 | * list. |
| 265 | */ |
Adam Spiers | f8a1113 | 2013-01-06 16:58:07 +0000 | [diff] [blame] | 266 | static const char **validate_pathspec(const char **argv, const char *prefix) |
Junio C Hamano | 3f06188 | 2007-11-25 10:10:10 -0800 | [diff] [blame] | 267 | { |
| 268 | const char **pathspec = get_pathspec(prefix, argv); |
| 269 | |
Junio C Hamano | 725b060 | 2008-08-04 00:52:37 -0700 | [diff] [blame] | 270 | if (pathspec) { |
| 271 | const char **p; |
| 272 | for (p = pathspec; *p; p++) { |
Adam Spiers | 512aaf9 | 2013-01-06 16:58:11 +0000 | [diff] [blame] | 273 | die_if_path_beyond_symlink(*p, prefix); |
Junio C Hamano | 725b060 | 2008-08-04 00:52:37 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
Junio C Hamano | 3f06188 | 2007-11-25 10:10:10 -0800 | [diff] [blame] | 277 | return pathspec; |
| 278 | } |
| 279 | |
Thomas Rast | 46b5139 | 2009-08-13 14:29:41 +0200 | [diff] [blame] | 280 | int run_add_interactive(const char *revision, const char *patch_mode, |
| 281 | const char **pathspec) |
| 282 | { |
| 283 | int status, ac, pc = 0; |
| 284 | const char **args; |
| 285 | |
| 286 | if (pathspec) |
| 287 | while (pathspec[pc]) |
| 288 | pc++; |
| 289 | |
| 290 | args = xcalloc(sizeof(const char *), (pc + 5)); |
| 291 | ac = 0; |
| 292 | args[ac++] = "add--interactive"; |
| 293 | if (patch_mode) |
| 294 | args[ac++] = patch_mode; |
| 295 | if (revision) |
| 296 | args[ac++] = revision; |
| 297 | args[ac++] = "--"; |
| 298 | if (pc) { |
| 299 | memcpy(&(args[ac]), pathspec, sizeof(const char *) * pc); |
| 300 | ac += pc; |
| 301 | } |
| 302 | args[ac] = NULL; |
| 303 | |
| 304 | status = run_command_v_opt(args, RUN_GIT_CMD); |
| 305 | free(args); |
| 306 | return status; |
| 307 | } |
| 308 | |
Conrad Irwin | b4bd466 | 2011-05-07 10:58:07 -0700 | [diff] [blame] | 309 | int interactive_add(int argc, const char **argv, const char *prefix, int patch) |
Kristian Høgsberg | 5868016 | 2007-09-17 20:06:44 -0400 | [diff] [blame] | 310 | { |
Junio C Hamano | 3f06188 | 2007-11-25 10:10:10 -0800 | [diff] [blame] | 311 | const char **pathspec = NULL; |
Junio C Hamano | 324ccbd | 2007-11-25 10:07:55 -0800 | [diff] [blame] | 312 | |
Junio C Hamano | 3f06188 | 2007-11-25 10:10:10 -0800 | [diff] [blame] | 313 | if (argc) { |
Adam Spiers | f8a1113 | 2013-01-06 16:58:07 +0000 | [diff] [blame] | 314 | pathspec = validate_pathspec(argv, prefix); |
Junio C Hamano | 3f06188 | 2007-11-25 10:10:10 -0800 | [diff] [blame] | 315 | if (!pathspec) |
| 316 | return -1; |
| 317 | } |
| 318 | |
Thomas Rast | 46b5139 | 2009-08-13 14:29:41 +0200 | [diff] [blame] | 319 | return run_add_interactive(NULL, |
Conrad Irwin | b4bd466 | 2011-05-07 10:58:07 -0700 | [diff] [blame] | 320 | patch ? "--patch" : NULL, |
Thomas Rast | 46b5139 | 2009-08-13 14:29:41 +0200 | [diff] [blame] | 321 | pathspec); |
Kristian Høgsberg | 5868016 | 2007-09-17 20:06:44 -0400 | [diff] [blame] | 322 | } |
| 323 | |
Linus Torvalds | 2af202b | 2009-06-18 10:28:43 -0700 | [diff] [blame] | 324 | static int edit_patch(int argc, const char **argv, const char *prefix) |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 325 | { |
Ramsay Jones | d292bfa | 2012-09-04 18:30:21 +0100 | [diff] [blame] | 326 | char *file = git_pathdup("ADD_EDIT.patch"); |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 327 | const char *apply_argv[] = { "apply", "--recount", "--cached", |
Gary V. Vaughan | 66dbfd5 | 2010-05-14 09:31:33 +0000 | [diff] [blame] | 328 | NULL, NULL }; |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 329 | struct child_process child; |
| 330 | struct rev_info rev; |
| 331 | int out; |
| 332 | struct stat st; |
| 333 | |
Gary V. Vaughan | 66dbfd5 | 2010-05-14 09:31:33 +0000 | [diff] [blame] | 334 | apply_argv[3] = file; |
| 335 | |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 336 | git_config(git_diff_basic_config, NULL); /* no "diff" UI options */ |
| 337 | |
| 338 | if (read_cache() < 0) |
Ævar Arnfjörð Bjarmason | 990ac4b | 2011-02-22 23:41:29 +0000 | [diff] [blame] | 339 | die (_("Could not read the index")); |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 340 | |
| 341 | init_revisions(&rev, prefix); |
| 342 | rev.diffopt.context = 7; |
| 343 | |
| 344 | argc = setup_revisions(argc, argv, &rev, NULL); |
| 345 | rev.diffopt.output_format = DIFF_FORMAT_PATCH; |
Johannes Schindelin | 701825d | 2012-02-07 05:05:48 +0100 | [diff] [blame] | 346 | DIFF_OPT_SET(&rev.diffopt, IGNORE_DIRTY_SUBMODULES); |
Jeff King | 6ff2b72 | 2012-07-10 02:37:22 -0400 | [diff] [blame] | 347 | out = open(file, O_CREAT | O_WRONLY, 0666); |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 348 | if (out < 0) |
Ævar Arnfjörð Bjarmason | 990ac4b | 2011-02-22 23:41:29 +0000 | [diff] [blame] | 349 | die (_("Could not open '%s' for writing."), file); |
Jim Meyering | 4169837 | 2009-09-12 10:43:27 +0200 | [diff] [blame] | 350 | rev.diffopt.file = xfdopen(out, "w"); |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 351 | rev.diffopt.close_file = 1; |
| 352 | if (run_diff_files(&rev, 0)) |
Ævar Arnfjörð Bjarmason | 990ac4b | 2011-02-22 23:41:29 +0000 | [diff] [blame] | 353 | die (_("Could not write patch")); |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 354 | |
| 355 | launch_editor(file, NULL, NULL); |
| 356 | |
| 357 | if (stat(file, &st)) |
Ævar Arnfjörð Bjarmason | 990ac4b | 2011-02-22 23:41:29 +0000 | [diff] [blame] | 358 | die_errno(_("Could not stat '%s'"), file); |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 359 | if (!st.st_size) |
Ævar Arnfjörð Bjarmason | 990ac4b | 2011-02-22 23:41:29 +0000 | [diff] [blame] | 360 | die(_("Empty patch. Aborted.")); |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 361 | |
| 362 | memset(&child, 0, sizeof(child)); |
| 363 | child.git_cmd = 1; |
| 364 | child.argv = apply_argv; |
| 365 | if (run_command(&child)) |
Ævar Arnfjörð Bjarmason | 990ac4b | 2011-02-22 23:41:29 +0000 | [diff] [blame] | 366 | die (_("Could not apply '%s'"), file); |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 367 | |
| 368 | unlink(file); |
Ramsay Jones | d292bfa | 2012-09-04 18:30:21 +0100 | [diff] [blame] | 369 | free(file); |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 370 | return 0; |
| 371 | } |
| 372 | |
Junio C Hamano | 021b6e4 | 2006-06-06 12:51:49 -0700 | [diff] [blame] | 373 | static struct lock_file lock_file; |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 374 | |
Petr Baudis | b39c53e | 2007-08-29 00:41:23 +0200 | [diff] [blame] | 375 | static const char ignore_error[] = |
Ævar Arnfjörð Bjarmason | 439fb82 | 2011-02-22 23:41:30 +0000 | [diff] [blame] | 376 | N_("The following paths are ignored by one of your .gitignore files:\n"); |
Junio C Hamano | 6a1ad32 | 2006-12-25 17:46:38 -0800 | [diff] [blame] | 377 | |
Junio C Hamano | 300c0a2 | 2013-03-08 22:21:13 -0800 | [diff] [blame] | 378 | static int verbose, show_only, ignored_too, refresh_only; |
Junio C Hamano | 45c45e3 | 2011-04-19 12:18:20 -0700 | [diff] [blame] | 379 | static int ignore_add_errors, intent_to_add, ignore_missing; |
| 380 | |
| 381 | #define ADDREMOVE_DEFAULT 0 /* Change to 1 in Git 2.0 */ |
| 382 | static int addremove = ADDREMOVE_DEFAULT; |
| 383 | static int addremove_explicit = -1; /* unspecified */ |
Kristian Høgsberg | 5c46f75 | 2007-10-03 17:45:02 -0400 | [diff] [blame] | 384 | |
| 385 | static struct option builtin_add_options[] = { |
Nguyễn Thái Ngọc Duy | 1b56024 | 2012-08-20 19:31:52 +0700 | [diff] [blame] | 386 | OPT__DRY_RUN(&show_only, N_("dry run")), |
| 387 | OPT__VERBOSE(&verbose, N_("be verbose")), |
Kristian Høgsberg | 5c46f75 | 2007-10-03 17:45:02 -0400 | [diff] [blame] | 388 | OPT_GROUP(""), |
Junio C Hamano | 300c0a2 | 2013-03-08 22:21:13 -0800 | [diff] [blame] | 389 | OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")), |
| 390 | OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")), |
| 391 | OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")), |
Nguyễn Thái Ngọc Duy | 1b56024 | 2012-08-20 19:31:52 +0700 | [diff] [blame] | 392 | OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files")), |
Junio C Hamano | 300c0a2 | 2013-03-08 22:21:13 -0800 | [diff] [blame] | 393 | OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")), |
| 394 | OPT_BOOL('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")), |
Junio C Hamano | 45c45e3 | 2011-04-19 12:18:20 -0700 | [diff] [blame] | 395 | OPT_BOOL('A', "all", &addremove_explicit, N_("add changes from all tracked and untracked files")), |
Junio C Hamano | 300c0a2 | 2013-03-08 22:21:13 -0800 | [diff] [blame] | 396 | OPT_BOOL( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")), |
| 397 | OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")), |
| 398 | OPT_BOOL( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")), |
Kristian Høgsberg | 5c46f75 | 2007-10-03 17:45:02 -0400 | [diff] [blame] | 399 | OPT_END(), |
| 400 | }; |
| 401 | |
Junio C Hamano | 9bd81e4 | 2008-05-25 14:25:02 -0700 | [diff] [blame] | 402 | static int add_config(const char *var, const char *value, void *cb) |
Alex Riesen | dad25e4 | 2008-05-12 19:59:23 +0200 | [diff] [blame] | 403 | { |
Jonathan Nieder | 8c2be75 | 2011-05-14 15:19:21 -0500 | [diff] [blame] | 404 | if (!strcmp(var, "add.ignoreerrors") || |
| 405 | !strcmp(var, "add.ignore-errors")) { |
Alex Riesen | dad25e4 | 2008-05-12 19:59:23 +0200 | [diff] [blame] | 406 | ignore_add_errors = git_config_bool(var, value); |
| 407 | return 0; |
| 408 | } |
Junio C Hamano | 9bd81e4 | 2008-05-25 14:25:02 -0700 | [diff] [blame] | 409 | return git_default_config(var, value, cb); |
Alex Riesen | dad25e4 | 2008-05-12 19:59:23 +0200 | [diff] [blame] | 410 | } |
| 411 | |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 412 | static int add_files(struct dir_struct *dir, int flags) |
| 413 | { |
| 414 | int i, exit_status = 0; |
| 415 | |
| 416 | if (dir->ignored_nr) { |
Ævar Arnfjörð Bjarmason | 439fb82 | 2011-02-22 23:41:30 +0000 | [diff] [blame] | 417 | fprintf(stderr, _(ignore_error)); |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 418 | for (i = 0; i < dir->ignored_nr; i++) |
| 419 | fprintf(stderr, "%s\n", dir->ignored[i]->name); |
Ævar Arnfjörð Bjarmason | 439fb82 | 2011-02-22 23:41:30 +0000 | [diff] [blame] | 420 | fprintf(stderr, _("Use -f if you really want to add them.\n")); |
| 421 | die(_("no files added")); |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | for (i = 0; i < dir->nr; i++) |
| 425 | if (add_file_to_cache(dir->entries[i]->name, flags)) { |
| 426 | if (!ignore_add_errors) |
Ævar Arnfjörð Bjarmason | 990ac4b | 2011-02-22 23:41:29 +0000 | [diff] [blame] | 427 | die(_("adding files failed")); |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 428 | exit_status = 1; |
| 429 | } |
| 430 | return exit_status; |
| 431 | } |
| 432 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 433 | int cmd_add(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 434 | { |
Alex Riesen | 7ae02a3 | 2008-05-12 19:58:10 +0200 | [diff] [blame] | 435 | int exit_status = 0; |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 436 | int newfd; |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 437 | const char **pathspec; |
| 438 | struct dir_struct dir; |
Gustaf Hendeby | 205ffa9 | 2008-05-22 23:59:42 +0200 | [diff] [blame] | 439 | int flags; |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 440 | int add_new_files; |
| 441 | int require_pathspec; |
Junio C Hamano | 81f45e7 | 2010-02-09 17:30:49 -0500 | [diff] [blame] | 442 | char *seen = NULL; |
Jonathan Nieder | 71c7b05 | 2013-03-19 15:50:50 -0700 | [diff] [blame] | 443 | int implicit_dot = 0; |
Junio C Hamano | d226b14 | 2013-04-17 12:32:21 -0700 | [diff] [blame] | 444 | struct update_callback_data update_data; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 445 | |
Stephen Boyd | ed342fd | 2009-06-18 02:17:54 -0700 | [diff] [blame] | 446 | git_config(add_config, NULL); |
| 447 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 448 | argc = parse_options(argc, argv, prefix, builtin_add_options, |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 449 | builtin_add_usage, PARSE_OPT_KEEP_ARGV0); |
Wincent Colaiuta | b63e995 | 2007-11-25 14:15:42 +0100 | [diff] [blame] | 450 | if (patch_interactive) |
| 451 | add_interactive = 1; |
Wincent Colaiuta | 7c0ab44 | 2007-11-22 01:02:52 +0100 | [diff] [blame] | 452 | if (add_interactive) |
Conrad Irwin | b4bd466 | 2011-05-07 10:58:07 -0700 | [diff] [blame] | 453 | exit(interactive_add(argc - 1, argv + 1, prefix, patch_interactive)); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 454 | |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 455 | if (edit_interactive) |
| 456 | return(edit_patch(argc, argv, prefix)); |
| 457 | argc--; |
| 458 | argv++; |
| 459 | |
Junio C Hamano | 45c45e3 | 2011-04-19 12:18:20 -0700 | [diff] [blame] | 460 | if (0 <= addremove_explicit) |
| 461 | addremove = addremove_explicit; |
| 462 | else if (take_worktree_changes && ADDREMOVE_DEFAULT) |
| 463 | addremove = 0; /* "-u" was given but not "-A" */ |
| 464 | |
Junio C Hamano | 3ba1f11 | 2008-07-19 19:51:11 -0700 | [diff] [blame] | 465 | if (addremove && take_worktree_changes) |
Ævar Arnfjörð Bjarmason | 990ac4b | 2011-02-22 23:41:29 +0000 | [diff] [blame] | 466 | die(_("-A and -u are mutually incompatible")); |
Junio C Hamano | 45c45e3 | 2011-04-19 12:18:20 -0700 | [diff] [blame] | 467 | |
| 468 | /* |
| 469 | * Warn when "git add pathspec..." was given without "-u" or "-A" |
Junio C Hamano | d226b14 | 2013-04-17 12:32:21 -0700 | [diff] [blame] | 470 | * and pathspec... covers a removed path. |
Junio C Hamano | 45c45e3 | 2011-04-19 12:18:20 -0700 | [diff] [blame] | 471 | */ |
Junio C Hamano | d226b14 | 2013-04-17 12:32:21 -0700 | [diff] [blame] | 472 | memset(&update_data, 0, sizeof(update_data)); |
| 473 | if (!take_worktree_changes && addremove_explicit < 0) |
| 474 | update_data.warn_add_would_remove = 1; |
Junio C Hamano | 45c45e3 | 2011-04-19 12:18:20 -0700 | [diff] [blame] | 475 | |
| 476 | if (!take_worktree_changes && addremove_explicit < 0 && argc) |
| 477 | /* |
| 478 | * Turn "git add pathspec..." to "git add -A pathspec..." |
| 479 | * in Git 2.0 but not yet |
| 480 | */ |
| 481 | ; /* addremove = 1; */ |
| 482 | |
Jens Lehmann | 108da0d | 2010-07-10 00:18:38 +0200 | [diff] [blame] | 483 | if (!show_only && ignore_missing) |
Ævar Arnfjörð Bjarmason | 990ac4b | 2011-02-22 23:41:29 +0000 | [diff] [blame] | 484 | die(_("Option --ignore-missing can only be used together with --dry-run")); |
Matthieu Moy | 0fa2eb5 | 2013-01-28 10:16:33 +0100 | [diff] [blame] | 485 | if (addremove) { |
| 486 | option_with_implicit_dot = "--all"; |
| 487 | short_option_with_implicit_dot = "-A"; |
| 488 | } |
| 489 | if (take_worktree_changes) { |
| 490 | option_with_implicit_dot = "--update"; |
| 491 | short_option_with_implicit_dot = "-u"; |
| 492 | } |
| 493 | if (option_with_implicit_dot && !argc) { |
Junio C Hamano | 3ba1f11 | 2008-07-19 19:51:11 -0700 | [diff] [blame] | 494 | static const char *here[2] = { ".", NULL }; |
| 495 | argc = 1; |
| 496 | argv = here; |
Jonathan Nieder | 71c7b05 | 2013-03-19 15:50:50 -0700 | [diff] [blame] | 497 | implicit_dot = 1; |
Junio C Hamano | 3ba1f11 | 2008-07-19 19:51:11 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 500 | add_new_files = !take_worktree_changes && !refresh_only; |
| 501 | require_pathspec = !take_worktree_changes; |
| 502 | |
Junio C Hamano | 30ca07a | 2007-03-31 23:09:02 -0700 | [diff] [blame] | 503 | newfd = hold_locked_index(&lock_file, 1); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 504 | |
Gustaf Hendeby | 205ffa9 | 2008-05-22 23:59:42 +0200 | [diff] [blame] | 505 | flags = ((verbose ? ADD_CACHE_VERBOSE : 0) | |
Junio C Hamano | 0166592 | 2008-05-25 14:03:50 -0700 | [diff] [blame] | 506 | (show_only ? ADD_CACHE_PRETEND : 0) | |
Junio C Hamano | 3942581 | 2008-08-21 01:44:53 -0700 | [diff] [blame] | 507 | (intent_to_add ? ADD_CACHE_INTENT : 0) | |
Junio C Hamano | 1e5f764 | 2008-07-22 22:30:40 -0700 | [diff] [blame] | 508 | (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) | |
| 509 | (!(addremove || take_worktree_changes) |
Jonathan Nieder | 71c7b05 | 2013-03-19 15:50:50 -0700 | [diff] [blame] | 510 | ? ADD_CACHE_IGNORE_REMOVAL : 0)) | |
| 511 | (implicit_dot ? ADD_CACHE_IMPLICIT_DOT : 0); |
Gustaf Hendeby | 205ffa9 | 2008-05-22 23:59:42 +0200 | [diff] [blame] | 512 | |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 513 | if (require_pathspec && argc == 0) { |
Ævar Arnfjörð Bjarmason | 990ac4b | 2011-02-22 23:41:29 +0000 | [diff] [blame] | 514 | fprintf(stderr, _("Nothing specified, nothing added.\n")); |
| 515 | fprintf(stderr, _("Maybe you wanted to say 'git add .'?\n")); |
Junio C Hamano | 93b0d86 | 2006-12-20 13:06:46 -0800 | [diff] [blame] | 516 | return 0; |
| 517 | } |
Adam Spiers | f8a1113 | 2013-01-06 16:58:07 +0000 | [diff] [blame] | 518 | pathspec = validate_pathspec(argv, prefix); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 519 | |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 520 | if (read_cache() < 0) |
Ævar Arnfjörð Bjarmason | 990ac4b | 2011-02-22 23:41:29 +0000 | [diff] [blame] | 521 | die(_("index file corrupt")); |
Johannes Schindelin | 2ce53f9 | 2009-01-02 19:08:40 +0100 | [diff] [blame] | 522 | treat_gitlinks(pathspec); |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 523 | |
Linus Torvalds | 1d8842d | 2009-05-14 13:22:36 -0700 | [diff] [blame] | 524 | if (add_new_files) { |
| 525 | int baselen; |
| 526 | |
| 527 | /* Set up the default git porcelain excludes */ |
| 528 | memset(&dir, 0, sizeof(dir)); |
| 529 | if (!ignored_too) { |
| 530 | dir.flags |= DIR_COLLECT_IGNORED; |
| 531 | setup_standard_excludes(&dir); |
| 532 | } |
| 533 | |
Junio C Hamano | 1e5f764 | 2008-07-22 22:30:40 -0700 | [diff] [blame] | 534 | /* This picks up the paths that are not tracked */ |
Jonathan Nieder | 9df84e9 | 2013-03-19 15:51:34 -0700 | [diff] [blame] | 535 | baselen = fill_directory(&dir, implicit_dot ? NULL : pathspec); |
Linus Torvalds | 1d8842d | 2009-05-14 13:22:36 -0700 | [diff] [blame] | 536 | if (pathspec) |
Jonathan Nieder | 9df84e9 | 2013-03-19 15:51:34 -0700 | [diff] [blame] | 537 | seen = prune_directory(&dir, pathspec, baselen, |
| 538 | implicit_dot ? WARN_IMPLICIT_DOT : 0); |
Linus Torvalds | 1d8842d | 2009-05-14 13:22:36 -0700 | [diff] [blame] | 539 | } |
Junio C Hamano | 1e5f764 | 2008-07-22 22:30:40 -0700 | [diff] [blame] | 540 | |
Alexandre Julliard | d616813 | 2007-08-11 23:59:01 +0200 | [diff] [blame] | 541 | if (refresh_only) { |
| 542 | refresh(verbose, pathspec); |
| 543 | goto finish; |
| 544 | } |
| 545 | |
Junio C Hamano | 81f45e7 | 2010-02-09 17:30:49 -0500 | [diff] [blame] | 546 | if (pathspec) { |
| 547 | int i; |
Junio C Hamano | eb69934 | 2012-06-05 21:44:22 -0700 | [diff] [blame] | 548 | struct path_exclude_check check; |
| 549 | |
| 550 | path_exclude_check_init(&check, &dir); |
Junio C Hamano | 81f45e7 | 2010-02-09 17:30:49 -0500 | [diff] [blame] | 551 | if (!seen) |
Adam Spiers | 4b78d7b | 2013-01-06 16:58:09 +0000 | [diff] [blame] | 552 | seen = find_pathspecs_matching_against_index(pathspec); |
Junio C Hamano | 81f45e7 | 2010-02-09 17:30:49 -0500 | [diff] [blame] | 553 | for (i = 0; pathspec[i]; i++) { |
| 554 | if (!seen[i] && pathspec[i][0] |
Jens Lehmann | 108da0d | 2010-07-10 00:18:38 +0200 | [diff] [blame] | 555 | && !file_exists(pathspec[i])) { |
| 556 | if (ignore_missing) { |
Nguyễn Thái Ngọc Duy | 0188f6b | 2010-11-11 20:03:22 +0700 | [diff] [blame] | 557 | int dtype = DT_UNKNOWN; |
Adam Spiers | 9013089 | 2012-12-27 02:32:23 +0000 | [diff] [blame] | 558 | if (is_path_excluded(&check, pathspec[i], -1, &dtype)) |
Jens Lehmann | 108da0d | 2010-07-10 00:18:38 +0200 | [diff] [blame] | 559 | dir_add_ignored(&dir, pathspec[i], strlen(pathspec[i])); |
| 560 | } else |
Ævar Arnfjörð Bjarmason | 4816885 | 2011-02-22 23:41:31 +0000 | [diff] [blame] | 561 | die(_("pathspec '%s' did not match any files"), |
Jens Lehmann | 108da0d | 2010-07-10 00:18:38 +0200 | [diff] [blame] | 562 | pathspec[i]); |
| 563 | } |
Junio C Hamano | 81f45e7 | 2010-02-09 17:30:49 -0500 | [diff] [blame] | 564 | } |
| 565 | free(seen); |
Junio C Hamano | eb69934 | 2012-06-05 21:44:22 -0700 | [diff] [blame] | 566 | path_exclude_check_clear(&check); |
Junio C Hamano | 81f45e7 | 2010-02-09 17:30:49 -0500 | [diff] [blame] | 567 | } |
| 568 | |
Junio C Hamano | 568508e | 2011-10-28 14:48:40 -0700 | [diff] [blame] | 569 | plug_bulk_checkin(); |
| 570 | |
Junio C Hamano | 561954b | 2013-04-22 11:11:45 -0700 | [diff] [blame] | 571 | if ((flags & ADD_CACHE_IMPLICIT_DOT) && prefix) { |
| 572 | /* |
| 573 | * Check for modified files throughout the worktree so |
| 574 | * update_callback has a chance to warn about changes |
| 575 | * outside the cwd. |
| 576 | */ |
| 577 | update_data.implicit_dot = prefix; |
| 578 | update_data.implicit_dot_len = strlen(prefix); |
| 579 | pathspec = NULL; |
| 580 | } |
| 581 | update_data.flags = flags & ~ADD_CACHE_IMPLICIT_DOT; |
Junio C Hamano | d226b14 | 2013-04-17 12:32:21 -0700 | [diff] [blame] | 582 | update_files_in_cache(prefix, pathspec, &update_data); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 583 | |
Junio C Hamano | d226b14 | 2013-04-17 12:32:21 -0700 | [diff] [blame] | 584 | exit_status |= !!update_data.add_errors; |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 585 | if (add_new_files) |
| 586 | exit_status |= add_files(&dir, flags); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 587 | |
Junio C Hamano | 568508e | 2011-10-28 14:48:40 -0700 | [diff] [blame] | 588 | unplug_bulk_checkin(); |
| 589 | |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 590 | finish: |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 591 | if (active_cache_changed) { |
| 592 | if (write_cache(newfd, active_cache, active_nr) || |
Brandon Casey | 4ed7cd3 | 2008-01-16 13:12:46 -0600 | [diff] [blame] | 593 | commit_locked_index(&lock_file)) |
Ævar Arnfjörð Bjarmason | 990ac4b | 2011-02-22 23:41:29 +0000 | [diff] [blame] | 594 | die(_("Unable to write new index file")); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Alex Riesen | 7ae02a3 | 2008-05-12 19:58:10 +0200 | [diff] [blame] | 597 | return exit_status; |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 598 | } |