]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-add.c
builtin-add.c: restructure the code for maintainability
[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"
58680165 11#include "run-command.h"
5c46f754 12#include "parse-options.h"
0d781539 13
5c46f754 14static const char * const builtin_add_usage[] = {
1b1dd23f 15 "git add [options] [--] <filepattern>...",
5c46f754
KH
16 NULL
17};
b63e9950 18static int patch_interactive = 0, add_interactive = 0;
93c44d49 19static int take_worktree_changes;
896bdfa2 20
0d781539
LT
21static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
22{
f2593398
LT
23 char *seen;
24 int i, specs;
0d781539
LT
25 struct dir_entry **src, **dst;
26
f2593398
LT
27 for (specs = 0; pathspec[specs]; specs++)
28 /* nothing */;
28f75818 29 seen = xcalloc(specs, 1);
f2593398 30
0d781539
LT
31 src = dst = dir->entries;
32 i = dir->nr;
33 while (--i >= 0) {
34 struct dir_entry *entry = *src++;
4d06f8ac
JH
35 if (match_pathspec(pathspec, entry->name, entry->len,
36 prefix, seen))
37 *dst++ = entry;
0d781539
LT
38 }
39 dir->nr = dst - dir->entries;
f2593398
LT
40
41 for (i = 0; i < specs; i++) {
e96980ef
JK
42 if (!seen[i] && !file_exists(pathspec[i]))
43 die("pathspec '%s' did not match any files",
44 pathspec[i]);
f2593398 45 }
399f0a8e 46 free(seen);
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;
039bc64e 59 setup_standard_excludes(dir);
e96980ef 60 }
0d781539
LT
61
62 /*
63 * Calculate common prefix for the pathspec, and
64 * use that to optimize the directory walk
65 */
66 baselen = common_prefix(pathspec);
67 path = ".";
68 base = "";
182af834
PH
69 if (baselen)
70 path = base = xmemdupz(*pathspec, baselen);
0d781539
LT
71
72 /* Read the directory and prune it */
9fc42d60 73 read_directory(dir, path, base, baselen, pathspec);
0d781539
LT
74 if (pathspec)
75 prune_directory(dir, pathspec, baselen);
76}
77
d616813d
AJ
78static void refresh(int verbose, const char **pathspec)
79{
80 char *seen;
81 int i, specs;
82
83 for (specs = 0; pathspec[specs]; specs++)
84 /* nothing */;
85 seen = xcalloc(specs, 1);
d14e7407
JH
86 refresh_index(&the_index, verbose ? REFRESH_SAY_CHANGED : REFRESH_QUIET,
87 pathspec, seen);
d616813d
AJ
88 for (i = 0; i < specs; i++) {
89 if (!seen[i])
90 die("pathspec '%s' did not match any files", pathspec[i]);
91 }
399f0a8e 92 free(seen);
d616813d
AJ
93}
94
3f061887
JH
95static const char **validate_pathspec(int argc, const char **argv, const char *prefix)
96{
97 const char **pathspec = get_pathspec(prefix, argv);
98
99 return pathspec;
100}
101
102int interactive_add(int argc, const char **argv, const char *prefix)
58680165 103{
b63e9950 104 int status, ac;
3f061887
JH
105 const char **args;
106 const char **pathspec = NULL;
107
108 if (argc) {
109 pathspec = validate_pathspec(argc, argv, prefix);
110 if (!pathspec)
111 return -1;
112 }
324ccbd6 113
b63e9950
WC
114 args = xcalloc(sizeof(const char *), (argc + 4));
115 ac = 0;
116 args[ac++] = "add--interactive";
117 if (patch_interactive)
118 args[ac++] = "--patch";
119 args[ac++] = "--";
120 if (argc) {
121 memcpy(&(args[ac]), pathspec, sizeof(const char *) * argc);
122 ac += argc;
123 }
124 args[ac] = NULL;
7c0ab445
WC
125
126 status = run_command_v_opt(args, RUN_GIT_CMD);
127 free(args);
128 return status;
58680165
KH
129}
130
021b6e45 131static struct lock_file lock_file;
0d781539 132
b39c53e6 133static const char ignore_error[] =
6a1ad325
JH
134"The following paths are ignored by one of your .gitignore files:\n";
135
5c46f754 136static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
3ba1f114 137static int ignore_add_errors, addremove;
5c46f754
KH
138
139static struct option builtin_add_options[] = {
140 OPT__DRY_RUN(&show_only),
141 OPT__VERBOSE(&verbose),
142 OPT_GROUP(""),
143 OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"),
b63e9950 144 OPT_BOOLEAN('p', "patch", &patch_interactive, "interactive patching"),
69c61c4f
SG
145 OPT_BOOLEAN('f', "force", &ignored_too, "allow adding otherwise ignored files"),
146 OPT_BOOLEAN('u', "update", &take_worktree_changes, "update tracked files"),
3ba1f114 147 OPT_BOOLEAN('A', "all", &addremove, "add all, noticing removal of tracked files"),
5c46f754 148 OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"),
984b83ef 149 OPT_BOOLEAN( 0 , "ignore-errors", &ignore_add_errors, "just skip files which cannot be added because of errors"),
5c46f754
KH
150 OPT_END(),
151};
152
9bd81e42 153static int add_config(const char *var, const char *value, void *cb)
dad25e4a
AR
154{
155 if (!strcasecmp(var, "add.ignore-errors")) {
156 ignore_add_errors = git_config_bool(var, value);
157 return 0;
158 }
9bd81e42 159 return git_default_config(var, value, cb);
dad25e4a
AR
160}
161
c972ec04
JH
162static int add_files(struct dir_struct *dir, int flags)
163{
164 int i, exit_status = 0;
165
166 if (dir->ignored_nr) {
167 fprintf(stderr, ignore_error);
168 for (i = 0; i < dir->ignored_nr; i++)
169 fprintf(stderr, "%s\n", dir->ignored[i]->name);
170 fprintf(stderr, "Use -f if you really want to add them.\n");
171 die("no files added");
172 }
173
174 for (i = 0; i < dir->nr; i++)
175 if (add_file_to_cache(dir->entries[i]->name, flags)) {
176 if (!ignore_add_errors)
177 die("adding files failed");
178 exit_status = 1;
179 }
180 return exit_status;
181}
182
a633fca0 183int cmd_add(int argc, const char **argv, const char *prefix)
0d781539 184{
7ae02a30 185 int exit_status = 0;
c972ec04 186 int newfd;
0d781539
LT
187 const char **pathspec;
188 struct dir_struct dir;
205ffa94 189 int flags;
c972ec04
JH
190 int add_new_files;
191 int require_pathspec;
5cde71d6 192
5c46f754
KH
193 argc = parse_options(argc, argv, builtin_add_options,
194 builtin_add_usage, 0);
b63e9950
WC
195 if (patch_interactive)
196 add_interactive = 1;
7c0ab445 197 if (add_interactive)
3f061887 198 exit(interactive_add(argc, argv, prefix));
0d781539 199
9bd81e42 200 git_config(add_config, NULL);
0d781539 201
3ba1f114
JH
202 if (addremove && take_worktree_changes)
203 die("-A and -u are mutually incompatible");
204 if (addremove && !argc) {
205 static const char *here[2] = { ".", NULL };
206 argc = 1;
207 argv = here;
208 }
209
c972ec04
JH
210 add_new_files = !take_worktree_changes && !refresh_only;
211 require_pathspec = !take_worktree_changes;
212
30ca07a2 213 newfd = hold_locked_index(&lock_file, 1);
0d781539 214
205ffa94 215 flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
01665924
JH
216 (show_only ? ADD_CACHE_PRETEND : 0) |
217 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0));
205ffa94 218
c972ec04 219 if (require_pathspec && argc == 0) {
93b0d86a
JH
220 fprintf(stderr, "Nothing specified, nothing added.\n");
221 fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
222 return 0;
223 }
5c46f754 224 pathspec = get_pathspec(prefix, argv);
0d781539 225
c972ec04
JH
226 /*
227 * If we are adding new files, we need to scan the working
228 * tree to find the ones that match pathspecs; this needs
229 * to be done before we read the index.
230 */
231 if (add_new_files)
232 fill_directory(&dir, pathspec, ignored_too);
0d781539 233
366bfcb6
NP
234 if (read_cache() < 0)
235 die("index file corrupt");
236
c972ec04
JH
237 if (refresh_only) {
238 refresh(verbose, pathspec);
239 goto finish;
6a1ad325
JH
240 }
241
3ba1f114 242 if (take_worktree_changes || addremove)
c972ec04
JH
243 exit_status |= add_files_to_cache(prefix, pathspec, flags);
244
245 if (add_new_files)
246 exit_status |= add_files(&dir, flags);
0d781539 247
dfdac5d9 248 finish:
0d781539
LT
249 if (active_cache_changed) {
250 if (write_cache(newfd, active_cache, active_nr) ||
4ed7cd3a 251 commit_locked_index(&lock_file))
0d781539
LT
252 die("Unable to write new index file");
253 }
254
7ae02a30 255 return exit_status;
0d781539 256}