blob: 105a9f0e1f6aef67f67d985a40a036a4ba833cbe [file] [log] [blame]
Linus Torvalds0d781532006-05-17 09:33:32 -07001/*
2 * "git add" builtin command
3 *
4 * Copyright (C) 2006 Linus Torvalds
5 */
Linus Torvalds0d781532006-05-17 09:33:32 -07006#include "cache.h"
7#include "builtin.h"
8#include "dir.h"
Junio C Hamano5cde71d2006-12-10 20:55:50 -08009#include "exec_cmd.h"
Junio C Hamano93872e02006-05-20 01:28:49 -070010#include "cache-tree.h"
Junio C Hamanodfdac5d2007-04-20 01:39:39 -070011#include "diff.h"
12#include "diffcore.h"
13#include "commit.h"
14#include "revision.h"
Linus Torvalds0d781532006-05-17 09:33:32 -070015
16static const char builtin_add_usage[] =
Brian Hetro480611d2007-08-25 23:20:06 -040017"git-add [-n] [-v] [-f] [--interactive | -i] [-u] [--refresh] [--] <filepattern>...";
Linus Torvalds0d781532006-05-17 09:33:32 -070018
Jeff King93c44d42007-05-12 02:42:00 -040019static int take_worktree_changes;
James Bowes896bdfa2007-02-27 22:31:10 -050020static const char *excludes_file;
21
Linus Torvalds0d781532006-05-17 09:33:32 -070022static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
23{
Linus Torvaldsf2593392006-05-17 13:23:19 -070024 char *seen;
25 int i, specs;
Linus Torvalds0d781532006-05-17 09:33:32 -070026 struct dir_entry **src, **dst;
27
Linus Torvaldsf2593392006-05-17 13:23:19 -070028 for (specs = 0; pathspec[specs]; specs++)
29 /* nothing */;
Peter Eriksen28f75812006-07-25 09:30:18 +020030 seen = xcalloc(specs, 1);
Linus Torvaldsf2593392006-05-17 13:23:19 -070031
Linus Torvalds0d781532006-05-17 09:33:32 -070032 src = dst = dir->entries;
33 i = dir->nr;
34 while (--i >= 0) {
35 struct dir_entry *entry = *src++;
Junio C Hamano4d06f8a2006-12-29 11:01:31 -080036 if (match_pathspec(pathspec, entry->name, entry->len,
37 prefix, seen))
38 *dst++ = entry;
Linus Torvalds0d781532006-05-17 09:33:32 -070039 }
40 dir->nr = dst - dir->entries;
Linus Torvaldsf2593392006-05-17 13:23:19 -070041
42 for (i = 0; i < specs; i++) {
Jeff Kinge96980e2007-06-12 23:42:14 +020043 if (!seen[i] && !file_exists(pathspec[i]))
44 die("pathspec '%s' did not match any files",
45 pathspec[i]);
Linus Torvaldsf2593392006-05-17 13:23:19 -070046 }
Linus Torvalds0d781532006-05-17 09:33:32 -070047}
48
Jeff Kinge96980e2007-06-12 23:42:14 +020049static void fill_directory(struct dir_struct *dir, const char **pathspec,
50 int ignored_too)
Linus Torvalds0d781532006-05-17 09:33:32 -070051{
52 const char *path, *base;
53 int baselen;
54
55 /* Set up the default git porcelain excludes */
56 memset(dir, 0, sizeof(*dir));
Jeff Kinge96980e2007-06-12 23:42:14 +020057 if (!ignored_too) {
58 dir->collect_ignored = 1;
59 dir->exclude_per_dir = ".gitignore";
60 path = git_path("info/exclude");
61 if (!access(path, R_OK))
62 add_excludes_from_file(dir, path);
Thomas Schwinge8b4aee02007-07-28 20:26:35 +020063 if (excludes_file != NULL && !access(excludes_file, R_OK))
Jeff Kinge96980e2007-06-12 23:42:14 +020064 add_excludes_from_file(dir, excludes_file);
65 }
Linus Torvalds0d781532006-05-17 09:33:32 -070066
67 /*
68 * Calculate common prefix for the pathspec, and
69 * use that to optimize the directory walk
70 */
71 baselen = common_prefix(pathspec);
72 path = ".";
73 base = "";
74 if (baselen) {
75 char *common = xmalloc(baselen + 1);
Linus Torvalds0d781532006-05-17 09:33:32 -070076 memcpy(common, *pathspec, baselen);
77 common[baselen] = 0;
78 path = base = common;
79 }
80
81 /* Read the directory and prune it */
Linus Torvalds9fc42d62007-03-30 20:39:30 -070082 read_directory(dir, path, base, baselen, pathspec);
Linus Torvalds0d781532006-05-17 09:33:32 -070083 if (pathspec)
84 prune_directory(dir, pathspec, baselen);
85}
86
Junio C Hamanodfdac5d2007-04-20 01:39:39 -070087static void update_callback(struct diff_queue_struct *q,
88 struct diff_options *opt, void *cbdata)
89{
90 int i, verbose;
91
92 verbose = *((int *)cbdata);
93 for (i = 0; i < q->nr; i++) {
94 struct diff_filepair *p = q->queue[i];
95 const char *path = p->one->path;
96 switch (p->status) {
97 default:
98 die("unexpacted diff status %c", p->status);
99 case DIFF_STATUS_UNMERGED:
100 case DIFF_STATUS_MODIFIED:
101 add_file_to_cache(path, verbose);
102 break;
103 case DIFF_STATUS_DELETED:
104 remove_file_from_cache(path);
Junio C Hamanoa4882c22007-08-15 14:12:14 -0700105 cache_tree_invalidate_path(active_cache_tree, path);
Junio C Hamanodfdac5d2007-04-20 01:39:39 -0700106 if (verbose)
107 printf("remove '%s'\n", path);
108 break;
109 }
110 }
111}
112
Salikh Zakirov2ed2c222007-08-16 02:01:43 +0900113static void update(int verbose, const char *prefix, const char **files)
Junio C Hamanodfdac5d2007-04-20 01:39:39 -0700114{
115 struct rev_info rev;
Salikh Zakirov2ed2c222007-08-16 02:01:43 +0900116 init_revisions(&rev, prefix);
Junio C Hamanodfdac5d2007-04-20 01:39:39 -0700117 setup_revisions(0, NULL, &rev, NULL);
Salikh Zakirov2ed2c222007-08-16 02:01:43 +0900118 rev.prune_data = get_pathspec(prefix, files);
Junio C Hamanodfdac5d2007-04-20 01:39:39 -0700119 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
120 rev.diffopt.format_callback = update_callback;
121 rev.diffopt.format_callback_data = &verbose;
122 if (read_cache() < 0)
123 die("index file corrupt");
124 run_diff_files(&rev, 0);
125}
126
Alexandre Julliardd6168132007-08-11 23:59:01 +0200127static void refresh(int verbose, const char **pathspec)
128{
129 char *seen;
130 int i, specs;
131
132 for (specs = 0; pathspec[specs]; specs++)
133 /* nothing */;
134 seen = xcalloc(specs, 1);
135 if (read_cache() < 0)
136 die("index file corrupt");
137 refresh_index(&the_index, verbose ? 0 : REFRESH_QUIET, pathspec, seen);
138 for (i = 0; i < specs; i++) {
139 if (!seen[i])
140 die("pathspec '%s' did not match any files", pathspec[i]);
141 }
142}
143
James Bowes896bdfa2007-02-27 22:31:10 -0500144static int git_add_config(const char *var, const char *value)
145{
146 if (!strcmp(var, "core.excludesfile")) {
147 if (!value)
148 die("core.excludesfile without value");
149 excludes_file = xstrdup(value);
150 return 0;
151 }
152
153 return git_default_config(var, value);
154}
155
Junio C Hamano021b6e42006-06-06 12:51:49 -0700156static struct lock_file lock_file;
Linus Torvalds0d781532006-05-17 09:33:32 -0700157
Petr Baudisb39c53e2007-08-29 00:41:23 +0200158static const char ignore_error[] =
Junio C Hamano6a1ad322006-12-25 17:46:38 -0800159"The following paths are ignored by one of your .gitignore files:\n";
160
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700161int cmd_add(int argc, const char **argv, const char *prefix)
Linus Torvalds0d781532006-05-17 09:33:32 -0700162{
163 int i, newfd;
Alexandre Julliardd6168132007-08-11 23:59:01 +0200164 int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
Linus Torvalds0d781532006-05-17 09:33:32 -0700165 const char **pathspec;
166 struct dir_struct dir;
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800167 int add_interactive = 0;
168
169 for (i = 1; i < argc; i++) {
Junio C Hamanodf59afe2007-01-17 10:52:36 -0800170 if (!strcmp("--interactive", argv[i]) ||
171 !strcmp("-i", argv[i]))
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800172 add_interactive++;
173 }
174 if (add_interactive) {
175 const char *args[] = { "add--interactive", NULL };
176
177 if (add_interactive != 1 || argc != 2)
178 die("add --interactive does not take any parameters");
179 execv_git_cmd(args);
180 exit(1);
181 }
Linus Torvalds0d781532006-05-17 09:33:32 -0700182
James Bowes896bdfa2007-02-27 22:31:10 -0500183 git_config(git_add_config);
Linus Torvalds0d781532006-05-17 09:33:32 -0700184
Junio C Hamano30ca07a2007-03-31 23:09:02 -0700185 newfd = hold_locked_index(&lock_file, 1);
Linus Torvalds0d781532006-05-17 09:33:32 -0700186
Linus Torvalds0d781532006-05-17 09:33:32 -0700187 for (i = 1; i < argc; i++) {
188 const char *arg = argv[i];
189
190 if (arg[0] != '-')
191 break;
192 if (!strcmp(arg, "--")) {
193 i++;
194 break;
195 }
196 if (!strcmp(arg, "-n")) {
197 show_only = 1;
198 continue;
199 }
Junio C Hamano6a1ad322006-12-25 17:46:38 -0800200 if (!strcmp(arg, "-f")) {
201 ignored_too = 1;
202 continue;
203 }
Linus Torvalds0d781532006-05-17 09:33:32 -0700204 if (!strcmp(arg, "-v")) {
205 verbose = 1;
206 continue;
207 }
Junio C Hamanodfdac5d2007-04-20 01:39:39 -0700208 if (!strcmp(arg, "-u")) {
Jeff King93c44d42007-05-12 02:42:00 -0400209 take_worktree_changes = 1;
Junio C Hamanodfdac5d2007-04-20 01:39:39 -0700210 continue;
211 }
Alexandre Julliardd6168132007-08-11 23:59:01 +0200212 if (!strcmp(arg, "--refresh")) {
213 refresh_only = 1;
214 continue;
215 }
Ramsay Allan Jones8cdf3362006-08-03 16:48:41 +0100216 usage(builtin_add_usage);
Linus Torvalds0d781532006-05-17 09:33:32 -0700217 }
Junio C Hamanodfdac5d2007-04-20 01:39:39 -0700218
Jeff King93c44d42007-05-12 02:42:00 -0400219 if (take_worktree_changes) {
Salikh Zakirov2ed2c222007-08-16 02:01:43 +0900220 update(verbose, prefix, argv + i);
Junio C Hamanodfdac5d2007-04-20 01:39:39 -0700221 goto finish;
222 }
223
Junio C Hamano93b0d862006-12-20 13:06:46 -0800224 if (argc <= i) {
225 fprintf(stderr, "Nothing specified, nothing added.\n");
226 fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
227 return 0;
228 }
Linus Torvalds0d781532006-05-17 09:33:32 -0700229 pathspec = get_pathspec(prefix, argv + i);
230
Alexandre Julliardd6168132007-08-11 23:59:01 +0200231 if (refresh_only) {
232 refresh(verbose, pathspec);
233 goto finish;
234 }
235
Jeff Kinge96980e2007-06-12 23:42:14 +0200236 fill_directory(&dir, pathspec, ignored_too);
Linus Torvalds0d781532006-05-17 09:33:32 -0700237
238 if (show_only) {
239 const char *sep = "", *eof = "";
240 for (i = 0; i < dir.nr; i++) {
241 printf("%s%s", sep, dir.entries[i]->name);
242 sep = " ";
243 eof = "\n";
244 }
245 fputs(eof, stdout);
246 return 0;
247 }
248
Nicolas Pitre366bfcb2006-12-04 11:13:39 -0500249 if (read_cache() < 0)
250 die("index file corrupt");
251
Jeff Kinge96980e2007-06-12 23:42:14 +0200252 if (dir.ignored_nr) {
Petr Baudisb39c53e2007-08-29 00:41:23 +0200253 fprintf(stderr, ignore_error);
Jeff Kinge96980e2007-06-12 23:42:14 +0200254 for (i = 0; i < dir.ignored_nr; i++) {
255 fprintf(stderr, "%s\n", dir.ignored[i]->name);
Junio C Hamano6a1ad322006-12-25 17:46:38 -0800256 }
Jeff Kinge96980e2007-06-12 23:42:14 +0200257 fprintf(stderr, "Use -f if you really want to add them.\n");
Petr Baudisb39c53e2007-08-29 00:41:23 +0200258 die("no files added");
Junio C Hamano6a1ad322006-12-25 17:46:38 -0800259 }
260
Linus Torvalds0d781532006-05-17 09:33:32 -0700261 for (i = 0; i < dir.nr; i++)
Junio C Hamanofd1c3bf2007-04-01 22:14:40 -0700262 add_file_to_cache(dir.entries[i]->name, verbose);
Linus Torvalds0d781532006-05-17 09:33:32 -0700263
Junio C Hamanodfdac5d2007-04-20 01:39:39 -0700264 finish:
Linus Torvalds0d781532006-05-17 09:33:32 -0700265 if (active_cache_changed) {
266 if (write_cache(newfd, active_cache, active_nr) ||
Junio C Hamano30ca07a2007-03-31 23:09:02 -0700267 close(newfd) || commit_locked_index(&lock_file))
Linus Torvalds0d781532006-05-17 09:33:32 -0700268 die("Unable to write new index file");
269 }
270
271 return 0;
272}