Skip to contentSkip to conversation

Allow git mv FILENAME Filename when core.ignorecase = true

PATCH In review Torsten Bögershausen 2 participants 2 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Allow git mv FILENAME Filename when core.ignorecase = true

Motivation:
The typical use case is when a file named "FILENAME" should be renamed into "Filename" and we are on a case ignoring file system (core.ignorecase = true).

Using "mv FILENAME Filename" outside git succeeds, (on Windows and MAC OS X, under Linux the mv command rejects "mv: Filename' and FILENAME' are the same file").

"git mv FILENAME Filename" is refused, "fatal: destination exists", unless "git mv --forced FILENAME Filename" is used.
The underlying file system makes git think that the destination "Filename" exists.

The following discussion assumes, that we are on a "case ignoring" file system, and core.ignorecase = true.

This change allows "git mv FILENAME Filename".
Using non ASCII works as well, like "git mv MÄRCHEN Märchen". The ambition is that "git mv FILENAME Filename" changes both the git index and the filename in the working tree,
in the same way how "git mv Filename NewFile" works.
Note: Under Linux+vfat The rename() function does not the rename in the working directory.

Implementation details:
A possible approach to allow the "git mv FILENAME Filename" is to compare both file names using strcasecmp().

This works for filenames where all characters are ASCII, It will fail for "git mv MÄRCHEN Märchen".

Git has now idea about the encoding of filenames
(like UTF-8, ISO-8859-1 or any other).
Neither has strcasecmp() an idea how to handle non ASCII characters.

With this patch git lets the underlying file system decide if 2 file names refer to the same file.

Remember that the file system does this already, by returning the same values for lstat("FILENAME") and lstat("Filename").

By comparing all members in "struct stat" we can be sure that both filenames point out the same file.
This is done in the function "equivalent_filenames()".

As lstat() on Windows (mingw.c or cygwin.c) sets st_ino to 0, (and st_dev and other fields in struct stat)
we need other checks when running under Windows.

Therefore a different implementation of equivalent_filenames() is used under Windows.
It uses GetFileInformationByHandle() to get and compare dwVolumeSerialNumber, nFileIndexLow and nFileIndexHigh.
It uses even lstat(), since Windows reports the same nFileIndexLow/High for a file and a softlink (under cygwin) pointing to it.

To summarize:
equivalent_filenames() is OS specific and checks under Windows:
dwVolumeSerialNumber, nFileIndexLow/High, st_mode, st_size, st_atime and st_mtime.
All other OS check
st_mode, st_dev, st_ino, st_uid, st_gid, st_size, st_atime, st_mtime.

As a bonus (or regression), a file name can be renamed to a file name which is already hard-linked to the same inode.

---
 builtin/mv.c      |   10 +++++++-
 compat/cygwin.c   |   44 ++++++++++++++++++++++++++++++++++++++++
 compat/cygwin.h   |    3 ++
 compat/mingw.c    |   33 ++++++++++++++++++++++++++++++
 compat/mingw.h    |    3 ++
 git-compat-util.h |   17 +++++++++++++++
 t/t7001-mv.sh     |   57 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 t/test-lib.sh     |    4 +++
 8 files changed, 169 insertions(+), 2 deletions(-)
