]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-add.c
install-sh from automake does not like -m without delimiting space
[thirdparty/git.git] / builtin-add.c
CommitLineData
0d781539
LT
1/*
2 * "git add" builtin command
3 *
4 * Copyright (C) 2006 Linus Torvalds
5 */
0d781539
LT
6#include "cache.h"
7#include "builtin.h"
8#include "dir.h"
5cde71d6 9#include "exec_cmd.h"
93872e07 10#include "cache-tree.h"
dfdac5d9
JH
11#include "diff.h"
12#include "diffcore.h"
13#include "commit.h"
14#include "revision.h"
58680165 15#include "run-command.h"
5c46f754 16#include "parse-options.h"
0d781539 17
5c46f754
KH
18static const char * const builtin_add_usage[] = {
19 "git-add [options] [--] <filepattern>...",
20 NULL
21};
0d781539 22
93c44d49 23static int take_worktree_changes;
896bdfa2 24
0d781539
LT
25static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
26{
f2593398
LT
27 char *seen;
28 int i, specs;
0d781539
LT
29 struct dir_entry **src, **dst;
30
f2593398
LT
31 for (specs = 0; pathspec[specs]; specs++)
32 /* nothing */;
28f75818 33 seen = xcalloc(specs, 1);
f2593398 34
0d781539
LT
35 src = dst = dir->entries;
36 i = dir->nr;
37 while (--i >= 0) {
38 struct dir_entry *entry = *src++;
4d06f8ac
JH
39 if (match_pathspec(pathspec, entry->name, entry->len,
40 prefix, seen))
41 *dst++ = entry;
0d781539
LT
42 }
43 dir->nr = dst - dir->entries;
f2593398
LT
44
45 for (i = 0; i < specs; i++) {
e96980ef
JK
46 if (!seen[i] && !file_exists(pathspec[i]))
47 die("pathspec '%s' did not match any files",
48 pathspec[i]);
f2593398 49 }
399f0a8e 50 free(seen);
0d781539
LT
51}
52
e96980ef
JK
53static void fill_directory(struct dir_struct *dir, const char **pathspec,
54 int ignored_too)
0d781539
LT
55{
56 const char *path, *base;
57 int baselen;
58
59 /* Set up the default git porcelain excludes */
60 memset(dir, 0, sizeof(*dir));
e96980ef
JK
61 if (!ignored_too) {
62 dir->collect_ignored = 1;
039bc64e 63 setup_standard_excludes(dir);
e96980ef 64 }
0d781539
LT
65
66 /*
67 * Calculate common prefix for the pathspec, and
68 * use that to optimize the directory walk
69 */
70 baselen = common_prefix(pathspec);
71 path = ".";
72 base = "";
182af834
PH
73 if (baselen)
74 path = base = xmemdupz(*pathspec, baselen);
0d781539
LT
75
76 /* Read the directory and prune it */
9fc42d60 77 read_directory(dir, path, base, baselen, pathspec);
0d781539
LT
78 if (pathspec)
79 prune_directory(dir, pathspec, baselen);
80}
81
dfdac5d9
JH
82static void update_callback(struct diff_queue_struct *q,
83 struct diff_options *opt, void *cbdata)
84{
85 int i, verbose;
86
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:
43b98acc 93 die("unexpected diff status %c", p->status);
dfdac5d9
JH
94 case DIFF_STATUS_UNMERGED:
95 case DIFF_STATUS_MODIFIED:
767c98a5 96 case DIFF_STATUS_TYPE_CHANGED:
dfdac5d9
JH
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;
104 }
105 }
106}
107
58680165 108void add_files_to_cache(int verbose, const char *prefix, const char **files)
dfdac5d9
JH
109{
110 struct rev_info rev;
2ed2c222 111 init_revisions(&rev, prefix);
dfdac5d9 112 setup_revisions(0, NULL, &rev, NULL);
2ed2c222 113 rev.prune_data = get_pathspec(prefix, files);
dfdac5d9
JH
114 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
115 rev.diffopt.format_callback = update_callback;
116 rev.diffopt.format_callback_data = &verbose;
fb63d7f8 117 run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
dfdac5d9
JH
118}
119
d616813d
AJ
120static void refresh(int verbose, const char **pathspec)
121{
122 char *seen;
123 int i, specs;
124
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]);
134 }
399f0a8e 135 free(seen);
d616813d
AJ
136}
137
58680165
KH
138int interactive_add(void)
139{
140 const char *argv[2] = { "add--interactive", NULL };
141
142 return run_command_v_opt(argv, RUN_GIT_CMD);
143}
144
021b6e45 145static struct lock_file lock_file;
0d781539 146
b39c53e6 147static const char ignore_error[] =
6a1ad325
JH
148"The following paths are ignored by one of your .gitignore files:\n";
149
5c46f754
KH
150static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
151static int add_interactive = 0;
152
153static struct option builtin_add_options[] = {
154 OPT__DRY_RUN(&show_only),
155 OPT__VERBOSE(&verbose),
156 OPT_GROUP(""),
157 OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"),
158 OPT_BOOLEAN('f', NULL, &ignored_too, "allow adding otherwise ignored files"),
159 OPT_BOOLEAN('u', NULL, &take_worktree_changes, "update tracked files"),
160 OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"),
161 OPT_END(),
162};
163
a633fca0 164int cmd_add(int argc, const char **argv, const char *prefix)
0d781539 165{
5c46f754 166 int i, newfd, orig_argc = argc;
0d781539
LT
167 const char **pathspec;
168 struct dir_struct dir;
5cde71d6 169
5c46f754
KH
170 argc = parse_options(argc, argv, builtin_add_options,
171 builtin_add_usage, 0);
5cde71d6 172 if (add_interactive) {
5c46f754 173 if (add_interactive != 1 || orig_argc != 2)
5cde71d6 174 die("add --interactive does not take any parameters");
58680165 175 exit(interactive_add());
5cde71d6 176 }
0d781539 177
039bc64e 178 git_config(git_default_config);
0d781539 179
30ca07a2 180 newfd = hold_locked_index(&lock_file, 1);
0d781539 181
93c44d49 182 if (take_worktree_changes) {
58680165
KH
183 if (read_cache() < 0)
184 die("index file corrupt");
3d66dc96 185 add_files_to_cache(verbose, prefix, argv);
dfdac5d9
JH
186 goto finish;
187 }
188
5c46f754 189 if (argc == 0) {
93b0d86a
JH
190 fprintf(stderr, "Nothing specified, nothing added.\n");
191 fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
192 return 0;
193 }
5c46f754 194 pathspec = get_pathspec(prefix, argv);
0d781539 195
d616813d
AJ
196 if (refresh_only) {
197 refresh(verbose, pathspec);
198 goto finish;
199 }
200
e96980ef 201 fill_directory(&dir, pathspec, ignored_too);
0d781539
LT
202
203 if (show_only) {
204 const char *sep = "", *eof = "";
205 for (i = 0; i < dir.nr; i++) {
206 printf("%s%s", sep, dir.entries[i]->name);
207 sep = " ";
208 eof = "\n";
209 }
210 fputs(eof, stdout);
211 return 0;
212 }
213
366bfcb6
NP
214 if (read_cache() < 0)
215 die("index file corrupt");
216
e96980ef 217 if (dir.ignored_nr) {
b39c53e6 218 fprintf(stderr, ignore_error);
e96980ef
JK
219 for (i = 0; i < dir.ignored_nr; i++) {
220 fprintf(stderr, "%s\n", dir.ignored[i]->name);
6a1ad325 221 }
e96980ef 222 fprintf(stderr, "Use -f if you really want to add them.\n");
b39c53e6 223 die("no files added");
6a1ad325
JH
224 }
225
0d781539 226 for (i = 0; i < dir.nr; i++)
fd1c3bf0 227 add_file_to_cache(dir.entries[i]->name, verbose);
0d781539 228
dfdac5d9 229 finish:
0d781539
LT
230 if (active_cache_changed) {
231 if (write_cache(newfd, active_cache, active_nr) ||
30ca07a2 232 close(newfd) || commit_locked_index(&lock_file))
0d781539
LT
233 die("Unable to write new index file");
234 }
235
236 return 0;
237}