diff: drop "name" parameter from prepare_temp_file()

The prepare_temp_file() function takes a diff_filespec as well as a filename. But it is almost certainly an error to pass in a name that isn't the filespec's "path" parameter, since that is the only thing that reliably tells us how to find the content (and indeed, this was the source of a recently-fixed bug). So let's drop the redundant "name" parameter and just use one->path throughout the function. This simplifies the interface a little bit, and makes it impossible for calling code to get it wrong. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jan 6, 2023 at 06:05 UTC f034bb1cad862a678030cdf0ae833636f9d7dbca
1 file changed +10 -11
diff.c
+10 -11
@@ -4213,7 +4213,6 @@ static void prep_temp_blob(struct index_state *istate,
4213 }
4214
4215 static struct diff_tempfile *prepare_temp_file(struct repository *r,
4216 - const char *name,
4216 struct diff_filespec *one)
4217 {
4218 struct diff_tempfile *temp = claim_diff_tempfile();
@@ -4231,18 +4230,18 @@ static struct diff_tempfile *prepare_temp_file(struct repository *r,
4230
4231 if (!S_ISGITLINK(one->mode) &&
4232 (!one->oid_valid ||
4234 - reuse_worktree_file(r->index, name, &one->oid, 1))) {
4233 + reuse_worktree_file(r->index, one->path, &one->oid, 1))) {
4234 struct stat st;
4236 - if (lstat(name, &st) < 0) {
4235 + if (lstat(one->path, &st) < 0) {
4236 if (errno == ENOENT)
4237 goto not_a_valid_file;
4239 - die_errno("stat(%s)", name);
4238 + die_errno("stat(%s)", one->path);
4239 }
4240 if (S_ISLNK(st.st_mode)) {
4241 struct strbuf sb = STRBUF_INIT;
4243 - if (strbuf_readlink(&sb, name, st.st_size) < 0)
4244 - die_errno("readlink(%s)", name);
4245 - prep_temp_blob(r->index, name, temp, sb.buf, sb.len,
4242 + if (strbuf_readlink(&sb, one->path, st.st_size) < 0)
4243 + die_errno("readlink(%s)", one->path);
4244 + prep_temp_blob(r->index, one->path, temp, sb.buf, sb.len,
4245 (one->oid_valid ?
4246 &one->oid : null_oid()),
4247 (one->oid_valid ?
@@ -4251,7 +4250,7 @@ static struct diff_tempfile *prepare_temp_file(struct repository *r,
4250 }
4251 else {
4252 /* we can borrow from the file in the work tree */
4254 - temp->name = name;
4253 + temp->name = one->path;
4254 if (!one->oid_valid)
4255 oid_to_hex_r(temp->hex, null_oid());
4256 else
@@ -4269,7 +4268,7 @@ static struct diff_tempfile *prepare_temp_file(struct repository *r,
4268 else {
4269 if (diff_populate_filespec(r, one, NULL))
4270 die("cannot read data blob for %s", one->path);
4272 - prep_temp_blob(r->index, name, temp,
4271 + prep_temp_blob(r->index, one->path, temp,
4272 one->data, one->size,
4273 &one->oid, one->mode);
4274 }
@@ -4280,7 +4279,7 @@ static void add_external_diff_name(struct repository *r,
4279 struct strvec *argv,
4280 struct diff_filespec *df)
4281 {
4283 - struct diff_tempfile *temp = prepare_temp_file(r, df->path, df);
4282 + struct diff_tempfile *temp = prepare_temp_file(r, df);
4283 strvec_push(argv, temp->name);
4284 strvec_push(argv, temp->hex);
4285 strvec_push(argv, temp->mode);
@@ -7034,7 +7033,7 @@ static char *run_textconv(struct repository *r,
7033 struct strbuf buf = STRBUF_INIT;
7034 int err = 0;
7035
7037 - temp = prepare_temp_file(r, spec->path, spec);
7036 + temp = prepare_temp_file(r, spec);
7037 strvec_push(&child.args, pgm);
7038 strvec_push(&child.args, temp->name);
7039