1/*
2* "git add" builtin command
3*
4* Copyright (C) 2006 Linus Torvalds
5*/
6#include <fnmatch.h>
78
#include "cache.h"
9#include "builtin.h"
10#include "dir.h"
11#include "cache-tree.h"
1213
static const char builtin_add_usage[] =
14"git-add [-n] [-v] <filepattern>...";
1516
static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
17{
18char *seen;
19int i, specs;
20struct dir_entry **src, **dst;
2122
for (specs = 0; pathspec[specs]; specs++)
23/* nothing */;
24seen = xcalloc(specs, 1);
2526
src = dst = dir->entries;
27i = dir->nr;
28while (--i >= 0) {
29struct dir_entry *entry = *src++;
30if (!match_pathspec(pathspec, entry->name, entry->len, prefix, seen)) {
31free(entry);
32continue;
33}
34*dst++ = entry;
35}
36dir->nr = dst - dir->entries;
3738
for (i = 0; i < specs; i++) {
39struct stat st;
40const char *match;
41if (seen[i])
42continue;
4344
/* Existing file? We must have ignored it */
45match = pathspec[i];
46if (!match[0] || !lstat(match, &st))
47continue;
48die("pathspec '%s' did not match any files", match);
49}
50}
5152
static void fill_directory(struct dir_struct *dir, const char **pathspec)
53{
54const char *path, *base;
55int baselen;
5657
/* Set up the default git porcelain excludes */
58memset(dir, 0, sizeof(*dir));
59dir->exclude_per_dir = ".gitignore";
60path = git_path("info/exclude");
61if (!access(path, R_OK))
62add_excludes_from_file(dir, path);
6364
/*
65* Calculate common prefix for the pathspec, and
66* use that to optimize the directory walk
67*/
68baselen = common_prefix(pathspec);
69path = ".";
70base = "";
71if (baselen) {
72char *common = xmalloc(baselen + 1);
73common = xmalloc(baselen + 1);
74memcpy(common, *pathspec, baselen);
75common[baselen] = 0;
76path = base = common;
77}
7879
/* Read the directory and prune it */
80read_directory(dir, path, base, baselen);
81if (pathspec)
82prune_directory(dir, pathspec, baselen);
83}
8485
static int add_file_to_index(const char *path, int verbose)
86{
87int size, namelen;
88struct stat st;
89struct cache_entry *ce;
9091
if (lstat(path, &st))
92die("%s: unable to stat (%s)", path, strerror(errno));
9394
if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode))
95die("%s: can only add regular files or symbolic links", path);
9697
namelen = strlen(path);
98size = cache_entry_size(namelen);
99ce = xcalloc(1, size);
100memcpy(ce->name, path, namelen);
101ce->ce_flags = htons(namelen);
102fill_stat_cache_info(ce, &st);
103104
ce->ce_mode = create_ce_mode(st.st_mode);
105if (!trust_executable_bit) {
106/* If there is an existing entry, pick the mode bits
107* from it.
108*/
109int pos = cache_name_pos(path, namelen);
110if (pos >= 0)
111ce->ce_mode = active_cache[pos]->ce_mode;
112}
113114
if (index_path(ce->sha1, path, &st, 1))
115die("unable to index file %s", path);
116if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD))
117die("unable to add %s to index",path);
118if (verbose)
119printf("add '%s'\n", path);
120cache_tree_invalidate_path(active_cache_tree, path);
121return 0;
122}
123124
static struct lock_file lock_file;
125126
int cmd_add(int argc, const char **argv, char **envp)
127{
128int i, newfd;
129int verbose = 0, show_only = 0;
130const char *prefix = setup_git_directory();
131const char **pathspec;
132struct dir_struct dir;
133134
git_config(git_default_config);
135136
newfd = hold_lock_file_for_update(&lock_file, get_index_file());
137if (newfd < 0)
138die("unable to create new index file");
139140
if (read_cache() < 0)
141die("index file corrupt");
142143
for (i = 1; i < argc; i++) {
144const char *arg = argv[i];
145146
if (arg[0] != '-')
147break;
148if (!strcmp(arg, "--")) {
149i++;
150break;
151}
152if (!strcmp(arg, "-n")) {
153show_only = 1;
154continue;
155}
156if (!strcmp(arg, "-v")) {
157verbose = 1;
158continue;
159}
160die(builtin_add_usage);
161}
162git_config(git_default_config);
163pathspec = get_pathspec(prefix, argv + i);
164165
fill_directory(&dir, pathspec);
166167
if (show_only) {
168const char *sep = "", *eof = "";
169for (i = 0; i < dir.nr; i++) {
170printf("%s%s", sep, dir.entries[i]->name);
171sep = " ";
172eof = "\n";
173}
174fputs(eof, stdout);
175return 0;
176}
177178
for (i = 0; i < dir.nr; i++)
179add_file_to_index(dir.entries[i]->name, verbose);
180181
if (active_cache_changed) {
182if (write_cache(newfd, active_cache, active_nr) ||
183close(newfd) || commit_lock_file(&lock_file))
184die("Unable to write new index file");
185}
186187
return 0;
188}