builtin/mv.c +8 -2
index 40f33ca..7be7d8a 100644
@@ -165,14 +165,20 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 		} else if (cache_name_pos(src, length) < 0)
 			bad = _("not under version control");
 		else if (lstat(dst, &st) == 0) {
+			int allow_force = force;
 			bad = _("destination exists");
-			if (force) {
+			if (!force && ignore_case && equivalent_filenames(src, dst)) {
+				allow_force = 1;
+				bad = NULL;
+			}
+			if (allow_force) {
 				/*
 				 * only files can overwrite each other:
 				 * check both source and destination
 				 */
 				if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
-					warning(_("%s; will overwrite!"), bad);
+					if (bad)
+						warning(_("%s; will overwrite!"), bad);
 					bad = NULL;
 				} else
 					bad = _("Cannot overwrite");
compat/cygwin.c +44 -0

56 lines not on this page · raw message

compat/cygwin.h +3 -0
index a3229f5..04cc17e 100644
@@ -7,3 +7,6 @@ extern stat_fn_t cygwin_lstat_fn;
 
 #define stat(path, buf) (*cygwin_stat_fn)(path, buf)
 #define lstat(path, buf) (*cygwin_lstat_fn)(path, buf)
+
+int cygwin_equivalent_filenames(const char *a, const char *b);
+#define equivalent_filenames cygwin_equivalent_filenames
compat/mingw.c +33 -0
index 878b1de..56be81a 100644
@@ -474,6 +474,39 @@ int mingw_fstat(int fd, struct stat *buf)
 	return -1;
 }
 
+int mingw_equivalent_filenames(const char *a, const char *b)
+{
+	BY_HANDLE_FILE_INFORMATION hia, hib;
+	HANDLE h;
+	struct stat st_a, st_b;
+
+	if (lstat(a, &st_a) || lstat(b, &st_b))
+		return 0;
+
+	h = CreateFile(a, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
+	if (INVALID_HANDLE_VALUE == h)
+		return 0;
+
+	if (!(GetFileInformationByHandle(h,&hia)))
+		return 0;
+	CloseHandle(h);
+
+	h = CreateFile(b, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
+	if (INVALID_HANDLE_VALUE == h)
+		return 0;
+	if (!(GetFileInformationByHandle(h,&hib)))
+		return 0;
+	CloseHandle(h);
+
+	return st_a.st_mode == st_b.st_mode &&
+	       st_a.st_size == st_b.st_size &&
+	       st_a.st_atime == st_b.st_atime &&
+	       st_a.st_mtime == st_b.st_mtime &&
+	       hia.dwVolumeSerialNumber == hib.dwVolumeSerialNumber &&
+	       hia.nFileIndexLow == hib.nFileIndexLow &&
+	       hia.nFileIndexHigh == hib.nFileIndexHigh;
+}
+
 static inline void time_t_to_filetime(time_t t, FILETIME *ft)
 {
 	long long winTime = t * 10000000LL + 116444736000000000LL;
compat/mingw.h +3 -0
index 62eccd3..3445104 100644
@@ -303,6 +303,9 @@ int winansi_fprintf(FILE *stream, const char *format, ...) __attribute__((format
 void mingw_open_html(const char *path);
 #define open_html mingw_open_html
 
+int mingw_equivalent_filenames(const char *a, const char *b);
+#define equivalent_filenames mingw_equivalent_filenames
+
 /*
  * helpers
  */
git-compat-util.h +17 -0
index 40498b3..d66cffe 100644
@@ -567,4 +567,21 @@ int rmdir_or_warn(const char *path);
  */
 int remove_or_warn(unsigned int mode, const char *path);
 
+#ifndef equivalent_filenames
+static inline int equivalent_filenames(const char *a, const char *b) {
+	struct stat st_a, st_b;
+	if (lstat(a, &st_a) || lstat(b, &st_b))
+		return 0;
+
+	return st_a.st_mode == st_b.st_mode &&
+	       st_a.st_dev == st_b.st_dev &&
+	       st_a.st_ino == st_b.st_ino &&
+	       st_a.st_uid == st_b.st_uid &&
+	       st_a.st_gid == st_b.st_gid &&
+	       st_a.st_size == st_b.st_size &&
+	       st_a.st_atime == st_b.st_atime &&
+	       st_a.st_mtime == st_b.st_mtime;
+}
+#endif
+
 #endif
t/t7001-mv.sh +57 -0

61 lines not on this page · raw message

t/test-lib.sh +4 -0
index abc47f3..8c71583 100644
@@ -1080,6 +1080,10 @@ fi
 # test whether the filesystem supports symbolic links
 ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
 rm -f y
+# test whether the filesystem supports hard links
+>x
+ln x y 2>/dev/null && test -f y 2>/dev/null && test_set_prereq HARDLINKS
+rm -f x y
 
 # When the tests are run as root, permission tests will report that
 # things are writable when they shouldn't be.
Signature · 1 line
1.7.4.3
#1 lore raw
Quoted 2 lines · Torsten Bögershausen

The following discussion assumes, that we are on a "case ignoring" file system, and core.ignorecase = true.

Both commas should be removed.

Josh