"git-add -n -u" should not add but just report
[git/git.git] / builtin-add.c
CommitLineData
0d781539
LT
1/*
2 * "git add" builtin command
3 *
4 * Copyright (C) 2006 Linus Torvalds
5 */
0d781539
LT
6#include "cache.h"
7#include "builtin.h"
8#include "dir.h"
5cde71d6 9#include "exec_cmd.h"
93872e07 10#include "cache-tree.h"
dfdac5d9
JH
11#include "diff.h"
12#include "diffcore.h"
13#include "commit.h"
14#include "revision.h"
58680165 15#include "run-command.h"
5c46f754 16#include "parse-options.h"
0d781539 17
5c46f754
KH
18static const char * const builtin_add_usage[] = {
19 "git-add [options] [--] <filepattern>...",
20 NULL
21};
b63e9950 22static int patch_interactive = 0, add_interactive = 0;
93c44d49 23static int take_worktree_changes;
896bdfa2 24
0d781539
LT
25static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
26{
f2593398
LT
27 char *seen;
28 int i, specs;
0d781539
LT
29 struct dir_entry **src, **dst;
30
f2593398
LT
31 for (specs = 0; pathspec[specs]; specs++)
32 /* nothing */;
28f75818 33 seen = xcalloc(specs, 1);
f2593398 34
0d781539
LT
35 src = dst = dir->entries;
36 i = dir->nr;
37 while (--i >= 0) {
38 struct dir_entry *entry = *src++;
4d06f8ac
JH
39 if (match_pathspec(pathspec, entry->name, entry->len,
40 prefix, seen))
41 *dst++ = entry;
0d781539
LT
42 }
43 dir->nr = dst - dir->entries;
f2593398
LT
44
45 for (i = 0; i < specs; i++) {
e96980ef
JK
46 if (!seen[i] && !file_exists(pathspec[i]))
47 die("pathspec '%s' did not match any files",
48 pathspec[i]);
f2593398 49 }
399f0a8e 50 free(seen);
0d781539
LT
51}
52
e96980ef
JK
53static void fill_directory(struct dir_struct *dir, const char **pathspec,
54 int ignored_too)
0d781539
LT
55{
56 const char *path, *base;
57 int baselen;
58
59 /* Set up the default git porcelain excludes */
60 memset(dir, 0, sizeof(*dir));
e96980ef
JK
61 if (!ignored_too) {
62 dir->collect_ignored = 1;
039bc64e 63 setup_standard_excludes(dir);
e96980ef 64 }
0d781539
LT
65
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 = "";
182af834
PH
73 if (baselen)
74 path = base = xmemdupz(*pathspec, baselen);
0d781539
LT
75
76 /* Read the directory and prune it */
9fc42d60 77 read_directory(dir, path, base, baselen, pathspec);
0d781539
LT
78 if (pathspec)
79 prune_directory(dir, pathspec, baselen);
80}
81
dfdac5d9
JH
82static void update_callback(struct diff_queue_struct *q,
83 struct diff_options *opt, void *cbdata)
84{
38ed1d89 85 int i, flags;
dfdac5d9 86
38ed1d89 87 flags = *((int *)cbdata);
dfdac5d9
JH
88 for (i = 0; i < q->nr; i++) {
89 struct diff_filepair *p = q->queue[i];
90 const char *path = p->one->path;
91 switch (p->status) {
92 default:
43b98acc 93 die("unexpected diff status %c", p->status);
dfdac5d9
JH
94 case DIFF_STATUS_UNMERGED:
95 case DIFF_STATUS_MODIFIED:
767c98a5 96 case DIFF_STATUS_TYPE_CHANGED:
38ed1d89 97 add_file_to_cache(path, flags);
dfdac5d9
JH
98 break;
99 case DIFF_STATUS_DELETED:
38ed1d89
JH
100 if (!(flags & ADD_CACHE_PRETEND))
101 remove_file_from_cache(path);
102 if (flags)
dfdac5d9
JH
103 printf("remove '%s'\n", path);
104 break;
105 }
106 }
107}
108
38ed1d89 109void add_files_to_cache(const char *prefix, const char **pathspec, int flags)
dfdac5d9
JH
110{
111 struct rev_info rev;
2ed2c222 112 init_revisions(&rev, prefix);
dfdac5d9 113 setup_revisions(0, NULL, &rev, NULL);
b6ec1d61 114 rev.prune_data = pathspec;
dfdac5d9
JH
115 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
116 rev.diffopt.format_callback = update_callback;
38ed1d89 117 rev.diffopt.format_callback_data = &flags;
fb63d7f8 118 run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
dfdac5d9
JH
119}
120
d616813d
AJ
121static void refresh(int verbose, const char **pathspec)
122{
123 char *seen;
124 int i, specs;
125
126 for (specs = 0; pathspec[specs]; specs++)
127 /* nothing */;
128 seen = xcalloc(specs, 1);
129 if (read_cache() < 0)
130 die("index file corrupt");
131 refresh_index(&the_index, verbose ? 0 : REFRESH_QUIET, pathspec, seen);
132 for (i = 0; i < specs; i++) {
133 if (!seen[i])
134 die("pathspec '%s' did not match any files", pathspec[i]);
135 }
399f0a8e 136 free(seen);
d616813d
AJ
137}
138
3f061887
JH
139static const char **validate_pathspec(int argc, const char **argv, const char *prefix)
140{
141 const char **pathspec = get_pathspec(prefix, argv);
142
143 return pathspec;
144}
145
146int interactive_add(int argc, const char **argv, const char *prefix)
58680165 147{
b63e9950 148 int status, ac;
3f061887
JH
149 const char **args;
150 const char **pathspec = NULL;
151
152 if (argc) {
153 pathspec = validate_pathspec(argc, argv, prefix);
154 if (!pathspec)
155 return -1;
156 }
324ccbd6 157
b63e9950
WC
158 args = xcalloc(sizeof(const char *), (argc + 4));
159 ac = 0;
160 args[ac++] = "add--interactive";
161 if (patch_interactive)
162 args[ac++] = "--patch";
163 args[ac++] = "--";
164 if (argc) {
165 memcpy(&(args[ac]), pathspec, sizeof(const char *) * argc);
166 ac += argc;
167 }
168 args[ac] = NULL;
7c0ab445
WC
169
170 status = run_command_v_opt(args, RUN_GIT_CMD);
171 free(args);
172 return status;
58680165
KH
173}
174
021b6e45 175static struct lock_file lock_file;
0d781539 176
b39c53e6 177static const char ignore_error[] =
6a1ad325
JH
178"The following paths are ignored by one of your .gitignore files:\n";
179
5c46f754 180static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
5c46f754
KH
181
182static struct option builtin_add_options[] = {
183 OPT__DRY_RUN(&show_only),
184 OPT__VERBOSE(&verbose),
185 OPT_GROUP(""),
186 OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"),
b63e9950 187 OPT_BOOLEAN('p', "patch", &patch_interactive, "interactive patching"),
5c46f754
KH
188 OPT_BOOLEAN('f', NULL, &ignored_too, "allow adding otherwise ignored files"),
189 OPT_BOOLEAN('u', NULL, &take_worktree_changes, "update tracked files"),
190 OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"),
191 OPT_END(),
192};
193
a633fca0 194int cmd_add(int argc, const char **argv, const char *prefix)
0d781539 195{
7c0ab445 196 int i, newfd;
0d781539
LT
197 const char **pathspec;
198 struct dir_struct dir;
5cde71d6 199
5c46f754
KH
200 argc = parse_options(argc, argv, builtin_add_options,
201 builtin_add_usage, 0);
b63e9950
WC
202 if (patch_interactive)
203 add_interactive = 1;
7c0ab445 204 if (add_interactive)
3f061887 205 exit(interactive_add(argc, argv, prefix));
0d781539 206
039bc64e 207 git_config(git_default_config);
0d781539 208
30ca07a2 209 newfd = hold_locked_index(&lock_file, 1);
0d781539 210
93c44d49 211 if (take_worktree_changes) {
b6ec1d61 212 const char **pathspec;
38ed1d89
JH
213 int flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
214 (show_only ? ADD_CACHE_PRETEND : 0));
215
58680165
KH
216 if (read_cache() < 0)
217 die("index file corrupt");
b6ec1d61 218 pathspec = get_pathspec(prefix, argv);
38ed1d89 219 add_files_to_cache(prefix, pathspec, flags);
dfdac5d9
JH
220 goto finish;
221 }
222
5c46f754 223 if (argc == 0) {
93b0d86a
JH
224 fprintf(stderr, "Nothing specified, nothing added.\n");
225 fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
226 return 0;
227 }
5c46f754 228 pathspec = get_pathspec(prefix, argv);
0d781539 229
d616813d
AJ
230 if (refresh_only) {
231 refresh(verbose, pathspec);
232 goto finish;
233 }
234
e96980ef 235 fill_directory(&dir, pathspec, ignored_too);
0d781539
LT
236
237 if (show_only) {
238 const char *sep = "", *eof = "";
239 for (i = 0; i < dir.nr; i++) {
240 printf("%s%s", sep, dir.entries[i]->name);
241 sep = " ";
242 eof = "\n";
243 }
244 fputs(eof, stdout);
245 return 0;
246 }
247
366bfcb6
NP
248 if (read_cache() < 0)
249 die("index file corrupt");
250
e96980ef 251 if (dir.ignored_nr) {
b39c53e6 252 fprintf(stderr, ignore_error);
e96980ef
JK
253 for (i = 0; i < dir.ignored_nr; i++) {
254 fprintf(stderr, "%s\n", dir.ignored[i]->name);
6a1ad325 255 }
e96980ef 256 fprintf(stderr, "Use -f if you really want to add them.\n");
b39c53e6 257 die("no files added");
6a1ad325
JH
258 }
259
0d781539 260 for (i = 0; i < dir.nr; i++)
38ed1d89 261 add_file_to_cache(dir.entries[i]->name, verbose ? ADD_CACHE_VERBOSE : 0);
0d781539 262
dfdac5d9 263 finish:
0d781539
LT
264 if (active_cache_changed) {
265 if (write_cache(newfd, active_cache, active_nr) ||
4ed7cd3a 266 commit_locked_index(&lock_file))
0d781539
LT
267 die("Unable to write new index file");
268 }
269
270 return 0;
271}