Merge branch 'nd/fetch-pass-quiet-to-gc-child-process'
[git] / builtin / add.c
1 /*
2  * "git add" builtin command
3  *
4  * Copyright (C) 2006 Linus Torvalds
5  */
6 #include "cache.h"
7 #include "builtin.h"
8 #include "dir.h"
9 #include "pathspec.h"
10 #include "exec_cmd.h"
11 #include "cache-tree.h"
12 #include "run-command.h"
13 #include "parse-options.h"
14 #include "diff.h"
15 #include "diffcore.h"
16 #include "revision.h"
17 #include "bulk-checkin.h"
18 #include "argv-array.h"
19
20 static const char * const builtin_add_usage[] = {
21         N_("git add [options] [--] <pathspec>..."),
22         NULL
23 };
24 static int patch_interactive, add_interactive, edit_interactive;
25 static int take_worktree_changes;
26
27 struct update_callback_data {
28         int flags;
29         int add_errors;
30 };
31
32 static int fix_unmerged_status(struct diff_filepair *p,
33                                struct update_callback_data *data)
34 {
35         if (p->status != DIFF_STATUS_UNMERGED)
36                 return p->status;
37         if (!(data->flags & ADD_CACHE_IGNORE_REMOVAL) && !p->two->mode)
38                 /*
39                  * This is not an explicit add request, and the
40                  * path is missing from the working tree (deleted)
41                  */
42                 return DIFF_STATUS_DELETED;
43         else
44                 /*
45                  * Either an explicit add request, or path exists
46                  * in the working tree.  An attempt to explicitly
47                  * add a path that does not exist in the working tree
48                  * will be caught as an error by the caller immediately.
49                  */
50                 return DIFF_STATUS_MODIFIED;
51 }
52
53 static void update_callback(struct diff_queue_struct *q,
54                             struct diff_options *opt, void *cbdata)
55 {
56         int i;
57         struct update_callback_data *data = cbdata;
58
59         for (i = 0; i < q->nr; i++) {
60                 struct diff_filepair *p = q->queue[i];
61                 const char *path = p->one->path;
62                 switch (fix_unmerged_status(p, data)) {
63                 default:
64                         die(_("unexpected diff status %c"), p->status);
65                 case DIFF_STATUS_MODIFIED:
66                 case DIFF_STATUS_TYPE_CHANGED:
67                         if (add_file_to_index(&the_index, path, data->flags)) {
68                                 if (!(data->flags & ADD_CACHE_IGNORE_ERRORS))
69                                         die(_("updating files failed"));
70                                 data->add_errors++;
71                         }
72                         break;
73                 case DIFF_STATUS_DELETED:
74                         if (data->flags & ADD_CACHE_IGNORE_REMOVAL)
75                                 break;
76                         if (!(data->flags & ADD_CACHE_PRETEND))
77                                 remove_file_from_index(&the_index, path);
78                         if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE))
79                                 printf(_("remove '%s'\n"), path);
80                         break;
81                 }
82         }
83 }
84
85 int add_files_to_cache(const char *prefix,
86                        const struct pathspec *pathspec, int flags)
87 {
88         struct update_callback_data data;
89         struct rev_info rev;
90
91         memset(&data, 0, sizeof(data));
92         data.flags = flags;
93
94         init_revisions(&rev, prefix);
95         setup_revisions(0, NULL, &rev, NULL);
96         if (pathspec)
97                 copy_pathspec(&rev.prune_data, pathspec);
98         rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
99         rev.diffopt.format_callback = update_callback;
100         rev.diffopt.format_callback_data = &data;
101         rev.max_count = 0; /* do not compare unmerged paths with stage #2 */
102         run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
103         return !!data.add_errors;
104 }
105
106 static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec, int prefix)
107 {
108         char *seen;
109         int i;
110         struct dir_entry **src, **dst;
111
112         seen = xcalloc(pathspec->nr, 1);
113
114         src = dst = dir->entries;
115         i = dir->nr;
116         while (--i >= 0) {
117                 struct dir_entry *entry = *src++;
118                 if (dir_path_match(entry, pathspec, prefix, seen))
119                         *dst++ = entry;
120         }
121         dir->nr = dst - dir->entries;
122         add_pathspec_matches_against_index(pathspec, seen);
123         return seen;
124 }
125
126 static void refresh(int verbose, const struct pathspec *pathspec)
127 {
128         char *seen;
129         int i;
130
131         seen = xcalloc(pathspec->nr, 1);
132         refresh_index(&the_index, verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET,
133                       pathspec, seen, _("Unstaged changes after refreshing the index:"));
134         for (i = 0; i < pathspec->nr; i++) {
135                 if (!seen[i])
136                         die(_("pathspec '%s' did not match any files"),
137                             pathspec->items[i].match);
138         }
139         free(seen);
140 }
141
142 int run_add_interactive(const char *revision, const char *patch_mode,
143                         const struct pathspec *pathspec)
144 {
145         int status, i;
146         struct argv_array argv = ARGV_ARRAY_INIT;
147
148         argv_array_push(&argv, "add--interactive");
149         if (patch_mode)
150                 argv_array_push(&argv, patch_mode);
151         if (revision)
152                 argv_array_push(&argv, revision);
153         argv_array_push(&argv, "--");
154         for (i = 0; i < pathspec->nr; i++)
155                 /* pass original pathspec, to be re-parsed */
156                 argv_array_push(&argv, pathspec->items[i].original);
157
158         status = run_command_v_opt(argv.argv, RUN_GIT_CMD);
159         argv_array_clear(&argv);
160         return status;
161 }
162
163 int interactive_add(int argc, const char **argv, const char *prefix, int patch)
164 {
165         struct pathspec pathspec;
166
167         parse_pathspec(&pathspec, 0,
168                        PATHSPEC_PREFER_FULL |
169                        PATHSPEC_SYMLINK_LEADING_PATH |
170                        PATHSPEC_PREFIX_ORIGIN,
171                        prefix, argv);
172
173         return run_add_interactive(NULL,
174                                    patch ? "--patch" : NULL,
175                                    &pathspec);
176 }
177
178 static int edit_patch(int argc, const char **argv, const char *prefix)
179 {
180         char *file = git_pathdup("ADD_EDIT.patch");
181         const char *apply_argv[] = { "apply", "--recount", "--cached",
182                 NULL, NULL };
183         struct child_process child = CHILD_PROCESS_INIT;
184         struct rev_info rev;
185         int out;
186         struct stat st;
187
188         apply_argv[3] = file;
189
190         git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
191
192         if (read_cache() < 0)
193                 die(_("Could not read the index"));
194
195         init_revisions(&rev, prefix);
196         rev.diffopt.context = 7;
197
198         argc = setup_revisions(argc, argv, &rev, NULL);
199         rev.diffopt.output_format = DIFF_FORMAT_PATCH;
200         rev.diffopt.use_color = 0;
201         DIFF_OPT_SET(&rev.diffopt, IGNORE_DIRTY_SUBMODULES);
202         out = open(file, O_CREAT | O_WRONLY, 0666);
203         if (out < 0)
204                 die(_("Could not open '%s' for writing."), file);
205         rev.diffopt.file = xfdopen(out, "w");
206         rev.diffopt.close_file = 1;
207         if (run_diff_files(&rev, 0))
208                 die(_("Could not write patch"));
209
210         launch_editor(file, NULL, NULL);
211
212         if (stat(file, &st))
213                 die_errno(_("Could not stat '%s'"), file);
214         if (!st.st_size)
215                 die(_("Empty patch. Aborted."));
216
217         child.git_cmd = 1;
218         child.argv = apply_argv;
219         if (run_command(&child))
220                 die(_("Could not apply '%s'"), file);
221
222         unlink(file);
223         free(file);
224         return 0;
225 }
226
227 static struct lock_file lock_file;
228
229 static const char ignore_error[] =
230 N_("The following paths are ignored by one of your .gitignore files:\n");
231
232 static int verbose, show_only, ignored_too, refresh_only;
233 static int ignore_add_errors, intent_to_add, ignore_missing;
234
235 #define ADDREMOVE_DEFAULT 1
236 static int addremove = ADDREMOVE_DEFAULT;
237 static int addremove_explicit = -1; /* unspecified */
238
239 static int ignore_removal_cb(const struct option *opt, const char *arg, int unset)
240 {
241         /* if we are told to ignore, we are not adding removals */
242         *(int *)opt->value = !unset ? 0 : 1;
243         return 0;
244 }
245
246 static struct option builtin_add_options[] = {
247         OPT__DRY_RUN(&show_only, N_("dry run")),
248         OPT__VERBOSE(&verbose, N_("be verbose")),
249         OPT_GROUP(""),
250         OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")),
251         OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")),
252         OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")),
253         OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files")),
254         OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")),
255         OPT_BOOL('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")),
256         OPT_BOOL('A', "all", &addremove_explicit, N_("add changes from all tracked and untracked files")),
257         { OPTION_CALLBACK, 0, "ignore-removal", &addremove_explicit,
258           NULL /* takes no arguments */,
259           N_("ignore paths removed in the working tree (same as --no-all)"),
260           PARSE_OPT_NOARG, ignore_removal_cb },
261         OPT_BOOL( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")),
262         OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")),
263         OPT_BOOL( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")),
264         OPT_END(),
265 };
266
267 static int add_config(const char *var, const char *value, void *cb)
268 {
269         if (!strcmp(var, "add.ignoreerrors") ||
270             !strcmp(var, "add.ignore-errors")) {
271                 ignore_add_errors = git_config_bool(var, value);
272                 return 0;
273         }
274         return git_default_config(var, value, cb);
275 }
276
277 static int add_files(struct dir_struct *dir, int flags)
278 {
279         int i, exit_status = 0;
280
281         if (dir->ignored_nr) {
282                 fprintf(stderr, _(ignore_error));
283                 for (i = 0; i < dir->ignored_nr; i++)
284                         fprintf(stderr, "%s\n", dir->ignored[i]->name);
285                 fprintf(stderr, _("Use -f if you really want to add them.\n"));
286                 die(_("no files added"));
287         }
288
289         for (i = 0; i < dir->nr; i++)
290                 if (add_file_to_cache(dir->entries[i]->name, flags)) {
291                         if (!ignore_add_errors)
292                                 die(_("adding files failed"));
293                         exit_status = 1;
294                 }
295         return exit_status;
296 }
297
298 int cmd_add(int argc, const char **argv, const char *prefix)
299 {
300         int exit_status = 0;
301         struct pathspec pathspec;
302         struct dir_struct dir;
303         int flags;
304         int add_new_files;
305         int require_pathspec;
306         char *seen = NULL;
307
308         git_config(add_config, NULL);
309
310         argc = parse_options(argc, argv, prefix, builtin_add_options,
311                           builtin_add_usage, PARSE_OPT_KEEP_ARGV0);
312         if (patch_interactive)
313                 add_interactive = 1;
314         if (add_interactive)
315                 exit(interactive_add(argc - 1, argv + 1, prefix, patch_interactive));
316
317         if (edit_interactive)
318                 return(edit_patch(argc, argv, prefix));
319         argc--;
320         argv++;
321
322         if (0 <= addremove_explicit)
323                 addremove = addremove_explicit;
324         else if (take_worktree_changes && ADDREMOVE_DEFAULT)
325                 addremove = 0; /* "-u" was given but not "-A" */
326
327         if (addremove && take_worktree_changes)
328                 die(_("-A and -u are mutually incompatible"));
329
330         if (!take_worktree_changes && addremove_explicit < 0 && argc)
331                 /* Turn "git add pathspec..." to "git add -A pathspec..." */
332                 addremove = 1;
333
334         if (!show_only && ignore_missing)
335                 die(_("Option --ignore-missing can only be used together with --dry-run"));
336
337         if ((0 < addremove_explicit || take_worktree_changes) && !argc) {
338                 static const char *whole[2] = { ":/", NULL };
339                 argc = 1;
340                 argv = whole;
341         }
342
343         add_new_files = !take_worktree_changes && !refresh_only;
344         require_pathspec = !take_worktree_changes;
345
346         hold_locked_index(&lock_file, 1);
347
348         flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
349                  (show_only ? ADD_CACHE_PRETEND : 0) |
350                  (intent_to_add ? ADD_CACHE_INTENT : 0) |
351                  (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) |
352                  (!(addremove || take_worktree_changes)
353                   ? ADD_CACHE_IGNORE_REMOVAL : 0));
354
355         if (require_pathspec && argc == 0) {
356                 fprintf(stderr, _("Nothing specified, nothing added.\n"));
357                 fprintf(stderr, _("Maybe you wanted to say 'git add .'?\n"));
358                 return 0;
359         }
360
361         if (read_cache() < 0)
362                 die(_("index file corrupt"));
363
364         /*
365          * Check the "pathspec '%s' did not match any files" block
366          * below before enabling new magic.
367          */
368         parse_pathspec(&pathspec, 0,
369                        PATHSPEC_PREFER_FULL |
370                        PATHSPEC_SYMLINK_LEADING_PATH |
371                        PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE,
372                        prefix, argv);
373
374         if (add_new_files) {
375                 int baselen;
376                 struct pathspec empty_pathspec;
377
378                 /* Set up the default git porcelain excludes */
379                 memset(&dir, 0, sizeof(dir));
380                 if (!ignored_too) {
381                         dir.flags |= DIR_COLLECT_IGNORED;
382                         setup_standard_excludes(&dir);
383                 }
384
385                 memset(&empty_pathspec, 0, sizeof(empty_pathspec));
386                 /* This picks up the paths that are not tracked */
387                 baselen = fill_directory(&dir, &pathspec);
388                 if (pathspec.nr)
389                         seen = prune_directory(&dir, &pathspec, baselen);
390         }
391
392         if (refresh_only) {
393                 refresh(verbose, &pathspec);
394                 goto finish;
395         }
396
397         if (pathspec.nr) {
398                 int i;
399
400                 if (!seen)
401                         seen = find_pathspecs_matching_against_index(&pathspec);
402
403                 /*
404                  * file_exists() assumes exact match
405                  */
406                 GUARD_PATHSPEC(&pathspec,
407                                PATHSPEC_FROMTOP |
408                                PATHSPEC_LITERAL |
409                                PATHSPEC_GLOB |
410                                PATHSPEC_ICASE |
411                                PATHSPEC_EXCLUDE);
412
413                 for (i = 0; i < pathspec.nr; i++) {
414                         const char *path = pathspec.items[i].match;
415                         if (pathspec.items[i].magic & PATHSPEC_EXCLUDE)
416                                 continue;
417                         if (!seen[i] && path[0] &&
418                             ((pathspec.items[i].magic &
419                               (PATHSPEC_GLOB | PATHSPEC_ICASE)) ||
420                              !file_exists(path))) {
421                                 if (ignore_missing) {
422                                         int dtype = DT_UNKNOWN;
423                                         if (is_excluded(&dir, path, &dtype))
424                                                 dir_add_ignored(&dir, path, pathspec.items[i].len);
425                                 } else
426                                         die(_("pathspec '%s' did not match any files"),
427                                             pathspec.items[i].original);
428                         }
429                 }
430                 free(seen);
431         }
432
433         plug_bulk_checkin();
434
435         exit_status |= add_files_to_cache(prefix, &pathspec, flags);
436
437         if (add_new_files)
438                 exit_status |= add_files(&dir, flags);
439
440         unplug_bulk_checkin();
441
442 finish:
443         if (active_cache_changed) {
444                 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
445                         die(_("Unable to write new index file"));
446         }
447
448         return exit_status;
449 }