apply: move "already exists" logic to check_to_create()
The check_to_create_blob() function used to check only the case where we are applying to the working tree. Rename the function to check_to_create() and make it also responsible for checking the case where we apply to the index. Also make its caller responsible for issuing an error message. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Jun 7, 2012 at 14:10 UTC
ec15be02679ed5a98e4a96b23b99dd1b90196fae
1 file changed
+29
-11
builtin/apply.c
+29
-11
@@ -3249,9 +3249,21 @@ static int check_preimage(struct patch *patch, struct cache_entry **ce, struct s
3249
return 0;
3250
}
3251
3252
-static int check_to_create_blob(const char *new_name, int ok_if_exists)
3252
+
3253
+#define EXISTS_IN_INDEX 1
3254
+#define EXISTS_IN_WORKTREE 2
3255
+
3256
+static int check_to_create(const char *new_name, int ok_if_exists)
3257
{
3258
struct stat nst;
3259
+
3260
+ if (check_index &&
3261
+ cache_name_pos(new_name, strlen(new_name)) >= 0 &&
3262
+ !ok_if_exists)
3263
+ return EXISTS_IN_INDEX;
3264
+ if (cached)
3265
+ return 0;
3266
+
3267
if (!lstat(new_name, &nst)) {
3268
if (S_ISDIR(nst.st_mode) || ok_if_exists)
3269
return 0;
@@ -3265,10 +3277,10 @@ static int check_to_create_blob(const char *new_name, int ok_if_exists)
3277
if (has_symlink_leading_path(new_name, strlen(new_name)))
3278
return 0;
3279
3268
- return error(_("%s: already exists in working directory"), new_name);
3269
- }
3270
- else if ((errno != ENOENT) && (errno != ENOTDIR))
3280
+ return EXISTS_IN_WORKTREE;
3281
+ } else if ((errno != ENOENT) && (errno != ENOTDIR)) {
3282
return error("%s: %s", new_name, strerror(errno));
3283
+ }
3284
return 0;
3285
}
3286
@@ -3316,15 +3328,21 @@ static int check_patch(struct patch *patch)
3328
3329
if (new_name &&
3330
((0 < patch->is_new) | (0 < patch->is_rename) | patch->is_copy)) {
3319
- if (check_index &&
3320
- cache_name_pos(new_name, strlen(new_name)) >= 0 &&
3321
- !ok_if_exists)
3331
+ int err = check_to_create(new_name, ok_if_exists);
3332
+
3333
+ switch (err) {
3334
+ case 0:
3335
+ break; /* happy */
3336
+ case EXISTS_IN_INDEX:
3337
return error(_("%s: already exists in index"), new_name);
3323
- if (!cached) {
3324
- int err = check_to_create_blob(new_name, ok_if_exists);
3325
- if (err)
3326
- return err;
3338
+ break;
3339
+ case EXISTS_IN_WORKTREE:
3340
+ return error(_("%s: already exists in working directory"),
3341
+ new_name);
3342
+ default:
3343
+ return err;
3344
}
3345
+
3346
if (!patch->new_mode) {
3347
if (0 < patch->is_new)
3348
patch->new_mode = S_IFREG | 0644;