Skip to content

Commit b11cf09

Browse files
committed
Merge branch 'da/pretty-tempname'
* da/pretty-tempname: diff: generate pretty filenames in prep_temp_blob() compat: add a basename() compatibility function compat: add a mkstemps() compatibility function Conflicts: Makefile
2 parents ceff8e7 + 003b33a commit b11cf09

File tree

10 files changed

+177
-1
lines changed

10 files changed

+177
-1
lines changed

Makefile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ all::
5252
#
5353
# Define NO_MKDTEMP if you don't have mkdtemp in the C library.
5454
#
55+
# Define NO_MKSTEMPS if you don't have mkstemps in the C library.
56+
#
57+
# Define NO_LIBGEN_H if you don't have libgen.h.
58+
#
5559
# Define NO_SYS_SELECT_H if you don't have sys/select.h.
5660
#
5761
# Define NO_SYMLINK_HEAD if you never want .git/HEAD to be a symbolic link.
@@ -636,10 +640,12 @@ EXTLIBS =
636640

637641
ifeq ($(uname_S),Linux)
638642
NO_STRLCPY = YesPlease
643+
NO_MKSTEMPS = YesPlease
639644
THREADED_DELTA_SEARCH = YesPlease
640645
endif
641646
ifeq ($(uname_S),GNU/kFreeBSD)
642647
NO_STRLCPY = YesPlease
648+
NO_MKSTEMPS = YesPlease
643649
THREADED_DELTA_SEARCH = YesPlease
644650
endif
645651
ifeq ($(uname_S),UnixWare)
@@ -651,6 +657,7 @@ ifeq ($(uname_S),UnixWare)
651657
SHELL_PATH = /usr/local/bin/bash
652658
NO_IPV6 = YesPlease
653659
NO_HSTRERROR = YesPlease
660+
NO_MKSTEMPS = YesPlease
654661
BASIC_CFLAGS += -Kthread
655662
BASIC_CFLAGS += -I/usr/local/include
656663
BASIC_LDFLAGS += -L/usr/local/lib
@@ -674,6 +681,7 @@ ifeq ($(uname_S),SCO_SV)
674681
SHELL_PATH = /usr/bin/bash
675682
NO_IPV6 = YesPlease
676683
NO_HSTRERROR = YesPlease
684+
NO_MKSTEMPS = YesPlease
677685
BASIC_CFLAGS += -I/usr/local/include
678686
BASIC_LDFLAGS += -L/usr/local/lib
679687
NO_STRCASESTR = YesPlease
@@ -702,6 +710,7 @@ ifeq ($(uname_S),SunOS)
702710
NO_MEMMEM = YesPlease
703711
NO_HSTRERROR = YesPlease
704712
NO_MKDTEMP = YesPlease
713+
NO_MKSTEMPS = YesPlease
705714
ifneq ($(uname_R),5.11)
706715
OLD_ICONV = UnfortunatelyYes
707716
endif
@@ -726,6 +735,7 @@ ifeq ($(uname_O),Cygwin)
726735
NO_D_INO_IN_DIRENT = YesPlease
727736
NO_STRCASESTR = YesPlease
728737
NO_MEMMEM = YesPlease
738+
NO_MKSTEMPS = YesPlease
729739
NO_SYMLINK_HEAD = YesPlease
730740
NEEDS_LIBICONV = YesPlease
731741
NO_FAST_WORKING_DIRECTORY = UnfortunatelyYes
@@ -769,11 +779,13 @@ ifeq ($(uname_S),NetBSD)
769779
BASIC_LDFLAGS += -L/usr/pkg/lib $(CC_LD_DYNPATH)/usr/pkg/lib
770780
THREADED_DELTA_SEARCH = YesPlease
771781
USE_ST_TIMESPEC = YesPlease
782+
NO_MKSTEMPS = YesPlease
772783
endif
773784
ifeq ($(uname_S),AIX)
774785
NO_STRCASESTR=YesPlease
775786
NO_MEMMEM = YesPlease
776787
NO_MKDTEMP = YesPlease
788+
NO_MKSTEMPS = YesPlease
777789
NO_STRLCPY = YesPlease
778790
NO_NSEC = YesPlease
779791
FREAD_READS_DIRECTORIES = UnfortunatelyYes
@@ -789,12 +801,14 @@ endif
789801
ifeq ($(uname_S),GNU)
790802
# GNU/Hurd
791803
NO_STRLCPY=YesPlease
804+
NO_MKSTEMPS = YesPlease
792805
endif
793806
ifeq ($(uname_S),IRIX64)
794807
NO_IPV6=YesPlease
795808
NO_SETENV=YesPlease
796809
NO_STRCASESTR=YesPlease
797810
NO_MEMMEM = YesPlease
811+
NO_MKSTEMPS = YesPlease
798812
NO_STRLCPY = YesPlease
799813
NO_SOCKADDR_STORAGE=YesPlease
800814
SHELL_PATH=/usr/gnu/bin/bash
@@ -807,6 +821,7 @@ ifeq ($(uname_S),HP-UX)
807821
NO_SETENV=YesPlease
808822
NO_STRCASESTR=YesPlease
809823
NO_MEMMEM = YesPlease
824+
NO_MKSTEMPS = YesPlease
810825
NO_STRLCPY = YesPlease
811826
NO_MKDTEMP = YesPlease
812827
NO_UNSETENV = YesPlease
@@ -823,6 +838,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))
823838
NO_PREAD = YesPlease
824839
NO_OPENSSL = YesPlease
825840
NO_CURL = YesPlease
841+
NO_LIBGEN_H = YesPlease
826842
NO_SYMLINK_HEAD = YesPlease
827843
NO_IPV6 = YesPlease
828844
NO_SETENV = YesPlease
@@ -836,6 +852,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))
836852
NO_C99_FORMAT = YesPlease
837853
NO_STRTOUMAX = YesPlease
838854
NO_MKDTEMP = YesPlease
855+
NO_MKSTEMPS = YesPlease
839856
SNPRINTF_RETURNS_BOGUS = YesPlease
840857
NO_SVN_TESTS = YesPlease
841858
NO_PERL_MAKEMAKER = YesPlease
@@ -855,6 +872,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))
855872
endif
856873
ifneq (,$(findstring arm,$(uname_M)))
857874
ARM_SHA1 = YesPlease
875+
NO_MKSTEMPS = YesPlease
858876
endif
859877

