I think this block here is not covering all cases. E.g. if the file doesn't exist, the update flag is set and the file is in the index: that's happening when you just delete a tracked file in the work-dir and call "add -u". 
You expect the file to be deleted. But we just add it again.

Check your if's against all possible combinations of "update flag set", "file exists in workdir", "file exists in index" -- these are just 8 cases.