Homebrewのパッケージをinstallした後にそのversionに問題があり、過去versionを使用したい場合がありますがありますよね?
ってことでHomebrewってバージョン管理できないのって思って調べてみました。
まずは、Homebrewについて←古いですが、結構網羅的に書いてあります。
では、過去versionをinstallする方法ですね
今回は、yqのパッケージを例として説明していきます。

目次

下準備

まずは、yqをinstallします。

$ brew info yq
..
...

$ yq -V
yq (https://github.com/mikefarah/yq/) version 4.25.1

ここでyqの4.25.1に問題があって、4.24系を使用したいとします。
$ brew install yq@4.24としてももちろんダメです。

$  brew install yq@4.24
Warning: No available formula with the name "yq@4.24".
==> Searching for similarly named formulae...
Error: No similarly named formulae found.
==> Searching for a previously deleted formula (in the last month)...
Warning: homebrew/core is shallow clone. To get its complete history, run:
  git -C "$(brew --repo homebrew/core)" fetch --unshallow

Error: No previously deleted formula found.
==> Searching taps on GitHub...
Error: No formulae found in taps.

過去versionをinstallする

まずは、installされたyqの情報を確認してみます。

$ brew info yq
yq: stable 4.25.1 (bottled), HEAD
Process YAML documents from the CLI
https://github.com/mikefarah/yq
Conflicts with:
  python-yq (because both install `yq` executables)
/usr/local/Cellar/yq/4.25.1 (9 files, 7.6MB) *
  Poured from bottle on 2022-05-11 at 00:07:20
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/yq.rb
License: MIT
==> Dependencies
Build: go ✘, pandoc ✘
==> Options
--HEAD
	Install HEAD version
==> Caveats
zsh completions have been installed to:
  /usr/local/share/zsh/site-functions
==> Analytics
install: 27,666 (30 days), 83,502 (90 days), 272,997 (365 days)
install-on-request: 27,426 (30 days), 82,758 (90 days), 270,278 (365 days)
build-error: 0 (30 days)

homebrewのcoreディレクトリに移動します。

$ cd /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/

homebrew-caskの場合は、以下のディレクトリになります。
$ cd /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask/Casks/

caskかどうかを調べるには、 brew listで確認できます

$ brew list
==> Formulae
ls /usr/local/Cellar
awscli gzip yq

==> Casks
ls /usr/local/Caskroom
multipass

git logで対象のバージョンのコミット一覧を出します。
(表示を簡素化したいので--onelineをつけます。)

$ git log --oneline yq.rb
5bb72725cdd yq: update 4.25.1 bottle.
30938091054 yq 4.25.1
b3cc83b8e78 yq: update 4.24.5 bottle.
e62faccb2b5 yq 4.24.5
c530cb21ee4 yq: update 4.24.4 bottle.
27f94f1d25c yq 4.24.4
ca3f87b0bdc yq: update 4.24.2 bottle.
e5d8631a595 yq 4.24.2
6f9c4542d48 yq: update 4.23.1 bottle.
99fe5894efc yq 4.23.1
a479a5f0324 yq: update 4.22.1 bottle.
e5d8cb2cd58 yq 4.22.1
87a495ad644 (grafted) dosbox-x: update 0.83.23 bottle.

version4.24.5がありました。 では、reversionを使って指定versionまで戻りましょう

$ git checkout b3cc83b8e78 yq.rb
Updated 1 path from b3cc83b8e78

$ brew info yq
yq: stable 4.24.5 (bottled), HEAD
Process YAML documents from the CLI
...

stableが4.24.5になりましたね

実際にinstallします。
installする前にパッケージを無効化しないと怒られるのでunlinkしてからinstallします。

$ brew unlink yq
Unlinking /usr/local/Cellar/yq/4.25.1... 5 symlinks removed.

$ brew install yq
...

$ yq -V
yq (https://github.com/mikefarah/yq/) version 4.24.5

無事4.24系をinstallできました。

gitのバージョンをもとに戻しておきましょう。installしたコマンドは元に戻らないので安心してください。

$ git reset --hard
HEAD is now at ffb125f99e0 deno: update 1.21.2 bottle.

install済みのversionに切り替える

brew switchに変わる良い方法を出たら記載しようと思います。

終わりに

これで、brewでinstallしたversionで困らないですね。
もっとスマートにversionを切り替えられたら嬉しいですね今後に期待です。
もしかして知ってる方います?