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>...",
23 static int take_worktree_changes;
24 static const char *excludes_file;
26 static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
30 struct dir_entry **src, **dst;
32 for (specs = 0; pathspec[specs]; specs++)
34 seen = xcalloc(specs, 1);
36 src = dst = dir->entries;
39 struct dir_entry *entry = *src++;
40 if (match_pathspec(pathspec, entry->name, entry->len,
44 dir->nr = dst - dir->entries;
46 for (i = 0; i < specs; i++) {
47 if (!seen[i] && !file_exists(pathspec[i]))
48 die("pathspec '%s' did not match any files",
54 static void fill_directory(struct dir_struct *dir, const char **pathspec,
57 const char *path, *base;
60 /* Set up the default git porcelain excludes */
61 memset(dir, 0, sizeof(*dir));
63 dir->collect_ignored = 1;
64 dir->exclude_per_dir = ".gitignore";
65 path = git_path("info/exclude");
66 if (!access(path, R_OK))
67 add_excludes_from_file(dir, path);
68 if (excludes_file != NULL && !access(excludes_file, R_OK))
69 add_excludes_from_file(dir, excludes_file);
73 * Calculate common prefix for the pathspec, and
74 * use that to optimize the directory walk
76 baselen = common_prefix(pathspec);
80 path = base = xmemdupz(*pathspec, baselen);
82 /* Read the directory and prune it */
83 read_directory(dir, path, base, baselen, pathspec);
85 prune_directory(dir, pathspec, baselen);
88 static void update_callback(struct diff_queue_struct *q,
89 struct diff_options *opt, void *cbdata)
93 verbose = *((int *)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 add_file_to_cache(path, verbose);
105 case DIFF_STATUS_DELETED:
106 remove_file_from_cache(path);
108 printf("remove '%s'\n", path);
114 void add_files_to_cache(int verbose, const char *prefix, const char **files)
117 init_revisions(&rev, prefix);
118 setup_revisions(0, NULL, &rev, NULL);
119 rev.prune_data = get_pathspec(prefix, files);
120 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
121 rev.diffopt.format_callback = update_callback;
122 rev.diffopt.format_callback_data = &verbose;
123 run_diff_files(&rev, 0);
126 static void refresh(int verbose, const char **pathspec)
131 for (specs = 0; pathspec[specs]; specs++)
133 seen = xcalloc(specs, 1);
134 if (read_cache() < 0)
135 die("index file corrupt");
136 refresh_index(&the_index, verbose ? 0 : REFRESH_QUIET, pathspec, seen);
137 for (i = 0; i < specs; i++) {
139 die("pathspec '%s' did not match any files", pathspec[i]);
144 static int git_add_config(const char *var, const char *value)
146 if (!strcmp(var, "core.excludesfile")) {
148 die("core.excludesfile without value");
149 excludes_file = xstrdup(value);
153 return git_default_config(var, value);
156 int interactive_add(void)
158 const char *argv[2] = { "add--interactive", NULL };
160 return run_command_v_opt(argv, RUN_GIT_CMD);
163 static struct lock_file lock_file;
165 static const char ignore_error[] =
166 "The following paths are ignored by one of your .gitignore files:\n";
168 static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
169 static int add_interactive = 0;
171 static struct option builtin_add_options[] = {
172 OPT__DRY_RUN(&show_only),
173 OPT__VERBOSE(&verbose),
175 OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"),
176 OPT_BOOLEAN('f', NULL, &ignored_too, "allow adding otherwise ignored files"),
177 OPT_BOOLEAN('u', NULL, &take_worktree_changes, "update tracked files"),
178 OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"),
182 int cmd_add(int argc, const char **argv, const char *prefix)
184 int i, newfd, orig_argc = argc;
185 const char **pathspec;
186 struct dir_struct dir;
188 argc = parse_options(argc, argv, builtin_add_options,
189 builtin_add_usage, 0);
190 if (add_interactive) {
191 if (add_interactive != 1 || orig_argc != 2)
192 die("add --interactive does not take any parameters");
193 exit(interactive_add());
196 git_config(git_add_config);
198 newfd = hold_locked_index(&lock_file, 1);
200 if (take_worktree_changes) {
201 if (read_cache() < 0)
202 die("index file corrupt");
203 add_files_to_cache(verbose, prefix, argv);
208 fprintf(stderr, "Nothing specified, nothing added.\n");
209 fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
212 pathspec = get_pathspec(prefix, argv);
215 refresh(verbose, pathspec);
219 fill_directory(&dir, pathspec, ignored_too);
222 const char *sep = "", *eof = "";
223 for (i = 0; i < dir.nr; i++) {
224 printf("%s%s", sep, dir.entries[i]->name);
232 if (read_cache() < 0)
233 die("index file corrupt");
235 if (dir.ignored_nr) {
236 fprintf(stderr, ignore_error);
237 for (i = 0; i < dir.ignored_nr; i++) {
238 fprintf(stderr, "%s\n", dir.ignored[i]->name);
240 fprintf(stderr, "Use -f if you really want to add them.\n");
241 die("no files added");
244 for (i = 0; i < dir.nr; i++)
245 add_file_to_cache(dir.entries[i]->name, verbose);
248 if (active_cache_changed) {
249 if (write_cache(newfd, active_cache, active_nr) ||
250 close(newfd) || commit_locked_index(&lock_file))
251 die("Unable to write new index file");