]>
Commit | Line | Data |
---|---|---|
0d781539 LT |
1 | /* |
2 | * "git add" builtin command | |
3 | * | |
4 | * Copyright (C) 2006 Linus Torvalds | |
5 | */ | |
0d781539 LT |
6 | #include "cache.h" |
7 | #include "builtin.h" | |
8 | #include "dir.h" | |
5cde71d6 | 9 | #include "exec_cmd.h" |
93872e07 | 10 | #include "cache-tree.h" |
0d781539 LT |
11 | |
12 | static const char builtin_add_usage[] = | |
df59afe3 | 13 | "git-add [-n] [-v] [-f] [--interactive | -i] [--] <filepattern>..."; |
0d781539 | 14 | |
0d781539 LT |
15 | static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix) |
16 | { | |
f2593398 LT |
17 | char *seen; |
18 | int i, specs; | |
0d781539 LT |
19 | struct dir_entry **src, **dst; |
20 | ||
f2593398 LT |
21 | for (specs = 0; pathspec[specs]; specs++) |
22 | /* nothing */; | |
28f75818 | 23 | seen = xcalloc(specs, 1); |
f2593398 | 24 | |
0d781539 LT |
25 | src = dst = dir->entries; |
26 | i = dir->nr; | |
27 | while (--i >= 0) { | |
28 | struct dir_entry *entry = *src++; | |
4d06f8ac JH |
29 | if (match_pathspec(pathspec, entry->name, entry->len, |
30 | prefix, seen)) | |
31 | *dst++ = entry; | |
0d781539 LT |
32 | } |
33 | dir->nr = dst - dir->entries; | |
f2593398 LT |
34 | |
35 | for (i = 0; i < specs; i++) { | |
36 | struct stat st; | |
37 | const char *match; | |
38 | if (seen[i]) | |
39 | continue; | |
40 | ||
f2593398 | 41 | match = pathspec[i]; |
4d06f8ac | 42 | if (!match[0]) |
f2593398 | 43 | continue; |
4d06f8ac JH |
44 | |
45 | /* Existing file? We must have ignored it */ | |
46 | if (!lstat(match, &st)) { | |
47 | struct dir_entry *ent; | |
48 | ||
49 | ent = dir_add_name(dir, match, strlen(match)); | |
50 | ent->ignored = 1; | |
51 | if (S_ISDIR(st.st_mode)) | |
52 | ent->ignored_dir = 1; | |
53 | continue; | |
54 | } | |
f2593398 LT |
55 | die("pathspec '%s' did not match any files", match); |
56 | } | |
0d781539 LT |
57 | } |
58 | ||
59 | static void fill_directory(struct dir_struct *dir, const char **pathspec) | |
60 | { | |
61 | const char *path, *base; | |
62 | int baselen; | |
63 | ||
64 | /* Set up the default git porcelain excludes */ | |
65 | memset(dir, 0, sizeof(*dir)); | |
66 | dir->exclude_per_dir = ".gitignore"; | |
67 | path = git_path("info/exclude"); | |
68 | if (!access(path, R_OK)) | |
69 | add_excludes_from_file(dir, path); | |
70 | ||
71 | /* | |
72 | * Calculate common prefix for the pathspec, and | |
73 | * use that to optimize the directory walk | |
74 | */ | |
75 | baselen = common_prefix(pathspec); | |
76 | path = "."; | |
77 | base = ""; | |
78 | if (baselen) { | |
79 | char *common = xmalloc(baselen + 1); | |
0d781539 LT |
80 | memcpy(common, *pathspec, baselen); |
81 | common[baselen] = 0; | |
82 | path = base = common; | |
83 | } | |
84 | ||
85 | /* Read the directory and prune it */ | |
86 | read_directory(dir, path, base, baselen); | |
87 | if (pathspec) | |
88 | prune_directory(dir, pathspec, baselen); | |
89 | } | |
90 | ||
021b6e45 | 91 | static struct lock_file lock_file; |
0d781539 | 92 | |
6a1ad325 JH |
93 | static const char ignore_warning[] = |
94 | "The following paths are ignored by one of your .gitignore files:\n"; | |
95 | ||
a633fca0 | 96 | int cmd_add(int argc, const char **argv, const char *prefix) |
0d781539 LT |
97 | { |
98 | int i, newfd; | |
6a1ad325 | 99 | int verbose = 0, show_only = 0, ignored_too = 0; |
0d781539 LT |
100 | const char **pathspec; |
101 | struct dir_struct dir; | |
5cde71d6 JH |
102 | int add_interactive = 0; |
103 | ||
104 | for (i = 1; i < argc; i++) { | |
df59afe3 JH |
105 | if (!strcmp("--interactive", argv[i]) || |
106 | !strcmp("-i", argv[i])) | |
5cde71d6 JH |
107 | add_interactive++; |
108 | } | |
109 | if (add_interactive) { | |
110 | const char *args[] = { "add--interactive", NULL }; | |
111 | ||
112 | if (add_interactive != 1 || argc != 2) | |
113 | die("add --interactive does not take any parameters"); | |
114 | execv_git_cmd(args); | |
115 | exit(1); | |
116 | } | |
0d781539 LT |
117 | |
118 | git_config(git_default_config); | |
119 | ||
40aaae88 | 120 | newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1); |
0d781539 | 121 | |
0d781539 LT |
122 | for (i = 1; i < argc; i++) { |
123 | const char *arg = argv[i]; | |
124 | ||
125 | if (arg[0] != '-') | |
126 | break; | |
127 | if (!strcmp(arg, "--")) { | |
128 | i++; | |
129 | break; | |
130 | } | |
131 | if (!strcmp(arg, "-n")) { | |
132 | show_only = 1; | |
133 | continue; | |
134 | } | |
6a1ad325 JH |
135 | if (!strcmp(arg, "-f")) { |
136 | ignored_too = 1; | |
137 | continue; | |
138 | } | |
0d781539 LT |
139 | if (!strcmp(arg, "-v")) { |
140 | verbose = 1; | |
141 | continue; | |
142 | } | |
8cdf3364 | 143 | usage(builtin_add_usage); |
0d781539 | 144 | } |
93b0d86a JH |
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; | |
149 | } | |
0d781539 LT |
150 | pathspec = get_pathspec(prefix, argv + i); |
151 | ||
152 | fill_directory(&dir, pathspec); | |
153 | ||
154 | if (show_only) { | |
155 | const char *sep = "", *eof = ""; | |
156 | for (i = 0; i < dir.nr; i++) { | |
4d06f8ac | 157 | if (!ignored_too && dir.entries[i]->ignored) |
6a1ad325 | 158 | continue; |
0d781539 LT |
159 | printf("%s%s", sep, dir.entries[i]->name); |
160 | sep = " "; | |
161 | eof = "\n"; | |
162 | } | |
163 | fputs(eof, stdout); | |
164 | return 0; | |
165 | } | |
166 | ||
366bfcb6 NP |
167 | if (read_cache() < 0) |
168 | die("index file corrupt"); | |
169 | ||
6a1ad325 | 170 | if (!ignored_too) { |
4d06f8ac JH |
171 | int has_ignored = 0; |
172 | for (i = 0; i < dir.nr; i++) | |
173 | if (dir.entries[i]->ignored) | |
174 | has_ignored = 1; | |
175 | if (has_ignored) { | |
6a1ad325 | 176 | fprintf(stderr, ignore_warning); |
4d06f8ac JH |
177 | for (i = 0; i < dir.nr; i++) { |
178 | if (!dir.entries[i]->ignored) | |
6a1ad325 | 179 | continue; |
4d06f8ac JH |
180 | fprintf(stderr, "%s", dir.entries[i]->name); |
181 | if (dir.entries[i]->ignored_dir) | |
182 | fprintf(stderr, " (directory)"); | |
183 | fputc('\n', stderr); | |
6a1ad325 JH |
184 | } |
185 | fprintf(stderr, | |
186 | "Use -f if you really want to add them.\n"); | |
187 | exit(1); | |
188 | } | |
189 | } | |
190 | ||
0d781539 LT |
191 | for (i = 0; i < dir.nr; i++) |
192 | add_file_to_index(dir.entries[i]->name, verbose); | |
193 | ||
194 | if (active_cache_changed) { | |
195 | if (write_cache(newfd, active_cache, active_nr) || | |
6244b249 | 196 | close(newfd) || commit_lock_file(&lock_file)) |
0d781539 LT |
197 | die("Unable to write new index file"); |
198 | } | |
199 | ||
200 | return 0; | |
201 | } |