Skip to contentSkip to conversation

diff: generate prettier filenames when using GIT_EXTERNAL_DIFF

PATCH In review David Aguilar 4 participants 7 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 diff: generate prettier filenames when using GIT_EXTERNAL_DIFF

Naturally, prep_temp_blob() did not care about filenames. As a result, scripts that use GIT_EXTERNAL_DIFF ended up with filenames such as ".diff_XXXXXX".

This specializes the GIT_EXTERNAL_DIFF code to generate prettier filenames.

Diffing "name.ext" now generates "name.XXXX.ext". Diffing files with no extension now generates "name_XXXX".

---

I renamed a few variables and unrolled the inner loop in git_mkstemps() since the last patch to make things easier to read/review.

We might as well go with this one if we haven't applied the previous versions of this patch.

 cache.h                  |    2 +
 diff.c                   |   55 +++++++++++++++++++++++++++++++-----
 path.c                   |   70 ++++++++++++++++++++++++++++++++++++++++++++++
 t/t4020-diff-external.sh |   18 ++++++++++++
 4 files changed, 137 insertions(+), 8 deletions(-)
cache.h +2 -0
index b8503ad..871c984 100644
@@ -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!!
  *
diff.c +47 -8

96 lines not on this page · raw message

path.c +70 -0

82 lines not on this page · raw message

