get rid of num_packed_objects()
[git/platforms.git] / builtin-add.c
blob9ec292590c49574dfd2a698772a406b5ccdc419e
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"
12 static const char builtin_add_usage[] =
13 "git-add [-n] [-v] [-f] [--interactive | -i] [--] <filepattern>...";
15 static const char *excludes_file;
17 static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
19 char *seen;
20 int i, specs;
21 struct dir_entry **src, **dst;
23 for (specs = 0; pathspec[specs]; specs++)
24 /* nothing */;
25 seen = xcalloc(specs, 1);
27 src = dst = dir->entries;
28 i = dir->nr;
29 while (--i >= 0) {
30 struct dir_entry *entry = *src++;
31 if (match_pathspec(pathspec, entry->name, entry->len,
32 prefix, seen))
33 *dst++ = entry;
35 dir->nr = dst - dir->entries;
37 for (i = 0; i < specs; i++) {
38 struct stat st;
39 const char *match;
40 if (seen[i])
41 continue;
43 match = pathspec[i];
44 if (!match[0])
45 continue;
47 /* Existing file? We must have ignored it */
48 if (!lstat(match, &st)) {
49 struct dir_entry *ent;
51 ent = dir_add_name(dir, match, strlen(match));
52 ent->ignored = 1;
53 if (S_ISDIR(st.st_mode))
54 ent->ignored_dir = 1;
55 continue;
57 die("pathspec '%s' did not match any files", match);
61 static void fill_directory(struct dir_struct *dir, const char **pathspec)
63 const char *path, *base;
64 int baselen;
66 /* Set up the default git porcelain excludes */
67 memset(dir, 0, sizeof(*dir));
68 dir->exclude_per_dir = ".gitignore";
69 path = git_path("info/exclude");
70 if (!access(path, R_OK))
71 add_excludes_from_file(dir, path);
72 if (!access(excludes_file, R_OK))
73 add_excludes_from_file(dir, excludes_file);
76 * Calculate common prefix for the pathspec, and
77 * use that to optimize the directory walk
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;
89 /* Read the directory and prune it */
90 read_directory(dir, path, base, baselen, pathspec);
91 if (pathspec)
92 prune_directory(dir, pathspec, baselen);
95 static int git_add_config(const char *var, const char *value)
97 if (!strcmp(var, "core.excludesfile")) {
98 if (!value)
99 die("core.excludesfile without value");
100 excludes_file = xstrdup(value);
101 return 0;
104 return git_default_config(var, value);
107 static struct lock_file lock_file;
109 static const char ignore_warning[] =
110 "The following paths are ignored by one of your .gitignore files:\n";
112 int cmd_add(int argc, const char **argv, const char *prefix)
114 int i, newfd;
115 int verbose = 0, show_only = 0, ignored_too = 0;
116 const char **pathspec;
117 struct dir_struct dir;
118 int add_interactive = 0;
120 for (i = 1; i < argc; i++) {
121 if (!strcmp("--interactive", argv[i]) ||
122 !strcmp("-i", argv[i]))
123 add_interactive++;
125 if (add_interactive) {
126 const char *args[] = { "add--interactive", NULL };
128 if (add_interactive != 1 || argc != 2)
129 die("add --interactive does not take any parameters");
130 execv_git_cmd(args);
131 exit(1);
134 git_config(git_add_config);
136 newfd = hold_locked_index(&lock_file, 1);
138 for (i = 1; i < argc; i++) {
139 const char *arg = argv[i];
141 if (arg[0] != '-')
142 break;
143 if (!strcmp(arg, "--")) {
144 i++;
145 break;
147 if (!strcmp(arg, "-n")) {
148 show_only = 1;
149 continue;
151 if (!strcmp(arg, "-f")) {
152 ignored_too = 1;
153 continue;
155 if (!strcmp(arg, "-v")) {
156 verbose = 1;
157 continue;
159 usage(builtin_add_usage);
161 if (argc <= i) {
162 fprintf(stderr, "Nothing specified, nothing added.\n");
163 fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
164 return 0;
166 pathspec = get_pathspec(prefix, argv + i);
168 fill_directory(&dir, pathspec);
170 if (show_only) {
171 const char *sep = "", *eof = "";
172 for (i = 0; i < dir.nr; i++) {
173 if (!ignored_too && dir.entries[i]->ignored)
174 continue;
175 printf("%s%s", sep, dir.entries[i]->name);
176 sep = " ";
177 eof = "\n";
179 fputs(eof, stdout);
180 return 0;
183 if (read_cache() < 0)
184 die("index file corrupt");
186 if (!ignored_too) {
187 int has_ignored = 0;
188 for (i = 0; i < dir.nr; i++)
189 if (dir.entries[i]->ignored)
190 has_ignored = 1;
191 if (has_ignored) {
192 fprintf(stderr, ignore_warning);
193 for (i = 0; i < dir.nr; i++) {
194 if (!dir.entries[i]->ignored)
195 continue;
196 fprintf(stderr, "%s", dir.entries[i]->name);
197 if (dir.entries[i]->ignored_dir)
198 fprintf(stderr, " (directory)");
199 fputc('\n', stderr);
201 fprintf(stderr,
202 "Use -f if you really want to add them.\n");
203 exit(1);
207 for (i = 0; i < dir.nr; i++)
208 add_file_to_cache(dir.entries[i]->name, verbose);
210 if (active_cache_changed) {
211 if (write_cache(newfd, active_cache, active_nr) ||
212 close(newfd) || commit_locked_index(&lock_file))
213 die("Unable to write new index file");
216 return 0;