2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
10 #include "cache-tree.h"
12 static const char builtin_add_usage
[] =
13 "git-add [-n] [-v] [-f] [--interactive | -i] [--] <filepattern>...";
15 static const char *excludes_file
;
17 static void prune_directory(struct dir_struct
*dir
, const char **pathspec
, int prefix
)
21 struct dir_entry
**src
, **dst
;
23 for (specs
= 0; pathspec
[specs
]; specs
++)
25 seen
= xcalloc(specs
, 1);
27 src
= dst
= dir
->entries
;
30 struct dir_entry
*entry
= *src
++;
31 if (match_pathspec(pathspec
, entry
->name
, entry
->len
,
35 dir
->nr
= dst
- dir
->entries
;
37 for (i
= 0; i
< specs
; i
++) {
47 /* Existing file? We must have ignored it */
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
))
57 die("pathspec '%s' did not match any files", match
);
61 static void fill_directory(struct dir_struct
*dir
, const char **pathspec
)
63 const char *path
, *base
;
66 /* Set up the default git porcelain excludes */
67 memset(dir
, 0, sizeof(*dir
));
68 dir
->exclude_per_dir
= ".gitignore";
69 path
= git_path("info/exclude");
70 if (!access(path
, R_OK
))
71 add_excludes_from_file(dir
, path
);
72 if (!access(excludes_file
, R_OK
))
73 add_excludes_from_file(dir
, excludes_file
);
76 * Calculate common prefix for the pathspec, and
77 * use that to optimize the directory walk
79 baselen
= common_prefix(pathspec
);
83 char *common
= xmalloc(baselen
+ 1);
84 memcpy(common
, *pathspec
, baselen
);
89 /* Read the directory and prune it */
90 read_directory(dir
, path
, base
, baselen
, pathspec
);
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
);
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";
112 int cmd_add(int argc
, const char **argv
, const char *prefix
)
115 int verbose
= 0, show_only
= 0, ignored_too
= 0;
116 const char **pathspec
;
117 struct dir_struct dir
;
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
);
136 newfd
= hold_lock_file_for_update(&lock_file
, get_index_file(), 1);
138 for (i
= 1; i
< argc
; i
++) {
139 const char *arg
= argv
[i
];
143 if (!strcmp(arg
, "--")) {
147 if (!strcmp(arg
, "-n")) {
151 if (!strcmp(arg
, "-f")) {
155 if (!strcmp(arg
, "-v")) {
159 usage(builtin_add_usage
);
162 fprintf(stderr
, "Nothing specified, nothing added.\n");
163 fprintf(stderr
, "Maybe you wanted to say 'git add .'?\n");
166 pathspec
= get_pathspec(prefix
, argv
+ i
);
168 fill_directory(&dir
, pathspec
);
171 const char *sep
= "", *eof
= "";
172 for (i
= 0; i
< dir
.nr
; i
++) {
173 if (!ignored_too
&& dir
.entries
[i
]->ignored
)
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");
207 for (i
= 0; i
< dir
.nr
; i
++)
208 add_file_to_index(dir
.entries
[i
]->name
, verbose
);
210 if (active_cache_changed
) {
211 if (write_cache(newfd
, active_cache
, active_nr
) ||
212 close(newfd
) || commit_lock_file(&lock_file
))
213 die("Unable to write new index file");