Merge branch 'js/cvs'
[git/debian.git] / builtin-add.c
blob3a73a173f729e0ca75e650779773316e8ae3fe98
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 = xcalloc(specs, 1);
26 src = dst = dir->entries;
27 i = dir->nr;
28 while (--i >= 0) {
29 struct dir_entry *entry = *src++;
30 if (!match_pathspec(pathspec, entry->name, entry->len, prefix, seen)) {
31 free(entry);
32 continue;
34 *dst++ = entry;
36 dir->nr = dst - dir->entries;
38 for (i = 0; i < specs; i++) {
39 struct stat st;
40 const char *match;
41 if (seen[i])
42 continue;
44 /* Existing file? We must have ignored it */
45 match = pathspec[i];
46 if (!match[0] || !lstat(match, &st))
47 continue;
48 die("pathspec '%s' did not match any files", match);
52 static void fill_directory(struct dir_struct *dir, const char **pathspec)
54 const char *path, *base;
55 int baselen;
57 /* Set up the default git porcelain excludes */
58 memset(dir, 0, sizeof(*dir));
59 dir->exclude_per_dir = ".gitignore";
60 path = git_path("info/exclude");
61 if (!access(path, R_OK))
62 add_excludes_from_file(dir, path);
65 * Calculate common prefix for the pathspec, and
66 * use that to optimize the directory walk
68 baselen = common_prefix(pathspec);
69 path = ".";
70 base = "";
71 if (baselen) {
72 char *common = xmalloc(baselen + 1);
73 common = xmalloc(baselen + 1);
74 memcpy(common, *pathspec, baselen);
75 common[baselen] = 0;
76 path = base = common;
79 /* Read the directory and prune it */
80 read_directory(dir, path, base, baselen);
81 if (pathspec)
82 prune_directory(dir, pathspec, baselen);
85 static int add_file_to_index(const char *path, int verbose)
87 int size, namelen;
88 struct stat st;
89 struct cache_entry *ce;
91 if (lstat(path, &st))
92 die("%s: unable to stat (%s)", path, strerror(errno));
94 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode))
95 die("%s: can only add regular files or symbolic links", path);
97 namelen = strlen(path);
98 size = cache_entry_size(namelen);
99 ce = xcalloc(1, size);
100 memcpy(ce->name, path, namelen);
101 ce->ce_flags = htons(namelen);
102 fill_stat_cache_info(ce, &st);
104 ce->ce_mode = create_ce_mode(st.st_mode);
105 if (!trust_executable_bit) {
106 /* If there is an existing entry, pick the mode bits
107 * from it.
109 int pos = cache_name_pos(path, namelen);
110 if (pos >= 0)
111 ce->ce_mode = active_cache[pos]->ce_mode;
114 if (index_path(ce->sha1, path, &st, 1))
115 die("unable to index file %s", path);
116 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD))
117 die("unable to add %s to index",path);
118 if (verbose)
119 printf("add '%s'\n", path);
120 cache_tree_invalidate_path(active_cache_tree, path);
121 return 0;
124 static struct lock_file lock_file;
126 int cmd_add(int argc, const char **argv, char **envp)
128 int i, newfd;
129 int verbose = 0, show_only = 0;
130 const char *prefix = setup_git_directory();
131 const char **pathspec;
132 struct dir_struct dir;
134 git_config(git_default_config);
136 newfd = hold_lock_file_for_update(&lock_file, get_index_file());
137 if (newfd < 0)
138 die("unable to create new index file");
140 if (read_cache() < 0)
141 die("index file corrupt");
143 for (i = 1; i < argc; i++) {
144 const char *arg = argv[i];
146 if (arg[0] != '-')
147 break;
148 if (!strcmp(arg, "--")) {
149 i++;
150 break;
152 if (!strcmp(arg, "-n")) {
153 show_only = 1;
154 continue;
156 if (!strcmp(arg, "-v")) {
157 verbose = 1;
158 continue;
160 die(builtin_add_usage);
162 git_config(git_default_config);
163 pathspec = get_pathspec(prefix, argv + i);
165 fill_directory(&dir, pathspec);
167 if (show_only) {
168 const char *sep = "", *eof = "";
169 for (i = 0; i < dir.nr; i++) {
170 printf("%s%s", sep, dir.entries[i]->name);
171 sep = " ";
172 eof = "\n";
174 fputs(eof, stdout);
175 return 0;
178 for (i = 0; i < dir.nr; i++)
179 add_file_to_index(dir.entries[i]->name, verbose);
181 if (active_cache_changed) {
182 if (write_cache(newfd, active_cache, active_nr) ||
183 close(newfd) || commit_lock_file(&lock_file))
184 die("Unable to write new index file");
187 return 0;