write_entry(): fix misuses of `path` in error messages

The variables `path` and `ce->name`, at write_entry(), usually have the same contents, but that's not the case when using a checkout prefix or writing to a tempfile. (In fact, `path` will be either empty or dirty when writing to a tempfile.) Therefore, these variables cannot be used interchangeably. In this sense, fix wrong uses of `path` in error messages where it should really be `ce->name`, and add some regression tests. (Note: there doesn't seem to be any misuse in the other way around.) Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matheus Tavares committed Feb 16, 2021 at 11:06 UTC 9334ea8e92d4011f07a35a98adf6207e175f852c
2 files changed +27 -4
entry.c
+4 -4
@@ -282,7 +282,7 @@ static int write_entry(struct cache_entry *ce,
282 new_blob = read_blob_entry(ce, &size);
283 if (!new_blob)
284 return error("unable to read sha1 file of %s (%s)",
285 - path, oid_to_hex(&ce->oid));
285 + ce->name, oid_to_hex(&ce->oid));
286
287 /*
288 * We can't make a real symlink; write out a regular file entry
@@ -309,7 +309,7 @@ static int write_entry(struct cache_entry *ce,
309 new_blob = read_blob_entry(ce, &size);
310 if (!new_blob)
311 return error("unable to read sha1 file of %s (%s)",
312 - path, oid_to_hex(&ce->oid));
312 + ce->name, oid_to_hex(&ce->oid));
313 }
314
315 /*
@@ -354,7 +354,7 @@ static int write_entry(struct cache_entry *ce,
354
355 case S_IFGITLINK:
356 if (to_tempfile)
357 - return error("cannot create temporary submodule %s", path);
357 + return error("cannot create temporary submodule %s", ce->name);
358 if (mkdir(path, 0777) < 0)
359 return error("cannot create submodule directory %s", path);
360 sub = submodule_from_ce(ce);
@@ -365,7 +365,7 @@ static int write_entry(struct cache_entry *ce,
365 break;
366
367 default:
368 - return error("unknown file mode for %s in index", path);
368 + return error("unknown file mode for %s in index", ce->name);
369 }
370
371 finish:
t/t2006-checkout-index-basic.sh
+23
@@ -32,4 +32,27 @@ test_expect_success 'checkout-index reports errors (stdin)' '
32 test_i18ngrep not.in.the.cache stderr
33 '
34
35 +test_expect_success 'checkout-index --temp correctly reports error on missing blobs' '
36 + test_when_finished git reset --hard &&
37 + missing_blob=$(echo "no such blob here" | git hash-object --stdin) &&
38 + cat >objs <<-EOF &&
39 + 100644 $missing_blob file
40 + 120000 $missing_blob symlink
41 + EOF
42 + git update-index --index-info <objs &&
43 +
44 + test_must_fail git checkout-index --temp symlink file 2>stderr &&
45 + test_i18ngrep "unable to read sha1 file of file ($missing_blob)" stderr &&
46 + test_i18ngrep "unable to read sha1 file of symlink ($missing_blob)" stderr
47 +'
48 +
49 +test_expect_success 'checkout-index --temp correctly reports error for submodules' '
50 + git init sub &&
51 + test_commit -C sub file &&
52 + git submodule add ./sub &&
53 + git commit -m sub &&
54 + test_must_fail git checkout-index --temp sub 2>stderr &&
55 + test_i18ngrep "cannot create temporary submodule sub" stderr
56 +'
57 +
58 test_done