Merge branch 'kh/commit' into wc/add-i
[git/peff.git] / builtin-add.c
blobdd895dfb1d35f7e90d6529821e284d73e912d6d6
1 /*
2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
5 */
6 #include "cache.h"
7 #include "builtin.h"
8 #include "dir.h"
9 #include "exec_cmd.h"
10 #include "cache-tree.h"
11 #include "diff.h"
12 #include "diffcore.h"
13 #include "commit.h"
14 #include "revision.h"
15 #include "run-command.h"
16 #include "parse-options.h"
18 static const char * const builtin_add_usage[] = {
19 "git-add [options] [--] <filepattern>...",
20 NULL
23 static int take_worktree_changes;
25 static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
27 char *seen;
28 int i, specs;
29 struct dir_entry **src, **dst;
31 for (specs = 0; pathspec[specs]; specs++)
32 /* nothing */;
33 seen = xcalloc(specs, 1);
35 src = dst = dir->entries;
36 i = dir->nr;
37 while (--i >= 0) {
38 struct dir_entry *entry = *src++;
39 if (match_pathspec(pathspec, entry->name, entry->len,
40 prefix, seen))
41 *dst++ = entry;
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",
48 pathspec[i]);
50 free(seen);
53 static void fill_directory(struct dir_struct *dir, const char **pathspec,
54 int ignored_too)
56 const char *path, *base;
57 int baselen;
59 /* Set up the default git porcelain excludes */
60 memset(dir, 0, sizeof(*dir));
61 if (!ignored_too) {
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);
71 path = ".";
72 base = "";
73 if (baselen)
74 path = base = xmemdupz(*pathspec, baselen);
76 /* Read the directory and prune it */
77 read_directory(dir, path, base, baselen, pathspec);
78 if (pathspec)
79 prune_directory(dir, pathspec, baselen);
82 static void update_callback(struct diff_queue_struct *q,
83 struct diff_options *opt, void *cbdata)
85 int i, verbose;
87 verbose = *((int *)cbdata);
88 for (i = 0; i < q->nr; i++) {
89 struct diff_filepair *p = q->queue[i];
90 const char *path = p->one->path;
91 switch (p->status) {
92 default:
93 die("unexpected diff status %c", p->status);
94 case DIFF_STATUS_UNMERGED:
95 case DIFF_STATUS_MODIFIED:
96 case DIFF_STATUS_TYPE_CHANGED:
97 add_file_to_cache(path, verbose);
98 break;
99 case DIFF_STATUS_DELETED:
100 remove_file_from_cache(path);
101 if (verbose)
102 printf("remove '%s'\n", path);
103 break;
108 void add_files_to_cache(int verbose, const char *prefix, const char **pathspec)
110 struct rev_info rev;
111 init_revisions(&rev, prefix);
112 setup_revisions(0, NULL, &rev, NULL);
113 rev.prune_data = pathspec;
114 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
115 rev.diffopt.format_callback = update_callback;
116 rev.diffopt.format_callback_data = &verbose;
117 run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
120 static void refresh(int verbose, const char **pathspec)
122 char *seen;
123 int i, specs;
125 for (specs = 0; pathspec[specs]; specs++)
126 /* nothing */;
127 seen = xcalloc(specs, 1);
128 if (read_cache() < 0)
129 die("index file corrupt");
130 refresh_index(&the_index, verbose ? 0 : REFRESH_QUIET, pathspec, seen);
131 for (i = 0; i < specs; i++) {
132 if (!seen[i])
133 die("pathspec '%s' did not match any files", pathspec[i]);
135 free(seen);
138 int interactive_add(int argc, const char **argv)
140 int status;
141 const char **args = xmalloc(sizeof(const char *) * (argc + 1));
142 args[0] = "add--interactive";
143 memcpy((void *)args + sizeof(const char *), argv, sizeof(const char *) * argc);
144 args[argc + 1] = NULL;
146 status = run_command_v_opt(args, RUN_GIT_CMD);
147 free(args);
148 return status;
151 static struct lock_file lock_file;
153 static const char ignore_error[] =
154 "The following paths are ignored by one of your .gitignore files:\n";
156 static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
157 static int add_interactive = 0;
159 static struct option builtin_add_options[] = {
160 OPT__DRY_RUN(&show_only),
161 OPT__VERBOSE(&verbose),
162 OPT_GROUP(""),
163 OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"),
164 OPT_BOOLEAN('f', NULL, &ignored_too, "allow adding otherwise ignored files"),
165 OPT_BOOLEAN('u', NULL, &take_worktree_changes, "update tracked files"),
166 OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"),
167 OPT_END(),
170 int cmd_add(int argc, const char **argv, const char *prefix)
172 int i, newfd;
173 const char **pathspec;
174 struct dir_struct dir;
176 argc = parse_options(argc, argv, builtin_add_options,
177 builtin_add_usage, 0);
178 if (add_interactive)
179 exit(interactive_add(argc, argv));
181 git_config(git_default_config);
183 newfd = hold_locked_index(&lock_file, 1);
185 if (take_worktree_changes) {
186 const char **pathspec;
187 if (read_cache() < 0)
188 die("index file corrupt");
189 pathspec = get_pathspec(prefix, argv);
190 add_files_to_cache(verbose, prefix, pathspec);
191 goto finish;
194 if (argc == 0) {
195 fprintf(stderr, "Nothing specified, nothing added.\n");
196 fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
197 return 0;
199 pathspec = get_pathspec(prefix, argv);
201 if (refresh_only) {
202 refresh(verbose, pathspec);
203 goto finish;
206 fill_directory(&dir, pathspec, ignored_too);
208 if (show_only) {
209 const char *sep = "", *eof = "";
210 for (i = 0; i < dir.nr; i++) {
211 printf("%s%s", sep, dir.entries[i]->name);
212 sep = " ";
213 eof = "\n";
215 fputs(eof, stdout);
216 return 0;
219 if (read_cache() < 0)
220 die("index file corrupt");
222 if (dir.ignored_nr) {
223 fprintf(stderr, ignore_error);
224 for (i = 0; i < dir.ignored_nr; i++) {
225 fprintf(stderr, "%s\n", dir.ignored[i]->name);
227 fprintf(stderr, "Use -f if you really want to add them.\n");
228 die("no files added");
231 for (i = 0; i < dir.nr; i++)
232 add_file_to_cache(dir.entries[i]->name, verbose);
234 finish:
235 if (active_cache_changed) {
236 if (write_cache(newfd, active_cache, active_nr) ||
237 close(newfd) || commit_locked_index(&lock_file))
238 die("Unable to write new index file");
241 return 0;