checkout: ensure full index
[git] / builtin / add.c
1 /*
2  * "git add" builtin command
3  *
4  * Copyright (C) 2006 Linus Torvalds
5  */
6 #define USE_THE_INDEX_COMPATIBILITY_MACROS
7 #include "cache.h"
8 #include "config.h"
9 #include "builtin.h"
10 #include "lockfile.h"
11 #include "dir.h"
12 #include "pathspec.h"
13 #include "exec-cmd.h"
14 #include "cache-tree.h"
15 #include "run-command.h"
16 #include "parse-options.h"
17 #include "diff.h"
18 #include "diffcore.h"
19 #include "revision.h"
20 #include "bulk-checkin.h"
21 #include "strvec.h"
22 #include "submodule.h"
23 #include "add-interactive.h"
24
25 static const char * const builtin_add_usage[] = {
26         N_("git add [<options>] [--] <pathspec>..."),
27         NULL
28 };
29 static int patch_interactive, add_interactive, edit_interactive;
30 static int take_worktree_changes;
31 static int add_renormalize;
32 static int pathspec_file_nul;
33 static const char *pathspec_from_file;
34 static int legacy_stash_p; /* support for the scripted `git stash` */
35
36 struct update_callback_data {
37         int flags;
38         int add_errors;
39 };
40
41 static int chmod_pathspec(struct pathspec *pathspec, char flip, int show_only)
42 {
43         int i, ret = 0;
44
45         for (i = 0; i < active_nr; i++) {
46                 struct cache_entry *ce = active_cache[i];
47                 int err;
48
49                 if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL))
50                         continue;
51
52                 if (!show_only)
53                         err = chmod_cache_entry(ce, flip);
54                 else
55                         err = S_ISREG(ce->ce_mode) ? 0 : -1;
56
57                 if (err < 0)
58                         ret = error(_("cannot chmod %cx '%s'"), flip, ce->name);
59         }
60
61         return ret;
62 }
63
64 static int fix_unmerged_status(struct diff_filepair *p,
65                                struct update_callback_data *data)
66 {
67         if (p->status != DIFF_STATUS_UNMERGED)
68                 return p->status;
69         if (!(data->flags & ADD_CACHE_IGNORE_REMOVAL) && !p->two->mode)
70                 /*
71                  * This is not an explicit add request, and the
72                  * path is missing from the working tree (deleted)
73                  */
74                 return DIFF_STATUS_DELETED;
75         else
76                 /*
77                  * Either an explicit add request, or path exists
78                  * in the working tree.  An attempt to explicitly
79                  * add a path that does not exist in the working tree
80                  * will be caught as an error by the caller immediately.
81                  */
82                 return DIFF_STATUS_MODIFIED;
83 }
84
85 static void update_callback(struct diff_queue_struct *q,
86                             struct diff_options *opt, void *cbdata)
87 {
88         int i;
89         struct update_callback_data *data = cbdata;
90
91         for (i = 0; i < q->nr; i++) {
92                 struct diff_filepair *p = q->queue[i];
93                 const char *path = p->one->path;
94                 switch (fix_unmerged_status(p, data)) {
95                 default:
96                         die(_("unexpected diff status %c"), p->status);
97                 case DIFF_STATUS_MODIFIED:
98                 case DIFF_STATUS_TYPE_CHANGED:
99                         if (add_file_to_index(&the_index, path, data->flags)) {
100                                 if (!(data->flags & ADD_CACHE_IGNORE_ERRORS))
101                                         die(_("updating files failed"));
102                                 data->add_errors++;
103                         }
104                         break;
105                 case DIFF_STATUS_DELETED:
106                         if (data->flags & ADD_CACHE_IGNORE_REMOVAL)
107                                 break;
108                         if (!(data->flags & ADD_CACHE_PRETEND))
109                                 remove_file_from_index(&the_index, path);
110                         if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE))
111                                 printf(_("remove '%s'\n"), path);
112                         break;
113                 }
114         }
115 }
116
117 int add_files_to_cache(const char *prefix,
118                        const struct pathspec *pathspec, int flags)
119 {
120         struct update_callback_data data;
121         struct rev_info rev;
122
123         memset(&data, 0, sizeof(data));
124         data.flags = flags;
125
126         repo_init_revisions(the_repository, &rev, prefix);
127         setup_revisions(0, NULL, &rev, NULL);
128         if (pathspec)
129                 copy_pathspec(&rev.prune_data, pathspec);
130         rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
131         rev.diffopt.format_callback = update_callback;
132         rev.diffopt.format_callback_data = &data;
133         rev.diffopt.flags.override_submodule_config = 1;
134         rev.max_count = 0; /* do not compare unmerged paths with stage #2 */
135         run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
136         clear_pathspec(&rev.prune_data);
137         return !!data.add_errors;
138 }
139
140 static int renormalize_tracked_files(const struct pathspec *pathspec, int flags)
141 {
142         int i, retval = 0;
143
144         /* TODO: audit for interaction with sparse-index. */
145         ensure_full_index(&the_index);
146         for (i = 0; i < active_nr; i++) {
147                 struct cache_entry *ce = active_cache[i];
148
149                 if (ce_stage(ce))
150                         continue; /* do not touch unmerged paths */
151                 if (!S_ISREG(ce->ce_mode) && !S_ISLNK(ce->ce_mode))
152                         continue; /* do not touch non blobs */
153                 if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL))
154                         continue;
155                 retval |= add_file_to_cache(ce->name, flags | ADD_CACHE_RENORMALIZE);
156         }
157
158         return retval;
159 }
160
161 static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec, int prefix)
162 {
163         char *seen;
164         int i;
165         struct dir_entry **src, **dst;
166
167         seen = xcalloc(pathspec->nr, 1);
168
169         src = dst = dir->entries;
170         i = dir->nr;
171         while (--i >= 0) {
172                 struct dir_entry *entry = *src++;
173                 if (dir_path_match(&the_index, entry, pathspec, prefix, seen))
174                         *dst++ = entry;
175         }
176         dir->nr = dst - dir->entries;
177         add_pathspec_matches_against_index(pathspec, &the_index, seen);
178         return seen;
179 }
180
181 static void refresh(int verbose, const struct pathspec *pathspec)
182 {
183         char *seen;
184         int i;
185
186         seen = xcalloc(pathspec->nr, 1);
187         refresh_index(&the_index, verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET,
188                       pathspec, seen, _("Unstaged changes after refreshing the index:"));
189         for (i = 0; i < pathspec->nr; i++) {
190                 if (!seen[i])
191                         die(_("pathspec '%s' did not match any files"),
192                             pathspec->items[i].match);
193         }
194         free(seen);
195 }
196
197 int run_add_interactive(const char *revision, const char *patch_mode,
198                         const struct pathspec *pathspec)
199 {
200         int status, i;
201         struct strvec argv = STRVEC_INIT;
202         int use_builtin_add_i =
203                 git_env_bool("GIT_TEST_ADD_I_USE_BUILTIN", -1);
204
205         if (use_builtin_add_i < 0) {
206                 int experimental;
207                 if (!git_config_get_bool("add.interactive.usebuiltin",
208                                          &use_builtin_add_i))
209                         ; /* ok */
210                 else if (!git_config_get_bool("feature.experimental", &experimental) &&
211                          experimental)
212                         use_builtin_add_i = 1;
213         }
214
215         if (use_builtin_add_i == 1) {
216                 enum add_p_mode mode;
217
218                 if (!patch_mode)
219                         return !!run_add_i(the_repository, pathspec);
220
221                 if (!strcmp(patch_mode, "--patch"))
222                         mode = ADD_P_ADD;
223                 else if (!strcmp(patch_mode, "--patch=stash"))
224                         mode = ADD_P_STASH;
225                 else if (!strcmp(patch_mode, "--patch=reset"))
226                         mode = ADD_P_RESET;
227                 else if (!strcmp(patch_mode, "--patch=checkout"))
228                         mode = ADD_P_CHECKOUT;
229                 else if (!strcmp(patch_mode, "--patch=worktree"))
230                         mode = ADD_P_WORKTREE;
231                 else
232                         die("'%s' not supported", patch_mode);
233
234                 return !!run_add_p(the_repository, mode, revision, pathspec);
235         }
236
237         strvec_push(&argv, "add--interactive");
238         if (patch_mode)
239                 strvec_push(&argv, patch_mode);
240         if (revision)
241                 strvec_push(&argv, revision);
242         strvec_push(&argv, "--");
243         for (i = 0; i < pathspec->nr; i++)
244                 /* pass original pathspec, to be re-parsed */
245                 strvec_push(&argv, pathspec->items[i].original);
246
247         status = run_command_v_opt(argv.v, RUN_GIT_CMD);
248         strvec_clear(&argv);
249         return status;
250 }
251
252 int interactive_add(const char **argv, const char *prefix, int patch)
253 {
254         struct pathspec pathspec;
255
256         parse_pathspec(&pathspec, 0,
257                        PATHSPEC_PREFER_FULL |
258                        PATHSPEC_SYMLINK_LEADING_PATH |
259                        PATHSPEC_PREFIX_ORIGIN,
260                        prefix, argv);
261
262         return run_add_interactive(NULL,
263                                    patch ? "--patch" : NULL,
264                                    &pathspec);
265 }
266
267 static int edit_patch(int argc, const char **argv, const char *prefix)
268 {
269         char *file = git_pathdup("ADD_EDIT.patch");
270         const char *apply_argv[] = { "apply", "--recount", "--cached",
271                 NULL, NULL };
272         struct child_process child = CHILD_PROCESS_INIT;
273         struct rev_info rev;
274         int out;
275         struct stat st;
276
277         apply_argv[3] = file;
278
279         git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
280
281         if (read_cache() < 0)
282                 die(_("Could not read the index"));
283
284         repo_init_revisions(the_repository, &rev, prefix);
285         rev.diffopt.context = 7;
286
287         argc = setup_revisions(argc, argv, &rev, NULL);
288         rev.diffopt.output_format = DIFF_FORMAT_PATCH;
289         rev.diffopt.use_color = 0;
290         rev.diffopt.flags.ignore_dirty_submodules = 1;
291         out = open(file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
292         if (out < 0)
293                 die(_("Could not open '%s' for writing."), file);
294         rev.diffopt.file = xfdopen(out, "w");
295         rev.diffopt.close_file = 1;
296         if (run_diff_files(&rev, 0))
297                 die(_("Could not write patch"));
298
299         if (launch_editor(file, NULL, NULL))
300                 die(_("editing patch failed"));
301
302         if (stat(file, &st))
303                 die_errno(_("Could not stat '%s'"), file);
304         if (!st.st_size)
305                 die(_("Empty patch. Aborted."));
306
307         child.git_cmd = 1;
308         child.argv = apply_argv;
309         if (run_command(&child))
310                 die(_("Could not apply '%s'"), file);
311
312         unlink(file);
313         free(file);
314         return 0;
315 }
316
317 static const char ignore_error[] =
318 N_("The following paths are ignored by one of your .gitignore files:\n");
319
320 static int verbose, show_only, ignored_too, refresh_only;
321 static int ignore_add_errors, intent_to_add, ignore_missing;
322 static int warn_on_embedded_repo = 1;
323
324 #define ADDREMOVE_DEFAULT 1
325 static int addremove = ADDREMOVE_DEFAULT;
326 static int addremove_explicit = -1; /* unspecified */
327
328 static char *chmod_arg;
329
330 static int ignore_removal_cb(const struct option *opt, const char *arg, int unset)
331 {
332         /* if we are told to ignore, we are not adding removals */
333         *(int *)opt->value = !unset ? 0 : 1;
334         return 0;
335 }
336
337 static struct option builtin_add_options[] = {
338         OPT__DRY_RUN(&show_only, N_("dry run")),
339         OPT__VERBOSE(&verbose, N_("be verbose")),
340         OPT_GROUP(""),
341         OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")),
342         OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")),
343         OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")),
344         OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files"), 0),
345         OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")),
346         OPT_BOOL(0, "renormalize", &add_renormalize, N_("renormalize EOL of tracked files (implies -u)")),
347         OPT_BOOL('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")),
348         OPT_BOOL('A', "all", &addremove_explicit, N_("add changes from all tracked and untracked files")),
349         OPT_CALLBACK_F(0, "ignore-removal", &addremove_explicit,
350           NULL /* takes no arguments */,
351           N_("ignore paths removed in the working tree (same as --no-all)"),
352           PARSE_OPT_NOARG, ignore_removal_cb),
353         OPT_BOOL( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")),
354         OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")),
355         OPT_BOOL( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")),
356         OPT_STRING(0, "chmod", &chmod_arg, "(+|-)x",
357                    N_("override the executable bit of the listed files")),
358         OPT_HIDDEN_BOOL(0, "warn-embedded-repo", &warn_on_embedded_repo,
359                         N_("warn when adding an embedded repository")),
360         OPT_HIDDEN_BOOL(0, "legacy-stash-p", &legacy_stash_p,
361                         N_("backend for `git stash -p`")),
362         OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
363         OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
364         OPT_END(),
365 };
366
367 static int add_config(const char *var, const char *value, void *cb)
368 {
369         if (!strcmp(var, "add.ignoreerrors") ||
370             !strcmp(var, "add.ignore-errors")) {
371                 ignore_add_errors = git_config_bool(var, value);
372                 return 0;
373         }
374
375         return git_default_config(var, value, cb);
376 }
377
378 static const char embedded_advice[] = N_(
379 "You've added another git repository inside your current repository.\n"
380 "Clones of the outer repository will not contain the contents of\n"
381 "the embedded repository and will not know how to obtain it.\n"
382 "If you meant to add a submodule, use:\n"
383 "\n"
384 "       git submodule add <url> %s\n"
385 "\n"
386 "If you added this path by mistake, you can remove it from the\n"
387 "index with:\n"
388 "\n"
389 "       git rm --cached %s\n"
390 "\n"
391 "See \"git help submodule\" for more information."
392 );
393
394 static void check_embedded_repo(const char *path)
395 {
396         struct strbuf name = STRBUF_INIT;
397
398         if (!warn_on_embedded_repo)
399                 return;
400         if (!ends_with(path, "/"))
401                 return;
402
403         /* Drop trailing slash for aesthetics */
404         strbuf_addstr(&name, path);
405         strbuf_strip_suffix(&name, "/");
406
407         warning(_("adding embedded git repository: %s"), name.buf);
408         if (advice_add_embedded_repo) {
409                 advise(embedded_advice, name.buf, name.buf);
410                 /* there may be multiple entries; advise only once */
411                 advice_add_embedded_repo = 0;
412         }
413
414         strbuf_release(&name);
415 }
416
417 static int add_files(struct dir_struct *dir, int flags)
418 {
419         int i, exit_status = 0;
420
421         if (dir->ignored_nr) {
422                 fprintf(stderr, _(ignore_error));
423                 for (i = 0; i < dir->ignored_nr; i++)
424                         fprintf(stderr, "%s\n", dir->ignored[i]->name);
425                 if (advice_add_ignored_file)
426                         advise(_("Use -f if you really want to add them.\n"
427                                 "Turn this message off by running\n"
428                                 "\"git config advice.addIgnoredFile false\""));
429                 exit_status = 1;
430         }
431
432         for (i = 0; i < dir->nr; i++) {
433                 if (add_file_to_index(&the_index, dir->entries[i]->name, flags)) {
434                         if (!ignore_add_errors)
435                                 die(_("adding files failed"));
436                         exit_status = 1;
437                 } else {
438                         check_embedded_repo(dir->entries[i]->name);
439                 }
440         }
441         return exit_status;
442 }
443
444 int cmd_add(int argc, const char **argv, const char *prefix)
445 {
446         int exit_status = 0;
447         struct pathspec pathspec;
448         struct dir_struct dir;
449         int flags;
450         int add_new_files;
451         int require_pathspec;
452         char *seen = NULL;
453         struct lock_file lock_file = LOCK_INIT;
454
455         git_config(add_config, NULL);
456
457         argc = parse_options(argc, argv, prefix, builtin_add_options,
458                           builtin_add_usage, PARSE_OPT_KEEP_ARGV0);
459         if (patch_interactive)
460                 add_interactive = 1;
461         if (add_interactive) {
462                 if (pathspec_from_file)
463                         die(_("--pathspec-from-file is incompatible with --interactive/--patch"));
464                 exit(interactive_add(argv + 1, prefix, patch_interactive));
465         }
466         if (legacy_stash_p) {
467                 struct pathspec pathspec;
468
469                 parse_pathspec(&pathspec, 0,
470                         PATHSPEC_PREFER_FULL |
471                         PATHSPEC_SYMLINK_LEADING_PATH |
472                         PATHSPEC_PREFIX_ORIGIN,
473                         prefix, argv);
474
475                 return run_add_interactive(NULL, "--patch=stash", &pathspec);
476         }
477
478         if (edit_interactive) {
479                 if (pathspec_from_file)
480                         die(_("--pathspec-from-file is incompatible with --edit"));
481                 return(edit_patch(argc, argv, prefix));
482         }
483         argc--;
484         argv++;
485
486         if (0 <= addremove_explicit)
487                 addremove = addremove_explicit;
488         else if (take_worktree_changes && ADDREMOVE_DEFAULT)
489                 addremove = 0; /* "-u" was given but not "-A" */
490
491         if (addremove && take_worktree_changes)
492                 die(_("-A and -u are mutually incompatible"));
493
494         if (!show_only && ignore_missing)
495                 die(_("Option --ignore-missing can only be used together with --dry-run"));
496
497         if (chmod_arg && ((chmod_arg[0] != '-' && chmod_arg[0] != '+') ||
498                           chmod_arg[1] != 'x' || chmod_arg[2]))
499                 die(_("--chmod param '%s' must be either -x or +x"), chmod_arg);
500
501         add_new_files = !take_worktree_changes && !refresh_only && !add_renormalize;
502         require_pathspec = !(take_worktree_changes || (0 < addremove_explicit));
503
504         hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
505
506         /*
507          * Check the "pathspec '%s' did not match any files" block
508          * below before enabling new magic.
509          */
510         parse_pathspec(&pathspec, PATHSPEC_ATTR,
511                        PATHSPEC_PREFER_FULL |
512                        PATHSPEC_SYMLINK_LEADING_PATH,
513                        prefix, argv);
514
515         if (pathspec_from_file) {
516                 if (pathspec.nr)
517                         die(_("--pathspec-from-file is incompatible with pathspec arguments"));
518
519                 parse_pathspec_file(&pathspec, PATHSPEC_ATTR,
520                                     PATHSPEC_PREFER_FULL |
521                                     PATHSPEC_SYMLINK_LEADING_PATH,
522                                     prefix, pathspec_from_file, pathspec_file_nul);
523         } else if (pathspec_file_nul) {
524                 die(_("--pathspec-file-nul requires --pathspec-from-file"));
525         }
526
527         if (require_pathspec && pathspec.nr == 0) {
528                 fprintf(stderr, _("Nothing specified, nothing added.\n"));
529                 if (advice_add_empty_pathspec)
530                         advise( _("Maybe you wanted to say 'git add .'?\n"
531                                 "Turn this message off by running\n"
532                                 "\"git config advice.addEmptyPathspec false\""));
533                 return 0;
534         }
535
536         if (!take_worktree_changes && addremove_explicit < 0 && pathspec.nr)
537                 /* Turn "git add pathspec..." to "git add -A pathspec..." */
538                 addremove = 1;
539
540         flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
541                  (show_only ? ADD_CACHE_PRETEND : 0) |
542                  (intent_to_add ? ADD_CACHE_INTENT : 0) |
543                  (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) |
544                  (!(addremove || take_worktree_changes)
545                   ? ADD_CACHE_IGNORE_REMOVAL : 0));
546
547         if (read_cache_preload(&pathspec) < 0)
548                 die(_("index file corrupt"));
549
550         die_in_unpopulated_submodule(&the_index, prefix);
551         die_path_inside_submodule(&the_index, &pathspec);
552
553         dir_init(&dir);
554         if (add_new_files) {
555                 int baselen;
556
557                 /* Set up the default git porcelain excludes */
558                 if (!ignored_too) {
559                         dir.flags |= DIR_COLLECT_IGNORED;
560                         setup_standard_excludes(&dir);
561                 }
562
563                 /* This picks up the paths that are not tracked */
564                 baselen = fill_directory(&dir, &the_index, &pathspec);
565                 if (pathspec.nr)
566                         seen = prune_directory(&dir, &pathspec, baselen);
567         }
568
569         if (refresh_only) {
570                 refresh(verbose, &pathspec);
571                 goto finish;
572         }
573
574         if (pathspec.nr) {
575                 int i;
576
577                 if (!seen)
578                         seen = find_pathspecs_matching_against_index(&pathspec, &the_index);
579
580                 /*
581                  * file_exists() assumes exact match
582                  */
583                 GUARD_PATHSPEC(&pathspec,
584                                PATHSPEC_FROMTOP |
585                                PATHSPEC_LITERAL |
586                                PATHSPEC_GLOB |
587                                PATHSPEC_ICASE |
588                                PATHSPEC_EXCLUDE);
589
590                 for (i = 0; i < pathspec.nr; i++) {
591                         const char *path = pathspec.items[i].match;
592                         if (pathspec.items[i].magic & PATHSPEC_EXCLUDE)
593                                 continue;
594                         if (!seen[i] && path[0] &&
595                             ((pathspec.items[i].magic &
596                               (PATHSPEC_GLOB | PATHSPEC_ICASE)) ||
597                              !file_exists(path))) {
598                                 if (ignore_missing) {
599                                         int dtype = DT_UNKNOWN;
600                                         if (is_excluded(&dir, &the_index, path, &dtype))
601                                                 dir_add_ignored(&dir, &the_index,
602                                                                 path, pathspec.items[i].len);
603                                 } else
604                                         die(_("pathspec '%s' did not match any files"),
605                                             pathspec.items[i].original);
606                         }
607                 }
608                 free(seen);
609         }
610
611         plug_bulk_checkin();
612
613         if (add_renormalize)
614                 exit_status |= renormalize_tracked_files(&pathspec, flags);
615         else
616                 exit_status |= add_files_to_cache(prefix, &pathspec, flags);
617
618         if (add_new_files)
619                 exit_status |= add_files(&dir, flags);
620
621         if (chmod_arg && pathspec.nr)
622                 exit_status |= chmod_pathspec(&pathspec, chmod_arg[0], show_only);
623         unplug_bulk_checkin();
624
625 finish:
626         if (write_locked_index(&the_index, &lock_file,
627                                COMMIT_LOCK | SKIP_IF_UNCHANGED))
628                 die(_("Unable to write new index file"));
629
630         dir_clear(&dir);
631         UNLEAK(pathspec);
632         return exit_status;
633 }