Homebrew/homebrew — pull request #28550 by finn (2014-04-20 17:13:18+00:00)
Title: Fix version in Formula's to_hash method.

When running

`brew info --json=v1 jpeg`

I noticed that in the JSON output the "version" field under "installed" was empty when it shouldn't be.

* The formula values displayed with the `--json` flag are generated using the `to_hash` method in the Formula class.
* The Formula class's `to_hash` calls `key.version.to_s` which determines the version using the `self.parse` and `self._parse` methods in `version.rb` (here: https://github.com/Homebrew/homebrew/blob/master/Library/Homebrew/version.rb#L245-L328).

It appears that these parse methods are intended for parsing the version from the url of the source tarball for a formula. But in `to_hash`, the value that is parsed is the path of the formula's cellar. The result is that the version is parsed and displayed correctly in some cases where the regular expressions match, but not in others. (There were a number of other formulae that displayed the wrong value when using the `brew info --json=v1` command.)

I looked at how the `brew list --versions` command parses and displays version numbers, and switched the Formula class's `to_hash` to use that method. (Here is the code for the `list --versions` command: https://github.com/Homebrew/homebrew/blob/master/Library/Homebrew/cmd/list.rb#L59-L68) It simply pulls `basename` from the cellar path. This returns the final component of the path which is the correct installed version.

I ran tests both before and after my change and they all passed. I am not familiar enough with the code base to write my own test for this case, so I am simply submitting the bug fix patch.