@matheusbsbs / matheus-bagliano / commits / 003b33a8ad
Fork 0

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 <davvid@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

David Aguilar committed May 31, 2009 at 01:35 UTC 003b33a8ad686ee4a0d0b36635bfd6aba940b24a
4 files changed +38 -1
cache.h
+2
@@ -614,6 +614,8 @@ extern int is_empty_blob_sha1(const unsigned char *sha1);
614
615 int git_mkstemp(char *path, size_t n, const char *template);
616
617 +int git_mkstemps(char *path, size_t n, const char *template, int suffix_len);
618 +
619 /*
620 * NOTE NOTE NOTE!!
621 *
diff.c
+11 -1
@@ -1964,8 +1964,16 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
1964 {
1965 int fd;
1966 struct strbuf buf = STRBUF_INIT;
1967 + struct strbuf template = STRBUF_INIT;
1968 + char *path_dup = xstrdup(path);
1969 + const char *base = basename(path_dup);
1970
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);
1977 if (fd < 0)
1978 die("unable to create temp-file: %s", strerror(errno));
1979 if (convert_to_working_tree(path,
@@ -1981,6 +1989,8 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
1989 temp->hex[40] = 0;
1990 sprintf(temp->mode, "%06o", mode);
1991 strbuf_release(&buf);
1992 + strbuf_release(&template);
1993 + free(path_dup);
1994 }
1995
1996 static struct diff_tempfile *prepare_temp_file(const char *name,
path.c
+16
@@ -139,6 +139,22 @@ int git_mkstemp(char *path, size_t len, const char *template)
139 return mkstemp(path);
140 }
141
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 +}
158
159 int validate_headref(const char *path)
160 {
t/t4020-diff-external.sh
+9
@@ -136,6 +136,15 @@ test_expect_success 'GIT_EXTERNAL_DIFF with more than one changed files' '
136 GIT_EXTERNAL_DIFF=echo git diff
137 '
138
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 +
148 echo "#!$SHELL_PATH" >fake-diff.sh
149 cat >> fake-diff.sh <<\EOF
150 cat $2 >> crlfed.txt