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