]> asedeno.scripts.mit.edu Git - git.git/blob - builtin-add.c
Port builtin-add.c to use the new option parser.
[git.git] / builtin-add.c
1 /*
2  * "git add" builtin command
3  *
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"
17
18 static const char * const builtin_add_usage[] = {
19         "git-add [options] [--] <filepattern>...",
20         NULL
21 };
22
23 static int take_worktree_changes;
24 static const char *excludes_file;
25
26 static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
27 {
28         char *seen;
29         int i, specs;
30         struct dir_entry **src, **dst;
31
32         for (specs = 0; pathspec[specs];  specs++)
33                 /* nothing */;
34         seen = xcalloc(specs, 1);
35
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;
43         }
44         dir->nr = dst - dir->entries;
45
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]);
50         }
51         free(seen);
52 }
53
54 static void fill_directory(struct dir_struct *dir, const char **pathspec,
55                 int ignored_too)
56 {
57         const char *path, *base;
58         int baselen;
59
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);
70         }
71
72         /*
73          * Calculate common prefix for the pathspec, and
74          * use that to optimize the directory walk
75          */
76         baselen = common_prefix(pathspec);
77         path = ".";
78         base = "";
79         if (baselen)
80                 path = base = xmemdupz(*pathspec, baselen);
81
82         /* Read the directory and prune it */
83         read_directory(dir, path, base, baselen, pathspec);
84         if (pathspec)
85                 prune_directory(dir, pathspec, baselen);
86 }
87
88 static void update_callback(struct diff_queue_struct *q,
89                             struct diff_options *opt, void *cbdata)
90 {
91         int i, verbose;
92
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;
110                 }
111         }
112 }
113
114 static void update(int verbose, const char *prefix, const char **files)
115 {
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);
126 }
127
128 static void refresh(int verbose, const char **pathspec)
129 {
130         char *seen;
131         int i, specs;
132
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]);
142         }
143         free(seen);
144 }
145
146 static int git_add_config(const char *var, const char *value)
147 {
148         if (!strcmp(var, "core.excludesfile")) {
149                 if (!value)
150                         die("core.excludesfile without value");
151                 excludes_file = xstrdup(value);
152                 return 0;
153         }
154
155         return git_default_config(var, value);
156 }
157
158 static struct lock_file lock_file;
159
160 static const char ignore_error[] =
161 "The following paths are ignored by one of your .gitignore files:\n";
162
163 static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
164 static int add_interactive = 0;
165
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(),
175 };
176
177 int cmd_add(int argc, const char **argv, const char *prefix)
178 {
179         int i, newfd, orig_argc = argc;
180         const char **pathspec;
181         struct dir_struct dir;
182
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 };
187
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);
192         }
193
194         git_config(git_add_config);
195
196         newfd = hold_locked_index(&lock_file, 1);
197
198         if (take_worktree_changes) {
199                 update(verbose, prefix, argv);
200                 goto finish;
201         }
202
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;
207         }
208         pathspec = get_pathspec(prefix, argv);
209
210         if (refresh_only) {
211                 refresh(verbose, pathspec);
212                 goto finish;
213         }
214
215         fill_directory(&dir, pathspec, ignored_too);
216
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";
223                 }
224                 fputs(eof, stdout);
225                 return 0;
226         }
227
228         if (read_cache() < 0)
229                 die("index file corrupt");
230
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);
235                 }
236                 fprintf(stderr, "Use -f if you really want to add them.\n");
237                 die("no files added");
238         }
239
240         for (i = 0; i < dir.nr; i++)
241                 add_file_to_cache(dir.entries[i]->name, verbose);
242
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");
248         }
249
250         return 0;
251 }