diff: use mks_tempfile_dt()

Git uses temporary files to pass the contents of blobs to external diff programs and textconv filters. It calls mks_tempfile_ts() to create them, which puts them all in the same directory. This requires adding a random name prefix. Use mks_tempfile_dt() instead, which allows the files to have arbitrary names, each in their own separate temporary directory. This way they can have the same basename as the original blob, which looks nicer in graphical diff programs. The test in t4020 to check the prettiness of the temporary paths was neutered by 5476bdf0e8 (diff tests: don't ignore "git diff" exit code in "read" loop, 2022-03-07), which removed its grep check without replacing it with an equivalent test_cmp check. Add one that only checks the basename of the temporary file and nothing else. And make the test more robust while at it, by using test_when_finished to get rid of the added file even if the test fails. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Apr 20, 2022 at 22:30 UTC 8af759374ec174372b841d2f8bc24edcba385418
2 files changed +5 -11
diff.c
+1 -7
@@ -4136,18 +4136,13 @@ static void prep_temp_blob(struct index_state *istate,
4136 int mode)
4137 {
4138 struct strbuf buf = STRBUF_INIT;
4139 - struct strbuf tempfile = STRBUF_INIT;
4139 char *path_dup = xstrdup(path);
4140 const char *base = basename(path_dup);
4141 struct checkout_metadata meta;
4142
4143 init_checkout_metadata(&meta, NULL, NULL, oid);
4144
4146 - /* Generate "XXXXXX_basename.ext" */
4147 - strbuf_addstr(&tempfile, "XXXXXX_");
4148 - strbuf_addstr(&tempfile, base);
4149 -
4150 - temp->tempfile = mks_tempfile_ts(tempfile.buf, strlen(base) + 1);
4145 + temp->tempfile = mks_tempfile_dt("git-blob-XXXXXX", base);
4146 if (!temp->tempfile)
4147 die_errno("unable to create temp-file");
4148 if (convert_to_working_tree(istate, path,
@@ -4162,7 +4157,6 @@ static void prep_temp_blob(struct index_state *istate,
4157 oid_to_hex_r(temp->hex, oid);
4158 xsnprintf(temp->mode, sizeof(temp->mode), "%06o", mode);
4159 strbuf_release(&buf);
4165 - strbuf_release(&tempfile);
4160 free(path_dup);
4161 }
4162
t/t4020-diff-external.sh
+4 -4
@@ -206,17 +206,17 @@ test_expect_success 'GIT_EXTERNAL_DIFF path counter/total' '
206 '
207
208 test_expect_success 'GIT_EXTERNAL_DIFF generates pretty paths' '
209 + test_when_finished "git rm -f file.ext" &&
210 touch file.ext &&
211 git add file.ext &&
212 echo with extension > file.ext &&
213
214 cat >expect <<-EOF &&
214 - file.ext file $(git rev-parse --verify HEAD:file) 100644 file.ext $(test_oid zero) 100644
215 + file.ext
216 EOF
217 GIT_EXTERNAL_DIFF=echo git diff file.ext >out &&
217 - cut -d" " -f1,3- <out >actual &&
218 - git update-index --force-remove file.ext &&
219 - rm file.ext
218 + basename $(cut -d" " -f2 <out) >actual &&
219 + test_cmp expect actual
220 '
221
222 echo "#!$SHELL_PATH" >fake-diff.sh