860878
-include config.mak.autogen
@@ -886,6 +904,11 @@ ifndef CC_LD_DYNPATH
886904
endif
887905
endif
888906

907+
ifdef NO_LIBGEN_H
908+
COMPAT_CFLAGS += -DNO_LIBGEN_H
909+
COMPAT_OBJS += compat/basename.o
910+
endif
911+
889912
ifdef NO_CURL
890913
BASIC_CFLAGS += -DNO_CURL
891914
else
@@ -1013,6 +1036,10 @@ ifdef NO_MKDTEMP
10131036
COMPAT_CFLAGS += -DNO_MKDTEMP
10141037
COMPAT_OBJS += compat/mkdtemp.o
10151038
endif
1039+
ifdef NO_MKSTEMPS
1040+
COMPAT_CFLAGS += -DNO_MKSTEMPS
1041+
COMPAT_OBJS += compat/mkstemps.o
1042+
endif
10161043
ifdef NO_UNSETENV
10171044
COMPAT_CFLAGS += -DNO_UNSETENV
10181045
COMPAT_OBJS += compat/unsetenv.o

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
*

compat/basename.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "../git-compat-util.h"
2+
3+
/* Adapted from libiberty's basename.c. */
4+
char *gitbasename (char *path)
5+
{
6+
const char *base;
7+
/* Skip over the disk name in MSDOS pathnames. */
8+
if (has_dos_drive_prefix(path))
9+
path += 2;
10+
for (base = path; *path; path++) {
11+
if (is_dir_sep(*path))
12+
base = path + 1;
13+
}
14+
return (char *)base;
15+
}

compat/mkstemps.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "../git-compat-util.h"
2+
3+
/* Adapted from libiberty's mkstemp.c. */
4+
5+
#undef TMP_MAX
6+
#define TMP_MAX 16384
7+
8+
int gitmkstemps(char *pattern, int suffix_len)
9+
{
10+
static const char letters[] =
11+
"abcdefghijklmnopqrstuvwxyz"
12+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
13+
"0123456789";
14+
static const int num_letters = 62;
15+
uint64_t value;
16+
struct timeval tv;
17+
char *template;
18+
size_t len;
19+
int fd, count;
20+
21+
len = strlen(pattern);
22+
23+
if (len < 6 + suffix_len) {
24+
errno = EINVAL;
25+
return -1;
26+
}
27+
28+
if (strncmp(&pattern[len - 6 - suffix_len], "XXXXXX", 6)) {
29+
errno = EINVAL;
30+
return -1;
31+
}
32+
33+
/*
34+
* Replace pattern's XXXXXX characters with randomness.
35+
* Try TMP_MAX different filenames.
36+
*/
37+
gettimeofday(&tv, NULL);
38+
value = ((size_t)(tv.tv_usec << 16)) ^ tv.tv_sec ^ getpid();
39+
template = &pattern[len - 6 - suffix_len];
40+
for (count = 0; count < TMP_MAX; ++count) {
41+
uint64_t v = value;
42+
/* Fill in the random bits. */
43+
template[0] = letters[v % num_letters]; v /= num_letters;
44+
template[1] = letters[v % num_letters]; v /= num_letters;
45+
template[2] = letters[v % num_letters]; v /= num_letters;
46+
template[3] = letters[v % num_letters]; v /= num_letters;
47+
template[4] = letters[v % num_letters]; v /= num_letters;
48+
template[5] = letters[v % num_letters]; v /= num_letters;
49+
50+
fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, 0600);
51+
if (fd > 0)
52+
return fd;
53+
/*
54+
* Fatal error (EPERM, ENOSPC etc).
55+
* It doesn't make sense to loop.
56+
*/
57+
if (errno != EEXIST)
58+
break;
59+
/*
60+
* This is a random value. It is only necessary that
61+
* the next TMP_MAX values generated by adding 7777 to
62+
* VALUE are different with (module 2^32).
63+
*/
64+
value += 7777;
65+
}
66+
/* We return the null string if we can't find a unique file name. */
67+
pattern[0] = '\0';
68+
errno = EINVAL;
69+
return -1;
70+
}

