4
4
* Copyright (C) 2006 Linus Torvalds
11
10
#include "cache-tree.h"
13
12
static const char builtin_add_usage[] =
14
"git-add [-n] [-v] <filepattern>...";
13
"git-add [-n] [-v] [-f] [--interactive | -i] [--] <filepattern>...";
15
static const char *excludes_file;
16
17
static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
29
30
struct dir_entry *entry = *src++;
30
if (!match_pathspec(pathspec, entry->name, entry->len, prefix, seen)) {
31
if (match_pathspec(pathspec, entry->name, entry->len,
36
35
dir->nr = dst - dir->entries;
44
47
/* Existing file? We must have ignored it */
46
if (!match[0] || !lstat(match, &st))
48
if (!lstat(match, &st)) {
49
struct dir_entry *ent;
51
ent = dir_add_name(dir, match, strlen(match));
53
if (S_ISDIR(st.st_mode))
48
57
die("pathspec '%s' did not match any files", match);
60
69
path = git_path("info/exclude");
61
70
if (!access(path, R_OK))
62
71
add_excludes_from_file(dir, path);
72
if (!access(excludes_file, R_OK))
73
add_excludes_from_file(dir, excludes_file);
65
76
* Calculate common prefix for the pathspec, and
81
92
prune_directory(dir, pathspec, baselen);
95
static int git_add_config(const char *var, const char *value)
97
if (!strcmp(var, "core.excludesfile")) {
99
die("core.excludesfile without value");
100
excludes_file = xstrdup(value);
104
return git_default_config(var, value);
84
107
static struct lock_file lock_file;
109
static const char ignore_warning[] =
110
"The following paths are ignored by one of your .gitignore files:\n";
86
112
int cmd_add(int argc, const char **argv, const char *prefix)
89
int verbose = 0, show_only = 0;
115
int verbose = 0, show_only = 0, ignored_too = 0;
90
116
const char **pathspec;
91
117
struct dir_struct dir;
93
git_config(git_default_config);
118
int add_interactive = 0;
120
for (i = 1; i < argc; i++) {
121
if (!strcmp("--interactive", argv[i]) ||
122
!strcmp("-i", argv[i]))
125
if (add_interactive) {
126
const char *args[] = { "add--interactive", NULL };
128
if (add_interactive != 1 || argc != 2)
129
die("add --interactive does not take any parameters");
134
git_config(git_add_config);
95
136
newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
98
die("index file corrupt");
100
138
for (i = 1; i < argc; i++) {
101
139
const char *arg = argv[i];
151
if (!strcmp(arg, "-f")) {
113
155
if (!strcmp(arg, "-v")) {
117
159
usage(builtin_add_usage);
162
fprintf(stderr, "Nothing specified, nothing added.\n");
163
fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
119
166
pathspec = get_pathspec(prefix, argv + i);
121
168
fill_directory(&dir, pathspec);
124
171
const char *sep = "", *eof = "";
125
172
for (i = 0; i < dir.nr; i++) {
173
if (!ignored_too && dir.entries[i]->ignored)
126
175
printf("%s%s", sep, dir.entries[i]->name);
183
if (read_cache() < 0)
184
die("index file corrupt");
188
for (i = 0; i < dir.nr; i++)
189
if (dir.entries[i]->ignored)
192
fprintf(stderr, ignore_warning);
193
for (i = 0; i < dir.nr; i++) {
194
if (!dir.entries[i]->ignored)
196
fprintf(stderr, "%s", dir.entries[i]->name);
197
if (dir.entries[i]->ignored_dir)
198
fprintf(stderr, " (directory)");
202
"Use -f if you really want to add them.\n");
134
207
for (i = 0; i < dir.nr; i++)
135
208
add_file_to_index(dir.entries[i]->name, verbose);