quote.c: silence compiler warnings from EMIT macro
[git/dscho.git] / builtin-add.c
blobbfbbb1bf52e10667f94dd574ab32fe070c98c4cb
1 /*
2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
5 */
6 #include <fnmatch.h>
8 #include "cache.h"
9 #include "builtin.h"
10 #include "dir.h"
11 #include "cache-tree.h"
13 static const char builtin_add_usage[] =
14 "git-add [-n] [-v] <filepattern>...";
16 static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
18 char *seen;
19 int i, specs;
20 struct dir_entry **src, **dst;
22 for (specs = 0; pathspec[specs]; specs++)
23 /* nothing */;
24 seen = xmalloc(specs);
25 memset(seen, 0, specs);
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, prefix, seen)) {
32 free(entry);
33 continue;
35 *dst++ = entry;
37 dir->nr = dst - dir->entries;
39 for (i = 0; i < specs; i++) {
40 struct stat st;
41 const char *match;
42 if (seen[i])
43 continue;
45 /* Existing file? We must have ignored it */
46 match = pathspec[i];
47 if (!match[0] || !lstat(match, &st))
48 continue;
49 die("pathspec '%s' did not match any files", match);
53 static void fill_directory(struct dir_struct *dir, const char **pathspec)
55 const char *path, *base;
56 int baselen;
58 /* Set up the default git porcelain excludes */
59 memset(dir, 0, sizeof(*dir));
60 dir->exclude_per_dir = ".gitignore";
61 path = git_path("info/exclude");
62 if (!access(path, R_OK))
63 add_excludes_from_file(dir, path);
66 * Calculate common prefix for the pathspec, and
67 * use that to optimize the directory walk
69 baselen = common_prefix(pathspec);
70 path = ".";
71 base = "";
72 if (baselen) {
73 char *common = xmalloc(baselen + 1);
74 common = xmalloc(baselen + 1);
75 memcpy(common, *pathspec, baselen);
76 common[baselen] = 0;
77 path = base = common;
80 /* Read the directory and prune it */
81 read_directory(dir, path, base, baselen);
82 if (pathspec)
83 prune_directory(dir, pathspec, baselen);
86 static int add_file_to_index(const char *path, int verbose)
88 int size, namelen;
89 struct stat st;
90 struct cache_entry *ce;
92 if (lstat(path, &st))
93 die("%s: unable to stat (%s)", path, strerror(errno));
95 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode))
96 die("%s: can only add regular files or symbolic links", path);
98 namelen = strlen(path);
99 size = cache_entry_size(namelen);
100 ce = xcalloc(1, size);
101 memcpy(ce->name, path, namelen);
102 ce->ce_flags = htons(namelen);
103 fill_stat_cache_info(ce, &st);
105 ce->ce_mode = create_ce_mode(st.st_mode);
106 if (!trust_executable_bit) {
107 /* If there is an existing entry, pick the mode bits
108 * from it.
110 int pos = cache_name_pos(path, namelen);
111 if (pos >= 0)
112 ce->ce_mode = active_cache[pos]->ce_mode;
115 if (index_path(ce->sha1, path, &st, 1))
116 die("unable to index file %s", path);
117 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD))
118 die("unable to add %s to index",path);
119 if (verbose)
120 printf("add '%s'\n", path);
121 cache_tree_invalidate_path(active_cache_tree, path);
122 return 0;
125 static struct lock_file lock_file;
127 int cmd_add(int argc, const char **argv, char **envp)
129 int i, newfd;
130 int verbose = 0, show_only = 0;
131 const char *prefix = setup_git_directory();
132 const char **pathspec;
133 struct dir_struct dir;
135 git_config(git_default_config);
137 newfd = hold_lock_file_for_update(&lock_file, get_index_file());
138 if (newfd < 0)
139 die("unable to create new index file");
141 if (read_cache() < 0)
142 die("index file corrupt");
144 for (i = 1; i < argc; i++) {
145 const char *arg = argv[i];
147 if (arg[0] != '-')
148 break;
149 if (!strcmp(arg, "--")) {
150 i++;
151 break;
153 if (!strcmp(arg, "-n")) {
154 show_only = 1;
155 continue;
157 if (!strcmp(arg, "-v")) {
158 verbose = 1;
159 continue;
161 die(builtin_add_usage);
163 git_config(git_default_config);
164 pathspec = get_pathspec(prefix, argv + i);
166 fill_directory(&dir, pathspec);
168 if (show_only) {
169 const char *sep = "", *eof = "";
170 for (i = 0; i < dir.nr; i++) {
171 printf("%s%s", sep, dir.entries[i]->name);
172 sep = " ";
173 eof = "\n";
175 fputs(eof, stdout);
176 return 0;
179 for (i = 0; i < dir.nr; i++)
180 add_file_to_index(dir.entries[i]->name, verbose);
182 if (active_cache_changed) {
183 if (write_cache(newfd, active_cache, active_nr) ||
184 commit_lock_file(&lock_file))
185 die("Unable to write new index file");
188 return 0;