patch: quote filenames when necessary
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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
diff --git a/src/diff_print.c b/src/diff_print.c
index 0253ca6..93c887b 100644
--- a/src/diff_print.c
+++ b/src/diff_print.c
@@ -300,46 +300,67 @@ static int diff_print_oid_range(
return git_buf_oom(out) ? -1 : 0;
}
+static int diff_delta_format_path(
+ git_buf *out, const char *prefix, const char *filename)
+{
+ if (git_buf_joinpath(out, prefix, filename) < 0)
+ return -1;
+
+ return git_buf_quote(out);
+}
+
+
static int diff_delta_format_with_paths(
git_buf *out,
const git_diff_delta *delta,
- const char *oldpfx,
- const char *newpfx,
- const char *template)
+ const char *template,
+ const char *oldpath,
+ const char *newpath)
{
- const char *oldpath = delta->old_file.path;
- const char *newpath = delta->new_file.path;
-
- if (git_oid_iszero(&delta->old_file.id)) {
- oldpfx = "";
+ if (git_oid_iszero(&delta->old_file.id))
oldpath = "/dev/null";
- }
- if (git_oid_iszero(&delta->new_file.id)) {
- newpfx = "";
+
+ if (git_oid_iszero(&delta->new_file.id))
newpath = "/dev/null";
- }
- return git_buf_printf(out, template, oldpfx, oldpath, newpfx, newpath);
+ return git_buf_printf(out, template, oldpath, newpath);
}
int diff_delta_format_rename_header(
git_buf *out,
const git_diff_delta *delta)
{
+ git_buf old_path = GIT_BUF_INIT, new_path = GIT_BUF_INIT;
+ int error = 0;
+
if (delta->similarity > 100) {
giterr_set(GITERR_PATCH, "invalid similarity %d", delta->similarity);
- return -1;
+ error = -1;
+ goto done;
}
+ if ((error = git_buf_puts(&old_path, delta->old_file.path)) < 0 ||
+ (error = git_buf_puts(&new_path, delta->new_file.path)) < 0 ||
+ (error = git_buf_quote(&old_path)) < 0 ||
+ (error = git_buf_quote(&new_path)) < 0)
+ goto done;
+
git_buf_printf(out,
"similarity index %d%%\n"
"rename from %s\n"
"rename to %s\n",
delta->similarity,
- delta->old_file.path,
- delta->new_file.path);
+ old_path.ptr,
+ new_path.ptr);
- return git_buf_oom(out) ? -1 : 0;
+ if (git_buf_oom(out))
+ error = -1;
+
+done:
+ git_buf_free(&old_path);
+ git_buf_free(&new_path);
+
+ return error;
}
int git_diff_delta__format_file_header(
@@ -349,7 +370,9 @@ int git_diff_delta__format_file_header(
const char *newpfx,
int oid_strlen)
{
+ git_buf old_path = GIT_BUF_INIT, new_path = GIT_BUF_INIT;
bool skip_index;
+ int error = 0;
if (!oldpfx)
oldpfx = DIFF_OLD_PREFIX_DEFAULT;
@@ -358,13 +381,21 @@ int git_diff_delta__format_file_header(
if (!oid_strlen)
oid_strlen = GIT_ABBREV_DEFAULT + 1;
+ if ((error = diff_delta_format_path(
+ &old_path, oldpfx, delta->old_file.path)) < 0 ||
+ (error = diff_delta_format_path(
+ &new_path, newpfx, delta->new_file.path)) < 0)
+ goto done;
+
git_buf_clear(out);
- git_buf_printf(out, "diff --git %s%s %s%s\n",
- oldpfx, delta->old_file.path, newpfx, delta->new_file.path);
+ git_buf_printf(out, "diff --git %s %s\n",
+ old_path.ptr, new_path.ptr);
- if (delta->status == GIT_DELTA_RENAMED)
- GITERR_CHECK_ERROR(diff_delta_format_rename_header(out, delta));
+ if (delta->status == GIT_DELTA_RENAMED) {
+ if ((error = diff_delta_format_rename_header(out, delta)) < 0)
+ goto done;
+ }
skip_index = (delta->status == GIT_DELTA_RENAMED &&
delta->similarity == 100 &&
@@ -372,14 +403,22 @@ int git_diff_delta__format_file_header(
delta->new_file.mode == 0);
if (!skip_index) {
- GITERR_CHECK_ERROR(diff_print_oid_range(out, delta, oid_strlen));
+ if ((error = diff_print_oid_range(out, delta, oid_strlen)) < 0)
+ goto done;
if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0)
- diff_delta_format_with_paths(
- out, delta, oldpfx, newpfx, "--- %s%s\n+++ %s%s\n");
+ diff_delta_format_with_paths(out, delta,
+ "--- %s\n+++ %s\n", old_path.ptr, new_path.ptr);
}
- return git_buf_oom(out) ? -1 : 0;
+ if (git_buf_oom(out))
+ error = -1;
+
+done:
+ git_buf_free(&old_path);
+ git_buf_free(&new_path);
+
+ return error;
}
static int format_binary(
@@ -420,6 +459,33 @@ static int format_binary(
return 0;
}
+static int diff_print_patch_file_binary_noshow(
+ diff_print_info *pi, git_diff_delta *delta,
+ const char *old_pfx, const char *new_pfx,
+ const git_diff_binary *binary)
+{
+ git_buf old_path = GIT_BUF_INIT, new_path = GIT_BUF_INIT;
+ int error;
+
+ if ((error = diff_delta_format_path(
+ &old_path, old_pfx, delta->old_file.path)) < 0 ||
+ (error = diff_delta_format_path(
+ &new_path, new_pfx, delta->new_file.path)) < 0)
+ goto done;
+
+
+ pi->line.num_lines = 1;
+ error = diff_delta_format_with_paths(
+ pi->buf, delta, "Binary files %s and %s differ\n",
+ old_path.ptr, new_path.ptr);
+
+done:
+ git_buf_free(&old_path);
+ git_buf_free(&new_path);
+
+ return error;
+}
+
static int diff_print_patch_file_binary(
diff_print_info *pi, git_diff_delta *delta,
const char *old_pfx, const char *new_pfx,
@@ -429,7 +495,8 @@ static int diff_print_patch_file_binary(
int error;
if ((pi->flags & GIT_DIFF_SHOW_BINARY) == 0)
- goto noshow;
+ return diff_print_patch_file_binary_noshow(
+ pi, delta, old_pfx, new_pfx, binary);
if (binary->new_file.datalen == 0 && binary->old_file.datalen == 0)
return 0;
@@ -446,18 +513,14 @@ static int diff_print_patch_file_binary(
if (error == GIT_EBUFS) {
giterr_clear();
git_buf_truncate(pi->buf, pre_binary_size);
- goto noshow;
+
+ return diff_print_patch_file_binary_noshow(
+ pi, delta, old_pfx, new_pfx, binary);
}
}
pi->line.num_lines++;
return error;
-
-noshow:
- pi->line.num_lines = 1;
- return diff_delta_format_with_paths(
- pi->buf, delta, old_pfx, new_pfx,
- "Binary files %s%s and %s%s differ\n");
}
static int diff_print_patch_file(