2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
10 #include "cache-tree.h"
11 #include "run-command.h"
12 #include "parse-options.h"
14 static const char * const builtin_add_usage
[] = {
15 "git add [options] [--] <filepattern>...",
18 static int patch_interactive
= 0, add_interactive
= 0;
19 static int take_worktree_changes
;
21 static void prune_directory(struct dir_struct
*dir
, const char **pathspec
, int prefix
)
25 struct dir_entry
**src
, **dst
;
27 for (specs
= 0; pathspec
[specs
]; specs
++)
29 seen
= xcalloc(specs
, 1);
31 src
= dst
= dir
->entries
;
34 struct dir_entry
*entry
= *src
++;
35 if (match_pathspec(pathspec
, entry
->name
, entry
->len
,
39 dir
->nr
= dst
- dir
->entries
;
41 for (i
= 0; i
< specs
; i
++) {
42 if (!seen
[i
] && !file_exists(pathspec
[i
]))
43 die("pathspec '%s' did not match any files",
49 static void fill_directory(struct dir_struct
*dir
, const char **pathspec
,
52 const char *path
, *base
;
55 /* Set up the default git porcelain excludes */
56 memset(dir
, 0, sizeof(*dir
));
58 dir
->collect_ignored
= 1;
59 setup_standard_excludes(dir
);
63 * Calculate common prefix for the pathspec, and
64 * use that to optimize the directory walk
66 baselen
= common_prefix(pathspec
);
70 path
= base
= xmemdupz(*pathspec
, baselen
);
72 /* Read the directory and prune it */
73 read_directory(dir
, path
, base
, baselen
, pathspec
);
75 prune_directory(dir
, pathspec
, baselen
);
78 static void refresh(int verbose
, const char **pathspec
)
83 for (specs
= 0; pathspec
[specs
]; specs
++)
85 seen
= xcalloc(specs
, 1);
86 refresh_index(&the_index
, verbose
? REFRESH_SAY_CHANGED
: REFRESH_QUIET
,
88 for (i
= 0; i
< specs
; i
++) {
90 die("pathspec '%s' did not match any files", pathspec
[i
]);
95 static const char **validate_pathspec(int argc
, const char **argv
, const char *prefix
)
97 const char **pathspec
= get_pathspec(prefix
, argv
);
102 int interactive_add(int argc
, const char **argv
, const char *prefix
)
106 const char **pathspec
= NULL
;
109 pathspec
= validate_pathspec(argc
, argv
, prefix
);
114 args
= xcalloc(sizeof(const char *), (argc
+ 4));
116 args
[ac
++] = "add--interactive";
117 if (patch_interactive
)
118 args
[ac
++] = "--patch";
121 memcpy(&(args
[ac
]), pathspec
, sizeof(const char *) * argc
);
126 status
= run_command_v_opt(args
, RUN_GIT_CMD
);
131 static struct lock_file lock_file
;
133 static const char ignore_error
[] =
134 "The following paths are ignored by one of your .gitignore files:\n";
136 static int verbose
= 0, show_only
= 0, ignored_too
= 0, refresh_only
= 0;
137 static int ignore_add_errors
, addremove
;
139 static struct option builtin_add_options
[] = {
140 OPT__DRY_RUN(&show_only
),
141 OPT__VERBOSE(&verbose
),
143 OPT_BOOLEAN('i', "interactive", &add_interactive
, "interactive picking"),
144 OPT_BOOLEAN('p', "patch", &patch_interactive
, "interactive patching"),
145 OPT_BOOLEAN('f', "force", &ignored_too
, "allow adding otherwise ignored files"),
146 OPT_BOOLEAN('u', "update", &take_worktree_changes
, "update tracked files"),
147 OPT_BOOLEAN('A', "all", &addremove
, "add all, noticing removal of tracked files"),
148 OPT_BOOLEAN( 0 , "refresh", &refresh_only
, "don't add, only refresh the index"),
149 OPT_BOOLEAN( 0 , "ignore-errors", &ignore_add_errors
, "just skip files which cannot be added because of errors"),
153 static int add_config(const char *var
, const char *value
, void *cb
)
155 if (!strcasecmp(var
, "add.ignore-errors")) {
156 ignore_add_errors
= git_config_bool(var
, value
);
159 return git_default_config(var
, value
, cb
);
162 static int add_files(struct dir_struct
*dir
, int flags
)
164 int i
, exit_status
= 0;
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");
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");
183 int cmd_add(int argc
, const char **argv
, const char *prefix
)
187 const char **pathspec
;
188 struct dir_struct dir
;
191 int require_pathspec
;
193 argc
= parse_options(argc
, argv
, builtin_add_options
,
194 builtin_add_usage
, 0);
195 if (patch_interactive
)
198 exit(interactive_add(argc
, argv
, prefix
));
200 git_config(add_config
, NULL
);
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
};
210 add_new_files
= !take_worktree_changes
&& !refresh_only
;
211 require_pathspec
= !take_worktree_changes
;
213 newfd
= hold_locked_index(&lock_file
, 1);
215 flags
= ((verbose
? ADD_CACHE_VERBOSE
: 0) |
216 (show_only
? ADD_CACHE_PRETEND
: 0) |
217 (ignore_add_errors
? ADD_CACHE_IGNORE_ERRORS
: 0));
219 if (require_pathspec
&& argc
== 0) {
220 fprintf(stderr
, "Nothing specified, nothing added.\n");
221 fprintf(stderr
, "Maybe you wanted to say 'git add .'?\n");
224 pathspec
= get_pathspec(prefix
, argv
);
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.
232 fill_directory(&dir
, pathspec
, ignored_too
);
234 if (read_cache() < 0)
235 die("index file corrupt");
238 refresh(verbose
, pathspec
);
242 if (take_worktree_changes
|| addremove
)
243 exit_status
|= add_files_to_cache(prefix
, pathspec
, flags
);
246 exit_status
|= add_files(&dir
, flags
);
249 if (active_cache_changed
) {
250 if (write_cache(newfd
, active_cache
, active_nr
) ||
251 commit_locked_index(&lock_file
))
252 die("Unable to write new index file");