Merge branch 'nd/maint-work-tree-fix' into maint
[alt-git.git] / builtin-add.c
blob4fc9d6f7ccdd7b989d75ae6e24df997c4deb3505
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"
16 static const char builtin_add_usage[] =
17 "git-add [-n] [-v] [-f] [--interactive | -i] [-u] [--refresh] [--] <filepattern>...";
19 static int take_worktree_changes;
21 static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
23 char *seen;
24 int i, specs;
25 struct dir_entry **src, **dst;
27 for (specs = 0; pathspec[specs]; specs++)
28 /* nothing */;
29 seen = xcalloc(specs, 1);
31 src = dst = dir->entries;
32 i = dir->nr;
33 while (--i >= 0) {
34 struct dir_entry *entry = *src++;
35 if (match_pathspec(pathspec, entry->name, entry->len,
36 prefix, seen))
37 *dst++ = entry;
39 dir->nr = dst - dir->entries;
41 for (i = 0; i < specs; i++) {
42 if (!seen[i] && !file_exists(pathspec[i]))
43 die("pathspec '%s' did not match any files",
44 pathspec[i]);
46 free(seen);
49 static void fill_directory(struct dir_struct *dir, const char **pathspec,
50 int ignored_too)
52 const char *path, *base;
53 int baselen;
55 /* Set up the default git porcelain excludes */
56 memset(dir, 0, sizeof(*dir));
57 if (!ignored_too) {
58 dir->collect_ignored = 1;
59 setup_standard_excludes(dir);
63 * Calculate common prefix for the pathspec, and
64 * use that to optimize the directory walk
66 baselen = common_prefix(pathspec);
67 path = ".";
68 base = "";
69 if (baselen) {
70 char *common = xmalloc(baselen + 1);
71 memcpy(common, *pathspec, baselen);
72 common[baselen] = 0;
73 path = base = common;
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 cache_tree_invalidate_path(active_cache_tree, path);
102 if (verbose)
103 printf("remove '%s'\n", path);
104 break;
109 static void update(int verbose, const char *prefix, const char **files)
111 struct rev_info rev;
112 init_revisions(&rev, prefix);
113 setup_revisions(0, NULL, &rev, NULL);
114 rev.prune_data = get_pathspec(prefix, files);
115 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
116 rev.diffopt.format_callback = update_callback;
117 rev.diffopt.format_callback_data = &verbose;
118 if (read_cache() < 0)
119 die("index file corrupt");
120 run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
123 static void refresh(int verbose, const char **pathspec)
125 char *seen;
126 int i, specs;
128 for (specs = 0; pathspec[specs]; specs++)
129 /* nothing */;
130 seen = xcalloc(specs, 1);
131 if (read_cache() < 0)
132 die("index file corrupt");
133 refresh_index(&the_index, verbose ? 0 : REFRESH_QUIET, pathspec, seen);
134 for (i = 0; i < specs; i++) {
135 if (!seen[i])
136 die("pathspec '%s' did not match any files", pathspec[i]);
138 free(seen);
141 static struct lock_file lock_file;
143 static const char ignore_error[] =
144 "The following paths are ignored by one of your .gitignore files:\n";
146 int cmd_add(int argc, const char **argv, const char *prefix)
148 int i, newfd;
149 int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
150 const char **pathspec;
151 struct dir_struct dir;
152 int add_interactive = 0;
154 for (i = 1; i < argc; i++) {
155 if (!strcmp("--interactive", argv[i]) ||
156 !strcmp("-i", argv[i]))
157 add_interactive++;
159 if (add_interactive) {
160 const char *args[] = { "add--interactive", NULL };
162 if (add_interactive != 1 || argc != 2)
163 die("add --interactive does not take any parameters");
164 execv_git_cmd(args);
165 exit(1);
168 git_config(git_default_config);
170 newfd = hold_locked_index(&lock_file, 1);
172 for (i = 1; i < argc; i++) {
173 const char *arg = argv[i];
175 if (arg[0] != '-')
176 break;
177 if (!strcmp(arg, "--")) {
178 i++;
179 break;
181 if (!strcmp(arg, "-n")) {
182 show_only = 1;
183 continue;
185 if (!strcmp(arg, "-f")) {
186 ignored_too = 1;
187 continue;
189 if (!strcmp(arg, "-v")) {
190 verbose = 1;
191 continue;
193 if (!strcmp(arg, "-u")) {
194 take_worktree_changes = 1;
195 continue;
197 if (!strcmp(arg, "--refresh")) {
198 refresh_only = 1;
199 continue;
201 usage(builtin_add_usage);
204 if (take_worktree_changes) {
205 update(verbose, prefix, argv + i);
206 goto finish;
209 if (argc <= i) {
210 fprintf(stderr, "Nothing specified, nothing added.\n");
211 fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
212 return 0;
214 pathspec = get_pathspec(prefix, argv + i);
216 if (refresh_only) {
217 refresh(verbose, pathspec);
218 goto finish;
221 fill_directory(&dir, pathspec, ignored_too);
223 if (show_only) {
224 const char *sep = "", *eof = "";
225 for (i = 0; i < dir.nr; i++) {
226 printf("%s%s", sep, dir.entries[i]->name);
227 sep = " ";
228 eof = "\n";
230 fputs(eof, stdout);
231 return 0;
234 if (read_cache() < 0)
235 die("index file corrupt");
237 if (dir.ignored_nr) {
238 fprintf(stderr, ignore_error);
239 for (i = 0; i < dir.ignored_nr; i++) {
240 fprintf(stderr, "%s\n", dir.ignored[i]->name);
242 fprintf(stderr, "Use -f if you really want to add them.\n");
243 die("no files added");
246 for (i = 0; i < dir.nr; i++)
247 add_file_to_cache(dir.entries[i]->name, verbose);
249 finish:
250 if (active_cache_changed) {
251 if (write_cache(newfd, active_cache, active_nr) ||
252 close(newfd) || commit_locked_index(&lock_file))
253 die("Unable to write new index file");
256 return 0;