140
for (specs = 0; pathspec[specs]; specs++)
141
/* nothing */;
142
seen = xcalloc(specs, 1);
143
- if (read_cache() < 0)
144
- die("index file corrupt");
143
refresh_index(&the_index, verbose ? 0 : REFRESH_QUIET, pathspec, seen);
144
for (i = 0; i < specs; i++) {
145
if (!seen[i])
214
return git_default_config(var, value, cb);
215
}
216
217
+static int add_files(struct dir_struct *dir, int flags)
218
+{
219
+ int i, exit_status = 0;
220
+
221
+ if (dir->ignored_nr) {
222
+ fprintf(stderr, ignore_error);
223
+ for (i = 0; i < dir->ignored_nr; i++)
224
+ fprintf(stderr, "%s\n", dir->ignored[i]->name);
225
+ fprintf(stderr, "Use -f if you really want to add them.\n");
226
+ die("no files added");
227
+ }
228
+
229
+ for (i = 0; i < dir->nr; i++)
230
+ if (add_file_to_cache(dir->entries[i]->name, flags)) {
231
+ if (!ignore_add_errors)
232
+ die("adding files failed");
233
+ exit_status = 1;
234
+ }
235
+ return exit_status;
236
+}
237
+
238
int cmd_add(int argc, const char **argv, const char *prefix)
239
{
240
int exit_status = 0;
222
- int i, newfd;
241
+ int newfd;
242
const char **pathspec;
243
struct dir_struct dir;
244
int flags;
245
+ int add_new_files;
246
+ int require_pathspec;
247
248
argc = parse_options(argc, argv, builtin_add_options,
249
builtin_add_usage, 0);
254
255
git_config(add_config, NULL);
256
257
+ add_new_files = !take_worktree_changes && !refresh_only;
258
+ require_pathspec = !take_worktree_changes;
259
+
260
newfd = hold_locked_index(&lock_file, 1);
261
262
flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
263
(show_only ? ADD_CACHE_PRETEND : 0) |
264
(ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0));
265
242
- if (take_worktree_changes) {
243
- const char **pathspec;
244
- if (read_cache() < 0)
245
- die("index file corrupt");
246
- pathspec = get_pathspec(prefix, argv);
247
- exit_status = add_files_to_cache(prefix, pathspec, flags);
248
- goto finish;
249
- }
250
-
251
- if (argc == 0) {
266
+ if (require_pathspec && argc == 0) {
267
fprintf(stderr, "Nothing specified, nothing added.\n");
268
fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
269
return 0;
270
}
271
pathspec = get_pathspec(prefix, argv);
272
258
- if (refresh_only) {
259
- refresh(verbose, pathspec);
260
- goto finish;
261
- }
262
-
263
- fill_directory(&dir, pathspec, ignored_too);
273
+ /*
274
+ * If we are adding new files, we need to scan the working
275
+ * tree to find the ones that match pathspecs; this needs
276
+ * to be done before we read the index.
277
+ */
278
+ if (add_new_files)
279
+ fill_directory(&dir, pathspec, ignored_too);
280
281
if (read_cache() < 0)
282
die("index file corrupt");
283
268
- if (dir.ignored_nr) {
269
- fprintf(stderr, ignore_error);
270
- for (i = 0; i < dir.ignored_nr; i++) {
271
- fprintf(stderr, "%s\n", dir.ignored[i]->name);
272
- }
273
- fprintf(stderr, "Use -f if you really want to add them.\n");
274
- die("no files added");
284
+ if (refresh_only) {
285
+ refresh(verbose, pathspec);
286
+ goto finish;
287
}
288
277
- for (i = 0; i < dir.nr; i++)
278
- if (add_file_to_cache(dir.entries[i]->name, flags)) {
279
- if (!ignore_add_errors)
280
- die("adding files failed");
281
- exit_status = 1;
282
- }
289
+ if (take_worktree_changes)
290
+ exit_status |= add_files_to_cache(prefix, pathspec, flags);
291
+
292
+ if (add_new_files)
293
+ exit_status |= add_files(&dir, flags);
294
295
finish:
296
if (active_cache_changed) {