2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
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)
20 struct dir_entry **src, **dst;
22 for (specs = 0; pathspec[specs]; specs++)
24 seen = xmalloc(specs);
25 memset(seen, 0, specs);
27 src = dst = dir->entries;
30 struct dir_entry *entry = *src++;
31 if (!match_pathspec(pathspec, entry->name, entry->len, prefix, seen)) {
37 dir->nr = dst - dir->entries;
39 for (i = 0; i < specs; i++) {
45 /* Existing file? We must have ignored it */
47 if (!match[0] || !lstat(match, &st))
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;
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);
73 char *common = xmalloc(baselen + 1);
74 common = xmalloc(baselen + 1);
75 memcpy(common, *pathspec, baselen);
80 /* Read the directory and prune it */
81 read_directory(dir, path, base, baselen);
83 prune_directory(dir, pathspec, baselen);
86 static int add_file_to_index(const char *path, int verbose)
90 struct cache_entry *ce;
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
110 int pos = cache_name_pos(path, namelen);
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);
120 printf("add '%s'\n", path);
121 cache_tree_invalidate_path(active_cache_tree, path);
125 static struct lock_file lock_file;
127 int cmd_add(int argc, const char **argv, char **envp)
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());
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];
149 if (!strcmp(arg, "--")) {
153 if (!strcmp(arg, "-n")) {
157 if (!strcmp(arg, "-v")) {
161 die(builtin_add_usage);
163 git_config(git_default_config);
164 pathspec = get_pathspec(prefix, argv + i);
166 fill_directory(&dir, pathspec);
169 const char *sep = "", *eof = "";
170 for (i = 0; i < dir.nr; i++) {
171 printf("%s%s", sep, dir.entries[i]->name);
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 close(newfd) || commit_lock_file(&lock_file))
185 die("Unable to write new index file");