Friday, January 30, 2015

Xtext and SVN - recovering from missing files when Xtext regenerates the src-gen folder

During the last 6 months I've been involved in a prototyping project using Xtext to express a DSL. This has been a really great learning experience exploring the world of Xtext. But some things are not that great. The site I'm working at uses Subversion and Xtext doesn't play perfectly with it. The key problem is that when running the "Generate Xtext artifacts" from your Xtext grammar, this regenerates the src-gen folders, i.e. deletes them and then regenerates the content. So if you've changed your grammar by renaming or removing a rule, the classes corresponding to the old rules will not be generated, which makes SVN nuts. The reason is that SVN will treat the deleted and no longer regenerated files as "missing" and thus Subversive refuses to commit them as a "Deleted file".

To recover, we can do some "magic" on the command line:
  1. Open a (cygwin) terminal (on Linux/OSX just a terminal)
  2. cd to the checked out SVN repository folder (or Eclipse project)
  3. run "svn status | grep '^! ' to list the "missing" files
  4. Now, we will revert all those files and then delete them using proper svn commands
  5. svn status | grep '^! ' | awk '{print $2}' | sed 's-\\-/-g' > MISSING.txt
    • The 'sed' expression is only necessary on Windows, since the "backslashing" will break the following commands
  6. cat MISSING.txt | xargs svn revert
  7. cat MISSING.txt | xargs svn delete
  8. Try committing again - those "missing" files should now be marked as deleted
  9. Clean up -> rm MISSING.txt
  10. Close the terminal and carry on :-)
Happy Xtext:ing!

PS: Update - created a script to automate this: https://github.com/fredrikattebrant/shell-scripts/blob/master/svn_recover_from_missing_files.sh