2010-02-04

Merging a unison conflict with vim

I use unison to synchronize various files. If I've edited the same file on both ends I can pick a version to keep or, until now, I aborted, manually merged them with vimdiff or some other tool and then started synchronizing again.

But unison is able to call an external tool to merge for you. I figured out how to get it to use vimdiff -- add the option -merge "Name * -> urxvt -e vimdiff CURRENT1 CURRENT2".

I've used urxvt rather than my usual urxvtcd because urxvtcd immediately forks and unison continues as soon as the process it runs exits. So why did I run a terminal emulator at all rather than just running vimdiff in the existing terminal? It won't display. Not sure why -- probably to do with it not realizing it has a virtual terminal to output on or something.

Update on 2010-10-10: I had another shot at figuring it out. I tried using screen -- "Must be connected to a terminal". Then I tried using ssh -t and screen -- same issue (but I think the error was from SSH this time rather than screen). Finally I found an option in screen's manual page which can start a screen session detached but yet not fork. That'll do. So now in default.prf (which my other unison config files include) I have these two lines:

merge = Name * -> screen -DmS unisonmerge vimdiff CURRENT1 CURRENT2
merge = Name .* -> screen -DmS unisonmerge vimdiff CURRENT1 CURRENT2

The second line is there because I found just now that dotfiles weren't included when only the first line was present.

So here's what happens when a merge is now requested. A new screen session is started with the session name "unisonmerge". That's started detached but the process doesn't fork as it usually would and so unison stops, waiting. We get the shell to stop the process and give us a prompt by pressing ^Z, then attach the new screen session with screen -RS unisonmerge. The vimdiff instance is running in that -- we merge the files (modifying only the one we want to keep, which will end up on both machines) and then exit. The screen session ends and we're back to the prompt. Then we bring unison back to the foreground with fg. Unison continues.

So now I can run unison on a headless server over SSH, merging when necessary.

No comments:

Post a Comment