blob: 9930cf53f5e94cb7389e7c0b2b760b113a366e51 [file] [log] [blame]
Linus Torvalds0d781532006-05-17 09:33:32 -07001/*
2 * "git add" builtin command
3 *
4 * Copyright (C) 2006 Linus Torvalds
5 */
Linus Torvalds0d781532006-05-17 09:33:32 -07006#include "cache.h"
7#include "builtin.h"
8#include "dir.h"
Junio C Hamano5cde71d2006-12-10 20:55:50 -08009#include "exec_cmd.h"
Junio C Hamano93872e02006-05-20 01:28:49 -070010#include "cache-tree.h"
Junio C Hamanodfdac5d2007-04-20 01:39:39 -070011#include "diff.h"
12#include "diffcore.h"
13#include "commit.h"
14#include "revision.h"
Kristian Høgsberg58680162007-09-17 20:06:44 -040015#include "run-command.h"
Kristian Høgsberg5c46f752007-10-03 17:45:02 -040016#include "parse-options.h"
Linus Torvalds0d781532006-05-17 09:33:32 -070017
Kristian Høgsberg5c46f752007-10-03 17:45:02 -040018static const char * const builtin_add_usage[] = {
19 "git-add [options] [--] <filepattern>...",
20 NULL
21};
Wincent Colaiutab63e9952007-11-25 14:15:42 +010022static int patch_interactive = 0, add_interactive = 0;
Jeff King93c44d42007-05-12 02:42:00 -040023static int take_worktree_changes;
James Bowes896bdfa2007-02-27 22:31:10 -050024
Linus Torvalds0d781532006-05-17 09:33:32 -070025static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
26{
Linus Torvaldsf2593392006-05-17 13:23:19 -070027 char *seen;
28 int i, specs;
Linus Torvalds0d781532006-05-17 09:33:32 -070029 struct dir_entry **src, **dst;
30
Linus Torvaldsf2593392006-05-17 13:23:19 -070031 for (specs = 0; pathspec[specs]; specs++)
32 /* nothing */;
Peter Eriksen28f75812006-07-25 09:30:18 +020033 seen = xcalloc(specs, 1);
Linus Torvaldsf2593392006-05-17 13:23:19 -070034
Linus Torvalds0d781532006-05-17 09:33:32 -070035 src = dst = dir->entries;
36 i = dir->nr;
37 while (--i >= 0) {
38 struct dir_entry *entry = *src++;
Junio C Hamano4d06f8a2006-12-29 11:01:31 -080039 if (match_pathspec(pathspec, entry->name, entry->len,
40 prefix, seen))
41 *dst++ = entry;
Linus Torvalds0d781532006-05-17 09:33:32 -070042 }
43 dir->nr = dst - dir->entries;
Linus Torvaldsf2593392006-05-17 13:23:19 -070044
45 for (i = 0; i < specs; i++) {
Jeff Kinge96980e2007-06-12 23:42:14 +020046 if (!seen[i] && !file_exists(pathspec[i]))
47 die("pathspec '%s' did not match any files",
48 pathspec[i]);
Linus Torvaldsf2593392006-05-17 13:23:19 -070049 }
Benoit Sigoure399f0a82007-10-29 08:00:33 +010050 free(seen);
Linus Torvalds0d781532006-05-17 09:33:32 -070051}
52
Jeff Kinge96980e2007-06-12 23:42:14 +020053static void fill_directory(struct dir_struct *dir, const char **pathspec,
54 int ignored_too)
Linus Torvalds0d781532006-05-17 09:33:32 -070055{
56 const char *path, *base;
57 int baselen;
58
59 /* Set up the default git porcelain excludes */
60 memset(dir, 0, sizeof(*dir));
Jeff Kinge96980e2007-06-12 23:42:14 +020061 if (!ignored_too) {
62 dir->collect_ignored = 1;
Junio C Hamano039bc642007-11-14 00:05:00 -080063 setup_standard_excludes(dir);
Jeff Kinge96980e2007-06-12 23:42:14 +020064 }
Linus Torvalds0d781532006-05-17 09:33:32 -070065
66 /*
67 * Calculate common prefix for the pathspec, and
68 * use that to optimize the directory walk
69 */
70 baselen = common_prefix(pathspec);
71 path = ".";
72 base = "";
Pierre Habouzit182af832007-09-16 00:32:36 +020073 if (baselen)
74 path = base = xmemdupz(*pathspec, baselen);
Linus Torvalds0d781532006-05-17 09:33:32 -070075
76 /* Read the directory and prune it */
Linus Torvalds9fc42d62007-03-30 20:39:30 -070077 read_directory(dir, path, base, baselen, pathspec);
Linus Torvalds0d781532006-05-17 09:33:32 -070078 if (pathspec)
79 prune_directory(dir, pathspec, baselen);
80}
81
Alex Riesen7ae02a32008-05-12 19:58:10 +020082struct update_callback_data
83{
84 int flags;
85 int add_errors;
86};
87
Junio C Hamanodfdac5d2007-04-20 01:39:39 -070088static void update_callback(struct diff_queue_struct *q,
89 struct diff_options *opt, void *cbdata)
90{
Alex Riesen7ae02a32008-05-12 19:58:10 +020091 int i;
92 struct update_callback_data *data = cbdata;
Junio C Hamanodfdac5d2007-04-20 01:39:39 -070093
Junio C Hamanodfdac5d2007-04-20 01:39:39 -070094 for (i = 0; i < q->nr; i++) {
95 struct diff_filepair *p = q->queue[i];
96 const char *path = p->one->path;
97 switch (p->status) {
98 default:
Benoit Sigoure43b98ac2007-09-14 10:29:04 +020099 die("unexpected diff status %c", p->status);
Junio C Hamanodfdac5d2007-04-20 01:39:39 -0700100 case DIFF_STATUS_UNMERGED:
101 case DIFF_STATUS_MODIFIED:
Junio C Hamano767c98a2007-09-14 00:45:29 -0700102 case DIFF_STATUS_TYPE_CHANGED:
Junio C Hamano01665922008-05-25 14:03:50 -0700103 if (add_file_to_cache(path, data->flags)) {
104 if (!(data->flags & ADD_CACHE_IGNORE_ERRORS))
Alex Riesen7ae02a32008-05-12 19:58:10 +0200105 die("updating files failed");
106 data->add_errors++;
107 }
Junio C Hamanodfdac5d2007-04-20 01:39:39 -0700108 break;
109 case DIFF_STATUS_DELETED:
Junio C Hamano01665922008-05-25 14:03:50 -0700110 if (!(data->flags & ADD_CACHE_PRETEND))
Junio C Hamano38ed1d82008-05-21 12:04:34 -0700111 remove_file_from_cache(path);
Junio C Hamano01665922008-05-25 14:03:50 -0700112 if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE))
Junio C Hamanodfdac5d2007-04-20 01:39:39 -0700113 printf("remove '%s'\n", path);
114 break;
115 }
116 }
117}
118
Alex Riesen7ae02a32008-05-12 19:58:10 +0200119int add_files_to_cache(const char *prefix, const char **pathspec, int flags)
Junio C Hamanodfdac5d2007-04-20 01:39:39 -0700120{
Alex Riesen7ae02a32008-05-12 19:58:10 +0200121 struct update_callback_data data;
Junio C Hamanodfdac5d2007-04-20 01:39:39 -0700122 struct rev_info rev;
Salikh Zakirov2ed2c222007-08-16 02:01:43 +0900123 init_revisions(&rev, prefix);
Junio C Hamanodfdac5d2007-04-20 01:39:39 -0700124 setup_revisions(0, NULL, &rev, NULL);
Junio C Hamanob6ec1d62007-11-18 01:12:04 -0800125 rev.prune_data = pathspec;
Junio C Hamanodfdac5d2007-04-20 01:39:39 -0700126 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
127 rev.diffopt.format_callback = update_callback;
Alex Riesen7ae02a32008-05-12 19:58:10 +0200128 data.flags = flags;
129 data.add_errors = 0;
130 rev.diffopt.format_callback_data = &data;
Junio C Hamanofb63d7f2007-11-09 18:22:52 -0800131 run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
Alex Riesen7ae02a32008-05-12 19:58:10 +0200132 return !!data.add_errors;
Junio C Hamanodfdac5d2007-04-20 01:39:39 -0700133}
134
Alexandre Julliardd6168132007-08-11 23:59:01 +0200135static void refresh(int verbose, const char **pathspec)
136{
137 char *seen;
138 int i, specs;
139
140 for (specs = 0; pathspec[specs]; specs++)
141 /* nothing */;
142 seen = xcalloc(specs, 1);
143 if (read_cache() < 0)
144 die("index file corrupt");
145 refresh_index(&the_index, verbose ? 0 : REFRESH_QUIET, pathspec, seen);
146 for (i = 0; i < specs; i++) {
147 if (!seen[i])
148 die("pathspec '%s' did not match any files", pathspec[i]);
149 }
Benoit Sigoure399f0a82007-10-29 08:00:33 +0100150 free(seen);
Alexandre Julliardd6168132007-08-11 23:59:01 +0200151}
152
Junio C Hamano3f061882007-11-25 10:10:10 -0800153static const char **validate_pathspec(int argc, const char **argv, const char *prefix)
154{
155 const char **pathspec = get_pathspec(prefix, argv);
156
157 return pathspec;
158}
159
160int interactive_add(int argc, const char **argv, const char *prefix)
Kristian Høgsberg58680162007-09-17 20:06:44 -0400161{
Wincent Colaiutab63e9952007-11-25 14:15:42 +0100162 int status, ac;
Junio C Hamano3f061882007-11-25 10:10:10 -0800163 const char **args;
164 const char **pathspec = NULL;
Junio C Hamano324ccbd2007-11-25 10:07:55 -0800165
Junio C Hamano3f061882007-11-25 10:10:10 -0800166 if (argc) {
167 pathspec = validate_pathspec(argc, argv, prefix);
168 if (!pathspec)
169 return -1;
170 }
171
Wincent Colaiutab63e9952007-11-25 14:15:42 +0100172 args = xcalloc(sizeof(const char *), (argc + 4));
173 ac = 0;
174 args[ac++] = "add--interactive";
175 if (patch_interactive)
176 args[ac++] = "--patch";
177 args[ac++] = "--";
178 if (argc) {
179 memcpy(&(args[ac]), pathspec, sizeof(const char *) * argc);
180 ac += argc;
181 }
182 args[ac] = NULL;
Kristian Høgsberg58680162007-09-17 20:06:44 -0400183
Wincent Colaiuta7c0ab442007-11-22 01:02:52 +0100184 status = run_command_v_opt(args, RUN_GIT_CMD);
185 free(args);
186 return status;
Kristian Høgsberg58680162007-09-17 20:06:44 -0400187}
188
Junio C Hamano021b6e42006-06-06 12:51:49 -0700189static struct lock_file lock_file;
Linus Torvalds0d781532006-05-17 09:33:32 -0700190
Petr Baudisb39c53e2007-08-29 00:41:23 +0200191static const char ignore_error[] =
Junio C Hamano6a1ad322006-12-25 17:46:38 -0800192"The following paths are ignored by one of your .gitignore files:\n";
193
Kristian Høgsberg5c46f752007-10-03 17:45:02 -0400194static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
Alex Riesen984b83e2008-05-12 19:58:29 +0200195static int ignore_add_errors;
Kristian Høgsberg5c46f752007-10-03 17:45:02 -0400196
197static struct option builtin_add_options[] = {
198 OPT__DRY_RUN(&show_only),
199 OPT__VERBOSE(&verbose),
200 OPT_GROUP(""),
201 OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"),
Wincent Colaiutab63e9952007-11-25 14:15:42 +0100202 OPT_BOOLEAN('p', "patch", &patch_interactive, "interactive patching"),
SZEDER Gábor69c61c42008-06-14 11:48:00 +0200203 OPT_BOOLEAN('f', "force", &ignored_too, "allow adding otherwise ignored files"),
204 OPT_BOOLEAN('u', "update", &take_worktree_changes, "update tracked files"),
Kristian Høgsberg5c46f752007-10-03 17:45:02 -0400205 OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"),
Alex Riesen984b83e2008-05-12 19:58:29 +0200206 OPT_BOOLEAN( 0 , "ignore-errors", &ignore_add_errors, "just skip files which cannot be added because of errors"),
Kristian Høgsberg5c46f752007-10-03 17:45:02 -0400207 OPT_END(),
208};
209
Junio C Hamano9bd81e42008-05-25 14:25:02 -0700210static int add_config(const char *var, const char *value, void *cb)
Alex Riesendad25e42008-05-12 19:59:23 +0200211{
212 if (!strcasecmp(var, "add.ignore-errors")) {
213 ignore_add_errors = git_config_bool(var, value);
214 return 0;
215 }
Junio C Hamano9bd81e42008-05-25 14:25:02 -0700216 return git_default_config(var, value, cb);
Alex Riesendad25e42008-05-12 19:59:23 +0200217}
218
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700219int cmd_add(int argc, const char **argv, const char *prefix)
Linus Torvalds0d781532006-05-17 09:33:32 -0700220{
Alex Riesen7ae02a32008-05-12 19:58:10 +0200221 int exit_status = 0;
Wincent Colaiuta7c0ab442007-11-22 01:02:52 +0100222 int i, newfd;
Linus Torvalds0d781532006-05-17 09:33:32 -0700223 const char **pathspec;
224 struct dir_struct dir;
Gustaf Hendeby205ffa92008-05-22 23:59:42 +0200225 int flags;
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800226
Kristian Høgsberg5c46f752007-10-03 17:45:02 -0400227 argc = parse_options(argc, argv, builtin_add_options,
228 builtin_add_usage, 0);
Wincent Colaiutab63e9952007-11-25 14:15:42 +0100229 if (patch_interactive)
230 add_interactive = 1;
Wincent Colaiuta7c0ab442007-11-22 01:02:52 +0100231 if (add_interactive)
Junio C Hamano3f061882007-11-25 10:10:10 -0800232 exit(interactive_add(argc, argv, prefix));
Linus Torvalds0d781532006-05-17 09:33:32 -0700233
Junio C Hamano9bd81e42008-05-25 14:25:02 -0700234 git_config(add_config, NULL);
Linus Torvalds0d781532006-05-17 09:33:32 -0700235
Junio C Hamano30ca07a2007-03-31 23:09:02 -0700236 newfd = hold_locked_index(&lock_file, 1);
Linus Torvalds0d781532006-05-17 09:33:32 -0700237
Gustaf Hendeby205ffa92008-05-22 23:59:42 +0200238 flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
Junio C Hamano01665922008-05-25 14:03:50 -0700239 (show_only ? ADD_CACHE_PRETEND : 0) |
240 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0));
Gustaf Hendeby205ffa92008-05-22 23:59:42 +0200241
Jeff King93c44d42007-05-12 02:42:00 -0400242 if (take_worktree_changes) {
Junio C Hamanob6ec1d62007-11-18 01:12:04 -0800243 const char **pathspec;
Kristian Høgsberg58680162007-09-17 20:06:44 -0400244 if (read_cache() < 0)
245 die("index file corrupt");
Junio C Hamanob6ec1d62007-11-18 01:12:04 -0800246 pathspec = get_pathspec(prefix, argv);
Alex Riesen7ae02a32008-05-12 19:58:10 +0200247 exit_status = add_files_to_cache(prefix, pathspec, flags);
Junio C Hamanodfdac5d2007-04-20 01:39:39 -0700248 goto finish;
249 }
250
Kristian Høgsberg5c46f752007-10-03 17:45:02 -0400251 if (argc == 0) {
Junio C Hamano93b0d862006-12-20 13:06:46 -0800252 fprintf(stderr, "Nothing specified, nothing added.\n");
253 fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
254 return 0;
255 }
Kristian Høgsberg5c46f752007-10-03 17:45:02 -0400256 pathspec = get_pathspec(prefix, argv);
Linus Torvalds0d781532006-05-17 09:33:32 -0700257
Alexandre Julliardd6168132007-08-11 23:59:01 +0200258 if (refresh_only) {
259 refresh(verbose, pathspec);
260 goto finish;
261 }
262
Jeff Kinge96980e2007-06-12 23:42:14 +0200263 fill_directory(&dir, pathspec, ignored_too);
Linus Torvalds0d781532006-05-17 09:33:32 -0700264
Nicolas Pitre366bfcb2006-12-04 11:13:39 -0500265 if (read_cache() < 0)
266 die("index file corrupt");
267
Jeff Kinge96980e2007-06-12 23:42:14 +0200268 if (dir.ignored_nr) {
Petr Baudisb39c53e2007-08-29 00:41:23 +0200269 fprintf(stderr, ignore_error);
Jeff Kinge96980e2007-06-12 23:42:14 +0200270 for (i = 0; i < dir.ignored_nr; i++) {
271 fprintf(stderr, "%s\n", dir.ignored[i]->name);
Junio C Hamano6a1ad322006-12-25 17:46:38 -0800272 }
Jeff Kinge96980e2007-06-12 23:42:14 +0200273 fprintf(stderr, "Use -f if you really want to add them.\n");
Petr Baudisb39c53e2007-08-29 00:41:23 +0200274 die("no files added");
Junio C Hamano6a1ad322006-12-25 17:46:38 -0800275 }
276
Linus Torvalds0d781532006-05-17 09:33:32 -0700277 for (i = 0; i < dir.nr; i++)
Junio C Hamano01665922008-05-25 14:03:50 -0700278 if (add_file_to_cache(dir.entries[i]->name, flags)) {
Alex Riesen984b83e2008-05-12 19:58:29 +0200279 if (!ignore_add_errors)
280 die("adding files failed");
281 exit_status = 1;
282 }
Linus Torvalds0d781532006-05-17 09:33:32 -0700283
Junio C Hamanodfdac5d2007-04-20 01:39:39 -0700284 finish:
Linus Torvalds0d781532006-05-17 09:33:32 -0700285 if (active_cache_changed) {
286 if (write_cache(newfd, active_cache, active_nr) ||
Brandon Casey4ed7cd32008-01-16 13:12:46 -0600287 commit_locked_index(&lock_file))
Linus Torvalds0d781532006-05-17 09:33:32 -0700288 die("Unable to write new index file");
289 }
290
Alex Riesen7ae02a32008-05-12 19:58:10 +0200291 return exit_status;
Linus Torvalds0d781532006-05-17 09:33:32 -0700292}