Today I had an SVN log that was in descending order by date (most recent at the top), and I wanted it in ascending order (most recent at the bottom). Now there may be a flag on the svn log command to do this, but I thought I'd see what I could do with Vim. One of the most useful tools for doing such things is macros. I read this article a while back about using macros over regexes for many situations, and it's proved to be a very useful technique, and one that looked to be of use here, too. Here's what the svn log looks like:
------------------------------------------------------------------------ r21783 | jbradsha | 2010-08-04 14:41:02 -0700 (Wed, 04 Aug 2010) | 2 lines Log notes ------------------------------------------------------------------------ r21765 | jbradsha | 2010-08-03 16:09:05 -0700 (Tue, 03 Aug 2010) | 2 lines Log notes ------------------------------------------------------------------------ r21519 | jbradsha | 2010-07-22 16:51:04 -0700 (Thu, 22 Jul 2010) | 4 lines Log notes
And so on.
The log notes can be of any length and number of lines, so obviously this wasn't going to be a straightforward task. But it ended up being pretty easy with macros. What I wanted to do was sort those blocks by revision number. To do that, I figured the easiest way is to get each block on one line in a way that I could revert, sort the lines, and then put them back. So I put my cursor on the first character of the first line and did this:
qj v /\n\n----Which quite nicely did the trick. Here's what each bit did::'<,'> s/[\r\n]/#!#/g j q 100@j :% sort :% s/#!#/\r/g
- Started recording a macro labelled "j" (for "join")
- Started visual selection mode
- Searched for two newlines followed by ----: this selects up to the end of the line before the end of the current block (by going two lines before the beginning of the next)
- Within that selection, replace all newlines with a unique symbol (#!#), making it all one line ready to be re-exploded afterwards
- Move down one line to the beginning of the next block
- Stop recording the macro
- Run the macro 100 times (I had less than 100 commits to sort)
- Sort the now-one-line commits, increasing in number (sort! would reverse the sort)
- Replace my delimiters with newlines, restoring the original format
No comments:
Post a Comment