t/t4020-diff-external.sh +18 -0
index 0720001..a9fdcb0 100755
@@ -136,6 +136,24 @@ 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 with no ext' '
+	touch filenoext &&
+	git add filenoext &&
+	echo no extension > filenoext &&
+	GIT_EXTERNAL_DIFF=echo git diff filenoext | grep filenoext_ &&
+	git update-index --force-remove filenoext &&
+	rm filenoext
+'
+
+test_expect_success 'GIT_EXTERNAL_DIFF generates pretty paths with ext' '
+	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
Signature · 1 line
1.6.1.3
#1 lore raw
Quoted 8 lines · David Aguilar
diff.c +7 -1
-	fd = git_mkstemp(temp->tmp_path, PATH_MAX, ".diff_XXXXXX");
+	if (pretty_filename) {
+		struct strbuf pretty_name = STRBUF_INIT;
+		char *pathdup = xstrdup(path);
+		char *base = basename(pathdup);
+		char *dot = strchr(base, '.');
+		int suffix_len = 0;
+

diff.c static void prep_temp_blob(const char *path, struct diff_tempfile *temp, · line 1985

Quoted 10 lines
diff.c +10 -0
+		if (dot) {
+			/* path has an extension, e.g. "foo.txt";
+			 * generate "foo.XXXX.txt".
+			 */
+			*dot = '\0';
+			strbuf_addstr(&pretty_name, base);
+			*dot = '.';
+			strbuf_addstr(&pretty_name, ".XXXXXX");
+			suffix_len = strlen(dot);
+			strbuf_addstr(&pretty_name, dot);

This *dot business annoys me. Would it be better to use strbuf_add() with some pointer math thrown in? Also, what happens with files such as "foo.bar.txt"? Do we want "foo.XXXXX.bar.txt"?

#3 lore raw
In reply to David Aguilar
Quoted 7 lines · David Aguilar

Naturally, prep_temp_blob() did not care about filenames. As a result, scripts that use GIT_EXTERNAL_DIFF ended up with filenames such as ".diff_XXXXXX".

This specializes the GIT_EXTERNAL_DIFF code to generate prettier filenames.

Quoted 8 lines

Diffing "name.ext" now generates "name.XXXX.ext". Diffing files with no extension now generates "name_XXXX".

---

I renamed a few variables and unrolled the inner loop in git_mkstemps() since the last patch to make things easier to read/review.

Thanks.

I actually think using mkstemps() where the function is natively available, and using compat/mkstemps.c implementation where it is not, is a saner approach.

It would make the implementation of git_mkstemps() easier to read, because the interface to mkstemps(), even though it may not be in POSIX (nor in glibc), is a well known quantity and people do not need to follow into its implementation when they want to follow the logic of higher level code.

#2 lore raw
In reply to Stephen Boyd
Quoted 15 lines · Stephen Boyd
Quoted 14 lines · David Aguilar
diff.c +13 -1
-	fd = git_mkstemp(temp->tmp_path, PATH_MAX, ".diff_XXXXXX");
+	if (pretty_filename) {
+		struct strbuf pretty_name = STRBUF_INIT;
+		char *pathdup = xstrdup(path);
+		char *base = basename(pathdup);
+		char *dot = strchr(base, '.');
+		int suffix_len = 0;
+
+		if (dot) {
+			/* path has an extension, e.g. "foo.txt";
+			 * generate "foo.XXXX.txt".
+			 */
+			*dot = '\0';
+			strbuf_addstr(&pretty_name, base);
Quoted 8 lines

diff.c static void prep_temp_blob(const char *path, struct diff_tempfile *temp, · line 1985

Quoted 4 lines
diff.c +4 -0
+			*dot = '.';
+			strbuf_addstr(&pretty_name, ".XXXXXX");
+			suffix_len = strlen(dot);
+			strbuf_addstr(&pretty_name, dot);

This *dot business annoys me. Would it be better to use strbuf_add() with some pointer math thrown in? Also, what happens with files such as "foo.bar.txt"? Do we want "foo.XXXXX.bar.txt"?

That was intentional; "foo.tar.gz" becomes "foo.XXXXXX.tar.gz", which is what I considered better behavior when writing it.

Thanks for the note about using strbuf_add(). I'll see if we can simplify things by using it instead of strbuf_addstr().

I should probably split the "add compat/mkstemps" and "use it in git_mkstemps()" parts into separate commits since we're going to want to use the native mkstemps implementation if it's available.

Signature · 1 line
		David
#4 lore raw
In reply to David Aguilar

David Aguilar, 26.05.2009:

Quoted 2 lines

Diffing "name.ext" now generates "name.XXXX.ext". Diffing files with no extension now generates "name_XXXX".

Would it simplify the patch when using "name_XXXX.ext" and "name_XXXX"? Or what about "XXXX_name.ext" and "XXXX_name"? Then you don't even have to search for the dot and it has the advantage to show the original filename without some random letters in the middle of it.

Markus

#5 lore raw
Quoted 11 lines · Markus Heidelberg

David Aguilar, 26.05.2009:

Quoted 2 lines

Diffing "name.ext" now generates "name.XXXX.ext". Diffing files with no extension now generates "name_XXXX".

Would it simplify the patch when using "name_XXXX.ext" and "name_XXXX"? Or what about "XXXX_name.ext" and "XXXX_name"? Then you don't even have to search for the dot and it has the advantage to show the original filename without some random letters in the middle of it.

Markus

In that case suffix_len is always len(basename) + 1 and there's less strbuf juggling to do. That is simpler. Let's do that.

My next patch series will be two parts:
part 1: add compat/mkstemps.c
part 2: use it in prep_temp_blob() via git_mkstemps()

Part 1 has me a little nervous because I'm not sure which platforms in the Makefile need the
NO_MKSTEMPS = YesPlease
setting. I only have access to mac + linux, so I can do the right thing there, but I'll have to make a best guess for the rest of the platforms.

Perhaps the best answer is to set them all to YesPlease except for mac and openbsd, which I know have native implementations.

Signature · 1 line
		David
#6 lore raw
Quoted 2 lines · David Aguilar

Perhaps the best answer is to set them all to YesPlease except for mac and openbsd, which I know have native implementations.

Some vintage of Solaris seems to have it:

    http://docs.sun.com/app/docs/doc/819-2243/mkstemps-3c?a=view

The notes section of

http://developer.apple.com/documentation/Darwin/Reference/ManPages/man3/mkstemps.3.html

says

The mkstemps() function first appeared in OpenBSD 2.4, and later in
FreeBSD 3.4.