2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
10 #include "cache-tree.h"
15 #include "run-command.h"
16 #include "parse-options.h"
18 static const char * const builtin_add_usage
[] = {
19 "git add [options] [--] <filepattern>...",
22 static int patch_interactive
= 0, add_interactive
= 0;
23 static int take_worktree_changes
;
25 static void prune_directory(struct dir_struct
*dir
, const char **pathspec
, int prefix
)
29 struct dir_entry
**src
, **dst
;
31 for (specs
= 0; pathspec
[specs
]; specs
++)
33 seen
= xcalloc(specs
, 1);
35 src
= dst
= dir
->entries
;
38 struct dir_entry
*entry
= *src
++;
39 if (match_pathspec(pathspec
, entry
->name
, entry
->len
,
43 dir
->nr
= dst
- dir
->entries
;
45 for (i
= 0; i
< specs
; i
++) {
46 if (!seen
[i
] && !file_exists(pathspec
[i
]))
47 die("pathspec '%s' did not match any files",
53 static void fill_directory(struct dir_struct
*dir
, const char **pathspec
,
56 const char *path
, *base
;
59 /* Set up the default git porcelain excludes */
60 memset(dir
, 0, sizeof(*dir
));
62 dir
->collect_ignored
= 1;
63 setup_standard_excludes(dir
);
67 * Calculate common prefix for the pathspec, and
68 * use that to optimize the directory walk
70 baselen
= common_prefix(pathspec
);
74 path
= base
= xmemdupz(*pathspec
, baselen
);
76 /* Read the directory and prune it */
77 read_directory(dir
, path
, base
, baselen
, pathspec
);
79 prune_directory(dir
, pathspec
, baselen
);
82 struct update_callback_data
88 static void update_callback(struct diff_queue_struct
*q
,
89 struct diff_options
*opt
, void *cbdata
)
92 struct update_callback_data
*data
= cbdata
;
94 for (i
= 0; i
< q
->nr
; i
++) {
95 struct diff_filepair
*p
= q
->queue
[i
];
96 const char *path
= p
->one
->path
;
99 die("unexpected diff status %c", p
->status
);
100 case DIFF_STATUS_UNMERGED
:
101 case DIFF_STATUS_MODIFIED
:
102 case DIFF_STATUS_TYPE_CHANGED
:
103 if (add_file_to_cache(path
, data
->flags
)) {
104 if (!(data
->flags
& ADD_CACHE_IGNORE_ERRORS
))
105 die("updating files failed");
109 case DIFF_STATUS_DELETED
:
110 if (!(data
->flags
& ADD_CACHE_PRETEND
))
111 remove_file_from_cache(path
);
112 if (data
->flags
& (ADD_CACHE_PRETEND
|ADD_CACHE_VERBOSE
))
113 printf("remove '%s'\n", path
);
119 int add_files_to_cache(const char *prefix
, const char **pathspec
, int flags
)
121 struct update_callback_data data
;
123 init_revisions(&rev
, prefix
);
124 setup_revisions(0, NULL
, &rev
, NULL
);
125 rev
.prune_data
= pathspec
;
126 rev
.diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
127 rev
.diffopt
.format_callback
= update_callback
;
130 rev
.diffopt
.format_callback_data
= &data
;
131 run_diff_files(&rev
, DIFF_RACY_IS_MODIFIED
);
132 return !!data
.add_errors
;
135 static void refresh(int verbose
, const char **pathspec
)
140 for (specs
= 0; pathspec
[specs
]; specs
++)
142 seen
= xcalloc(specs
, 1);
143 refresh_index(&the_index
, verbose
? 0 : REFRESH_QUIET
, pathspec
, seen
);
144 for (i
= 0; i
< specs
; i
++) {
146 die("pathspec '%s' did not match any files", pathspec
[i
]);
151 static const char **validate_pathspec(int argc
, const char **argv
, const char *prefix
)
153 const char **pathspec
= get_pathspec(prefix
, argv
);
158 int interactive_add(int argc
, const char **argv
, const char *prefix
)
162 const char **pathspec
= NULL
;
165 pathspec
= validate_pathspec(argc
, argv
, prefix
);
170 args
= xcalloc(sizeof(const char *), (argc
+ 4));
172 args
[ac
++] = "add--interactive";
173 if (patch_interactive
)
174 args
[ac
++] = "--patch";
177 memcpy(&(args
[ac
]), pathspec
, sizeof(const char *) * argc
);
182 status
= run_command_v_opt(args
, RUN_GIT_CMD
);
187 static struct lock_file lock_file
;
189 static const char ignore_error
[] =
190 "The following paths are ignored by one of your .gitignore files:\n";
192 static int verbose
= 0, show_only
= 0, ignored_too
= 0, refresh_only
= 0;
193 static int ignore_add_errors
;
195 static struct option builtin_add_options
[] = {
196 OPT__DRY_RUN(&show_only
),
197 OPT__VERBOSE(&verbose
),
199 OPT_BOOLEAN('i', "interactive", &add_interactive
, "interactive picking"),
200 OPT_BOOLEAN('p', "patch", &patch_interactive
, "interactive patching"),
201 OPT_BOOLEAN('f', "force", &ignored_too
, "allow adding otherwise ignored files"),
202 OPT_BOOLEAN('u', "update", &take_worktree_changes
, "update tracked files"),
203 OPT_BOOLEAN( 0 , "refresh", &refresh_only
, "don't add, only refresh the index"),
204 OPT_BOOLEAN( 0 , "ignore-errors", &ignore_add_errors
, "just skip files which cannot be added because of errors"),
208 static int add_config(const char *var
, const char *value
, void *cb
)
210 if (!strcasecmp(var
, "add.ignore-errors")) {
211 ignore_add_errors
= git_config_bool(var
, value
);
214 return git_default_config(var
, value
, cb
);
217 static int add_files(struct dir_struct
*dir
, int flags
)
219 int i
, exit_status
= 0;
221 if (dir
->ignored_nr
) {
222 fprintf(stderr
, ignore_error
);
223 for (i
= 0; i
< dir
->ignored_nr
; i
++)
224 fprintf(stderr
, "%s\n", dir
->ignored
[i
]->name
);
225 fprintf(stderr
, "Use -f if you really want to add them.\n");
226 die("no files added");
229 for (i
= 0; i
< dir
->nr
; i
++)
230 if (add_file_to_cache(dir
->entries
[i
]->name
, flags
)) {
231 if (!ignore_add_errors
)
232 die("adding files failed");
238 int cmd_add(int argc
, const char **argv
, const char *prefix
)
242 const char **pathspec
;
243 struct dir_struct dir
;
246 int require_pathspec
;
248 argc
= parse_options(argc
, argv
, builtin_add_options
,
249 builtin_add_usage
, 0);
250 if (patch_interactive
)
253 exit(interactive_add(argc
, argv
, prefix
));
255 git_config(add_config
, NULL
);
257 add_new_files
= !take_worktree_changes
&& !refresh_only
;
258 require_pathspec
= !take_worktree_changes
;
260 newfd
= hold_locked_index(&lock_file
, 1);
262 flags
= ((verbose
? ADD_CACHE_VERBOSE
: 0) |
263 (show_only
? ADD_CACHE_PRETEND
: 0) |
264 (ignore_add_errors
? ADD_CACHE_IGNORE_ERRORS
: 0));
266 if (require_pathspec
&& argc
== 0) {
267 fprintf(stderr
, "Nothing specified, nothing added.\n");
268 fprintf(stderr
, "Maybe you wanted to say 'git add .'?\n");
271 pathspec
= get_pathspec(prefix
, argv
);
274 * If we are adding new files, we need to scan the working
275 * tree to find the ones that match pathspecs; this needs
276 * to be done before we read the index.
279 fill_directory(&dir
, pathspec
, ignored_too
);
281 if (read_cache() < 0)
282 die("index file corrupt");
285 refresh(verbose
, pathspec
);
289 if (take_worktree_changes
)
290 exit_status
|= add_files_to_cache(prefix
, pathspec
, flags
);
293 exit_status
|= add_files(&dir
, flags
);
296 if (active_cache_changed
) {
297 if (write_cache(newfd
, active_cache
, active_nr
) ||
298 commit_locked_index(&lock_file
))
299 die("Unable to write new index file");