[PATCH] gitweb: Fix blob linenr links in pathinfo mode
[git/gitweb.git] / builtin-add.c
blobcb6e5906fb76f2460f212c54b557e5ae0ce23579
1 /*
2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
5 */
6 #include "cache.h"
7 #include "builtin.h"
8 #include "dir.h"
9 #include "exec_cmd.h"
10 #include "cache-tree.h"
11 #include "run-command.h"
12 #include "parse-options.h"
13 #include "diff.h"
14 #include "revision.h"
16 static const char * const builtin_add_usage[] = {
17 "git add [options] [--] <filepattern>...",
18 NULL
20 static int patch_interactive, add_interactive, edit_interactive;
21 static int take_worktree_changes;
23 static void fill_pathspec_matches(const char **pathspec, char *seen, int specs)
25 int num_unmatched = 0, i;
28 * Since we are walking the index as if we were walking the directory,
29 * we have to mark the matched pathspec as seen; otherwise we will
30 * mistakenly think that the user gave a pathspec that did not match
31 * anything.
33 for (i = 0; i < specs; i++)
34 if (!seen[i])
35 num_unmatched++;
36 if (!num_unmatched)
37 return;
38 for (i = 0; i < active_nr; i++) {
39 struct cache_entry *ce = active_cache[i];
40 match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen);
44 static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
46 char *seen;
47 int i, specs;
48 struct dir_entry **src, **dst;
50 for (specs = 0; pathspec[specs]; specs++)
51 /* nothing */;
52 seen = xcalloc(specs, 1);
54 src = dst = dir->entries;
55 i = dir->nr;
56 while (--i >= 0) {
57 struct dir_entry *entry = *src++;
58 if (match_pathspec(pathspec, entry->name, entry->len,
59 prefix, seen))
60 *dst++ = entry;
62 dir->nr = dst - dir->entries;
63 fill_pathspec_matches(pathspec, seen, specs);
65 for (i = 0; i < specs; i++) {
66 if (!seen[i] && pathspec[i][0] && !file_exists(pathspec[i]))
67 die("pathspec '%s' did not match any files",
68 pathspec[i]);
70 free(seen);
73 static void treat_gitlinks(const char **pathspec)
75 int i;
77 if (!pathspec || !*pathspec)
78 return;
80 for (i = 0; i < active_nr; i++) {
81 struct cache_entry *ce = active_cache[i];
82 if (S_ISGITLINK(ce->ce_mode)) {
83 int len = ce_namelen(ce), j;
84 for (j = 0; pathspec[j]; j++) {
85 int len2 = strlen(pathspec[j]);
86 if (len2 <= len || pathspec[j][len] != '/' ||
87 memcmp(ce->name, pathspec[j], len))
88 continue;
89 if (len2 == len + 1)
90 /* strip trailing slash */
91 pathspec[j] = xstrndup(ce->name, len);
92 else
93 die ("Path '%s' is in submodule '%.*s'",
94 pathspec[j], len, ce->name);
100 static void refresh(int verbose, const char **pathspec)
102 char *seen;
103 int i, specs;
105 for (specs = 0; pathspec[specs]; specs++)
106 /* nothing */;
107 seen = xcalloc(specs, 1);
108 refresh_index(&the_index, verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET,
109 pathspec, seen, "Unstaged changes after refreshing the index:");
110 for (i = 0; i < specs; i++) {
111 if (!seen[i])
112 die("pathspec '%s' did not match any files", pathspec[i]);
114 free(seen);
117 static const char **validate_pathspec(int argc, const char **argv, const char *prefix)
119 const char **pathspec = get_pathspec(prefix, argv);
121 if (pathspec) {
122 const char **p;
123 for (p = pathspec; *p; p++) {
124 if (has_symlink_leading_path(*p, strlen(*p))) {
125 int len = prefix ? strlen(prefix) : 0;
126 die("'%s' is beyond a symbolic link", *p + len);
131 return pathspec;
134 int run_add_interactive(const char *revision, const char *patch_mode,
135 const char **pathspec)
137 int status, ac, pc = 0;
138 const char **args;
140 if (pathspec)
141 while (pathspec[pc])
142 pc++;
144 args = xcalloc(sizeof(const char *), (pc + 5));
145 ac = 0;
146 args[ac++] = "add--interactive";
147 if (patch_mode)
148 args[ac++] = patch_mode;
149 if (revision)
150 args[ac++] = revision;
151 args[ac++] = "--";
152 if (pc) {
153 memcpy(&(args[ac]), pathspec, sizeof(const char *) * pc);
154 ac += pc;
156 args[ac] = NULL;
158 status = run_command_v_opt(args, RUN_GIT_CMD);
159 free(args);
160 return status;
163 int interactive_add(int argc, const char **argv, const char *prefix)
165 const char **pathspec = NULL;
167 if (argc) {
168 pathspec = validate_pathspec(argc, argv, prefix);
169 if (!pathspec)
170 return -1;
173 return run_add_interactive(NULL,
174 patch_interactive ? "--patch" : NULL,
175 pathspec);
178 static int edit_patch(int argc, const char **argv, const char *prefix)
180 char *file = xstrdup(git_path("ADD_EDIT.patch"));
181 const char *apply_argv[] = { "apply", "--recount", "--cached",
182 file, NULL };
183 struct child_process child;
184 struct rev_info rev;
185 int out;
186 struct stat st;
188 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
190 if (read_cache() < 0)
191 die ("Could not read the index");
193 init_revisions(&rev, prefix);
194 rev.diffopt.context = 7;
196 argc = setup_revisions(argc, argv, &rev, NULL);
197 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
198 out = open(file, O_CREAT | O_WRONLY, 0644);
199 if (out < 0)
200 die ("Could not open '%s' for writing.", file);
201 rev.diffopt.file = xfdopen(out, "w");
202 rev.diffopt.close_file = 1;
203 if (run_diff_files(&rev, 0))
204 die ("Could not write patch");
206 launch_editor(file, NULL, NULL);
208 if (stat(file, &st))
209 die_errno("Could not stat '%s'", file);
210 if (!st.st_size)
211 die("Empty patch. Aborted.");
213 memset(&child, 0, sizeof(child));
214 child.git_cmd = 1;
215 child.argv = apply_argv;
216 if (run_command(&child))
217 die ("Could not apply '%s'", file);
219 unlink(file);
220 return 0;
223 static struct lock_file lock_file;
225 static const char ignore_error[] =
226 "The following paths are ignored by one of your .gitignore files:\n";
228 static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
229 static int ignore_add_errors, addremove, intent_to_add;
231 static struct option builtin_add_options[] = {
232 OPT__DRY_RUN(&show_only),
233 OPT__VERBOSE(&verbose),
234 OPT_GROUP(""),
235 OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"),
236 OPT_BOOLEAN('p', "patch", &patch_interactive, "interactive patching"),
237 OPT_BOOLEAN('e', "edit", &edit_interactive, "edit current diff and apply"),
238 OPT_BOOLEAN('f', "force", &ignored_too, "allow adding otherwise ignored files"),
239 OPT_BOOLEAN('u', "update", &take_worktree_changes, "update tracked files"),
240 OPT_BOOLEAN('N', "intent-to-add", &intent_to_add, "record only the fact that the path will be added later"),
241 OPT_BOOLEAN('A', "all", &addremove, "add all, noticing removal of tracked files"),
242 OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"),
243 OPT_BOOLEAN( 0 , "ignore-errors", &ignore_add_errors, "just skip files which cannot be added because of errors"),
244 OPT_END(),
247 static int add_config(const char *var, const char *value, void *cb)
249 if (!strcasecmp(var, "add.ignore-errors")) {
250 ignore_add_errors = git_config_bool(var, value);
251 return 0;
253 return git_default_config(var, value, cb);
256 static int add_files(struct dir_struct *dir, int flags)
258 int i, exit_status = 0;
260 if (dir->ignored_nr) {
261 fprintf(stderr, ignore_error);
262 for (i = 0; i < dir->ignored_nr; i++)
263 fprintf(stderr, "%s\n", dir->ignored[i]->name);
264 fprintf(stderr, "Use -f if you really want to add them.\n");
265 die("no files added");
268 for (i = 0; i < dir->nr; i++)
269 if (add_file_to_cache(dir->entries[i]->name, flags)) {
270 if (!ignore_add_errors)
271 die("adding files failed");
272 exit_status = 1;
274 return exit_status;
277 int cmd_add(int argc, const char **argv, const char *prefix)
279 int exit_status = 0;
280 int newfd;
281 const char **pathspec;
282 struct dir_struct dir;
283 int flags;
284 int add_new_files;
285 int require_pathspec;
287 git_config(add_config, NULL);
289 argc = parse_options(argc, argv, prefix, builtin_add_options,
290 builtin_add_usage, PARSE_OPT_KEEP_ARGV0);
291 if (patch_interactive)
292 add_interactive = 1;
293 if (add_interactive)
294 exit(interactive_add(argc - 1, argv + 1, prefix));
296 if (edit_interactive)
297 return(edit_patch(argc, argv, prefix));
298 argc--;
299 argv++;
301 if (addremove && take_worktree_changes)
302 die("-A and -u are mutually incompatible");
303 if ((addremove || take_worktree_changes) && !argc) {
304 static const char *here[2] = { ".", NULL };
305 argc = 1;
306 argv = here;
309 add_new_files = !take_worktree_changes && !refresh_only;
310 require_pathspec = !take_worktree_changes;
312 newfd = hold_locked_index(&lock_file, 1);
314 flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
315 (show_only ? ADD_CACHE_PRETEND : 0) |
316 (intent_to_add ? ADD_CACHE_INTENT : 0) |
317 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) |
318 (!(addremove || take_worktree_changes)
319 ? ADD_CACHE_IGNORE_REMOVAL : 0));
321 if (require_pathspec && argc == 0) {
322 fprintf(stderr, "Nothing specified, nothing added.\n");
323 fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
324 return 0;
326 pathspec = validate_pathspec(argc, argv, prefix);
328 if (read_cache() < 0)
329 die("index file corrupt");
330 treat_gitlinks(pathspec);
332 if (add_new_files) {
333 int baselen;
335 /* Set up the default git porcelain excludes */
336 memset(&dir, 0, sizeof(dir));
337 if (!ignored_too) {
338 dir.flags |= DIR_COLLECT_IGNORED;
339 setup_standard_excludes(&dir);
342 /* This picks up the paths that are not tracked */
343 baselen = fill_directory(&dir, pathspec);
344 if (pathspec)
345 prune_directory(&dir, pathspec, baselen);
348 if (refresh_only) {
349 refresh(verbose, pathspec);
350 goto finish;
353 exit_status |= add_files_to_cache(prefix, pathspec, flags);
355 if (add_new_files)
356 exit_status |= add_files(&dir, flags);
358 finish:
359 if (active_cache_changed) {
360 if (write_cache(newfd, active_cache, active_nr) ||
361 commit_locked_index(&lock_file))
362 die("Unable to write new index file");
365 return exit_status;