Home > Software engineering >  git difftool fails silently when file is not found
git difftool fails silently when file is not found

Time:02-10

Take any git repo and run the following

git difftool -- asdfasdfasdsfawerf

i.e. give it an invalid filename. It will return immediately showing no diff, but it also has 0 return code. Obviously it should give some sort of error because it's not that there's no diffs in my file, the file itself doesn't exist either in my working tree or in HEAD so I'm asking an invalid question.

In case you think this is a contrived example, this can be a real issue if the name of a file is mistyped. You end up thinking there's no diff in the file when actually it couldn't find the file.

Does anyone know of a way to force it to fail if the file is not found?

CodePudding user response:

It seems this only happens when -- is used. Removing it solves the problem, although it still seems like a bug.

CodePudding user response:

git difftool ends up executing git diff "$@" Looking a the relevant source code at line 2713:

2713                         if (strcmp(arg, "--"))
2714                                 continue;
2715                         argv[i] = NULL;
2716                         argc = i;

we can see that the argument list are cut short when -- is encountered.

Later on in the code, at line 2782 the file name would have been checked and an error would be have been raised, if it wasn't for the !strcmp("--", arg) above:

2781                         for (j = i; j < argc; j  )
2782                                 verify_filename(revs->prefix, argv[j], j == i);

It seems like a bug in the revision setup, but I don't contribute to git so can't say much about the source code, I just spend a few minutes digging into it.

When calling git diff -- abc argv will contain { "diff" } after it have been cut short, and when called with git diff abc it will contain { "diff", "abc" }

  •  Tags:  
  • git
  • Related