Use GIT_REFLOG_ACTION environment variable instead.
[git/dkf.git] / builtin-add.c
blob8ed4a6a9f32b9cd0f71289af2296cacf79864448
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"
12 static const char builtin_add_usage[] =
13 "git-add [-n] [-v] [-f] [--interactive] [--] <filepattern>...";
15 static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
17 char *seen;
18 int i, specs;
19 struct dir_entry **src, **dst;
21 for (specs = 0; pathspec[specs]; specs++)
22 /* nothing */;
23 seen = xcalloc(specs, 1);
25 src = dst = dir->entries;
26 i = dir->nr;
27 while (--i >= 0) {
28 struct dir_entry *entry = *src++;
29 int how = match_pathspec(pathspec, entry->name, entry->len,
30 prefix, seen);
32 * ignored entries can be added with exact match,
33 * but not with glob nor recursive.
35 if (!how ||
36 (entry->ignored_entry && how != MATCHED_EXACTLY)) {
37 free(entry);
38 continue;
40 *dst++ = entry;
42 dir->nr = dst - dir->entries;
44 for (i = 0; i < specs; i++) {
45 struct stat st;
46 const char *match;
47 if (seen[i])
48 continue;
50 /* Existing file? We must have ignored it */
51 match = pathspec[i];
52 if (!match[0] || !lstat(match, &st))
53 continue;
54 die("pathspec '%s' did not match any files", match);
58 static void fill_directory(struct dir_struct *dir, const char **pathspec)
60 const char *path, *base;
61 int baselen;
63 /* Set up the default git porcelain excludes */
64 memset(dir, 0, sizeof(*dir));
65 if (pathspec)
66 dir->show_both = 1;
67 dir->exclude_per_dir = ".gitignore";
68 path = git_path("info/exclude");
69 if (!access(path, R_OK))
70 add_excludes_from_file(dir, path);
73 * Calculate common prefix for the pathspec, and
74 * use that to optimize the directory walk
76 baselen = common_prefix(pathspec);
77 path = ".";
78 base = "";
79 if (baselen) {
80 char *common = xmalloc(baselen + 1);
81 memcpy(common, *pathspec, baselen);
82 common[baselen] = 0;
83 path = base = common;
86 /* Read the directory and prune it */
87 read_directory(dir, path, base, baselen);
88 if (pathspec)
89 prune_directory(dir, pathspec, baselen);
92 static struct lock_file lock_file;
94 static const char ignore_warning[] =
95 "The following paths are ignored by one of your .gitignore files:\n";
97 int cmd_add(int argc, const char **argv, const char *prefix)
99 int i, newfd;
100 int verbose = 0, show_only = 0, ignored_too = 0;
101 const char **pathspec;
102 struct dir_struct dir;
103 int add_interactive = 0;
105 for (i = 1; i < argc; i++) {
106 if (!strcmp("--interactive", argv[i]))
107 add_interactive++;
109 if (add_interactive) {
110 const char *args[] = { "add--interactive", NULL };
112 if (add_interactive != 1 || argc != 2)
113 die("add --interactive does not take any parameters");
114 execv_git_cmd(args);
115 exit(1);
118 git_config(git_default_config);
120 newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
122 for (i = 1; i < argc; i++) {
123 const char *arg = argv[i];
125 if (arg[0] != '-')
126 break;
127 if (!strcmp(arg, "--")) {
128 i++;
129 break;
131 if (!strcmp(arg, "-n")) {
132 show_only = 1;
133 continue;
135 if (!strcmp(arg, "-f")) {
136 ignored_too = 1;
137 continue;
139 if (!strcmp(arg, "-v")) {
140 verbose = 1;
141 continue;
143 usage(builtin_add_usage);
145 if (argc <= i) {
146 fprintf(stderr, "Nothing specified, nothing added.\n");
147 fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
148 return 0;
150 pathspec = get_pathspec(prefix, argv + i);
152 fill_directory(&dir, pathspec);
154 if (show_only) {
155 const char *sep = "", *eof = "";
156 for (i = 0; i < dir.nr; i++) {
157 if (!ignored_too && dir.entries[i]->ignored_entry)
158 continue;
159 printf("%s%s", sep, dir.entries[i]->name);
160 sep = " ";
161 eof = "\n";
163 fputs(eof, stdout);
164 return 0;
167 if (read_cache() < 0)
168 die("index file corrupt");
170 if (!ignored_too) {
171 int has_ignored = -1;
172 for (i = 0; has_ignored < 0 && i < dir.nr; i++)
173 if (dir.entries[i]->ignored_entry)
174 has_ignored = i;
175 if (0 <= has_ignored) {
176 fprintf(stderr, ignore_warning);
177 for (i = has_ignored; i < dir.nr; i++) {
178 if (!dir.entries[i]->ignored_entry)
179 continue;
180 fprintf(stderr, "%s\n", dir.entries[i]->name);
182 fprintf(stderr,
183 "Use -f if you really want to add them.\n");
184 exit(1);
188 for (i = 0; i < dir.nr; i++)
189 add_file_to_index(dir.entries[i]->name, verbose);
191 if (active_cache_changed) {
192 if (write_cache(newfd, active_cache, active_nr) ||
193 close(newfd) || commit_lock_file(&lock_file))
194 die("Unable to write new index file");
197 return 0;