Skip to content

Commit

Permalink
diff: generate pretty filenames in prep_temp_blob()
Browse files Browse the repository at this point in the history
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 <davvid@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
David Aguilar authored and Junio C Hamano committed Jun 1, 2009
1 parent e1c0688 commit 003b33a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,8 @@ extern int is_empty_blob_sha1(const unsigned char *sha1);

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

int git_mkstemps(char *path, size_t n, const char *template, int suffix_len);

/*
* NOTE NOTE NOTE!!
*
Expand Down
12 changes: 11 additions & 1 deletion diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -1964,8 +1964,16 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
{
int fd;
struct strbuf buf = STRBUF_INIT;
struct strbuf template = STRBUF_INIT;
char *path_dup = xstrdup(path);
const char *base = basename(path_dup);

fd = git_mkstemp(temp->tmp_path, PATH_MAX, ".diff_XXXXXX");
/* Generate "XXXXXX_basename.ext" */
strbuf_addstr(&template, "XXXXXX_");
strbuf_addstr(&template, base);

fd = git_mkstemps(temp->tmp_path, PATH_MAX, template.buf,
strlen(base) + 1);
if (fd < 0)
die("unable to create temp-file: %s", strerror(errno));
if (convert_to_working_tree(path,
Expand All @@ -1981,6 +1989,8 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
temp->hex[40] = 0;
sprintf(temp->mode, "%06o", mode);
strbuf_release(&buf);
strbuf_release(&template);
free(path_dup);
}

static struct diff_tempfile *prepare_temp_file(const char *name,
Expand Down
16 changes: 16 additions & 0 deletions path.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,22 @@ int git_mkstemp(char *path, size_t len, const char *template)
return mkstemp(path);
}

/* git_mkstemps() - create tmp file with suffix honoring TMPDIR variable. */
int git_mkstemps(char *path, size_t len, const char *template, int suffix_len)
{
const char *tmp;
size_t n;

tmp = getenv("TMPDIR");
if (!tmp)
tmp = "/tmp";
n = snprintf(path, len, "%s/%s", tmp, template);
if (len <= n) {
errno = ENAMETOOLONG;
return -1;
}
return mkstemps(path, suffix_len);
}

int validate_headref(const char *path)
{
Expand Down
9 changes: 9 additions & 0 deletions t/t4020-diff-external.sh
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ test_expect_success 'GIT_EXTERNAL_DIFF with more than one changed files' '
GIT_EXTERNAL_DIFF=echo git diff
'

test_expect_success 'GIT_EXTERNAL_DIFF generates pretty paths' '
touch file.ext &&
git add file.ext &&
echo with extension > file.ext &&
GIT_EXTERNAL_DIFF=echo git diff file.ext | grep ......_file\.ext &&
git update-index --force-remove file.ext &&
rm file.ext
'

echo "#!$SHELL_PATH" >fake-diff.sh
cat >> fake-diff.sh <<\EOF
cat $2 >> crlfed.txt
Expand Down

0 comments on commit 003b33a

Please sign in to comment.