]>
Commit | Line | Data |
---|---|---|
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 | |
16 | static const char builtin_add_usage[] = | |
dfdac5d9 | 17 | "git-add [-n] [-v] [-f] [--interactive | -i] [-u] [--] <filepattern>..."; |
0d781539 | 18 | |
93c44d49 | 19 | static int take_worktree_changes; |
896bdfa2 JB |
20 | static const char *excludes_file; |
21 | ||
0d781539 LT |
22 | static 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 |
49 | static 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); | |
63 | if (!access(excludes_file, R_OK)) | |
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 = ""; | |
74 | if (baselen) { | |
75 | char *common = xmalloc(baselen + 1); | |
0d781539 LT |
76 | memcpy(common, *pathspec, baselen); |
77 | common[baselen] = 0; | |
78 | path = base = common; | |
79 | } | |
80 | ||
81 | /* Read the directory and prune it */ | |
9fc42d60 | 82 | read_directory(dir, path, base, baselen, pathspec); |
0d781539 LT |
83 | if (pathspec) |
84 | prune_directory(dir, pathspec, baselen); | |
85 | } | |
86 | ||
dfdac5d9 JH |
87 | static void update_callback(struct diff_queue_struct *q, |
88 | struct diff_options *opt, void *cbdata) | |
89 | { | |
90 | int i, verbose; | |
91 | ||
92 | verbose = *((int *)cbdata); | |
93 | for (i = 0; i < q->nr; i++) { | |
94 | struct diff_filepair *p = q->queue[i]; | |
95 | const char *path = p->one->path; | |
96 | switch (p->status) { | |
97 | default: | |
98 | die("unexpacted diff status %c", p->status); | |
99 | case DIFF_STATUS_UNMERGED: | |
100 | case DIFF_STATUS_MODIFIED: | |
101 | add_file_to_cache(path, verbose); | |
102 | break; | |
103 | case DIFF_STATUS_DELETED: | |
104 | remove_file_from_cache(path); | |
105 | if (verbose) | |
106 | printf("remove '%s'\n", path); | |
107 | break; | |
108 | } | |
109 | } | |
110 | } | |
111 | ||
93c44d49 | 112 | static void update(int verbose, const char **files) |
dfdac5d9 JH |
113 | { |
114 | struct rev_info rev; | |
115 | init_revisions(&rev, ""); | |
116 | setup_revisions(0, NULL, &rev, NULL); | |
93c44d49 | 117 | rev.prune_data = get_pathspec(rev.prefix, files); |
dfdac5d9 JH |
118 | rev.diffopt.output_format = DIFF_FORMAT_CALLBACK; |
119 | rev.diffopt.format_callback = update_callback; | |
120 | rev.diffopt.format_callback_data = &verbose; | |
121 | if (read_cache() < 0) | |
122 | die("index file corrupt"); | |
123 | run_diff_files(&rev, 0); | |
124 | } | |
125 | ||
896bdfa2 JB |
126 | static int git_add_config(const char *var, const char *value) |
127 | { | |
128 | if (!strcmp(var, "core.excludesfile")) { | |
129 | if (!value) | |
130 | die("core.excludesfile without value"); | |
131 | excludes_file = xstrdup(value); | |
132 | return 0; | |
133 | } | |
134 | ||
135 | return git_default_config(var, value); | |
136 | } | |
137 | ||
021b6e45 | 138 | static struct lock_file lock_file; |
0d781539 | 139 | |
6a1ad325 JH |
140 | static const char ignore_warning[] = |
141 | "The following paths are ignored by one of your .gitignore files:\n"; | |
142 | ||
a633fca0 | 143 | int cmd_add(int argc, const char **argv, const char *prefix) |
0d781539 LT |
144 | { |
145 | int i, newfd; | |
6a1ad325 | 146 | int verbose = 0, show_only = 0, ignored_too = 0; |
0d781539 LT |
147 | const char **pathspec; |
148 | struct dir_struct dir; | |
5cde71d6 JH |
149 | int add_interactive = 0; |
150 | ||
151 | for (i = 1; i < argc; i++) { | |
df59afe3 JH |
152 | if (!strcmp("--interactive", argv[i]) || |
153 | !strcmp("-i", argv[i])) | |
5cde71d6 JH |
154 | add_interactive++; |
155 | } | |
156 | if (add_interactive) { | |
157 | const char *args[] = { "add--interactive", NULL }; | |
158 | ||
159 | if (add_interactive != 1 || argc != 2) | |
160 | die("add --interactive does not take any parameters"); | |
161 | execv_git_cmd(args); | |
162 | exit(1); | |
163 | } | |
0d781539 | 164 | |
896bdfa2 | 165 | git_config(git_add_config); |
0d781539 | 166 | |
30ca07a2 | 167 | newfd = hold_locked_index(&lock_file, 1); |
0d781539 | 168 | |
0d781539 LT |
169 | for (i = 1; i < argc; i++) { |
170 | const char *arg = argv[i]; | |
171 | ||
172 | if (arg[0] != '-') | |
173 | break; | |
174 | if (!strcmp(arg, "--")) { | |
175 | i++; | |
176 | break; | |
177 | } | |
178 | if (!strcmp(arg, "-n")) { | |
179 | show_only = 1; | |
180 | continue; | |
181 | } | |
6a1ad325 JH |
182 | if (!strcmp(arg, "-f")) { |
183 | ignored_too = 1; | |
184 | continue; | |
185 | } | |
0d781539 LT |
186 | if (!strcmp(arg, "-v")) { |
187 | verbose = 1; | |
188 | continue; | |
189 | } | |
dfdac5d9 | 190 | if (!strcmp(arg, "-u")) { |
93c44d49 | 191 | take_worktree_changes = 1; |
dfdac5d9 JH |
192 | continue; |
193 | } | |
8cdf3364 | 194 | usage(builtin_add_usage); |
0d781539 | 195 | } |
dfdac5d9 | 196 | |
93c44d49 JK |
197 | if (take_worktree_changes) { |
198 | update(verbose, argv + i); | |
dfdac5d9 JH |
199 | goto finish; |
200 | } | |
201 | ||
93b0d86a JH |
202 | if (argc <= i) { |
203 | fprintf(stderr, "Nothing specified, nothing added.\n"); | |
204 | fprintf(stderr, "Maybe you wanted to say 'git add .'?\n"); | |
205 | return 0; | |
206 | } | |
0d781539 LT |
207 | pathspec = get_pathspec(prefix, argv + i); |
208 | ||
e96980ef | 209 | fill_directory(&dir, pathspec, ignored_too); |
0d781539 LT |
210 | |
211 | if (show_only) { | |
212 | const char *sep = "", *eof = ""; | |
213 | for (i = 0; i < dir.nr; i++) { | |
214 | printf("%s%s", sep, dir.entries[i]->name); | |
215 | sep = " "; | |
216 | eof = "\n"; | |
217 | } | |
218 | fputs(eof, stdout); | |
219 | return 0; | |
220 | } | |
221 | ||
366bfcb6 NP |
222 | if (read_cache() < 0) |
223 | die("index file corrupt"); | |
224 | ||
e96980ef JK |
225 | if (dir.ignored_nr) { |
226 | fprintf(stderr, ignore_warning); | |
227 | for (i = 0; i < dir.ignored_nr; i++) { | |
228 | fprintf(stderr, "%s\n", dir.ignored[i]->name); | |
6a1ad325 | 229 | } |
e96980ef JK |
230 | fprintf(stderr, "Use -f if you really want to add them.\n"); |
231 | exit(1); | |
6a1ad325 JH |
232 | } |
233 | ||
0d781539 | 234 | for (i = 0; i < dir.nr; i++) |
fd1c3bf0 | 235 | add_file_to_cache(dir.entries[i]->name, verbose); |
0d781539 | 236 | |
dfdac5d9 | 237 | finish: |
0d781539 LT |
238 | if (active_cache_changed) { |
239 | if (write_cache(newfd, active_cache, active_nr) || | |
30ca07a2 | 240 | close(newfd) || commit_locked_index(&lock_file)) |
0d781539 LT |
241 | die("Unable to write new index file"); |
242 | } | |
243 | ||
244 | return 0; | |
245 | } |