Skip to content

Commit 003b33a

Browse files
davvidgitster
authored andcommitted
diff: generate pretty filenames in prep_temp_blob()
Naturally, prep_temp_blob() did not care about filenames. As a result, GIT_EXTERNAL_DIFF and textconv generated filenames such as ".diff_XXXXXX". This modifies prep_temp_blob() to generate user-friendly filenames when creating temporary files. Diffing "name.ext" now generates "XXXXXX_name.ext". Signed-off-by: David Aguilar <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e1c0688 commit 003b33a

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,8 @@ extern int is_empty_blob_sha1(const unsigned char *sha1);
614614

615615
int git_mkstemp(char *path, size_t n, const char *template);
616616

617+
int git_mkstemps(char *path, size_t n, const char *template, int suffix_len);
618+
617619
/*
618620
* NOTE NOTE NOTE!!
619621
*

diff.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1964,8 +1964,16 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
19641964
{
19651965
int fd;
19661966
struct strbuf buf = STRBUF_INIT;
1967+
struct strbuf template = STRBUF_INIT;
1968+
char *path_dup = xstrdup(path);
1969+
const char *base = basename(path_dup);
19671970

1968-
fd = git_mkstemp(temp->tmp_path, PATH_MAX, ".diff_XXXXXX");
1971+
/* Generate "XXXXXX_basename.ext" */
1972+
strbuf_addstr(&template, "XXXXXX_");
1973+
strbuf_addstr(&template, base);
1974+
1975+
fd = git_mkstemps(temp->tmp_path, PATH_MAX, template.buf,
1976+
strlen(base) + 1);
19691977
if (fd < 0)
19701978
die("unable to create temp-file: %s", strerror(errno));
19711979
if (convert_to_working_tree(path,
@@ -1981,6 +1989,8 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
19811989
temp->hex[40] = 0;
19821990
sprintf(temp->mode, "%06o", mode);
19831991
strbuf_release(&buf);
1992+
strbuf_release(&template);
1993+
free(path_dup);
19841994
}
19851995

19861996
static struct diff_tempfile *prepare_temp_file(const char *name,

path.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,22 @@ int git_mkstemp(char *path, size_t len, const char *template)
139139
return mkstemp(path);
140140
}
141141

142+
/* git_mkstemps() - create tmp file with suffix honoring TMPDIR variable. */
143+
int git_mkstemps(char *path, size_t len, const char *template, int suffix_len)
144+
{
145+
const char *tmp;
146+
size_t n;
147+
148+
tmp = getenv("TMPDIR");
149+
if (!tmp)
150+
tmp = "/tmp";
151+
n = snprintf(path, len, "%s/%s", tmp, template);
152+
if (len <= n) {
153+
errno = ENAMETOOLONG;
154+
return -1;
155+
}
156+
return mkstemps(path, suffix_len);
157+
}
142158

143159
int validate_headref(const char *path)
144160
{

t/t4020-diff-external.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ test_expect_success 'GIT_EXTERNAL_DIFF with more than one changed files' '
136136
GIT_EXTERNAL_DIFF=echo git diff
137137
'
138138

139+
test_expect_success 'GIT_EXTERNAL_DIFF generates pretty paths' '
140+
touch file.ext &&
141+
git add file.ext &&
142+
echo with extension > file.ext &&
143+
GIT_EXTERNAL_DIFF=echo git diff file.ext | grep ......_file\.ext &&
144+
git update-index --force-remove file.ext &&
145+
rm file.ext
146+
'
147+
139148
echo "#!$SHELL_PATH" >fake-diff.sh
140149
cat >> fake-diff.sh <<\EOF
141150
cat $2 >> crlfed.txt

0 commit comments

Comments
 (0)