LoginSignup
0
2

More than 1 year has passed since last update.

ghqとpecoでラクラク・リポジトリ管理! for Windows

Last updated at Posted at 2023-03-28

はじめに

Gitで扱うリポジトリが増えてくると、プロジェクト間の移動が手間になってきませんか?
ghqとpecoを組み合わせることで、ターミナルを開いて、すぐに指定したリポジトリに移動できるようになります。
また、別のリポジトリに移動するのも楽になります。
ghqとpecoを利用したリポジトリ管理の方法は、多くの方がご紹介されてますが、Windowsユーザ向けの説明がないので、とりわけPowershellユーザに向けてご紹介します。

ghq_peco_sample.gif

登場するコマンド

ghq

ghqはGitHubなどのリポジトリを管理するツールです。
このコマンドによって、Gitのリポジトリのパスを管理します。

peco

pecoは標準入力で受け取った内容に対して、あいまい検索を行い、選択した行を返します。
このコマンドを利用することで、リポジトリの一覧に対して、検索したり、選択することができます。

インストール

ghq・pecoともに、go製のツールなので、まずはgoの環境を作りましょう。

golangのインストール

winget install -e --id GoLang.Go.1.19

ghqのインストール

go install github.com/x-motemen/ghq@latest

pecoのインストール

リポジトリのREADMを拝見すると、go install経由でのインストールは非推奨となっておりますので、chocolateyでのインストールを行いましょう。(もしくは、ビルドを行う。)

choco install peco

Chocolateyのインストール方法は、1コマンドで済みます。
管理者権限でPowerShellを起動し、下記コマンドを実行してください。

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

ghqとpecoの連携

頻繁に利用するコマンドとなると思うので、できるだけ短いコマンドでリポジトリ間の移動を済ませるようにしたいものです。 ghq + pecoを実行するコマンドをPowerShellで実行できるようにしましょう。

PowerShellでは起動時に読み込まれるファイル(プロファイル)に登録しておきましょう。 $PROFILEに、プロファイルのパスが格納されています。

echo $PROFILE
// 直接vscodeを開きたい方は下記
code $PROFILE

お好きなエディタで上記のファイルに下記を貼り付けて、PowerShellを再起動してください。

function psPeco ([switch]$fuzzy) {
  $origin = [System.Console]::OutputEncoding
  $utf8 = [System.Text.Encoding]::UTF8
  $OutputEncoding = $utf8
  [System.Console]::OutputEncoding = $utf8
  if ($fuzzy) {
      $out = $input | peco.exe --initial-filter=Fuzzy
  }
  else {
      $out = $input | peco.exe
  }
  [System.Console]::OutputEncoding = $origin
  return $out
}
function repo() {
$origin = [System.Console]::OutputEncoding
$utf8 = [System.Text.Encoding]::UTF8
$OutputEncoding = $utf8
[System.Console]::OutputEncoding = $utf8
$repository =  $(ghq list | peco)
$repositoryPath = (ghq root) + '/' + $repository
cd $repositoryPath
[System.Console]::OutputEncoding = $origin
}

これで、repoと入力するだけで、リポジトリの移動ができるようになります。

リポジトリの操作

あとは今後下記コマンドを通して、クローンを行い

ghq get http://github.com/<username>/<repository>

下記コマンドで、リポジトリの選択ができるようになります。

repo

参考文献

ghqとpecoでリポジトリの管理を便利にする
https://zenn.dev/obregonia1/articles/e82868e8f66793

おわりに

これで、らくらくリポジトリの移動ができるようになります!
システム開発を行っていると様々なプロジェクトに携わったり、1つのシステムで複数のリポジトリに分かれていることがあります。
そのリポジトリのパスを覚えるのはめんどくさいことだと思うので、ぜひ使ってみてください。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2