]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-add.c
Fix ugly magic special case in exact rename detection
[thirdparty/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"
0d781539
LT
15
16static const char builtin_add_usage[] =
480611d1 17"git-add [-n] [-v] [-f] [--interactive | -i] [-u] [--refresh] [--] <filepattern>...";
0d781539 18
93c44d49 19static int take_worktree_changes;
896bdfa2
JB
20static const char *excludes_file;
21
0d781539
LT
22static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
23{
f2593398
LT
24 char *seen;
25 int i, specs;
0d781539
LT
26 struct dir_entry **src, **dst;
27
f2593398
LT
28 for (specs = 0; pathspec[specs]; specs++)
29 /* nothing */;
28f75818 30 seen = xcalloc(specs, 1);
f2593398 31
0d781539
LT
32 src = dst = dir->entries;
33 i = dir->nr;
34 while (--i >= 0) {
35 struct dir_entry *entry = *src++;
4d06f8ac
JH
36 if (match_pathspec(pathspec, entry->name, entry->len,
37 prefix, seen))
38 *dst++ = entry;
0d781539
LT
39 }
40 dir->nr = dst - dir->entries;
f2593398
LT
41
42 for (i = 0; i < specs; i++) {
e96980ef
JK
43 if (!seen[i] && !file_exists(pathspec[i]))
44 die("pathspec '%s' did not match any files",
45 pathspec[i]);
f2593398 46 }
0d781539
LT
47}
48
e96980ef
JK
49static void fill_directory(struct dir_struct *dir, const char **pathspec,
50 int ignored_too)
0d781539
LT
51{
52 const char *path, *base;
53 int baselen;
54
55 /* Set up the default git porcelain excludes */
56 memset(dir, 0, sizeof(*dir));
e96980ef
JK
57 if (!ignored_too) {
58 dir->collect_ignored = 1;
59 dir->exclude_per_dir = ".gitignore";
60 path = git_path("info/exclude");
61 if (!access(path, R_OK))
62 add_excludes_from_file(dir, path);
8b4aee01 63 if (excludes_file != NULL && !access(excludes_file, R_OK))
e96980ef
JK
64 add_excludes_from_file(dir, excludes_file);
65 }
0d781539
LT
66
67 /*
68 * Calculate common prefix for the pathspec, and
69 * use that to optimize the directory walk
70 */
71 baselen = common_prefix(pathspec);
72 path = ".";
73 base = "";
182af834
PH
74 if (baselen)
75 path = base = xmemdupz(*pathspec, baselen);
0d781539
LT
76
77 /* Read the directory and prune it */
9fc42d60 78 read_directory(dir, path, base, baselen, pathspec);
0d781539
LT
79 if (pathspec)
80 prune_directory(dir, pathspec, baselen);
81}
82
dfdac5d9
JH
83static void update_callback(struct diff_queue_struct *q,
84 struct diff_options *opt, void *cbdata)
85{
86 int i, verbose;
87
88 verbose = *((int *)cbdata);
89 for (i = 0; i < q->nr; i++) {
90 struct diff_filepair *p = q->queue[i];
91 const char *path = p->one->path;
92 switch (p->status) {
93 default:
43b98acc 94 die("unexpected diff status %c", p->status);
dfdac5d9
JH
95 case DIFF_STATUS_UNMERGED:
96 case DIFF_STATUS_MODIFIED:
767c98a5 97 case DIFF_STATUS_TYPE_CHANGED:
dfdac5d9
JH
98 add_file_to_cache(path, verbose);
99 break;
100 case DIFF_STATUS_DELETED:
101 remove_file_from_cache(path);
102 if (verbose)
103 printf("remove '%s'\n", path);
104 break;
105 }
106 }
107}
108
2ed2c222 109static void update(int verbose, const char *prefix, const char **files)
dfdac5d9
JH
110{
111 struct rev_info rev;
2ed2c222 112 init_revisions(&rev, prefix);
dfdac5d9 113 setup_revisions(0, NULL, &rev, NULL);
2ed2c222 114 rev.prune_data = get_pathspec(prefix, files);
dfdac5d9
JH
115 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
116 rev.diffopt.format_callback = update_callback;
117 rev.diffopt.format_callback_data = &verbose;
118 if (read_cache() < 0)
119 die("index file corrupt");
120 run_diff_files(&rev, 0);
121}
122
d616813d
AJ
123static void refresh(int verbose, const char **pathspec)
124{
125 char *seen;
126 int i, specs;
127
128 for (specs = 0; pathspec[specs]; specs++)
129 /* nothing */;
130 seen = xcalloc(specs, 1);
131 if (read_cache() < 0)
132 die("index file corrupt");
133 refresh_index(&the_index, verbose ? 0 : REFRESH_QUIET, pathspec, seen);
134 for (i = 0; i < specs; i++) {
135 if (!seen[i])
136 die("pathspec '%s' did not match any files", pathspec[i]);
137 }
138}
139
896bdfa2
JB
140static int git_add_config(const char *var, const char *value)
141{
142 if (!strcmp(var, "core.excludesfile")) {
143 if (!value)
144 die("core.excludesfile without value");
145 excludes_file = xstrdup(value);
146 return 0;
147 }
148
149 return git_default_config(var, value);
150}
151
021b6e45 152static struct lock_file lock_file;
0d781539 153
b39c53e6 154static const char ignore_error[] =
6a1ad325
JH
155"The following paths are ignored by one of your .gitignore files:\n";
156
a633fca0 157int cmd_add(int argc, const char **argv, const char *prefix)
0d781539
LT
158{
159 int i, newfd;
d616813d 160 int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
0d781539
LT
161 const char **pathspec;
162 struct dir_struct dir;
5cde71d6
JH
163 int add_interactive = 0;
164
165 for (i = 1; i < argc; i++) {
df59afe3
JH
166 if (!strcmp("--interactive", argv[i]) ||
167 !strcmp("-i", argv[i]))
5cde71d6
JH
168 add_interactive++;
169 }
170 if (add_interactive) {
171 const char *args[] = { "add--interactive", NULL };
172
173 if (add_interactive != 1 || argc != 2)
174 die("add --interactive does not take any parameters");
175 execv_git_cmd(args);
176 exit(1);
177 }
0d781539 178
896bdfa2 179 git_config(git_add_config);
0d781539 180
30ca07a2 181 newfd = hold_locked_index(&lock_file, 1);
0d781539 182
0d781539
LT
183 for (i = 1; i < argc; i++) {
184 const char *arg = argv[i];
185
186 if (arg[0] != '-')
187 break;
188 if (!strcmp(arg, "--")) {
189 i++;
190 break;
191 }
192 if (!strcmp(arg, "-n")) {
193 show_only = 1;
194 continue;
195 }
6a1ad325
JH
196 if (!strcmp(arg, "-f")) {
197 ignored_too = 1;
198 continue;
199 }
0d781539
LT
200 if (!strcmp(arg, "-v")) {
201 verbose = 1;
202 continue;
203 }
dfdac5d9 204 if (!strcmp(arg, "-u")) {
93c44d49 205 take_worktree_changes = 1;
dfdac5d9
JH
206 continue;
207 }
d616813d
AJ
208 if (!strcmp(arg, "--refresh")) {
209 refresh_only = 1;
210 continue;
211 }
8cdf3364 212 usage(builtin_add_usage);
0d781539 213 }
dfdac5d9 214
93c44d49 215 if (take_worktree_changes) {
2ed2c222 216 update(verbose, prefix, argv + i);
dfdac5d9
JH
217 goto finish;
218 }
219
93b0d86a
JH
220 if (argc <= i) {
221 fprintf(stderr, "Nothing specified, nothing added.\n");
222 fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
223 return 0;
224 }
0d781539
LT
225 pathspec = get_pathspec(prefix, argv + i);
226
d616813d
AJ
227 if (refresh_only) {
228 refresh(verbose, pathspec);
229 goto finish;
230 }
231
e96980ef 232 fill_directory(&dir, pathspec, ignored_too);
0d781539
LT
233
234 if (show_only) {
235 const char *sep = "", *eof = "";
236 for (i = 0; i < dir.nr; i++) {
237 printf("%s%s", sep, dir.entries[i]->name);
238 sep = " ";
239 eof = "\n";
240 }
241 fputs(eof, stdout);
242 return 0;
243 }
244
366bfcb6
NP
245 if (read_cache() < 0)
246 die("index file corrupt");
247
e96980ef 248 if (dir.ignored_nr) {
b39c53e6 249 fprintf(stderr, ignore_error);
e96980ef
JK
250 for (i = 0; i < dir.ignored_nr; i++) {
251 fprintf(stderr, "%s\n", dir.ignored[i]->name);
6a1ad325 252 }
e96980ef 253 fprintf(stderr, "Use -f if you really want to add them.\n");
b39c53e6 254 die("no files added");
6a1ad325
JH
255 }
256
0d781539 257 for (i = 0; i < dir.nr; i++)
fd1c3bf0 258 add_file_to_cache(dir.entries[i]->name, verbose);
0d781539 259
dfdac5d9 260 finish:
0d781539
LT
261 if (active_cache_changed) {
262 if (write_cache(newfd, active_cache, active_nr) ||
30ca07a2 263 close(newfd) || commit_locked_index(&lock_file))
0d781539
LT
264 die("Unable to write new index file");
265 }
266
267 return 0;
268}