[v2,1/1] add: respect `--ignore-errors` when `lstat()` reports errors
diff mbox series

Message ID d88ea544d93ffc9b222ac3a4974df4c499e76430.1572812819.git.gitgitgadget@gmail.com
State New
Headers show
Series
  • add: respect --ignore-errors when lstat() reports errors
Related show

Commit Message

Johannes Schindelin via GitGitGadget Nov. 3, 2019, 8:26 p.m. UTC
From: qusielle <31454380+qusielle@users.noreply.github.com>

"git add --ignore-errors" command fails immediately when lstat returns
an error, despite the ignore errors' flag is enabled.

There could be files that triggers an error on stat(), when other files
proceed correctly.
Issue can be reproduced when running git under Cygwin and some target files
have utf-8 long names (200+ utf chars). Windows can handle them, but all
operations on them failed under Cygwin.
Issue can not be reproduced with usual latin/numeric only names.
For example, create a file with 220 'й' letters by Windows Explorer,
then in Cygwin:

 # Here and below "ййй..." means the line of й copied 220 times.
$ echo -n 'ййй...' | wc -c  # check the real size
440

$ ls -la
ls: cannot access 'ййй...'$'\320': No such file or directory
-????????? ? ?              ?        ?            ? 'ййй...'$'\320'

$ ls й*
ls: cannot access 'ййй...'$'\320': No such file or directory

$ stat й*
stat: cannot stat 'ййй...'$'\320': No such file or directory

In my perspective, it's okay to skip these problematic files when ignore
error flag is specified, but official Git terminates entire git add command
when such files come up. But with proposed patch it is the desired behavior:

$ git add --ignore-errors .
error: ййй... can only add regular files, symbolic links or git-directories
 # All other files have been added correctly.

Signed-off-by: Qusielle <qusielle@gmail.com>
---
 read-cache.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Patch
diff mbox series

diff --git a/read-cache.c b/read-cache.c
index 133f790fa4..791ef65bac 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -801,8 +801,12 @@  int add_to_index(struct index_state *istate, const char *path, struct stat *st,
 int add_file_to_index(struct index_state *istate, const char *path, int flags)
 {
 	struct stat st;
-	if (lstat(path, &st))
-		die_errno(_("unable to stat '%s'"), path);
+	if (lstat(path, &st)) {
+		if (flags & ADD_CACHE_IGNORE_ERRORS)
+			return -1;
+		else
+			die_errno(_("unable to stat '%s'"), path);
+	}
 	return add_to_index(istate, path, &st, flags);
 }