Skip to content

Commit 78580ad

Browse files
committed
apply: test modifying a file after renaming it
Ensure that we cannot modify a file after it's been renamed out of the way. If multiple deltas exist for a single path, ensure that we do not attempt to modify a file after it's been renamed out of the way. To support this, we must track the paths that have been removed or renamed; add to a string map when we remove a path and remove from the string map if we recreate a path. Validate that we are not applying to a path that is in this map, unless the delta is a rename, since git supports renaming one file to two different places in two different deltas. Further, test that we cannot apply a modification delta to a path that will be created in the future by a rename (a path that does not yet exist.)
1 parent 605066e commit 78580ad

File tree

3 files changed

+125
-12
lines changed

3 files changed

+125
-12
lines changed

src/apply.c

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ static int apply_one(
425425
git_reader *postimage_reader,
426426
git_index *postimage,
427427
git_diff *diff,
428+
git_strmap *removed_paths,
428429
size_t i,
429430
const git_apply_options *opts)
430431
{
@@ -437,6 +438,7 @@ static int apply_one(
437438
git_filemode_t pre_filemode;
438439
git_index_entry pre_entry, post_entry;
439440
bool skip_preimage = false;
441+
size_t pos;
440442
int error;
441443

442444
if ((error = git_patch_from_diff(&patch, diff, i)) < 0)
@@ -458,11 +460,21 @@ static int apply_one(
458460
/*
459461
* We may be applying a second delta to an already seen file. If so,
460462
* use the already modified data in the postimage instead of the
461-
* content from the index or working directory. (Renames must be
462-
* specified before additional deltas since we are applying deltas
463-
* to the _target_ filename.)
463+
* content from the index or working directory. (Don't do this in
464+
* the case of a rename, which must be specified before additional
465+
* deltas since we apply deltas to the target filename.)
466+
*
467+
* Additionally, make sure that the file has not been deleted or renamed
468+
* out of the way; again, except in the rename case, since we support
469+
* renaming a single file into two target files.
464470
*/
465471
if (delta->status != GIT_DELTA_RENAMED) {
472+
pos = git_strmap_lookup_index(removed_paths, delta->old_file.path);
473+
if (git_strmap_valid_index(removed_paths, pos)) {
474+
error = apply_err("path '%s' has been renamed or deleted", delta->old_file.path);
475+
goto done;
476+
}
477+
466478
if ((error = git_reader_read(&pre_contents, &pre_id, &pre_filemode,
467479
postimage_reader, delta->old_file.path)) == 0) {
468480
skip_preimage = true;
@@ -531,6 +543,14 @@ static int apply_one(
531543
goto done;
532544
}
533545

546+
if (delta->status == GIT_DELTA_RENAMED ||
547+
delta->status == GIT_DELTA_DELETED)
548+
git_strmap_insert(removed_paths, delta->old_file.path, (char *)delta->old_file.path, &error);
549+
550+
if (delta->status == GIT_DELTA_RENAMED ||
551+
delta->status == GIT_DELTA_ADDED)
552+
git_strmap_delete(removed_paths, delta->new_file.path);
553+
534554
done:
535555
git_buf_dispose(&pre_contents);
536556
git_buf_dispose(&post_contents);
@@ -540,6 +560,32 @@ static int apply_one(
540560
return error;
541561
}
542562

563+
static int apply_deltas(
564+
git_repository *repo,
565+
git_reader *pre_reader,
566+
git_index *preimage,
567+
git_reader *post_reader,
568+
git_index *postimage,
569+
git_diff *diff,
570+
const git_apply_options *opts)
571+
{
572+
git_strmap *removed_paths;
573+
size_t i;
574+
int error;
575+
576+
if (git_strmap_alloc(&removed_paths) < 0)
577+
return -1;
578+
579+
for (i = 0; i < git_diff_num_deltas(diff); i++) {
580+
if ((error = apply_one(repo, pre_reader, preimage, post_reader, postimage, diff, removed_paths, i, opts)) < 0)
581+
goto done;
582+
}
583+
584+
done:
585+
git_strmap_free(removed_paths);
586+
return error;
587+
}
588+
543589
int git_apply_to_tree(
544590
git_index **out,
545591
git_repository *repo,
@@ -586,10 +632,8 @@ int git_apply_to_tree(
586632
goto done;
587633
}
588634

589-
for (i = 0; i < git_diff_num_deltas(diff); i++) {
590-
if ((error = apply_one(repo, pre_reader, NULL, post_reader, postimage, diff, i, &opts)) < 0)
591-
goto done;
592-
}
635+
if ((error = apply_deltas(repo, pre_reader, NULL, post_reader, postimage, diff, &opts)) < 0)
636+
goto done;
593637

594638
*out = postimage;
595639

@@ -725,7 +769,6 @@ int git_apply(
725769
git_index *index = NULL, *preimage = NULL, *postimage = NULL;
726770
git_reader *pre_reader = NULL, *post_reader = NULL;
727771
git_apply_options opts = GIT_APPLY_OPTIONS_INIT;
728-
size_t i;
729772
int error = GIT_EINVALID;
730773

731774
assert(repo && diff);
@@ -774,10 +817,8 @@ int git_apply(
774817
(error = git_indexwriter_init(&indexwriter, index)) < 0)
775818
goto done;
776819

777-
for (i = 0; i < git_diff_num_deltas(diff); i++) {
778-
if ((error = apply_one(repo, pre_reader, preimage, post_reader, postimage, diff, i, &opts)) < 0)
779-
goto done;
780-
}
820+
if ((error = apply_deltas(repo, pre_reader, preimage, post_reader, postimage, diff, &opts)) < 0)
821+
goto done;
781822

782823
switch (location) {
783824
case GIT_APPLY_LOCATION_BOTH:

tests/apply/apply_helpers.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,56 @@
344344
"-but the shin or knuckle is the nicest.\n" \
345345
"+but the shin or knuckle is the nicest!\n"
346346

347+
#define DIFF_RENAME_AFTER_MODIFY_TARGET_PATH \
348+
"diff --git a/beef.txt b/beef.txt\n" \
349+
"index 292cb60..61c686b 100644\n" \
350+
"--- a/beef.txt\n" \
351+
"+++ b/beef.txt\n" \
352+
"@@ -1,4 +1,4 @@\n" \
353+
"-VEAL SOUP!\n" \
354+
"+VEAL SOUP\n" \
355+
"\n" \
356+
" Put into a pot three quarts of water, three onions cut small, one\n" \
357+
" spoonful of black pepper pounded, and two of salt, with two or three\n" \
358+
"diff --git a/veal.txt b/beef.txt\n" \
359+
"similarity index 96%\n" \
360+
"rename from veal.txt\n" \
361+
"rename to beef.txt\n" \
362+
"index 94d2c01..292cb60 100644\n" \
363+
"--- a/veal.txt\n" \
364+
"+++ b/beef.txt\n" \
365+
"@@ -15,4 +15,4 @@ will curdle in the soup. For a change you may put a dozen ripe tomatos\n" \
366+
" in, first taking off their skins, by letting them stand a few minutes in\n" \
367+
" hot water, when they may be easily peeled. When made in this way you\n" \
368+
" must thicken it with the flour only. Any part of the veal may be used,\n" \
369+
"-but the shin or knuckle is the nicest.\n" \
370+
"+but the shin or knuckle is the nicest!\n"
371+
372+
#define DIFF_RENAME_AND_MODIFY_SOURCE_PATH \
373+
"diff --git a/veal.txt b/asdf.txt\n" \
374+
"similarity index 96%\n" \
375+
"rename from veal.txt\n" \
376+
"rename to asdf.txt\n" \
377+
"index 94d2c01..292cb60 100644\n" \
378+
"--- a/veal.txt\n" \
379+
"+++ b/asdf.txt\n" \
380+
"@@ -15,4 +15,4 @@ will curdle in the soup. For a change you may put a dozen ripe tomatos\n" \
381+
" in, first taking off their skins, by letting them stand a few minutes in\n" \
382+
" hot water, when they may be easily peeled. When made in this way you\n" \
383+
" must thicken it with the flour only. Any part of the veal may be used,\n" \
384+
"-but the shin or knuckle is the nicest.\n" \
385+
"+but the shin or knuckle is the nicest!\n" \
386+
"diff --git a/veal.txt b/veal.txt\n" \
387+
"index 292cb60..61c686b 100644\n" \
388+
"--- a/veal.txt\n" \
389+
"+++ b/veal.txt\n" \
390+
"@@ -1,4 +1,4 @@\n" \
391+
"-VEAL SOUP!\n" \
392+
"+VEAL SOUP\n" \
393+
"\n" \
394+
" Put into a pot three quarts of water, three onions cut small, one\n" \
395+
" spoonful of black pepper pounded, and two of salt, with two or three\n"
396+
347397
struct iterator_compare_data {
348398
struct merge_index_entry *expected;
349399
size_t cnt;

tests/apply/both.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,3 +676,25 @@ void test_apply_both__rename_delta_after_modify_delta(void)
676676

677677
git_diff_free(diff);
678678
}
679+
680+
void test_apply_both__cant_rename_after_modify_nonexistent_target_path(void)
681+
{
682+
git_diff *diff;
683+
684+
cl_git_pass(git_diff_from_buffer(&diff, DIFF_RENAME_AFTER_MODIFY_TARGET_PATH,
685+
strlen(DIFF_RENAME_AFTER_MODIFY_TARGET_PATH)));
686+
cl_git_fail(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
687+
688+
git_diff_free(diff);
689+
}
690+
691+
void test_apply_both__cant_modify_source_path_after_rename(void)
692+
{
693+
git_diff *diff;
694+
695+
cl_git_pass(git_diff_from_buffer(&diff, DIFF_RENAME_AND_MODIFY_SOURCE_PATH,
696+
strlen(DIFF_RENAME_AND_MODIFY_SOURCE_PATH)));
697+
cl_git_fail(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
698+
699+
git_diff_free(diff);
700+
}

0 commit comments

Comments
 (0)