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