config.mak.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ NEEDS_SSL_WITH_CRYPTO=@NEEDS_SSL_WITH_CRYPTO@
3030
NO_OPENSSL=@NO_OPENSSL@
3131
NO_CURL=@NO_CURL@
3232
NO_EXPAT=@NO_EXPAT@
33+
NO_LIBGEN_H=@NO_LIBGEN_H@
3334
NEEDS_LIBICONV=@NEEDS_LIBICONV@
3435
NEEDS_SOCKET=@NEEDS_SOCKET@
3536
NO_SYS_SELECT_H=@NO_SYS_SELECT_H@
@@ -46,6 +47,7 @@ NO_STRTOUMAX=@NO_STRTOUMAX@
4647
NO_SETENV=@NO_SETENV@
4748
NO_UNSETENV=@NO_UNSETENV@
4849
NO_MKDTEMP=@NO_MKDTEMP@
50+
NO_MKSTEMPS=@NO_MKSTEMPS@
4951
NO_ICONV=@NO_ICONV@
5052
OLD_ICONV=@OLD_ICONV@
5153
NO_DEFLATE_BOUND=@NO_DEFLATE_BOUND@

configure.ac

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,12 @@ AC_SUBST(SNPRINTF_RETURNS_BOGUS)
627627
## (in default C library and libraries checked by AC_CHECK_LIB)
628628
AC_MSG_NOTICE([CHECKS for library functions])
629629
#
630+
# Define NO_LIBGEN_H if you don't have libgen.h.
631+
AC_CHECK_HEADER([libgen.h],
632+
[NO_LIBGEN_H=],
633+
[NO_LIBGEN_H=YesPlease])
634+
AC_SUBST(NO_LIBGEN_H)
635+
#
630636
# Define NO_STRCASESTR if you don't have strcasestr.
631637
GIT_CHECK_FUNC(strcasestr,
632638
[NO_STRCASESTR=],
@@ -677,6 +683,13 @@ GIT_CHECK_FUNC(mkdtemp,
677683
[NO_MKDTEMP=YesPlease])
678684
AC_SUBST(NO_MKDTEMP)
679685
#
686+
# Define NO_MKSTEMPS if you don't have mkstemps in the C library.
687+
GIT_CHECK_FUNC(mkstemps,
688+
[NO_MKSTEMPS=],
689+
[NO_MKSTEMPS=YesPlease])
690+
AC_SUBST(NO_MKSTEMPS)
691+
#
692+
#
680693
# Define NO_MMAP if you want to avoid mmap.
681694
#
682695
# Define NO_ICONV if your libc does not properly support iconv.

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,

git-compat-util.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@
9999
#include "compat/mingw.h"
100100
#endif /* __MINGW32__ */
101101

102+
#ifndef NO_LIBGEN_H
103+
#include <libgen.h>
104+
#else
105+
#define basename gitbasename
106+
extern char *gitbasename(char *);
107+
#endif
108+
102109
#ifndef NO_ICONV
103110
#include <iconv.h>
104111
#endif
@@ -234,6 +241,11 @@ extern int gitsetenv(const char *, const char *, int);
234241
extern char *gitmkdtemp(char *);
235242
#endif
236243

244+
#ifdef NO_MKSTEMPS
245+
#define mkstemps gitmkstemps
246+
extern int gitmkstemps(char *, int);
247+
#endif
248+
237249
#ifdef NO_UNSETENV
238250
#define unsetenv gitunsetenv
239251
extern void gitunsetenv(const char *);

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)