apply: handle exact renames Deltas containing exact renames are special; they simple indicate that a file was renamed without providing additional metadata (like the filemode). Teach the reader to provide the file mode and use the preimage's filemode in the case that the delta does not provide one.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
diff --git a/src/apply.c b/src/apply.c
index 8d94e78..a8ca106 100644
--- a/src/apply.c
+++ b/src/apply.c
@@ -433,6 +433,7 @@ static int apply_one(
char *filename = NULL;
unsigned int mode;
git_oid pre_id, post_id;
+ git_filemode_t pre_filemode;
git_index_entry pre_entry, post_entry;
int error;
@@ -453,7 +454,7 @@ static int apply_one(
}
if (delta->status != GIT_DELTA_ADDED) {
- error = git_reader_read(&pre_contents, &pre_id,
+ error = git_reader_read(&pre_contents, &pre_id, &pre_filemode,
preimage_reader, delta->old_file.path);
/* ENOTFOUND means the preimage was not found; apply failed. */
@@ -477,11 +478,15 @@ static int apply_one(
* application. (Without this, we would fail to write the
* postimage contents to any file that had been modified
* from HEAD on-disk, even if the patch application succeeded.)
+ * Use the contents from the delta where available - some
+ * fields may not be available, like the old file mode (eg in
+ * an exact rename situation) so trust the patch parsing to
+ * validate and use the preimage data in that case.
*/
if (preimage) {
memset(&pre_entry, 0, sizeof(git_index_entry));
pre_entry.path = delta->old_file.path;
- pre_entry.mode = delta->old_file.mode;
+ pre_entry.mode = delta->old_file.mode ? delta->old_file.mode : pre_filemode;
git_oid_cpy(&pre_entry.id, &pre_id);
if ((error = git_index_add(preimage, &pre_entry)) < 0)
diff --git a/src/reader.c b/src/reader.c
index 026bc2b..9375ff3 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -25,6 +25,7 @@ typedef struct {
static int tree_reader_read(
git_buf *out,
git_oid *out_id,
+ git_filemode_t *out_filemode,
git_reader *_reader,
const char *filename)
{
@@ -41,6 +42,9 @@ static int tree_reader_read(
if (out_id)
git_oid_cpy(out_id, git_tree_entry_id(tree_entry));
+ if (out_filemode)
+ *out_filemode = git_tree_entry_filemode(tree_entry);
+
done:
git_blob_free(blob);
git_tree_entry_free(tree_entry);
@@ -74,6 +78,7 @@ typedef struct {
static int workdir_reader_read(
git_buf *out,
git_oid *out_id,
+ git_filemode_t *out_filemode,
git_reader *_reader,
const char *filename)
{
@@ -130,6 +135,9 @@ static int workdir_reader_read(
if (out_id)
git_oid_cpy(out_id, &id);
+ if (out_filemode)
+ *out_filemode = filemode;
+
done:
git_filter_list_free(filters);
git_buf_dispose(&path);
@@ -173,6 +181,7 @@ typedef struct {
static int index_reader_read(
git_buf *out,
git_oid *out_id,
+ git_filemode_t *out_filemode,
git_reader *_reader,
const char *filename)
{
@@ -190,6 +199,9 @@ static int index_reader_read(
if (out_id)
git_oid_cpy(out_id, &entry->id);
+ if (out_filemode)
+ *out_filemode = entry->mode;
+
error = git_blob__getbuf(out, blob);
done:
@@ -229,12 +241,13 @@ int git_reader_for_index(
int git_reader_read(
git_buf *out,
git_oid *out_id,
+ git_filemode_t *out_filemode,
git_reader *reader,
const char *filename)
{
assert(out && reader && filename);
- return reader->read(out, out_id, reader, filename);
+ return reader->read(out, out_id, out_filemode, reader, filename);
}
void git_reader_free(git_reader *reader)
diff --git a/src/reader.h b/src/reader.h
index f9dd6b5..18a6a11 100644
--- a/src/reader.h
+++ b/src/reader.h
@@ -25,7 +25,7 @@ typedef struct git_reader git_reader;
* reader after disposing the underlying object that it reads.
*/
struct git_reader {
- int (*read)(git_buf *out, git_oid *out_oid, git_reader *reader, const char *filename);
+ int (*read)(git_buf *out, git_oid *out_oid, git_filemode_t *mode, git_reader *reader, const char *filename);
};
/**
@@ -93,6 +93,7 @@ extern int git_reader_for_workdir(
extern int git_reader_read(
git_buf *out,
git_oid *out_id,
+ git_filemode_t *out_filemode,
git_reader *reader,
const char *filename);
diff --git a/tests/apply/apply_helpers.h b/tests/apply/apply_helpers.h
index 1b40cd3..a3f7f18 100644
--- a/tests/apply/apply_helpers.h
+++ b/tests/apply/apply_helpers.h
@@ -112,6 +112,12 @@
"+but the shin OR knuckle is the nicest.\n" \
"+Another new line.\n" \
+#define DIFF_RENAME_FILE \
+ "diff --git a/beef.txt b/notbeef.txt\n" \
+ "similarity index 100%\n" \
+ "rename from beef.txt\n" \
+ "rename to notbeef.txt\n"
+
#define DIFF_RENAME_AND_MODIFY_FILE \
"diff --git a/beef.txt b/notbeef.txt\n" \
"similarity index 97%\n" \
diff --git a/tests/apply/both.c b/tests/apply/both.c
index abbabbd..15b2016 100644
--- a/tests/apply/both.c
+++ b/tests/apply/both.c
@@ -400,6 +400,31 @@ void test_apply_both__honors_crlf_attributes(void)
git_diff_free(diff);
}
+void test_apply_both__rename(void)
+{
+ git_diff *diff;
+
+ struct merge_index_entry both_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "notbeef.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" },
+ };
+ size_t both_expected_cnt = sizeof(both_expected) /
+ sizeof(struct merge_index_entry);
+
+ cl_git_pass(git_diff_from_buffer(&diff, DIFF_RENAME_FILE,
+ strlen(DIFF_RENAME_FILE)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+ validate_apply_index(repo, both_expected, both_expected_cnt);
+ validate_apply_workdir(repo, both_expected, both_expected_cnt);
+
+ git_diff_free(diff);
+}
+
void test_apply_both__rename_and_modify(void)
{
git_diff *diff;