Code

Add tests for git tag
[git.git] / builtin-add.c
1 /*
2  * "git add" builtin command
3  *
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 "diff.h"
12 #include "diffcore.h"
13 #include "commit.h"
14 #include "revision.h"
15 #include "run-command.h"
16 #include "parse-options.h"
18 static const char * const builtin_add_usage[] = {
19         "git-add [options] [--] <filepattern>...",
20         NULL
21 };
23 static int take_worktree_changes;
24 static const char *excludes_file;
26 static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
27 {
28         char *seen;
29         int i, specs;
30         struct dir_entry **src, **dst;
32         for (specs = 0; pathspec[specs];  specs++)
33                 /* nothing */;
34         seen = xcalloc(specs, 1);
36         src = dst = dir->entries;
37         i = dir->nr;
38         while (--i >= 0) {
39                 struct dir_entry *entry = *src++;
40                 if (match_pathspec(pathspec, entry->name, entry->len,
41                                    prefix, seen))
42                         *dst++ = entry;
43         }
44         dir->nr = dst - dir->entries;
46         for (i = 0; i < specs; i++) {
47                 if (!seen[i] && !file_exists(pathspec[i]))
48                         die("pathspec '%s' did not match any files",
49                                         pathspec[i]);
50         }
51         free(seen);
52 }
54 static void fill_directory(struct dir_struct *dir, const char **pathspec,
55                 int ignored_too)
56 {
57         const char *path, *base;
58         int baselen;
60         /* Set up the default git porcelain excludes */
61         memset(dir, 0, sizeof(*dir));
62         if (!ignored_too) {
63                 dir->collect_ignored = 1;
64                 dir->exclude_per_dir = ".gitignore";
65                 path = git_path("info/exclude");
66                 if (!access(path, R_OK))
67                         add_excludes_from_file(dir, path);
68                 if (excludes_file != NULL && !access(excludes_file, R_OK))
69                         add_excludes_from_file(dir, excludes_file);
70         }
72         /*
73          * Calculate common prefix for the pathspec, and
74          * use that to optimize the directory walk
75          */
76         baselen = common_prefix(pathspec);
77         path = ".";
78         base = "";
79         if (baselen)
80                 path = base = xmemdupz(*pathspec, baselen);
82         /* Read the directory and prune it */
83         read_directory(dir, path, base, baselen, pathspec);
84         if (pathspec)
85                 prune_directory(dir, pathspec, baselen);
86 }
88 static void update_callback(struct diff_queue_struct *q,
89                             struct diff_options *opt, void *cbdata)
90 {
91         int i, verbose;
93         verbose = *((int *)cbdata);
94         for (i = 0; i < q->nr; i++) {
95                 struct diff_filepair *p = q->queue[i];
96                 const char *path = p->one->path;
97                 switch (p->status) {
98                 default:
99                         die("unexpected diff status %c", p->status);
100                 case DIFF_STATUS_UNMERGED:
101                 case DIFF_STATUS_MODIFIED:
102                 case DIFF_STATUS_TYPE_CHANGED:
103                         add_file_to_cache(path, verbose);
104                         break;
105                 case DIFF_STATUS_DELETED:
106                         remove_file_from_cache(path);
107                         if (verbose)
108                                 printf("remove '%s'\n", path);
109                         break;
110                 }
111         }
114 void add_files_to_cache(int verbose, const char *prefix, const char **files)
116         struct rev_info rev;
117         init_revisions(&rev, prefix);
118         setup_revisions(0, NULL, &rev, NULL);
119         rev.prune_data = get_pathspec(prefix, files);
120         rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
121         rev.diffopt.format_callback = update_callback;
122         rev.diffopt.format_callback_data = &verbose;
123         run_diff_files(&rev, 0);
126 static void refresh(int verbose, const char **pathspec)
128         char *seen;
129         int i, specs;
131         for (specs = 0; pathspec[specs];  specs++)
132                 /* nothing */;
133         seen = xcalloc(specs, 1);
134         if (read_cache() < 0)
135                 die("index file corrupt");
136         refresh_index(&the_index, verbose ? 0 : REFRESH_QUIET, pathspec, seen);
137         for (i = 0; i < specs; i++) {
138                 if (!seen[i])
139                         die("pathspec '%s' did not match any files", pathspec[i]);
140         }
141         free(seen);
144 static int git_add_config(const char *var, const char *value)
146         if (!strcmp(var, "core.excludesfile")) {
147                 if (!value)
148                         die("core.excludesfile without value");
149                 excludes_file = xstrdup(value);
150                 return 0;
151         }
153         return git_default_config(var, value);
156 int interactive_add(void)
158         const char *argv[2] = { "add--interactive", NULL };
160         return run_command_v_opt(argv, RUN_GIT_CMD);
163 static struct lock_file lock_file;
165 static const char ignore_error[] =
166 "The following paths are ignored by one of your .gitignore files:\n";
168 static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
169 static int add_interactive = 0;
171 static struct option builtin_add_options[] = {
172         OPT__DRY_RUN(&show_only),
173         OPT__VERBOSE(&verbose),
174         OPT_GROUP(""),
175         OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"),
176         OPT_BOOLEAN('f', NULL, &ignored_too, "allow adding otherwise ignored files"),
177         OPT_BOOLEAN('u', NULL, &take_worktree_changes, "update tracked files"),
178         OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"),
179         OPT_END(),
180 };
182 int cmd_add(int argc, const char **argv, const char *prefix)
184         int i, newfd, orig_argc = argc;
185         const char **pathspec;
186         struct dir_struct dir;
188         argc = parse_options(argc, argv, builtin_add_options,
189                           builtin_add_usage, 0);
190         if (add_interactive) {
191                 if (add_interactive != 1 || orig_argc != 2)
192                         die("add --interactive does not take any parameters");
193                 exit(interactive_add());
194         }
196         git_config(git_add_config);
198         newfd = hold_locked_index(&lock_file, 1);
200         if (take_worktree_changes) {
201                 if (read_cache() < 0)
202                         die("index file corrupt");
203                 add_files_to_cache(verbose, prefix, argv);
204                 goto finish;
205         }
207         if (argc == 0) {
208                 fprintf(stderr, "Nothing specified, nothing added.\n");
209                 fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
210                 return 0;
211         }
212         pathspec = get_pathspec(prefix, argv);
214         if (refresh_only) {
215                 refresh(verbose, pathspec);
216                 goto finish;
217         }
219         fill_directory(&dir, pathspec, ignored_too);
221         if (show_only) {
222                 const char *sep = "", *eof = "";
223                 for (i = 0; i < dir.nr; i++) {
224                         printf("%s%s", sep, dir.entries[i]->name);
225                         sep = " ";
226                         eof = "\n";
227                 }
228                 fputs(eof, stdout);
229                 return 0;
230         }
232         if (read_cache() < 0)
233                 die("index file corrupt");
235         if (dir.ignored_nr) {
236                 fprintf(stderr, ignore_error);
237                 for (i = 0; i < dir.ignored_nr; i++) {
238                         fprintf(stderr, "%s\n", dir.ignored[i]->name);
239                 }
240                 fprintf(stderr, "Use -f if you really want to add them.\n");
241                 die("no files added");
242         }
244         for (i = 0; i < dir.nr; i++)
245                 add_file_to_cache(dir.entries[i]->name, verbose);
247  finish:
248         if (active_cache_changed) {
249                 if (write_cache(newfd, active_cache, active_nr) ||
250                     close(newfd) || commit_locked_index(&lock_file))
251                         die("Unable to write new index file");
252         }
254         return 0;