9
#include "cache-tree.h"
10
#include "tree-walk.h"
11
#include "builtin.h"
12
+#include "refs.h"
13
14
/*
15
* Default to not allowing changes to the list of files. The
61
return -1;
62
}
63
63
-static int process_file(const char *path)
64
+static int remove_one_path(const char *path)
65
{
65
- int size, namelen, option, status;
66
- struct cache_entry *ce;
67
- struct stat st;
68
-
69
- status = lstat(path, &st);
70
-
71
- /* We probably want to do this in remove_file_from_cache() and
72
- * add_cache_entry() instead...
73
- */
74
- cache_tree_invalidate_path(active_cache_tree, path);
66
+ if (!allow_remove)
67
+ return error("%s: does not exist and --remove not passed", path);
68
+ if (remove_file_from_cache(path))
69
+ return error("%s: cannot remove from the index", path);
70
+ return 0;
71
+}
72
76
- if (status < 0 || S_ISDIR(st.st_mode)) {
77
- /* When we used to have "path" and now we want to add
78
- * "path/file", we need a way to remove "path" before
79
- * being able to add "path/file". However,
80
- * "git-update-index --remove path" would not work.
81
- * --force-remove can be used but this is more user
82
- * friendly, especially since we can do the opposite
83
- * case just fine without --force-remove.
84
- */
85
- if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
86
- if (allow_remove) {
87
- if (remove_file_from_cache(path))
88
- return error("%s: cannot remove from the index",
89
- path);
90
- else
91
- return 0;
92
- } else if (status < 0) {
93
- return error("%s: does not exist and --remove not passed",
94
- path);
95
- }
96
- }
97
- if (0 == status)
98
- return error("%s: is a directory - add files inside instead",
99
- path);
100
- else
101
- return error("lstat(\"%s\"): %s", path,
102
- strerror(errno));
103
- }
73
+/*
74
+ * Handle a path that couldn't be lstat'ed. It's either:
75
+ * - missing file (ENOENT or ENOTDIR). That's ok if we're
76
+ * supposed to be removing it and the removal actually
77
+ * succeeds.
78
+ * - permission error. That's never ok.
79
+ */
80
+static int process_lstat_error(const char *path, int err)
81
+{
82
+ if (err == ENOENT || err == ENOTDIR)
83
+ return remove_one_path(path);
84
+ return error("lstat(\"%s\"): %s", path, strerror(errno));
85
+}
86
105
- namelen = strlen(path);
106
- size = cache_entry_size(namelen);
107
- ce = xcalloc(1, size);
108
- memcpy(ce->name, path, namelen);
109
- ce->ce_flags = htons(namelen);
110
- fill_stat_cache_info(ce, &st);
111
-
112
- if (trust_executable_bit && has_symlinks)
113
- ce->ce_mode = create_ce_mode(st.st_mode);
114
- else {
115
- /* If there is an existing entry, pick the mode bits and type
116
- * from it, otherwise assume unexecutable regular file.
117
- */
118
- struct cache_entry *ent;
119
- int pos = cache_name_pos(path, namelen);
87
+static int add_one_path(struct cache_entry *old, const char *path, int len, struct stat *st)
88
+{
89
+ int option, size = cache_entry_size(len);
90
+ struct cache_entry *ce = xcalloc(1, size);
91
121
- ent = (0 <= pos) ? active_cache[pos] : NULL;
122
- ce->ce_mode = ce_mode_from_stat(ent, st.st_mode);
123
- }
92
+ memcpy(ce->name, path, len);
93
+ ce->ce_flags = htons(len);
94
+ fill_stat_cache_info(ce, st);
95
+ ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
96
125
- if (index_path(ce->sha1, path, &st, !info_only))
97
+ if (index_path(ce->sha1, path, st, !info_only))
98
return -1;
99
option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
100
option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
101
if (add_cache_entry(ce, option))
130
- return error("%s: cannot add to the index - missing --add option?",
131
- path);
102
+ return error("%s: cannot add to the index - missing --add option?", path);
103
return 0;
104
}
105
106
+/*
107
+ * Handle a path that was a directory. Four cases:
108
+ *
109
+ * - it's already a gitlink in the index, and we keep it that
110
+ * way, and update it if we can (if we cannot find the HEAD,
111
+ * we're going to keep it unchanged in the index!)
112
+ *
113
+ * - it's a *file* in the index, in which case it should be
114
+ * removed as a file if removal is allowed, since it doesn't
115
+ * exist as such any more. If removal isn't allowed, it's
116
+ * an error.
117
+ *
118
+ * (NOTE! This is old and arguably fairly strange behaviour.
119
+ * We might want to make this an error unconditionally, and
120
+ * use "--force-remove" if you actually want to force removal).
121
+ *
122
+ * - it used to exist as a subdirectory (ie multiple files with
123
+ * this particular prefix) in the index, in which case it's wrong
124
+ * to try to update it as a directory.
125
+ *
126
+ * - it doesn't exist at all in the index, but it is a valid
127
+ * git directory, and it should be *added* as a gitlink.
128
+ */
129
+static int process_directory(const char *path, int len, struct stat *st)
130
+{
131
+ unsigned char sha1[20];
132
+ int pos = cache_name_pos(path, len);
133
+
134
+ /* Exact match: file or existing gitlink */
135
+ if (pos >= 0) {
136
+ struct cache_entry *ce = active_cache[pos];
137
+ if (S_ISDIRLNK(ntohl(ce->ce_mode))) {
138
+
139
+ /* Do nothing to the index if there is no HEAD! */
140
+ if (resolve_gitlink_ref(path, "HEAD", sha1) < 0)
141
+ return 0;
142
+
143
+ return add_one_path(ce, path, len, st);
144
+ }
145
+ /* Should this be an unconditional error? */
146
+ return remove_one_path(path);
147
+ }
148
+
149
+ /* Inexact match: is there perhaps a subdirectory match? */
150
+ pos = -pos-1;
151
+ while (pos < active_nr) {
152
+ struct cache_entry *ce = active_cache[pos++];
153
+
154
+ if (strncmp(ce->name, path, len))
155
+ break;
156
+ if (ce->name[len] > '/')
157
+ break;
158
+ if (ce->name[len] < '/')
159
+ continue;
160
+
161
+ /* Subdirectory match - error out */
162
+ return error("%s: is a directory - add individual files instead", path);
163
+ }
164
+
165
+ /* No match - should we add it as a gitlink? */
166
+ if (!resolve_gitlink_ref(path, "HEAD", sha1))
167
+ return add_one_path(NULL, path, len, st);
168
+
169
+ /* Error out. */
170
+ return error("%s: is a directory - add files inside instead", path);
171
+}
172
+
173
+/*
174
+ * Process a regular file
175
+ */
176
+static int process_file(const char *path, int len, struct stat *st)
177
+{
178
+ int pos = cache_name_pos(path, len);
179
+ struct cache_entry *ce = pos < 0 ? NULL : active_cache[pos];
180
+
181
+ if (ce && S_ISDIRLNK(ntohl(ce->ce_mode)))
182
+ return error("%s is already a gitlink, not replacing", path);
183
+
184
+ return add_one_path(ce, path, len, st);
185
+}
186
+
187
+static int process_path(const char *path)
188
+{
189
+ int len;
190
+ struct stat st;
191
+
192
+ /* We probably want to do this in remove_file_from_cache() and
193
+ * add_cache_entry() instead...
194
+ */
195
+ cache_tree_invalidate_path(active_cache_tree, path);
196
+
197
+ /*
198
+ * First things first: get the stat information, to decide
199
+ * what to do about the pathname!
200
+ */
201
+ if (lstat(path, &st) < 0)
202
+ return process_lstat_error(path, errno);
203
+
204
+ len = strlen(path);
205
+ if (S_ISDIR(st.st_mode))
206
+ return process_directory(path, len, &st);
207
+
208
+ return process_file(path, len, &st);
209
+}
210
+
211
static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
212
const char *path, int stage)
213
{
286
report("remove '%s'", path);
287
goto free_return;
288
}
213
- if (process_file(p))
214
- die("Unable to process file %s", path);
289
+ if (process_path(p))
290
+ die("Unable to process path %s", path);
291
report("add '%s'", path);
292
free_return:
293
if (p < path || p > path + strlen(path))