FlaShG / GitMerge-for-Unity

Unity plugin for merging sceneand prefabs files when using git.
GNU General Public License v2.0
197 stars 27 forks source link

False diffs reported, even outside of git merge/pull #62

Closed horiaciu closed 3 years ago

horiaciu commented 3 years ago

When using GMfU, I always see a few diffs on my scene, even when not in the middle of a git merge/pull operation. I checked and confirmed that the --ours and --theirs files are identical (and identical to the scene file in the working directory). The diffs seem to be harmless, and no matter what I choose to use (ours or theirs), they don't actually change my scene file, but they also never go away.

I suspected this is a bug in the comparison algorithm, so I started diving into the code and, sure enough, I found that GameObjectMergeActions.DifferentValuesFlat returns true (as in not equals) on my properties.

What all these properties have in common is that they are of the type ObjectReference, so they have an objectReferenceValue that gets compared. They all correspond to fields in my scripts that are marked as [SerializeField] and refer to Components (for example Transforms).

Inside DifferentValuesFlat, I logged:

I'm stuck at this point, mostly because I don't yet have a full understanding of how SerializedProperties work. I'll keep digging, but I thought I'd summarize where I got so far, in case it's something obvious.

(Sidenote: GMfU is a life saver! Thank you very much for building this, happy to help iron out all the kinks!)

FlaShG commented 3 years ago

Thanks for your help!

It should be fixed now, but as a short explanation... the way this tool works is that it loads one version of the scene, remembers all the objects in it, then loads the other version and compares the remembered objects to the currently loaded ones. When an object references another object, the tool has to check whether our version references our version of the referenced object, while the other version references the other version of the referenced object. It seems that the issue was that objectA--ours and objectA--theirs reference different objects (which is correct), and GMfU was (incorrectly) concluding that there's a difference.

The implemented solution is that DifferentValuesFlat now compares the object IDs (which are the same for both versions of the referenced object) instead of the reference itself when object reference properties are passed.

horiaciu commented 3 years ago

Thank you for the prompt fix! I can confirm the bogus diffs went away in my case.

As a side question (not sure what's the best place to ask this): I love that Unity hot reloads editor scripts (like GMfU), just as it does with the script components. I'm using Visual Studio Code as my code editor, so tweaking GMfU is as easy as editing GMfU code in VSC then switching to Unity and reopening the GMfU window. So convenient! However, I couldn't figure out how to debug editor scripts (set breakpoints, inspect objects, step through). There is a lot of documentation out there for debugging component scripts (as you run your Unity project), but I couldn't find much about editor scripts. So while investigating this bug, I relied only on log statements. What is the way to attach a debugger to editor scripts?

FlaShG commented 3 years ago

The debugger works with any kind of script. If you set a breakpoint in an editor script, the editor will pause execution once you get there.