Fixed a command line option type for builtin-fsck.c
[git/git-svn.git] / builtin-add.c
blobf61681c3ae186e1a6f96c5f7d4d6b978d0de5ddb
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;
24 static const char *excludes_file;
26 static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
28 char *seen;
29 int i, specs;
30 struct dir_entry **src, **dst;
32 for (specs = 0; pathspec[specs]; specs++)
33 /* nothing */;
34 seen = xcalloc(specs, 1);
36 src = dst = dir->entries;
37 i = dir->nr;
38 while (--i >= 0) {
39 struct dir_entry *entry = *src++;
40 if (match_pathspec(pathspec, entry->name, entry->len,
41 prefix, seen))
42 *dst++ = entry;
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",
49 pathspec[i]);
51 free(seen);
54 static void fill_directory(struct dir_struct *dir, const char **pathspec,
55 int ignored_too)
57 const char *path, *base;
58 int baselen;
60 /* Set up the default git porcelain excludes */
61 memset(dir, 0, sizeof(*dir));
62 if (!ignored_too) {
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);
77 path = ".";
78 base = "";
79 if (baselen)
80 path = base = xmemdupz(*pathspec, baselen);
82 /* Read the directory and prune it */
83 read_directory(dir, path, base, baselen, pathspec);
84 if (pathspec)
85 prune_directory(dir, pathspec, baselen);
88 static void update_callback(struct diff_queue_struct *q,
89 struct diff_options *opt, void *cbdata)
91 int i, verbose;
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;
97 switch (p->status) {
98 default:
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);
104 break;
105 case DIFF_STATUS_DELETED:
106 remove_file_from_cache(path);
107 if (verbose)
108 printf("remove '%s'\n", path);
109 break;
114 static void update(int verbose, const char *prefix, const char **files)
116 struct rev_info rev;
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 if (read_cache() < 0)
124 die("index file corrupt");
125 run_diff_files(&rev, 0);
128 static void refresh(int verbose, const char **pathspec)
130 char *seen;
131 int i, specs;
133 for (specs = 0; pathspec[specs]; specs++)
134 /* nothing */;
135 seen = xcalloc(specs, 1);
136 if (read_cache() < 0)
137 die("index file corrupt");
138 refresh_index(&the_index, verbose ? 0 : REFRESH_QUIET, pathspec, seen);
139 for (i = 0; i < specs; i++) {
140 if (!seen[i])
141 die("pathspec '%s' did not match any files", pathspec[i]);
143 free(seen);
146 static int git_add_config(const char *var, const char *value)
148 if (!strcmp(var, "core.excludesfile")) {
149 if (!value)
150 die("core.excludesfile without value");
151 excludes_file = xstrdup(value);
152 return 0;
155 return git_default_config(var, value);
158 static struct lock_file lock_file;
160 static const char ignore_error[] =
161 "The following paths are ignored by one of your .gitignore files:\n";
163 static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
164 static int add_interactive = 0;
166 static struct option builtin_add_options[] = {
167 OPT__DRY_RUN(&show_only),
168 OPT__VERBOSE(&verbose),
169 OPT_GROUP(""),
170 OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"),
171 OPT_BOOLEAN('f', NULL, &ignored_too, "allow adding otherwise ignored files"),
172 OPT_BOOLEAN('u', NULL, &take_worktree_changes, "update tracked files"),
173 OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"),
174 OPT_END(),
177 int cmd_add(int argc, const char **argv, const char *prefix)
179 int i, newfd, orig_argc = argc;
180 const char **pathspec;
181 struct dir_struct dir;
183 argc = parse_options(argc, argv, builtin_add_options,
184 builtin_add_usage, 0);
185 if (add_interactive) {
186 const char *args[] = { "add--interactive", NULL };
188 if (add_interactive != 1 || orig_argc != 2)
189 die("add --interactive does not take any parameters");
190 execv_git_cmd(args);
191 exit(1);
194 git_config(git_add_config);
196 newfd = hold_locked_index(&lock_file, 1);
198 if (take_worktree_changes) {
199 update(verbose, prefix, argv);
200 goto finish;
203 if (argc == 0) {
204 fprintf(stderr, "Nothing specified, nothing added.\n");
205 fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
206 return 0;
208 pathspec = get_pathspec(prefix, argv);
210 if (refresh_only) {
211 refresh(verbose, pathspec);
212 goto finish;
215 fill_directory(&dir, pathspec, ignored_too);
217 if (show_only) {
218 const char *sep = "", *eof = "";
219 for (i = 0; i < dir.nr; i++) {
220 printf("%s%s", sep, dir.entries[i]->name);
221 sep = " ";
222 eof = "\n";
224 fputs(eof, stdout);
225 return 0;
228 if (read_cache() < 0)
229 die("index file corrupt");
231 if (dir.ignored_nr) {
232 fprintf(stderr, ignore_error);
233 for (i = 0; i < dir.ignored_nr; i++) {
234 fprintf(stderr, "%s\n", dir.ignored[i]->name);
236 fprintf(stderr, "Use -f if you really want to add them.\n");
237 die("no files added");
240 for (i = 0; i < dir.nr; i++)
241 add_file_to_cache(dir.entries[i]->name, verbose);
243 finish:
244 if (active_cache_changed) {
245 if (write_cache(newfd, active_cache, active_nr) ||
246 close(newfd) || commit_locked_index(&lock_file))
247 die("Unable to write new index file");
250 return 0;