jedbrown / git-fat

Simple way to handle fat files without committing them to git, supports synchronization using rsync
BSD 2-Clause "Simplified" License
622 stars 136 forks source link

Problem with 0 length files #18

Open trenouf opened 11 years ago

trenouf commented 11 years ago

Hi

I have a problem with any file that has an extension that I have marked as being filtered by git-fat, but is zero length. It seems to be clean filtered when it is added to the repository, and smudge filtered when it is checked out, so all correct so far. But then git thinks that the file is dirty, so git status shows it as an unstaged change, and git diff shows a difference where the "old" state is the "#$# git-fat" token and the "new" state is the 0 length file.

Maybe git has some optimization where it decides not to run the smudge filter in some cases when the file is 0 length?

I have a workaround, which is to modify git-fat to do nothing in the clean filter when the file is zero length. Does this seem like a reasonable change?

diff --git a/git-fat b/git-fat
index e62f99b..0d8021a 100755
--- a/git-fat
+++ b/git-fat
@@ -244,7 +244,14 @@ class GitFat(object):
                     os.rename(tmpname, objfile)
                     self.verbose('git-fat filter-clean: caching to %s' % objfile)
                 cached = True
-                outstreamclean.write(self.encode(digest, bytes))
+                # Only write hash if the original file is not 0 length. If it
+                # is 0 length, just leave a 0 length file. This works around
+                # an apparent git problem where it thinks the working tree is
+                # dirty if the smudge filter generates a 0 length file.
+                if bytes != 0:
+                    outstreamclean.write(self.encode(digest, bytes))
+                else:
+                    self.verbose('git-fat filter-clean: not modifying 0 length file')
         finally:
             if not cached:
                 os.remove(tmpname)
jedbrown commented 11 years ago

Thanks for debugging this. Would you be willing to add a zero-length file to the test script so that this case gets tested and submit that, along with your patch above, as a pull request?