Fix a bug in email extraction used in git-send-email.
[git] / builtin-add.c
1 /*
2  * "git add" builtin command
3  *
4  * Copyright (C) 2006 Linus Torvalds
5  */
6 #include <fnmatch.h>
7
8 #include "cache.h"
9 #include "builtin.h"
10 #include "dir.h"
11 #include "cache-tree.h"
12
13 static const char builtin_add_usage[] =
14 "git-add [-n] [-v] <filepattern>...";
15
16 static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
17 {
18         char *seen;
19         int i, specs;
20         struct dir_entry **src, **dst;
21
22         for (specs = 0; pathspec[specs];  specs++)
23                 /* nothing */;
24         seen = xmalloc(specs);
25         memset(seen, 0, specs);
26
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;
34                 }
35                 *dst++ = entry;
36         }
37         dir->nr = dst - dir->entries;
38
39         for (i = 0; i < specs; i++) {
40                 struct stat st;
41                 const char *match;
42                 if (seen[i])
43                         continue;
44
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);
50         }
51 }
52
53 static void fill_directory(struct dir_struct *dir, const char **pathspec)
54 {
55         const char *path, *base;
56         int baselen;
57
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);
64
65         /*
66          * Calculate common prefix for the pathspec, and
67          * use that to optimize the directory walk
68          */
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;
78         }
79
80         /* Read the directory and prune it */
81         read_directory(dir, path, base, baselen);
82         if (pathspec)
83                 prune_directory(dir, pathspec, baselen);
84 }
85
86 static int add_file_to_index(const char *path, int verbose)
87 {
88         int size, namelen;
89         struct stat st;
90         struct cache_entry *ce;
91
92         if (lstat(path, &st))
93                 die("%s: unable to stat (%s)", path, strerror(errno));
94
95         if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode))
96                 die("%s: can only add regular files or symbolic links", path);
97
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);
104
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.
109                  */
110                 int pos = cache_name_pos(path, namelen);
111                 if (pos >= 0)
112                         ce->ce_mode = active_cache[pos]->ce_mode;
113         }
114
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;
123 }
124
125 static struct cache_file cache_file;
126
127 int cmd_add(int argc, const char **argv, char **envp)
128 {
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;
134
135         git_config(git_default_config);
136
137         newfd = hold_index_file_for_update(&cache_file, get_index_file());
138         if (newfd < 0)
139                 die("unable to create new cachefile");
140
141         if (read_cache() < 0)
142                 die("index file corrupt");
143
144         for (i = 1; i < argc; i++) {
145                 const char *arg = argv[i];
146
147                 if (arg[0] != '-')
148                         break;
149                 if (!strcmp(arg, "--")) {
150                         i++;
151                         break;
152                 }
153                 if (!strcmp(arg, "-n")) {
154                         show_only = 1;
155                         continue;
156                 }
157                 if (!strcmp(arg, "-v")) {
158                         verbose = 1;
159                         continue;
160                 }
161                 die(builtin_add_usage);
162         }
163         git_config(git_default_config);
164         pathspec = get_pathspec(prefix, argv + i);
165
166         fill_directory(&dir, pathspec);
167
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";
174                 }
175                 fputs(eof, stdout);
176                 return 0;
177         }
178
179         for (i = 0; i < dir.nr; i++)
180                 add_file_to_index(dir.entries[i]->name, verbose);
181
182         if (active_cache_changed) {
183                 if (write_cache(newfd, active_cache, active_nr) ||
184                     commit_index_file(&cache_file))
185                         die("Unable to write new index file");
186         }
187
188         return 0;
189 }