]> asedeno.scripts.mit.edu Git - git.git/blob - builtin-add.c
git-add: warn when adding an ignored file with an explicit request.
[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
12 static const char builtin_add_usage[] =
13 "git-add [-n] [-v] [--interactive] [--] <filepattern>...";
14
15 static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
16 {
17         char *seen;
18         int i, specs;
19         struct dir_entry **src, **dst;
20
21         for (specs = 0; pathspec[specs];  specs++)
22                 /* nothing */;
23         seen = xcalloc(specs, 1);
24
25         src = dst = dir->entries;
26         i = dir->nr;
27         while (--i >= 0) {
28                 struct dir_entry *entry = *src++;
29                 int how = match_pathspec(pathspec, entry->name, entry->len,
30                                          prefix, seen);
31                 /*
32                  * ignored entries can be added with exact match,
33                  * but not with glob nor recursive.
34                  */
35                 if (!how ||
36                     (entry->ignored_entry && how != MATCHED_EXACTLY)) {
37                         free(entry);
38                         continue;
39                 }
40                 if (entry->ignored_entry)
41                         fprintf(stderr, "warning: '%s' is an ignored path.\n",
42                                 entry->name);
43                 *dst++ = entry;
44         }
45         dir->nr = dst - dir->entries;
46
47         for (i = 0; i < specs; i++) {
48                 struct stat st;
49                 const char *match;
50                 if (seen[i])
51                         continue;
52
53                 /* Existing file? We must have ignored it */
54                 match = pathspec[i];
55                 if (!match[0] || !lstat(match, &st))
56                         continue;
57                 die("pathspec '%s' did not match any files", match);
58         }
59 }
60
61 static void fill_directory(struct dir_struct *dir, const char **pathspec)
62 {
63         const char *path, *base;
64         int baselen;
65
66         /* Set up the default git porcelain excludes */
67         memset(dir, 0, sizeof(*dir));
68         if (pathspec)
69                 dir->show_both = 1;
70         dir->exclude_per_dir = ".gitignore";
71         path = git_path("info/exclude");
72         if (!access(path, R_OK))
73                 add_excludes_from_file(dir, path);
74
75         /*
76          * Calculate common prefix for the pathspec, and
77          * use that to optimize the directory walk
78          */
79         baselen = common_prefix(pathspec);
80         path = ".";
81         base = "";
82         if (baselen) {
83                 char *common = xmalloc(baselen + 1);
84                 memcpy(common, *pathspec, baselen);
85                 common[baselen] = 0;
86                 path = base = common;
87         }
88
89         /* Read the directory and prune it */
90         read_directory(dir, path, base, baselen);
91         if (pathspec)
92                 prune_directory(dir, pathspec, baselen);
93 }
94
95 static struct lock_file lock_file;
96
97 int cmd_add(int argc, const char **argv, const char *prefix)
98 {
99         int i, newfd;
100         int verbose = 0, show_only = 0;
101         const char **pathspec;
102         struct dir_struct dir;
103         int add_interactive = 0;
104
105         for (i = 1; i < argc; i++) {
106                 if (!strcmp("--interactive", argv[i]))
107                         add_interactive++;
108         }
109         if (add_interactive) {
110                 const char *args[] = { "add--interactive", NULL };
111
112                 if (add_interactive != 1 || argc != 2)
113                         die("add --interactive does not take any parameters");
114                 execv_git_cmd(args);
115                 exit(1);
116         }
117
118         git_config(git_default_config);
119
120         newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
121
122         for (i = 1; i < argc; i++) {
123                 const char *arg = argv[i];
124
125                 if (arg[0] != '-')
126                         break;
127                 if (!strcmp(arg, "--")) {
128                         i++;
129                         break;
130                 }
131                 if (!strcmp(arg, "-n")) {
132                         show_only = 1;
133                         continue;
134                 }
135                 if (!strcmp(arg, "-v")) {
136                         verbose = 1;
137                         continue;
138                 }
139                 usage(builtin_add_usage);
140         }
141         if (argc <= i) {
142                 fprintf(stderr, "Nothing specified, nothing added.\n");
143                 fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
144                 return 0;
145         }
146         pathspec = get_pathspec(prefix, argv + i);
147
148         fill_directory(&dir, pathspec);
149
150         if (show_only) {
151                 const char *sep = "", *eof = "";
152                 for (i = 0; i < dir.nr; i++) {
153                         printf("%s%s", sep, dir.entries[i]->name);
154                         sep = " ";
155                         eof = "\n";
156                 }
157                 fputs(eof, stdout);
158                 return 0;
159         }
160
161         if (read_cache() < 0)
162                 die("index file corrupt");
163
164         for (i = 0; i < dir.nr; i++)
165                 add_file_to_index(dir.entries[i]->name, verbose);
166
167         if (active_cache_changed) {
168                 if (write_cache(newfd, active_cache, active_nr) ||
169                     close(newfd) || commit_lock_file(&lock_file))
170                         die("Unable to write new index file");
171         }
172
173         return 0;
174 }